Skip to content

fix(format): read {#! and {#+ as the lexer does in the line-ending pass - #2892

Merged
robcohen merged 3 commits into
mainfrom
fix/source-state-lbrace-hash
Nov 7, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/source-state-lbrace-hash

Conversation

@robcohen

@robcohen robcohen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

What changed

Issue #2780 has two parts. One was already done on main; this PR fixes the other and widens the drift guard.

  1. The % gap in the drift guard was already closed on main. The lexer-side classifier in source_state_classification_agrees_with_lexer maps Token::PercentComment to InComment. IDEMPOTENCE_MATRIX's percent_flag_and_comment fixture has a column-0 % c "q line, and I checked that the guard can trip: with PercentComment removed from the classifier, it fails on that fixture.
  2. {#! and {#+ are fixed here. The lexer reads {# as one LBraceHash token. The formatter's line-ending state machine (advance_source_state) instead saw the #! / #+ after the { and opened a comment. A " later on the line was then taken for comment text, so a string's own line endings could be rewritten on a CRLF round trip.
    • Now a # right after a { that starts a token opens nothing.
    • The lexer pairs {{ greedily from the left ({{ is one token, and {{{# is {{ then {#), so the condition is an odd run of { in code.
    • next_lbrace_odd tracks that run. It is one definition, used by all four walkers: crlf_to_lf_outside_strings, lf_to_crlf_outside_strings, cr_outside_strings_present, and the test classifier.
  3. The drift guard now also runs on generated inputs, as the issue suggested. Its per-byte comparison is now a helper, assert_source_state_agrees_with_lexer, which runs on:
    • every IDEMPOTENCE_MATRIX fixture, as before;
    • SOURCE_STATE_EXTRA, edge inputs that are not formatter fixtures: the issue's "\n{#! \n" and "{#!#\"2\u{feff}\n%\"", plus {#+, {{#!, {{{#!, and a % comment with a ";
    • every string of up to five characters over the thirteen characters the two classifiers treat specially (\r { # ! + % ; " \n space a * \), about 2 s in a debug build.
    • Only inputs with a mid-line \r (more of its line after it) are skipped. Such a \r closes a comment in the state machine but stays in the comment for the lexer, which is a documented difference. A \r before its line ending is checked.

Why

Issue #2780.

How to test

cargo test -p rustledger-parser --all-features --lib source_state_classification

Verification

  • The new checks can fail. With the {# rule disabled, both the extra-inputs check and the generated-input check fail (the first disagreements are at lbrace_hash_bang byte 4 and generated {#! byte 3).
  • Corpus: rledger format --no-ledger output is byte-identical between origin/main and this branch on all 1,113 in-tree and compat ledgers, and so are the exit codes. No corpus file contains {#!.
  • No parse-cache bump: parser output does not change; only the formatter's line-ending helpers do.
  • Checks: cargo test --workspace --all-features --no-fail-fast, STRICT_BASELINE=1 corpus and CST baselines, cargo clippy --all-features --all-targets -- -D warnings, RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps and cargo fmt --check all pass.

Review round 1

  • Three em-dashes in comment text that I re-indented into assert_source_state_agrees_with_lexer are replaced with plain punctuation.
  • The generated-input check now includes \r in its alphabet and skips only inputs with a mid-line \r.
  • Merged origin/main.
  • Parser and LSP tests, clippy -D warnings and cargo fmt --check pass.

Closes #2780

🤖 Generated with Claude Code

…g pass

The lexer reads `{#` as one `LBraceHash` token, but the formatter's
line-ending state machine (`advance_source_state`) saw the `#!` / `#+`
after the `{` and opened a comment, so a `"` later on the line was taken
for comment text and a string's own line endings could be rewritten on a
CRLF round trip. A `#` right after a `{` that starts a token now opens
nothing. The lexer pairs `{{` greedily from the left, so that is an odd
run of `{` in code; `next_lbrace_odd` tracks it, one definition shared by
the four walkers.

The drift guard (`source_state_classification_agrees_with_lexer`) is now
a helper run on the formatter fixtures, on edge inputs that are not
fixtures (`SOURCE_STATE_EXTRA`: the issue's two inputs, `{#+`, `{{#!`,
`{{{#!`, a `%` comment with a `"`), and on every string of up to five
characters over the twelve characters the two classifiers treat
specially (271,452 inputs, 4 s in a debug build). Both new checks fail
without the fix. The `%` half of the issue was already done: the
classifier maps `PercentComment`, and the `percent_flag_and_comment`
fixture makes it trip.

Closes #2780

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

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 50.8ms ± 1.1ms 821.2ms ± 18.3ms 16.2x 42.9 MB / 51.1 MB
investment 78.2ms ± 2.7ms 2928.4ms ± 26.7ms 37.4x 58.9 MB / 59.0 MB
fifo 73.3ms ± 1.4ms 14713.4ms ± 135.0ms 200.9x 54.9 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 50.6ms ± 1.1ms 50.8ms ± 1.1ms +0.6% ✅ within noise 42.9 MB → 42.9 MB (-0.0%)
investment 77.2ms ± 1.2ms 78.2ms ± 2.7ms +1.3% ✅ within noise 58.1 MB → 58.9 MB (+1.2%)
fifo 72.8ms ± 0.7ms 73.3ms ± 1.4ms +0.7% ✅ within noise 54.9 MB → 54.9 MB (+0.0%)

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

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.02913% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/format.rs 99.02% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 7, 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 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

robcohen and others added 2 commits November 7, 2026 12:24
…puts too

Review round 1 on #2780: three em-dashes in comment text re-indented
into `assert_source_state_agrees_with_lexer` are replaced with plain
punctuation. The generated-input check now includes `\r` in its alphabet
and skips only an input with a mid-line `\r` (more of its line after it),
the documented difference; a `\r` before its line ending is checked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit b33a43f into main Nov 7, 2026
61 of 62 checks passed
@robcohen
robcohen deleted the fix/source-state-lbrace-hash branch November 7, 2026 19:16
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.

test(format): the line-ending drift guard skips % comments; {#! and {#+ are misclassified

1 participant