Repository navigation
fix(parser): ignore column-0 !, &, ? and : lines and report bare markers, as beancount does - #2877
Merged
Merged
Conversation
…ers, as beancount does
Beancount's lexer ignores a whole line that starts at column 0 with `*`,
`:`, `#` or a flag character (`^[\*\:\#]/.` and `^{FLAGS}/.`, with
`FLAGS [!&#?%]`). rledger reported a `!`, `&`, `?` or `:` line as P0012,
so check, query and format refused files bean-check accepts (an org-mode
`:PROPERTIES:` drawer in the compat corpus, for one). Both rules need one
more character on the line (flex's `.`, which a `\r` satisfies), so a bare
marker is a token and an error there; rledger read a bare `#`, `%` or `*`
line as a comment.
- One rule, `logos_lexer::beancount_ignores_line`, decides which column-0
lines beancount ignores. The lexer's column-0 arms use it: a `!`, `&`,
`?` or `:` line is one comment token, like a `#` comment, and is never
lexed (a `"` there opens no string); a bare `#` or `%` stays its own
token. `is_section_marker` (the `*` heading) and `previous_line_is_a_gap`
(an indented line after an ignored line is `unexpected INDENT`) call it
instead of re-deriving it.
- Formatter: the line-ending state machine treats a column-0 `!`, `&`, `?`
or `:` as a comment, with a drift-guard fixture.
- Stray-character matrix: the 192 pinned column-0 rows now match beancount
and are checked; the pin is gone. The TSV is unchanged.
- Loader CACHE_VERSION 58, WASM 74.
- Tests (checked against bean-check 3.2.3), CHANGELOG.
Closes #2779
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…ixtures #2779 moved Review round 1 and CI on the #2779 change: - Formatter: a lone column-0 marker that beancount ignores only because of the whitespace or `\r` after it (`#\r\n`, `*\r\n`, `* `, `*\t`) became a bare-marker error after `rledger format`, which trimmed the whitespace or folded the CRLF away. Such a line now keeps one space after the marker, in comment emission (`push_comment_text`, all five sites), `*` headings in `emit_error_node`, and `crlf_to_lf_outside_strings`. bean-format folds the `\r` away (Python text mode), so it is not matched here. A marker that was already bare stays an error. Fixtures cover every marker with `\r\n`, a space and a tab, mid-file and at EOF, through format_source, try_format_source, a second pass and range formatting. - The formatter's `is_flag_line_comment` is documented as a deliberate char-level mirror of `beancount_ignores_line`, at both sites. - LSP: the code_lens parse-error fixture used a column-0 `!!!` line, now a comment as in beancount; it uses `@@@` instead. - Corpus baselines: the parser-output and CST rows for parser-lima/examples_data_full.beancount (its `:PROPERTIES:` drawer is now comment trivia). Only that row was taken from the regenerated manifests; the local corpus differs from CI's on other files. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # CHANGELOG.md
… fold CRLF in linear time Review round 2 on the #2779 change: - LSP: format-on-save on a file with parse errors falls back to `surface_cleanup_edits`, whose `clean_line` trimmed `# ` to `#`, a bare-marker error. It now keeps one space when the trim turns a line beancount ignores into one it does not, by the shared rule `logos_lexer::beancount_ignores_line`, now public for this. Tested for every marker with a space, a tab and mixed whitespace, LF and CRLF. - `crlf_to_lf_outside_strings` found the current output line with an `rfind` over the whole output for every `\r`, quadratic on a long line of stray `\r`s. It now keeps the line start as a running index, with a counter test on the reviewer's input. - The lexer's whole-line cut (`line_end_before_newline`) ends before the stray-`\r` run (`[ \t\r]` from a `\r` on) at the line end, which is the trivia `crlf_to_lf_outside_strings` drops, so `format_source` and `try_format_source` agree on `#\r \r\n`, `# c\r \r\n` and `#\r ` at EOF. - Note at `crlf_to_lf_outside_strings` and in the CHANGELOG: stricter than bean-format, which folds `#\r\n` to `#\n`, a line bean-check rejects. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…eep a BOM-led marker line ignored in LSP cleanup Review round 3 on the #2779 change: - `crlf_to_lf_lone_marker_check_is_linear` counted the bytes pushed per step, which cannot exceed one per input char, so restoring the old `rfind` still passed. The line-start lookup is now `OutLine::start_in`, which scans only what was pushed since its last call and counts what it scans. With the `rfind` restored there, the test counts 25,600,320,000 bytes on a 320,003-byte input and fails. - LSP `clean_line`: a file's first line `BOM + "# "` was trimmed to `BOM + "#"`, a bare-marker error. The BOM is left out of the `beancount_ignores_line` check and kept in the output, with a test for every marker. Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
enabled auto-merge (squash)
November 6, 2026 22:03
robcohen
disabled auto-merge
November 6, 2026 22:04
This was referenced Nov 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Beancount's lexer ignores a whole line that starts at column 0 with
*,:,#or a flag character (^[\*\:\#]/.and^{FLAGS}/., withFLAGS [!&#?%]). rledger now does the same:!,&,?or:line is a comment. It was a P0012 error, sorledger check,queryandformatrefused files that bean-check accepts. The line becomes one comment token, like a column-0#comment, and it is never lexed, so a"on it opens no string..), so bean-check reportsunexpected FLAG/HASH/ASTERISK/COLONthere. rledger used to read a bare#,%or*line as a comment. A\rcounts as the extra character, so a bare marker on a CRLF line is still ignored, as in beancount.unexpected INDENT, as it is after any column-0 comment.Implementation:
logos_lexer::beancount_ignores_lineis now the one rule for which column-0 lines beancount ignores. The lexer's column-0#/!/&/?/:/%arms use it.convert::is_section_marker(the*heading) andprevious_line_is_a_gapused to re-derive the rule inline; they now call it.In the formatter, the line-ending state machine treats a column-0
!,&,?or:as a comment. There is a new drift-guard fixture (flag_line_quote_then_string).Stray-character matrix: the 192 pinned
known_column_0_divergencerows now match beancount and are checked. The pin is removed. The TSV is unchanged, because beancount's verdicts did not move.LSP: the
balance_lens_neutral_when_parse_errors_skip_validationfixture used a column-0!!! syntax garbageline as its parse error. That line is now a comment, as in bean-check (which reports nothing on it), so the fixture uses@@@ syntax garbage, which both tools reject.Parse caches: loader
CACHE_VERSION58 (withFIXTURE_VERSIONto match), WASMCACHE_VERSION74 (withLOADER_CACHE_VERSION_PIN58). Both are named in the CHANGELOG.Formatter: a lone marker that beancount ignores only because of the whitespace or
\rafter it (#\r\n,!\r\n,%\r\n,*\r\n,*,*\t) keeps one space after the marker. Without it, trimming the line or folding its CRLF would leave a bare-marker error. This covers comment tokens (push_comment_text, at all five emission sites),*headings inemit_error_node, andcrlf_to_lf_outside_strings, whichformat_sourceruns before parsing. bean-format keeps such lines' trailing whitespace but folds the\raway in Python's text mode, which breaks them, so this keeps a space instead of matching bean-format. A marker that was already bare stays bare and stays an error.#2872 rows covered
!x,&x,:xcolumn-0 lines (also with CRLF): bean-check3 3, rledger was2 3, now3 3.#,%or*line: bean-check also reports the marker on its own line, rledger did not, now it does (also for bare!,&,?,:).The other #2872 rows are not touched here.
Known difference (for #2872, not fixed here): rledger reports every line of a run of consecutive bare markers (
!\n#\n*\n: lines 2, 3, 4), but bean-check's error recovery reports only the first (line 2).Why
Issue #2779: the decision was to match beancount.
How to test
Verification done:
x,x,"qand bare, LF and CRLF, at EOF, inside a transaction, inside a multi-line string, and followed by an indented bad date. Both report parse errors on the same lines, with the same counts. Three kinds of probe still differ, and none of them comes from this change: they behave the same with a;comment in place of the marker, or they are another Parse-error recovery differences from bean-check around invalid dates and column-0 markers #2872 row. They are a comment line between a transaction header and its postings, a#a btag line, and a file-leading BOM.parser-lima/examples_data_full.beancountloses its three P0012 errors on lines 5-7, an org-mode:PROPERTIES:/:END:drawer, and bean-check reports nothing there either. That file is also the one baseline change: its rows intests/baselines/parser-corpus.manifestandcst-corpus.manifestare updated (source hash unchanged; the parser-output hash, and the CST token-sequence and node-shape hashes, move because the three drawer lines are now comment trivia instead of error nodes). The rows were taken from a run ofscripts/regen-corpus-baselines.sh, but only this row: the local corpus differs from CI's frozen copy on about 20 other files, so the rest of the regenerated manifest was discarded.cargo test --workspace --all-features --no-fail-fast(after review round 1; the round-1 clippy-only refactor was then re-tested on parser, lsp, the CLI, loader, wasm and ffi-component),cargo clippy --all-features --all-targets -- -D warnings,RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps,cargo fmt --check.Review round 1
code_lensfixture used!!! syntax garbage, which is now a comment. It now uses@@@ syntax garbage. Both corpus manifests are updated forparser-lima/examples_data_full.beancount. Only that row is taken from the regenerated output, and the parser-output hash now equals the one CI reported (076e637db4fd727d). The earlier claim that the CST manifest does not move was wrong and is corrected above.rledger formatno longer turns an ignored lone-marker line into a bare-marker error (see the Formatter bullet). New fixtures cover#,!,&,?,:,%and*followed by\r\n, a space or a tab, plus#\r\r\n,* \tand*\r \r\n. Each one is tested mid-file, at EOF, and at EOF without a final newline. The tests check parse preservation and idempotence forformat_sourceandtry_format_source, and parse preservation for whole-file range formatting withformat_parsed_range, the LSP path; range formatting refuses anERROR_NODE*heading, so it cannot break one.crlf_to_lf_keeps_the_lexer_line_structuregained the CRLF cases. With the CLI, all 14<marker>\r\nprobes (mid-file and at EOF),* \nand*\t\ngive 0 errors after onerledger format -iand after a second one.is_flag_line_commentcannot callbeancount_ignores_line:advance_source_stateis aconst fnover one char and a peek. The rule's doc now lists it as a deliberate char-level mirror, with a cross-reference at both sites. The existingsource_state_classification_agrees_with_lexerguard pins agreement.Review round 2
surface_cleanup_edits, and itsclean_linetrimmed#to#. It now keeps one space when the trim would turn a line beancount ignores into one it does not. The check usesrustledger_parser::logos_lexer::beancount_ignores_line, which is now public for this; its doc lists its users. A new testsurface_cleanup_keeps_whitespace_marker_lines_ignoredcovers all 7 markers with a space, a tab and mixed whitespace, LF and CRLF. The error count stays 1, and a bare marker is left alone. Other formatting paths were checked: WASM, FFI, the CLI and the MCP format tool all go through the parser formatter, and none has a trailing-whitespace stripper of its own.crlf_to_lf_outside_stringsfound the current output line with anrfindover the whole output for every\r, which made a long line of stray\rs quadratic. It now keeps the line start as a running index. A counter test,crlf_to_lf_lone_marker_check_is_linear, runs the reviewer's;+ 160ka+ 160k\rinput and a#\r+ 160k\rinput.format_sourceand the non-folding paths now agree on#\r \r\n,# c\r \r\n,#\t\r\n,* c\r \r\nand on#\r/# c\r \rat EOF, so the byte-identity claim holds. The fix is in the lexer: its whole-line cut,line_end_before_newline, now ends before the stray-\rrun at the line end (a[ \t\r]run from a\ron), and that run is exactly what the CRLF fold drops. Spaces and tabs before the run's first\rstay in the comment, as before. These cases were added tocrlf_to_lf_keeps_the_lexer_line_structure, which assertstry_format_source == format_source. The bean-format deviation is noted in the CHANGELOG and with a "stricter than bean-format" comment atcrlf_to_lf_outside_strings.Verification for this round:
cargo test --workspace --all-features --no-fail-fastpasses.-D warnings, rustdoc-Dwarningsandcargo fmt --checkare clean.rledger format --no-ledgeron 1,113 in-tree and compat ledgers: the output is byte-identical on all of them, and the exit codes match (the run listed 1,136 paths, because 23 in-tree compat fixtures were listed once from each checkout; of those runs, 1,042 formatted and 94 were refused for parse errors, on both).Review round 3
crlf_to_lf_lone_marker_check_is_linearcould not fail: it counted the bytes pushed per step, which is at most one per input char. The line-start lookup is nowOutLine::start_in, which scans only what was pushed since its last call and counts the bytes it scans. I checked that the test can now fail: with the oldrfindput back insidestart_in, it counts 25,600,320,000 bytes on the 320,003-byte input and fails. With the fix restored, it passes.clean_line: a first line of BOM +#(with a parse error elsewhere) was trimmed to BOM +#, a bare-marker error. The BOM is now left out of thebeancount_ignores_linecheck and kept in the output. The new testsurface_cleanup_keeps_whitespace_marker_line_after_bom_ignoredcovers every marker, with a space and with a tab.Verification: parser and LSP tests, clippy
-D warnings, rustdoc-Dwarningsandcargo fmt --checkall pass.Closes #2779
🤖 Generated with Claude Code