forked from mafintosh/protocol-buffers-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
363 lines (298 loc) · 7.96 KB
/
parse.js
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
var tokenize = require('./tokenize')
var MAX_RANGE = 0x1FFFFFFF
var onfieldoptions = function(tokens) {
var opts = {}
while (tokens.length) {
switch(tokens[0]) {
case '[':
case ',':
tokens.shift()
var name = tokens.shift()
if (tokens[0] !== '=') throw new Error('Unexpected token in field options: '+tokens[0])
tokens.shift()
if (tokens[0] === ']') throw new Error('Unexpected ] in field option')
opts[name] = tokens.shift()
break
case ']':
tokens.shift()
return opts
default:
throw new Error('Unexpected token in field options: '+tokens[0])
}
}
throw new Error('No closing tag for field options')
}
var onfield = function(tokens) {
var param = []
var field = {
name: null,
type: null,
tag: 0,
required: false,
repeated: false,
options: {}
}
while (tokens.length) {
switch (tokens[0]) {
case '=':
tokens.shift()
field.tag = parseInt(tokens.shift(), 10)
break
case 'repeated':
case 'required':
case 'optional':
var t = tokens.shift()
field.required = t === 'required'
field.repeated = t === 'repeated'
field.type = tokens.shift()
field.name = tokens.shift()
break
case '[':
field.options = onfieldoptions(tokens)
break
case ';':
tokens.shift()
return field
default:
throw new Error('Unexpected token in message field: '+tokens[0])
}
}
throw new Error('No ; found for message field')
}
var onmessagebody = function(tokens) {
var body = {
enums: [],
messages: [],
fields: [],
oneof: [],
extensions: null
}
while (tokens.length) {
switch (tokens[0]) {
case 'repeated':
case 'optional':
case 'required':
body.fields.push(onfield(tokens))
break
case 'enum':
body.enums.push(onenum(tokens))
break
case 'message':
body.messages.push(onmessage(tokens))
break
case 'extensions':
body.extensions = onextensions(tokens)
break
case 'oneof':
body.oneof.push(ononeof(tokens))
break
default:
throw new Error('Unexpected token in message: '+tokens[0])
}
}
return body
}
var onextend = function(tokens) {
var out = {
name: tokens[1],
message: onmessage(tokens)
}
return out
}
var onextensions = function(tokens) {
tokens.shift()
var from = parseInt(tokens.shift(), 10)
if (isNaN(from)) throw new Error('Invalid from in extensions definition')
if (tokens.shift() !== 'to') throw new Error("Expected keyword 'to' in extensions definition")
var to = tokens.shift()
if (to === 'max') to = MAX_RANGE
to = parseInt(to, 10)
if (isNaN(to)) throw new Error('Invalid to in extensions definition')
if (tokens.shift() !== ';') throw new Error('Missing ; in extensions definition')
return {from: from, to: to}
}
var onmessage = function(tokens) {
tokens.shift()
var lvl = 1
var body = []
var msg = {
name: tokens.shift(),
enums: [],
oneof: [],
messages: [],
fields: []
}
if (tokens[0] !== '{') throw new Error('Expected { but found '+tokens[0])
tokens.shift()
while (tokens.length) {
if (tokens[0] === '{') lvl++
else if (tokens[0] === '}') lvl--
if (!lvl) {
tokens.shift()
var body = onmessagebody(body)
msg.enums = body.enums
msg.oneof = body.oneof
msg.messages = body.messages
msg.fields = body.fields
msg.extensions = body.extensions
return msg
}
body.push(tokens.shift())
}
if (lvl) throw new Error('No closing tag for message')
}
var onpackagename = function(tokens) {
tokens.shift()
var name = tokens.shift()
if (tokens[0] !== ';') throw new Error('Expected ; but found '+tokens[0])
tokens.shift()
return name
}
var onenumvalue = function(tokens) {
if (tokens.length < 4) throw new Error('Invalid enum value: '+tokens.slice(0, 3).join(' '))
if (tokens[1] !== '=') throw new Error('Expected = but found '+tokens[1])
if (tokens[3] !== ';') throw new Error('Expected ; but found '+tokens[1])
var name = tokens.shift()
tokens.shift()
var value = parseInt(tokens.shift(), 10)
tokens.shift()
return {
name: name,
value: value
}
}
var onenum = function(tokens) {
tokens.shift()
var e = {
name: tokens.shift(),
values: {}
}
if (tokens[0] !== '{') throw new Error('Expected { but found '+tokens[0])
tokens.shift()
while (tokens.length) {
if (tokens[0] === '}') {
tokens.shift()
// there goes optional semicolon after the enclosing "}"
if (tokens[0] === ';') tokens.shift()
return e
}
var val = onenumvalue(tokens)
e.values[val.name] = val.value
}
throw new Error('No closing tag for enum')
}
var ononeof = function(tokens) {
tokens.shift()
var oneof = {
name: tokens.shift(),
fields: []
}
if (tokens[0] !== '{') throw new Error('Expected { but found '+tokens[0])
tokens.shift()
while (tokens.length) {
if (tokens[0] === '}') {
tokens.shift()
return oneof
}
var field = {}
field.type = tokens.shift()
field.name = tokens.shift()
if (tokens[0] !== '=') throw new Error('Expected = but found '+tokens[0])
tokens.shift()
field.tag = parseInt(tokens.shift(), 10)
oneof.fields.push(field)
if (tokens[0] !== ';') throw new Error('Expected ; but found '+tokens[0])
tokens.shift()
}
throw new Error('No closing tag for oneof')
}
var onoption = function(tokens) {
var name = null
var value = null
var parse = function(value) {
if (value === 'true') return true
if (value === 'false') return false
return value.replace(/^"+|"+$/gm,'')
}
while (tokens.length) {
if (tokens[0] === ';') {
tokens.shift()
return {name:name, value:value}
}
switch (tokens[0]) {
case 'option':
tokens.shift()
name = tokens.shift()
break
case '=':
tokens.shift()
if (name === null) throw new Error('Expected key for option with value: '+tokens[0])
value = parse(tokens.shift())
if (name === 'optimize_for' && !/^(SPEED|CODE_SIZE|LITE_RUNTIME)$/.test(value)) {
throw new Error('Unexpected value for option optimize_for: '+value)
}
break
default:
throw new Error('Unexpected token in option: '+tokens[0])
}
}
}
var onimport = function(tokens) {
tokens.shift()
var file = tokens.shift().replace(/^"+|"+$/gm,'')
if (tokens[0] !== ';') throw new Error('Unexpected token: '+tokens[0]+'. Expected ";"')
tokens.shift()
return file
}
var parse = function(buf) {
var tokens = tokenize(buf.toString())
var schema = {
package: null,
imports: [],
enums: [],
messages: [],
options: {},
extends: []
}
while (tokens.length) {
switch (tokens[0]) {
case 'package':
schema.package = onpackagename(tokens)
break
case 'message':
schema.messages.push(onmessage(tokens))
break
case 'enum':
schema.enums.push(onenum(tokens))
break
case 'option':
var opt = onoption(tokens)
if (schema.options[opt.name]) throw new Error('Duplicate option '+opt.name)
schema.options[opt.name] = opt.value
break
case 'import':
schema.imports.push(onimport(tokens))
break
case 'extend':
schema.extends.push(onextend(tokens))
break
default:
throw new Error('Unexpected token: '+tokens[0])
}
}
//now iterate over messages and propagate extends
schema.extends.forEach(function(ext) {
schema.messages.forEach(function(msg) {
if (msg.name === ext.name) {
ext.message.fields.forEach(function(field) {
if (!msg.extensions || field.tag < msg.extensions.from || field.tag > msg.extensions.to) {
throw new Error(msg.name+" does not declare "+field.tag+" as an extension number")
}
msg.fields.push(field)
})
}
})
})
return schema
}
module.exports = parse