Skip to content

fix(parser): read a stray \r before an invalid token as a blank, as beancount does - #2980

Merged
robcohen merged 5 commits into
mainfrom
fix/cr-glued-token
Nov 14, 2026
Merged

robcohen merged 5 commits into
mainfrom
fix/cr-glued-token

Conversation

@robcohen

@robcohen robcohen commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

What

A \r that is not part of a line ending, with an invalid token after it on its line, is now a blank, as in beancount, instead of an error of its own beside the token's. That was one error more per line than bean-check 3.2.3 on every shape in #2966:

Input (line shown) bean-check 3.2.3 main this PR
2024-01-02 open Assets:C\r$ 1 2 1
2024-01-02 * "x"\r$ 1 2 1
Assets:A 1 USD\rx 1 2 1
kk: "v"\r$ (under a note) 1 2 1
a \r<BOM> or \r$ line after close, open, commodity, a transaction, pushtag, or nothing; CRLF; no final newline; the whole file 1 2 1
close Assets:A\r<BOM>~, \r<BOM>@, \r <BOM>~, \r $~, \r $ (also after balance, price) 1 2 1
k: "v"\r<BOM>~, \r<BOM>@, \r <BOM>~, \r$~, \r $~ under a close (LF and CRLF) 2 3 2

Every count in the tests was checked against bean-check 3.2.3.

