-
Notifications
You must be signed in to change notification settings - Fork 8
/
proto.peg
217 lines (193 loc) · 6.27 KB
/
proto.peg
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
proto = z:ws? a:syntax b:(import / package / option / emptyStatement / message / enum / service / ws)*
{
var arr = {syntax: a.syntax};
arr.content = b.filter(function(a){return a !== undefined});
return arr;
}
slcomment = "//" (!("\r\n" / "\n") .)* ("\n" / "\r\n")
{}
mlcomment = "/*" (!"*/" .)* "*/"
{}
comment = ( slcomment / mlcomment )
{}
rws = [ \r\n\t] / comment // Real white space
{}
ws = rws+
{}
letter = [A-Za-z]
decDigit = [0-9]
octDigit = [0-7]
hexDigit = [0-9A-Fa-f]
ident = a:letter b:(letter / decDigit / "_")*
{ return a+b.join("") }
fullIdent = a:ident b:("." ident)*
{ b.unshift([a]); return b.map(function(c){return c.join('')}).join('') }
messageName = ident
enumName = ident
fieldName = ident
oneofName = ident
mapName = ident
serviceName = ident
rpcName = ident
messageType = a:"."? b:(ident ".")* c:messageName
{ return (a == '.' ? '.' : '')+b.map(function(a){return a.join('')}).join('')+c }
enumType = a:"."? b:(ident ".")* c:enumName
{ return (a == '.' ? '.' : '')+b.map(function(a){return a.join('')}).join('')+c }
intLit = decimalLit / octalLit / hexLit
decimalLit = a:("0" / [1-9] decDigit*)
{ return parseInt(a[0]+ (a[1] ? a[1].join('') : '')) }
octalLit = "0" a:octDigit+
{ return parseInt(a, 8) }
hexLit = "0" [xX] a:hexDigit+
{ return parseInt(a, 16) }
floatLit = (decimals "." decimals? exponent?) / (decimals exponent) / ("." decimals exponent? ) / "inf" / "nan"
decimals = decDigit+
exponent = [eE]? [+-]? decimals
boolLit = "true" / "false"
strLit = obj:( "'" charValue* "'" ) / obj:( '"' charValue* '"' )
{ return obj[1].join('') }
charValue = hexEscape / octEscape / charEscape / [^\0\n\\\"\']
hexEscape = '\\' [xX] hexDigit hexDigit
octEscape = '\\' octDigit octDigit octDigit
quote = [\"\']
charEscape = '\\' [abfnrtv\"\'\\]
emptyStatement = ";"
{}
constant = strLit / fullIdent / ( [-+]? intLit ) / ( [-+]? floatLit ) / boolLit
syntax = "syntax" ws? "=" ws? quote val:("proto2" / "proto3") quote ws? ";"
{ return { type: "syntax", syntax: val } }
import = "import" ws? mod:("weak" / "public")? ws? pkg:strLit ws? ";"
{ return {type: "import", package: pkg, modifier: mod } }
package = "package" ws? val:fullIdent ws? ";"
{ return { type: "package", package: val } }
option = "option" ws? name:optionName ws? "=" ws? val:constant ";"
{ return { type: "option", name: name, val: val } }
optionName = a:( ident / "(" fullIdent ")" ) c:("." ident)*
{ if(Array.isArray(a)) a = a.join(''); return a+(c[0] ? c[0].join('') : "") }
type = "double" / "float" / "int32" / "int64" / "uint32" / "uint64" / "sint32" /
"sint64" / "fixed32" / "fixed64" / "sfixed32" / "sfixed64" / "bool" /
"string" / "bytes" / messageType / enumType
fieldNumber = intLit
fieldOptionsParam = "[" ws? a:fieldOptions ws? "]"
{
var opts = {};
a.map(function(a){
if(typeof a == 'object' && a != null)
opts[a.name] = a.val;
});
return opts;
}
field = a:"repeated"? ws? b:type ws? c:fieldName ws? "=" ws? d:fieldNumber ws? e:(fieldOptionsParam)? ws? ";"
{
return {
type: "field",
repeated: a != null,
typename: b,
name: c,
fieldNo: d,
opts: e == null ? {} : e
}
}
fieldOptions = a:fieldOption ws? b:("," ws? fieldOption ws?)*
{ var arr = [a]; b.map(function(elem){arr.push(elem)}); return arr; }
fieldOption = name:optionName ws? "=" ws? val:constant
{ return {name: name, val: val} }
oneof = "oneof" ws? b:oneofName ws? "{" c:( ws / oneofField / emptyStatement)+ "}"
{
var arr = [];
c.map(function(a){
if(a && a.type)
arr.push(a);
});
return { type: "oneof", name: b, content: arr }
}
oneofField = b:type ws? c:fieldName ws? "=" ws? d:fieldNumber ws? e:(fieldOptionsParam)? ws? ";"
{
return {
type: "field",
typename: b,
name: c,
fieldNo: d,
opts: e == null ? {} : e
}
}
mapField = "map" ws? "<" ws? a:keyType ws? "," ws? b:type ws? ">" ws? c:mapName ws? "=" ws?
d:fieldNumber ws? e:(fieldOptionsParam)? ws? ";"
{
return {
type: "field",
typename: "map",
key: a,
value: b,
name: c,
fieldNo: d,
opts: e == null ? {} : e
}
}
keyType = "int32" / "int64" / "uint32" / "uint64" / "sint32" / "sint64" /
"fixed32" / "fixed64" / "sfixed32" / "sfixed64" / "bool" / "string"
reserved = "reserved" ws ( ranges / fieldNames ) ";"
fieldNames = fieldName ("," fieldName)*
ranges = range ( ws? "," ws? (range) )*
range = intLit ws "to" ws intLit / intLit
enum = "enum" ws? a:enumName ws? b:enumBody
{ return {type: 'enum', name: a, content: b.content, opts: b.opts } }
enumBody = "{" a:(ws / option / enumField / emptyStatement)* "}"
{
var opts = {};
var content = [];
for(var elem of a){
if(!elem)
continue;
else if(elem.type == 'enumField')
content.push(elem);
else if(elem.type == 'option')
opts[elem.name] = elem.val;
}
return {content: content, opts: opts};
}
enumField = a:ident ws? "=" ws? b:intLit ws? c:(fieldOptionsParam)? ws? ";"
{
return { type: 'enumField', name: a, val: b, opts: c == null ? {} : c };
}
message = "message" ws a:messageName ws? b:messageBody
{ return {type: 'message', name: a, content: b.content, opts: b.opts } }
messageBody = "{" a:( field / enum / message / option / oneof / mapField /reserved / emptyStatement / ws )* "}"
{
var opts = {};
var content = [];
for(var elem of a){
if(!elem)
continue;
else if(elem.type == 'option')
opts[elem.name] = elem.val;
else
content.push(elem);
}
return {content: content, opts: opts};
}
service = "service" ws a:serviceName ws? "{" b:( ws / option / rpc / emptyStatement )* "}"
{
var opts = {};
var content = [];
for(var elem of b){
if(!elem)
continue;
else if(elem.type == 'option')
opts[elem.name] = elem.val;
else
content.push(elem);
}
return {type: "service", name: a, content: content, opts: opts};
}
rpc = "rpc" ws a:rpcName ws? "(" ws? b:("stream" ws)? c:messageType ws? ")" ws? "returns" ws? "(" ws? d:("stream" ws)? e:messageType ws? ")" ws? f:(rpcBody / ";")
{
if(b && b[0] == "stream")
c.stream = true;
if(d && d[0] == "stream")
e.stream = true;
if( f == ";" )
f = {};
return { type: "rpc", name: a, param: c, returns: e, opts: f }
}
rpcBody = ( "{" (option / emptyStatement / ws )* "}" )