Skip to content

fix(parser): start bison's recovery at a BOM beancount reads as an invalid token, and model four more recovery shapes - #3036

Merged
robcohen merged 1 commit into
mainfrom
fix/recovery-bom-cr
Nov 26, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/recovery-bom-cr

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

#3035's differential left 8 inputs that moved away from bean-check 3.2.3: 2 further and 6 sideways, all on lines with a BOM or a \r. This PR finds their causes and fixes them. It also documents one deliberate deviation the maintainer asked for: rledger accepts an indented comment after a column-0 comment.

Cause

Not the lexer port. I checked BcLexer token by token against beancount's own lexer (lexer.lex_iter_string). About 190,000 generated BOM- and \r-heavy inputs gave 0 differences.

The 8 cases come from four gaps in the recovery driver:

  1. A BOM never started bison's recovery.
    • starts_recovery excluded every BomInDirectiveBody (P0026).
    • So after \t<BOM> or <BOM>g, the lines bison discards kept their errors.
    • Example: \t<BOM> / \tT 2024-01-02 commodity EUR "a b" gave 2 errors on line 2. \t$ gives 1.
    • Fix: a BOM diagnostic now starts recovery when beancount's lexer reads that BOM into an invalid token. It does not start recovery when the BOM is read into an account, a string or a comment, or when the line has a stray \r.
    • A forward-only probe lexer (bom_is_invalid_token) decides this.
  2. An entry bison builds mid-line, with an invalid token on its own line, kept the errors of its indented lines.
    • Examples: $ 2024-01-02 * "y" $ or <BOM>g 2024-01-02 * "n" $x, each followed by postings.
    • In beancount, that token's YYerror drops the entry and starts the recovery over.
    • Fix: the entry's line now gets the errors it has when parsed on its own (compat(parser): a dated line that opens no directive reports each indented line under it, unlike beancount #2979's mechanism, factored into phantom_errors). The window then continues from the end of the line, so the postings are quiet.
    • With that in place, a BOM inside such an entry no longer forces it to be left unmodeled.
  3. A misgrouped number (1,50) inside a window had no error.
  4. A header failing past a multi-line string was reported on the header's first line.
    • Example: 2024/1/3 event "x\nb" x is line 2 in beancount, line 1 in rledger.
    • The recovery pass already counted it on line 2 (grammar_failures). report_at_multi_line_string_ends only moved it when the string itself was the failing token. It now also covers a failure past the string.

Also:

Deliberate deviation: indented comment after a column-0 comment

The input ; note followed by ; continued (also after a blank line, a * heading, or at the top of the file):

  • beancount 3.2.3 reports syntax error, unexpected INDENT;
  • rledger reads it as a comment, on purpose.

Per the Python Compatibility Policy:

  • an inline comment at the code site (cst/parser.rs, the top-level trivia loop);
  • a regression test pinning acceptance, CRLF included: test_parse_indented_comment_after_a_column_0_comment_is_accepted;
  • the parity harness drops beancount's error there (is_indented_comment_indent_error), documented with its other normalizations and covered by the self-test.

Scope of the harness rule: it is context-aware. After a directive that takes no metadata (option, include, pushtag, ...), both sides report the line, so it stays an error.

The BOM matrix generator deliberately does not apply this drop. Applying it would mean regenerating the whole table, including its main columns. Its docstring says so.

Results (bean-check 3.2.3 from nixpkgs)

The 8 #3035 cases:

  • 3 match fully.
  • 4 match on every line except the failing line of an orphan indented line. There beancount reports its INDENT error plus the lexer's, and rledger reports one. That is the general shape ( $x alone is 2 vs 1).
  • 1 is unchanged: a tag line after a transaction's first posting (A #tag after a transaction's first posting is kept by rledger, not by beancount #2575). rledger reports it on the tag's line, beancount on the transaction's line; that happens without any recovery too.

Parse-error parity corpus (pinned fuzz 9bc79f4f, matrix, tracked, compat):

Matching
main 106,447
main + comment normalization only 106,454
this PR 107,279

0 regressions. The manifest is rewritten with --update.

BOM combination matrix: 11,854 → 12,762 rows match.

Generated differential, branch vs main. The first three rows reuse #3035's own input files.

Generator Inputs Gained Closer Regressed Further Sideways
G1 line-type sequences 300,000 0 0 0 0 0
G2 random beancount tokens 300,000 4,120 1,588 3 0 0
G3 mutated real ledgers 240,000 348 36 0 0 0
G1b, weighted to BOM and \r 150,000 5,237 1,807 6 2 0
G2b, weighted 150,000 9,494 7,554 7 9 4
G3b, weighted 120,000 1,288 352 0 0 0

What still moves away (31 inputs of 1.26M):

