Skip to content

fix(parser): count the lines after a stray \r as bean-check does, and four more BOM and pipe line shapes - #3052

Merged
robcohen merged 1 commit into
mainfrom
fix/parser-recovery-stray-cr
Nov 27, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/parser-recovery-stray-cr

Conversation

@robcohen

@robcohen robcohen commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

What

Item 1 of the parse-error parity long tail #3043 left: stray \r, mid-file BOM lines, and the random-token case that turned out to be a pipe warning.

Stray \r (the main change)

A stray \r (one not part of a CRLF line end) is a blank to beancount's lexer ([ \t\r]+). rledger reports one before a token that lexes, or at a line start, as an error of its own. That is a documented deliberate difference (#2980, test_parse_cr_with_more_of_the_line_after_it_stays_an_error). cst/recovery.rs started no recovery from such a line, so after a statement failing there, the lines bison keeps quiet kept rledger's errors:

Input bean-check main this PR
\r$ / Assets:A 1 USD 1 1 2 1
pushtag #a\r$ / 2024-01-02 2024-01-03 * "n" 1 1 2 1
2024-01-02 open Assets:B \r5 / "a" / Assets:A 1 1 2 3 1
2024-01-02 open\rAssets:B / "a" / Assets:A 2 1 2 3 1 2 (the line 1 error is the deliberate one)
option \r;c / - 1 1 2 1

Model (recovery::model_stray_cr_lines).

  • The input is parsed once more as beancount reads it (recovery::stray_cr_analog):
    • each stray \r becomes a space, of the same length, so every other byte keeps its offset;
    • a line whose leading blanks run into a stray \r has no INDENT in beancount (^[ \t]+/[^ \t\r\n]). Its content moves to column 0 and the blanks go after it. Error spans are mapped back.
    • Lines are left as written where the two readings part: content beancount would skip at column 0 (*, :, a flag, a # that starts no tag), a BOM that would land at byte 0, and a lone capital before a \r ([A-Z]/[ \t\n]).
    • Lines are found with BcLexer, so a \r inside a multi-line string is content.
  • From the first line holding a stray \r on, each line takes that parse's errors, except a line where rledger reads a stray \r as an ERROR_TOKEN of its own. That line keeps its own errors, so the deliberate error stays.
  • A push left open at the end of the file is still counted from rledger's own parse, where a line with a reported \r adds no push.
  • It costs one more parse, and only for an input with a parse error and a stray \r. It is never nested: guarded by PHANTOM_DEPTH and a thread-local.

Four more line shapes (found while closing the above)

  • BOM diagnostic and a plain first token (convert::report_invalid_tokens_of_error_line). A line's P0026 now stands for the line's first invalid token when that token holds no BOM, so a run with a BOM inside it later on the line is one more Invalid token. Example: 2024-01-02 $ o<BOM>6 is bean-check 2, main 1, now 2. A line where rledger reports a stray \r keeps its old count, since that error is one beancount does not have. This retires most of the BOM matrix's inner-bom-under-line-error rows.
  • Misgrouped number beside a BOM token (recovery). Inside a window, 1,50 is reported on a line whose BOMs are each one token, or none, on both sides: a BOM-led invalid token (<BOM>x, which runs to the next blank in both lexers, compat(parser): an invalid run should swallow a following quote, #! or #+ mid-line, as beancount's lexer does #2928) or a BOM inside an ACCOUNT beancount's lexer reads (Assets:<BOM>A). It was left out on any line with a BOM. With the stray-\r model this closes 30 of the 48 inputs fix(parser): an invalid account starts bison's recovery, and three more recovery shapes count as bean-check does #3043 moved away.
  • Pipe after a header failure (item 3's 2000-01-01Z open 1,50 |). beancount warns about | from its grammar, so a header its grammar fails on first gives no Pipe symbol is deprecated. That covers a second flag, a keyword or a tag before the strings, and a lone capital glued to what follows it. Example: 2024-01-02 Z open | is now 1 error, as in bean-check, where it was 2. Too many strings is a builder error raised after the pipe is read, so it keeps the warning.
  • Booking-method line. Invalid booking method is reported on the open's own line (node_content_span, compat(parser): a pushtag, poptag or popmeta with an invalid date changes no state, and a pop error is on the pop's own line #2995's rule), not on a blank or comment line its node starts with after an earlier directive. Example: open / ;c / open Assets:B "FOO" is bean-check line 3, main line 2, now line 3.

Measurements (beancount 3.2.3)

Generated differential, main (635bf818a4c) vs this branch: the #3035/#3036 generators' own 27 input files, 1,260,000 inputs. Per-line counts compared as the parity harness does.

Generator Inputs Gained Closer Regressed Further Sideways
G1 line types 300,000 825 11 6 0 40
G2 random tokens 300,000 3,192 3,958 3 4 3
G3 mutated ledgers 240,000 11 0 0 0 0
G1b weighted to BOM / \r 150,000 7,438 8,594 26 29 58
G2b weighted 150,000 10,949 11,474 13 30 22
G3b weighted 120,000 1,773 1,688 3 0 0
all 1,260,000 24,188 25,725 51 63 123

What moved away, 237 inputs, each classified:

  • 168: the recovery model's gaps without any \r. On every changed line, this branch counts exactly what main counts for the same input with each stray \r read as beancount reads it (the analog). beancount counts that analog as it counts the original. 131 of them are on lines with a BOM. Examples:
    • $ 2024-01-02 * "y" / 2024-02-30 5 / "s" reports "s" (bean-check does not). That is a window that starts inside an entry bison builds mid-line, which the phantom parse ends at the body; it is item 2, in the follow-up PR.
    • 2013-05-18 "" / option "a" "b" / postings / |.
  • 66: the booking-method line. beancount drops that open, because the next line starts with an invalid token or bison is still recovering. rledger keeps it and its error ("entries beancount drops are kept"), now on the open's line instead of a blank line before it.
  • 3: BOM-heavy lines that moved sideways.

Other measurements:

Timing: linear. Seconds at 20k / 40k / 80k / 160k repetitions, release build:

Shape Seconds
\r$ / posting 0.07 / 0.15 / 0.33 / 0.70
open\rAssets:A / "a" / Assets:A 0.12 / 0.23 / 0.47 / 0.98
moved lines + entries after them 0.16 / 0.31 / 0.61 / 1.28
pushtag #a\r$ / second-date entry / posting 0.22 / 0.44 / 0.89 / 1.81
2024-01-02 $ o<BOM>6 x<BOM>y 0.05 / 0.10 / 0.22 / 0.44
  • test_quiet_bison_recovery_scans_linear_bytes gains three stray-\r shapes, which count both parses.
  • The account lookup on BOM lines is a binary search: a linear scan was quadratic on test_tokenize_long_bom_runs_are_linear's 1 MB line. That test caught it, and it passes now.

Robustness: no panics, and green and red agree, on the 1,260,000 generated inputs and the 44,598 fuzz-corpus files plus fuzz/regressions. Every error span stays inside the source.

Caches:

  • WASM CACHE_VERSION 141 → 142: parse errors move.
  • Loader cache unchanged (v79): parsed directives do not move, and every changed source has a parse error, before and after, which the loader's cache is never saved for.

Deliberate deviations

None changed. The stray-\r error stays where rledger reports it, and the booking-method change keeps "entries beancount drops are kept".

How to test

cargo test -p rustledger-parser --test bison_recovery_model --test unreadable_line_recovery \
  --test bom_combination_matrix --test bom_mid_line_run --test open_booking_method_failed_line \
  --test txn_header_conformance
cargo test -p rustledger-parser --lib recovery
cargo build --release -p rustledger-loader --no-default-features --example parse_error_lines
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
printf '\r$\n  Assets:A 1 USD\n' > x.beancount && rledger check x.beancount   # one error, as bean-check

Run locally:

  • cargo clippy --workspace --all-features --all-targets -- -D warnings (exit 0);
  • tests for parser, loader, wasm, lsp and rustledger, all passing;
  • STRICT_BASELINE=1 corpus and CST baselines, no drift;
  • the parity harness --self-test;
  • RUSTDOCFLAGS=-Dwarnings cargo doc -p rustledger-parser.

🤖 Generated with Claude Code

… four more BOM and pipe line shapes

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]>
@robcohen
robcohen requested a review from a team as a code owner November 27, 2026 00:02
@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 54.7ms ± 4.0ms 848.1ms ± 13.9ms 15.5x 43.2 MB / 51.1 MB
investment 79.8ms ± 1.4ms 2938.6ms ± 33.8ms 36.8x 47.1 MB / 59.1 MB
fifo 77.0ms ± 0.8ms 14682.6ms ± 27.4ms 190.6x 49.2 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 52.4ms ± 0.5ms 54.7ms ± 4.0ms +4.4% ✅ within noise 43.2 MB → 43.2 MB (+0.2%)
investment 78.7ms ± 0.9ms 79.8ms ± 1.4ms +1.4% ✅ within noise 47.3 MB → 47.1 MB (-0.4%)
fifo 76.7ms ± 0.7ms 77.0ms ± 0.8ms +0.4% ✅ within noise 49.2 MB → 49.2 MB (-0.1%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.8ms, investment 0.8ms, 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 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.84483% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/recovery.rs 97.53% 5 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%, 179 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 0ebfbdc into main Nov 27, 2026
63 of 64 checks passed
@robcohen
robcohen deleted the fix/parser-recovery-stray-cr branch November 27, 2026 03:00
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