Skip to content

fix(parser): report a second BOM at the start of a file, as on any later line - #3101

Merged
robcohen merged 1 commit into
mainfrom
fix/double-bom-line1
Nov 29, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/double-bom-line1

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

A second BOM at the start of a file is now reported (P0026, an invalid token on line 1), the same way one at the start of any later line is.

Input (line 1) beancount 3.2.3 rledger before rledger after
<BOM><BOM>;x / %x / !x Invalid token nothing (a comment line) P0026
<BOM><BOM> (rest of the line empty) Invalid token nothing P0026
<BOM><BOM>; c x 3 errors nothing 3 errors
<BOM><BOM>$x, <BOM><BOM>2024-01-01 open ... 1 1 1 (now P0026)

Why

convert strips the file's leading BOM, then built its tree with SourceFile::parse(stripped). That token stream (KindTokenIter::new) strips a leading BOM too. So a second BOM at byte 3 was taken for the leading one, and the rest of line 1 was lexed at column 0. Under beancount's line-skip rules, ;x, %x and !x there are comment lines, so nothing was reported. The bom.rs test said the second BOM was left "for the lexer to flag", but it never got to the lexer.

How

  • parse_structured_stripped / SourceFile::parse_stripped / KindTokenIter::new_stripped build the tree from an already-stripped source without stripping it again. Only convert calls them. ParseResult::syntax_root is still that tree, and the syntax_root round-trip test now compares against this entry.
  • On line 1 the lexer now sees the BOM exactly as it sees a line-start BOM anywhere else, so <BOM>;x is one invalid token, as in beancount.
  • The tree still holds the second BOM as a BOM token of its own. The formatter keys on that token to keep the leading BOM, so its shape is unchanged. Anything else the lexer reads into the same invalid token follows as an ERROR_TOKEN. Both converter walks (green and red) report a BOM token as BomInDirectiveBody. In the converter's tree it can only be a second BOM.
  • Tests:
    • New regression test test_parse_second_bom_at_file_start_is_reported: line 1 and a later line give the same per-line counts for ; % ! & ? : # $ *, an empty rest, and ; c x.
    • New test test_parse_star_after_second_leading_bom_is_reported.
    • Updated test_format_source_reads_line_endings_as_the_tree_reads_a_second_bom: the tree now reads <BOM>""" and <BOM>" as beancount does, so the \r\n lands inside or outside a string as there.
  • Loader cache v84 (a file that loaded clean now has a parse error, and an error load is never cached), WASM cache v166, pin 84. Other open parser PRs use 83/165 and 85/167; whichever merges later rebases.
  • docs/reference/compatibility.md P1 notes the second BOM. Changelog entry added under Fixed.

Not changed: <BOM><BOM>*x still names its error invalid token 'x', where beancount names the whole run '*x'. The line count matches. The oracle's reconciler keeps the narrowest diagnostic on the slot, and fixing that is outside this change.

Parity

  • compat-parse-error-parity.py --check: no regressions. 5 pinned fuzz bc-only inputs now match.
    • fuzz: 43994 → 43999 matches.
    • all: 122378 → 122383 (99.2%).
    • bc-only: 20 → 15.
    • The impl-fewer|oracle:lex:token|top|^ error category (5) is gone.
    • Manifest updated.
  • compat-semantic-parity.py --check: no regressions. Match rate 99.93%.

How to test

cargo test -p rustledger-parser --test line_start_bom_recovery --test indented_star_line --test format_roundtrip_properties
printf '\xef\xbb\xbf\xef\xbb\xbf;x\n' > /tmp/b.beancount && rledger check /tmp/b.beancount   # P0026 on line 1

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 29, 2026 00:59
@robcohen
robcohen force-pushed the fix/double-bom-line1 branch from 6463ce7 to 3c6f918 Compare November 29, 2026 01:11
@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 414.9M 417.9M +0.72% ✅ +19.6%
check/investment 616.8M 620.9M +0.67% ✅ +20.3%
check/tagged 639.5M 644.9M +0.84% ✅ +25.1%
check/multicurrency 579.8M 583.9M +0.70% ✅ -0.1%
check/fifo 594.7M 598.5M +0.64% ✅ +13.3%
check/metadata 1.01G 1.02G +0.96% ✅ +27.6%
check-warm/simple 162.4M 162.4M +0.00% ✅ +3.1%
format/simple 1.07G 1.07G +0.55% ✅ +98.8%
format/metadata 2.71G 2.73G +0.73% ✅ +129.4%
format/errors 120.7M 121.3M +0.49% ✅ +167.7%
check/errors 303.2M 303.7M +0.17% ✅ —
check-json/errors 183.3M 183.9M +0.30% ✅ —
check/junkline 33.2M 33.2M +0.06% ✅ +335.6%
check-json/junkline 46.7M 46.6M -0.08% ✅ +659.6%
query/journal/simple 641.7M 644.3M +0.41% ✅ +3.5%
query/groupby/simple 509.7M 512.4M +0.53% ✅ +11.4%
query/groupby/investment 800.5M 804.1M +0.45% ✅ +7.9%

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 52.7ms ± 2.4ms 663.0ms ± 9.2ms 12.6x 43.0 MB / 51.1 MB
investment 78.5ms ± 2.0ms 2390.1ms ± 13.8ms 30.4x 55.2 MB / 59.0 MB
fifo 78.6ms ± 1.0ms 13382.4ms ± 290.7ms 170.3x 55.2 MB / 58.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 51.3ms ± 0.7ms 52.7ms ± 2.4ms +2.8% ✅ within noise 43.3 MB → 43.0 MB (-0.8%)
investment 79.0ms ± 2.5ms 78.5ms ± 2.0ms -0.6% ✅ within noise 54.3 MB → 55.2 MB (+1.6%)
fifo 78.6ms ± 1.0ms 78.6ms ± 1.0ms -0.1% ✅ within noise 59.1 MB → 55.2 MB (-6.6%)

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

@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

@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!

@robcohen
robcohen force-pushed the fix/double-bom-line1 branch from 3c6f918 to 75c6b2a Compare November 29, 2026 04:12
…ter line

The parser strips the file's leading BOM and then built its tree with
`SourceFile::parse`, whose token stream strips a leading BOM again. A second
BOM at byte 3 was taken for the leading one and the rest of line 1 lexed at
column 0, so `<BOM><BOM>; x`, `% x`, `! x` (and a bare `<BOM><BOM>`) were a
skipped comment line with no error, where bean-check 3.2.3 reports
`Invalid token` on line 1.

The converter now builds its tree with `parse_structured_stripped`, which
does not strip again: line 1 lexes as a line a BOM starts does anywhere
else (`<BOM>;x` is one invalid token). The tree keeps that BOM as a `BOM`
token of its own, the shape the formatter keys on, with the rest of its
invalid token after it, and both converter walks report it (`P0026`).

Parse-error parity: 5 pinned `bc-only` fuzz inputs now match (fuzz
43994 -> 43999), no regressions. Loader cache v84, WASM cache v166.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/double-bom-line1 branch from 75c6b2a to a20ccd3 Compare November 29, 2026 07:31
@robcohen
robcohen merged commit 5949a58 into main Nov 29, 2026
67 of 71 checks passed
@robcohen
robcohen deleted the fix/double-bom-line1 branch November 29, 2026 07:58
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]>
robcohen added a commit that referenced this pull request Nov 30, 2026
`<BOM><BOM>*x` on line 1 reported `P0012 unexpected input: invalid token
'x'` at column 8, where the same line anywhere else, and `<BOM><BOM>$x` on
line 1, report the BOM (`P0026`). bean-check 3.2.3 reports one
`Invalid token: '<BOM><BOM>*x'` on each.

The tree keeps line 1's second BOM as a token of its own (#3101) and
lexes the rest of the word apart, so the line held a diagnostic on the
`x` as well as the BOM's, both on the token the reference reports, and
the reconciler named the slot with the tightest. `reconcile` gains a
`names(e, slot)` hook, taken before width when it pairs a slot with a
diagnostic on its token; beancount's names that second BOM's `P0026` for
the token beancount reads from it, on line 1 only (P1). Later lines are
unchanged (a wider rule took a mid-file `InvalidAccount` message over a
BOM-led account word). The line keeps its error count.

The header-defect error that line 1 had sat inside the transaction after
the BOM, so `transactions_beancount_drops` asked it and left it out, as
beancount builds nothing there; the BOM's `P0026` starts before the
transaction's span. The rule now reads a directive from the start of its
header line (past a byte-0 BOM, as P1 strips it), so such a transaction
stays out of the ledger.

Loader cache v86, WASM cache v170.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 30, 2026
…ne (#3168)

`<BOM><BOM>*x` on line 1 reported `P0012 unexpected input: invalid token
'x'` at column 8, where the same line anywhere else, and `<BOM><BOM>$x` on
line 1, report the BOM (`P0026`). bean-check 3.2.3 reports one
`Invalid token: '<BOM><BOM>*x'` on each.

The tree keeps line 1's second BOM as a token of its own (#3101) and
lexes the rest of the word apart, so the line held a diagnostic on the
`x` as well as the BOM's, both on the token the reference reports, and
the reconciler named the slot with the tightest. `reconcile` gains a
`names(e, slot)` hook, taken before width when it pairs a slot with a
diagnostic on its token; beancount's names that second BOM's `P0026` for
the token beancount reads from it, on line 1 only (P1). Later lines are
unchanged (a wider rule took a mid-file `InvalidAccount` message over a
BOM-led account word). The line keeps its error count.

The header-defect error that line 1 had sat inside the transaction after
the BOM, so `transactions_beancount_drops` asked it and left it out, as
beancount builds nothing there; the BOM's `P0026` starts before the
transaction's span. The rule now reads a directive from the start of its
header line (past a byte-0 BOM, as P1 strips it), so such a transaction
stays out of the ledger.

Loader cache v86, WASM cache v170.

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