Timing. Every new path is linear. The scanned-bytes unit test gains 5 shapes. Wall clock, 20k vs 80k repetitions:

  • the new shapes: ratios 2.2 to 4.5;
  • a single line of 80k BOM-led entries: 0.25 s.

How to test

cargo test -p rustledger-parser --test bison_recovery_model --test bom_combination_matrix
cargo test -p rustledger-parser --lib recovery
python3 -I scripts/compat-parse-error-parity.py --self-test
python3 -I scripts/compat-parse-error-parity.py --rledger target/release/examples/parse_error_lines \
  --fuzz-dir DIR --fuzz-commit 9bc79f4f35f52c50813164f43307e7d151d70d8c \
  --compat-dir tests/compatibility/files --check

Run locally:

  • clippy -D warnings, whole workspace;
  • tests for parser, loader, wasm, lsp and rustledger (125 test binaries);
  • STRICT_BASELINE=1 corpus and CST baselines (unchanged).

WASM CACHE_VERSION 132 → 133, since parse errors move.

🤖 Generated with Claude Code

…valid token, and model four more recovery shapes

#3035's differential left eight inputs that moved away from bean-check
3.2.3, each holding a BOM or a `\r`. The port of beancount's lexer in
`cst/recovery.rs` was not the cause: it agrees with `lexer.l` token for
token on about 190,000 BOM- and `\r`-heavy inputs. The causes, and what
fixing them led to:

- A mid-file BOM's diagnostic never started bison's recovery. It now does
  where beancount's lexer reads that BOM into an invalid token (not an
  account, a string or a comment, nor on a stray-`\r` line), checked with a
  forward-only probe lexer.
- An entry bison builds mid-line whose own line holds an invalid token is
  dropped at that token's YYerror: its line counts as that line parsed on
  its own, and the window goes on from the line's end, so its indented lines
  are quiet. A BOM inside such an entry no longer leaves it unmodeled.
- A misgrouped number (`1,50`) inside the window is reported as
  `InvalidNumber`, as beancount's lexer reports it, where rledger had no
  error of its own; a lone BOM token no longer blocks that addition.
- A header failing at a token after a multi-line string is reported on that
  token's line (`report_at_multi_line_string_ends`).

Also documents and pins rledger's deliberate acceptance of an indented
comment after a column-0 comment, a blank line or the top of the file, and
the parse-error parity harness drops beancount's `unexpected INDENT` there.

Parity corpus: 106,447 -> 107,279 matching, 0 regressions. BOM matrix:
11,854 -> 12,762. WASM cache 133.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 26, 2026 03:39
@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 41.7ms ± 1.8ms 479.1ms ± 4.0ms 11.5x 43.1 MB / 51.0 MB
investment 59.6ms ± 1.0ms 2009.5ms ± 110.0ms 33.7x 47.2 MB / 59.0 MB
fifo 58.9ms ± 0.7ms 11176.8ms ± 89.9ms 189.8x 49.3 MB / 59.1 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 40.1ms ± 0.6ms 41.7ms ± 1.8ms +4.1% ✅ within noise 43.5 MB → 43.1 MB (-0.8%)
investment 59.6ms ± 0.6ms 59.6ms ± 1.0ms +0.0% ✅ within noise 47.1 MB → 47.2 MB (+0.1%)
fifo 58.4ms ± 0.9ms 58.9ms ± 0.7ms +0.9% ✅ within noise 49.1 MB → 49.3 MB (+0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.8ms, investment 0.5ms, fifo 0.5ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.93878% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/recovery.rs 96.82% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 180 masked)
Full AST Match 100%
Total Files 828
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen merged commit 2058395 into main Nov 26, 2026
62 of 63 checks passed
@robcohen
robcohen deleted the fix/recovery-bom-cr branch November 26, 2026 03:54
robcohen added a commit that referenced this pull request Nov 27, 2026
… four more BOM and pipe line shapes (#3052)

A stray `\r` is a blank to beancount's lexer; rledger reports one before a
token that lexes as an error of its own (deliberate, #2980) and started no
model of bison's recovery from such a line, so the lines bison keeps quiet
after a statement failing there kept their errors. The input is now parsed
once more as beancount reads it (`recovery::stray_cr_analog`), and from the
first line holding a stray `\r` on, each line takes that parse's errors,
except a line where rledger reports the `\r` itself, which keeps its own.

Also: a line's BOM diagnostic stands for a first invalid token with no BOM;
a misgrouped number beside a BOM-led token or a BOM inside an account is
reported in the recovery; a pipe after a header failure gives no warning;
an invalid booking method is reported on its `open`'s line.

Generated differential (1.26M inputs, #3035/#3036 generators): 24,188 gained,
25,725 closer, 237 moved away (168 reproduce on the \r-free analog, 66 are the
booking line where beancount drops the open). Parity corpus 107,647 ->
108,454, no regressions; BOM matrix 12,853 -> 12,917. WASM cache 142.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant