| /* |
| * 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: |
| RegularImport | PublicImport; |
| |
| RegularImport: |
| 'import' importURI=STRING ';'; |
| |
| PublicImport: |
| 'import' 'public' importURI=STRING ';'; |
| |
| QualifiedName: |
| '.'? Name ('.' Name)*; |
| |
| Option: |
| BuiltInOption | CustomOption; |
| |
| BuiltInOption: |
| 'option' name=Name '=' value=ValueRef ';'; |
| |
| CustomOption: |
| 'option' '(' name=QualifiedName ')' '=' value=ValueRef ';'; |
| |
| Type: |
| Message | Enum; |
| |
| Message: |
| 'message' name=Name '{' |
| elements+=MessageElement* |
| '}' (';')?; |
| |
| MessageElement: |
| Extensions | Type | Field | ExtendMessage | Option; |
| |
| Extensions: |
| 'extensions' ranges+=Range (',' ranges+=Range)* ';'; |
| |
| Range: |
| from=INT ('to' to=RangeMax)?; |
| |
| RangeMax: |
| INT | 'max'; |
| |
| Field: |
| Property | Group; |
| |
| Group: |
| modifier=Modifier 'group' name=Name '=' index=(INT | HEX) |
| ('[' (fieldOptions+=FieldOption (',' fieldOptions+=FieldOption)*) ']')? '{' |
| elements+=GroupElement* |
| '}' (';')?; |
| |
| GroupElement: |
| Field | Option | Enum; |
| |
| Property: |
| modifier=Modifier type=AbstractTypeRef name=Name '=' index=(INT | HEX) |
| ('[' fieldOptions+=FieldOption (',' fieldOptions+=FieldOption)* ']')? ';'; |
| |
| enum Modifier: |
| required |
| | optional |
| | repeated; |
| |
| AbstractTypeRef: |
| ScalarTypeRef | TypeRef; |
| |
| ScalarTypeRef: |
| scalar=ScalarType; |
| |
| enum ScalarType: |
| double | float | int32 | int64 | uint32 | uint64 | sint32 | sint64 | fixed32 | fixed64 | sfixed32 | sfixed64 | bool | |
| string | bytes; |
| |
| TypeRef: |
| type=[Type|QualifiedName]; |
| |
| ValueRef: |
| LiteralRef | BooleanRef | NumberRef | StringRef | Nan; |
| |
| LiteralRef: |
| literal=[Literal]; |
| |
| BooleanRef: |
| boolean=BOOL; |
| |
| enum BOOL: |
| true |
| | false; |
| |
| NumberRef: |
| HexRef | IntRef | LongRef | FloatRef | DoubleRef; |
| |
| HexRef: |
| hex=HEX; |
| |
| IntRef: |
| int=INT; |
| |
| terminal INT returns ecore::EInt: |
| SIGNED_INT; |
| |
| LongRef: |
| long=LONG; |
| |
| terminal LONG returns ecore::ELong: |
| SIGNED_INT; |
| |
| terminal SIGNED_INT: |
| ('-')? (NUMBER)+; |
| |
| FloatRef: |
| float=FLOAT; |
| |
| terminal FLOAT returns ecore::EFloat: |
| DECIMAL; |
| |
| DoubleRef: |
| double=DOUBLE; |
| |
| terminal DOUBLE returns ecore::EDouble: |
| DECIMAL; |
| |
| terminal DECIMAL: |
| ('-')? (NUMBER)* ('.' (NUMBER)+)?; |
| |
| StringRef: |
| string=STRING; |
| |
| terminal STRING: |
| SL_STRING (SL_STRING)*; |
| |
| terminal SL_STRING: |
| '"' ('\\' ('b' | 't' | 'n' | 'f' | 'r' | 'u' | '"' | "'" | '\\') | !('\\' | '"'))* '"' (WS)*; |
| |
| Nan: |
| number='nan'; |
| |
| Enum: |
| 'enum' name=Name '{' |
| elements+=EnumElement* |
| '}' ';'?; |
| |
| EnumElement: |
| Option | Literal; |
| |
| Literal: |
| name=Name '=' index=(INT | HEX) |
| ('[' fieldOptions+=FieldOption (',' fieldOptions+=FieldOption)* ']')? ';'; |
| |
| FieldOption: |
| BuiltInFieldOption | CustomFieldOption; |
| |
| BuiltInFieldOption: |
| name=Name '=' value=ValueRef; |
| |
| CustomFieldOption: |
| '(' name=QualifiedName ')' '=' value=ValueRef; |
| |
| terminal HEX returns ecore::EInt: |
| '0x' (NUMBER | 'a'..'f' | 'A'..'F')+; |
| |
| terminal NUMBER: |
| '0'..'9'; |
| |
| ExtendMessage: |
| 'extend' message=MessageRef '{' |
| elements+=MessageElement* |
| '}' (';')?; |
| |
| Service: |
| 'service' name=Name '{' |
| rpcs+=Rpc* |
| '}' (';')?; |
| |
| Rpc: |
| 'rpc' name=Name '(' argType=MessageRef ')' 'returns' '(' returnType=MessageRef ')' |
| (('{' options+=Option* '}') (';')? | ';'); |
| |
| Name: |
| ID | 'package' | 'import' | 'public' | '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'; |
| |
| MessageRef: |
| type=[Message|QualifiedName]; |
| |