Repository navigation
fix(parser)!: require a flag or txn after a transaction's date, as beancount does - #2909
Merged
Merged
Conversation
…ancount does `2024-01-02 "x"` with postings, with no `*`, `!` or `txn`, loaded clean as a `*` transaction, where beancount 3.2.3 reports `syntax error, unexpected STRING`: its grammar has no flagless transaction. The form was never a documented extension. rledger's first parser required a flag; the shorthand came in with the winnow parser and was carried into the CST parser on the belief that beancount accepts it. A header whose first string, tag or link comes straight after the date is now a `MissingFlag` header defect, reported at that token by the one rule both converters share (`txn_header::first_header_defect`). As for every header defect, the transaction is kept, so one missing flag does not cascade into unopened-account or balance errors. A header that starts with a tag or a link is now a transaction node too, so its postings are not each reported as stray lines: one error, as in beancount. On a flagless line, the `|` deprecation and a mid-line `#!` / `#+` after the first string are not reported, since beancount's grammar stops there. Parse-error parity: 76886 -> 77153 matching inputs; compat and tracked ledgers unchanged. Loader cache v65, WASM cache v86. Closes #2905 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 |
# Conflicts: # CHANGELOG.md # crates/rustledger-wasm/src/cache.rs
…t newline byte Review of #2909. The `#!` / `#+` suppression on a header with no flag found the line's end with the first `\n` byte, which can sit inside a first string that spans two lines, so `2024-01-02 "a\nb" #!` still reported the `#!`. It now uses the NEWLINE start that `header_line_tokens` returns, and the conformance test pins that shape. Also corrects a stale comment in tests/ast.rs that still called the flagless form an implied-flag arm. 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 changed
A transaction header with no flag (
2024-01-02 "x"followed by postings, with no*,!ortxn) loaded clean in rledger as a*transaction. beancount 3.2.3 rejects it withsyntax error, unexpected STRING, because its grammar needstxnor a flag after the date. It is now a parse error.txn_header::first_header_defect(the one rule shared by the green and red walkers) has a newMissingFlagdefect: a string, tag or link straight after the date. It is reported at that token, with the messageunexpected "\"x\"" in transaction header: a transaction needs a flag (*or!) ortxnafter the date.identify_directivenow also opens aTRANSACTIONnode onDATE TAGandDATE LINK, so a header that starts with a tag or link keeps its postings. It used to give one error for the header and another for each posting line. Now it gives one error, as beancount does.REPORTS BUT DOES NOT DROP), so one missing flag does not cascade into unopened-account or balance errors. beancount drops the transaction, but the load fails on the parse error either way.spec/impl/error-recovery.mdalready describes this (missing flag: recover, keep, report).|deprecation warning (which comes from beancount's grammar) and a mid-line#!/#+are no longer reported on top of the header error. These two follow-ups are scoped toMissingFlagonly.CHANGELOG.mdhas a Breaking entry naming the fix (add*ortxnafter the date).History check: not a deliberate extension
0b08da4c7a5, "Initial release") required a flag:transaction_bodystarts withflag(), with no.or_not().d65a62fdeac("perf(parser): add winnow-based parser", Feb 2026) as one arm,Token::String(_) => '*', // Implied txn. It had no test, doc or CHANGELOG entry.ae22b566cf0) copied that arm. Copilot's review only pointed out thatidentify_directivedid not match the legacy parser's STRING dispatch. The words "Beancount accepts the implied-transaction shorthand" were written in feat(parser): #1262 phase 2.1b TRANSACTION header recognition + multi-line body #1274's own fixup commit, andtransaction_with_implied_flag_via_bare_stringpinned them. The claim is false.docs/orCHANGELOG.mddescribes it as an rledger extension.bean-check comparison (beancount 3.2.3,
nix develop .#bench)Each case has
open Assets:Aandopen Assets:Bon lines 1 and 2, with the header under test on line 3.2024-01-02 "x"+ postings3: unexpected STRING3:12header error2024-01-02 "p" "x"+ postings3: unexpected STRING3:122024-01-02 "x" #t ^l+ postings3: unexpected STRING3:122024-01-02 #t ^l+ postings3: unexpected TAG3:12(one)2024-01-03 ^l "y"+ postings3: unexpected LINK3:12(one)2024-01-02 "p" | "x"+ postings3: unexpected STRING3:12(no pipe warning)2024-01-02 "x" $(#2903 review)3: unexpected STRING,3: Invalid token: '$'3:16only3:12and3:162024-01-02 "x", then2024-01-03 * "y"+ postings3: unexpected STRING,ykept3:12,ykept2024-01-02+ postings4: unexpected EOLThere are two known differences, both from bison's three-token error recovery, which suppresses a second error that comes too soon after the first:
"x" #t ^l+ postings followed by2024-01-03 ^l "y"+ postings: beancount reports only line 3, and rledger reports lines 3 and 6.Parse-error parity harness (
scripts/compat-parse-error-parity.py)bc-only(beancount errors, rledger silent) went from 78 to 27. The manifest is re-pinned with--update, and no ledger row changed.--checkis clean.12 fuzz inputs that used to match no longer do. In every one, the earlier match was a coincidence that the new header error now exposes:
BomInDirectiveBodythere, but beancount accepts a BOM in a string (2024-01-02 * "ab"loads clean in bean-check and errors in rledger, on main too). Before this PR, that BOM error stood in for beancount's syntax error in the line count. This is a separate, pre-existing divergence and worth its own issue.!on line 1, then a flagless header on line 2).Real-ledger impact
.beancountfiles have one.spec/impl/error-recovery.md, which already expects a missing-flag error.This is still user-visible: any user ledger with this form stops loading, so the CHANGELOG entry is under Breaking.
Downstream consumers
rledger format, LSP formatting andtry_format_sourceall refuse a file with parse errors, so a flagless file is refused rather than rewritten. The CST formatter never removes a flag.DIRECTIVES, which offertxn,*and!. No snippet inserts a bare string after a date.rledger add, CSV/OFX importers: they buildTransaction::new(..).with_flag('*'). CoreTransaction::flagis acharthat is always rendered (format_transaction_lineswrites"{date} {flag}").ag-rledgerwrites through the same core formatter." ") passesdata_to_transaction(it accepts any single char) and would render a header with no visible flag. This is pre-existing, applies to any invalid flag char, and is not changed here.How to test
The four new conformance tests fail on main. They check both
parseandparse_red_only.Verified locally:
cargo test --workspace --all-featurescargo clippy --all-features --all-targets -D warningsRUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-depscargo fmt --checkcorpus_baselineandcst_baseline(no row moved)--checkCloses #2905
🤖 Generated with Claude Code