Skip to content

refactor(parser): share cost-spec and meta-value semantics between green and red walkers - #1737

Merged
robcohen merged 3 commits into
mainfrom
refactor/green-red-cost-spec
Jul 10, 2026
Merged

robcohen merged 3 commits into
mainfrom
refactor/green-red-cost-spec

Conversation

@robcohen

@robcohen robcohen commented Jul 9, 2026

Copy link
Copy Markdown
Member

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 SyntaxNode converter and green GreenNodeData fast 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:

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_token accessors are direct-child-only too), so the semantics are a pure function of the (kind, text) token sequence. This adds:

  • TokenView — a pub(super) two-method trait (kind + text), implemented for rowan::SyntaxToken<BeancountLanguage> and &GreenTokenData;
  • cost_spec_from_tokens / meta_value_from_tokens in convert.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_entry and green's convert_cost_spec/meta_value become one-line delegations; cost_compound_numbers, cost_is_merge, and meta_entry_has_minus_sign are deleted. The ast::CostSpec accessors remain (used by format.rs); the raw-token sibling value_tokens_to_meta (adjacency-based pushmeta/custom semantics) is out of scope and unchanged.

Testing

  • Full parser suite green, including parse_green_eq_red_corpus (~40 curated inputs with the three named fuzz regressions) and the fuzz_regressions parity fixtures.
  • One-off differential sweep (scratch test, not committed): parsed all 322 .beancount/.bean fixture files in the repo through parse (green-wired) and parse_red_only, comparing directives/errors/comments/options/account+currency occurrences — zero divergences.
  • Loader suite green; clippy --all-features --all-targets -D warnings, fmt, cargo doc --all-features -D warnings clean.
  • cargo-fuzz isn't available in this environment; the fuzz_green_eq_red target should get CI/scheduled time on this branch if that's cheap to trigger.

🤖 Generated with Claude Code

…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]>
@robcohen
robcohen requested a review from a team as a code owner July 9, 2026 18:27
Copilot AI review requested due to automatic review settings July 9, 2026 18:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TokenView trait and shared helpers cost_spec_from_tokens / meta_value_from_tokens in cst/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.

@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 35.7ms ± 3ms 23.7 MB
beancount 918.8ms ± 12.8ms 50.7 MB
Comparison 25.7x faster 2.1x less

vs main branch (2026-07-10)

Metric main this PR Change
Time 33.7ms 35.7ms +5.9% 🟡
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.29078% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/convert.rs 99.28% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit b84852b into main Jul 10, 2026
50 of 51 checks passed
@robcohen
robcohen deleted the refactor/green-red-cost-spec branch July 10, 2026 09:46
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.

2 participants