-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjsparse.js
657 lines (591 loc) · 19.5 KB
/
jsparse.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
// Copyright (C) 2007 Chris Double.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
function foldl(f, initial, seq) {
for(var i=0; i< seq.length; ++i)
initial = f(initial, seq[i]);
return initial;
}
var memoize = true;
function ParseState(input, index) {
this.input = input;
this.index = index || 0;
this.length = input.length - this.index;
this.cache = { };
return this;
}
ParseState.prototype.from = function(index) {
var r = new ParseState(this.input, this.index + index);
r.cache = this.cache;
r.length = this.length - index;
return r;
}
ParseState.prototype.substring = function(start, end) {
return this.input.substring(start + this.index, (end || this.length) + this.index);
}
ParseState.prototype.trimLeft = function() {
var s = this.substring(0);
var m = s.match(/^\s+/);
return m ? this.from(m[0].length) : this;
}
ParseState.prototype.at = function(index) {
return this.input.charAt(this.index + index);
}
ParseState.prototype.toString = function() {
return 'PS"' + this.substring(0) + '"';
}
ParseState.prototype.getCached = function(pid) {
if(!memoize)
return false;
var p = this.cache[pid];
if(p)
return p[this.index];
else
return false;
}
ParseState.prototype.putCached = function(pid, cached) {
if(!memoize)
return false;
var p = this.cache[pid];
if(p)
p[this.index] = cached;
else {
p = this.cache[pid] = { };
p[this.index] = cached;
}
}
function ps(str) {
return new ParseState(str);
}
// 'r' is the remaining string to be parsed.
// 'matched' is the portion of the string that
// was successfully matched by the parser.
// 'ast' is the AST returned by the successfull parse.
function make_result(r, matched, ast) {
return { remaining: r, matched: matched, ast: ast };
}
var parser_id = 0;
// 'token' is a parser combinator that given a string, returns a parser
// that parses that string value. The AST contains the string that was parsed.
function token(s) {
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var r = state.length >= s.length && state.substring(0,s.length) == s;
if(r)
cached = { remaining: state.from(s.length), matched: s, ast: s };
else
cached = false;
savedState.putCached(pid, cached);
return cached;
};
}
// Like 'token' but for a single character. Returns a parser that given a string
// containing a single character, parses that character value.
function ch(c) {
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var r = state.length >= 1 && state.at(0) == c;
if(r)
cached = { remaining: state.from(1), matched: c, ast: c };
else
cached = false;
savedState.putCached(pid, cached);
return cached;
};
}
// 'range' is a parser combinator that returns a single character parser
// (similar to 'ch'). It parses single characters that are in the inclusive
// range of the 'lower' and 'upper' bounds ("a" to "z" for example).
function range(lower, upper) {
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
if(state.length < 1)
cached = false;
else {
var ch = state.at(0);
if(ch >= lower && ch <= upper)
cached = { remaining: state.from(1), matched: ch, ast: ch };
else
cached = false;
}
savedState.putCached(pid, cached);
return cached;
};
}
// Helper function to convert string literals to token parsers
// and perform other implicit parser conversions.
function toParser(p) {
return (typeof(p) == "string") ? token(p) : p;
}
// Parser combinator that returns a parser that
// skips whitespace before applying parser.
function whitespace(p) {
var p = toParser(p);
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
cached = p(state.trimLeft());
savedState.putCached(pid, cached);
return cached;
};
}
// Parser combinator that passes the AST generated from the parser 'p'
// to the function 'f'. The result of 'f' is used as the AST in the result.
function action(p, f) {
var p = toParser(p);
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var x = p(state);
if(x) {
x.ast = f(x.ast);
cached = x;
}
else {
cached = false;
}
savedState.putCached(pid, cached);
return cached;
};
}
// Given a parser that produces an array as an ast, returns a
// parser that produces an ast with the array joined by a separator.
function join_action(p, sep) {
return action(p, function(ast) { return ast.join(sep); });
}
// Given an ast of the form [ Expression, [ a, b, ...] ], convert to
// [ [ [ Expression [ a ] ] b ] ... ]
// This is used for handling left recursive entries in the grammar. e.g.
// MemberExpression:
// PrimaryExpression
// FunctionExpression
// MemberExpression [ Expression ]
// MemberExpression . Identifier
// new MemberExpression Arguments
function left_factor(ast) {
return foldl(function(v, action) {
return [ v, action ];
},
ast[0],
ast[1]);
}
// Return a parser that left factors the ast result of the original
// parser.
function left_factor_action(p) {
return action(p, left_factor);
}
// 'negate' will negate a single character parser. So given 'ch("a")' it will successfully
// parse any character except for 'a'. Or 'negate(range("a", "z"))' will successfully parse
// anything except the lowercase characters a-z.
function negate(p) {
var p = toParser(p);
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
if(state.length >= 1) {
var r = p(state);
if(!r)
cached = make_result(state.from(1), state.at(0), state.at(0));
else
cached = false;
}
else {
cached = false;
}
savedState.putCached(pid, cached);
return cached;
};
}
// 'end_p' is a parser that is successful if the input string is empty (ie. end of parse).
function end_p(state) {
if(state.length == 0)
return make_result(state, undefined, undefined);
else
return false;
}
// 'nothing_p' is a parser that always fails.
function nothing_p(state) {
return false;
}
// 'sequence' is a parser combinator that processes a number of parsers in sequence.
// It can take any number of arguments, each one being a parser. The parser that 'sequence'
// returns succeeds if all the parsers in the sequence succeeds. It fails if any of them fail.
function sequence() {
var parsers = [];
for(var i = 0; i < arguments.length; ++i)
parsers.push(toParser(arguments[i]));
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached) {
return cached;
}
var ast = [];
var matched = "";
var i;
for(i=0; i< parsers.length; ++i) {
var parser = parsers[i];
var result = parser(state);
if(result) {
state = result.remaining;
if(result.ast != undefined) {
ast.push(result.ast);
matched = matched + result.matched;
}
}
else {
break;
}
}
if(i == parsers.length) {
cached = make_result(state, matched, ast);
}
else
cached = false;
savedState.putCached(pid, cached);
return cached;
};
}
// Like sequence, but ignores whitespace between individual parsers.
function wsequence() {
var parsers = [];
for(var i=0; i < arguments.length; ++i) {
parsers.push(whitespace(toParser(arguments[i])));
}
return sequence.apply(null, parsers);
}
// 'choice' is a parser combinator that provides a choice between other parsers.
// It takes any number of parsers as arguments and returns a parser that will try
// each of the given parsers in order. The first one that succeeds results in a
// successfull parse. It fails if all parsers fail.
function choice() {
var parsers = [];
for(var i = 0; i < arguments.length; ++i)
parsers.push(toParser(arguments[i]));
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached) {
return cached;
}
var i;
for(i=0; i< parsers.length; ++i) {
var parser=parsers[i];
var result = parser(state);
if(result) {
break;
}
}
if(i == parsers.length)
cached = false;
else
cached = result;
savedState.putCached(pid, cached);
return cached;
}
}
// 'butnot' is a parser combinator that takes two parsers, 'p1' and 'p2'.
// It returns a parser that succeeds if 'p1' matches and 'p2' does not, or
// 'p1' matches and the matched text is longer that p2's.
// Useful for things like: butnot(IdentifierName, ReservedWord)
function butnot(p1,p2) {
var p1 = toParser(p1);
var p2 = toParser(p2);
var pid = parser_id++;
// match a but not b. if both match and b's matched text is shorter
// than a's, a failed match is made
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var br = p2(state);
if(!br) {
cached = p1(state);
} else {
var ar = p1(state);
if (ar) {
if(ar.matched.length > br.matched.length)
cached = ar;
else
cached = false;
}
else {
cached = false;
}
}
savedState.putCached(pid, cached);
return cached;
}
}
// 'difference' is a parser combinator that takes two parsers, 'p1' and 'p2'.
// It returns a parser that succeeds if 'p1' matches and 'p2' does not. If
// both match then if p2's matched text is shorter than p1's it is successfull.
function difference(p1,p2) {
var p1 = toParser(p1);
var p2 = toParser(p2);
var pid = parser_id++;
// match a but not b. if both match and b's matched text is shorter
// than a's, a successfull match is made
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var br = p2(state);
if(!br) {
cached = p1(state);
} else {
var ar = p1(state);
if(ar.matched.length >= br.matched.length)
cached = br;
else
cached = ar;
}
savedState.putCached(pid, cached);
return cached;
}
}
// 'xor' is a parser combinator that takes two parsers, 'p1' and 'p2'.
// It returns a parser that succeeds if 'p1' or 'p2' match but fails if
// they both match.
function xor(p1, p2) {
var p1 = toParser(p1);
var p2 = toParser(p2);
var pid = parser_id++;
// match a or b but not both
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var ar = p1(state);
var br = p2(state);
if(ar && br)
cached = false;
else
cached = ar || br;
savedState.putCached(pid, cached);
return cached;
}
}
// A parser combinator that takes one parser. It returns a parser that
// looks for zero or more matches of the original parser.
function repeat0(p) {
var p = toParser(p);
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached) {
return cached;
}
var ast = [];
var matched = "";
var result;
while(result = p(state)) {
ast.push(result.ast);
matched = matched + result.matched;
if(result.remaining.index == state.index)
break;
state = result.remaining;
}
cached = make_result(state, matched, ast);
savedState.putCached(pid, cached);
return cached;
}
}
// A parser combinator that takes one parser. It returns a parser that
// looks for one or more matches of the original parser.
function repeat1(p) {
var p = toParser(p);
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var ast = [];
var matched = "";
var result= p(state);
if(!result)
cached = false;
else {
while(result) {
ast.push(result.ast);
matched = matched + result.matched;
if(result.remaining.index == state.index)
break;
state = result.remaining;
result = p(state);
}
cached = make_result(state, matched, ast);
}
savedState.putCached(pid, cached);
return cached;
}
}
// A parser combinator that takes one parser. It returns a parser that
// matches zero or one matches of the original parser.
function optional(p) {
var p = toParser(p);
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var r = p(state);
cached = r || make_result(state, "", false);
savedState.putCached(pid, cached);
return cached;
}
}
// A parser combinator that ensures that the given parser succeeds but
// ignores its result. This can be useful for parsing literals that you
// don't want to appear in the ast. eg:
// sequence(expect("("), Number, expect(")")) => ast: Number
function expect(p) {
return action(p, function(ast) { return undefined; });
}
function chain(p, s, f) {
var p = toParser(p);
return action(sequence(p, repeat0(action(sequence(s, p), f))),
function(ast) { return [ast[0]].concat(ast[1]); });
}
// A parser combinator to do left chaining and evaluation. Like 'chain', it expects a parser
// for an item and for a seperator. The seperator parser's AST result should be a function
// of the form: function(lhs,rhs) { return x; }
// Where 'x' is the result of applying some operation to the lhs and rhs AST's from the item
// parser.
function chainl(p, s) {
var p = toParser(p);
return action(sequence(p, repeat0(sequence(s, p))),
function(ast) {
return foldl(function(v, action) { return action[0](v, action[1]); }, ast[0], ast[1]);
});
}
// A parser combinator that returns a parser that matches lists of things. The parser to
// match the list item and the parser to match the seperator need to
// be provided. The AST is the array of matched items.
function list(p, s) {
return chain(p, s, function(ast) { return ast[1]; });
}
// Like list, but ignores whitespace between individual parsers.
function wlist() {
var parsers = [];
for(var i=0; i < arguments.length; ++i) {
parsers.push(whitespace(arguments[i]));
}
return list.apply(null, parsers);
}
// A parser that always returns a zero length match
function epsilon_p(state) {
return make_result(state, "", undefined);
}
// Allows attaching of a function anywhere in the grammer. If the function returns
// true then parse succeeds otherwise it fails. Can be used for testing if a symbol
// is in the symbol table, etc.
function semantic(f) {
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
cached = f() ? make_result(state, "", undefined) : false;
savedState.putCached(pid, cached);
return cached;
}
}
// The and predicate asserts that a certain conditional
// syntax is satisfied before evaluating another production. Eg:
// sequence(and("0"), oct_p)
// (if a leading zero, then parse octal)
// It succeeds if 'p' succeeds and fails if 'p' fails. It never
// consume any input however, and doesn't put anything in the resulting
// AST.
function and(p) {
var p = toParser(p);
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
var r = p(state);
cached = r ? make_result(state, "", undefined) : false;
savedState.putCached(pid, cached);
return cached;
}
}
// The opposite of 'and'. It fails if 'p' succeeds and succeeds if
// 'p' fails. It never consumes any input. This combined with 'and' can
// be used for 'lookahead' and disambiguation of cases.
//
// Compare:
// sequence("a",choice("+","++"),"b")
// parses a+b
// but not a++b because the + matches the first part and peg's don't
// backtrack to other choice options if they succeed but later things fail.
//
// sequence("a",choice(sequence("+", not("+")),"++"),"b")
// parses a+b
// parses a++b
//
function not(p) {
var p = toParser(p);
var pid = parser_id++;
return function(state) {
var savedState = state;
var cached = savedState.getCached(pid);
if(cached)
return cached;
cached = p(state) ? false : make_result(state, "", undefined);
savedState.putCached(pid, cached);
return cached;
}
}