blob: d27a97b9023868a689e17e209b68500de573ea46 [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*
('extensions' extensionsFrom=INT 'to' (extensionsTo=INT | 'max') ';')?
'}'(';')?;
MessageElement:
Type | Field | ExtendMessage | Option;
Field:
Property | Group;
Group:
modifier=Modifier 'group' name=Name '=' index=INT
('[' (fieldOptions+=FieldOption (',' fieldOptions+=FieldOption)*) ']')? '{'
elements+=Property*
'}'(';')?;
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
| FloatRef
| StringRef;
LiteralRef:
literal=[Literal];
BooleanRef:
boolean=BOOL;
enum BOOL:
true
| false;
IntRef:
int=INT;
FloatRef:
float=FLOAT;
StringRef:
string=STRING;
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';
MessageReference:
type=[Message | QualifiedName];
terminal INT returns ecore::EInt: ('-')? ('0'..'9')+;
terminal FLOAT returns ecore::EFloat: ('-')? ('0'..'9')* ('.' ('0'..'9')+)?;