Repository navigation
feat(parser): #1262 phase 2.1b TRANSACTION header recognition + multi-line body - #1274
Merged
Merged
Conversation
…-line body Phase 2.1b of the parser-CST migration. PR 2.1a recognized 14 single-line directive shapes; this PR adds TRANSACTION, the only multi-line directive whose body is structurally distinct from metadata (postings, intra-transaction metadata, indented comments all coexist). Body content STRUCTURE (POSTING / AMOUNT / COST_SPEC / META_ENTRY sub-nodes) lands in PR 2.2; this PR just wraps the full multi-line span in TRANSACTION. ## What this PR ships - `TRANSACTION` SyntaxKind variant added. - Trigger: `DATE WHITESPACE (STAR | PENDING_KW | FLAG | TXN_KW)` where STAR is `*` (completed), PENDING_KW is `!` (incomplete), FLAG is the letter-flag set (P/S/T/C/U/R/M/?/&), TXN_KW is the explicit `txn` keyword. - New `emit_transaction_body` consumes header through terminator NEWLINE then keeps consuming ANY indented sub-line (`WS` + non-`NEWLINE`) until a blank line, non-indented top-level token, or EOF. Looser than the single-line-directive body which only accepts `WS META_KEY` (and gated indented comments). - `identify_single_line_directive` renamed to `identify_directive` and extended to dispatch DATE+STAR/PENDING_KW/FLAG/TXN_KW into TRANSACTION. - The parse loop branches on `directive_kind == TRANSACTION` to pick the body handler. - 9 new source-driven integration tests covering: STAR/PENDING/ TXN_KW header forms, header-only and full multi-line shapes, postings + metadata, payee + narration + tag + link, termination at next top-level / blank line, indented comment between postings, EOF unterminated. - `recognized_and_passthrough_can_coexist` updated: OPEN + TRANSACTION + CLOSE are now ALL recognized (only OPTION still passes through). - `dump_top_level_directives` example extended with TRANSACTION label so `scripts/compat-treesitter.sh` cross-checks it. - Closed-form partition test + `nodes_are_not_tokens` test updated. - Corpus baseline regenerated. 691 of the 714 main-corpus files have IDENTICAL token_seq AND source vs main; the remaining 23 files differ only because upstream `beanahead` repo pushed changes (verified by source_hash differences correlating with token_seq differences). For every file where the source content didn't change, token_seq is byte-identical — the phase-2 contract holds. ## What this PR does NOT add - POSTING / AMOUNT / COST_SPEC / PRICE_ANNOTATION / META_ENTRY sub-node structure inside TRANSACTION (and the other dated directives). That's PR 2.2. - OPTION / INCLUDE / PLUGIN / CUSTOM edge directives (PR 2.3). - Error recovery + broken-corpus differential (PR 2.4). ## Verified - 45 structured-directive tests + 171 unit + 4+1+1+55+2 doctest pass. - cargo check workspace, clippy -D warnings, fmt check, deny check bans, RUSTDOCFLAGS=-Dwarnings cargo doc — all clean. - STRICT_BASELINE=1 cargo test ... cst_baseline passes against the regenerated manifest. Refs #1262. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the phase-2 structured CST builder to recognize TRANSACTION directives and to wrap their multi-line bodies (indented postings/metadata/comments) inside a single node, aligning transaction handling with the Directive-Terminator Rule’s multi-line ownership behavior.
Changes:
- Add
SyntaxKind::TRANSACTIONnode kind and expose it in kind partition tests and the directive-dumping example. - Extend structured parsing dispatch to recognize transaction headers and consume transaction bodies spanning subsequent indented lines.
- Add/update structured-directive integration tests to cover transaction wrapping and termination behavior.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| crates/rustledger-parser/src/cst/parser.rs | Adds TRANSACTION recognition and multi-line body emission in the structured CST parser. |
| crates/rustledger-parser/src/cst/syntax_kind.rs | Introduces the TRANSACTION node kind and updates SyntaxKind tests/docs accordingly. |
| crates/rustledger-parser/tests/structured_directives.rs | Replaces pass-through expectations with TRANSACTION recognition + body/termination integration tests. |
| crates/rustledger-parser/examples/dump_top_level_directives.rs | Adds TRANSACTION to the top-level directive label output for cross-validation tooling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
📊 Benchmark Results
vs main branch (2026-06-06)
What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
… txn flags + 4 review tests Deep review on v1 surfaced 4 findings: 2 real false negatives in identify_directive (HASH and single-char CURRENCY missing as transaction flags) and 2 documentation gaps (blank-line termination and trailing-comment policy weren't pinned by tests). All four addressed here. ## Finding 1: HASH not accepted as transaction flag Token::is_txn_flag (logos_lexer.rs:317) treats `#` as a transaction flag, and the AST parser's parse_flag accepts Hash. v1's identify_directive omitted HASH from the DATE-followed-by-flag arm, so `2024-01-15 # "narr"\n Assets:Cash 100\n` fell into the unrecognized-line branch and the posting orphaned. Fixed by adding SyntaxKind::HASH to the match. ## Finding 2: single-char CURRENCY not accepted as transaction flag NYSE/NASDAQ-style ticker letters (T, V, F, X, ...) lex as CURRENCY (priority 3 in logos_lexer.rs:112 wins over FLAG). The AST parser's parse_flag has an explicit arm `Token::Currency(s) if s.len() == 1 => parse_flag(stream)` (parser.rs:1164). v1's identify_directive omitted this case. Fixed by adding a CURRENCY arm gated on `tokens[j].1.len() == 1` (the lexeme length from the token's range). Guard test `transaction_multi_char_currency_after_date_is_not_a_flag` pins that real currencies like `USD` (length 3) do NOT trigger TRANSACTION recognition. ## Finding 3: blank line mid-transaction orphans postings Documented in the body predicate's rustdoc but no test pinned the behavior. Added `transaction_blank_line_inside_body_terminates_and_orphans_subsequent_postings` that locks in the contract — PR 2.2's POSTING wrapping must NOT widen the body scope across blank lines, since that would change the orphaning behavior and silently break existing parser users. ## Finding 4: trailing indented comment at EOF absorbed into TRANSACTION (inconsistent with rule 4) TRANSACTION's looser body predicate accepts WS+COMMENT unconditionally, whereas the single-line-directive path is more careful (uses upcoming_indented_block_has_meta gating). For transactions this is defensible — documentation comments interleaved with postings are common — but the inconsistency with rule 4 on other directives wasn't pinned. Added `transaction_trailing_indented_comment_at_eof_stays_inside` plus a rustdoc comment in is_indented_transaction_body_line explaining the deliberate divergence. ## What changed in this commit - parser.rs: HASH and single-char-CURRENCY added to the identify_directive DATE+flag arm; rustdoc comment added to is_indented_transaction_body_line explaining the rule-4 divergence. - structured_directives.rs: 5 new tests (HASH flag, single-char CURRENCY flag, multi-char CURRENCY guard, blank-line termination, trailing comment absorption). 50 tests total now (was 45). - cst-corpus.manifest: regenerated. Any corpus file with a `#` or single-char-CURRENCY transaction now wraps the transaction; the node_shape hash on those files changes accordingly. ## Verified - 50 structured-directive tests + 171 unit + 4 + 1 + 1 + 55 + 2 doctest pass. - cargo check workspace, clippy -D warnings, fmt check, deny check bans, RUSTDOCFLAGS=-Dwarnings cargo doc — all clean. - STRICT_BASELINE=1 cargo test ... cst_baseline passes against the regenerated manifest. Refs #1262. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…from copilot review Four Copilot inline review comments on PR #1274, all addressed: 1. identify_directive missed STRING (implied-txn shorthand). The legacy AST parser at parser.rs:1713 dispatches Token::String(_) to parse_transaction_directive with an implied star flag, so 2024-01-15 "Coffee" is a valid transaction. v2 omitted this, causing the line and any postings to fall through as flat passthrough. Added SyntaxKind::STRING to the transaction-trigger arm; pinned by transaction_with_implied_flag_via_bare_string. 2. identify_directive rustdoc listed STAR / FLAG / TXN_KW but missed PENDING_KW (and now STRING/HASH/single-char CURRENCY). Updated to enumerate the full set with a cross-ref to the legacy AST parser. 3. TRANSACTION SyntaxKind variant comment said STAR / FLAG / TXN_KW but the parser also handles PENDING_KW, HASH, STRING, and single-char CURRENCY. Updated to match. 4. transaction_with_exclamation_flag had an inaccurate comment calling ! a FLAG token (it is actually PENDING_KW per logos_lexer.rs) and the test did not assert the token sequence. Renamed to transaction_with_pending_kw_flag, corrected the comment, and added exact-shape tok_seq assertion to catch token-classification regressions. 51 tests + 171 unit + 4+1+1+55+2 doctest pass. Manifest regenerated; strict mode passes. Refs #1262. Co-Authored-By: Claude Opus 4.7 (1M context) <[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.
Summary
Phase 2.1b of #1262. PR 2.1a (#1272) recognized the 14 single-line directive shapes; this PR adds TRANSACTION — the only multi-line directive whose body is structurally distinct from metadata (postings, intra-transaction metadata, indented comments all coexist).
What this PR ships
TRANSACTIONSyntaxKind variant added.DATE WHITESPACE (STAR | PENDING_KW | FLAG | TXN_KW):STAR=*(completed transactions)PENDING_KW=!(incomplete, warning state)FLAG= single-letter flagsP / S / T / C / U / R / M / ? / &TXN_KW= explicittxnkeywordemit_transaction_body— consumes header through terminator NEWLINE, then keeps consuming ANY indented sub-line (WS+ non-NEWLINE) until a blank line, non-indented top-level token, or EOF. Looser body shape thanemit_directive_body(which only acceptsWS + META_KEY+ gated indented comments).identify_single_line_directive→identify_directiverename and extension to dispatch DATE + transaction-trigger into TRANSACTION.transaction_with_star_flag_header_onlytransaction_with_exclamation_flagtransaction_with_txn_keywordtransaction_with_postings_wraps_full_multi_line_bodytransaction_with_metadata_and_postingstransaction_with_payee_and_narrationtransaction_terminates_at_next_top_level_directivetransaction_terminates_at_blank_line_before_next_directivetransaction_with_indented_comment_between_postingstransaction_unterminated_at_eof_with_postingsrecognized_and_passthrough_can_coexistupdated: OPEN + TRANSACTION + CLOSE all recognized now (only OPTION still passes through, until PR 2.3).dump_top_level_directivesexample extended withTRANSACTIONlabel soscripts/compat-treesitter.shcross-validates it.nodes_are_not_tokenstest updated.Corpus baseline contract
Regenerated. The phase-2 contract says token_seq stays stable across structural PRs. Verified:
beanaheadrepo pushed changes since the last regen). For every file where source content didn't change, token_seq is byte-identical.The contract holds: this PR only added structural wrapping, no token classification change.
What this PR does NOT add
Verified
cargo check --workspace --all-features --all-targetsclean.cargo clippy -p rustledger-parser --all-features --examples --tests -- -D warningsclean.cargo fmt --all --checkclean.cargo deny check bansclean.RUSTDOCFLAGS=-Dwarnings cargo doc -p rustledger-parser --no-depsclean.STRICT_BASELINE=1 cargo test -p rustledger-parser --test cst_baselinepasses.Refs #1262.
🤖 Generated with Claude Code