Repository navigation
refactor(parser): walk the template tree with a work list, not per-level recursion - #520
Merged
Merged
Conversation
…er passes The parser passes that visit every nested body (finding ignored content, checking fill groups, recomputing used variables, collecting and checking template bindings, and extracting fills) now keep their own list of what is left to visit, so their native stack use no longer grows with the template's depth. The results are unchanged. A test runs every pass over a tree 3,000 levels deep on a 64 KiB thread. Part of #300. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…elf per level `Template`'s `Clone`, `PartialEq` and `Drop` keep their own stack of the bodies still to visit instead of running through the stack guard, so they work on a small thread at any depth. `Debug` stays on the guard, because printing the derived format without calling itself would mean writing that format by hand. A test clones, compares and drops a tree 3,000 levels deep on a 64 KiB thread, and checks that a change at the innermost level is seen. Part of #300. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The stack guard module, the crate's AGENTS.md and the deep nesting test now describe what is left on the guard: the compiler, the parse of a template written in an attribute value, and `Template`'s `Debug`. The grammar keeps its stack reservation because Pest's generated parser enters each rule by calling a function, and the attribute-template parse keeps its guard because it is a whole parse called from inside another. Part of #300. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bring the remaining comments that described the old per-level recursion in line with the code (the deep nesting tests, `check_nesting_depth`, the compile entry in the Python bindings, and the expression red zone). Let `Template`'s clone and comparison skip the work list for a body without nodes, which is the common case PyO3 copies on every `body` read, and name every field in the comparison so a new one cannot be missed. Part of #300. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Template.__eq__` now returns `self == other`, so Python's `==` uses the
same comparison as Rust, field list included. Also correct a few comments:
the grammar's brace rules call themselves per `{` (#300 tracks whether
they can loop), the order the binding passes visit, and the depth of the
test tree against the control-flow limit.
Part of #300.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 28, 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.
Part of #300. Two kinds of walks over a parsed template now keep their own list of what is left to visit, so their stack use does not grow with nesting depth: the parser's passes over the finished tree, and copying, comparing and freeing a
Template. Until now, each nesting level called the same function again and went through the stack guard (crate::stack::with_enough_stack).The results stay the same, and so does the order. A pass that visited parents before children still does. A pass that needs its children's results first still visits children first.
What changed
parser.rs):find_ignored_content_problem,_contains_only_fills_and_control_flow,validate_fill_group_content,recompute_template_used_variables,collect_template_binding_tokens,validate_template_bindingsandextract_fill_nodes. The two near-identical branches ofrecompute_node_used_variablesare merged into one.Template(ast.rs):Clone,PartialEqandDropkeep their own stack.body, skips the list.==now goes through the samePartialEq.Debugkeeps the guard, because printing the derived format without calling itself would mean writing that format by hand.python_exprandpython_nested_curlycall themselves once per{. Fix direction 3 of Follow-up to #272: walk the template tree with an explicit stack in the Rust parser and compiler, for the planned JS binding #300 is about whether they can loop.stack.rsmodule doc, the crate'sAGENTS.mdand the docs of both deep-nesting tests now say this.Out of scope:
Tests
A new test module,
walks_without_recursion(inparser.rs), builds a tree of 3,000 nested<c-if>by hand. The parser itself stops control flow at 256. On a 64 KiB thread, the module then:Putting back a recursive drop or a recursive comparison makes the thread overflow.
cargo test -p citry_template_parser(all 34 test binaries),cargo clippy --all-targets --no-deps -D warnings,cargo fmt --check.Python, on a release build of this branch: the
citry_coretests pass (543), and so does thecitrysuite (10,360). The one failure is a test that also fails onmain; test: name the component for a link in the div that closes its paragraph #518 fixes it.No changelog entry: nothing a user of the package can observe changes.
🤖 Generated with Claude Code