Repository navigation
fix(parser): model bison's error recovery on beancount's tokens, so rledger is as quiet as beancount after every failed line - #3035
Merged
Conversation
…ledger is as quiet as beancount after every failed line beancount's one error rule is `declarations: declarations error`: after a failed statement bison discards what a `declarations` cannot start with, shifts only an end of line, a date or an undated keyword, and reports no syntax error until it has shifted three tokens; only its lexer's errors are reported meanwhile. The earlier windows modeled this for a few line kinds. A new pass, `cst/recovery.rs`, ports beancount's lexer (`lexer.l`) and runs bison's recovery over its tokens from each error rledger reports while bison is not recovering. On every line inside the window it keeps as many of rledger's errors as beancount's lexer finds there. An entry bison builds at a date it shifts mid-line (#2979) is parsed on its own for its body's errors and not emitted, a recovery artifact not copied. A lone capital glued to a `\r` or the end of the input is the invalid token it is to beancount (#3028's leftover). The parity harness moves beancount's `unexpected EOL` errors back one line: rledger reports an end-of-line error on the line it ends, on purpose. Also makes three error lookups linear (multi-line string placement, the posting and metadata line checks), which were quadratic in a file with many parse errors. Parity corpus: 93,185 -> 106,447 matching, 0 regressions. WASM cache 132. Closes #2993 Closes #2979 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
robcohen
force-pushed
the
fix/recovery-window-general
branch
from
November 26, 2026 02:25
b63e244 to
da10f0e
Compare
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
robcohen
added a commit
that referenced
this pull request
Nov 26, 2026
…valid token, and model four more recovery shapes (#3036) #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]>
This was referenced Nov 26, 2026
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]>
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
After a line beancount cannot read,
rledgernow reports what bean-check 3.2.3 reports until beancount's grammar has read three tokens again. This now holds after every kind of failed line, where the earlier windows covered a few kinds. The changes, by issue:pushtag #a $/ blank /2024-02-30 5: bean-check {2, 4}, main {2, 4x2}, branch {2, 4}). So is the unterminated-string row.optionrows are quiet too, so they count one error, not two.2024-01-02 2024-01-03 * "n"followed by postings is one error, as in beancount. The postings are the body of the entry bison builds there.Model
beancount's grammar has one error rule,
declarations: declarations error. After a failure, bison:declarationscannot start with, so an indented line goes to its end;During that window its lexer still reports invalid tokens, invalid dates and malformed numbers. A lexer error is
YYerror, which puts bison straight into recovery.crates/rustledger-parser/src/cst/recovery.rs(quiet_bison_recovery) implements this in four parts.BcLexeris a port oflexer.l:INDENT, and theIGNORErules for lines it skips;YYerrorfor invalid runs, invalid dates and malformed numbers.acceptsencodes the first three tokens of every statement, taken fromgrammar.y.The driver starts at each error rledger reports while bison is not recovering. It finds the token bison fails at, which is an indent for an orphan indented line, and steps the error status. Then, on each physical line inside the window, it keeps as many of rledger's errors as beancount's lexer finds there. It keeps the error that failed the line first, then rledger's invalid-token errors.
push/popthat bison builds mid-line changes state, and that is not modeled, so that line keeps its errors.Linear time:
RECOVERY_SHIFT_SCANNED_BYTES. A new unit test requires at most 8× the input over seven shapes.Not replaced:
quiet_after_failed_line_startsstill runs ahead of the new pass. I tested removing each part of it:It still wins on its calibrated shapes (BOM-started lines, an invalid second date), so I kept it and documented the division between the two.
Deliberate differences (documented, not copied)
\n. rledger keeps it on the line whose statement fails, where the mistake is.syntax error, unexpected EOLback one line (docstring and self-test updated).!, whose error beancount puts on the next line.YYerror. That also drops the entry's builder errors (too many strings, booking method):* "a" "b" "c"/foogives bean-check {3}, rledger {2, 3}. Losing a valid entry is a beancount bug, so rledger keeps the entry and its error. A test pins this.Leftovers, each
AB-C/key:2024/01/03 P | 1$ 2024-01-02 * "y"+ posting$/Assets:A 1 USD 2024-01-02$/2024-01-02 123/ postingP T h1 Z\r\n(also at EOF)option "a"(EOL on next line)* "a" "b" "c"/fooP T !/ postingAlso fixed (found by the timing runs)
Three lookups were quadratic in a file with many parse errors. All three were present on main:
report_at_multi_line_string_endsdescended from the root for each error (fix(parser): report a syntax error at a multi-line string on the line the string ends #3033).posting_line_checkandMetaCheckeach scanned the directive's errors for every line.Each is now a binary search over one sorted list. Effect: 160k
pushmeta k: "v"lines took 205 s and now take 0.27 s; 40k bad postings took 11.6 s and now take 0.29 s.Results
Parse-error parity (pinned fuzz
9bc79f4f, matrix, tracked, compat; beancount 3.2.3 from nixpkgs):--update; 13,999 pins were stale.BOM combination matrix: 11,179 → 11,893 rows match. Regenerated with
BOM_MATRIX_UPDATE=1 BOM_MATRIX_RELABEL=1:Generated differentials against main (
8e6feae). Three generator designs, six seeds each, 870,000 inputs:\x11,\r, glued)examples/, parser test data){*, *}cost-merge spec inside an entry bison builds after$. beancount reportsDuplicate merge-cost specthere, which is part of the cost merging rledger supports on purpose. The entry, parsed alone, matches rledger's own count.\r(fix(parser): read a stray \r before an invalid token as a blank, as beancount does #2980) on the entry line.Timing: linear. Seconds at 20k / 40k / 80k / 160k repetitions, branch:
$lines$+ postingpushmeta k: "v"linespushtag #a $/ blank /2024-02-30 5$ 2024-01-02 * "y"× N on one lineA further sweep of 17 shapes at 20k vs 80k gave ratios between 3.1 and 4.6.
Caches:
CACHE_VERSION131 → 132, since it archives parse errors. The v130 note is corrected: v129 was skipped, and fix(wasm): run Early validation over the stream as written, so an E3002 on a transaction that books is reported #3027 landed as v131.Tests
New:
crates/rustledger-parser/tests/bison_recovery_model.rs. It pins:Every count was checked against bean-check 3.2.3, and green and red agree on each.
Unit tests in
recovery.rscover the lexer port,accepts, and the linear byte bound.Updated, all toward bean-check 3.2.3 (re-measured):
unreadable_line_recovery.rs(its "not modeled" rows now match),dated_line_no_directive.rs(EOL placement),bom_account_and_tag_recovery.rs,bom_mid_line_run.rs,line_start_bom_recovery.rs,green_red_properties.rsandbom_combination_matrix.rs.green_red_properties.rs, the unclosed-cost fixture moved after a blank line, because a posting right after the failed metadata line is now quiet, as in beancount.Corpus manifest:
tests/baselines/parser-corpus.manifesttakes the new output hashes of the three compat files whose parse errors move (double-entry-generator/example_oklink,parser-lima/examples_data_custom,examples_data_full), sources unchanged.Checks:
cargo test -p rustledger-parser -p rustledger-lsp -p rustledger-wasm -p rustledger-loader -p rustledgerpasses, as do clippy with-D warnings --all-targets --all-features,cargo doc -Dwarningsfor the parser,cargo fmt --check, and the harness--self-test.How to test
Closes #2993
Closes #2979
🤖 Generated with Claude Code