-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
367 lines (326 loc) · 8.41 KB
/
parser.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
364
365
366
jsio('import logging');
jsio('from net.later import Later');
var TYPES = ['arg', 'string', 'date', 'int', 'decimal', 'struct'];
var logger = logging.getLogger("pijo.parser");
//logger.setLevel(0);
var fs = null;
var readSource = function(filename) {
if (!posix) {
fs = jsio.__env.require('fs');
}
logger.debug('reading', filename);
return fs.cat(filename).wait()
}
exports.parse = function(filename, src) {
if (typeof(src) != 'string') {
// Get the source
src = readSource(filename);
}
p = new Parser(filename, src);
p.parse();
return p.file;
}
var commentRegex = /(.*)?\/\/.*?(\n|$)/g;
var tokenRegex = /(\".*?\"|\b\w+?\b|[,;(){}<>\[\]]|->|\n)/g;
var tokenize = function(raw) {
// Remove comments
raw = raw.replace(commentRegex, '$1$2');
// tokenize
return raw.match(tokenRegex);
}
var Parser = Class(function(supr) {
this.init = function(filename, src) {
this._src = src;
this._filename = filename;
this.file = new PijoFile(filename);
this._protocols = {}
this._structs = {}
this._customs = {}
this._tokens = tokenize(src);
this._line = 0;
}
this.makeError = function(msg) {
msg = !!msg ? msg : "Parse Error";
var e =new Error("Error parsing Pijo file " + this._filename + ", line " + this._line + ": " + msg)
e.tracked = true;
e.name = 'PijoParseError';
return e;
}
this._shiftExpected = function(expected, msg) {
if (expected.constructor !== Array) {
expected = [expected];
}
var token = this._shift();
for (var i = 0, compare; compare = expected[i]; ++i) {
if (compare == token) {
return token;
}
}
throw this.makeError(msg || 'Expected one of: "' + expected + '" not "' + token + '"');
}
this._shift = function() {
var token = this._tokens.shift();
logger.debug('shifted', token);
while (token == '\n') {
logger.debug('LINE', this._line);
this._line++;
token = this._tokens.shift();
}
return token;
}
this._shiftWord = function(msg) {
var token = this._shift();
if (token.match(/\w*/)[0].length != token.length) {
throw this.makeError(msg || 'Expected name, not "' + token + '"');
}
return token;
}
this._shiftInt = function(msg) {
var token = this._shiftWord(msg);
var value = this.parseInt(token);
if (isNaN(value)) {
throw this.makeError(msg || 'Expected int value, not "' + token + '"');
}
}
this._peek = function() {
var token = this._shift();
if (token !== undefined) {
this._tokens.unshift(token);
}
return token;
}
this._peekCompare = function(expected) {
if (!expected || expected.constructor !== Array) {
expected = [expected];
}
var token = this._peek();
logger.debug('token is', token);
for (var i = 0, compare; (compare = expected[i]) || i < expected.length; ++i) {
logger.debug('compare', token, compare);
if (compare == token) {
return true;
}
}
return false;
}
this.parse = function() {
this.state = 'top';
while (this.state != 'finished') {
// try {
this['state_' + this.state]();
// } catch(e) {
// if (!e.tracked) {
// throw this.makeError(e.message);
// }
// }
}
}
this._parseArgs = function(intoArgs, allowConditions) {
while (true) {
if (this._peekCompare('}')) {
return
}
var type = this._shiftExpected(TYPES);
var structName = null;
if (type == 'struct') {
this._shiftExpected('<')
structName = this._shiftWord()
this._shiftExpected('>')
}
var name = this._shiftWord();
this._shiftExpected(';');
conditions = [];
if (allowConditions && this._peekCompare('[')) {
this._shift()
while (true) {
var condition = [this._shiftWord()] // symbol
if (this._peekCompare('(')) {
this._shift();
while (true) {
condition.push(this._shiftWord()) // arg
if (this._shiftExpected([',', ')']) == ')') {
break;
}
}
}
conditions.push(condition);
if (this._shiftExpected([']', ',']) == ']') {
break
}
}
}
intoArgs.addArg(new PijoArg(name, type, conditions, structName))
}
}
this.state_top = function() {
if (this._peekCompare(undefined)) {
this.state = 'finished'
return;
}
var type = this._shiftExpected(['protocol', 'struct']);
this.state = 'top_' + type;
}
this.state_top_struct = function() {
this._shiftExpected('<');
var name = this._shiftWord();
this._shiftExpected('>');
var struct = new PijoStruct(name);
this.file.addStruct(struct);
this._shiftExpected('{');
this._parseArgs(struct.args);
this._shiftExpected('}');
this.state = 'top';
}
this.state_top_protocol = function() {
var name = this._shiftWord();
this._protocol = new PijoProtocol(name);
this.file.addProtocol(this._protocol)
this._shiftExpected('{')
this.state = 'protocol_block'
while (this.state.indexOf('protocol') == 0) {
this['state_' + this.state]();
}
delete this._protocol;
this._shiftExpected('}')
this.state = 'top'
}
this.state_protocol_block = function() {
if (this._peekCompare('}')) {
this.state = 'done';
return;
}
var type = this._shiftExpected(['rpc', 'event']);
this._direction = null;
if (this._peekCompare('->')) {
this._shift();
this._direction = this._shiftExpected(['server', 'client']);
}
this.state = 'protocol_' + type;
this['state_' + this.state]();
delete this._direction;
this._shiftExpected('}');
this.state = 'protocol_block'
// this.state = 'done';
}
this.state_protocol_rpc = function() {
var name = this._shiftWord();
var rpc = new PijoRPC(name, this._direction);
this._protocol.addRpc(rpc);
this._shiftExpected('{');
while (!this._peekCompare('}')) {
var type = this._shiftExpected(['request', 'response']);
if (rpc[type]) {
throw this.makeError("Redefinition of RPC " + name + ' ' + type);
}
var item = null;
if (type == 'request') {
item = rpc.request = new PijoRPCRequest();
}
else {
item = rpc.response = new PijoRPCResponse();
}
this._shiftExpected('{');
this._parseArgs(item.args, true);
this._shiftExpected('}');
}
}
this.state_protocol_event = function() {
var name = this._shiftWord();
var event = new PijoEvent(name, this._direction);
this._protocol.add_event(event);
this._parseArgs(event.args, true);
}
});
var PijoFile = Class(function() {
this.init = function(name) {
this.structs = {}
this.protocols = {}
}
this.addStruct = function(struct) {
if (struct.name in this.structs) {
throw new Error("Redefinition of struct " + struct.name);
}
this.structs[struct.name] = struct;
}
this.addProtocol = function(protocol) {
if (protocol.name in this.protocols) {
throw new Error("Redefinition of protocol " + protocol.name);
}
this.protocols[protocol.name] = protocol;
}
})
var PijoStruct = Class(function() {
this.init = function(name) {
this.args = new PijoArgGroup();
this.name = name;
}
})
var PijoProtocol = Class(function() {
this.init = function(name) {
this.name = name
this.rpcs = {}
this.events = {}
}
this.addRpc = function(rpc) {
if (rpc.name in this.rpcs) {
throw new Error("redefinition of rpc " + rpc.name);
}
if (rpc.name in this.events) {
throw new Error("conficting event and rpc definitions of " + rpc.name);
}
this.rpcs[rpc.name] = rpc;
}
this.addEvent = function(event) {
if (event.name in this.events) {
throw new Error("redefinition of rpc " + event.name);
}
if (event.name in this.events) {
throw new Error("conficting event and rpc definitions of " + event.name);
}
this.events[event.name] = event;
}
});
var PijoRPC = Class(function() {
this.init = function(name, direction) {
this.name = name;
this.direction = direction || null;
this.request = null;
this.response = null;
}
});
var PijoArgGroup = Class(function() {
this.init = function() {
this.args = {};
}
this.addArg = function(arg) {
if (arg.name in this.args) {
throw new Error("redefinition of argument " + arg.name)
}
this.args[arg.name] = arg;
}
});
var PijoArg = Class(function() {
this.init = function(name, type, conditions, struct_name) {
this.name = name
this.type = type
this.conditions = conditions
this.struct_name = struct_name || null;
}
});
var PijoRPCRequest = Class(function() {
this.init = function() {
this.args = new PijoArgGroup()
}
});
var PijoRPCResponse = Class(function() {
this.init = function() {
this.args = new PijoArgGroup()
}
});
var PijoEvent = Class(function() {
this.init = function(name, direction) {
this.name = name
this.direction = direction || null;
this.args = {}
}
});