blob: d72264ce8316953da05cfaa73ca746e5b9322160 [file] [log] [blame]
/*
* Copyright (c) 2011 Google Inc.
*
* All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
* Public License v1.0 which accompanies this distribution, and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*
* Author: alruiz@google.com (Alex Ruiz)
*/
grammar com.google.eclipse.protobuf.Protobuf with org.eclipse.xtext.common.Terminals
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate protobuf "http://www.google.com/eclipse/protobuf/Protobuf"
Protobuf:
(syntax=Syntax)?
((package=Package)? & (imports+=Import)* & (options+=Option)*)
(elements+=ProtobufElement)*;
Syntax:
'syntax' '=' name=STRING ';';
Package:
'package' name=QualifiedName ';';
Import:
'import' importURI=STRING ';';
QualifiedName:
ID ('.' ID)*;
Option:
'option' name=ID '=' value=ValueRef ';';
ProtobufElement:
Type | ExtendMessage | Service;
Type:
Message | Enum;
Message:
'message' name=ID '{'
elements+=MessageElement*
('extensions' extensionsFrom=INT 'to' (extensionsTo=INT | 'max') ';')?
'}'(';')?;
MessageElement:
Type | Property | Group | ExtendMessage;
Group:
modifier=Modifier 'group' name=ID '=' index=INT ('[' 'deprecated' '=' deprecated=BooleanRef ']')? '{'
elements+=Property*
'}'(';')?;
Property:
modifier=Modifier type=AbstractTypeReference name=ID '=' index=INT (('[' 'default' '=' default=ValueRef
']') | ('[' 'packed' '=' packed=BooleanRef ']') | ('[' 'deprecated' '=' deprecated=BooleanRef ']')
| ('[' 'default' '=' default=ValueRef ',' 'deprecated' '=' deprecated=BooleanRef ']') )? ';';
enum Modifier:
required
| optional
| repeated;
AbstractTypeReference:
ScalarTypeReference | TypeReference;
ScalarTypeReference:
scalar=ScalarType;
enum ScalarType:
double
| float
| int32
| int64
| uint32
| uint64
| sint32
| sint64
| fixed32
| fixed64
| sfixed32
| sfixed64
| bool
| string
| bytes;
TypeReference:
type=[Type | QualifiedName];
ValueRef:
LiteralRef
| BooleanRef
| IntRef
| FloatRef
| StringRef;
LiteralRef:
literal=[Literal];
BooleanRef:
boolean=BOOL;
enum BOOL:
true
| false;
IntRef:
int=INT;
FloatRef:
float=FLOAT;
StringRef:
string=STRING;
Enum:
'enum' name=ID '{'
literals+=Literal*
'}' ';'?;
Literal:
name=ID '=' index=INT ';';
ExtendMessage:
'extend' message=MessageReference '{'
elements+=MessageElement*
'}';
Service:
'service' name=ID '{'
rpcs+=Rpc*
'}';
Rpc:
'rpc' name=ID '(' argType=MessageReference ')' 'returns' '(' returnType=MessageReference ')' (('{' '}') | ';')
;
MessageReference:
type=[Message | QualifiedName];
terminal INT returns ecore::EInt: ('-')? ('0'..'9')+;
terminal FLOAT returns ecore::EFloat: ('-')? ('0'..'9')* ('.' ('0'..'9')+)?;