blob: 61a0f2c16eacd5402c1a8ddcbe90eb3a003b8520 [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)?
(elements+=ProtobufElement)*;
Syntax:
'syntax' '=' name=STRING ';';
ProtobufElement:
Package | Import | Option | Type | ExtendMessage | Service;
Package:
'package' name=QualifiedName ';';
Import:
'import' importURI=STRING ';';
QualifiedName:
'.'? Name ('.' Name)*;
Option:
'option' name=Name '=' value=ValueRef ';';
Type:
Message | Enum;
Message:
'message' name=Name '{'
elements+=MessageElement*
'}'(';')?;
MessageElement:
Extensions | Type | Field | ExtendMessage | Option;
Extensions:
'extensions' extensionsFrom=INT 'to' (extensionsTo=INT | 'max') ';';
Field:
Property | Group;
Group:
modifier=Modifier 'group' name=Name '=' index=INT
('[' (fieldOptions+=FieldOption (',' fieldOptions+=FieldOption)*) ']')? '{'
elements+=GroupElement*
'}'(';')?;
GroupElement:
Field | Option | Enum
;
Property:
modifier=Modifier type=AbstractTypeReference name=Name '=' index=INT
(
('[' 'default' '=' default=ValueRef ']') |
('[' 'default' '=' default=ValueRef (',' fieldOptions+=FieldOption)* ']') |
('[' fieldOptions+=FieldOption (',' fieldOptions+=FieldOption)* ']')
)? ';';
FieldOption:
name=Name '=' value=ValueRef;
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
| LongRef
| FloatRef
| DoubleRef
| StringRef
| Nan;
LiteralRef:
literal=[Literal];
BooleanRef:
boolean=BOOL;
enum BOOL:
true
| false;
IntRef:
int=INT;
LongRef:
long=LONG;
FloatRef:
float=FLOAT;
DoubleRef:
double=DOUBLE;
StringRef:
string=STRING;
Nan:
number='nan';
Enum:
'enum' name=Name '{'
literals+=Literal*
'}' ';'?;
Literal:
name=Name '=' index=INT ';';
ExtendMessage:
'extend' message=MessageReference '{'
elements+=MessageElement*
'}'(';')?;
Service:
'service' name=Name '{'
rpcs+=Rpc*
'}';
Rpc:
'rpc' name=Name '(' argType=MessageReference ')' 'returns' '(' returnType=MessageReference ')'
(('{' options+=Option* '}')(';')? | ';')
;
Name:
ID | 'package' | 'import' | 'option' | 'extend' | 'message' | 'optional' | 'required' | 'repeated' | 'group' | 'enum'
| 'service' | 'rpc' | 'returns' | 'true' | 'false' | 'default' | 'extensions' | 'to' | 'max' | 'double' | 'float'
| 'int32' | 'int64' | 'uint32' | 'uint64' | 'sint32' | 'sint64' | 'fixed32' | 'fixed64' | 'sfixed32' | 'sfixed64' |
'bool' | 'string' | 'bytes';
MessageReference:
type=[Message | QualifiedName];
terminal INT returns ecore::EInt: ('-')? ('0'..'9')+;
terminal LONG returns ecore::ELong: ('-')? ('0'..'9')+;
terminal FLOAT returns ecore::EFloat: ('-')? ('0'..'9')* ('.' ('0'..'9')+)?;
terminal DOUBLE returns ecore::EDouble: ('-')? ('0'..'9')* ('.' ('0'..'9')+)?;