Skip to content

fix(parser): report a syntax error at a multi-line string on the line the string ends - #3033

Merged
robcohen merged 2 commits into
mainfrom
fix/multiline-string-error-line
Nov 25, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/multiline-string-error-line

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

A syntax error whose offending token is a string that runs over line ends is now reported on the line the string ends, as bean-check 3.2.3 reports it. This holds in rledger check (text and JSON), the LSP, WASM and the FFI.

Why

bison reports a syntax error at the lexer's current line, which is where the lookahead token ends. For a multi-line STRING that is the string's last line. The parser started the error at the token, so LineIndex put it on the string's first line.

How

  • report_at_multi_line_string_ends is a new last error pass in convert.rs. It moves the error's span start to the first byte of the string's last line and keeps the span's end. Every surface maps that one span, so they all agree, and the message still names the string.
  • The offending token comes from grammar_fails_at. This rule used to live inline in report_invalid_dates and is now factored out and shared by both passes. On an indented line after a gap (a blank line, or the top of the file), beancount fails at the INDENT, so the error stays on the line the string starts.
  • When a directive is dropped, its whole-node error uses its header's failure point (grammar_failures), e.g. note "a\nb".
  • Errors raised by beancount's builder are not moved. They stay on the statement's first line:
    • an invalid booking method;
    • too many strings on a transaction header. Its range now starts at the first string and runs to the third (txn_header::TOO_MANY_STRINGS).
  • Related multi-line cases fixed in the same change:
    • quiet_after_failed_line_starts: a column-0 failed line that holds a multi-line string now ends where the string does. The lines after it are quiet, as after any other failed line ("a\nb" then kk: 5).
    • report_invalid_dates: a bad date on a string's last line counts as being on the header's statement line, so event "a\nb" 2024-02-30 is one error.

Results (bean-check 3.2.3)

Input beancount main this PR
P"a\nb"\nS 3 4 2 4 3 4
P"a\nb"\n 3 2 3
P"a\nb\nc"\nS 4 5 2 5 4 5
Poption "title" "a\nb" "c\nd"\nS 4 5 3 5 4 5
  • Parse-error parity: 92,545 → 92,606 inputs match, 0 regressions. The manifest is updated.
  • Generated differential: 9,896 inputs, with 5 string shapes (LF, CRLF, "\n", blank line inside) in 77 positions, 4 prefixes (including inside a recovery window) and 6 suffixes. Result: 4,617 gained, 245 closer, 556 sideways, 0 regressed, 0 further.
  • Every remaining mismatch in the differential also mismatches with the strings collapsed to one line, so none of them is specific to multi-line strings. The sideways rows are those classes:
  • The BOM combination matrix is unchanged.
  • WASM CACHE_VERSION 127 → 128. The loader cache stays at 76, because parsed directives do not move.

Tests

  • crates/rustledger-parser/tests/multi_line_string_error_line.rs: the issue rows plus extra cases. They cover every position, the gap and indent cases, builder errors, the invalid date, quiet windows, CRLF, BOM, unclosed strings, the end-of-line difference and the span. Each count was checked with bean-check 3.2.3, and the green and red converters must agree.
  • crates/rustledger-lsp/tests/multi_line_string_error_line.rs: the LSP diagnostic line (UTF-8 and UTF-16) must equal the LineIndex line that rledger check prints, and both must equal beancount's.
  • crates/rustledger/tests/multi_line_string_error_line_test.rs: rledger check --format json and the text header report lines 4 and 5.

How to test

cargo test -p rustledger-parser --test multi_line_string_error_line
cargo test -p rustledger-lsp --test multi_line_string_error_line
cargo test -p rustledger --test multi_line_string_error_line_test
printf '2024-01-01 open Assets:A\n"a\nb"\n' > x.beancount && rledger check x.beancount   # line 3, as bean-check

Closes #2994

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 25, 2026 22:07
@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

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 42.8ms ± 0.7ms 515.8ms ± 1.5ms 12.1x 43.0 MB / 51.2 MB
investment 62.0ms ± 0.4ms 2017.9ms ± 9.7ms 32.6x 47.0 MB / 59.2 MB
fifo 61.5ms ± 0.4ms 11799.7ms ± 27.6ms 191.7x 49.1 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 42.3ms ± 0.4ms 42.8ms ± 0.7ms +1.1% ✅ within noise 42.9 MB → 43.0 MB (+0.2%)
investment 62.0ms ± 0.6ms 62.0ms ± 0.4ms -0.1% ✅ within noise 47.2 MB → 47.0 MB (-0.3%)
fifo 61.5ms ± 0.6ms 61.5ms ± 0.4ms +0.0% ✅ within noise 49.2 MB → 49.1 MB (-0.3%)

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

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 and others added 2 commits November 25, 2026 16:58
… the string ends

bison reports a syntax error where its lookahead token ends, so for a
string that runs over line ends bean-check 3.2.3 names the string's last
line. The parser started such an error at the string, so `rledger check`,
the LSP, WASM and the FFI all put it on the string's first line.

report_at_multi_line_string_ends, the last error pass, now moves the
span's start to the string's last line (the end is kept, the message still
names the string), so every surface reports beancount's line from the one
span. The offending token is found by grammar_fails_at, the rule
report_invalid_dates already used (now shared): an indented line after a
gap fails at its indent and stays. A dropped directive's whole-node error
uses its header's failure point (`note "a\nb"`). Builder errors stay on
the statement's first line: an invalid booking method, and too many
strings on a transaction header, whose range now starts at the first
string.

Related multi-line cases fixed with it:
- quiet_after_failed_line_starts: a column-0 failed line holding a
  multi-line string ends where the string does, so the lines after it are
  quiet as after any failed line;
- report_invalid_dates: a date on a string's last line is on the header's
  statement line, so `event "a\nb" 2024-02-30` is one error.

Parse-error parity: 92,545 -> 92,606 inputs match, 0 regressions. A
generated differential of 9,896 multi-line-string inputs: 4,617 gained,
245 closer, 0 regressed, 0 further. BOM matrix unchanged. WASM cache
127 -> 128; the loader cache stays at 76 (parsed directives do not move).

Closes #2994

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…on a transaction header

The error now starts at the first string, on the transaction's first line
(#2994), so the two parser-lima files with three header strings hash
differently.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/multiline-string-error-line branch from cf6462d to 0be66ab Compare November 25, 2026 23:01
@robcohen
robcohen merged commit 37b335e into main Nov 25, 2026
62 of 63 checks passed
@robcohen
robcohen deleted the fix/multiline-string-error-line branch November 25, 2026 23:21
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): report an error at a multi-line string on the line the string ends, as beancount does

1 participant