Skip to content

fix(parser): read a stray \r after a line's first token as a blank - #3099

Merged
robcohen merged 1 commit into
mainfrom
fix/stray-cr-posting
Nov 29, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/stray-cr-posting

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

A stray \r (one that is not part of a CRLF line end) after a line's first token is now a blank, as beancount 3.2.3 reads it:

2024-01-02 * "t"
  Assets:A  10 USD @@\r 9 EUR
  Assets:B

On main, rledger gave two errors here and dropped the total price. It did the same with Assets:A 10\r USD, where it dropped the units, and then booking reported an interpolation error on top. bean-check accepts both. The parity manifest pinned this as impl-more|impl:P0012|indented|INDENT ACCOUNT (42 inputs). Dropping a value changes the file's meaning, and P3 in compatibility.md promised at most one error.

How

There is still one lexer and one rule. TokenIter::stray_cr_trivia_end turns any [ \t\r] run that holds a stray \r into Whitespace trivia once the line has a token. So it applies between tokens, inside cost specs and prices, before a comment, and in metadata. The bytes stay in the CST, so lossless round-trip is unchanged.

A run that starts its line is different in beancount too: its indent rule does not accept a run that holds a \r. I checked this black-box. \r Assets:A inside a transaction gives unexpected ACCOUNT, and \r2024-01-01 open … parses as an open. rledger keeps that \r as an error of its own (P3, now narrowed to this case). The exceptions are unchanged: the run is trivia if it ends the line, or if the token after it is an error anyway (#2966).

Because a mid-line run is now always trivia, the #2966 chain walk (cr_chain_is_trivia, the peeking clones, cr_chain_until) is no longer needed and is removed. A line-start run peeks one token (line_start_run_is_trivia).

Docs: the P3 section and the stray-carriage-return overlay doc now describe only the line-start case.

Tests

  • stray_cr.rs:
    • test_parse_cr_between_tokens_is_a_blank checks units, cost, price, arithmetic, tags, metadata and flags. Each posting must parse error-free and identical to the same text with a space in place of every \r.
    • test_parse_cr_in_a_line_start_run_stays_an_error pins the line-start case.
  • Updated pins, each re-checked against bean-check 3.2.3:
    • bison_recovery_model, unreadable_line_recovery, mid_line_hash_bang: Assets:A 1 USD\r#!x now gives beancount's two errors (#! and x).
    • The stray-char matrix: its 5 known mid-line \r exceptions now agree with beancount, so the exception is removed.
  • cargo test -p rustledger-parser passes, including the format tests and the strict corpus/CST baselines. Clippy with -D warnings is clean on parser, loader and wasm.

Parity (beancount 3.2.3)

  • compat-parse-error-parity.py --check: 122,478 / 123,394 match, up from 122,378.
    • 113 pinned mismatches now match.
    • 13 inputs that matched now mismatch. I verified that each of them gives the same per-line counts, in both rledger and beancount, as the input with a space for every \r. So they are existing mismatches that the extra \r error happened to cancel out: a BOM-led indented line, and a builder error (too many strings) on a transaction beancount drops for an error in its body. They are left out of this PR's scope. The manifest is updated.
  • compat-semantic-parity.py --check: passes, unchanged (9,563 match, 7 mismatch).

Loader cache v85, WASM cache v167 (pin 85). Other open parser PRs take 83/165 and 84/166; whichever merges later rebases its number.

How to test

printf '2024-01-01 open Assets:A\n2024-01-01 open Assets:B\n2024-01-02 * "t"\n  Assets:A  10 USD @@\r 9 EUR\n  Assets:B\n' > /tmp/x.bc
rledger check /tmp/x.bc   # clean, as bean-check

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 29, 2026 00:45
@robcohen
robcohen force-pushed the fix/stray-cr-posting branch from 8a8d625 to 0052798 Compare November 29, 2026 01:15
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Instruction counts vs base (cachegrind, deterministic)

Workload base this PR Change vs v0.24.0
check/simple 417.8M 417.8M -0.01% ✅ +19.5%
check/investment 621.5M 621.0M -0.07% ✅ +20.4%
check/tagged 645.1M 644.8M -0.03% ✅ +25.1%
check/multicurrency 583.8M 583.9M +0.02% ✅ -0.1%
check/fifo 598.4M 598.6M +0.02% ✅ +13.3%
check/metadata 1.02G 1.02G -0.04% ✅ +27.6%
check-warm/simple 162.4M 162.4M +0.00% ✅ +3.1%
format/simple 1.07G 1.07G -0.02% ✅ +98.8%
format/metadata 2.73G 2.73G -0.02% ✅ +129.3%
format/errors 121.3M 121.3M -0.01% ✅ +167.6%
check/errors 303.9M 303.7M -0.06% ✅ —
check-json/errors 183.9M 183.9M -0.05% ✅ —
check/junkline 33.3M 33.2M -0.15% ✅ +335.6%
check-json/junkline 46.7M 46.6M -0.23% ✅ +659.1%
query/journal/simple 644.7M 644.3M -0.06% ✅ +3.5%
query/groupby/simple 512.7M 512.3M -0.07% ✅ +11.4%
query/groupby/investment 804.7M 804.3M -0.05% ✅ +8.0%

Instruction counts (cachegrind I refs) are deterministic: the same binary on the same input gives the same count to within ~0.02%, so unlike wall clock a 1% change is real. 🟡 ≥ 1%, 🔴 ≥ 5%. The last column is cumulative drift from the pinned v0.24.0 baseline (scripts/perf-baseline.json).

Wall clock (hyperfine)

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 37.7ms ± 2.3ms 564.2ms ± 10.4ms 15.0x 42.8 MB / 51.0 MB
investment 57.5ms ± 0.5ms 2113.0ms ± 10.0ms 36.7x 55.2 MB / 59.2 MB
fifo 57.1ms ± 0.4ms 11681.3ms ± 44.1ms 204.7x 57.0 MB / 58.7 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 36.9ms ± 0.3ms 37.7ms ± 2.3ms +2.4% ✅ within noise 43.2 MB → 42.8 MB (-0.9%)
investment 57.7ms ± 0.5ms 57.5ms ± 0.5ms -0.2% ✅ within noise 55.1 MB → 55.2 MB (+0.2%)
fifo 56.9ms ± 0.4ms 57.1ms ± 0.4ms +0.3% ✅ within noise 59.2 MB → 57.0 MB (-3.8%)

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

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 29, 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 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 force-pushed the fix/stray-cr-posting branch from 0052798 to 11378a2 Compare November 29, 2026 04:21
`  Assets:A  10 USD @@\r 9 EUR` and `  Assets:A  10\r USD` are clean in
beancount 3.2.3, which reads a `\r` that is not part of a CRLF line end
as a blank. rledger reported the `\r` as an error of its own (P3, kept
from when the formatter folded a bare `\r` into a line break, #2804),
and the parser then dropped what followed it from the posting (the
price, the units), so the transaction also failed to book.

The lexer now turns any `[ \t\r]` run holding a stray `\r` into
`Whitespace` trivia once the line has a token. A run that starts its
line keeps its error (beancount does not take it as an indent, so it
reads the line as unindented), unless it ends the line or the token
after it is an error anyway. With mid-line runs always trivia, the
longer needed: a line-start run peeks one token.

P3 in compatibility.md and the overlay doc say only the line-start
case now. Parse-error parity: 122,478 of 123,394 (was 122,378); the 13
inputs that newly mismatch read the same as with a space for the `\r`.
Loader cache v85, WASM cache v167.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/stray-cr-posting branch from 11378a2 to 70fdebb Compare November 29, 2026 08:03
@robcohen
robcohen merged commit 2ecf161 into main Nov 29, 2026
68 of 74 checks passed
@robcohen
robcohen deleted the fix/stray-cr-posting branch November 29, 2026 08:26
robcohen added a commit that referenced this pull request Nov 29, 2026
…ndants walk

Since #3101 the descendants walk reports every BOM token in the tree (the
leading BOM is stripped before the build, so one there is a second BOM), but
the recorder only asked for the walk on an invalid token: the lean parse
skipped it on such a file and lost the BomInDirectiveBody error. The recorder
now flags a BOM token as it does an invalid one. The node-shape fixtures gain
a second BOM at the start of the file and on a later line, and stray \r
blanks after a line first token (#3099).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 29, 2026
… built (ADR 0010) (#3113)

* perf(parser): record the lines a defect check reads while the tree is built

The tree builder hands each line, in the role it gives it (header, posting,
metadata, other body line), to a BuildObserver. The converter builds with a
recorder that runs the canonical line rules on the builder's own token
slice once and keeps only which top-level items hold a line some check
could report on. The checks then read the tree for those items alone; a
clean file flags nothing.

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

* perf(parser): skip the descendants walk when the tree builder saw nothing it reports

The recorder now also notes what the descendants walk could report on (an
invalid token, a cost spec with a shape defect or no closing brace, a link as
a metadata value, a custom or pushmeta directive) and collects the column-0
comments by the walk's own rule as the tokens go by. Without occurrences to
collect, a file with none of those needs no walk. The line extraction is now
slice-based, so the recorder copies no tokens for a clean line.

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

* perf(parser): cheaper recorder verdicts for metadata and sale postings

Inline the recorder's per-token hook; a plain metadata line skips the walk's
link scan; the plain-posting shortcut takes a sale's empty cost {}; and
unevaluable_meta_value answers None before allocating when the value holds
no operator or parenthesis, as cost_region_read would.

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

* docs: ADR 0010, the tree builder records which lines the defect checks read

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

* docs(changelog): record the build-time line checks

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

* fix(parser): the line-check recorder flags a second BOM for the descendants walk

Since #3101 the descendants walk reports every BOM token in the tree (the
leading BOM is stripped before the build, so one there is a second BOM), but
the recorder only asked for the walk on an invalid token: the lean parse
skipped it on such a file and lost the BomInDirectiveBody error. The recorder
now flags a BOM token as it does an invalid one. The node-shape fixtures gain
a second BOM at the start of the file and on a later line, and stray \r
blanks after a line first token (#3099).

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

* docs(changelog): re-measure the build-time line checks against main

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

* docs: ADR 0010 names a second BOM among the walk's triggers

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

* docs(parser): drop a redundant intra-doc link target in line_checks

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

---------

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