Repository navigation
fix(parser): lex an account component with any non-ASCII start, as beancount does - #2908
Merged
Merged
Conversation
…ancount does
beancount 3.2.3's lexer lets any non-ASCII character start an account
component (`{UTF-8-ONLY}` in ACCOUNTTYPE / ACCOUNTNAME), and its grammar
then checks each account it lexed with `valid_account_regexp`, a
`regex.match` of `(roots)(:[\p{Lu}\p{Nd}][\p{L}\p{Nd}-]*)+`. So
`Assets:٣` and `Assets:٣Foo` are valid there, and `Assets:éclair`,
`Assets:😀` or `Assets:²` are one account token that is an `Invalid
account name`. rledger required every component to start with
`\p{Lu}\p{Lo}\p{Lt}` or an ASCII digit, so valid ledgers using such
names failed to parse.
- Lexer: the Account token is beancount's rule, except that a BOM does
not start an account (it stays the layout-transparent mid-file BOM
error).
- `is_valid_account_name` (the canonical rule, still lexer-backed) adds
beancount's name check: the root starts with `\p{Lu}`, the first
sub-component with `\p{Lu}` or `\p{Nd}`, and later components take
anything the lexer takes (the regex is a prefix match). The
`\p{Lo}`/`\p{Lt}` start from #816 stays accepted, documented. The
validator reports a rejected name as E1005 through it, as before.
- LSP references, rename and semantic tokens cover such an account.
- Parse-error parity: 77,744 of 111,323 inputs match, from 76,886;
compat 488 and tracked 381 unchanged. Manifest repinned. The 80 fuzz
inputs that moved away are garbage lines where an account now lexes
before an invalid token and rledger reports the line's recovery
error beside it, while beancount's bison recovery stays quiet.
- Formatter output is byte-identical to main on all 1,136 in-tree and
compat ledgers; the stray-character matrix regenerates unchanged.
Loader cache v64, WASM cache v85.
Closes #2906
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 |
…trict rule Review round 1 on #2908. beancount checks configuration values with `account.is_valid` / `is_valid_leaf` / `is_valid_root`, a FULL match: every component starts with `\p{Lu}` (or `\p{Nd}` after the root) and holds only `[\p{L}\p{Nd}-]`. The grammar's prefix check that `is_valid_account_name` now mirrors is looser, so two call sites went looser than beancount: - the `currency_accounts` plugin took `"Equity:CA:é"` as its base account; beancount falls back to `Equity:CurrencyAccounts`. - the `account_*` options took `"Conv:😀"` and `"Rounding:éclair"`, which beancount rejects (`Invalid leaf account name`); the `name_*` options took `"A😀"` (`Invalid root account name`). Add `rustledger_parser::is_valid_account_name_strict` next to `is_valid_account_name` (it runs that first, so it is still lexer-backed, then checks every component's start and characters with the same logos tables), keeping the `\p{Lo}`/`\p{Lt}` start of #816 on every component. Use it at the plugin and option sites. The other callers take ledger accounts (validator, FFI builder, budget, CLI `--account`) and keep the grammar rule, as beancount does. The LSP's `is_word_char` now takes every character the `Account` token takes, through a new lexer-backed `is_account_name_char`, so the word under the cursor is the whole account (`Assets:A😀`, combining marks). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-start # Conflicts: # CHANGELOG.md # crates/rustledger-validate/tests/validation_integration_test.rs # crates/rustledger-wasm/src/cache.rs
clippy's too_long_first_doc_paragraph. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…, and keep LSP words out of prose Review round 2 on #2908. - Document discovery: beancount's `find_documents` walks with `account.walk`, which yields only directories whose path passes `account.is_valid` and skips the rest in silence. Since this PR a ledger can open `Assets:A:éclair`, so the plugin found documents beancount does not. It now takes a directory only when `is_valid_account_name_strict` holds (bean-check: 1 Document of 4 in the new loader test, no warning for `Assets/misc`). The NFKC normalization `walk` applies is noted, not mirrored (predates #2906). - LSP: round 1 made `is_word_char` take every non-ASCII character, so prose words ran across `、`, `。` and NBSP. It is back to letters, digits, `:`, `-` and `_`; `get_symbol_at_position` now prefers the parsed account token at the cursor (new `ParseResult::account_at`, the counterpart of `currency_at`), and call and type hierarchy go through it. The unused `is_account_name_char` is dropped. - Drop an em-dash from an `options.rs` doc comment. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… ranges, and read whole accounts in WASM Review round 3 on #2908. - Document discovery NFKC-normalizes the account path built from the directory names before the strict check, as beancount's `account.walk` does, so a decomposed (macOS) `Liabilities/Ünï` and a fullwidth `Expenses/Ok` are found again (bean-check: 3 Documents in the loader test). The #816 divergence (`Assets/日本` is discovered here) is noted and pinned. - Selection range: no word expansion inside an ACCOUNT token, whose colon-delimited segment is the innermost range; the word scan crossed `:` but stopped at an emoji, so the ranges did not nest. - WASM `get_symbol_at_position` prefers `account_at`, as the LSP does. - LSP and WASM: a word that starts on an account is that account, so `open Assets:Bank:` gives `Assets:Bank` past the trailing colon too. - `account_at` binary-searches the occurrences; a test pins that they are sorted and agree with a linear scan. - CHANGELOG: the discovery change, and the #2649 entry's warning now applies only to strictly valid directory names. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… scan cannot cross it Review round 4 on #2908. Right after the trailing colon of `open Assets:A😀B:` both editors returned `B:`: the token lookup at the cursor misses (the account ends at the colon) and the backward word scan stops at the emoji. When the character before the cursor is a `:`, the account ending there is the symbol, in the LSP and WASM alike. CHANGELOG gains the editor-side changes of #2906. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-start # Conflicts: # crates/rustledger-loader/src/cache.rs # crates/rustledger-wasm/src/cache.rs # tests/baselines/parse-error-parity.manifest
robcohen
added a commit
that referenced
this pull request
Nov 10, 2026
WASM cache v88: v86 is #2905's on main, v87 is claimed by #2908. Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 10, 2026
Brings in #2908 (strict account validator; loader v66, WASM v87), #2919 and #2924. Keeps this branch at loader v68 / WASM v90 with one note per version, drops the merged warn_if_invalid_root (rejection now goes through check_root), and adds the issue's `A😀` root to the rejected-name tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 10, 2026
Brings in #2919's linear merge_padding_into_sorted (with the tolerance_multiplier threaded through), #2908 and #2924. WASM cache v88, loader pin 66. 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.
The beancount rule
beancount 3.2.3,
lexer.l:So any non-ASCII character can start or continue any component. Its grammar then checks every account it lexed (
Builder.account) withvalid_account_regexp. That check isregex.match(anchored at the start only) of(name_assets|...)(:[\p{Lu}\p{Nd}][\p{L}\p{Nd}\-]*)+, and on failure it reportsInvalid account name. This works out to:\p{Lu}(is_valid_root,ACC_COMP_TYPE_RE).\p{Lu}or\p{Nd}.What changed
logos_lexer.rs): theAccounttoken follows beancount's rule. There is one documented exception: a BOM does not start an account. It stays the layout-transparent mid-file BOM error token, so theBomInDirectiveBodyhandling for concatenated files is unchanged. beancount rejects that line too.is_valid_account_name(the canonical rule, still lexer-backed) now also applies beancount's name check: the root starts with\p{Lu}, and the first sub-component starts with\p{Lu}or\p{Nd}. Characters are classified by a small logos enum, so the Unicode tables are the lexer's own. It is the rule for an account a ledger holds: the validator's E1005 (Open check, unknown-account references, metadata andcustomvalues), FFIentry.create, a budget's quoted account and the CLI's--accountgo through it. Configuration values that beancount checks with its stricteraccount.is_validfamily (thecurrency_accountsplugin base account, theaccount_*andname_*options) use the newis_valid_account_name_strict(see "Review round 1").\p{Lo}) or titlecase (\p{Lt}) start stays valid, soAssets:日本and資産:現金load. beancount rejects both. This is documented at the function and pinned by tests.bean-check 3.2.3 vs rledger (
2024-01-01 open <name>)Assets:٣Assets:٣FooAssets:A٣Assets:3Assets:Ünï/Assets:ÄAssets:éclair(lowercase start)Assets:日本(Lo)Assets:Dž(Lt)Assets:😀,Assets:😀AAssets:A😀Assets:̈A(combining mark start)Assets:²(No),Assets:Ⅰ(Nl)Assets:A:日本Assets:A:éclair,Assets:٣:éÜnï:A(root not configured)Équité:A,Équité:٣withoption "name_equity" "Équité"Équité:éclairwith that optionAssets:éclairParse-error parity (
scripts/compat-parse-error-parity.py)Of the 1,104 pinned mismatches that changed, 938 now match and 166 changed category; 80 inputs that matched now mismatch, so the net gain is 858. The manifest is repinned with
--updateand--checkpasses. The 80 are fuzz garbage lines where an account now lexes ahead of an invalid token (e.g.FáTTT晒:<U+1680>\x1b): 61 are recovery cases, where rledger reports the line's recovery error alongside the invalid token while beancount is still in bison error recovery from an earlier line and reports only the invalid token (the existingrl-top-level-line/ recovery class), and 12 come from a BOM inside a name (P0026). That BOM behavior predates this PR (open Assets:A\u{feff}Bpasses bean-check, rledger reports P0026) and is tracked in #2910. No real ledger is affected.Stray-character matrix
Regenerated with beancount 3.2.3:
stray_char_matrix.tsvis byte-identical, so no row moved. All of its inserted characters are ASCII, and the ASCII side of the account rule is unchanged.Downstream checks
rledger formatoutput (stdout, stderr and exit code) is byte-identical to main on all 1,136 git-tracked and compat ledgers.Assets:٣Foo), rename (2 edits) and semantic tokens (one account token, length 11). Completion is string-based over the parsed account list, so it picks the account up.leaf,parent,root(account, 2)andaccount_depthonAssets:٣Foo:éxmatch bean-query.is_valid_account_name(FFI builds ledger directives, so it takes the ledger rule); the WASM cache is bumped with its loader pin.Review round 1
Two call sites had become looser than beancount, because they used the grammar's prefix check where beancount uses its full-match
account.is_validfamily. Both are fixed with one new canonical check,rustledger_parser::is_valid_account_name_strict, next tois_valid_account_name. It runsis_valid_account_namefirst, so it is still lexer-backed, and then fully matches every component against beancount's[\p{Lu}\p{Nd}][\p{L}\p{Nd}-]*(\p{Lu}only for the root), using the same logos Unicode tables. It keeps the\p{Lo}/\p{Lt}start of #816 on every component, consistent with the ledger rule, sooption "name_liabilities" "負債"stays usable.currency_accountsplugin base accountaccount.is_valid, falls back toEquity:CurrencyAccounts"Equity:CA:é"createdEquity:CA:é:USDaccount_*optionsis_valid_leaf"Conv:😀","Rounding:éclair","Conv😀"acceptedInvalid leaf account namename_*optionsis_valid_root"A😀"acceptedInvalid root account nameEach verdict was checked against beancount 3.2.3. Accepted on both sides:
Equity:٣,Equity:CA:٣x, leaves٣,Conv:٣,Conv:A-b, and rootsÉquité,Ünï,É-1. Rejected on both sides: the values in the table, plusEquity:CA:😀,Equity:C😀andConv:A_b. Differs only through #816:Equity:CA:日本, leaf日本and root日本.The other callers take accounts as ledger text, where beancount uses the grammar check, so they keep
is_valid_account_name: the validator, FFIentry.create, a budget's quoted account and the CLI'slint --account. beancount callsis_valid_leafandis_valid_rootonly from those options, andis_validfrom that plugin and fromaccount.walk, which document discovery uses (see round 2).The LSP's word boundaries were widened in this round; round 2 replaced that approach (see below).
The new tests (strict predicate, plugin, both option kinds, LSP word) each fail with the previous call site. I merged main for #2898, which took WASM cache v85, so this PR moved to WASM v86 then (after #2909, it is loader v66 and WASM v87 with the pin at 66; each version has one note).
Review round 2
option "documents"). beancount'sfind_documentswalks the tree withaccount.walk, which yields only directories whose path passesaccount.is_valid(after NFKC normalization) and skips every other directory silently. Before this PR no such directory could match an opened account; now one can (Assets:A:éclair).document_discovery.rsnow takes a directory only whenis_valid_account_name_strictholds for its account path, and otherwise skips it with no document and no warning, as beancount does. The testdocument_discovery_takes_only_strictly_valid_account_directoriesopensAssets:A:éclair,Assets:A:B😀,Assets:٣:éandAssets:Ok, with a dated file under each and underAssets/misc. bean-check finds 1 Document (Assets:Ok) with no diagnostics, and so does rledger now. Before this round rledger found 4 and also warned aboutAssets:misc.rledger doctor directories(cmd/doctor/directories.rs:61) uses its own "first character is uppercase" heuristic instead of this rule.is_word_chartreated every non-ASCII character as a word character, so in prose、,。, NBSP and curly quotes joined words. It is back to its old letters/digits/:/-/_rule. Whole accounts now come from the parser's token span: a newParseResult::account_at(the account counterpart ofcurrency_at) is preferred inget_symbol_at_position, and call and type hierarchy now go throughget_symbol_at_positiontoo. I dropped round 1'sis_account_name_char, since nothing uses it now. New tests cover a wholeAssets:A😀̈Bsymbol,"昼食、渋谷"(both the symbol and the selection range give昼食) and; from Assets:Cash、Assets:Bank(givesAssets:Cash). The two prose tests fail with round 1'sis_word_char.options.rs.name_*/account_*option values still being applied is tracked in fix(loader): an invalid name_* or account_* option value is applied; beancount keeps the default #2911 and left out of this PR.Review round 3
account.walk(significant). The account path built from the directory names is now NFKC-normalized (unicode-normalization, already a workspace dependency) before the strict check. A decomposedLiabilities/Ünï(as macOS stores it) and a fullwidthExpenses/Ok(Expenses:Ok) are found, as in beancount. Round 2 had dropped them silently; on main they only warned. The loader test now covers both cases. bean-check findsAssets:Ok,Expenses:OkandLiabilities:Ünïwith no diagnostics, and rledger matches. The [Bug]: rledger format error on Unicode accounts #816 divergence is noted at the site and pinned in the test:Assets/日本is discovered here (withopen Assets:日本), while beancount skips it.:but stopped at an emoji or mark, soopen Assets:A😀Bat 0:16 gave16-24 < 16-22 < 16-27. For an account made only of word characters, the scan took the whole token and added no level, so ASCII behavior is unchanged. A new test asserts that every range contains the one before it, at five cursor positions.rustledger-wasm'sget_symbol_at_positionnow triesaccount_atfirst, thencurrency_at, then the word scan, as the LSP does. It has a test forAssets:A😀̈Bat every column.open Assets:Bank:givesAssets:Bankat columns 16, 27 and 28. Before, column 28 gaveAssets:Bank:from the word scan.account_atusespartition_point. A parser test pins thataccount_occurrencesare sorted and non-overlapping, and that the binary search agrees with a linear scan at every byte of a ledger with accounts in every position.Assets/A/éclair) are not discovered; NFD and fullwidth names are matched; and lowercase, space and underscore directories lost the rledger-only [Compat]: on-disk documents posted to non-existent accounts trigger "Invalid reference to unknown account" #1434 warning and are now silent, as in beancount. The option documents: discovered documents on accounts only auto_accounts opens are dropped #2649 entry's "skipped with a warning" now says it applies only to strictly valid names (and its em-dash is now a colon).Each new test fails without its fix.
How to test
New tests, each of which fails without the lexer change:
is_valid_account_name_matches_beancount(parser)test_e1005_matches_beancount_on_non_ascii_component_starts(validate)*_non_ascii_digit_*LSP testsThe full workspace tests, clippy
-D warnings,cargo doc -Dwarnings,cargo fmt --check, the STRICT baselines and the harness--checkall pass.Closes #2906
🤖 Generated with Claude Code