How

  • Lexer: the \r run (TokenIter::stray_cr_trivia_end). A [ \t\r] run holding a stray \r was trivia only when it reached the line ending (fix(parser): a stray \r before the line ending is a parse error; bean-check accepts it #2774). It is now trivia too when the token the iterator emits after it is an error. That token is read as emitted, after every rewrite in next.
  • Lexer: a lone capital glued to such a \r. Beancount's one-letter rule ([A-Z]/[ \t\n]) does not take a capital before a \r, so B\r$ is Invalid token: 'B', then '$'. On main that count came out right only because the \r error stood in for the capital's. The capital is now the invalid token whenever the \r run after it is trivia.
    • Chains. Whether that run is trivia can depend on the next link (\rB\rC\r$), so cr_chain_is_trivia walks the chain once, in a loop, and remembers the answer for every link. This keeps it linear and non-recursive, pinned by test_tokenize_lossless_cr_lookaheads_are_linear_and_shallow.

    • Runs. A blank-free run that a lone capital starts and a capital glued to the \r ends (P@T, B,B) is ONE invalid token, as in beancount (error_later_in_run).

    • Flag positions are left to the parser, as on main, so a transaction keeps its postings quiet. Three cases:

      • A capital after a column-0 date and blanks (2024-01-02 P\r) is reported by the header's GluedLetterFlag.
      • A capital glued straight to a date that starts its line, indented or not (2024-01-02P\r, 2024-1-2Z\r), is read as the flag by identify_directive.
      • A run that a flag-position capital starts (2024-01-02 P@T\r, 2024-01-02 P@Z\r) is reported by the header rule.

      After an indented date and spaces or tabs, identify_directive takes the one-letter invalid token as the transaction's flag, where the CURRENCY flag stood on main. After a stray \r ( 2024-01-02\rP\r) the line is no transaction, as on main.

  • Error lines (convert.rs):
    • A stray \r in the prefix before a line's first invalid token is read as a blank, in both places that decide whether the line error stands for that token. Such a \r is trivia there by construction.
    • After an invalid date, the line's later grammar errors are dropped, as in beancount, but its lexer errors are kept. A one-letter capital glued to a stray \r is a lexer error in beancount (an invalid token), so its glued-flag error is now kept too (2229-2-37B\r\r\n: beancount 2, main 1, now 2). The exception: when the \r is still an error of its own, that error already stands for it. This is limited to a stray \r, so every input without one is unchanged.

Not changed, deliberately: a \r before a token that lexes (open\rAssets:Cash, \rUSD, a \r that starts a line) is still an error. Beancount accepts these, but rledger format folds a bare \r into a line break, so accepting them would let format split a line check accepted (test_parse_cr_with_more_of_the_line_after_it_stays_an_error). The #2966 case cannot reach the formatter that way, because the line keeps the invalid token's error.

Generated differential (400,000 inputs)

How the inputs were built: random preceding lines (none, two opens, an open transaction, a line that already failed, a note with metadata, a blank line), then a line start (dates at column 0, indented or glued; directive headers; postings; metadata; pushtag; numbers; comments; \r). After that come one to four pieces (capitals, currencies, $, @, ,, ~, strings, tags, links, a BOM, \x01, {1 USD}, txn, k:), each preceded by a separator from "", space, tab, \r, \r\r, \r , \r, \r<BOM>, \r\t. Last come an ending (\n, \r\n, \r\r\n, \r, EOF, \r\t\n, \r\r\r\n, \r\n) and optional follow-up postings or metadata.

How they were run: beancount through the parity harness's own run_beancount, and rledger through run_rledger, compared per line exactly as the harness does.

  • Inputs: 357,618 have a stray \r, and 42,382 are controls without one. No input crashed beancount.
  • Main: matches 40,916 of the stray-\r inputs.
Matching Gained Regressed Net Controls differing from main
this PR 97,548 60,725 4,093 +56,632 0
this PR without the lone-capital rule 93,491 56,842 4,267 +52,575 0

Each regression (matched on main, not here) was bucketed by comparing this PR's count with main's count on an analog. Each analog has the same meaning in beancount. Main mismatches beancount on it the same way.

Family this PR without the capital rule
space: this PR counts what main counts with every stray \r replaced by a space 2,534 3,733
glued: what main counts with every stray \r replaced by $ 915 183
mixed: the \r after a lone capital replaced by $, the other stray \r bytes by a space or kept 422 150
compound: none of those 222 201

Why the lone-capital rule stays:

  • It gains 4,057 more net matches.
  • It costs 21 more compound cases.
  • Every review shape that needs it (B\r$, P\r$, B,B\r) matches only with it.

The compound cases:

  • 220 of the 222 have two or more stray \r on the differing line, each read differently. Some are blanks before an invalid token, some are kept as errors before a token that lexes (86 lines hold one), and some are glued to a capital. A whole-line analog cannot express that mix.
  • The other 2 combine the BOM undercount below with a capital.
  • An independent review differential (300,000 different inputs) also found compound cases with a single stray \r, where main already misses a second invalid token after the first on the same line and its \r error stood in for it: 2024-01-02 P\r$ Assets: is beancount 3, main 3, this PR 2 (main gives 2024-01-02 open Assets:A $ Assets: 1 where beancount gives 2). Other tokens seen there: B"q", T@, a P at EOF.
  • Every one is off by exactly one on a single line. None reports a later line or drops a transaction.

Parity corpus

scripts/compat-parse-error-parity.py against the pinned corpus (fuzz commit 9bc79f4f, 111,323 inputs):

Matching
main (ed1735f5b33) 78,500 (70.5%)
this PR 79,511 (71.4%)
  • Newly matching: 1,035.
  • No longer matching: 24. These are the same 24 inputs as in the first version of this PR. Each was rechecked with every stray \r replaced by a space:
    • 23 of them: beancount gives the same count for \r and for a space, and rledger now reads the \r as main reads the space. Main undercounts that space version too, mostly through a BOM diagnostic standing for more than one of beancount's invalid tokens.
    • The 24th keeps a deliberate \r error and also undercounts on main with a space.

The manifest takes in both sets.

Known mismatches this PR leaves (documented, not fixed)

Each family below is one where this PR counts exactly what main counts for the analog in the last column, and main mismatches beancount on that analog. Counts are for the line shown.

Space family (2,534 in the differential, the 23 corpus losses): a stray \r before an invalid token is a blank, and main already undercounts the same line with a space.

Line beancount main this PR analog on main
2024-01-02\r$ (also \r a, \r<BOM>, \r $~, tab indents) 2 2 1 2024-01-02 $: 1
the same under an open 2 2 1 1
2024-01-02 a\r$ 3 3 2 2024-01-02 a $: 2
1T#t\ra 3 3 2 space: 2
2024-01-02 \r\x01 T\r 2 2 1 space: 1
2024-01-02 txnT\ra T 3 3 2 space: 2

Glued family (915): a capital glued to a stray \r that only ends its line is now an invalid token, so the line counts what main counts for the glued analog. Main had no \r error there, and it matched only because the capital was a token.

Line beancount main this PR analog on main
1 P\r\r\n (also Assets:A P, txn P, 100 P) under an open's metadata 1 1 2 1 P$: 2
\r2024-01-02 P\r at EOF, after the opens or under metadata 1 1 2 \r2024-01-02 P$: 2
P P\r\r\n in a transaction body 1 1 2 glued: 2
2024-01-02 price HOOL 2 USD^l:~B\r\r\n 2 2 3 glued: 3
\t2024-01-02 #tAssets:B\r 2 2 3 glued: 3
1\r,Z\r\r\n 2 2 3 glued: 3
pushtag~\r\t-T\r\r\n 2 2 3 glued: 3

Mixed family (422) and compound family (222): both mechanisms on one line, described above.

Unchanged from main:

  • An indented date with a stray \r before a one-letter flag, in a clean context ( kk: "v"\n 2024-01-02\rP\r): beancount 2, main 1, this PR 1. An earlier head of this PR matched it, but only by making the line a transaction, which was wrong after a line that already failed. There beancount, in error recovery, reports the invalid token alone.
  • After a failed line, an indented date with a flag after a space ( 2024-01-02 P\r): beancount 1, main 2, this PR 2. With postings under such a line, rledger reports each posting, on main too, where beancount reports none.
  • A date followed by an invalid token in the flag position (2024-01-02 $, 2024-01-02 P$, at column 0 or indented) reports every posting under it on main, where beancount reports only the header line.

Caches

  • WASM CACHE_VERSION 114 -> 115. Both payloads archive parse errors, so an older blob would keep serving the old counts.

  • Loader cache unchanged at 75. The loader caches only error-free loads, and every input whose output changes has a parse error both before and after:

    • Every change acts on a line that already errors on main.
    • Where this PR returns to main's reading (flag positions), the line errors on main too.
    • The invalid-date change only keeps an error on a line that has a bad date.
    • In the differential, all 42,382 inputs without a stray \r give exactly main's errors.

    The pin and FIXTURE_VERSION do not move.

How to test

cargo test -p rustledger-parser --test stray_cr
cargo test -p rustledger-parser --lib logos_lexer
cargo test -p rustledger-wasm --lib cache

Closes #2966

🤖 Generated with Claude Code

…eancount does

A `\r` that is not part of a line ending, with an invalid token after it
on its line (`open Assets:C\r$`, `  kk: "v"\r$`, a `\r<BOM>` line,
`close Assets:A\r<BOM>~`, `\r $~`), was an error of its own beside the
token's: one error more per line than bean-check 3.2.3, which reads the
`\r` as a blank.

- The lexer makes such a `[ \t\r]` run trivia when the token it emits
  after the run is an error. A lone capital glued to a stray `\r` that
  is trivia is an invalid token, as beancount's one-letter rule needs a
  space, tab or `\n` after it; a `\rB\rC\r$` chain is walked once, in a
  loop, and its answer kept for every link, so it stays linear.
- The error-line logic reads a stray `\r` in the prefix before a line's
  first invalid token as a blank (it is trivia there by construction).
- A `\r` before a token that lexes stays an error, deliberately, as the
  formatter folds a bare `\r` into a line break.

Parity corpus (111,323 inputs): 78,500 -> 79,504 matching. 1,028 newly
match; 24 no longer do, each one where the `\r` error had stood in for
an error rledger misses with a real space there too. The manifest takes
both in.

WASM cache v115; the loader cache is unchanged, as every input that
changes has a parse error before and after.

Closes #2966

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

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 40.1ms ± 0.4ms 557.6ms ± 2.6ms 13.9x 45.2 MB / 51.0 MB
investment 61.6ms ± 0.3ms 2133.1ms ± 18.9ms 34.6x 47.3 MB / 59.0 MB
fifo 59.9ms ± 0.8ms 11734.3ms ± 240.7ms 196.0x 49.2 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 40.4ms ± 0.7ms 40.1ms ± 0.4ms -0.8% ✅ within noise 45.1 MB → 45.2 MB (+0.2%)
investment 61.7ms ± 0.5ms 61.6ms ± 0.3ms -0.1% ✅ within noise 47.2 MB → 47.3 MB (+0.2%)
fifo 59.7ms ± 0.4ms 59.9ms ± 0.8ms +0.2% ✅ 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 0.4ms, investment 0.3ms, fifo 0.4ms), 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 14, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
crates/rustledger-parser/src/logos_lexer.rs 97.39% 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 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

