Skip to content

feat(parser): #1262 phase 2.1b TRANSACTION header recognition + multi-line body - #1274

Merged
robcohen merged 3 commits into
mainfrom
feat/cst-phase-2-1b-transaction-header
Jun 6, 2026
Merged

robcohen merged 3 commits into
mainfrom
feat/cst-phase-2-1b-transaction-header

Conversation

@robcohen

@robcohen robcohen commented Jun 6, 2026

Copy link
Copy Markdown
Member

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).

Scope: TRANSACTION wrapper + multi-line body span. POSTING / AMOUNT / COST_SPEC / PRICE_ANNOTATION / META_ENTRY sub-node structure inside it is PR 2.2.

What this PR ships

  • TRANSACTION SyntaxKind variant added.
  • Trigger pattern: DATE WHITESPACE (STAR | PENDING_KW | FLAG | TXN_KW):
    • STAR = * (completed transactions)
    • PENDING_KW = ! (incomplete, warning state)
    • FLAG = single-letter flags P / S / T / C / U / R / M / ? / &
    • TXN_KW = explicit txn keyword
  • 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 body shape than emit_directive_body (which only accepts WS + META_KEY + gated indented comments).
  • identify_single_line_directive → identify_directive rename and extension to dispatch DATE + transaction-trigger into TRANSACTION.
  • 9 new source-driven integration tests:
    • transaction_with_star_flag_header_only
    • transaction_with_exclamation_flag
    • transaction_with_txn_keyword
    • transaction_with_postings_wraps_full_multi_line_body
    • transaction_with_metadata_and_postings
    • transaction_with_payee_and_narration
    • transaction_terminates_at_next_top_level_directive
    • transaction_terminates_at_blank_line_before_next_directive
    • transaction_with_indented_comment_between_postings
    • transaction_unterminated_at_eof_with_postings
  • recognized_and_passthrough_can_coexist updated: OPEN + TRANSACTION + CLOSE all recognized now (only OPTION still passes through, until PR 2.3).
  • dump_top_level_directives example extended with TRANSACTION label so scripts/compat-treesitter.sh cross-validates it.
  • Closed-form partition test + nodes_are_not_tokens test updated.

Corpus baseline contract

Regenerated. The phase-2 contract says token_seq stays stable across structural PRs. Verified:

  • 691 of 714 main-corpus files have IDENTICAL token_seq AND source vs main.
  • 23 files differ — and in every case, the source_hash also differs (upstream beanahead repo 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

  • POSTING / AMOUNT / COST_SPEC / PRICE_ANNOTATION / META_ENTRY sub-node structure inside TRANSACTION (and the other dated directives) — 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 --all-features --all-targets clean.
  • cargo clippy -p rustledger-parser --all-features --examples --tests -- -D warnings clean.
  • cargo fmt --all --check clean.
  • cargo deny check bans clean.
  • RUSTDOCFLAGS=-Dwarnings cargo doc -p rustledger-parser --no-deps clean.
  • STRICT_BASELINE=1 cargo test -p rustledger-parser --test cst_baseline passes.

Refs #1262.

🤖 Generated with Claude Code

…-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]>
@robcohen
robcohen requested a review from a team as a code owner June 6, 2026 06:49
Copilot AI review requested due to automatic review settings June 6, 2026 06:49
@codecov

codecov Bot commented Jun 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::TRANSACTION node 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.

@github-actions

github-actions Bot commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 35.2ms ± 0.3ms 25.3 MB
beancount 749.2ms ± 7.2ms 50.7 MB
Comparison 21.3x faster 2.0x less

vs main branch (2026-06-06)

Metric main this PR Change
Time 35.8ms 35.2ms -1.7% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 2 commits June 6, 2026 09:03
… 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]>
@robcohen
robcohen enabled auto-merge (squash) June 6, 2026 08:15
@robcohen
robcohen merged commit ae22b56 into main Jun 6, 2026
40 checks passed
@robcohen
robcohen deleted the feat/cst-phase-2-1b-transaction-header branch June 6, 2026 08:15
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.

2 participants