Skip to content

fix(parser): evaluate arithmetic in custom and pushmeta values - #2814

Merged
robcohen merged 7 commits into
rustledger:mainfrom
ryanduguid:ryan/custom-value-arithmetic
Oct 30, 2026
Merged

robcohen merged 7 commits into
rustledger:mainfrom
ryanduguid:ryan/custom-value-arithmetic

Conversation

@ryanduguid

Copy link
Copy Markdown
Contributor

Summary

A custom or pushmeta value written as an arithmetic expression was split into separate values with no error: custom "c" 10/4 USD parsed as 10 and 4 USD, and pushmeta kk: 1+2 as 1. Beancount evaluates both, to 2.5 USD and 3. 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 error invalid arithmetic expression in custom value (overflow or division by zero) (or pushmeta 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

  • Bug fix (non-breaking change that fixes an issue)

Changes

  • read_arith_expr distinguishes a malformed expression from one with no value; parse_arith_expr keeps its Option signature, so balance, price, cost and amount callers are unchanged.
  • One walk over the value tokens (value_groups) is shared by extract_custom_values, the pushmeta reader and the new checks in the red and green trees.
  • CHANGELOG entry under Fixed.

Testing

  • cargo test passes: cargo test -p rustledger-parser 780 passed. The workspace run fails the same 21 Windows-only tests as main.
  • cargo clippy passes (-D warnings), and cargo fmt --check is clean
  • Tested manually with a beancount file, against beancount 3.2.3
  • Added new tests for the changes: they fail on main and pass here, and new green-equals-red corpus cases

Beancount 3.2.3 gives the same values for 1/3 USD, 2/7 and -1/3 USD. 10/3 differs (28 decimal places here, 28 significant digits in beancount), but it differs the same way in price, balance and metadata through the shared evaluator, so I left it and noted it in a test comment.

Review Focus

  • Please review for beancount compatibility

Related Issues

Fixes #2806

Not covered: an ordinary metadata line such as kk: 1/0 still goes through the separate metadata reader and loads as 1.

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
@ryanduguid
ryanduguid requested a review from a team as a code owner October 29, 2026 22:03
ryanduguid and others added 3 commits October 30, 2026 13:18
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

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.77301% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/convert.rs 98.67% 8 Missing ⚠️

📢 Thoughts on this report? Let us know!

ryanduguid and others added 3 commits October 30, 2026 14:27
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]>
@robcohen
robcohen merged commit 3373a35 into rustledger:main Oct 30, 2026
62 of 66 checks passed
@ryanduguid
ryanduguid deleted the ryan/custom-value-arithmetic branch November 2, 2026 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(parser): arithmetic in a custom value is silently cut short

2 participants