Repository navigation
fix(parser): read a lone capital glued to a non-blank as one invalid token, as beancount does - #2990
Merged
Merged
Conversation
…token, as beancount does beancount lexes a lone capital (CAPITAL) only before a space, a tab or a line break ([A-Z]/[ \t\n]). Glued to anything that starts no longer CURRENCY or ACCOUNT, no rule takes it, and its default rule reads the run to the next blank as one invalid token: U~SD, U?SD, V, and T#! each are. The lexer here kept such a capital a token whenever the rest of its run lexed, and the parser glued one-letter rules reported it. That matched on a clean line, but after an earlier invalid token those rules are quieted with the line other construct checks, so the word went uncounted. The run is now the invalid token beancount reads, wherever it stands; a transaction flag position keeps the glued-flag rule. No error-free file changes (1,130 repository and compatibility ledgers, 450,000 generated inputs). Web cache 121 to 122; loader cache unchanged. Closes #2982 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 |
The glued-capital rule asked capital_rule_applies, which scans back to the start of the blank-free run, for every lone capital, before the cheap next-byte test. In a run a transaction flag leads every capital stays a token, so the lexer goes on inside the run and each later capital rescanned it: quadratic (2024-01-02 P followed by "x"T repeated: 15.8 s at 160k against 45 ms on main). The next-byte test now comes first, and a flag-led run is remembered to its end (flag_led_run_until), so it is scanned once. A byte-counter test pins it on four flag-led shapes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 15, 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
A lone capital glued to a non-blank (
U~SD,U?SD,V,,T#!) is now, with the rest of its run, one invalid token, as in beancount 3.2.3, also where the run otherwise lexes. Fixes the per-line counts of #2982: a line that already holds an invalid token (Assets:A $ U~SD,2024-01-02 balance Assets:A 2x U?SD) now counts beancount's 2, not 1.The beancount rule (lexer.l, beancount 3.2.3)
A capital is
CAPITALonly before a space, a tab or\n. Glued to anything else, a two-or-more-characterCURRENCY(a later letter or digit in[A-Z0-9'._-]) or anACCOUNTcan still take it (US~DisUS,~,D;U.SDis a currency). Otherwise no rule matches, the default rule entersINVALID, and the run up to the next space, tab,\ror\nis ONE invalid token. Confirmed withlex_iter_stringand bean-check on every shape in the tests.Why it was off
The lexer here kept such a capital a token whenever the rest of its run lexed (a one-letter currency, then
~, thenSD), and the parser's glued one-letter rules reported it: one error, which matched beancount's one on a clean line. After an earlier invalid token on the same line,quiet_after_invalid_tokensquiets the line's construct checks, those rules with them, so the word went uncounted. And a line whose first glued capital was reported through grammar errors (sometimes two) before the line's first invalid token miscounted a later one.Fix
TokenIter::next(crates/rustledger-parser/src/logos_lexer.rs): after the existing lookahead (error_later_in_run, which still handles runs with an error later in them and #2966's stray-\rchains), a lone capital followed by a non-blank that is not a\ris the error; theErrarm runs it to the next blank, as it does for every invalid token. No new scan, so lexing stays linear.Left as it was: a transaction's flag position (
flag_position, and runs a flag leads viacapital_rule_applies), wheretxn_header's glued-flag rule reports the flag and keeps the transaction (2024-01-02 P;c), and an indented date's flag keeps beancount's two errors (#2966'sidentify_directivearm reads only a one-letter error token as a flag). A capital before a\rstays with #2966's rule, and one at the end of the source with the existing rules.Real-ledger control
All 1,130
.beancountfiles in the repository and the fetched compatibility corpus (one fava file is not UTF-8 and was skipped), mainfa3680ade0aagainst this branch: 1,034 error-free on main, 0 flip between error-free and erroring, 0 change any per-line count.corpus_baseline(which hashes the full parse result, directives included) drifts on one file,parser-lima/test-cases_SyntaxErrors.ErrorInTransactionLine.beancount, already an error file: itsA:*:Bafter the narration is now the invalid token beancount reports (Invalid token: 'A:*:B'), one error before and after, with the invalid-token message in place of the header one. Both baselines are updated for that line only (fourledger2beancountrows that a local fetch flags as upstream content changes are left out, as in #2988).Caches
Parity corpus (pinned fuzz 9bc79f4, matrix, tracked, compat)
Counted from the manifests (main's on
origin/main, this branch's after--update): main 82,699 matching, this PR 82,828. 249 inputs newly match, 120 no longer match, and 116 that mismatch on both change category;--checkpasses with no regressions. (The report table's "Match" column, 82,846 here, counts all 111,341 inputs including fourledger2beancountfiles whose upstream content a local fetch changed and the manifest does not pin, so it is not the figure to compare.)Of the 120: 109 count exactly what main counts with each glued run written as an ordinary invalid token (
$q). 22 of those are matrix rows such asP Assets:#B, where a capital-led word that is no account is now one invalid token, as in beancount. The other 11 are fuzz lines ofR:R.../:Kshapes, a capital right after a:, which the analog script does not rewrite: the same mechanism.Generated differential
Three seeds of 150,000 inputs each (about 120,000 with a lone capital glued to every ASCII punctuation, digits,
é,€,Üor a BOM, in postings, costs, prices, balances, opens, commodities, transaction headers and flags, customs, options, metadata and bare lines, after 0 to 3 earlier pieces including invalid ones, then LF, CRLF or end of file, with following postings or metadata; about 30,000 controls with capitals only before blanks). beancount 3.2.3 (the harness'srun_beancount), main and branch, per line:Documented families (all 1,722 regressed, further and sideways inputs)
P0006andP0026, a glued flag before|), and the uncounted word made up for it. Each counts exactly what main counts with every glued run written as$q. None changes another line or drops a directive.|after it (2024-01-02 J|{1,50 X}~) that line already reports the flag and the deprecated pipe, one more than beancount, and a later glued word on it is now counted.:,(,[or{(8), real glued runs that the analog script does not rewrite, and 9 others of the same off-by-one shape on one line.BOM combination matrix
Regenerated with
BOM_MATRIX_UPDATE=1 BOM_MATRIX_RELABEL=1: matching rows 11,004 to 11,164, known list 2,432 to 2,272. 213 rows keep their counts and gainT#!among their token texts. 44 rows count a line further from beancount than recorded, each with a hand-written reason in the known list for exactly its new counts: all hold aT#!, now counted, on a line already off by its other errors. In the further-than-main list, 20 rows main no longer beats are removed and 18 added, each accepted by a new check,main_missed_glued_capitals: main's count is exactly rledger's less the line's capital-led invalid runs (asmain_read_lessdoes for #2928's#!).Speed
Linear. Seconds for
parse_error_lines, main against this branch, at 20k / 40k / 80k / 160k repetitions:2024-01-02 P+"x"Trepeated2024-01-02 P@+T@repeated2024-01-02P@+T@repeated2024-01-02 P@+T@repeatedU~SDwords on one lineV,run with no blankX(words on one line$ U~SDlinesT#!words on one lineA;xwords on one lineThe first version was quadratic on a run a transaction's flag leads (round-1 review: 15.8 s at 160k on the first shape). It asked
capital_rule_applies, which scans back to the run's start, for every capital before the cheap next-byte test, and in a flag-led run every capital stays a token, so each one rescanned the run. The next-byte test now comes first, and a flag-led run is remembered to its end (flag_led_run_until), so it is scanned once.test_tokenize_lossless_flag_led_run_is_decided_oncepins it with a byte counter (CAPITAL_RULE_SCANNED_BYTES) on the four flag-led shapes.A blank-free run of
A#!(#2989) is linear now too, as the capital makes the run one token (main 0.09 / 0.36 / 1.42 s at 10k / 20k / 40k, 0.00 here). A run of1#!, with no capital, is unchanged (1.43 s at 40k), so #2989 stays open.Note
10 U,SDin a posting now also gets a BOOK "cannot infer currency" error after its parse error, as main already gives10 U$D: onceU,SDis an invalid token the posting has no currency. Such a file has a parse error either way.Tests
crates/rustledger-parser/tests/capital_led_invalid_word.rs: every issue row (LF and CRLF), the header rows, two glued words on one line, the rule's boundaries (US~D,USD~,U.SD,U @,V{,{T@, a cost spec'sV,, a currency list), and the flag position unchanged. Every count checked against bean-check 3.2.3.test_parse_check_dropped_before_a_capital_led_invalid_word), and fix(parser): no error for a BOM-led account after a line's first error, and report a header defect before a tag (#2968) #2983's tag rows forT#!andU?SDcount beancount's 2.invalid_token.rs,directive_header.rsandtxn_header_conformance.rsnow expectV,,T#!,U~SD,U*SD,U,SD,E,URandA:*:Bas one invalid token each; the linear-lookahead test'sV,run is one.How to test
Closes #2982
🤖 Generated with Claude Code