Skip to content

fix(parser): an indented tag or link line in a transaction, then an invalid token, is one error - #3028

Merged
robcohen merged 1 commit into
mainfrom
fix/indented-tag-line-invalid-token
Nov 25, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/indented-tag-line-invalid-token

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

An indented line of tags and links in a transaction, then an invalid token ( #tag h, ^link h, #tag $), is now one error, as in bean-check 3.2.3. rledger reported the line's grammar error beside the token's own.

Why it works

beancount's grammar reads a tags-and-links line in a transaction's body (posting_or_kv_list INDENT tags_links EOL). So on #tag h its first error is the lexer's Invalid token: 'h', and its recovery discards the rest of the line. drop_errors_inside_a_line_error (crates/rustledger-parser/src/cst/convert.rs) merges the generic line error with a line's first invalid token when the prefix before that token is one beancount reads. It accepted nothing, a flag, or a posting start. Inside a transaction node (span ranges gathered during the directive loop), a prefix made only of tags and links now counts as readable too. The line error then stands for the token and takes its name (unexpected input: invalid token 'h'), and each later invalid token is still one more.

This is the same place the issue named. The table's misses reach drop_errors_inside_a_line_error's readable_start closure, not readable_start_of_an_error_line: inside a transaction the line is not an ERROR_NODE, so the latter never sees it.

Under open, balance, note, event and custom such a line is a syntax error at the tag first. Both errors stay there, as in beancount.

Narrowed: one shape keeps main's two errors. That is a line where a lone capital glued to what follows it (Z\r, or Z at the end of the input) comes after the token. beancount reports that capital as one more invalid token, and this parser does not yet do so on any line ( h1 Z\r\n is 1 here, 2 in beancount, on main too). main matched there only because the extra line error stood in for it. Applying the fix there would have turned 10 generated inputs from matching to mismatching. The underlying miss is left for a follow-up.

Results

Issue rows: all match bean-check 3.2.3 (P = 2024-01-01 open Assets:A\n, T = 2024-01-02 * "n"\n):

Input beancount main branch
P + T + " #tag h\n" 3 3x2 3
P + T + " ^link h\n" 3 3x2 3
P + T + " #tag $\n" 3 3x2 3
P + T + " #tag hh\n" 3 3x2 3
control P + " #tag h\n" 2x2 2x2 2x2
control P + T + " h\n" 3 3 3
control P + T + " Assets:A 1 USD h\n" 3 3 3
control P + T + " #tag\n" + postings - - -

Parse-error parity corpus (pinned fuzz 9bc79f4f, matrix, tracked, compat; beancount 3.2.3 from nixpkgs): 92,482 → 92,545 inputs match, with 0 regressions against main's committed manifest. 67 pins were stale: 63 inputs now match and 4 changed category. The manifest is rewritten with --update, and only id lines move.

Source Before After
fuzz 29,465 29,490
matrix 62,147 62,185
compat 488 488
tracked 382 382

Generated differential: a generator built transaction bodies (and open / balance / custom / event / note / commodity / price / pad bodies). Their lines held tag and link runs with invalid tokens inserted, glued and repeated, plus other tokens, postings, metadata, comments, blank lines, mixed indentation, CRLF and no final newline. It compared per-line counts of beancount 3.2.3 (parse_string), main bf195c80321 and this branch over 3 seeds × 40,000 inputs:

Inputs Gained Closer Regressed Further Sideways
120,000 6,368 6,244 0 21 6

All 27 further or sideways inputs already mismatched on main. Each holds an indented lone posting flag ( !, \t?, Z) before the tag line. beancount reports that flag's unexpected EOL, expecting ACCOUNT on the next line, and rledger reports it on the flag's own line. main's extra error on the tag line offset that by coincidence. The tag line now counts as a plain invalid-token line does in the same spot ( !\n h\n, unchanged).

Caches: WASM 125 → 126, since it archives parse errors. (The open WASM validation PR also takes 126; whichever merges second renumbers.) The loader stays at 75: every changed source has a parse error on the line before and after, and the pass only drops errors, so parsed directives do not move.

Timing: linear. 40k / 160k #a ^l h x lines: main 0.12 / 0.43 s, branch 0.10 / 0.39 s. The extra scan of the rest of the line is added to ERROR_LINE_SCANNED_BYTES, and the existing linearity test passes.

Tests

  • New file: crates/rustledger-parser/tests/indented_tag_line_invalid_token.rs. It pins the issue rows and controls, and more prefixes, indentations and line endings (#a ^l h, \t^l h, #a$, #a h #b, #a h ;c, CRLF, EOF). It also pins a tag line after a posting, two invalid tokens (#a h x → 2), prefixes beancount fails on first (#a 1 h, #a 2024-01-03 h, #a Assets:B h → 2), other directives (→ 2) and the narrowed lone-capital shapes (→ 2). Every count was checked against bean-check 3.2.3, and green and red agree on each.
  • BOM combination matrix: regenerated with BOM_MATRIX_UPDATE=1 BOM_MATRIX_RELABEL=1. Six rows moved from known to matching (11,166 → 11,172), and two known rows are relabelled with the same counts.
    • In worse_than_main.tsv, the T#! row is no longer worse than main, so it is removed.
    • The open ... #t ^l\t<BOM> row's line now counts 1 alone, as beancount does, so its cause is the recovery cause indented-line-after-error, alone 1, replacing like-main-without-bom.
    • Two texts rows now name T#! as the token.
  • Checks: cargo fmt --check and clippy -D warnings --all-targets --all-features (parser, wasm) pass, as does cargo doc with -Dwarnings. cargo test -p rustledger-parser, -p rustledger-lsp and -p rustledger-wasm all pass (1,700 tests, 0 failed).

How to test

cargo test -p rustledger-parser --test indented_tag_line_invalid_token
cargo test -p rustledger-parser
cargo build --release -p rustledger-loader --no-default-features --example parse_error_lines
python -I scripts/compat-parse-error-parity.py --rledger target/release/examples/parse_error_lines \
  --fuzz-dir <fuzz_parse + fuzz_green_eq_red at 9bc79f4f> --fuzz-commit 9bc79f4f35f52c50813164f43307e7d151d70d8c \
  --compat-dir tests/compatibility/files --check

Not changed (documented)

  • A lone capital glued to \r or to the end of the input, after an invalid token on an indented line, is not reported as its own invalid token ( h1 Z\r\n: bean-check 2, rledger 1). Tag lines holding it keep main's count.
  • A lone posting flag's unexpected EOL is reported on the flag's line, where beancount reports it on the next line.

Closes #2997

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 25, 2026 18:14
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 45.6ms ± 5.3ms 500.7ms ± 28.6ms 11.0x 43.4 MB / 51.0 MB
investment 60.2ms ± 1.0ms 1994.3ms ± 51.4ms 33.1x 47.0 MB / 59.0 MB
fifo 58.3ms ± 0.7ms 11818.6ms ± 118.4ms 202.8x 49.1 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 43.9ms ± 2.0ms 45.6ms ± 5.3ms +3.7% ✅ within noise 45.4 MB → 43.4 MB (-4.3%)
investment 60.3ms ± 1.3ms 60.2ms ± 1.0ms -0.2% ✅ within noise 47.0 MB → 47.0 MB (+0.1%)
fifo 58.5ms ± 0.9ms 58.3ms ± 0.7ms -0.3% ✅ within noise 48.9 MB → 49.1 MB (+0.5%)

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

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

…nvalid token, is one error

beancount's grammar reads a line of tags and links in a transaction's body
(posting_or_kv_list INDENT tags_links EOL), so on `  #tag h` bean-check
3.2.3's first error is the lexer's `Invalid token: 'h'`, and its recovery
discards the rest of the line. drop_errors_inside_a_line_error only took
nothing, a flag or a posting start as a readable prefix, so the line error
was reported beside the token's own. In a transaction node a prefix of tags
and links now counts as readable too; under any other directive the tag is
a syntax error first and both errors stay, as in beancount.

A line where a lone capital glued to what follows it (`Z\r`, `Z` at the end
of the input) comes after the token keeps both errors: beancount reports
that capital as one more invalid token, which the parser does not yet, so
main only matched there by coincidence.

Parse-error parity: 92,482 -> 92,545 inputs match, 0 regressions. BOM
combination matrix: 11,166 -> 11,172 matching. WASM cache v126; the loader
cache stays at v75 (parsed directives do not move).

Closes #2997

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/indented-tag-line-invalid-token branch from 7034d80 to 48d55cb Compare November 25, 2026 19:05
@robcohen
robcohen merged commit c4b0f71 into main Nov 25, 2026
62 of 63 checks passed
@robcohen
robcohen deleted the fix/indented-tag-line-invalid-token branch November 25, 2026 20:18
robcohen added a commit that referenced this pull request Nov 26, 2026
…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]>
robcohen added a commit that referenced this pull request Nov 26, 2026
…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]>
robcohen added a commit that referenced this pull request Nov 26, 2026
…ledger is as quiet as beancount after every failed line (#3035)

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]>
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): an indented tag or link line in a transaction followed by an invalid token is one error

1 participant