Skip to content

The default tree of the TypeScript, Go and Rust parsers holds every token again, punctuation included - #214

Merged
johnsoncodehk merged 12 commits into
masterfrom
land/lossless-cst
Nov 29, 2026
Merged

johnsoncodehk merged 12 commits into
masterfrom
land/lossless-cst

Conversation

@johnsoncodehk

Copy link
Copy Markdown
Owner

What changes

The tree that the generated TypeScript, Go and Rust parsers build by default now holds every token as a leaf again, punctuation included. Until now these three targets left out (, ), ;, =, , and every other punctuation token, while the reference interpreter kept them and the README said the tree holds every token. Now the three targets give exactly the interpreter's tree.

Code Leaves before Leaves now
let a = f(1); let a f 1 let a = f ( 1 ) ;

Who must change their code: anyone who walks the default tree and reads a node's children by position. The call f(1) above had two children, the callee and the argument. It now has four, with the ( and ) leaves between and after them. Code that picks a child by its kind or rule name is not affected. Go and Rust users of the slim builder see no change: it still keeps only rule nodes and named tokens, as a deliberately smaller tree.

Results

  • On every input of the parity corpus that the interpreter accepts, each target's default tree equals the interpreter's byte for byte. Before, only the shape of the rule nodes was compared, and leaves were ignored.
  • That stricter comparison found one more bug, fixed here. In the TypeScript target, an assignment operator in a destructuring pattern such as [a, b] = x; got a wrong span (start 7, end 6). It now spans its own =.
  • Error recovery quality is unchanged: 60.8% of the JavaScript test mutants and 58.4% of the TypeScript ones keep the tree outside the damaged region, the same as before.

Cost

Instructions per parse, Rust target, a fresh parse, before and after:

input before after change
checker.ts 104,951,632 107,165,448 +2.1%
typescript.js 395,676,728 404,774,112 +2.3%
typescript.d.ts 17,910,165 18,600,815 +3.9%
1 MB of empty statements 227,937,854 283,697,432 +24.5%
  • Go: about 4% more instructions on real files. TypeScript target: about 1% less work but 14% to 20% more allocated objects.
  • Against oxc, counted in instructions with a reused parser: on checker.ts monogram goes from 0.896 to 0.915 of oxc's count. On typescript.d.ts it goes from 0.997 to 1.034, so monogram now executes slightly more instructions than oxc there.
  • Generated parser sizes are unchanged within 0.1%.

Known limits

  • The slim builder of the Go and Rust targets still leaves punctuation out, by design.
  • Wall-clock time against oxc was not measured again. Only instruction counts were.

🤖 Generated with Claude Code

…e TS, Go and Rust targets keeps punctuation leaves

$punct gets a real tt id in ArenaIdPlan (it was the sentinel 255, never stored); the TS cstBuilder,
Go CstBuilder.Leaf and Rust CstBuilder::leaf no longer drop it, and the Go direct engine's matchLit
pushes a $punct leaf for a non-word literal. SlimBuilder still drops it. Every target's default CST
equals the interpreter's CST on the parity corpus of javascript (2487 accepted inputs) and
typescript (2647).
@johnsoncodehk
johnsoncodehk merged commit 2d84373 into master Nov 29, 2026
12 checks passed
johnsoncodehk added a commit that referenced this pull request Nov 29, 2026
…II in the interpreter and the TS target, as in Go and Rust: `$` starts no identifier in a grammar whose identifier pattern refuses it

Before: in a grammar whose identifier token's ASCII pattern does not take `$` (cappedcalc, calc,
envspec, envtpl, guardjs, ledexitjs, lookrec, the pattern* fixtures, startargets), the interpreter
and the TS target lexed `$a` as an identifier, and Go and Rust refused it as an unexpected
character. The TS target accepted `$a;` in cappedcalc; Go and Rust rejected it. Recovery inherited
the difference: on the total-lex inputs (`` `a ${x `` and three more) the TS repairs differed from Go's
and Rust's (the recovery gate's cross-target check P6, 4 mismatches per target), and the last
landing had fed those inputs to the javascript / typescript recovery class only.
Now: all four agree. `$` is lexed as an identifier only where the grammar's own pattern takes it.

Cause: not recovery, the lexer. Each target ends its dispatch with a Unicode IdentifierName
fallback (the identifier classes, ECMAScript's by default: `$`, `_`, ID_Start / ID_Continue).
uni-ident.ts defines it as an extension BEYOND ASCII ("a run starting at a non-ASCII ID_Start
character"): an ASCII character is the token pattern's to take or refuse. Go and Rust guard the
fallback with `c >= 0x80`, and the TS target's continuation (lx_ext) guards `< 0x80`; the
interpreter's fallback (lexer.ts) and the TS target's did not, so the ASCII members of the class
(`$`) reached it. The interpreter was inconsistent with itself too: `$` started an identifier but
did not continue one (`a$` lexed as `a`, `$`).

Fix: the same guard in the interpreter's fallback (src/interpreter/lexer.ts) and the TS target's
(src/targets/target-ts.ts). `$a;` joins cappedcalc's reject list, so the strict lists gate (every
target against the interpreter) sees this class; the recovery gate's narrowing is removed: the
total-lex inputs run in every recovery grammar's class again.

HTML (landing #213 first): html.ts's attribute names were the tag-name pattern, so `<a _b="1">` and
`<a $b="1">` (valid HTML) were accepted only through this fallback; #213 gave html its own
attribute-name token, matched before the fallback. With the guard, html's verdict on every case of
#213's table (25 cases: `_b`, `$b`, `@click`, `:x`, `[p]`, `(e)`, `#r`, `*ngIf`, `1b`, `-b`, `é`, the
parse-error names, ...) is identical, interpreter and jsTarget; html-conformance green. No html
reject case for `_` / `$` is added: they are valid.

