Repository navigation
Expand file tree
/
Copy pathportable-targets.ts
More file actions
733 lines (715 loc) · 39.4 KB
/
Copy pathportable-targets.ts
File metadata and controls
733 lines (715 loc) · 39.4 KB
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Gate: the TARGET-AGNOSTIC emitter (issue #6) — `emitParser(grammar, target)`
// derives a parser in EACH target language that accepts/rejects the same inputs the
// interpreter (createParser) does. The agnosticism proof by EXECUTION: every grammar is
// rendered to TypeScript, Go, and Rust; the Go/Rust sources are COMPILED and RUN, and each
// parser's accept/reject outcome is compared against the createParser oracle over an
// adversarial corpus (accept-parity on valid input, reject-parity on malformed input).
//
// The gate checks ACCEPT/REJECT PARITY, not byte-identical CST: the portable targets are
// free to use a different internal node shape (arena, compressed CST) as long as they agree
// with the oracle on what parses and what fails. A small CST-shape snapshot on tiny inputs
// guards against accidental semantic drift within a single target run.
//
// - calc: operator precedence/associativity, prefix unary, nested grouping.
// - minijs: a real JavaScript SUBSET — a string/comment lexer, the full operator ladder,
// call/member/index chains, arrays, and statement forms (the grammar the Go/Rust
// output is benchmarked against oxc with).
//
// Go/Rust toolchains are optional: a missing `go`/`rustc` is logged and skipped (the TS
// rendering, which needs only node, always runs).
import { execFileSync, spawnSync } from 'node:child_process';
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { createParser } from '../src/gen-parser.ts';
import { createLexer } from '../src/gen-lexer.ts';
import { emitParser, tsTarget, goTarget, rustTarget } from '../src/emit.ts';
import type { CstGrammar } from '../src/types.ts';
type Case = { grammar: string; path: string; accept: string[]; reject: string[]; tsOnly?: boolean };
const CASES: Case[] = [
{
grammar: 'calc', path: './fixtures/calc.ts',
accept: [
'1;', 'a;', '', '1 + 2 * 3;', '1 * 2 + 3;', '1 - 2 - 3;', 'a / b / c;', '1 + 2 + 3 + 4;',
'-a;', '-(-a);', '- - a;', '-a * b;', '-a + b * c;', '-(a + b) * c;',
'(1);', '((a));', '(1 + 2) * (3 - 4);', 'a * b + c * d - e / f;',
'let x = 1; let y = x + 2 * x; (y);', 'let z = -(a * b) / (c - -d);', 'foo; bar; baz;',
],
reject: ['1 +;', '(1;', '1 2;', 'let = 1;', ') ;', '* a;', 'let x 1;'],
},
{
grammar: 'minijs', path: './fixtures/minijs.ts',
accept: [
'1;', 'a;', '', 'x = 1 + 2 * 3;', '-a * b + 1;', '(1 + 2) * 3;',
'foo(a, b);', 'a.b.c;', 'a[0][1];', 'f()()();', 'a.b(c).d[e];',
'let x = 1; let y = x + 2;', '[1, 2, 3];', '[];', '[a, [b, c]];',
'if (x < 10) { x = x + 1; } else { y(); }', 'while (i) { i = i - 1; }',
'function f(a, b) { return a + b; }', 'var s = "hi\\"x"; // c\n s.length;',
'/* block */ a;', 'a === b !== c;', 'a && b || c;', '!a && -b;',
'return;', 'return a + b;', 'const PI = 3;', '{ a; b; }',
'f(g(h(x)), [1, 2], y.z);', 'while (a < b) { if (c) { d(); } e = e + 1; }',
],
// (note: `let = 1;` is VALID minijs — no reserved-word guard, so `let` is an
// identifier and it's an assignment expression; the oracle accepts it too.)
reject: ['1 +;', '(1;', 'if x {}', 'foo(a,;', 'a.;', '[1,', 'function (){}'],
},
{
// The general token-pattern matcher (stateless real-JS token tier): \u-escaped
// identifiers, the decimal/hex number family with a boundary, both-quote strings —
// compiled to a backtracking-free matcher in all three targets.
grammar: 'richtokens', path: './fixtures/richtokens.ts',
accept: [
'123', '0xFF', '1_000_000', '3.14', 'foo', 'bar_$x9', '"hi"', "'single'",
'"esc\\"q\\n"', '123 0xa foo "s" 3.14', '0xDEADbeef 42 _id $x cafe // line\n 7',
'/* block */ 99 x', 'caf\\u00e9 \\u0041bc', '1_2_3 0X1F 10.5 a1 b2',
],
reject: ['12abc', '0x', '"unterminated', '3.', '#'], // ($ is a valid identifier start, not a reject)
},
{
// The STATEFUL regex-vs-division lexer: `/` is a regex in expression context, division
// after a value. Exercises every branch of prevIsValue — after `=`/keyword/`(`-head
// (regex) vs after value/`)`/`]`/member/call (division), plus regex escapes & classes.
grammar: 'regexjs', path: './fixtures/regexjs.ts',
accept: [
'a / b;', 'var r = /abc/g;', 'return /re/;', 'if (x) /re/;', '(a + b) / c;',
'a.b / c;', 'foo(x) / y;', '[1, 2] / 3;', 'var x = a / b / c;',
'var re = /[a-z]+/i; x / y;', 'f(/re/, a / b);', 'var z = /a\\/b/;',
'var d = /\\d+\\w/g;', 'var k = /[\\]]/;', 'if (a) /x/; else b / c;',
],
// (`var ;` is VALID — `var` is an identifier, so it's the expression statement `var;`.)
reject: ['a / ;', 'if (x /re/;', '/re/', '* a;', 'a = = b;'],
},
{
// STATEFUL template literals: the `${…}` interpolation split (head/middle/tail) with a
// brace-depth stack — adjacent/multiple holes, exprs in holes, nested templates, and a
// nested `{…}` object inside a hole (which must NOT close the hole).
grammar: 'templatejs', path: './fixtures/templatejs.ts',
accept: [
'var a = `hello`;', 'var b = `hi ${name}!`;', 'var c = `${x}${y}`;',
'var d = `a${ x + 1 }b${ y * 2 }c`;', 'var e = `outer ${ `inner ${z}` } end`;',
'var f = `${ {a} }`;', 'var f2 = `${ {a, b} } and ${ c }`;', 'var g = `no holes $ here`;',
'f(`${a}`, `${b}`);', 'var h = `${a}${b}${c}`;', 'return `${ {x, y} }`;',
'tag`hello`;', 'tag`${a}${b}`;', 'String.raw`a${b}c`.length;', 'x.tag`${y}`;', // tagged (postfix-token LED)
],
reject: ['var x = `${ }`;', 'var y = `${a`;', '`${a} ${}`;'],
},
{
// Newline-only mode (engine-emitted NEWLINE at significant line boundaries; flow suspension).
grammar: 'envspec', path: './fixtures/envspec.ts',
accept: [
'A=1', 'A=1\nB=2', 'A=1\n', 'A=1\n# c\nB=2', 'A=fn(1,\n2)\nB=3',
'A=1\n\n\nB=2', '\n\nA=1', 'A=fn(1,\n2)',
],
reject: ['A B', 'A=', 'A=1 B=2', 'A=fn(1,'],
},
{
// General (non-literal) inline alt: object keys are alt(Ident | Str | Number) — a
// backtracking alternation of token refs inside a rule sequence.
grammar: 'altjs', path: './fixtures/altjs.ts',
accept: [
'{a: 1};', '{"k": 2};', '{1: x};', '{a: 1, "b": 2, 3: c};', '{x: 1 + 2 * 3};',
'({nested: {inner: 1}});', '{};', 'a + b;', '{k: (1 + 2)};',
],
reject: ['{a:};', '{: 1};', '{a 1};', '{a: 1,, b: 2};'],
},
{
// General Pratt NUD sequences: a reserved-word-guarded identifier (`not(kw)… Ident`,
// a zero-width negative lookahead) and a quantifier-first class expression.
grammar: 'nudjs', path: './fixtures/nudjs.ts',
accept: [
'x;', 'foo + bar;', 'class C {};', 'class {};', 'class C extends B {};',
'@dec class C { m(){} };', 'new Foo;', 'new C();', 'a.b.c;',
'class C { @x m(){} n(){} };', 'x + class {} + y;',
],
reject: ['if;', 'class;', 'new;', 'return + 1;'], // reserved words can't be bare identifiers
},
{
// Postfix-operator LED (`x++`/`x--`) + the access-tail closure: once a postfix binds, the
// operand is an update expression, so a further postfix or an access tail (`.`/`[`/`(`)
// can't attach (`a++--`, `a++.b` are ill-formed; `(a++).b` is fine).
grammar: 'postjs', path: './fixtures/postjs.ts',
accept: [
'x++;', 'x--;', 'a + b++;', '++x;', 'x++ + y;', 'a.b++;', '(x)++;', '--a.b;',
'x++ * 2;', '(a++).b;', 'x.y.z++;',
],
reject: ['a++--;', 'a++.b;', 'a++ ++;', '++;'],
},
{
// A grouped sub-sequence `seq` step: comma lists as `star([',', $])` (e.g. `many(',', $)`),
// the array/argument-list shape javascript.ts uses.
grammar: 'seqjs', path: './fixtures/seqjs.ts',
accept: [
'[1, 2, 3];', '[];', '[1];', 'f(1, 2);', 'f();', '[a + b, c];',
'f(g(1, 2), 3);', '(x);', 'f(a)(b, c);', '[[1,2],[3,4]];',
],
reject: ['[1 2];', 'f(1,);', '[, 1];', 'f(1 2);'],
},
{
// The `sameLine` zero-width assertion (no line terminator before the next token):
// `return` takes a value only on the same line. Also verifies the lexer's newline-before
// tracking across a block comment that spans a newline.
grammar: 'sljs', path: './fixtures/sljs.ts',
accept: [
'return 1;', 'return;', 'return 1 + 2;', '1 + 2;', 'return /* c */ 1;',
'(a);', 'return (1);', 'return\t1;',
],
// `\r`, LS, PS are JS line terminators just like `\n` (ASI / "no LineTerminator here"), so a
// `return` followed by any of them takes no operand — across all four lexers (interpreter,
// emitted JS, portable ts/go/rust). A `\t` (tab) is whitespace but NOT a terminator → accepted above.
reject: ['return\n1;', 'return\nx;', 'return /*\n*/ 1;', 'return // c\n 1;', 'return\r1;', 'return\r\n1;', 'return /*\r*/ 1;', 'return\u20281;', 'return\u2029x;'],
},
{
// capBelow (assignment-level) arrow functions: a NUD parsed only when minBp < the
// connector's bp, admitting NO led once parsed; the `(x) => y` vs `(x)` ambiguity is
// resolved by longest-match ordering (the arrow is tried first, falls back to grouping).
grammar: 'arrowjs', path: './fixtures/arrowjs.ts',
accept: [
'x => x;', '(a, b) => a + b;', '() => {};', 'x = (() => 1);', 'f(() => 1, 2);',
'(x);', 'a + b;', 'x => y => x;', '(() => 2);', '(a) => a;', 'x = y => y;', 'foo();',
'(a,) => b;', '(a, b,) => a;', // trailing comma in params (sep allows a trailing delimiter)
],
reject: ['=> x;', 'x => ;', '1 + () => 2;', '(,) => b;'],
},
{
// Precedence-gated mixfix LEDs: ternary `? :` (binds below the operators) and the
// chain-rhs relational leds `in`/`instanceof` (`a in b in c` left-chains).
grammar: 'ledjs', path: './fixtures/ledjs.ts',
accept: [
'a == b ? c : d;', 'a ? b : c ? d : e;', 'a + b ? c : d - e;', 'a in b;',
'a in b in c;', 'x instanceof Y;', 'a < b in c;', '1 + 2 * 3 ? 4 : 5;',
'(a ? b : c) + d;', 'a in b ? c : d;', 'a = b ? c : d;',
],
reject: ['a ? b;', 'a ? : c;', 'in b;', 'a instanceof;'],
},
{
// The no-`in` (suppress) context: a `for (binding in iterable)` head parses its binding
// with the `in` led disabled, so `in` belongs to the for-head, not the binding.
grammar: 'noinjs', path: './fixtures/noinjs.ts',
accept: [
'for (x in y) z;', 'x in y;', 'for (a.b in c) d;', 'a in b in c;',
'for ((x) in y) z;', 'for (x in y) a in b;', 'for (x in a in b) z;',
'(a in b);', 'for (a in b) for (c in d) e;',
],
reject: ['for (x y) z;', 'for x in y;', 'for (in y) z;', 'for (x in) z;'],
},
{
// The REAL javascript.ts grammar (89 rules after the [Await]/[Yield] fork) — the proof
// that the target-agnostic emitter handles a full language end-to-end in ts/go/rust.
// ASCII corpus only (byte-based go/rust use UTF-8 offsets, identical to the JS oracle's
// UTF-16 offsets for ASCII; non-ASCII offset units differ inherently).
grammar: 'javascript', path: '../javascript.ts',
accept: [
'var x = 1, y = 2;', 'function f(a, b) { return a + b; }', 'const g = (x) => x * 2;',
'x => x + 1;', 'a ? b : c;', 'a.b.c();', 'f(g(1, 2), 3);', '[1, 2, 3].map(f);',
'for (let i = 0; i < n; i++) x();', 'for (const k in obj) { y(); }', 'while (x) { z(); }',
'if (a) b(); else c();', 'class C extends B { m() {} get p() { return 1; } }', 'a++; b--;',
'typeof x; void 0;', 'new Foo(1, 2); new.target;', 'a ?? b; a?.b?.c;',
'try { f(); } catch (e) { g(); } finally { h(); }', 'switch (x) { case 1: f(); break; default: g(); }',
'a instanceof B; a in obj;', '(function () {})(); (() => {})();', 'x = a && b || c;',
'do { x(); } while (y);', 'function* gen() { yield* o(); }', 'const { a, b: c, ...r } = o;',
'const [p, , q, ...z] = arr;', 'label: for (;;) { break label; }', 'async function h() { await x; }',
'(() => {})();', '(() => {})() + 1;', 'f(() => a, () => b);', 'let g = () => x => x + 1;',
'x.y.z++;', '(a++).b;', 'a++ instanceof B;', 'a++(x);',
'for (a in b) c;',
'for (a = x in b) c;',
'for (a in b) { d = c in e; }',
'new a.b();', '((((x))));',
'for (a = x + y in b) c;',
],
reject: ['function (', 'a +;', 'if x {}', '{ a: }', 'for (;;', 'a ? b ;',
'1 + () => 2;', 'a++.b;', 'f(a, b,',
'for (a = (x in b)) c;',
'for (a = b ? x in c : y in d) e;'],
},
{
// The real typescript.ts grammar — the second, most complex full language proving the
// agnostic emitter (types, generics, interfaces, enums, assertions, variance). ASCII.
grammar: 'typescript', path: '../typescript.ts',
accept: [
'const a: number = 1;', 'let s: string;', 'type Alias = { a: number; b?: string };',
'type U = "a" | "b" | "c";', 'function gen2<T, U extends T>(x: T, y: U): T { return x; }',
'interface I<T> extends A<T> { m(x: T): T; }', 'const c = x as const;',
'function isStr(x: unknown): x is string { return true; }', 'enum E { A, B, C }',
'const n = maybe!;', 'let arr: number[];', 'type Fn = (x: number) => string;',
'class C<in out T> { value!: T; }',
],
reject: ['interface {}', 'const x: = 1;', 'enum {}', 'a + ;'],
},
];
const sortKeys = (o: unknown): unknown =>
Array.isArray(o) ? o.map(sortKeys)
: (o && typeof o === 'object') ? Object.fromEntries(Object.keys(o as object).sort().map((k) => [k, sortKeys((o as Record<string, unknown>)[k])]))
: o;
const canon = (o: unknown) => JSON.stringify(sortKeys(o));
// Rule-node skeleton: the tree of rule labels with all leaves (trivia/punct/semantic tokens)
// stripped. Two CSTs that differ only in their leaves — e.g. after Phase 5's trivia compression
// — share a skeleton; a semantic restructure (a node relabeled, merged, or dropped) does not.
// A node is a RULE node iff it carries a non-empty `rule` label (Rust leaves use rule:""; TS
// leaves omit the key entirely), so this is target-agnostic over the canon'd JSON.
const skelObj = (o: unknown): unknown => {
if (Array.isArray(o)) return o.map(skelObj).filter((x) => x !== null);
if (o && typeof o === 'object') {
const r = (o as { rule?: string }).rule;
if (r && r !== '') return { r, k: skelObj((o as { children?: unknown }).children ?? []) };
}
return null;
};
const skeleton = (cstStr: string) => skelObj(JSON.parse(cstStr));
const TMP = '/tmp/portable-targets';
rmSync(TMP, { recursive: true, force: true });
mkdirSync(TMP, { recursive: true });
const have = (cmd: string, args: string[]) => { try { execFileSync(cmd, args, { stdio: 'pipe' }); return true; } catch { return false; } };
const HAS_GO = have('go', ['version']);
const HAS_RUST = have('rustc', ['--version']);
if (!HAS_GO) console.log(' go: (toolchain absent — skipped)');
if (!HAS_RUST) console.log(' rust: (toolchain absent — skipped)');
type Outcome = { ok: true; cst: string } | { ok: false };
function runProc(cmd: string, args: string[], src: string): Outcome {
try { return { ok: true, cst: canon(JSON.parse(execFileSync(cmd, args, { input: src, stdio: ['pipe', 'pipe', 'pipe'] }).toString())) }; }
catch { return { ok: false }; }
}
function runTokSpans(cmd: string, args: string[], src: string): string | null {
const r = spawnSync(cmd, args, { input: src, encoding: 'utf8' });
if (r.status !== 0) return null;
return r.stdout;
}
type Align = { oldN: number; newN: number; prefix: number; suffix: number; relexed?: number; reused?: number; streamEq?: boolean; treeEq?: boolean };
type EditOutcome = Outcome & { align?: Align };
function parseAlignStderr(stderr: string): Align | undefined {
for (const line of stderr.split('\n')) {
const t = line.trim();
if (!t.startsWith('{')) continue;
try { return JSON.parse(t) as Align; } catch { /* keep scanning */ }
}
return undefined;
}
function runEditSession(cmd: string, args: string[], json: string): EditOutcome {
const r = spawnSync(cmd, args, { input: json, encoding: 'utf8' });
const align = parseAlignStderr(r.stderr ?? '');
if (r.status !== 0) return { ok: false, align };
try {
return { ok: true, cst: canon(JSON.parse(r.stdout)), align };
} catch { return { ok: false, align }; }
}
type AlignMeta = { kind: string; off: number; end: number; nl: boolean };
const oracleToks = (g: CstGrammar, src: string): AlignMeta[] => {
const { tokenize } = createLexer(g);
return tokenize(src).map((t) => ({ kind: t.type, off: t.offset, end: t.offset + t.text.length, nl: t.newlineBefore }));
};
const computeAlign = (oldText: string, oldToks: AlignMeta[], newText: string, newToks: AlignMeta[]): Align => {
const oldN = oldToks.length, newN = newToks.length;
let prefix = 0;
while (prefix < oldN && prefix < newN) {
const o = oldToks[prefix], n = newToks[prefix];
if (o.kind !== n.kind || o.off !== n.off || o.end !== n.end || o.nl !== n.nl) break;
if (oldText.slice(o.off, o.end) !== newText.slice(n.off, n.end)) break;
prefix++;
}
const delta = newText.length - oldText.length;
const minN = Math.min(oldN, newN);
let suffix = 0;
while (prefix + suffix < minN) {
const o = oldToks[oldN - 1 - suffix], n = newToks[newN - 1 - suffix];
if (o.kind !== n.kind || o.nl !== n.nl || n.off !== o.off + delta || n.end !== o.end + delta) break;
if (oldText.slice(o.off, o.end) !== newText.slice(n.off, n.end)) break;
suffix++;
}
return { oldN, newN, prefix, suffix };
};
const expectAlign = (g: CstGrammar, init: string, batches: EditBatch[]): Align => {
let text = init;
for (let i = 0; i < batches.length - 1; i++) for (const [s, e, r] of batches[i]) text = text.slice(0, s) + r + text.slice(e);
const oldText = text;
const batch = batches[batches.length - 1];
for (const [s, e, r] of batch) text = text.slice(0, s) + r + text.slice(e);
return computeAlign(oldText, oracleToks(g, oldText), text, oracleToks(g, text));
};
type EditBatch = [number, number, string][];
type EditScenario = {
init: string;
batches: EditBatch[];
large?: boolean;
maxRelexed?: number;
fullRelex?: boolean;
/** Minimum reused top-level kids (tree reuse) — all targets. */
minReused?: number;
/** Exact reused count — all targets. */
reused?: number;
/** Reused must be strictly less than this (lookahead / ext gate) — all targets. */
maxReused?: number;
/** Long arena session: assert treeEq after every cumulative batch prefix. */
longSession?: boolean;
};
const calcLargeInit = '1+2*3+'.repeat(199) + '1+2*3';
const calcReuseInit = Array.from({ length: 40 }, (_, i) => `let x${i}=${i};`).join('\n');
const calcReuseMid = calcReuseInit.indexOf('let x20=') + 'let x20='.length;
const calcReuseFirstEq = calcReuseInit.indexOf('=') + 1;
const calcReuseLastEq = calcReuseInit.lastIndexOf('=') + 1;
const calcReuseSemi = calcReuseInit.indexOf(';');
const calcLongInit = Array.from({ length: 80 }, (_, i) => `let y${i}=${i};`).join('\n');
const calcLongBatches: EditBatch[] = Array.from({ length: 24 }, (_, i) => {
const needle = `let y${i}=`;
const at = calcLongInit.indexOf(needle) + needle.length;
return [[at, at + 1, String((i * 7 + 3) % 10)]] as EditBatch;
});
const envspecLargeInit = 'A=1\n'.repeat(300);
const envReuseInit = Array.from({ length: 40 }, (_, i) => `K${i}=${i}`).join('\n');
const envReuseMid = envReuseInit.indexOf('K20=') + 'K20='.length;
const envReuseFirstEq = envReuseInit.indexOf('=') + 1;
const envReuseLastEq = envReuseInit.lastIndexOf('=') + 1;
const envFlowInit = 'A=1\nB=fn(a,\nb)\nC=3';
const envFlowEdit = envFlowInit.indexOf('fn(a') + 'fn('.length; // 'a' inside hanging flow stmt
const envMultiBatches: EditBatch[] = [
[[envReuseMid, envReuseMid + 1, '9']],
[[envReuseInit.indexOf('K5=') + 'K5='.length, envReuseInit.indexOf('K5=') + 'K5='.length + 1, '7']],
[[envReuseLastEq, envReuseLastEq + 1, '8']],
];
// Shape-B adopted Seg.Ext must shift with tokDelta: insert-front (adopt suffix) then poke a later
// value whose align.prefix sits in the stale-ext..shifted-ext window — unshifted Ext over-reuses.
const envSegExtInit = Array.from({ length: 8 }, (_, i) => `K${i}=${i}`).join('\n');
const envSegExtIns = 'Z=9\n';
const envSegExtPoke = envSegExtInit.indexOf('K3=') + 'K3='.length + envSegExtIns.length;
const regexjsLargeInit = 'var r = /abc/g; a / b;\n'.repeat(60);
const jsLookaheadInit = 'let x = (a)\nfoo;\nbar;';
const jsLookaheadEdit = jsLookaheadInit.indexOf('\n') + 1; // first char of peeked-into second stmt
const EDIT_SCENARIOS: Record<string, EditScenario[]> = {
calc: [
{ init: '1+2*3', batches: [[[3, 3, '4']]] },
{ init: '1+2*3', batches: [[[1, 3, '']]] },
{ init: '1+2*3', batches: [[[2, 5, '(7-8)']]] },
{ init: '1+2*3', batches: [[[0, 0, '9-']], [[7, 8, '']]] },
{ init: calcLargeInit, batches: [[[600, 601, '9']]], large: true, maxRelexed: 8 },
{ init: '1 2;', batches: [[[1, 2, '']]] },
{ init: calcLargeInit, batches: [[[calcLargeInit.length, calcLargeInit.length, '+4']]], maxRelexed: 5 },
{ init: calcLargeInit, batches: [[[0, 1, '9']]], maxRelexed: 8 },
// S7b tree-reuse scenarios
{ init: calcReuseInit, batches: [[[calcReuseMid, calcReuseMid + 1, '9']]], minReused: 30 },
{ init: calcReuseInit, batches: [[[calcReuseFirstEq, calcReuseFirstEq + 1, '9']]], minReused: 1 },
{ init: calcReuseInit, batches: [[[calcReuseLastEq, calcReuseLastEq + 1, '9']]], minReused: 1 },
{ init: calcReuseInit, batches: [[[calcReuseSemi - 1, calcReuseSemi + 1, '9;']]], minReused: 1 },
{ init: calcReuseInit, batches: [[[calcReuseMid, calcReuseMid + 1, '9']], [[calcReuseMid, calcReuseMid + 1, '8']]], minReused: 30 },
// Boundary-hit must be exact (===): merging b;c via `;`→`+` lands on `d`; a `>=` mutant would adopt too early.
{ init: 'a;\nb;\nc;\nd;', batches: [[[4, 5, '+']]] },
// Mid overshoot past suffix candidates → tryReuseTop fails, full-parse fallback (reused:0) still ≡ fresh.
{ init: 'a;\nb;\nc;\nd;', batches: [[[7, 8, '+']]], reused: 0 },
// S7c long arena session: ≥20 continuous edits; each cumulative prefix treeEq + final ≡ fresh.
{ init: calcLongInit, batches: calcLongBatches, longSession: true, minReused: 1 },
// Reject-then-edit: batch 1 breaks the doc; batch 2 must not resurrect a partial old
// tree via reuse (a Go parse wrapper without the full-consumption guard did exactly that).
{ init: 'let a = 1;\nlet b = 2;\nlet c = 3;', batches: [[[19, 20, '']], [[29, 30, '4']]], reused: 0 },
// Reject-then-repair: same setup, batch 2 restores validity; must parse fresh (reused:0) ≡ fresh.
{ init: 'let a = 1;\nlet b = 2;\nlet c = 3;', batches: [[[19, 20, '']], [[19, 19, '2']]], reused: 0 },
],
javascript: [
{ init: 'let a = 1;\nf(a);', batches: [[[8, 9, '42']]] },
{ init: 'let a = 1;\nf(a);', batches: [[[11, 16, '']]] },
{ init: 'let a = 1;\nf(a);', batches: [[[4, 5, 'b'], [12, 13, 'b']]] },
{ init: 'let a = 1;\nf(a);', batches: [[[16, 16, '\ng(b);']], [[0, 4, 'var']]] },
{ init: 'let a = 1;\nf(a);\n'.repeat(80), batches: [[[688, 689, '9']]], large: true, maxRelexed: 15 },
{ init: 'var q = `x${a/b}y`;', batches: [[[12, 13, 'c']]] },
// predecessor peeked into second stmt (ext > tokEnd); editing that region must not keep stmt0
{ init: jsLookaheadInit, batches: [[[jsLookaheadEdit, jsLookaheadEdit + 1, '+']]], maxReused: 1 },
],
templatejs: [
{ init: '`a${x}b`;', batches: [[[4, 5, 'y']]] },
{ init: '`a${ {x} }b`;', batches: [[[6, 7, 'y']]] },
{ init: '`a${x}b`;', batches: [[[2, 4, '']]] },
{ init: '`a${x}b${y}c`;', batches: [[[6, 7, 'z']]] },
{ init: ('`a${x}b`; f(1);\n').repeat(50), batches: [[[600, 601, '9']]], large: true, maxRelexed: 15 },
{ init: '`a`; z; `b${x}c`;', batches: [[[5, 6, 'w']]] },
],
envspec: [
// Existing scenes — reused pinned from S8 measurement (was 0 under shape-A-only).
{ init: 'A=1\nB=2', batches: [[[2, 2, '3']]], minReused: 1 },
{ init: envspecLargeInit, batches: [[[600, 601, '9']]], large: true, maxRelexed: 10, minReused: 200 },
// Comma→digit rejects the doc → full-parse fallback, reused:0.
{ init: 'A=fn(1,\n2)\nB=3', batches: [[[6, 7, '9']]], reused: 0 },
{ init: 'A=fn(1,\n2)\nB=3', batches: [[[4, 4, '(']]], reused: 0 },
{ init: 'A=1\n# note\nB=2', batches: [[[7, 7, 'x']]], minReused: 1 },
{ init: 'A=1\n\n\nB=2', batches: [[[4, 4, '\n']]], minReused: 1 },
{ init: envspecLargeInit, batches: [[[envspecLargeInit.length, envspecLargeInit.length, 'Z=9']]], large: true, maxRelexed: 10, minReused: 200 },
{ init: 'A=fn(1,\n2)\nB=3', batches: [[[0, 0, 'X']], [[7, 7, '0']]], minReused: 1 },
// S8 segment-reuse scenes
{ init: envReuseInit, batches: [[[envReuseMid, envReuseMid + 1, '9']]], minReused: 30 },
{ init: envReuseInit, batches: [[[envReuseFirstEq, envReuseFirstEq + 1, '9']]], minReused: 1 },
{ init: envReuseInit, batches: [[[envReuseLastEq, envReuseLastEq + 1, '9']]], minReused: 1 },
{ init: 'A=1\n\n\n\nB=2', batches: [[[4, 5, '']]], minReused: 1 }, // blank-line fold
{ init: 'A=1\n\nB=2', batches: [[[4, 4, '\n']]], minReused: 1 }, // blank-line expand
{ init: envFlowInit, batches: [[[envFlowEdit, envFlowEdit + 1, 'x']]], minReused: 2 }, // mid flow stmt; sides reused
{ init: envReuseInit, batches: envMultiBatches, longSession: true, minReused: 1 },
// Go adopted Seg.Ext + tokDelta (pre-fix: reused 9 from stale keep-prefix watermarks).
{ init: envSegExtInit, batches: [[[0, 0, envSegExtIns]], [[envSegExtPoke, envSegExtPoke + 1, '9']]], reused: 8 },
],
regexjs: [
{ init: 'var re = /[a-z]+/i; x / y;', batches: [[[11, 12, '0']]] },
{ init: 'x = a / b / c;', batches: [[[4, 6, '']]] },
{ init: 'if (x) /y/;', batches: [[[0, 2, 'f']]] },
{ init: regexjsLargeInit, batches: [[[600, 601, '9']]], large: true, maxRelexed: 15 },
{ init: 'f(x); a / b;', batches: [[[2, 2, '(']]] },
{ init: 'if (x) /y/;', batches: [[[11, 11, ' /z/;']]] },
],
};
const applyEdits = (init: string, batches: EditBatch[]): string => {
let text = init;
for (const batch of batches) for (const [start, end, repl] of batch) text = text.slice(0, start) + repl + text.slice(end);
return text;
};
let failures = 0;
for (const c of CASES) {
const grammar: CstGrammar = (await import(c.path)).default;
const oracle = createParser(grammar);
const oracleOut = (src: string): Outcome => { try { return { ok: true, cst: canon(oracle.parse(src)) }; } catch { return { ok: false }; } };
const dir = `${TMP}/${c.grammar}`;
mkdirSync(dir, { recursive: true });
const runners: Array<{ label: string; run: (src: string) => Outcome }> = [];
// emitParser is the parser LIBRARY (exports a `parse` entry, no I/O); the executable CLI
// runner (stdin → CST JSON) is the target's emitRunner(), assembled in HERE — the harness's
// job, not the parser's. ts/rust append the runner to the library (one file); Go needs it as
// a SEPARATE runner.go in the same `package main` (its import rules forbid appending inline).
const tsFile = `${dir}/p.ts`;
writeFileSync(tsFile, emitParser(grammar, tsTarget) + (tsTarget.emitRunner?.() ?? ''));
runners.push({ label: 'typescript', run: (src) => runProc('node', [tsFile], src) });
if (HAS_GO && !c.tsOnly) {
const gdir = `${dir}/go`; mkdirSync(gdir, { recursive: true });
writeFileSync(`${gdir}/parser.go`, emitParser(grammar, goTarget));
writeFileSync(`${gdir}/runner.go`, goTarget.emitRunner?.() ?? '');
writeFileSync(`${gdir}/go.mod`, 'module p\n\ngo 1.21\n');
execFileSync('go', ['build', '-o', `${gdir}/p`, '.'], { cwd: gdir, stdio: 'pipe' });
runners.push({ label: 'go', run: (src) => runProc(`${gdir}/p`, [], src) });
}
if (HAS_RUST && !c.tsOnly) {
const rfile = `${dir}/main.rs`;
writeFileSync(rfile, emitParser(grammar, rustTarget) + (rustTarget.emitRunner?.() ?? ''));
execFileSync('rustc', ['-O', '-A', 'warnings', rfile, '-o', `${dir}/pr`], { stdio: 'pipe' });
runners.push({ label: 'rust', run: (src) => runProc(`${dir}/pr`, [], src) });
}
// tok-spans: top-level kids record parse-consumption [tokStart, tokEnd); cross-target
// identity + contiguous partition of the consumed token stream. Empty accept inputs are
// still run (identity), but contiguous checks skip when there are no kids.
// envspec: Newline tokens are zero-width in the byte sense but still occupy a token index;
// if contiguous ever fails here, partition by grammar with evidence rather than relaxing.
{
const spanRunners: Array<{ label: string; cmd: string; args: string[] }> = [
{ label: 'typescript', cmd: 'node', args: [tsFile, 'tok-spans'] },
];
if (HAS_GO && !c.tsOnly) spanRunners.push({ label: 'go', cmd: `${dir}/go/p`, args: ['tok-spans'] });
if (HAS_RUST && !c.tsOnly) spanRunners.push({ label: 'rust', cmd: `${dir}/pr`, args: ['tok-spans'] });
const parseSpans = (out: string) => {
const lines = out.replace(/\r\n/g, '\n').replace(/\r/g, '\n').trimEnd().split('\n').filter(Boolean);
const totalLine = lines[lines.length - 1]!;
const kids = lines.slice(0, -1).map((line) => {
const [name, a, b] = line.split('\t');
return { name: name!, tokStart: Number(a), tokEnd: Number(b) };
});
const [, , tot] = totalLine.split('\t');
return { kids, total: Number(tot) };
};
const assertPartition = (src: string, out: string, label: string) => {
const { kids, total } = parseSpans(out);
if (kids.length === 0) {
if (total !== 0) {
failures++;
console.log(` ${c.grammar}/${label}: tok-spans empty kids but total=${total} on ${JSON.stringify(src)}`);
}
return;
}
if (kids[0]!.tokStart !== 0) {
failures++;
console.log(` ${c.grammar}/${label}: tok-spans first tokStart want=0 got=${kids[0]!.tokStart} on ${JSON.stringify(src)}`);
}
if (kids[kids.length - 1]!.tokEnd !== total) {
failures++;
console.log(` ${c.grammar}/${label}: tok-spans last tokEnd want=${total} got=${kids[kids.length - 1]!.tokEnd} on ${JSON.stringify(src)}`);
}
for (let i = 0; i < kids.length; i++) {
const k = kids[i]!;
if (k.tokStart > k.tokEnd) {
failures++;
console.log(` ${c.grammar}/${label}: tok-spans inverted span ${k.name} [${k.tokStart},${k.tokEnd}) on ${JSON.stringify(src)}`);
}
if (i > 0 && kids[i - 1]!.tokEnd !== k.tokStart) {
failures++;
console.log(` ${c.grammar}/${label}: tok-spans gap/overlap at kid ${i}: prevEnd=${kids[i - 1]!.tokEnd} start=${k.tokStart} on ${JSON.stringify(src)}`);
console.log(` spans:\n${out}`);
}
}
};
const cases = c.accept.slice(0, 2);
for (const src of cases) {
const outs: Array<{ label: string; out: string | null }> = spanRunners.map((r) => ({
label: r.label,
out: runTokSpans(r.cmd, r.args, src),
}));
const ref = outs[0]!;
if (ref.out === null) {
failures++;
console.log(` ${c.grammar}/${ref.label}: tok-spans reject/fail on accept case ${JSON.stringify(src)}`);
continue;
}
for (const o of outs.slice(1)) {
if (o.out === null) {
failures++;
console.log(` ${c.grammar}/${o.label}: tok-spans reject/fail on accept case ${JSON.stringify(src)}`);
continue;
}
if (o.out !== ref.out) {
failures++;
console.log(` ${c.grammar}: tok-spans cross-target mismatch ${ref.label}≢${o.label} on ${JSON.stringify(src)}`);
console.log(` ${ref.label}: ${JSON.stringify(ref.out)}`);
console.log(` ${o.label}: ${JSON.stringify(o.out)}`);
}
}
for (const o of outs) {
if (o.out !== null) assertPartition(src, o.out, o.label);
}
}
if (c.grammar === 'calc') {
const src = 'let a=1;\n2+3;\nlet b=a;';
const out = runTokSpans(spanRunners[0]!.cmd, spanRunners[0]!.args, src);
if (out === null) {
failures++;
console.log(` calc: tok-spans reject on trailing-punct probe`);
} else {
for (const r of spanRunners.slice(1)) {
const o = runTokSpans(r.cmd, r.args, src);
if (o !== out) {
failures++;
console.log(` calc: tok-spans cross-target mismatch on trailing-punct probe (${r.label})`);
}
}
const { kids } = parseSpans(out);
const first = kids[0];
if (!first || first.tokEnd !== 5) {
failures++;
console.log(` calc: first Stmt tokEnd want=5 got=${first?.tokEnd} (name=${first?.name}) on trailing-punct probe`);
console.log(` spans:\n${out}`);
} else {
console.log(` calc: tok-spans trailing-punct Stmt tokEnd=5 ✓`);
}
}
}
console.log(` ${c.grammar}: tok-spans checked on ${cases.length} accept case(s)${c.grammar === 'calc' ? ' + trailing-punct probe' : ''}`);
}
for (const r of runners) {
let acc = 0, rej = 0, snap = 0;
for (const src of c.accept) {
const want = oracleOut(src), got = r.run(src);
if (want.ok && got.ok) {
acc++;
// Shape snapshot on every accepted input: the rule-node skeleton (rule labels tree,
// trivia/punct leaves stripped) must match the oracle's. Robust to leaf-level CST
// compression (Phase 5) but catches semantic restructure (a node becoming a different
// rule / merging / disappearing) — e.g. the rust prefix arm once dropped its operand
// subtree, visible only on inputs beyond the first two.
if (src && canon(skeleton(want.cst)) !== canon(skeleton(got.cst))) {
snap++;
failures++;
console.log(` ${c.grammar}/${r.label}: SHAPE drift on ${JSON.stringify(src)}`);
console.log(` want ${canon(skeleton(want.cst)).slice(0, 160)}`);
console.log(` got ${canon(skeleton(got.cst)).slice(0, 160)}`);
}
continue;
}
failures++;
console.log(` ${c.grammar}/${r.label}: ACCEPT mismatch on ${JSON.stringify(src)} (oracle ok=${want.ok}, ${r.label} ok=${got.ok})`);
}
for (const src of c.reject) {
const want = oracleOut(src), got = r.run(src);
if (!want.ok && !got.ok) { rej++; continue; }
failures++;
console.log(` ${c.grammar}/${r.label}: REJECT mismatch on ${JSON.stringify(src)} (oracle ok=${want.ok}, ${r.label} ok=${got.ok})`);
}
console.log(` ${c.grammar}/${r.label}: ${acc}/${c.accept.length} accept ≡ oracle · ${rej}/${c.reject.length} reject ≡ oracle${snap ? ` · ${snap} shape drift` : ''}`);
const editScenarios = EDIT_SCENARIOS[c.grammar];
if (editScenarios) {
let editOk = 0, alignOk = 0, fastOk = 0;
const editCmd = r.label === 'typescript' ? 'node' : r.label === 'go' ? `${dir}/go/p` : `${dir}/pr`;
const editArgs = r.label === 'typescript' ? [`${dir}/p.ts`, 'edit-session'] : ['edit-session'];
const fastArgs = r.label === 'typescript' ? [`${dir}/p.ts`, 'edit-session-fast'] : ['edit-session-fast'];
const runEdit = (json: string) => runEditSession(editCmd, editArgs, json);
const runFast = (json: string) => runEditSession(editCmd, fastArgs, json);
for (const sc of editScenarios) {
const final = applyEdits(sc.init, sc.batches);
const payload = JSON.stringify({ init: sc.init, batches: sc.batches });
const a = runEdit(payload);
const b = r.run(final);
if (a.ok === b.ok && (!a.ok || a.cst === b.cst) && a.ok === oracleOut(final).ok) editOk++;
else {
failures++;
console.log(` ${c.grammar}/${r.label}: edit-session mismatch (final=${JSON.stringify(final)}) A ok=${a.ok} B ok=${b.ok}`);
}
const want = expectAlign(grammar, sc.init, sc.batches);
if (a.align && a.align.oldN === want.oldN && a.align.newN === want.newN && a.align.prefix === want.prefix && a.align.suffix === want.suffix) {
alignOk++;
if (sc.large && want.prefix + want.suffix < want.oldN - 8) {
failures++;
console.log(` ${c.grammar}/${r.label}: large-doc align too narrow prefix+suffix=${want.prefix + want.suffix} oldN=${want.oldN}`);
}
if (a.align.streamEq !== true) {
failures++;
console.log(` ${c.grammar}/${r.label}: streamEq want=true got=${JSON.stringify(a.align.streamEq)}`);
}
if (a.align.treeEq !== true) {
failures++;
console.log(` ${c.grammar}/${r.label}: treeEq want=true got=${JSON.stringify(a.align.treeEq)}`);
}
if (a.align.reused === undefined) {
failures++;
console.log(` ${c.grammar}/${r.label}: reused field missing got=${JSON.stringify(a.align)}`);
}
if (sc.reused !== undefined && a.align.reused !== sc.reused) {
failures++;
console.log(` ${c.grammar}/${r.label}: reused want=${sc.reused} got=${JSON.stringify(a.align.reused)}`);
}
if (sc.minReused !== undefined && (a.align.reused === undefined || a.align.reused < sc.minReused)) {
failures++;
console.log(` ${c.grammar}/${r.label}: reused want>=${sc.minReused} got=${JSON.stringify(a.align.reused)}`);
}
if (sc.maxReused !== undefined && (a.align.reused === undefined || a.align.reused > sc.maxReused)) {
failures++;
console.log(` ${c.grammar}/${r.label}: reused want<=${sc.maxReused} got=${JSON.stringify(a.align.reused)}`);
}
if (sc.longSession) {
for (let bi = 1; bi < sc.batches.length; bi++) {
const prefixPayload = JSON.stringify({ init: sc.init, batches: sc.batches.slice(0, bi) });
const p = runEdit(prefixPayload);
if (p.align?.treeEq !== true) {
failures++;
console.log(` ${c.grammar}/${r.label}: longSession batch ${bi} treeEq want=true got=${JSON.stringify(p.align?.treeEq)}`);
}
}
}
if (sc.maxRelexed !== undefined && (a.align.relexed === undefined || a.align.relexed > sc.maxRelexed)) {
failures++;
console.log(` ${c.grammar}/${r.label}: relexed want<=${sc.maxRelexed} got=${JSON.stringify(a.align.relexed)}`);
}
if (sc.fullRelex && a.align.relexed !== want.newN) {
failures++;
console.log(` ${c.grammar}/${r.label}: fullRelex want relexed=${want.newN} got=${JSON.stringify(a.align.relexed)}`);
}
} else {
failures++;
console.log(` ${c.grammar}/${r.label}: token-align mismatch want=${JSON.stringify(want)} got=${JSON.stringify(a.align)}`);
}
const f = runFast(payload);
const sixEq = !!(a.align && f.align
&& a.align.oldN === f.align.oldN && a.align.newN === f.align.newN
&& a.align.prefix === f.align.prefix && a.align.suffix === f.align.suffix
&& a.align.relexed === f.align.relexed && a.align.reused === f.align.reused);
const noValidateKeys = f.align !== undefined && !('streamEq' in f.align) && !('treeEq' in f.align);
const outEq = a.ok === f.ok && (!a.ok || a.cst === f.cst);
if (sixEq && noValidateKeys && outEq) fastOk++;
else {
failures++;
console.log(` ${c.grammar}/${r.label}: fast≢validated sixEq=${sixEq} noValidateKeys=${noValidateKeys} outEq=${outEq} validated=${JSON.stringify(a.align)} fast=${JSON.stringify(f.align)}`);
}
}
console.log(` ${c.grammar}/${r.label}: ${editOk}/${editScenarios.length} edit-sessions ≡ fresh`);
console.log(` ${c.grammar}/${r.label}: ${alignOk}/${editScenarios.length} token-alignments ≡ oracle`);
console.log(` ${c.grammar}/${r.label}: ${fastOk}/${editScenarios.length} fast ≡ validated`);
}
}
}
if (failures > 0) {
console.error(`\n✗ portable targets diverge from the interpreter (${failures} case(s))`);
process.exit(1);
}
console.log('\n✓ portable parsers (ts/go/rust) derived from each grammar ≡ interpreter on accept/reject parity (compiled & run)');