Repository navigation
fix(parser): read a stray \r before an invalid token as a blank, as beancount does - #2980
Merged
Merged
Conversation
…eancount does A `\r` that is not part of a line ending, with an invalid token after it on its line (`open Assets:C\r$`, ` kk: "v"\r$`, a `\r<BOM>` line, `close Assets:A\r<BOM>~`, `\r $~`), was an error of its own beside the token's: one error more per line than bean-check 3.2.3, which reads the `\r` as a blank. - The lexer makes such a `[ \t\r]` run trivia when the token it emits after the run is an error. A lone capital glued to a stray `\r` that is trivia is an invalid token, as beancount's one-letter rule needs a space, tab or `\n` after it; a `\rB\rC\r$` chain is walked once, in a loop, and its answer kept for every link, so it stays linear. - The error-line logic reads a stray `\r` in the prefix before a line's first invalid token as a blank (it is trivia there by construction). - A `\r` before a token that lexes stays an error, deliberately, as the formatter folds a bare `\r` into a line break. Parity corpus (111,323 inputs): 78,500 -> 79,504 matching. 1,028 newly match; 24 no longer do, each one where the `\r` error had stood in for an error rledger misses with a real space there too. The manifest takes both in. WASM cache v115; the loader cache is unchanged, as every input that changes has a parse error before and after. Closes #2966 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❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
… flag Review of #2980: the new lone-capital rule made a transaction's one-letter flag glued to a stray `\r` (`2024-01-02 P\r\r\n`, `P\r\t\n`, `S`) an invalid token, which dropped the header and reported every posting and metadata line under it, where bean-check 3.2.3 and main report the header line alone. The rule now leaves the flag position (a capital after a column-0 date and blanks) to the parser's glued one-letter rule (`GluedLetterFlag`), which keeps the transaction, as on main. The chain walk skips it too. `P\r$` is still the flag's error and the `$`'s, as in beancount. Tests: P and S with `\r\r\n`, `\r\t\n`, `\r$`, `\r $`, `\r\r$` and `\r` at EOF, each with postings and metadata under the header and with postings only, per-line counts checked against bean-check 3.2.3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ction Round-2 review of #2980: after an indented date at the top level, a one-letter flag glued to a stray `\r` (` 2024-01-02 P\r\r\n`, `P\r` at EOF, tab indent, CRLF) became an invalid token in the flag position, so the line was no transaction: the indented-directive error swallowed the token's, and every posting under it was reported. bean-check 3.2.3 and main report two errors on that line and nothing under it. The lexer cannot tell a top-level indented line from a body line, so the parser decides: `identify_directive` takes a one-letter capital invalid token after a date and a blank as a transaction's flag, as it takes the one-letter `CURRENCY`. The transaction keeps its postings quiet, and the line has the indented-directive error and the invalid token. Under a transaction the line stays a body line, with beancount's two errors. Tests: space and tab indent, CRLF, `\r\r\n`, `\r\t\n`, `\r` at EOF and `\r$`, with and without postings, at the top level and under a transaction, per-line counts checked against bean-check 3.2.3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er a blank Round-3 review of #2980: the `identify_directive` arm that takes a one-letter capital invalid token after a date as a transaction's flag also fired after a stray `\r` (` 2024-01-02\rP\r\r\n`). After a line that already failed (`open Assets:A $`, `$`, ` kk: "v" $`, ` k: "v"` under open/close/note), beancount is in error recovery and reports only `Invalid token: 'P'` there, as main does; the arm made the line a transaction and added the indented-directive error, one more. The arm now requires spaces or tabs between the date and the capital, where the one-letter `CURRENCY` flag stood on main, so after a stray `\r` the line reads as on main. The round-2 indented-date shapes, with a blank, are unchanged. Tests: the five failed-line contexts, `\r`, `\r ` and ` \r` before the capital, `\r`, `\r\r\n` and `\rB\r\r\n` after it, and ` k: 1` with `\r$`, per-line counts checked against bean-check 3.2.3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…beancount does Round-4 review of #2980: - A blank-free run that a lone capital starts and a capital glued to a stray `\r` ends (`P@T\r`, `B,B\r`) counted the last capital as an error of its own after the run; beancount reads the run as ONE invalid token. `error_later_in_run` now sees that last capital as the run's error, so the run merges. - A capital glued straight to a date that starts its line (`2024-01-02P\r`, ` 2024-1-2Z\r`) became an invalid token, which dropped the transaction and reported every posting under it. It is left to the parser as the flag, as on main, and so is a run that a flag-position capital starts (` 2024-01-02 P@Z\r`). - After an invalid date the line's grammar errors are dropped, but its lexer errors stand. A one-letter capital glued to a stray `\r` is an invalid token in beancount, so its glued-flag error now stands too (`2229-2-37B\r\r\n`: beancount 2, main 1, now 2), unless the `\r` is still an error of its own, which stands for it as on main. A generated differential of 400,000 inputs (357,618 with a stray `\r`) against bean-check 3.2.3: 60,725 newly match, 4,093 no longer do, each bucketed by mechanism in the PR; no input without a stray `\r` changes. Without the lone-capital rule the net is 4,057 lower. The parity corpus is at 79,511 (main 78,500); the manifest moves 11 pins. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 26, 2026
robcohen
added a commit
that referenced
this pull request
Nov 27, 2026
… four more BOM and pipe line shapes (#3052) A stray `\r` is a blank to beancount's lexer; rledger reports one before a token that lexes as an error of its own (deliberate, #2980) and started no model of bison's recovery from such a line, so the lines bison keeps quiet after a statement failing there kept their errors. The input is now parsed once more as beancount reads it (`recovery::stray_cr_analog`), and from the first line holding a stray `\r` on, each line takes that parse's errors, except a line where rledger reports the `\r` itself, which keeps its own. Also: a line's BOM diagnostic stands for a first invalid token with no BOM; a misgrouped number beside a BOM-led token or a BOM inside an account is reported in the recovery; a pipe after a header failure gives no warning; an invalid booking method is reported on its `open`'s line. Generated differential (1.26M inputs, #3035/#3036 generators): 24,188 gained, 25,725 closer, 237 moved away (168 reproduce on the \r-free analog, 66 are the booking line where beancount drops the open). Parity corpus 107,647 -> 108,454, no regressions; BOM matrix 12,853 -> 12,917. WASM cache 142. 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
A
\rthat is not part of a line ending, with an invalid token after it on its line, is now a blank, as in beancount, instead of an error of its own beside the token's. That was one error more per line than bean-check 3.2.3 on every shape in #2966:2024-01-02 open Assets:C\r$2024-01-02 * "x"\r$Assets:A 1 USD\rxkk: "v"\r$(under anote)\r<BOM>or\r$line afterclose,open,commodity, a transaction,pushtag, or nothing; CRLF; no final newline; the whole fileclose Assets:A\r<BOM>~,\r<BOM>@,\r <BOM>~,\r $~,\r $(also afterbalance,price)k: "v"\r<BOM>~,\r<BOM>@,\r <BOM>~,\r$~,\r $~under aclose(LF and CRLF)Every count in the tests was checked against bean-check 3.2.3.
How
\rrun (TokenIter::stray_cr_trivia_end). A[ \t\r]run holding a stray\rwas trivia only when it reached the line ending (fix(parser): a stray \r before the line ending is a parse error; bean-check accepts it #2774). It is now trivia too when the token the iterator emits after it is an error. That token is read as emitted, after every rewrite innext.\r. Beancount's one-letter rule ([A-Z]/[ \t\n]) does not take a capital before a\r, soB\r$isInvalid token: 'B', then'$'. On main that count came out right only because the\rerror stood in for the capital's. The capital is now the invalid token whenever the\rrun after it is trivia.Chains. Whether that run is trivia can depend on the next link (
\rB\rC\r$), socr_chain_is_triviawalks the chain once, in a loop, and remembers the answer for every link. This keeps it linear and non-recursive, pinned bytest_tokenize_lossless_cr_lookaheads_are_linear_and_shallow.Runs. A blank-free run that a lone capital starts and a capital glued to the
\rends (P@T,B,B) is ONE invalid token, as in beancount (error_later_in_run).Flag positions are left to the parser, as on main, so a transaction keeps its postings quiet. Three cases:
2024-01-02 P\r) is reported by the header'sGluedLetterFlag.2024-01-02P\r,2024-1-2Z\r), is read as the flag byidentify_directive.2024-01-02 P@T\r,2024-01-02 P@Z\r) is reported by the header rule.After an indented date and spaces or tabs,
identify_directivetakes the one-letter invalid token as the transaction's flag, where theCURRENCYflag stood on main. After a stray\r(2024-01-02\rP\r) the line is no transaction, as on main.convert.rs):\rin the prefix before a line's first invalid token is read as a blank, in both places that decide whether the line error stands for that token. Such a\ris trivia there by construction.\ris a lexer error in beancount (an invalid token), so its glued-flag error is now kept too (2229-2-37B\r\r\n: beancount 2, main 1, now 2). The exception: when the\ris still an error of its own, that error already stands for it. This is limited to a stray\r, so every input without one is unchanged.Not changed, deliberately: a
\rbefore a token that lexes (open\rAssets:Cash,\rUSD, a\rthat starts a line) is still an error. Beancount accepts these, butrledger formatfolds a bare\rinto a line break, so accepting them would let format split a line check accepted (test_parse_cr_with_more_of_the_line_after_it_stays_an_error). The #2966 case cannot reach the formatter that way, because the line keeps the invalid token's error.Generated differential (400,000 inputs)
How the inputs were built: random preceding lines (none, two opens, an open transaction, a line that already failed, a
notewith metadata, a blank line), then a line start (dates at column 0, indented or glued; directive headers; postings; metadata;pushtag; numbers; comments;\r). After that come one to four pieces (capitals, currencies,$,@,,,~, strings, tags, links, a BOM,\x01,{1 USD},txn,k:), each preceded by a separator from"", space, tab,\r,\r\r,\r,\r,\r<BOM>,\r\t. Last come an ending (\n,\r\n,\r\r\n,\r, EOF,\r\t\n,\r\r\r\n,\r\n) and optional follow-up postings or metadata.How they were run: beancount through the parity harness's own
run_beancount, and rledger throughrun_rledger, compared per line exactly as the harness does.\r, and 42,382 are controls without one. No input crashed beancount.\rinputs.Each regression (matched on main, not here) was bucketed by comparing this PR's count with main's count on an analog. Each analog has the same meaning in beancount. Main mismatches beancount on it the same way.
\rreplaced by a space\rreplaced by$\rafter a lone capital replaced by$, the other stray\rbytes by a space or keptWhy the lone-capital rule stays:
B\r$,P\r$,B,B\r) matches only with it.The compound cases:
\ron the differing line, each read differently. Some are blanks before an invalid token, some are kept as errors before a token that lexes (86 lines hold one), and some are glued to a capital. A whole-line analog cannot express that mix.\r, where main already misses a second invalid token after the first on the same line and its\rerror stood in for it:2024-01-02 P\r$ Assets:is beancount 3, main 3, this PR 2 (main gives2024-01-02 open Assets:A $ Assets:1 where beancount gives 2). Other tokens seen there:B"q",T@, aPat EOF.Parity corpus
scripts/compat-parse-error-parity.pyagainst the pinned corpus (fuzz commit9bc79f4f, 111,323 inputs):ed1735f5b33)\rreplaced by a space:\rand for a space, and rledger now reads the\ras main reads the space. Main undercounts that space version too, mostly through a BOM diagnostic standing for more than one of beancount's invalid tokens.\rerror and also undercounts on main with a space.The manifest takes in both sets.
Known mismatches this PR leaves (documented, not fixed)
Each family below is one where this PR counts exactly what main counts for the analog in the last column, and main mismatches beancount on that analog. Counts are for the line shown.
Space family (2,534 in the differential, the 23 corpus losses): a stray
\rbefore an invalid token is a blank, and main already undercounts the same line with a space.2024-01-02\r$(also\r a,\r<BOM>,\r $~, tab indents)2024-01-02 $: 1open2024-01-02 a\r$2024-01-02 a $: 21T#t\ra2024-01-02 \r\x01 T\r2024-01-02 txnT\ra TGlued family (915): a capital glued to a stray
\rthat only ends its line is now an invalid token, so the line counts what main counts for the glued analog. Main had no\rerror there, and it matched only because the capital was a token.1 P\r\r\n(alsoAssets:A P,txn P,100 P) under anopen's metadata1 P$: 2\r2024-01-02 P\rat EOF, after the opens or under metadata\r2024-01-02 P$: 2P P\r\r\nin a transaction body2024-01-02 price HOOL 2 USD^l:~B\r\r\n\t2024-01-02 #tAssets:B\r1\r,Z\r\r\npushtag~\r\t-T\r\r\nMixed family (422) and compound family (222): both mechanisms on one line, described above.
Unchanged from main:
\rbefore a one-letter flag, in a clean context (kk: "v"\n 2024-01-02\rP\r): beancount 2, main 1, this PR 1. An earlier head of this PR matched it, but only by making the line a transaction, which was wrong after a line that already failed. There beancount, in error recovery, reports the invalid token alone.2024-01-02 P\r): beancount 1, main 2, this PR 2. With postings under such a line, rledger reports each posting, on main too, where beancount reports none.2024-01-02 $,2024-01-02 P$, at column 0 or indented) reports every posting under it on main, where beancount reports only the header line.Caches
WASM
CACHE_VERSION114 -> 115. Both payloads archive parse errors, so an older blob would keep serving the old counts.Loader cache unchanged at 75. The loader caches only error-free loads, and every input whose output changes has a parse error both before and after:
\rgive exactly main's errors.The pin and
FIXTURE_VERSIONdo not move.How to test
Closes #2966
🤖 Generated with Claude Code