-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
test-tracing.js
482 lines (397 loc) · 15.4 KB
/
test-tracing.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
'use strict';
// --------------------------------------------------------------------
// Imports
// --------------------------------------------------------------------
const test = require('ava');
const ohm = require('..');
const common = require('../src/common');
const testUtil = require('./helpers/testUtil');
// --------------------------------------------------------------------
// Private stuff
// --------------------------------------------------------------------
function descendant(traceNode, ...pathNodes) {
let ans = traceNode;
for (const node of pathNodes) {
ans = ans.children[node];
}
return ans;
}
function displayString(traceNode) {
return traceNode.displayString;
}
function succeeded(traceNode) {
return traceNode.succeeded;
}
function onlyChild(traceNode) {
common.assert(traceNode.children.length === 1, 'expected only 1 child');
return traceNode.children[0];
}
// --------------------------------------------------------------------
// Tests
// --------------------------------------------------------------------
test('basic tracing', t => {
const g = ohm.grammar('G { start = "a" | letter* }');
const start = g.trace('hallo').children[0];
t.is(start.displayString, 'start');
t.is(start.succeeded, true);
t.is(start.pos, 0);
const cstNode = start.bindings[0];
t.is(cstNode.isNonterminal(), true);
t.is(cstNode.numChildren(), 1);
t.is(cstNode.ctorName, 'start');
const alt = start.children[0];
t.is(alt.displayString, '"a" | letter*');
t.is(alt.succeeded, true);
t.is(alt.children.length, 2);
t.is(alt.children[0].succeeded, false);
t.is(alt.children[1].succeeded, true);
const altCSTNode = alt.bindings[0];
t.is(altCSTNode.isIteration(), true);
t.is(altCSTNode.numChildren(), 5);
const many = alt.children[1];
t.is(many.displayString, 'letter*');
t.is(many.source.contents, 'hallo');
t.is(many.children.length, 6);
const manyCSTNode = many.bindings[0];
t.is(manyCSTNode, altCSTNode);
const childrenSucceeded = many.children.map(c => c.succeeded);
t.deepEqual(childrenSucceeded, [true, true, true, true, true, false]);
const cstChildrenName = manyCSTNode.children.map(c => c.ctorName === 'letter');
t.deepEqual(cstChildrenName, [true, true, true, true, true]);
});
test('space skipping', t => {
const g = ohm.grammar('G { Start = "a" }');
let trace = g.trace('a');
t.deepEqual(trace.children.map(displayString), ['spaces', 'Start', 'spaces', 'end']);
let start = trace.children[1];
t.deepEqual(start.children.map(displayString), ['spaces', '"a"']);
t.is(start.children[1].children.length, 0, 'primitive node has no children');
trace = ohm.grammar('G { start = "a" }').trace('a');
t.deepEqual(trace.children.map(displayString), ['start', 'end']);
start = trace.children[0];
t.deepEqual(start.children.map(displayString), ['"a"'], 'no spaces in lexical context');
t.is(start.children[0].children.length, 0, 'prim node has no children in lexical context');
trace = ohm.grammar('G { Start = foo\n foo = "a" "b" }').trace(' ab ');
t.deepEqual(trace.children.map(displayString), ['spaces', 'Start', 'spaces', 'end']);
start = trace.children[1];
t.deepEqual(start.children.map(displayString), ['spaces', 'foo']);
const fooAppl = start.children[1];
t.deepEqual(
fooAppl.children.map(displayString),
['"a" "b"'],
'no spaces under lexical rule appl'
);
});
test('tracing with parameterized rules', t => {
const g = ohm.grammar('G { start = foo<"123"> foo<x> = "a" | letter* }');
const start = g.trace('hallo').children[0];
t.is(start.displayString, 'start');
t.is(start.succeeded, true);
t.is(start.pos, 0);
let cstNode = start.bindings[0];
t.is(cstNode.ctorName, 'start');
t.is(cstNode.numChildren(), 1);
const app = start.children[0];
t.is(app.displayString, 'foo<"123">');
t.is(app.succeeded, true);
t.is(app.children.length, 1);
t.is(app.children[0].succeeded, true);
cstNode = app.bindings[0];
t.is(cstNode.ctorName, 'foo');
t.is(cstNode.numChildren(), 1);
const alt = app.children[0];
t.is(alt.displayString, '"a" | letter*');
t.is(alt.succeeded, true);
t.is(alt.children.length, 2);
t.is(alt.children[0].succeeded, false);
t.is(alt.children[1].succeeded, true);
cstNode = alt.bindings[0];
t.is(cstNode.isIteration(), true);
t.is(cstNode.numChildren(), 5);
const many = alt.children[1];
t.is(many.displayString, 'letter*');
t.is(many.source.contents, 'hallo');
t.is(many.children.length, 6);
cstNode = many.bindings[0];
t.is(cstNode.ctorName, '_iter');
t.is(cstNode.numChildren(), 5);
t.deepEqual(many.children.map(succeeded), [true, true, true, true, true, false]);
const cstChildrenName = cstNode.children.map(c => c.ctorName === 'letter');
t.deepEqual(cstChildrenName, [true, true, true, true, true]);
});
test('tracing with memoization', t => {
const g = ohm.grammar('G { Start = letter ~letter | letter* }');
const start = g.trace(' aB').children[1];
t.deepEqual(start.children.map(displayString), ['letter ~letter | letter*']);
const alt = start.children[0];
t.deepEqual(alt.children.map(displayString), ['letter ~letter', 'letter*']);
t.is(alt.children[0].succeeded, false);
t.is(alt.children[1].succeeded, true);
t.is(alt.bindings[0].isIteration(), true);
const star = alt.children[1];
t.is(star.bindings[0].isIteration(), true);
t.deepEqual(star.children.map(displayString), [
'spaces',
'letter',
'spaces',
'letter',
'spaces',
'letter',
]);
// The 'letter*' should succeed, and its 'letter' children should be
// memoized from the other size of the Alt (letter ~letter). Ensure
// the the trace is still recorded properly.
t.deepEqual(star.children.map(succeeded), [true, true, true, true, true, false]);
const cstNode = star.children[1].bindings[0];
t.is(cstNode.ctorName, 'letter');
t.is(cstNode.numChildren(), 1);
t.deepEqual(descendant(star, 1).children.map(displayString), [
'lower\n | upper\n | unicodeLtmo',
]);
t.deepEqual(descendant(star, 1, 0).children.map(displayString), ['lower']);
t.deepEqual(descendant(star, 1, 0, 0).children.map(displayString), [
'Unicode [Ll] character',
]);
t.deepEqual(descendant(star, 3).children.map(displayString), [
'lower\n | upper\n | unicodeLtmo',
]);
t.deepEqual(descendant(star, 3, 0).children.map(displayString), ['lower', 'upper']);
t.deepEqual(descendant(star, 3, 0, 0).children.map(displayString), [
'Unicode [Ll] character',
]);
});
test('tracing with parameterized rules and memoization', t => {
const g = ohm.grammar('G { start = foo<"123"> ~foo<"123"> | foo<"123">* foo<x> = letter }');
const start = g.trace('aB').children[0];
const alt = start.children[0];
t.is(alt.children[0].succeeded, false);
t.is(alt.children[1].succeeded, true);
t.is(alt.children.length, 2);
const many = alt.children[1];
t.is(many.children.length, 3);
// The 'letter*' should succeed, but its first two children should be
// memoized from the other size of the Alt (letter ~letter). Ensure
// the the trace is still recorded properly.
t.is(many.children[0].succeeded, true);
t.is(many.children[0].children.length, 1);
t.is(descendant(many, 0, 0).children.length, 1);
t.is(descendant(many, 0, 0, 0).children.length, 1);
t.is(descendant(many, 0, 0, 0, 0).children.length, 1);
t.is(descendant(many, 0, 0, 0, 0, 0).displayString, 'Unicode [Ll] character');
t.is(many.children[1].succeeded, true);
t.is(many.children[1].children.length, 1);
t.is(descendant(many, 1, 0).children.length, 1);
t.is(descendant(many, 1, 0, 0).children.length, 2);
t.is(descendant(many, 1, 0, 0, 0).children.length, 1);
t.is(descendant(many, 1, 0, 0, 0, 0).displayString, 'Unicode [Ll] character');
t.is(descendant(many, 1, 0, 0, 1).children.length, 1);
t.is(descendant(many, 1, 0, 0, 1, 0).displayString, 'Unicode [Lu] character');
});
test('tracing with left recursion', t => {
const g = ohm.grammar('G { id = id letter -- rec\n | letter }');
let id = g.trace('abc').children[0];
t.is(id.isHeadOfLeftRecursion, true);
// For successful left recursion, there is an extra entry holding the trace for the
// last iteration of the loop, which failed to grow the seed result.
const terminatingEntry = id.terminatingLREntry;
t.truthy(terminatingEntry, 'has an entry for the last iteration of the growSeedResult loop');
t.is(terminatingEntry.expr, id.expr, 'it is for the same expression');
t.is(terminatingEntry.succeeded, false, 'but marked as a failure');
t.is(terminatingEntry.isHeadOfLeftRecursion, false);
t.is(terminatingEntry.pos, id.pos);
t.true(
terminatingEntry.source.length <= id.source.length,
'its source interval is not longer'
);
t.is(id.children.length, 1, 'has a single child');
const choice = id.children[0];
t.is(choice.children.length, 1);
const recApply = choice.children[0];
t.is(recApply.displayString, 'id_rec');
t.is(recApply.children.length, 1);
let cstNode = recApply.bindings[0];
t.is(cstNode.ctorName, 'id_rec');
t.is(cstNode.numChildren(), 2);
const seq = recApply.children[0];
t.is(seq.source.contents, 'abc');
t.is(seq.children.length, 2);
t.is(seq.children[0].displayString, 'id');
t.is(seq.children[0].isHeadOfLeftRecursion, false);
t.is(seq.children[1].displayString, 'letter');
cstNode = seq.children[0].bindings[0];
t.is(cstNode.ctorName, 'id');
t.is(cstNode.numChildren(), 1);
cstNode = seq.children[0].children[0].bindings[0];
t.is(cstNode.ctorName, 'id_rec');
t.is(cstNode.numChildren(), 2);
id = g.trace('a').children[0];
t.is(id.isHeadOfLeftRecursion, true);
t.is(id.children.length, 1);
id = g.trace('9').children[0];
t.is(id.succeeded, false);
t.is(id.isHeadOfLeftRecursion, true, 'failed LR node is still an LR head');
t.is(id.terminatingLREntry, null, 'it has no terminatingLREntry');
});
test('tracing with left recursion and leading space', t => {
const g = testUtil.makeGrammar([
'G {',
' Foo = Foo "x" -- x',
' | Foo "y" -- y',
' | "z"',
'}',
]);
const foo = g.trace(' zy').children[1]; // First child is 'spaces'.
t.is(foo.isHeadOfLeftRecursion, true);
t.is(foo.children.length, 1);
let alt = foo.children[0];
t.deepEqual(alt.children.map(displayString), ['spaces', 'Foo_x', 'spaces', 'Foo_y']);
t.deepEqual(alt.children.map(succeeded), [true, false, true, true]);
const seq = onlyChild(alt.children[3]);
t.deepEqual(seq.children.map(displayString), ['spaces', 'Foo', 'spaces', '"y"']);
const applyFoo = seq.children[1];
alt = onlyChild(applyFoo);
t.deepEqual(alt.children.map(displayString), [
'spaces',
'Foo_x',
'spaces',
'Foo_y',
'spaces',
'"z"',
]);
t.deepEqual(alt.children.map(succeeded), [true, false, true, false, true, true]);
t.is(alt.children[5].children.length, 0);
});
test('toString', t => {
let g = ohm.grammar('G { start = "a" | letter* }');
let lines = g.trace('hi').toString().split('\n').slice(0, -1);
const exprs = lines.map(l => l.split(/\s+/)[2]);
t.deepEqual(
exprs,
[
'start',
'"a"', // Failed.
'letter*',
'letter',
'lower',
'Unicode',
'letter',
'lower',
'Unicode',
'letter',
'lower',
'Unicode', // Failed.
'upper',
'Unicode', // Failed.
'unicodeLtmo',
'Unicode', // Failed.
'end',
],
'expressions'
);
const excerpts = lines.map(l => l.split(/\s+/)[0]);
t.deepEqual(
excerpts,
common.repeat('hi', 6).concat(common.repeat('i', 3)).concat(common.repeat('', 8)),
'excerpts'
);
// Test that newlines are escaped in the trace output.
g = ohm.grammar('G { start = space* }');
lines = g.trace(' ', 'space').toString().split('\n');
t.is(lines.length, 4, 'trace is only four lines long');
lines = g.trace('\n\n', 'space').toString().split('\n');
t.is(lines.length, 4, 'trace is still four lines long');
t.is(lines[lines.length - 1], '', 'last line is empty');
});
test('toString with left recursion', t => {
const g = ohm.grammar('G { start = start letter -- rec\n | letter }');
const lines = g.trace('a').toString().split('\n').slice(0, -1);
// TODO: Augment the trace with the missing information about growing the seed.
// We show the initial failure due to left recursion, but not the retry which fails
// to grow the seed.
t.deepEqual(lines, [
'a ✓ start (LR) ⇒ "a"',
'a ✗ start_rec',
'a ✗ start letter',
'a ✗ start', // Left-recursive base case.
'a ✓ letter ⇒ "a"',
'a ✓ lower ⇒ "a"',
'a ✓ Unicode [Ll] character ⇒ "a"',
' ✓ end ⇒ ""',
]);
});
test('displayString', t => {
const g = ohm.grammar('G { start = caseInsensitive<"tk"> }');
const start = g.trace('tK').children[0];
const app = start.children[0];
t.is(app.displayString, 'caseInsensitive<"tk">');
const caseInsensitive = app.children[0];
// TODO: Get this working! It should be is() here.
t.not(caseInsensitive.displayString, '"tk" (case-insensitive)');
});
// TODO: Get these tests working again for the incremental parser!
test.failing('memoization', t => {
const g = testUtil.makeGrammar([
'G {',
' start = &id id',
' id = id alnum -- rec',
' | letter',
'}',
]);
const start = g.trace('a9').children[0];
const seq = start.children[0];
const lookahead = seq.children[0];
const applyId = lookahead.children[0];
t.is(applyId.expr.ruleName, 'id');
t.is(applyId.isHeadOfLeftRecursion, true);
t.is(applyId.expr.source.startIdx, 15);
t.is(applyId.expr.source.endIdx, 17);
let cstNode = applyId.bindings[0];
t.is(cstNode.ctorName, 'id');
t.is(cstNode.source.startIdx, 0);
t.is(cstNode.source.endIdx, 2);
const applyId2 = seq.children[1];
t.is(applyId2.expr.ruleName, 'id');
t.is(applyId2.isHeadOfLeftRecursion, true);
t.is(applyId2.expr.source.startIdx, 18);
t.is(applyId2.expr.source.endIdx, 20);
cstNode = applyId2.bindings[0];
t.is(cstNode.ctorName, 'id');
t.is(cstNode.source.startIdx, 0);
t.is(cstNode.source.endIdx, 2);
t.is(applyId.pos, 0);
t.is(applyId2.pos, 0);
t.truthy(applyId.succeeded);
t.truthy(applyId2.succeeded);
});
test.failing('bindings', t => {
const g = ohm.grammar('G { start = "a" "b" | "c" notX\n notX = ~"x" any }');
let trace = g.trace('ab');
t.is(trace.succeeded, true);
t.is(trace.bindings.length, 2, 'top-level Seq has two bindings (start appl + end)');
t.is(trace.bindings[0].source.contents, 'ab');
const start = trace.children[0];
t.is(start.displayString, 'start');
t.is(start.succeeded, true);
t.is(start.bindings.length, 1);
let alt = start.children[0];
t.is(alt.expr.constructor, ohm.pexprs.Alt);
t.is(alt.bindings.length, 2, 'alt has two bindings');
t.is(alt.bindings[0].source.contents, 'a');
t.is(alt.bindings[1].source.contents, 'b');
t.deepEqual(
alt.bindings.map(b => b.ctorName),
['_terminal', '_terminal']
);
trace = g.trace('cd');
alt = trace.children[0].children[0];
t.is(alt.expr.constructor, ohm.pexprs.Alt);
t.is(alt.bindings.length, 2, 'alt has two bindings');
t.is(alt.bindings[0].source.contents, 'c');
t.is(alt.bindings[1].source.contents, 'd');
t.deepEqual(
alt.bindings.map(b => b.ctorName),
['_terminal', 'notX']
);
const notX = alt.children[1];
t.deepEqual(notX.children.map(succeeded), [true, true], 'both children succeeded');
});