robcohen and others added 4 commits November 14, 2026 12:34
… flag

Review of #2980: the new lone-capital rule made a transaction's one-letter
flag glued to a stray `\r` (`2024-01-02 P\r\r\n`, `P\r\t\n`, `S`) an
invalid token, which dropped the header and reported every posting and
metadata line under it, where bean-check 3.2.3 and main report the
header line alone.

The rule now leaves the flag position (a capital after a column-0 date
and blanks) to the parser's glued one-letter rule (`GluedLetterFlag`),
which keeps the transaction, as on main. The chain walk skips it too.
`P\r$` is still the flag's error and the `$`'s, as in beancount.

Tests: P and S with `\r\r\n`, `\r\t\n`, `\r$`, `\r $`, `\r\r$` and `\r`
at EOF, each with postings and metadata under the header and with
postings only, per-line counts checked against bean-check 3.2.3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ction

Round-2 review of #2980: after an indented date at the top level, a
one-letter flag glued to a stray `\r` (`  2024-01-02 P\r\r\n`, `P\r` at
EOF, tab indent, CRLF) became an invalid token in the flag position, so
the line was no transaction: the indented-directive error swallowed the
token's, and every posting under it was reported. bean-check 3.2.3 and
main report two errors on that line and nothing under it.

The lexer cannot tell a top-level indented line from a body line, so the
parser decides: `identify_directive` takes a one-letter capital invalid
token after a date and a blank as a transaction's flag, as it takes the
one-letter `CURRENCY`. The transaction keeps its postings quiet, and the
line has the indented-directive error and the invalid token. Under a
transaction the line stays a body line, with beancount's two errors.

