Repository navigation
fix(parser): evaluate arithmetic in custom and pushmeta values - #2814
Merged
robcohen merged 7 commits intoOct 30, 2026
Merged
Conversation
A custom value such as 10/4 USD was split into Int(10) and Amount(4 USD) with no error; beancount evaluates it to 2.5 USD. The shared value_tokens_to_meta now evaluates an expression with the canonical arithmetic reader and pairs an adjacent currency, so custom and pushmeta values match beancount and the metadata path. A currency after a closing parenthesis, as in (1+2) USD, is no longer reported as bare. An expression that is well formed but does not evaluate (10/0 USD, an overflow) is now the error "invalid arithmetic expression in custom value (overflow or division by zero)", or "pushmeta value", over the expression and its currency, instead of a silent split into 10 and 0 USD. Both trees raise it from one walk, value_groups/unevaluable_value_expressions, which the value extractors share, and parse_arith_expr is now a wrapper over read_arith_expr, which tells a malformed expression from one with no value. No value is emitted for a rejected expression, as for the values #2190 drops; a pushmeta key gets MetaValue::None. A malformed expression stays reported once, by the header-line check, and its group takes the value token it broke off at, so 10 + TRUE emits nothing rather than TRUE. Fixes #2806
…thmetic # Conflicts: # CHANGELOG.md
A pushmeta value follows the metadata grammar, but was read by the custom-value walk, so the same value differed by where it was written: `pushmeta kk: 1/0` was an error and `kk: 1/0` loaded as 1, and `pushmeta kk: "a\"b"` kept its escape (#2812). - pushmeta_value now calls meta_value_from_tokens. - One check, unevaluable_meta_value, reports a well-formed expression with no value (division by zero, overflow) on a metadata line and on a pushmeta, from the shared directive loop, so both trees agree; the value is MetaValue::None instead of the first number. - A malformed pushmeta expression pushes nothing, as in beancount, so its popmeta reports the absent key. - Overflow is documented as a deliberate deviation from beancount. - ArithRead is an enum (Malformed, NoValue, Value). - Tests: every pushmeta row of the #2812 table, a drift guard comparing each value as a metadata line and as a pushmeta, the overflow pin, and green == red for the custom arithmetic cases. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Parser output changes for custom and pushmeta values (#2806, #2812), so a stale blob would keep serving the old split values. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
examples_data_custom.beancount now parses its arithmetic custom values, so its parser-output hash changes; its source hash is unchanged.
Before, only a malformed expression stopped the push: `pushmeta kk: "v" !`, `1 2`, `USD,EUR`, `10 USD @` or `^link` still pushed part of the value, and the matching popmeta stayed silent. beancount pushes nothing for any syntax error on the line and then reports the absent key at popmeta. The push is now decided by the same header check that reports the error, except a split date (`2024-02-002`), which beancount reads as one valid date: it is reported until #2808 but still pushed, so popmeta does not add a second error. Also replaces two em-dashes in comments. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…le value The exemption added in e45211b compared defect ranges, so any pushmeta line that started with a split date still pushed even with more on the line (`2024-02-002 !`, `"v" 2024-02-002`, a run-on `2024-02-002.`), where beancount reports the syntax error, pushes nothing, and then reports the absent key at popmeta. Only a value that is exactly one date in beancount's DATE shape is exempt now. The CHANGELOG describes the rule. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 3, 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.
Summary
A
customorpushmetavalue written as an arithmetic expression was split into separate values with no error:custom "c" 10/4 USDparsed as10and4 USD, andpushmeta kk: 1+2as1. Beancount evaluates both, to2.5 USDand3. The shared value reader now evaluates the expression through the same evaluator as balance, price and posting amounts, and pairs an adjacent currency.An expression that is well formed but does not evaluate (
10/0 USD, an overflow) is now the errorinvalid arithmetic expression in custom value (overflow or division by zero)(orpushmeta value), in both parse trees, and no value is emitted for it. A malformed expression (10 *,10 + USD,10 + TRUE) was already a header-line syntax error and still is, once; none of its parts is emitted as a value.Type of Change
Changes
read_arith_exprdistinguishes a malformed expression from one with no value;parse_arith_exprkeeps itsOptionsignature, so balance, price, cost and amount callers are unchanged.value_groups) is shared byextract_custom_values, the pushmeta reader and the new checks in the red and green trees.Testing
cargo testpasses:cargo test -p rustledger-parser780 passed. The workspace run fails the same 21 Windows-only tests asmain.cargo clippypasses (-D warnings), andcargo fmt --checkis cleanmainand pass here, and new green-equals-red corpus casesBeancount 3.2.3 gives the same values for
1/3 USD,2/7and-1/3 USD.10/3differs (28 decimal places here, 28 significant digits in beancount), but it differs the same way inprice,balanceand metadata through the shared evaluator, so I left it and noted it in a test comment.Review Focus
Related Issues
Fixes #2806
Not covered: an ordinary metadata line such as
kk: 1/0still goes through the separate metadata reader and loads as1.