Repository navigation
fix(parser): a * that begins any cost component is the merge marker - #2399
Conversation
Beancount's grammar takes a cost as a comma-separated list of components
(number, currency, date, label, `*`), and `*` may be any of them:
`{100.00 USD, *}`, `{2020-01-01, *}` and `{*, 100.00 USD}` all set merge
(checked against beancount 3.2.3's parser, which reaches its "Cost merging is
not supported yet" action for each). `MergeFlag` decided the flag from the
first token after the opener only, so a trailing `*` parsed as a plain
per-unit spec, with no diagnostic: `-5 X {110.00 USD, *}` failed with
`No matching lot`, and `-5 X {100.00 USD, *}` silently sold from the lot at
100 instead of the merged pool.
The rule is now: a `*` is the merge marker when the previous non-whitespace
token is the opener or a comma, i.e. when it begins a component. A `*` after a
number or `)` is still multiplication (`{500 * 2 USD}`). `MergeFlag` is the one
machine both the green and red walkers feed, so both change together; the
test that pinned `{2 USD, *}` as not-merge now pins it as a merge, with rows
for the other positions, and the red-tree accessor test gains the trailing
form. `rledger format` round-trips every form unchanged.
Caches: parser output changes, so loader CACHE_VERSION goes to 36 and the
wasm cache to 24 (with its loader pin). The wasm cache archives BOOKED
directives and errors, and the booking fixes merged since v23 (#2379, #2385,
#2391, #2395, #2397) each changed those without a bump, so a v23 blob could
serve their pre-fix answers; v24 covers them too. The CostNumber and
MetaValue byte-layout tripwires still compare their bytes; only their
FIXTURE_VERSION moves.
Not changed: a number in a merge spec is ignored, for the leading form as it
always was and now for the trailing one (#2398).
Tests: the MergeFlag row test and the ast accessor test; a loader test that
`{*}`, `{110.00 USD, *}` and `{100.00 USD, *}` all book from the merged pool
at 110, which fails on main's parser. Workspace suite green (169 binaries);
`check` byte-identical to main on four benchmarks; callgrind -0.03% to
+0.004%.
Closes #2329
Refs #2398
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
📊 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?
|
…fter, the opener Review of #2399. `cargo mutants --in-diff` (the CI mutation gate covers rustledger-core only, not the parser) left the two `past_opener` guards alive: flipping either let a comma before the opener, or a second `{` on malformed input, arm a component start, so `, * {...` or `{2 USD {*` would read as a merge. Both are malformed-input paths the doc comment promises to ignore; two direct-feed assertions now pin them, and all 11 in-diff mutants are caught. Also checked in review: - Only `merge` changes. Parsing 18 shapes with main's and this branch's parser gives identical number, currency, date and label for every one (compound `#` and total `{{}}` included); a missing comma (`{100 USD *}`) is still a parse error, as in beancount. - No other code re-derives the merge flag; `Amount::is_arithmetic` sees the posting's amount node, never a cost spec's tokens. - A trailing `*` on a buy books the same lot as before, as the leading form always has. Refs #2329 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Deep review (head 0282650)Verdict: ready once CI is green. No defects. One test gap closed. Scope: only
|
| spec | main merge |
PR merge |
|---|---|---|
{*}, { * }, {*, 100 USD}, {* 100 USD}, {*, *} |
true | true |
{100.00 USD, *}, {100 USD,*}, {2020-01-01, *}, {"lbl", *}, {{100 USD, *}}, {100 # 5 USD, *}, {100 USD, *, 2020-01-01} |
false | true |
{500 * 2 USD, *}, {(1 + 2) * 3 USD, *} |
false | true (value still 1000 / 9) |
{500 * 2 USD}, {(1 + 2) * 3 USD}, {*, 500 * 2 USD} |
as before | as before |
{100 USD *} (no comma) |
parse error | parse error (beancount: syntax error) |
No second copy of the rule
MergeFlag is the only merge detector. The green walker (cost_spec_from_tokens) and the red one (ast::CostSpec::is_merge) both feed it, and ast_is_merge_agrees_with_the_converted_cost_spec ties them. The other STAR match in ast.rs is Amount::is_arithmetic, which sees a posting's amount node, never a cost spec's tokens. The LSP, completion and wasm read the parsed CostSpec.
Behavior
- Sales: all three forms (
{*},{110 USD, *},{100 USD, *}) now book from the merged pool; on main the trailing forms failed or sold from one lot. - Buys: a trailing
*on an augmentation books the same lot as before (5 X {100 USD, 2020-01-02}), as the leading form always has. - Format:
rledger formatround-trips every form unchanged. - A number inside a merge spec is ignored, as it always was for the leading form (A number in a {*} merge spec is silently ignored: {*, 100 USD} books at the pool's 110 #2398).
Mutation testing (cargo mutants --in-diff, parser)
The CI mutation gate covers rustledger-core only. Locally, 11 mutants on the changed lines: the first run missed both past_opener guards. A comma before the opener, or a second { on malformed input, could arm a component start, reading , * {… or {2 USD {* as a merge. 0282650 pins both with direct-feed tests, and all 11 are now caught.
Caches
- Loader v36 (parser output changed).
- Wasm v24. It archives booked output, and the booking fixes merged since v23 (fix(booking): apply a transaction in the order booking read it #2379, fix(booking): drop lots netted to zero, and sell an undated pool last #2385, fix(booking): a cost matching only its own side adds to that side #2391, fix(core): AVERAGE pools only the side a reduction takes from #2395, fix(core): a cost spec, even {}, matches only lots held at cost #2397) each changed that without a bump, so v24 is overdue for them as well as needed for this. The pin test ties the two versions.
- Byte-layout tripwires: they still compare bytes; only
FIXTURE_VERSIONmoved.
Evidence
- Tests: workspace suite green (169 binaries), clippy and rustdoc clean.
- Loader test: the pipeline test fails on main's parser ("No matching lot").
- Output and perf:
checkbyte-identical to main on four benchmarks; callgrind −0.03% to +0.004%.
What
A
*that begins any cost component is now the merge marker, not only a leading one.{100.00 USD, *},{2020-01-01, *}and{*, 100.00 USD}are all merges, as in beancount's grammar. A*after a number or)is still multiplication ({500 * 2 USD}).Why
Beancount's cost is a comma-separated list of components (number, currency, date, label,
*), and*may be any of them. Checked against beancount 3.2.3's parser:{*},{*, 100 USD}{100.00 USD, *},{2020-01-01, *},{"lbl", *},{{100 USD, *}}{500 * 2 USD}{500 * 2 USD, *}{* 100.00 USD}(no comma)On main the trailing form was a silent change of operation:
-5 X {110.00 USD, *}failed withNo matching lot, and-5 X {100.00 USD, *}sold from the 100 lot instead of the merged pool. Both now book from the pool at 110, like{*}.How
MergeFlag: the one state machine both the green and red CST walkers feed. It now sets the flag on a*whose previous non-whitespace token is the opener or a comma, instead of deciding on the first token after the opener. Tokens before the first opener, and later openers on malformed input, are still ignored.{2 USD, *}as not a merge now pins it as a merge, with rows for the other positions and a multiplication-then-comma case. The red-tree accessor test gains the trailing form, and two tests were renamed that named the old rule.rledger formatround-trips every form unchanged.Caches
Parser output changes, so the loader cache goes to v36 and the wasm cache to v24 (with its loader pin).
Found while doing this: the wasm cache archives booked directives and errors, and the booking fixes merged since its v23 (#2379, #2385, #2391, #2395, #2397) each changed those without a bump, so a v23 blob could still serve their pre-fix answers. v24 covers them too, and its doc comment lists them.
The
CostNumber/MetaValuebyte-layout tripwires still compare bytes; only theirFIXTURE_VERSIONmoves, since neither encoding changed.Evidence
a_trailing_star_component_merges_like_a_leading_one(loader):{*},{110.00 USD, *}and{100.00 USD, *}all book as a merge at 110. It fails on main's parser ("No matching lot").checkbyte-identical to main on four benchmark ledgers.check: −0.03% to +0.004%.Not in this PR
A number in a merge spec is ignored:
{*, 100 USD}has always booked at the pool's 110, and the trailing form now does too. What it should mean is its own question: #2398.Closes #2329
Refs #2398
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr