Skip to content

fix(parser): report a line beancount cannot read as often as beancount does - #2885

Merged
robcohen merged 3 commits into
mainfrom
fix/small-leniencies
Nov 7, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/small-leniencies

Conversation

@robcohen

@robcohen robcohen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

What changed

#2787 listed four small leniencies against beancount 3.2.3. Each was checked against bean-check 3.2.3 before coding:

Row Status
A lone * line at column 0 Already an error, as in beancount, since #2877 (#2779). Pinned here.
A trailing * after a directive Already an error after every directive kind (#2802). Pinned here.
An indented * h line after a transaction's postings gives overlapping errors Fixed here.
A one-character metadata key ( k: 1) Already an error (#1955). Pinned here.

The overlapping errors

The issue suggested "one error" per line. That turns out not to be beancount's rule:

  • Beancount reports an Invalid token error for each token its lexer cannot read.
  • It reports a syntax error only when its grammar fails at a readable token before that. Once the lexer has reported an error, bison's recovery reports nothing more for the statement.

So bean-check counts per line come out like this:

  • * h is 1 error: the * is a posting flag, then h is unreadable.
  • * h x is 2: h and x.
  • ** h is 2: the grammar fails at the second *, then h.
  • @ x is 2.
  • Assets:A****7f855c 1 USD is 2.

rledger reported the line error plus an inline error for each ERROR_TOKEN on the line. The first unreadable token was therefore counted twice: once by the line error and once by itself.

The new drop_errors_inside_a_line_error runs on the final, sorted error list. Inside a syntax error, it drops the first generic unexpected input error on that error's first line, but only when the tokens before it are a readable start of an indented line:

  • nothing;
  • a posting flag;
  • an optional flag, an account, and a prefix of a plain amount ([-+]? NUMBER CURRENCY).

In every other case both errors are kept, as on main. Other details:

  • The outer error is the one kept, because the formatter reads its verbatim-copy ranges (ErrorRanges) from the outer errors.
  • Errors of any other kind are never dropped.
  • A generic error on a later line of a multi-line error is never dropped.

Why

Issue #2787: match beancount. No deviation is introduced.

How to test

cargo test -p rustledger-parser --all-features --test line_error_count
cargo test -p rustledger-parser --all-features --lib test_drop_errors_inside_a_line_error_is_linear_in_the_input

Verification

  • Probes against bean-check 3.2.3: 15 broken indented-line shapes in a transaction and under an open, plus the issue's own shapes and 5 posting shapes. The parse-error lines and counts now match on 11 more probes than on main, and none matches on main that does not match here.
  • Corpus, current origin/main vs branch: parse errors on all 1,113 in-tree and compat ledgers. Three files differ, all in parser-lima, and all now match bean-check's per-line counts exactly:
    • examples_data_error-parse.beancount: lines 16, 17 and 22.
    • examples_data_error-includes.beancount: line 30.
    • examples_data_error-includes_simple-errors.beancount: lines 2 and 6.
  • Baselines: only those three rows in tests/baselines/parser-corpus.manifest are updated. Their source hashes are unchanged; the errors are part of the fingerprinted output. Their CST rows do not move. STRICT_BASELINE=1 corpus and CST baselines pass.
  • Formatting paths: format_source and whole-file format_parsed_range (the LSP range path) give byte-identical output on main and the branch for all 1,112 readable ledgers.
    • The change only removes errors that lie inside another error, so files with fewer than two syntax errors (every valid file) are untouched.
    • rledger format and the LSP whole-document path only format files with no errors. LSP surface cleanup and WASM/FFI do not read the error list, or fall back to format_source.
  • Linear time: test_drop_errors_inside_a_line_error_is_linear_in_the_input counts the bytes the pass scans: the source once for line starts, plus each lexed line prefix. It tests 20,000 broken lines, and 20,000 unreadable tokens on one line.
    • It can fail: with the "first inner error per outer error" gate removed, it counted 400,100,060 bytes on a 40,060-byte input and failed after 253 s.
  • Checks run:
    • cargo test --workspace --all-features --no-fail-fast
    • cargo clippy --all-features --all-targets -- -D warnings
    • RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps
    • cargo fmt --check
  • Parse caches: loader CACHE_VERSION 59 (with FIXTURE_VERSION), WASM 76 (with LOADER_CACHE_VERSION_PIN 59). WASM v75 is refactor: one rule for when a query answers despite load errors #2881's.

#2872 rows

None of the #2872 rows is changed by this PR. The "unterminated string followed by ;x" row still shows its duplicated line-2 error. There, the line error and the inline error sit on a directive header (2024-01-02 note Assets:A "x), and this PR's rule only covers indented lines. Extending it to headers through the header grammar's failure point is a natural follow-up for that row.

Review round 1

  1. The bad token is named again. The line error that stands for a dropped inner error now names the token: unexpected input: invalid token 'h', like bean-check's Invalid token: 'h'. The token text is escaped with char::escape_debug and capped at 40 characters, with ... after a longer one. The outer range is unchanged, so the formatter's verbatim-copy ranges are unchanged too.
    • Only a generic outer error is renamed. An outer unexpected trailing tokens after posting keeps its own message.
    • No test pinned the old text.
    • One parser-corpus baseline row moved, parser-lima/examples_data_error-parse.beancount, and it is updated. Its source hash is unchanged and its CST row does not move.
    • New test: test_parse_line_error_names_the_invalid_token covers the plain, capped and control-character cases.
  2. The dates linear-test doc comment is back on test_report_invalid_dates_is_linear_in_the_input. The two long lines are wrapped (the is_generic closure is now a match, so rustfmt keeps it wrapped).
  3. Follow-ups for Parse-error recovery differences from bean-check around invalid dates and column-0 markers #2872 (not fixed here):
    • #tag h gives 2 errors where bean-check gives 1.
    • ^link h gives 2 errors where bean-check gives 1.
    • Assets:a 1 USD gives 2 errors where bean-check gives 1.
    • In the open context (a broken metadata line under a non-transaction directive), rledger reports one error where bean-check reports the grammar error plus the invalid token.

Verification for this round: parser and LSP tests, STRICT_BASELINE=1 corpus and CST baselines, clippy -D warnings, rustdoc -Dwarnings and cargo fmt --check all pass.

Closes #2787

🤖 Generated with Claude Code

robcohen and others added 2 commits November 6, 2026 20:37
…t does

Beancount reports an `Invalid token` error for each token its lexer cannot
read, and a syntax error only when its grammar fails at a readable token
first (once the lexer has reported, bison's recovery reports nothing more
for the statement). rledger reported the line error and, on top of it, an
inline error for the line's unreadable token, so `  * h` after a
transaction's postings, `  k: 1` in a transaction, `Assets:A  -39.65 oops
NZD` and `  ofxid*: "x"` each gave two overlapping errors where bean-check
gives one.

`drop_errors_inside_a_line_error` runs on the sorted errors: inside a
syntax error, the first generic error on its first line is dropped when
the tokens before it are a readable start of an indented line (nothing, a
posting flag, an account and a prefix of a plain amount). Later unreadable
tokens keep their errors (`  * h x` is two, as there), and so does a line
whose grammar fails first (`  ** h`, `  @ x`, `Assets:A****7f855c`). The
outer error is the one kept, since the formatter reads its verbatim-copy
ranges from it. A counter test pins it linear.

The other #2787 rows (lone `*` line, trailing `*`, one-character key)
were already errors as in beancount; they are pinned by tests.

- Parser-corpus baseline rows for the three parser-lima error files whose
  error lists moved (all three now match bean-check's counts); the CST
  rows do not move.
- Loader CACHE_VERSION 59, WASM 76 (v75 is #2881).

Closes #2787

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	CHANGELOG.md
#	crates/rustledger-wasm/src/cache.rs
@robcohen
robcohen requested a review from a team as a code owner November 7, 2026 02:49
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 31.3ms ± 0.9ms 406.5ms ± 11.6ms 13.0x 42.6 MB / 51.0 MB
investment 47.1ms ± 0.6ms 1678.1ms ± 27.9ms 35.6x 58.8 MB / 59.0 MB
fifo 51.9ms ± 2.3ms 8629.2ms ± 566.5ms 166.2x 54.9 MB / 58.7 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 31.1ms ± 0.8ms 31.3ms ± 0.9ms +0.8% ✅ within noise 42.8 MB → 42.6 MB (-0.4%)
investment 47.5ms ± 0.8ms 47.1ms ± 0.6ms -0.9% ✅ within noise 58.9 MB → 58.8 MB (-0.1%)
fifo 50.6ms ± 1.1ms 51.9ms ± 2.3ms +2.6% ✅ within noise 54.2 MB → 54.9 MB (+1.3%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.6ms, investment 0.4ms, fifo 1.1ms), 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 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.44961% with 2 lines in your changes missing coverage. Please review.

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

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 7, 2026

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 826
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

Review round 1 on #2787: with the inner error dropped, the editor showed
only a whole-line "unexpected input" for `  * h`, where bean-check says
`Invalid token: 'h'`. A generic line error that stands for a dropped
inner error now reads `unexpected input: invalid token 'h'`, the token
escaped and capped at 40 characters. The outer range is unchanged, so the
formatter's verbatim-copy ranges are too. The one parser-corpus row whose
error text moved (parser-lima examples_data_error-parse) is updated.

Also: the dates linear-test doc comment is back on its test, and two long
lines are wrapped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 036554b into main Nov 7, 2026
67 of 69 checks passed
@robcohen
robcohen deleted the fix/small-leniencies branch November 7, 2026 04:05
robcohen added a commit to MoTechnicalities/rustledger that referenced this pull request Nov 7, 2026
Brings in rustledger#2885-rustledger#2889. The only conflict was CHANGELOG.md, in Breaking
and in Fixed, where both sides added entries; both are kept.

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.

compat: small leniencies vs beancount (lone *, trailing *, overlapping errors, 1-char metadata keys)

1 participant