Skip to content

Grammars declare their context parameters; JavaScript for-heads follow the specification's [In] rule - #212

Merged
johnsoncodehk merged 11 commits into
masterfrom
land/contexts-3
Nov 28, 2026
Merged

johnsoncodehk merged 11 commits into
masterfrom
land/contexts-3

Conversation

@johnsoncodehk

Copy link
Copy Markdown
Owner

What changes

For grammar authors: context parameters are now declared by the grammar and set where the specification sets them. A grammar lists its parameters with their values at the top level (params: { yield: false, await: false, in: true }). ctx({ in: false }, …) sets or clears some of them for what it holds, and when('+in', …) makes a rule item, an operator or a reserved word depend on them. This replaces exclude, reservableNot and the four markers awaitCtx, yieldCtx, asyncGenCtx, resetCtx, which are removed. A grammar that used them must be rewritten (see docs/GRAMMAR.md, "Contexts").

For users of the JavaScript parser: a for head now follows the specification's [In] rule exactly, in every generated parser.

Code (JavaScript) Before Now
for (x => x in y;;); parsed syntax error (an arrow body keeps the head's "no in")
for (a ? b : c in d;;); parsed syntax error
for (a = b in c); parsed syntax error (the left side must be an assignable expression)
for (a + b of c); parsed syntax error
for (`${a in b}`;;); parsed parsed (brackets and templates allow in again)

TypeScript keeps what TypeScript's own parser accepts: it reads any expression before in in a for head.

Results

  • test262: 21,777 of 24,677 tests agree with the specification (was 21,773). No test moved the other way.
  • The TypeScript and Python verdict records, and the tree-sitter, TextMate and Monaco grammars, are unchanged.

Cost

  • Generated JavaScript parsers grow by about 0.5% to 0.7% (TypeScript, Go, JS targets) and 0.2% (Rust). Each place where brackets allow in again saves and restores one small value.
  • The TypeScript grammar's parsers move by -0.15% to +0.44%. The generated text-dialect parsers shrink by about 0.3%.
  • Speed (instruction counts, same work counted on both sides): the Rust parser takes 0.5% to 0.7% more instructions on real TypeScript and JavaScript files (checker.ts +0.69%), and 2.7% more on a synthetic stress file. The Go parser takes 0.3% to 0.4% fewer. The TypeScript-target parser is +0.1%.

Known limits

  • for (!a in c) and for (a++ in c) still parse in JavaScript. Their left side is a unary or update expression, which the specification does not allow there.
  • TypeScript's own contexts (conditional types) and the script / module goals are later steps.

🤖 Generated with Claude Code

… printers and recovery on ctx equations and gated leds; suppress deleted
…er the template token's holeCtx; Python's primary resets in
…ft is a LeftHandSideExpression; mixfix re-bind sees a ctx operand; tree-sitter reads the context-free view
…in left verdicts; jsTarget reads a Pratt rule at a level from another rule; a field on a group names its reference
… [~In]; TS lists as on master; the arrow-body greedy captures hold; tree-sitter record moves
@johnsoncodehk
johnsoncodehk merged commit d849781 into master Nov 28, 2026
12 checks passed
@johnsoncodehk
johnsoncodehk deleted the land/contexts-3 branch November 28, 2026 19:41
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