-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPEG.js
546 lines (512 loc) · 15.3 KB
/
PEG.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
/*
index.js - "tart-PEG": Parsing Expression Grammar (PEG) tools (tart module)
The MIT License (MIT)
Copyright (c) 2015-2016 Dale Schumacher
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
"use strict";
var PEG = module.exports;
//var log = console.log;
var log = function () {};
var defaultLog = log;
var error = function error(m, e) {
console.log('error', m, e);
m.fail({
start: m.input,
end: m.input,
error: e
});
};
PEG.fail = function failBeh(m) {
log('failBeh'+this.self+':', m);
m.fail({
start: m.input,
end: m.input
});
};
PEG.empty = function emptyBeh(m) {
log('emptyBeh'+this.self+':', m);
m.ok({
start: m.input,
end: m.input,
value: []
});
};
PEG.predicate = function predicatePtrn(predicate) {
return function predicateBeh(m) {
log('predicateBeh'+this.self+':', m);
try {
var token = m.input.value;
if (token !== undefined) {
if (predicate(token)) {
m.input.next(this.sponsor(function readBeh(input) {
m.ok({
start: m.input,
end: input,
value: token
});
}));
return;
}
}
m.fail({
start: m.input,
end: m.input
});
} catch (e) {
error(m, e);
}
};
};
PEG.dot = PEG.anything = PEG.predicate(function isTrue(token) {
return true;
});
PEG.terminal = function terminalPtrn(expect) {
return PEG.predicate(function isEqual(actual) {
return (expect == actual);
});
}
PEG.not = function notPtrn(pattern) {
return function notBeh(m) {
log('notBeh'+this.self+':', m);
try {
var result = {
start: m.input,
end: m.input
};
pattern({
input: m.input,
ok: this.sponsor(function okBeh(r) {
m.fail(result);
}),
fail: this.sponsor(function failBeh(r) {
m.ok(result);
})
});
} catch (e) {
error(m, e);
}
};
};
PEG.follow = function followPtrn(pattern) {
return function followBeh(m) {
log('followBeh'+this.self+':', m);
try {
var result = {
start: m.input,
end: m.input
};
pattern({
input: m.input,
ok: this.sponsor(function okBeh(r) {
m.ok(result);
}),
fail: this.sponsor(function failBeh(r) {
m.fail(result);
})
});
} catch (e) {
error(m, e);
}
};
};
var andThen = function andPtrn(first, rest) {
return function andBeh(m) {
log('andBeh'+this.self+':', m);
var failure = this.sponsor(function failBeh(r) {
// log('failBeh'+this.self+':', r, m);
m.fail({
start: m.input,
end: r.end
});
});
var next = this.sponsor(function nextBeh(r) {
log('nextBeh'+this.self+':', r, m);
var success = this.sponsor(function okBeh(rr) {
// log('okBeh'+this.self+':', rr, r, m);
rr.value.unshift(r.value); // mutate rr.value
m.ok({
start: m.input,
end: rr.end,
value: rr.value
});
});
rest({
input: r.end,
ok: success,
fail: failure
});
});
first({
input: m.input,
ok: next,
fail: failure
});
};
};
PEG.sequence = function sequencePtrn(list) {
return function sequenceBeh(m) {
if (list.length > 0) {
var pattern = list.shift();
this.behavior = andThen(
pattern,
this.sponsor(PEG.sequence(list))
);
} else {
this.behavior = PEG.empty;
}
this.self(m);
};
};
var orElse = function orPtrn(first, rest) {
return function orBeh(m) {
log('orBeh'+this.self+':', m);
var next = this.sponsor(function nextBeh(r) {
log('nextBeh'+this.self+':', r, m);
rest({
input: m.input,
ok: m.ok,
fail: m.fail
});
});
first({
input: m.input,
ok: m.ok,
fail: next
});
};
};
PEG.choice = function choicePtrn(list) {
return function choiceBeh(m) {
if (list.length > 0) {
var pattern = list.shift();
this.behavior = orElse(
pattern,
this.sponsor(PEG.choice(list))
);
} else {
this.behavior = PEG.fail;
}
this.self(m);
};
};
PEG.star = PEG.zeroOrMore = function zeroOrMorePtrn(pattern) {
return function zeroOrMoreBeh(m) {
log('zeroOrMoreBeh'+this.self+':', m);
var list = [];
var more = this.sponsor(function moreBeh(r) {
log('zeroOrMoreBeh'+this.self+'.more:', r, list);
list.push(r.value); // mutate list
pattern({
input: r.end,
ok: more,
fail: done
});
});
var done = this.sponsor(function doneBeh(r) {
log('zeroOrMoreBeh'+this.self+'.done:', r, list);
m.ok({
start: m.input,
end: r.start,
value: list
});
});
pattern({
input: m.input,
ok: more,
fail: done
});
};
};
PEG.plus = PEG.oneOrMore = function oneOrMorePtrn(pattern) {
return function oneOrMoreBeh(m) {
log('oneOrMoreBeh'+this.self+':', m);
var list = [];
var more = this.sponsor(function moreBeh(r) {
log('oneOrMoreBeh'+this.self+'.more:', r, list);
list.push(r.value); // mutate list
pattern({
input: r.end,
ok: more,
fail: done
});
});
var done = this.sponsor(function doneBeh(r) {
log('oneOrMoreBeh'+this.self+'.done:', r, list);
m.ok({
start: m.input,
end: r.start,
value: list
});
});
pattern({
input: m.input,
ok: more,
fail: m.fail
});
};
};
PEG.question = PEG.optional = PEG.zeroOrOne = function zeroOrOnePtrn(pattern) {
return function zeroOrOneBeh(m) {
log('zeroOrOneBeh'+this.self+':', m);
pattern({
input: m.input,
ok: this.sponsor(function oneBeh(r) {
m.ok({
start: m.input,
end: r.end,
value: [ r.value ]
});
}),
fail: this.sponsor(function zeroBeh(r) {
m.ok({
start: m.input,
end: m.input,
value: []
});
})
});
};
};
PEG.object = function objectPtrn(expect) {
return PEG.predicate(function matchObject(actual) {
for (var name in expect) {
if (expect.hasOwnProperty(name)) {
if (expect[name] !== actual[name]) {
return false;
}
}
}
return true;
});
}
PEG.memoize = PEG.packrat = function packratPtrn(pattern, name, log) {
var results = [];
name = name || '';
log = log || defaultLog;
return function packratBeh(m) {
// log('packratBeh:', name, m);
try {
var at = m.input.pos;
var r = results[at];
if (r) {
log('used:', name, at, r);
m.ok(r);
} else {
var memo = this.sponsor(function memo(r) {
results[at] = r;
log('memo:', name, at, r);
m.ok(r);
});
pattern({
input: m.input,
ok: memo,
fail: m.fail
});
}
} catch (e) {
error(m, e);
}
};
};
PEG.namespace = function namespace(log) {
var ns = {};
var ruleNamed = {};
log = log || defaultLog;
ns.defaultTransform = function ruleValue(name, value, r) {
return {
name: name,
start: r.start,
end: r.end,
value: value
};
};
ns.define = function setRule(name, pattern) {
if (ruleNamed[name]) {
throw Error('Redefined rule: ' + name);
}
// log('setRule:', name);
var rule = {
name: name,
pattern: pattern,
transform: ns.defaultTransform
};
ruleNamed[name] = rule;
return rule;
};
ns.lookup = function getRule(name) {
// delay name lookup until rule is invoked
// log('getRule:', name);
return function callBeh(m) {
// log('callBeh:', name, m);
var rule = ruleNamed[name];
if (!rule) {
throw Error('Undefined rule: ' + name);
}
if (!rule.wrapped) {
rule.wrapped = this.sponsor(ns.wrapper(rule));
}
rule.wrapped(m);
};
};
ns.transform = function setTransform(name, transform) {
var rule = ruleNamed[name];
if (!rule) {
throw Error('Undefined rule: ' + name);
}
rule.transform = transform;
};
ns.transformWrapper = function transformWrapper(rule) {
return function transformBeh(m) {
// log('transformBeh:', rule, m);
rule.pattern({
input: m.input,
ok: this.sponsor(function okBeh(r) {
try {
r.value = rule.transform(rule.name, r.value, r);
} catch (e) {
r.value = undefined;
r.error = e;
}
m.ok(r);
}),
fail: this.sponsor(function failBeh(r) {
m.fail(r);
})
});
};
};
var ruleStack = [];
var isRecursive = function isRecursive(rule, m) {
var i = ruleStack.length;
while (i > 0) {
i -= 1;
if (ruleStack[i].pos < m.input.pos) {
return false; // safe
}
if (ruleStack[i].rule === rule) {
return true; // RECURSIVE
}
}
return false; // safe
};
ns.stackingWrapper = function stackingWrapper(pattern, rule) {
return function stackingBeh(m) {
if (isRecursive(rule, m)) {
log('RECURSIVE:', rule, m);
m.fail({
start: m.input,
end: m.input
});
return;
}
// log('stackingBeh:', rule, m);
ruleStack.push({
rule: rule,
pos: m.input.pos
});
// log('ruleStack.push:', ruleStack);
pattern({
input: m.input,
ok: this.sponsor(function okBeh(r) {
ruleStack.pop();
// log('ruleStack.ok:', ruleStack);
m.ok(r);
}),
fail: this.sponsor(function failBeh(r) {
ruleStack.pop();
// log('ruleStack.fail:', ruleStack);
m.fail(r);
})
});
};
};
ns.wrapper = function wrapRule(rule) {
return function wrapBeh(m) {
log('wrapBeh:', rule, m);
var transform = this.sponsor(ns.transformWrapper(rule));
var stacking = this.sponsor(ns.stackingWrapper(transform, rule));
this.behavior = PEG.memoize(stacking, rule.name, log);
this.self(m);
}
};
return ns;
};
PEG.start = function start(pattern, ok, fail) {
return function startBeh(s) {
pattern({
input: s,
ok: ok,
fail: fail
});
};
};
PEG.factory = function factory(sponsor) {
var kit = {
fail: sponsor(PEG.fail),
empty: sponsor(PEG.empty)
};
kit.dot = kit.any = kit.anything = sponsor(PEG.anything);
kit.term = kit.terminal = function aTerminal(token) {
return sponsor(PEG.terminal(token));
};
kit.if = kit.cond = kit.predicate = function aPredicate(predicate) {
return sponsor(PEG.predicate(predicate));
};
kit.not = function aNot(pattern) {
return sponsor(PEG.not(pattern));
};
kit.follow = function aFollow(pattern) {
return sponsor(PEG.follow(pattern));
};
kit.seq = kit.sequence = function aSequence(list) {
return sponsor(PEG.sequence(list));
};
kit.alt = kit.choice = function aChoice(list) {
return sponsor(PEG.choice(list));
};
kit.star = kit.zeroOrMore = function aZeroOrMore(pattern) {
return sponsor(PEG.zeroOrMore(pattern));
};
kit.plus = kit.oneOrMore = function aOneOrMore(pattern) {
return sponsor(PEG.oneOrMore(pattern));
};
kit.opt = kit.optional = kit.question = kit.zeroOrOne = function aZeroOrOne(pattern) {
return sponsor(PEG.zeroOrOne(pattern));
};
kit.like = kit.object = function anObject(expect) {
return sponsor(PEG.object(expect));
};
kit.memo = kit.memoize = kit.packrat = function aPackrat(pattern, name, log) {
return sponsor(PEG.packrat(pattern, name, log));
};
kit.scope = kit.namespace = function newNamespace(log) {
var ns = PEG.namespace(log);
ns.factory = kit;
ns.call = function aCall(name) {
return sponsor(ns.lookup(name));
};
return ns;
};
kit.matcher = kit.match = kit.start = function aMatcher(pattern, ok, fail) {
return sponsor(PEG.start(pattern, ok, fail));
};
return kit;
};