Skip to content

fix(parser): end a column-0 # comment before the CRLF that follows it - #2773

Merged
robcohen merged 3 commits into
mainfrom
fix/crlf-hash-comment
Oct 28, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/crlf-hash-comment

Conversation

@robcohen

@robcohen robcohen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

What changed

  • TokenIter's column-0 # comment arm (crates/rustledger-parser/src/logos_lexer.rs) now ends the comment before the one \r that the following Newline token (\r?\n) claims. Extra \r bytes before it, and a lone \r at EOF (no \n follows, so no newline token claims it), stay in the comment.
  • Loader CACHE_VERSION 43 -> 44 and WASM 58 -> 59 (LOADER_CACHE_VERSION_PIN follows; fixture pins updated). This is a parser OUTPUT change with no archived layout move: a file ending in a CRLF # comment parsed clean before the fix, so the CLI cached it with its last directive's span one byte past the end of the file, and the WASM cache also archives parse_errors, so a stale blob would keep serving P0012.
  • CHANGELOG entry under ### Fixed.

Why

The comment span ran to the \n, so on a CRLF line it included the \r the newline token also starts at. The two tokens overlapped by one byte, so the CST text and the lossless token stream came out one byte longer than the source, and the directive after the comment failed with P0012 top-level directive must start at column 0. rledger check reported a valid ledger as broken and rledger format refused to run, while bean-check (beancount 3.2.3) accepts the same file. Beancount's own lexer treats a column-0 # line as a comment (^[\*\:\#]/. skips the line) and \r as whitespace. ; comments were never affected because their regex (;[^\n\r]*) stops before \r.

How to test

cargo test -p rustledger-parser --test crlf_hash_comment and cargo test -p rustledger-parser --lib test_tokenize_lossless_hash_comment_ends_before_crlf. The tests cover:

  • the comment token's exact text and span: it ends where the Newline token starts (# note\r\n gives # note at 0..6 and the newline at 6..8; # note\r\r\n keeps one \r in the comment);
  • parse_structured(..).text() and the concatenated tokenize_lossless spans equal the source for # and ; comments followed by \n, \r\n, \r\r\n and \r\r\r\n;
  • parse reports no errors and both directives after a # comment with each of those endings (and ; with \n / \r\n), after a BOM, and after a pushtag line;
  • a # comment as the last line (with \r\n, with no ending, with a lone trailing \r), including that the last directive's span ends at the source length and the comment value has no \r;
  • try_format_source (what rledger format calls) formats a CRLF # file to the same output as the LF one.

All 7 tests fail with the fix disabled. Parser, loader and WASM suites: 1200 passed, 0 failed; workspace clippy, docs (including private items) and fmt clean.

Reviewed in depth before merge: a ~307k-input differential (base vs this branch) found 0 token overlaps left (base: 4,689), no new lossless gaps, no panics, and CRLF results identical to the LF versions; rledger check and format now agree with bean-check on every CRLF # case tried.

Not in this PR

Closes #2769

🤖 Generated with Claude Code

The comment token ran to the \n, so on a CRLF line it included the \r
that the \r?\n Newline token also starts at. The two tokens overlapped
by one byte: the CST text and lossless tokens came out one byte longer
than the source, and the directive after the comment was reported as
"top-level directive must start at column 0". bean-check accepts the
same file. ; comments were never affected (their regex stops before \r).

The comment now ends before the one \r the Newline token claims; extra
\r bytes before it, and a lone \r at EOF, stay in the comment.

Closes #2769

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

codecov Bot commented Oct 28, 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 Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 52.9ms ± 2.0ms 745.1ms ± 1.3ms 14.1x 34.0 MB / 51.1 MB
investment 77.8ms ± 2.0ms 2763.6ms ± 40.5ms 35.5x 43.8 MB / 59.0 MB
fifo 72.1ms ± 1.0ms 16120.5ms ± 482.1ms 223.5x 39.6 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 51.6ms ± 1.4ms 52.9ms ± 2.0ms +2.5% ✅ within noise 33.6 MB → 34.0 MB (+1.2%)
investment 78.0ms ± 1.9ms 77.8ms ± 2.0ms -0.2% ✅ within noise 43.8 MB → 43.8 MB (-0.1%)
fifo 72.3ms ± 0.6ms 72.1ms ± 1.0ms -0.2% ✅ within noise 39.3 MB → 39.6 MB (+0.7%)

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

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

Review follow-ups for the CRLF # comment fix:

- Loader CACHE_VERSION 44 and WASM 59 (pin follows): the fix changes
  parser output. A file ending in a CRLF # comment parsed clean before,
  so it was cached with its last directive span one byte past the end of
  the file; the WASM cache also archives parse errors and would keep
  serving P0012.
- CHANGELOG entry under Fixed.
- Tests: a lexer unit test pinning the comment text and span (it ends
  where the Newline token starts); the in-bounds EOF span and comment
  value; the BOM-prefix and post-pushtag cases; try_format_source (what
  rledger format calls) now formats a CRLF # file like the LF one. All
  fail with the fix reverted. Test names follow test_<function>_<scenario>.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen enabled auto-merge (squash) October 28, 2026 14:46
@robcohen
robcohen merged commit cea8250 into main Oct 28, 2026
65 of 66 checks passed
@robcohen
robcohen deleted the fix/crlf-hash-comment branch October 28, 2026 15:00
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.

fix(parser): a column-0 # comment with CRLF line endings breaks the next directive

1 participant