Repository navigation
Add typed code generation for Rust: the etalii-lark-derive crate - #10
Merged
Merged
Conversation
#[lark_grammar(file = "calc.lark", parser = "lalr")] on a module fills it
at compile time with:
- the parser: parse, parse_{start}, lex, on a Lark built on first use;
- name constants: rules::ADD, terminals::NUMBER;
- a Copy node wrapper per rule, alias and template (AddNode<'a>), with an
accessor per child: node wrappers, tokens, generated enums for children
that can be several things, Option for [x] and x?, iterators for x*;
- with #[lark_result(T, names...)] mappings, a typed Visitor trait whose
methods get their children already visited and converted to the mapped
types. It runs without recursion and stops on Self::Error.
The types come from the tree shapes of the conformance suite. The engine
is etalii_lark::codegen::Generator, which build scripts can use directly;
the macro is a thin wrapper that reports grammar and mapping errors at the
attribute.
Tests evaluate the calculator and JSON grammars with typed visitors, read
purchase orders through the node wrappers, and compile the generated code
of all 206 distinct grammars of the conformance suite (codegen-tests,
unpublished). The purchase order example of the .NET docs is ported in
examples/purchase_orders.rs. The release workflow publishes both crates.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FHXTnmdnhE64asNUmffcLr
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 second of the three codegen PRs. It follows the design doc: a pest-style attribute macro in a companion crate, built on the tree shapes from #9.
What the module gets
parse,parse_{start}andlex, on aLarkbuilt on first use. The grammar file is embedded withinclude_str!, so cargo rebuilds when it changes.rules::ADD,terminals::NUMBER.AddNode<'a>for each rule, alias and template. Each one isCopyand derefs toTree, with an accessor per child:&Tokenfor a child of one kind;?rule(Shipping::ExpressShipping);Optionfor[x]andx?;x*andx+.#[lark_result]): one required method per mapped tree, plusvisit_x_tokenper mapped terminal. Each method gets its children already visited and converted. Unmapped children come as their node, so only mapped trees are visited. It doesn't recurse.visit_x_treevisits a tree, andvisit_{start}is added when all of a start rule's trees share a result type.?rulesets the type of the children it stands for, and every tree it can be must then have that type (like LARK008). Children whose trees have different result types get a...Valueenum, so nothing falls back to an untyped object.The engine is
etalii_lark::codegen::Generator, which abuild.rscan use to write the same code to a file. The macro is a thin wrapper around it.One deviation from the doc: I left out the separate untyped visitor trait. The node wrappers work with the crate's existing
Visitor, and two traits calledVisitorin one module would clash.Tests
etalii-lark-derive/tests:?valuemapped as a whole;codegen-tests(unpublished): generates and compiles the code for all 206 distinct grammars in the conformance suite, in two mappings. One maps everything tou64; the other maps the terminals toString, which exercises the value enums. This caught a keyword bug (as) and a name-clash bug: result types now resolve through aliases in the declaring scope, so a user type named like a generated enum still works.examples/purchase_orders.rsports the .NET docs example to Rust, and CI runs it.Release
release-rust.ymlsets it for both and runscargo publish -p etalii-lark -p etalii-lark-derive.etalii-lark-deriveneeds the token to have the publish-new scope. After that, trusted publishing can be set up for it too.Generated by Claude Code