Repository navigation
refactor(parser): share cost-spec and meta-value semantics between green and red walkers - #1737
Merged
Merged
Conversation
…een and red walkers The green (GreenNodeData) and red (SyntaxNode) CST converters each carried a hand-written copy of the two most drift-prone token semantics, and every historical green/red fuzz divergence (#1704, #1713, the {*} merge re-arm) landed in one of them: - cost specs: red spread the rules over four sources (ast accessors is_total/is_merge/number/currency/date/label plus cost_compound_numbers), green re-fused them into one loop with explanatory comments about how to mirror red; - meta values: red's meta_value_from_entry priority ladder plus meta_entry_has_minus_sign, green's meta_value re-collected the same first-of-kind latches. Both walkers consume exactly the node's DIRECT child tokens (red's first_token accessors are direct-child too), so the semantics are a pure function of the (kind, text) token sequence. Introduce a pub(super) TokenView trait (implemented by rowan::SyntaxToken and &GreenTokenData) and move the two fused loops into shared functions in convert.rs — cost_spec_from_tokens and meta_value_from_tokens — that both walkers now call. The hard-won semantics notes (dual number semantics with compound retry vs plain latch, fused L_BRACE_HASH opener, positional merge flag, minus-before-number detection, priority ladder with parse-failure fallthrough) are consolidated on the shared functions. The red walker's cost_compound_numbers and meta_entry_has_minus_sign are deleted (their logic lives in the shared loop); the ast accessors remain for other consumers (format.rs). The raw-token sibling value_tokens_to_meta (adjacency-based pushmeta/custom semantics) is unchanged. Verified behavior-identical: full parser suite including the parse_green_eq_red_corpus regressions and fuzz_regressions fixtures, plus a one-off differential sweep parsing all 322 repo .beancount fixtures through both paths and comparing directives/errors/comments/ options/occurrences — zero divergences. Loader suite green. Co-Authored-By: Claude Fable 5 <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the parser CST-to-AST conversion to eliminate green/red walker drift by centralizing two historically divergence-prone semantics (cost-spec parsing and meta-value typing) into shared token-level helpers used by both walkers.
Changes:
- Introduces a small
TokenViewtrait and shared helperscost_spec_from_tokens/meta_value_from_tokensincst/convert.rs. - Updates the green walker (
cst/green.rs) to delegate cost-spec and meta-value conversion to the shared helpers, removing duplicated logic and deleted helper functions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| crates/rustledger-parser/src/cst/green.rs | Replaces local cost-spec + meta-value logic with delegations to shared token-level helpers. |
| crates/rustledger-parser/src/cst/convert.rs | Adds TokenView plus shared cost_spec_from_tokens/meta_value_from_tokens helpers and wires red conversion to use them. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Contributor
📊 Benchmark Results
vs main branch (2026-07-10)
What do these numbers mean?
|
Co-Authored-By: Claude Fable 5 <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
What
Extracts the two most drift-prone token semantics — cost-spec conversion and meta-value typing — into shared token-level functions that BOTH CST walkers (red
SyntaxNodeconverter and greenGreenNodeDatafast path) now call. Kills the hand-mirroring that produced every historical green/red fuzz divergence.Why
Phase-1 correctness sweep, green/red family. The green walker existed as a hand-written mirror of the red converter, and the mirror had to reproduce some genuinely subtle semantics:
{N CCY},{{T}},{N # T},{*}): red spreads the rules across four sources (theast::CostSpecaccessors pluscost_compound_numbers); green re-fused them into one loop. All three named fuzz divergences (fix(parser,booking)!: compound cost {a # b} carries both components (N*a + b) #1704, parser: green/red divergence found by fuzz_green_eq_red (pre-existing; reproduces on main) #1713, the{*}merge re-arm) landed here — dual number semantics (compound side retries past unparsable NUMBER tokens, plain side latches the first token parsed-or-not), the fusedL_BRACE_HASHopener, and the positional merge flag.meta_value_from_entry+meta_entry_has_minus_sign), green'smeta_valuere-collecting the same first-of-kind latches and minus detection.Every future change to either had to be manually replayed on the other; the fuzzer kept proving that doesn't happen reliably.
How
Both walkers consume exactly the node's direct child tokens (red's
first_tokenaccessors are direct-child-only too), so the semantics are a pure function of the(kind, text)token sequence. This adds:TokenView— apub(super)two-method trait (kind + text), implemented forrowan::SyntaxToken<BeancountLanguage>and&GreenTokenData;cost_spec_from_tokens/meta_value_from_tokensinconvert.rs(the established home for green/red-shared helpers) — the fused loops, with the hard-won semantics notes from both sides consolidated onto them. The merge-flag machine runs alongside the value machine in the same pass, preserving red's exact positional rule (openers and whitespace never decide; the first other token after an opener does).Red's
convert_cost_spec/meta_value_from_entryand green'sconvert_cost_spec/meta_valuebecome one-line delegations;cost_compound_numbers,cost_is_merge, andmeta_entry_has_minus_signare deleted. Theast::CostSpecaccessors remain (used byformat.rs); the raw-token siblingvalue_tokens_to_meta(adjacency-basedpushmeta/customsemantics) is out of scope and unchanged.Testing
parse_green_eq_red_corpus(~40 curated inputs with the three named fuzz regressions) and thefuzz_regressionsparity fixtures..beancount/.beanfixture files in the repo throughparse(green-wired) andparse_red_only, comparing directives/errors/comments/options/account+currency occurrences — zero divergences.--all-features --all-targets -D warnings, fmt,cargo doc --all-features -D warningsclean.cargo-fuzzisn't available in this environment; thefuzz_green_eq_redtarget should get CI/scheduled time on this branch if that's cheap to trigger.🤖 Generated with Claude Code