Repository navigation
Add tree shapes for code generation to the conformance suite - #9
Merged
Merged
Conversation
The code generators type each tree by its shape: what can stand at each position among its children. conformance/tools/shapes.py defines the shapes on Lark's compiled BNF rules, following what the tree builder does with them (filtered tokens, inlined _rules and ?rules, aliases, templates, imports, placeholders, and the helper rules of *, + and ~). generate.py adds the shapes to every case and fails when a tree Lark builds doesn't match them; all 1,400 trees in the suite do. New hand-written cases in sources/codegen.json cover the purchase order grammar, inlining, placeholders, repetitions, kept tokens and templates. Rust (etalii_lark::codegen::Shapes) and TypeScript (shapesOf) compute the same shapes from their own compiled grammars, and their conformance runs compare them. The .NET source generator is compared too: it is coarser on the cases in known-failures/dotnet-shapes.txt, and a check makes sure its shapes still fit every tree. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FHXTnmdnhE64asNUmffcLr
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by Peter · project thread
The first of the three codegen PRs. It lays the shared foundation: the shape of each tree, meaning what can stand at each position among its children. The Rust and TypeScript code generators will type nodes and visitor parameters from it.
What's in it
conformance/tools/shapes.pyis the reference analysis. It works on the BNF rules Lark compiles a grammar into, and follows what Lark's tree builder does with them: filtered tokens, inlined_rulesand?rules, aliases, templates, imports,maybe_placeholdersplaceholders, and the helper rules that*,+and~become.generate.pyadds ashapesfield to every case. It fails when a tree Lark builds doesn't match the shapes, and all 1,400 trees in the suite match.sources/codegen.jsonhas new hand-written cases: the purchase order grammar, inlining, placeholders, repetitions, kept tokens, templates and multiple start rules.etalii_lark::codegen::Shapes::of(&lark). TypeScript:shapesOf(lark). Both compute the shapes from their own compiled grammars, and their conformance runs compare them on all 883 cases. Both match on every case.ShapeConformanceTestscompares the source generator's shapes with the spec. The generator reads the grammar text, so it is coarser on 414 cases, which are listed inknown-failures/dotnet-shapes.txt. The causes are unnamed anonymous terminals, templates, imports and recursive rules. A second check makes sure its shapes still fit every tree Lark builds, and they do. So the generator never mistypes a child; it only types fewer of them. The .NET generator itself is unchanged.Testing
python conformance/tools/generate.py: the existing cases are unchanged apart from the newshapesfield.cargo test --release,cargo clippy -D warnings,cargo fmt --checknpm run typecheck,npm testdotnet test(net10.0): 2925 tests pass.Generated by Claude Code