-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
725 lines (607 loc) · 15.3 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/**
Version 0.0.1
*/
/*
This parser is based on the work of Douglas
Crockford, as described on his web page [1].
I intend to modify and extend it for funzies.
Note: All comments are written by Nick Gideo
in less otherwise attributed (ie: -DC is from
Douglas Crockford).
Top Down Operator Precedence (TDOP) is a type of
parser that is based on a Recursive descent model,
but it differs by treating the tokens as something
simmilar to objects. Recursive descent associates
'semantic actions' with grammer rules, while TDOP
has the actions associated with the tokens [2].
This feature allows for TDOP to be implemented
very well by a dynamic functional-object-oriented
language like JavaScript. Generic objects are first
created by a factory function, and methods can then
be dynamically assigned to allow for the token to
handle its own parsing logic.
References:
[1] http://javascript.crockford.com/tdop/tdop.html
[2] eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing/
*/
/*
"Transform a token object into an exception object and throw it." -DC
Crockford uses this error method throughout the his original parser code,
and within his tokenizer function. -Nick
*/
Object.prototype.error = function (message, t) {
t = t || this;
t.name = "SyntaxError";
t.message = message;
throw t;
};
const tokenizer = require("./tokenizer.js");
/*
Crockford originally added the tokenizer to the String
prototype, so I've imported the function and added it here.
*/
String.prototype.tokens = tokenizer;
/*
Create the NodeJs module by using an
immediately-invoked function closure.
In other words, the parser is accessed
by a function which the parser function
itself returns. Very meta, like whoa!
*/
module.exports = (function() {
// Holds current token. It is assigned by advance().
let token;
// Holds an array of token objects. It is assigned by the tokenizer.
let tokens;
// Index of the current token in tokens[]. It is incremented within advance()
let token_nr;
// holds the current scope.
let scope;
// All Symbols are kept in the symbol table
let symbol_table = {};
/*
This object serves as the Prototype for all tokens, which are created by
advance(). Each symbol has a unique token object with methods that handle
parsing operator precedence. The methods provided here are intented to be
re-assigned at token-definition time.
original_symbol#nud Stands for Null Denotation. The nud method is used by
prefix operators and values (ie variables and literals). When a variable
token is defined, its nud() method is reassigned to the static-method
itself(). If an incorrect token is parsed (like a spelling error), this
#nud() will be called to throw the "Undefined" error.
original_symbol#led Stands for Left Denotation. The led method is used
by infix and suffix operators.
*/
let original_symbol = {
/** @abstract*/
nud: function() {
this.error("Undefined.");
},
/** @abstract*/
led: function() {
this.error("Missing operator.");
}
};
/**
Advances to the next token in the token array, and places a newly created
token object in the place of the 'global' token variable.
@param {string} id - optional. Tests that current token has the same id.
*/
let advance = function(id) {
let a, o, t, v;
let l, c; /* adding line and column info -NZG */
if (id && token.id !== id) {
token.error("Expected '" + id + "'.");
}
if (token_nr >= tokens.length) {
token = symbol_table["(end)"];
return;
}
t = tokens[token_nr];
token_nr += 1;
v = t.value;
a = t.type;
l = t.line; // line number from source
c = t.from; // column number from source -NZG
if (a === "name") {
o = scope.find(v);
} else if (a === "operator") {
o = symbol_table[v];
if (!o) {
t.error("Unknown operator.");
}
} else if (a === "string" || a === "number") {
a = "literal";
o = symbol_table["(literal)"];
} else {
t.error("Unexpected token.");
}
token = Object.create(o);
token.value = v;
token.arity = a;
token.source = {}; //Tracking info from source code -NZG
token.source.line = t.line;
token.source.column = t.column;
return token;
};
/**
The symbol creating factory function.
It checks to see if the symbol object
already exists in the symbol_table, and
if so it potentially updates the binding
power. If the symbol doesn't exist, it creates
one and puts it in the symbol_table object.
@param {string} id - symbol. ie: "("
@param {number} bp - binding power
@return {object} s - new symbol
*/
let symbol = function(id, bp){
let s = symbol_table[id];
bp = bp || 0;
if (s) {
if (bp >= s.lbp) {
s.lbp = bp;
}
} else {
s = Object.create(original_symbol);
s.id = s.value = id;
s.lbp = bp;
symbol_table[id] = s;
}
return s;
};
/*
The original_scope object
*/
let original_scope = {
define: function(n) {
let t = this.def[n.value];
if (typeof t === "object") {
n.error(t.reserved ?
"Already reserved." :
"Already defined.");
}
this.def[n.value] = n;
n.reserved = false;
n.nud = itself;
n.led = null;
n.std = null;
n.lbp = 0;
n.scope = scope;
return n;
},
find: function(n) {
let e = this;
while (true) {
var o = e.def[n];
if (o) {
return o;
}
e = e.parent;
if (!e) {
return symbol_table[
symbol_table.hasOwnProperty(n) ?
n : "(name)"];
}
}
},
pop: function() {
scope = this.parent;
},
reserve: function(n) {
if (n.arity !== "name" || n.reserved) {
return;
}
let t = this.def[n.value];
if (t) {
if (t.reserved) {
return;
}
if (t.arity === "name") {
n.error("Already defined.");
}
}
this.def[n.value] = n;
n.reserved = true;
}
};
let new_scope = function(){
let s = scope;
scope = Object.create(original_scope);
scope.def = {};
scope.parent = s;
return scope;
}
let expression = function(rbp) {
let left;
let t = token;
advance();
left = t.nud();
while (rbp < token.lbp) {
t = token;
advance();
left = t.led(left);
}
return left;
};
/*
Identity function
*/
let itself = function() {
return this;
};
let infix = function(id, bp, led) {
let s = symbol(id, bp);
s.led = led || function(left) {
this.first = left;
this.second = expression(bp);
this.arity = "binary";
return this;
};
return s;
};
let infixr = function(id, bp, led) {
let s = symbol(id, bp);
s.led = led || function(left) {
this.first = left;
this.second = expression(bp -1);
this.arity = "binary";
return this;
};
return s;
};
let prefix = function(id, nud){
let s = symbol(id);
s.nud = nud || function() {
scope.reserve(this);
this.first = expression(80);
this.arity = "unary";
return this;
};
return s;
};
let assignment = function(id) {
return infixr(id, 10, function(left) {
if (left.id !== "." && left.id !== "[" &&
left.arity !== "name") {
left.error("Bad lvalue.");
}
this.first = left;
this.second = expression(9);
this.assignment = true;
this.arity = "binary";
return this;
});
};
let constant = function(s,v) {
let x = symbol(s);
x.nud = function(){
scope.reserve(this);
this.value = symbol_table[this.id].value;
this.arity = "literal";
return this;
};
x.value = v;
return x;
};
let statement = function() {
let n = token, v;
if (n.std) {
advance();
scope.reserve(n);
return n.std();
}
v = expression(0);
if (!v.assignment && v.id !== "(") {
v.error("Bad expression statement.");
}
advance(";");
return v;
};
let statements = function() {
let a = [], s;
while (true){
if (token.id === "}" || token.id === "(end)") {
break;
}
s = statement();
if (s) {
a.push(s);
}
}
return a.length === 0 ? null : a.length === 1 ? a[0] : a;
};
let stmt = function(s, f) {
let x = symbol(s);
x.std = f;
return x;
};
let block = function() {
let t = token;
advance("{");
return t.std();
};
/* Symbol Initialization */
/*
symbol() takes a sting for the token's text character
and an optional binding-power value. If not provided,
the binding-power defaults to 0.
*/
symbol(':');
symbol(';');
symbol(',');
symbol(')');
symbol(']');
symbol('}');
symbol('else');
symbol('(end)');
symbol('(name)');
symbol('(literal)').nud = itself;
symbol('this').nud = function() {
scope.reserve(this);
this.arity = 'this';
return this;
};
/* Infix operators */
infix("+", 60);
infix("-", 60);
infix("*", 70);
infix("/", 70);
infix("===", 50);
infix("!==", 50);
infix("<", 50);
infix("<=", 50);
infix(">", 50);
infix(">=", 50);
/* Ternary operator */
infix("?", 20, function(left) {
this.first = left;
this.second = expression(0);
advance(":");
this.third = expression(0);
this.arity = "ternary";
return this;
});
infix(".", 90, function(left) {
this.first = left;
if (token.arity !== "name") {
token.error("Expected a property name.");
}
token.arity = "literal";
this.second = token;
this.arity = "binary";
advance();
return this;
});
infix("[", 90, function(left) {
this.first = left;
this.second = expression(0);
this.arity = "binary";
advance("]");
return this;
});
infixr("&&", 40);
infixr("||", 40);
/* Prefix operators */
prefix("-");
prefix("!");
prefix("typeof");
/*
"The '(' token does not become part of the parse tree because
the nud returns the expression." -DC
By calling expression() with 0 as the right binding-power,
it effectively allows all of the expressions with
the parens to be gathered into a sub-tree. After this is completed,
advance() is called with ")", which logically
checks that the current token is the closing paren.
*/
prefix("(", function() {
let e = expression(0);
advance(")");
return e;
});
/* Assignment operators */
assignment("=");
assignment("+=");
assignment("-=");
/* Constants */
constant("true", true);
constant("false", false);
constant("null", null);
constant("pi", 3.141592653589793);
/* Statements */
stmt("{", function(){
new_scope();
let a = statements();
advance("}");
scope.pop();
return a;
});
stmt("var", function(){
let a = [], n, t;
while (true) {
n = token;
if (n.arity !== "name"){
n.error("Expected a new variable name.");
}
scope.define(n);
advance();
if (token.id === "=") {
t = token;
advance("=");
t.first = n;
t.second = expression(0);
t.arity = "binary";
a.push(t);
}
if (token.id !== ",") {
break;
}
advance(",");
}
advance(";");
return a.length === 0 ? null : a.length === 1 ? a[0] : a;
});
stmt("while", function() {
advance("(");
this.first = expression(0);
advance(")");
this.second = block();
this.arity = "statement";
return this;
});
stmt("if", function () {
advance("(");
this.first = expression(0);
advance(")");
this.second = block();
if (token.id === "else") {
scope.reserve(token);
advance("else");
this.third = token.id === "if" ?
statement() :
block();
}
this.arity = "statement";
return this;
});
stmt("break", function() {
advance(";");
if (token.id !== "}") {
token.error("Unreachable statement.");
}
this.arity = "statement";
return this;
});
stmt("return", function(){
if (token.id !== ";") {
this.first = expression(0);
}
advance(";");
if (token.id !== "}") {
token.error("Unreachable statement.");
}
this.arity = "statement";
return this;
});
/* Functions */
prefix("function", function() {
let a = [];
scope = new_scope();
if (token.arity === "name") {
scope.define(token);
this.name = token.value;
advance();
}
advance("(");
if (token.id !== ")") {
while(true) {
if (token.arity !== "name") {
token.error("Expected a parameter name.");
}
scope.define(token);
a.push(token);
advance();
if (token.id !== ",") {
break;
}
advance(",");
}
}
this.first = a;
advance(")");
advance("{");
this.second = statements();
advance("}");
this.arity = "function";
scope.pop();
return this;
});
/* Function Invocation */
infix("(", 90, function(left) {
let a = [];
this.first = left;
this.second = a;
this.arity = "binary";
if ( (left.arity !== "unary" ||
left.id !== "function") &&
left.arity !== "name" &&
(left.arity !== "binary" ||
(left.id !== "." &&
left.id !== "(" &&
left.id !== "[")) ) {
left.error("Expected a variable name.");
}
if (token.id !== ")") {
while(true) {
a.push(expression(0));
if (token.id !== ",") {
break;
}
advance(",");
}
}
advance(")");
return this;;
});
prefix("[", function() {
let a = [];
if (token.id !== "]") {
while(true) {
a.push(expression(0));
if (token.id !== ",") {
break;
}
advance(",");
}
}
advance("]");
this.first = a;
this.arity = "unary";
return this;
});
prefix("{", function() {
let a = [];
if (token.id !== "}") {
while(true){
var n = token;
if (n.arity !== "name" && narity !== "literal") {
token.error("Bad key.");
}
advance();
advance(":");
var v = expression(0);
v.key = n.value;
a.push(v);
if (token.id !== ",") {
break;
}
advance(",");
}
}
advance("}");
this.first = a;
this.arity = "unary";
return this;
});
/*
Here we return a function that executes
the parser on source code once it is used.
Using a closure to protect the parsing logic
seems like a safe bet, but I'm not sure it is
entirely necessary. Either way, this is how
the parser is currently configured.
*/
return function(source) {
console.log("Scanning..");
tokens = source.tokens('=<>!+-*&|/%^', '=<>&|');
token_nr = 0;
console.log("Parsing..");
new_scope();
advance();
let s = statements();
advance("(end)");
scope.pop();
return s;
};
})();//END OF EXPORTED MODULE