Tests: space and tab indent, CRLF, `\r\r\n`, `\r\t\n`, `\r` at EOF and
`\r$`, with and without postings, at the top level and under a
transaction, per-line counts checked against bean-check 3.2.3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er a blank

Round-3 review of #2980: the `identify_directive` arm that takes a
one-letter capital invalid token after a date as a transaction's flag
also fired after a stray `\r` (`  2024-01-02\rP\r\r\n`). After a line
that already failed (`open Assets:A $`, `$`, `  kk: "v" $`, `  k: "v"`
under open/close/note), beancount is in error recovery and reports only
`Invalid token: 'P'` there, as main does; the arm made the line a
transaction and added the indented-directive error, one more.

The arm now requires spaces or tabs between the date and the capital,
where the one-letter `CURRENCY` flag stood on main, so after a stray
`\r` the line reads as on main. The round-2 indented-date shapes, with
a blank, are unchanged.

Tests: the five failed-line contexts, `\r`, `\r ` and ` \r` before the
capital, `\r`, `\r\r\n` and `\rB\r\r\n` after it, and `  k: 1` with
`\r$`, per-line counts checked against bean-check 3.2.3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…beancount does

Round-4 review of #2980:

- A blank-free run that a lone capital starts and a capital glued to a
  stray `\r` ends (`P@T\r`, `B,B\r`) counted the last capital as an
  error of its own after the run; beancount reads the run as ONE invalid
  token. `error_later_in_run` now sees that last capital as the run's
  error, so the run merges.
- A capital glued straight to a date that starts its line
  (`2024-01-02P\r`, `  2024-1-2Z\r`) became an invalid token, which
  dropped the transaction and reported every posting under it. It is
  left to the parser as the flag, as on main, and so is a run that a
  flag-position capital starts (`  2024-01-02 P@Z\r`).
- After an invalid date the line's grammar errors are dropped, but its
  lexer errors stand. A one-letter capital glued to a stray `\r` is an
  invalid token in beancount, so its glued-flag error now stands too
  (`2229-2-37B\r\r\n`: beancount 2, main 1, now 2), unless the `\r` is
  still an error of its own, which stands for it as on main.

A generated differential of 400,000 inputs (357,618 with a stray `\r`)
against bean-check 3.2.3: 60,725 newly match, 4,093 no longer do, each
bucketed by mechanism in the PR; no input without a stray `\r` changes.
Without the lone-capital rule the net is 4,057 lower. The parity
corpus is at 79,511 (main 78,500); the manifest moves 11 pins.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 9e834a9 into main Nov 14, 2026
63 of 64 checks passed
@robcohen
robcohen deleted the fix/cr-glued-token branch November 14, 2026 19:51
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.

compat(parser): a \r with a token glued after it counts one error more than beancount

1 participant