Repository navigation
fix(parser): read a mid-line #! or #+ as a # and the token after it, as beancount does - #2974
Merged
Merged
Conversation
…r it, as beancount does A `#!` or `#+` that does not start its line was one comment-like token (`Shebang` / `EmacsDirective`) running to the end of the line. beancount skips a `#` line only at column 0 (`^[\*\:\#]/.`); anywhere else it lexes a `#` (HASH), then a `!` (FLAG) or a `+` (PLUS), and the rest of the line as usual. So every invalid token after a mid-line `#!` went unreported (`2024-01-02 * "x" #!cd` is two errors in bean-check 3.2.3, one here), and an indented `#+STARTUP` or `#!` line, under a directive or a posting, was skipped as a comment where beancount reports it. - Lexer: a `Shebang` / `EmacsDirective` token not at column 0 is rewritten to `Hash`, and the lexer resumes right after the `#`, as the mid-line `%` rewrite does. `error_later_in_run` lexes on past a `#` the same way, so a lone capital glued to `#!x` still makes the run one invalid token. - CST recovery: the mid-line SHEBANG / EMACS_DIRECTIVE special cases are gone (the transaction header's `#!` defect, `indented_comment_check`, the two-character defect range), since those tokens now occur at column 0 only. A header defect at a `#` keeps the "only `;` starts a comment in the middle of a line" hint. Parse-error parity (pinned fuzz corpus 9bc79f4): 78,500 of 111,323 inputs match beancount, from 78,478: 71 newly match, 49 regress. 48 of the 49 count each differing line as beancount does when the line stands alone (beancount's recovery after an earlier error line, #2872), and the other is a lone capital glued to tokens that all lex (`X!!#+++02`). `tracked` and `compat` are unchanged. The BOM combination matrix keeps its beancount and main columns; its generated lists are updated with `BOM_MATRIX_UPDATE=1 BOM_MATRIX_RELABEL=1`, the hand-kept worse-than-main list drops 341 lines no longer further from beancount than main and gains 51 that a hidden `#!` / `#+` used to mask, and a line main counted closer only because it did not read past a mid-line `#!` / `#+` is verified as such (`main_read_less`). Caches: loader v74 and WASM v112. A ledger with an indented `#+` line loaded with no error before, so an error-free v73 loader blob of it is stale too. (WASM v111 is held by #2973.) Refs #2928 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?
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Review round 1 of #2974. - Removing `indented_comment_check` lost the message that named an indented `#!` / `#+`: the line's error became the generic `unexpected input` at the indentation. `name_mid_line_hash_comments`, a shared post-pass beside `name_misspelled_booleans`, finds each mid-line marker with the lexer itself (a `#` followed right away by its `!` or `+`, so never one in a string or a comment) and rewrites the line's first error at or before the `#` to point at the two characters and say that only `;` starts a comment that is indented or in the middle of a line. A defect already reported at the `#` (a posting's, a directive header's) is widened to both characters with the same hint. Only messages and spans move, on the same line, so every count stays beancount's. The hint now also reaches a top-level indented line and a directive's metadata, which main read as comments. Pinned by `tests/mid_line_hash_bang.rs` (exact message and span under a posting, at the top level, after a column-0 `#+TITLE` line, in an `open`'s metadata, and on posting and header lines). - The `Shebang` / `EmacsDirective` arms of `invalid_runs` are gone: its text starts with a blank, so a `#!` there is mid-line. The `is_comment_token` rustdoc no longer calls an indented `#+STARTUP` comment trivia. - BOM matrix harness: `hash_bang_cut` finds the markers with the lexer, so it no longer cuts inside a string or a comment, and `main_read_less` also requires the cut to move the line's count; its doc says it accepts a line whatever its cause. The matrix test passes with no list change. - CHANGELOG: the entry names the restored message and the cache bumps. Refs #2928 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… marker leads the line Review round 2 of #2974. `name_mid_line_hash_comments` renamed the first generic `unexpected input` anywhere on the line before the `#`, so an earlier real error on the line (`$x` in ` Assets:A 1 USD $x #+y`, `open Assets:A ÜÜ #!x`, a stray `\r` before `#!x`, a line led by `$x`, `true`, `k: true` or `é`) was moved onto the marker and its own report vanished, and the error list could leave its start order. Now the line's error is renamed only when the marker leads its line (after its indentation); otherwise only a defect reported at the `#` itself is widened. Only blanks lie between the line start and a leading marker, so the list stays sorted. Tests in `tests/mid_line_hash_bang.rs`: an earlier bad token keeps its own error over its span (posting, `open` header, a line led by `$x`, metadata), a stray `\r` before `#!x` keeps its error, a `#!` inside a string renames nothing, errors stay sorted by start, and the `#+` of `<BOM> #+x` is bytes 5..7. Per-line counts match bean-check 3.2.3 on every repro. Refs #2928 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 14, 2026
…s an invalid token (#2975) A transaction whose posting holds an invalid token is kept under error recovery (#2932) with what recovery could read of it, so a posting's price or cost after the token is gone (`1 HOOL <BOM>{2 USD}`, `1 HOOL \x11@ 2 USD`, `1 HOOL $x{2 USD}`). The transaction then did not balance, and every surface reported two `E3001` beside the token's parse error, where bean-check 3.2.3 reports the token alone: beancount's grammar never builds such a transaction. The late balance check now asks, only once it has found a residual, whether the transaction holds an invalid token, by the parser's own rule: `rustledger_parser::transaction_holds_invalid_token` parses the transaction's own text (its directive span) and applies `holds_an_invalid_token`, the rule the duplicate-metadata check already skips such a transaction by. The text comes through `SourceLines`, which every surface already hands the validator: `SourceLines::with_text` / `text`, filled by `rustledger_loader::source_lines_overlaid` from the source map and from an editor's buffers (whose text is now kept), and by the new `source_lines_of_text` for an LSP buffer validated outside a ledger. Without the text (dropped, or a synthesized transaction) the residual is reported as before. A ledger that balances never reads its source here. The parse output is unchanged. Pinned by a surface-agreement case (CLI, loader, WASM, FFI, LSP) for `{`, `@`, `@@`, BOM-led, BOM-glued, control character and invalid-run shapes beside a transaction that really does not balance, by `test_transaction_holds_invalid_token_reads_the_parsers_rule`, and by a loader test of the text access. Caches: WASM v113 (its payloads archive validation findings; v111 is held by #2973 and v112 by #2974). Loader v73, unchanged: every affected source has a parse error, and that cache is saved only for an error-free load. Closes #2954 Co-authored-by: Claude Opus 5.5 <[email protected]>
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
Part 2 of #2928 (part 1 was #2931). A
#!or#+that does not start its line is now a#and the token after it, as in beancount, instead of one comment-like token running to the end of the line.Lexer (
logos_lexer.rs):Shebang/EmacsDirectivetoken not at column 0 (at_line_start && span.start == last_newline_endis false) is rewritten toHash, and the lexer resumes right after the#, exactly as the mid-line%rewrite does. The!then lexes as a FLAG, the+as a PLUS, and the rest of the line as usual. An indented one still gets itsIndent.error_later_in_runlexes on past a#the same way, so a lone capital glued to#!xstill makes its run one invalid token (A#!x, as in beancount).#!/#+line is still one comment token, as beancount skips it (^[\*\:\#]/.).CST recovery: the mid-line SHEBANG / EMACS_DIRECTIVE special cases are removed, since those tokens now only occur at column 0:
#!/#+defect intxn_header::first_header_defect;indented_comment_check(and its helperflatten_green_matching) inconvert.rs;directive_header. A header defect at a mid-line#keeps the "Only;starts a comment in the middle of a line" hint (defect_messagenow namesHASHinstead of the two removed kinds).The formatter's string scan already runs the lexer (
StringLiterals), so it follows the new lexing; two stale comments there are updated.Diagnostics (
name_mid_line_hash_comments, round 1): the line's error names the#!/#+. It is a shared post-pass besidename_misspelled_booleans. It finds each mid-line marker with the lexer itself. Where the marker leads its line (after its indentation), it rewrites the line's first error at or before the#to point at the two characters, withunexpected "#+": only;starts a comment that is indented or in the middle of a line;#+starts one only at the beginning of a line. A defect already reported at the#itself (a posting's or a directive header's) is widened to both characters and keeps the same hint. After another token on the line, an error before the#is that token's and is left alone (round 2). Only messages and spans move, on the same line, so no count changes. This replaces the oldindented_comment_checkmessage, which named the marker only inside a transaction; it now also reaches a top-level indented line and a directive's metadata, which main read as comments. Pinned with exact message and span intests/mid_line_hash_bang.rs.Why
beancount's lexer skips a
#line only at column 0. Anywhere else it reads#as HASH and!/+as FLAG / PLUS, and keeps lexing, so every invalid token later on the line is its ownInvalid tokenerror, even after the grammar has given up. rledger read the whole rest of the line as one token, so:#!/#+went unreported;#+STARTUP/#!line was a comment, where beancount reports it.Before / after (bean-check 3.2.3; parse errors per line)
2024-01-02 * "x" #!cd2024-01-02 * "x" #+cd2024-01-02 * "x" #!c<BOM>d2024-01-01 open Assets:A #!xAssets:A 1 USD #!x(posting)#!xunder a transaction, after its postings#+min anopen's metadata#+STARTUP: showall(indented, top level)2024-01-01 note Assets:A "n" #!x $y2024-01-02 * "p" #! \x112024-01-02 "x" #+ y(no flag)2024-01-02 * "x" #!,#+,#!"y",#! ; c,#!/7#! note,#+ notein a transaction#!x/#+ylinesIn an
open's metadata, the metadata line after the#+mline is then one error more than in beancount, as it already is on main after any other invalid indented line ($m); that is beancount's recovery after an error line (#2872).One field changes with the count:
2024-01-02 * "p" #!xnow holds an invalid token (x) after its lone string, so the #2932 rule keepspas the payee with an empty narration, as it does before any other invalid token (test_parse_invalid_token_after_a_mid_line_hash_bang_keeps_the_payee). Error-free ledgers are unaffected except for indented#!/#+lines, which are now parse errors as in beancount.Parse-error parity
compat-parse-error-parity.pyat the pinned fuzz commit9bc79f4f, manifest updated with--update:fuzz, 9matrix); 159 mismatches change category (allfuzz); 49 regress (allfuzz).trackedandcompatare unchanged.2024-01-02 *header when indented). Corrected in review round 1:#!/#+happened to offset by hiding the rest of its line. One of them,7ce689fbe0fa(regressed linepoptag#+a:...), also ends with a BOM inside an invalid run on an indented line (x<BOM>: bean-check 2, rledger 1 on main and here), a pre-existing difference the review pointed at; its regressed line itself counts as beancount alone.\r, which rledger reports on purpose and beancount takes as a blank (fix(parser): a stray \r before the line ending is a parse error; bean-check accepts it #2774,stray_cr_trivia_end). In 7 the\rstands right before a#!(k\r#!x: bean-check 2, main 2, here 3), in 4 it comes after a#+on the line ({ #+.a\r}), and in 1 the line has no marker, but main's count there matched beancount only through a#!elsewhere in the input. In each, main's#!/#+comment hid a token and so offset the\rerror.2615-2-3!!X!!#+++02(bean-check 2, here 1, main 2): the rest of the run all lexes, so rledger keeps the capital as a token.A@#!!@"\x12...(e29ba079a13b): the run holds a"whose string runs onto the next lines, whicherror_later_in_runcounts as a token that lexes, so the quote opens a multi-line string that swallows three lines that bean-check reports. That second one is pre-existing (A@"x\nfoo": bean-check 1 error on each line, main and here only line 1), and is now compat(parser): a lone capital glued to a quote opens a multi-line string where beancount reads one invalid token #2977.BOM combination matrix
bom_combination_matrix.tsvis unchanged: it holds beancount's and main's counts, neither of which this PR moves.known,matching,texts) are rewritten by the test itself withBOM_MATRIX_UPDATE=1 BOM_MATRIX_RELABEL=1. 85 rows that now count a line further from beancount than recorded get a known-list reason: every one of them holds a mid-line#!/#+, and what it hid is now read and counted by that row's causes.worse_than_mainlist:bom-glued-to-account&mid-line-hash-bangtobom-glued-to-account(their#!/#+part is gone);#!/#+used to mask on main.holdsgains one fallback,main_read_less: a further-than-main line also holds when main's count is exactly what rledger counts with each mid-line#!/#+and the rest of its line cut off (hash_bang_cut), as main read them, and that cut moves the line's count.hash_bang_cutfinds the markers with the lexer, so it never cuts inside a string or a;comment (round 1). It accepts a line whatever its cause, which its doc says: it explains why main was closer, while the known list still names why rledger differs from beancount. Six listed lines rely on it. Both helpers have unit tests (test_hash_bang_cut_cuts_only_past_a_lines_start,test_holds_takes_a_line_main_read_less_of).Caches
CACHE_VERSION73 to 74. That cache is saved only for an error-free load. But a ledger with an indented#+/#!line loaded with no error on v73 and is now a parse error, so a v73 blob would keep serving the old error-free load. The two layout tripwires move to 74; no archived layout changes.CACHE_VERSION110 to 112,LOADER_CACHE_VERSION_PIN74. Both payloads archive parse errors and directives. v111 is held by fix(wasm)!: give a validation finding the hint rledger check gives it #2973.Corpus
rledger check --no-cacheandrledger doctor parse -vover all 337 tracked*.beancountfiles: byte-identical between main (f785188cc68) and this branch.How to test
Gates run locally:
cargo fmt --all --check, clippy--all-features --all-targets -D warningson parser, loader and wasm,RUSTDOCFLAGS=-Dwarnings cargo doc --no-depson the same, and the parser, loader, wasm and lsp test suites with--all-features.Not in this PR
X!!#+++02,T#! "ab" #t Ü:A) is still a token here where beancount starts an invalid run.T#!now counts asT!already did on main. A glued quote whose string runs onto the next line is compat(parser): a lone capital glued to a quote opens a multi-line string where beancount reads one invalid token #2977.Review round 1
#!/#+message (see "Diagnostics" above); pinned intests/mid_line_hash_bang.rsunder a posting, at the top level (#+STARTUP: showall, and after a column-0#+TITLEline), in anopen's metadata, and on posting and header lines.\r(fix(parser): a stray \r before the line ending is a parse error; bean-check accepts it #2774), 2 lone capital.error_later_in_runmulti-line string hole.Shebang/EmacsDirectivearms ofinvalid_runs(its text always starts with a blank), and updated theis_comment_tokenrustdoc.hash_bang_cut(lexer-found markers only) andmain_read_less(the cut must move the line's count), and documented that it accepts a line whatever its cause. The matrix lists did not change.Parse-error counts per line do not move in this round (only messages and spans, on the same line), so the parity manifest and the BOM matrix data are unchanged.
Review round 2
unexpected inputanywhere on the line before the#, so an earlier real error ($xinAssets:A 1 USD $x #+y,open Assets:A ÜÜ #!x, a stray\rbefore#!x, a line led by$x,true,k: trueoré) was moved onto the marker, and the error list could leave start order. Now only a marker that leads its line renames the line's error; otherwise only a defect at the#itself is widened. Per-line counts on every repro match bean-check 3.2.3 (bean-check / this PR:$x #+yin a posting 2 / 2,open Assets:A $x #+y2 / 2,\r#!x2 / 2,k: true #+y3 / 3).tests/mid_line_hash_bang.rs: an earlier bad token keeps its own error over its span, the stray\rkeeps its error, errors stay sorted by start, a#!inside a string renames nothing (kk: "a #!x" $y,* "a #!x" $y), and the#+of<BOM> #+xis bytes 5..7. With the rename now limited to a leading marker or an error at the#itself, a#!in a string or comment can never lead its line nor carry an error at its#, so a raw byte scan for markers would give the same result; the lexer stays the source of the markers so the rule has one definition.Closes #2928
🤖 Generated with Claude Code