-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto-specification.ebnf
175 lines (146 loc) · 4.91 KB
/
proto-specification.ebnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(*
operators:
- | alternating
- () grouping
- [] option (zero or one time)
- {} repetition
*)
(* Basic Element *)
letter = "A" ... "Z" | "a" ... "z"
decimalDigit = "0" ... "9"
octalDigit = "0" ... "7"
hexDigit = "0" ... "9" | "A" ... "F" | "a" ... "f"
(* Identifiers *)
ident = letter { letter | decimalDigit | "_" }
fullIdent = ident { "." ident }
messageName = ident
enumName = ident
fieldName = ident
oneofName = ident
mapName = ident
serviceName = ident
rpcName = ident
messageType = [ "." ] { ident "." } messageName
enumType = [ "." ] { ident "." } enumName
(* Integer Literals *)
decimalLit = ( "1" … "9" ) { decimalDigit }
octalLit = "0" { octalDigit }
hexLit = "0" ( "x" | "X" ) hexDigit { hexDigit }
intLit = decimalLit | octalLit | hexLit
(* Floating-point Literals *)
decimals = decimalDigit { decimalDigit }
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals
floatLit = ( decimals "." [ decimals ] [ exponent ] | decimals exponent | "."decimals [ exponent ] ) | "inf" | "nan"
(* Boolean *)
boolLit = "true" | "false"
(* String Literals *)
charEscape = '\\' ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | '\\' | "'" | '"' )
hexEscape = '\\' ( "x" | "X" ) hexDigit hexDigit
octEscape = '\\' octalDigit octalDigit octalDigit
charValue = hexEscape | octEscape | charEscape | /[^\0\n\\]/
strLit = ( "'" { charValue } "'" ) | ( '"' { charValue } '"' )
(* EmptyStatement Datatype *)
emptyStatement = ";"
(* Constant *)
constant = fullIdent | ( [ "-" | "+" ] intLit ) | ( [ "-" | "+" ] floatLit ) | strLit | boolLit
(* Syntax Statement*)
syntax = "syntax" "=" ("'" "proto3" "'" | '"' "proto3" '"') ";"
(* Example: stntax = "proto3" *)
(* Import Statement *)
import = "import" [ "weak" | "public" ] strLit ";"
(* Example: import public "other.proto"; *)
(* Package *)
package = "package" fullIdent ";"
(* Example: package foo.bar; *)
(* Option *)
optionName = ( ident | "(" fullIdent ")" ) { "." ident }
option = "option" optionName "=" constant ";"
(* Fields *)
type = "double" | "float" | "int32" | "int64" | "uint32" | "uint64"
| "sint32" | "sint64" | "fixed32" | "fixed64" | "sfixed32" | "sfixed64"
| "bool" | "string" | "bytes" | messageType | enumType
fieldNumber = intLit;
(* Normal Field *)
fieldOption = optionName "=" constant
fieldOptions = fieldOption { "," fieldOption }
field = [ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
(* Oneof and Oneof Field *)
oneof = "oneof" oneofName "{" { option | oneofField | emptyStatement } "}"
oneofField = type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
(* Example:
oneof foo {
string name = 4;
SubMessage sub_message = 9;
}
*)
(* Map Field *)
keyType = "int32" | "int64" | "uint32" | "uint64" | "sint32" | "sint64" |"fixed32" | "fixed64" | "sfixed32" | "sfixed64" | "bool" | "string"
mapField = "map" "<" keyType "," type ">" mapName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
(* Example: map<string, Project> projects = 3;*)
(* Reserved *)
range = intLit [ "to" ( intLit | "max" ) ]
ranges = range { "," range }
strFieldName = "'" fieldName "'" | '"' fieldName '"'
strFieldNames = strFieldName { "," strFieldName }
reserved = "reserved" ( ranges | strFieldNames ) ";"
(* example:
reserved 2, 15, 9 to 11;
reserved "foo", "bar";
*)
(* Enum Definition *)
enumValueOption = optionName "=" constant
enumField = ident "=" [ "-" ] intLit [ "[" enumValueOption { "," enumValueOption } "]" ]";"
enumBody = "{" { option | enumField | emptyStatement | reserved } "}"
enum = "enum" enumName enumBody
(* Example:
enum EnumAllowingAlias {
option allow_alias = true;
EAA_UNSPECIFIED = 0;
EAA_STARTED = 1;
EAA_RUNNING = 2 [(custom_option) = "hello world"];
}
*)
(* Message Definition *)
messageBody = "{" { field | enum | message | option | oneof | mapField |reserved | emptyStatement } "}"
message = "message" messageName messageBody
(* Example:
message Outer {
option (my_option).a = true;
message Inner { // Level 2
int64 ival = 1;
}
map<int32, string> my_map = 2;
}
*)
(* Service Definition *)
rpc = "rpc" rpcName "(" [ "stream" ] messageType ")" "returns" "(" [ "stream" ]messageType ")" (( "{" {option | emptyStatement } "}" ) | ";")
service = "service" serviceName "{" { option | rpc | emptyStatement } "}"
(* Example:
service SearchService {
rpc Search (SearchRequest) returns (SearchResponse);
}
*)
(* Proto File *)
proto = syntax { import | package | option | topLevelDef | emptyStatement }
topLevelDef = message | enum | service
(* Example:
syntax = "proto3";
import public "other.proto";
option java_package = "com.example.foo";
enum EnumAllowingAlias {
option allow_alias = true;
EAA_UNSPECIFIED = 0;
EAA_STARTED = 1;
EAA_RUNNING = 1;
EAA_FINISHED = 2 [(custom_option) = "hello world"];
}
message Outer {
option (my_option).a = true;
message Inner { // Level 2
int64 ival = 1;
}
repeated Inner inner_message = 2;
EnumAllowingAlias enum_field = 3;
map<int32, string> my_map = 4;
}
*)