Repository navigation
fix(parser): report stray characters in metadata lines - #2810
Merged
Merged
Conversation
The metadata converter took the first value token of each kind on a `key: value` line and ignored the rest, so `kk: "v" !`, `kk: 10 USD @`, `kk: USD,EUR` and `kk: Assets:B Assets:C` loaded clean and `rledger format` deleted the rest of the value. Every metadata line, under any directive, a transaction or a posting, is now checked against beancount's `key_value` rule (shared with `pushmeta`), from the shared directive loop so the green and red paths agree. The directive is still kept. The stray-character matrix gains 22 metadata snippets. Of its 6,148 metadata-line cases, rledger accepted 2,501 that beancount rejects; it now agrees on all but 68 pinned rows (60 column-0 lines, #2779; 8 date digits, #2808, of which 3 are deliberately stricter: `kk: 2024-02-002` loaded as the integer 2). Loader cache v51, WASM cache v66. Part of #2802 (stage 2 of 3) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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 |
This was referenced Oct 29, 2026
Closed
beancount's DATE is `[0-9]{4,}[-/][0-9]+[-/][0-9]+`, ours
`\d{4}[-/]\d{1,2}[-/]\d{1,2}`. Where they differ our tokens come out glued:
a DATE then a NUMBER (`2024-02-002`, `2024/02/002`, `2024-02-0002`) or a
plain-digit NUMBER, `-`/`/`, NUMBER, `-`/`/`, NUMBER run (`2024-002-02`,
`02024-02-02`, `20024-02-02`). Read our way they loaded as a wrong number
with no error in a metadata value, a custom or pushmeta value, and a
balance or price amount (`kk: 2024-002-02` was 2020). They are now
reported, on header and metadata lines, until #2808 lexes them as
beancount does; documented as a deliberate deviation and pinned.
Also documents and pins `kk: 1,,000` (1000 in beancount, the integer 1
here before), now reported like a posting or header amount already was.
Part of #2802 (stage 2 of 3)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
beancount's DATE takes the leading digits of `2024-002-02.5`, `2024-002-02,000` or `2024-02-002.` and then rejects the rest; read our way the run was arithmetic again (2019.5, 22). The split-date check no longer requires the last number to be plain digits (the first two still must: `2024.5-02-02` is arithmetic in beancount too). Also corrects what main loaded for `custom "x" 2024-02-002` (just 2, #2811). Part of #2802 (stage 2 of 3) Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 5, 2026
…te on its line (#2868) * fix(parser): read a date as beancount does, and name every invalid date on its line beancount's DATE is [0-9]{4,}[-/][0-9]+[-/][0-9]+ with the fields read as integers, so 02024-01-01, 2024-001-01 and 2024-01-016 are dates there. Our lexer took \d{4}[-/]\d{1,2}[-/]\d{1,2}: such a header date was an error, and a value was split into a number expression that #2810/#2817 reported as a stopgap. The lexer now takes beancount's shape, and one rule, parse_date_token (via diagnostics::date_fields), decides whether the fields form a real date (years 1 to 9999). The split-date stopgap (split_beancount_date, the pushmeta exemption, the 440 pinned matrix rows) is gone: those rows now match beancount and are checked. An invalid date is now reported wherever it stands, as beancount reports it while lexing, and stands for its line (report_invalid_dates, one pass over the green tree): event, price, transaction header, option, plugin and orphan lines name the date instead of a generic syntax error; two bad dates on one line are two errors; bad value dates under a directive with a bad date of its own are reported too. FFI input dates go through the same rule (rustledger_parser::parse_date), so year 0 is refused there and an invalid cost date is an error instead of an undated lot. Checked against bean-check 3.2.3 on 147 probe files (15 date shapes in 8 positions, the #2856 shapes, header dates): the same lines get parse errors, with the same counts. No compatibility-corpus or in-tree ledger changes (1,113 files); strict corpus baselines unchanged; the fuzz corpus replays clean (45,973 inputs, same 3 pre-existing idempotence cases as main); about 0.6% more instructions on the 913 KB example ledger. Loader cache v57, WASM cache v73. Closes #2808 Closes #2856 Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(parser): keep lexer errors on a bad-date line, linear date pass, ASCII date digits Review follow-ups on the date rework: - report_invalid_dates is linear: dates sorted, line bounds from one forward pass, errors matched by binary search; counter-based test. - Diagnostic columns are found through a character-mark table, so many errors on one long line no longer make check --format json quadratic. - A bad date drops only its line's grammar errors; an error covering an ERROR_TOKEN (stray character, mid-file BOM) stands, as in beancount. A date inside a beancount invalid-token run is not named. - parse_date takes only text the lexer reads as one DATE token, so 24-01-15 and 1-1-1 are refused; new leading_date_len shares the rule. - The DATE regex uses ASCII [0-9], as beancount does. - An out-of-range date field is named as written, not saturated. - Completion recognizes every header date shape via leading_date_len. Co-Authored-By: Claude Opus 5.5 <[email protected]> * chore: regenerate cargo-vet exemptions * fix(parser): keep a grammar error that comes before an invalid date Review round 2 on the date rework: - A bad date now drops only the grammar errors it causes: those that start at or after the line's first bad date, or a dropped directive's whole-node error when the header grammar fails at or after the date. An error before the date stands, as in beancount (`balance Assets:A USD 2024-02-30`, `note Assets:A 5 2024-02-30`). - rustledger-completion re-exports rustledger_core::ACCOUNT_TYPES instead of keeping a copy; the LSP drift test for the copy is gone. - parse_date's not-a-date error no longer repeats the input, so the FFI message names it once. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(parser): place a dropped directive's error by its header, and keep an orphan line's earlier error Review round 3 on the date rework: - A dropped directive's whole-node error spans the blank and comment lines before it, so it was not placed on its bad-date line and was kept beside the date. Each dropped directive now records its header start and the header grammar's failure point (usize::MAX when there is none). - A whole-line recovery error (an orphan indented line, a body line no posting takes) fails at its first token, or at the indent after a blank line, a column-0 comment or the top of the file, as beancount does; an error from before the date stands. - The regression test checks 22 rows directly, after a blank line and after a comment line, against bean-check 3.2.3's counts. - rustledger-completion's ACCOUNT_TYPES keeps its `&[&str]` type. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(parser): report a dropped directive's error on its header line, and more indent gaps Review round 4 on the date rework: - The whole-node error of a dropped directive now starts at the header, not at the blank or comment line its node span takes in, so it is on the line bean-check reports. - An indented line after option, include, plugin, pushtag, poptag, pushmeta, popmeta or an org-mode heading is no continuation, so it fails at the indent, as in bean-check 3.2.3. - The regression matrix asserts each error's line as well as the count. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(parser): a line inside a multi-line string is never an indent gap Review round 5 on the date rework: - previous_line_is_a_gap read raw text, so a multi-line string continuing on a line that starts with `*`, `;` or a gap keyword counted as a gap. The tree walk now records STRING tokens that run over a line end, and a line starting inside one is not a gap. - `%` and `# ` comment lines are gaps, as in bean-check 3.2.3. - Test rows for those and for pushtag, poptag, pushmeta and popmeta. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(parser): no indent gap after a bare marker, and read a string's line for the gap Review round 6 on the date rework: - A column-0 comment is `;` and anything, or `*`, `%` or `#` with at least one more character on the line and `#` not starting a tag, as in bean-check 3.2.3. A bare marker is a token there, so it is no gap. - A previous line that starts inside a multi-line string steps back to the line the string starts on, and only the no-metadata check applies there, so an indented line after a multi-line `option`, `plugin`, `include` or `pushmeta` value fails at the indent as in beancount. - Test rows for both. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(parser): a broken no-metadata directive is no indent gap, and `#\r` is a comment Review round 7 on the date rework: - A directive that takes no metadata but is itself a syntax error is no gap: beancount's recovery from it swallows the indented line after it, so it reports no unexpected INDENT there (bean-check 3.2.3). - `#\r`, `%\r` and `*\r` are comments, as flex's `.` takes the `\r`. - Test rows for both, including a valid multi-line plugin that still is a gap. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: rustledger-bot[bot] <257329324+rustledger-bot[bot]@users.noreply.github.com>
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
Stage 2 of #2802: a stray character on a metadata line (
key: valueunder any directive, a transaction or a posting) is now a parse error, matching beancount 3.2.3.The metadata converter (
meta_value_from_tokens) takes the first value token of each kind and ignores the rest. So these loaded clean onmain, andrledger formatthen deleted the rest of the value:mainloadedkk: "v" !"v"kk: 10 USD @10 USDkk: USD,EURUSDkk: Assets:B Assets:CAssets:BEach metadata line is now checked against beancount's grammar rule:
pushmetaalready followed the same rule, so stage 1'spushmetacode was factored out intokey_value_valueincst/directive_header.rsand is now shared. The new entry point isfirst_meta_defect.The check (
meta_entry_checkincst/convert.rs) runs from the shared directive loop over green children. So the green and red parse paths cannot disagree, and it allocates nothing per directive.As in stage 1:
unexpected "!" in a metadata line: expected the end of the line.Matrix
scripts/stray-char-matrix.pygains 22 metadata snippets, one per value type, each under a parent:TRUE,FALSE, tag,NULL, empty, a;comment, a key with-/_/digits, CRLF, two lines;open,balance,price,custom,note, a transaction and a posting.That gives 44,616 cases, 1,241 of them excluded. Existing rows are unchanged.
stray_char_matrix.rsnow classifies each case by the line the character lands on (stage()) and checks stages 1 and 2. A miss is a row beancount rejects that rledger accepts; a false error is the reverse. Every non-excluded row is counted, with no pins:Everything left in stages 1 and 2 is pinned by count in the test:
!,?,&lines (compat: column-0 :, !, & and ? lines are ignored by beancount but rejected by rledger #2779): 60 header rows plus 60 rows at the start of a metadata line.DATErule is[0-9]{4,}[-/][0-9]+[-/][0-9]+, and ours is not. The 6 metadata rows are deliberate false errors, covered under "Deviation" below.\r(fix(parser): treat a stray \r before a line ending as trivia, as beancount does #2804's documented deviation): 1 row.The stage-1 false-error count is unchanged in meaning. It grew from #2805's 186 only because the new snippets carry header lines too.
Deviations (AGENTS.md checklist)
Two places where rledger is now stricter than beancount on purpose. In both, our lexer splits a token that beancount reads whole, and on
mainthe pieces loaded as a wrong value with no error. A wrong value is worse than an error.1. Text beancount reads as one date.
beancount's
DATEis[0-9]{4,}[-/][0-9]+[-/][0-9]+and ours is\d{4}[-/]\d{1,2}[-/]\d{1,2}. Where they differ, our tokens come out glued (no whitespace between them), in one of two shapes.split_beancount_dateincst/directive_header.rsreports both:DATEwith aNUMBERright after it, when the last field has more than two digits:2024-02-002,2024/02/002,2024-2-002,2024-02-0002;NUMBER,-or/,NUMBER,-or/,NUMBERrun whose first two numbers are plain digits, the first at least four of them:2024-002-02,02024-02-02,2024/002/02, and invalid dates such as20024-02-02and99999-01-01.In both shapes the last number may run on past the digits (
2024-002-02.5,2024-002-02,000,2024-02-002.). beancount'sDATEtakes the leading digits and then rejects the rest, and bean-check rejects all of these.Where they were silently misread on
main, and what happens now:mainloadedkk: 2024-002-02kk: 2024-02-002customvaluecustom "x" 2024-02-0022024-02-00is dropped, #2811)pushmetavaluepushmeta kk: 2024-002-02pushmetakeeps the first number, #2812)custom,balance,price,pushmetakk: 2024-002-02.5,balance Assets:A 2024-002-02,000 USDbalance/priceamountbalance Assets:A 2024-002-02 USDbeancount reads a date there (2024-02-02), or rejects an invalid one. So this is a false error where beancount's date is valid, and agreement where it is not. These are still read as before: a spaced expression (
2024 - 002 - 02), a thousands separator (1,234-02-02), a short first number (202-002-02), and a decimal in the first two numbers (2024.5-02-02,2024-0.5-02), which is arithmetic in beancount too. The real fix, lexing these the way beancount does, is #2808.2. Doubled thousands separators.
beancount's
NUMBERis[0-9][0-9,]+[0-9], sokk: 1,,000is 1000 there. Ours lexes1,,,,,000, which loaded as the integer 1. The metadata grammar now reports the stray comma. A posting amount (1,,000 USD) and abalanceamount already reported it.Where both are recorded:
split_beancount_dateandfirst_meta_defect;test_parse_reports_a_split_beancount_date_instead_of_misreading_it(15 values in 5 places, plus 7 controls that must still parse) andtest_parse_reports_doubled_thousands_separators_instead_of_misreading_them;KNOWN_DATESin the matrix test.Tests
Matrix test:
test_parse_reports_a_stray_character_exactly_when_beancount_doeschecks rledger against beancount for stages 1 and 2. Onmainit fails.Explicit stray-character test:
test_parse_reports_a_stray_character_in_a_metadata_line_and_keeps_the_directivecovers 10 shapes, including posting and transaction metadata. For each it checks:try_format_sourcerefuses the file;format_sourcecopies it verbatim.On
mainit fails.Accepted values:
test_parse_accepts_every_metadata_value_typechecks that every value type beancount accepts still parses, with and without a trailing; c.Property test:
test_try_format_source_keeps_the_text_of_a_clean_metadata_linemakes up to three insertions into a metadata value (2,048 cases, fixed seed). For each clean result it checks thatrledger formatkeeps every non-whitespace character.Format-text check: the existing matrix check now allows the formatter's documented thousands-separator normalization, since
kk: 2,024-02-02formats as2024 - 02 - 02.How to test
Verified locally
Test suites:
cargo test -p rustledger-parser -p rustledger-core -p rustledger-loader -p rustledger -p rustledger-lsp --all-features, 125 results, 0 failed (rerun after the review fixes).Builds and lints:
cargo check -p rustledger-wasm, clippy-D warningson the parser, loader and wasm crates, rustdoc-Dwarningswith private items, andcargo fmt --checkare all clean.Corpus baselines: both strict baselines pass with no row moved, over the 731-file compat corpus.
Fuzz corpus: all 42,092 UTF-8 inputs of the
fuzz-corpusbranch (fuzz_parse,fuzz_green_eq_red,fuzz_parse_line) plus the in-tree regressions pass. Checked for each: lossless round-trip, green==red on directives, errors and currency occurrences, andformat_sourceidempotence. 0 failures.Parse time (release, best of 25,
fava_investor_examples_huge-example.beancount, 913 KB):main;main.That is about 2 to 3%. After the review fixes, a rerun against
mainwas within noise: 12.0 vs 11.5 to 12.0 ms, and 15.2 vs 15.0 to 15.1 ms with metadata.Caches: loader v51, WASM v66; the pin and both
FIXTURE_VERSIONs are 51.Found, left for stage 3 or other issues
Assets:A 2024-002-02 USDis 2020 USD, and the same goes for{2024-002-02 USD}and@ 2024-002-02 USD. beancount rejects all three. They are stage-3 code, so they are left for compat(parser): dates with extra digits (02024-01-01, 2024-001-01) are rejected; beancount accepts them #2808.%,#!and#+after a posting amount;{*};!Assets:A(a flag glued to the account), which is a false error.k: 1): beancount rejects them, rledger accepts them. They are left to compat: small leniencies vs beancount (lone *, trailing *, overlapping errors, 1-char metadata keys) #2787, as decided, and the matrix has no one-character-key snippet.kk: ^l: beancount rejects a link as a metadata value, and rledger already reports it (extract_link_metadata_value_errors), so it is not a matrix snippet.Part of #2802
🤖 Generated with Claude Code