Three tests pinned the unguarded fallback and follow it now (CI round 1 on this branch found them;
each old expectation is proven wrong against the contract, uni-ident.ts "beyond ASCII", and Go /
Rust already behaved so):
- test/portable-corpus.ts: the expected-fail entry for exactly this cause (envspec / envtpl,
  `#\n$ x #\n$ x`, Go / Rust disagreeing with the interpreter) no longer disagrees: removed (the gate
  demands it), the list is empty.
- test/lexer-verify.ts: the identifier-extension toy (`[a-z_][a-z0-9_]*`) expected `a$` as `a`, `$`
  (the fallback's identifier); now a lex error at the `$` (`a$`, `a$b`, `é$ a$`); the extension rows
  (`aé$`, `naïve`, `café`) unchanged. docs/TARGETS.md's witness sentence says the same.
- test/agnostic.ts: the raw-text toy lexed `[CODE]` only because the fallback took ASCII upper case
  its lowercase Word refuses; the toy now declares a word with both cases (its checks unchanged).

Can it happen on javascript / typescript: no. A sweep of every ASCII character of the identifier
start class, alone and followed by `a`, through each grammar's interpreter lexer: on javascript,
typescript, javascriptreact, typescriptreact (and minijs, regexjs, templatejs, the dialects) every one
is lexed as the identifier by the token's own pattern, so none reaches the fallback; the guard
changes nothing there (the emitted Go / Rust already had it). Only `$` in the eleven fixtures above
is affected.

Evidence
- Recovery gate, cappedcalc (`node test/portable-recovery.ts --grammars=cappedcalc`, this branch's
  tests, from master 093dfb8): with master's two lexer sources, Go 8 and Rust 6 mismatches: on the
  four total-lex inputs 4 P6 per target (`` `a ${x ``: TS read `$` as an Ident and repaired `$`,
  `{x` as two statements; Go / Rust skipped `${x` as one $error), plus the new `$a;` reject case
  (the TS target accepted it, Go / Rust did not: 1 P6 per target, and the oracle-vs-parseTotal and
  step-count lines on Go); with the guard: recovery ok on TS / Go / Rust, 0 P6.
- Interpreter on cappedcalc `$a;`: before accepted (an Ident `$a`); after "Unexpected character at
  offset 0: '$'"; `é;` and `aé;` unchanged (accepted).
- Emission against master 093dfb8's lexer sources (javascript, typescript, cappedcalc, calc;
  default and recovery): Go and Rust byte-identical; TS changes in one line per grammar,
  `const _um = LX_UNI_IDENT.exec(src);` -> `const _um = src.charCodeAt(pos) >= 0x80 ? LX_UNI_IDENT.exec(src) : null;`
  (+37 bytes; the fallback runs only after every dispatch arm failed).
- CI green on 3534c18 (all jobs; runs 46296307593 CI, 46296307528 profile), after merging #214
  and re-running the recovery (cappedcalc, calc, envspec: 0 P6), parity corpus (the eleven `$`
  fixtures), lexer-verify and agnostic locally. Profile default rows A against M (Rust Callgrind
  instructions, reuse and fresh, 13 inputs each): 26 of 26 rows +0.000% (Rust emission is
  byte-identical). Then merged master d07cb23 (shape-plan / rust-shape only): fd67497.

Owed (unchanged by this landing): an invalid escape INSIDE a template gives no tree (the lexer must
hand out escape offsets); a repair that skips a zero-width template tail says "unexpected end of input".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant