Skip to content

fix(parser): read a lone capital glued to a string past its word as one invalid token, as beancount does - #2988

Merged
robcohen merged 2 commits into
mainfrom
fix/capital-glued-quote
Nov 15, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/capital-glued-quote

Conversation

@robcohen

@robcohen robcohen commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

What

A lone capital glued to a string or comment that reaches past its run of non-blank characters is now, with that run, one invalid token, as in beancount 3.2.3.

beancount lexes a lone capital only before a blank ([A-Z]/[ \t\n]). Glued to anything else, its invalid token [^ \t\n\r]+ takes the run up to the next blank, a quote or ; included, so the quote opens no string and the ; no comment, and the next line lexes on its own. Here the capital stayed a token whenever the rest of its run lexed (TokenIter::error_later_in_run), and a " lexed even when its string ran past a blank or onto later lines. So the string swallowed the lines after it:

Input bean-check 3.2.3 main this PR
A@"x / foo" (the issue) {1:1, 2:1} {1:1} {1:1, 2:1}
T"x y" 1 (after two opens) {3:2} {3:1} {3:2}
k: T"a b" (metadata) 3 on the line 1 3
T"x / 2024-01-03 open Assets:C / " {3:1, 5:1} {3:1} {3:1, 5:1}
2024-01-02 open Assets:C T;x y {3:2} {3:1} {3:2}
2024-01-02 T"x y" then two postings {3:2} {3:1} {3:2}

How

  • Lexer (error_later_in_run, crates/rustledger-parser/src/logos_lexer.rs): a token that starts in the capital's run and ends past the run's end (a string with a blank or a line break inside, a comment) counts as an error in the run. The lexer handed back stands at the run's end, so the error is the run, as beancount's is, and lexing resumes at the blank. The Err arm does not take it for a stray quote (its start is past the quote), so no line-at-a-time mode is entered. A string that closes inside the run (T"x") is unchanged: the capital stays a token, which the glued one-letter rules report, one error as in beancount.
  • Dated windows (holds_a_lone_capital_glued_to_a_non_blank, crates/rustledger-parser/src/cst/convert.rs): fix(parser): a dated line that opens no directive is one error, as in beancount (#2979) #2985 left a line holding a capital glued to punctuation as main had it, because the lexer read that capital otherwise. It now asks the lexer, and applies only where the capital is still a token of its own. Where the run is now one invalid token, the line is an ordinary failed line: 2024-01-02 T"x y" with postings is {3:2} as in beancount, where the postings would otherwise each get an error. This also stops it matching a T" inside a string.

Caches

  • WASM CACHE_VERSION 120 to 121: both payloads archive parse errors, which move.
  • Loader cache stays at 75: every changed source has a parse error before and after (main's glued one-letter rules reported the capital), and the loader caches only error-free loads. Across the 16,958 changed inputs of the differential below, none went from error-free to erroring or back.

Parse-error parity corpus (pinned fuzz 9bc79f4)

57 inputs newly match, 3 no longer do, and 27 mismatching inputs changed category. --check passes with the updated manifest.

The 3 lost (10971147319b, 97eb7bfdb11b, 9f2a05fd77d0) each count exactly what main counts with an ordinary invalid token ($q) in place of the glued run, so the run now lexes as beancount's does and the difference is rledger's recovery on that line:

  • qR:R"\r'"0... under a failed dated line: beancount's two invalid tokens, R" and '"0..., are now this PR's too, plus the grammar error rledger reports on such a line (3 against 2). Main matched because its R and string covered the '.
  • 1114-2-13T%Ù\tK+: T%Ù is now one invalid token; K+ (a capital glued to a + that lexes) is still two tokens here, compat(parser): a word like U~SD after an invalid token is one error fewer than beancount #2982's gap, so the line is 1 against beancount's 2.

The 4 ledger2beancount files the local fetch reported as unpinned are upstream content changes, unrelated to this PR; their rows are left out of the manifest.

Generated differential

A generator of lone capitals (and controls: lowercase, two-letter, USD, an account, a digit) glued to every ASCII punctuation character, \x11, € and a BOM; quotes closing on the line, past a blank, on a later line or never, several quotes, ;, escaped quotes, tabs; in 10 contexts (column 0, after two opens, after a date, after a transaction's narration, a posting amount, a metadata value, an open, a posting's account, a price, a cost); followed by 0 to 3 lines with postings, metadata, quotes, directives, blank and comment lines; 15% CRLF, 20% without a final newline. Each changed input was also run through main with the glued run replaced by $q, an ordinary invalid token, which main lexes as beancount lexes the run.

Seed Inputs Gained Closer Regressed Further Sideways Controls changed
1 100,000 + 25,000 controls 4,653 760 57 47 158 0
2 100,000 + 25,000 controls 4,684 724 52 40 158 0
3 100,000 + 25,000 controls 4,628 732 59 43 163 0

In this generator, every regressed, further and sideways input (777) counts exactly what main counts with $q in place of the glued run. No crash. They fall in two families, both #2872's recovery, which main reached the same way only because the string swallowed the lines involved:

359 have only the first, 302 only the second, and 116 both:

  • Indented lines under the error line: beancount's recovery stays quiet on them, rledger reports them, as it does after any column-0 or header invalid token. Example: U*"\" / ; c / Assets:B / " is beancount {1, 4}, this PR {1, 3, 4}, main {1, 4}.
  • A string that starts on a later line: rledger reports a string spanning lines at its first line, beancount at its last (Parse-error recovery differences from bean-check around invalid dates and column-0 markers #2872's "error on the string's end line" row). Example: A+"#t in a posting, then a posting, then " and "x" "y": beancount {4, 7}, this PR {4, 6, 7}, main {4, 7}.

Probes of the issue and the shapes above, 26 inputs: 16 gained, the rest unchanged.

The round-1 review's own generator (300,000 glued inputs and 60,000 controls, more fragments per input) measured 33,226 gained, 30,076 closer, 985 regressed, 2,480 further and 4,943 sideways; controls changed only where a real glued capital exists; no error-free input changed. All but 16 of its regressed, further and sideways inputs count what main counts with $q in place of the run. 14 of those 16 are one shape, documented here and left as is: a mid-line BOM inside the glued run. The run is now one error token led by a capital, which gets the capital-led "Invalid account" reading and the BOM's own BomInDirectiveBody beside it, where $q gets neither: 2024-01-01 open Assets:B then J;<BOM>Assets:A 1 USD is beancount {2:1}, main {2:1}, this PR {2:2}; J"<BOM>Assets:A y" is beancount {1:2}, main {1:1}, this PR {1:3}.

Speed

Linear. The check after a lone capital decides from the run's own bytes whether a comment (;, %, #!, #+) or a string in the run reaches past it: a comment runs to its line break, so it does exactly when the run ends at a blank; a string does when no unescaped " closes it inside the run. It never lexes such a token to its end. (The first version did, and a line of A;x words was quadratic, 3.45 s at 80k; a line of A%x or A#!x words already was on main.) test_tokenize_lossless_lone_capital_lookahead_reads_no_token_past_its_run guards it with a byte counter.

Seconds at 20k / 40k / 80k repetitions, main against this PR:

Shape main this PR
A@"x / foo" lines 0.03 / 0.06 / 0.13 0.05 / 0.12 / 0.23
A" lines 0.02 / 0.03 / 0.05 0.03 / 0.06 / 0.10
T"x y" on one line 0.01 / 0.01 / 0.03 0.05 / 0.11 / 0.21
A@"x, N postings, then " 0.00 / 0.00 / 0.01 0.03 / 0.07 / 0.14
X;c d lines 0.03 / 0.05 / 0.10 0.05 / 0.10 / 0.22
2024-01-02 T"x y" and a posting 0.09 / 0.15 / 0.32 0.13 / 0.29 / 0.49
A@"x\" y" lines 0.04 / 0.06 / 0.11 0.06 / 0.11 / 0.23
A;x on one line (and 160k) 0.00 / 0.00 / 0.00 / 0.00 0.03 / 0.06 / 0.11 / 0.20
2024-01-01 open Assets:A + T;x on one line (and 160k) 0.00 / 0.00 / 0.00 / 0.00 0.02 / 0.05 / 0.10 / 0.18
A%x on one line (and 160k) 0.40 / 1.68 / 6.73 / 25.99 0.03 / 0.05 / 0.10 / 0.20
A#!x on one line (and 160k) 0.53 / 2.20 / 8.32 / 27.51 0.03 / 0.05 / 0.09 / 0.14
A"x on one line (and 160k) 0.01 / 0.02 / 0.04 / 0.08 0.01 / 0.03 / 0.07 / 0.14
A"x\ on one line (and 160k) 0.01 / 0.02 / 0.04 / 0.08 0.02 / 0.03 / 0.07 / 0.14
A"x" on one line (and 160k) 0.01 / 0.01 / 0.02 / 0.03 0.01 / 0.01 / 0.02 / 0.03

How to test

cargo test -p rustledger-parser --test capital_glued_to_a_string
cargo test -p rustledger-parser
cargo test -p rustledger-wasm --lib cache

Every expected count in capital_glued_to_a_string.rs was checked against bean-check 3.2.3: the 15 sources of the main table, each also with CRLF, plus 2024-01-02 T%x y with postings ({3:2}) and 2024-01-02 ~ "a T(" with postings ({3:1}, where main reported each posting too: a T( inside a string no longer counts as a glued capital in #2979's dated window), each with CRLF too.

Closes #2977

🤖 Generated with Claude Code

…ne invalid token, as beancount does (#2977)

A lone capital glued to a string or comment that reaches past its run of
non-blank characters (A@"x with the string closing on a later line,
T"x y", T;x y) is, with the run, one invalid token, as in beancount, so
the quote opens no string and the next line lexes on its own. The dated
window from #2979 now leaves a line alone only where the lexer still
reads such a capital as a token of its own.

WASM cache 121; the loader cache is unchanged (every changed source has
a parse error before and after).

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

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 56.2ms ± 0.8ms 705.3ms ± 3.5ms 12.5x 45.2 MB / 51.0 MB
investment 81.6ms ± 1.0ms 2623.4ms ± 61.3ms 32.2x 47.0 MB / 58.9 MB
fifo 80.1ms ± 1.0ms 14644.0ms ± 396.6ms 182.9x 49.2 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 55.8ms ± 0.9ms 56.2ms ± 0.8ms +0.8% ✅ within noise 44.2 MB → 45.2 MB (+2.1%)
investment 82.6ms ± 0.8ms 81.6ms ± 1.0ms -1.2% ✅ within noise 47.2 MB → 47.0 MB (-0.4%)
fifo 79.3ms ± 0.8ms 80.1ms ± 1.0ms +0.9% ✅ within noise 49.2 MB → 49.2 MB (-0.0%)

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

@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

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

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

The check after a lone capital lexed a comment or string in its run to the
token end, then threw it away when the run ended at a blank, so a line of
A;x words was quadratic. It now decides from the run bytes whether a
comment (; % #! #+) or a string reaches past the run, which also makes
lines of A%x and A#!x words linear (quadratic on main too). A byte
counter guards it.

Tests for T%x y in the flag position and for a T( inside a string no
longer ending #2979s dated window.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit fa3680a into main Nov 15, 2026
62 of 63 checks passed
@robcohen
robcohen deleted the fix/capital-glued-quote branch November 26, 2026 04:41
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 lone capital glued to a quote opens a multi-line string where beancount reads one invalid token

1 participant