Skip to content

feat(loomgen): #loom.line_pattern annotation for line-oriented block-level token production#698

Merged
dowdiness merged 5 commits into
mainfrom
feat/561-line-pattern
Jul 11, 2026
Merged

feat(loomgen): #loom.line_pattern annotation for line-oriented block-level token production#698
dowdiness merged 5 commits into
mainfrom
feat/561-line-pattern

Conversation

@dowdiness

@dowdiness dowdiness commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Summary

Implements #561: #loom.line_pattern annotation for line-oriented block-level token production.

What

  • #loom.line_pattern("regex") on token variants: marks the variant as a line-level pattern candidate. Matched against the rest-of-line (pos→newline), not character-by-character.
  • #loom.line_mode on #loom.lexmode term variants: declares the mode as line-oriented. Each such mode gets a generated lexer function that tries #loom.line_pattern tokens in declaration order.
  • Payload extraction via #loom.custom_lex: pattern+payload tokens like HeadingMarker(Int) use a custom_lex function to construct the token from the match text.

Files

New:

  • loomgen/emit_line_lexer.mbt — line-mode lexer code generator
  • loomgen/fixtures/line_pattern_fixture.mbt — test fixture

Modified:

  • parse_annotations.mbt — parse/validate #loom.line_pattern + #loom.line_mode
  • emit_lexer.mbt — exclude line_pattern tokens from character-level lexer
  • emit_syntax_kind.mbt — handle None-role line_pattern tokens
  • emit_token_impls.mbt — handle None-role line_pattern tokens
  • main.mbt--line-lexer CLI flag, preflight, and write

CLI Usage

moon run loomgen --target native -- --line-lexer <path>/line_lexer.g.mbt --term <term.mbt> <token.mbt> <token_out> <syntax_out>

The generated line_lexer.g.mbt contains per-mode functions (e.g., lex_line_start) that follow the same signature as the lexer_skeleton.g.mbt stubs.

Verification

  • 161 loomgen tests (155 existing + 6 new), 3315 project-wide — all pass.
  • No regressions in existing lexer/syntax-kind/grammar-IR generation.

Closes #561

Summary by CodeRabbit

  • New Features

    • Added support for line-pattern tokens and line-mode lexing.
    • Added --line-lexer generation for producing line-based lexer output.
    • Added validation for line-pattern and line-mode annotations, including regex and configuration constraints.
    • Added support for line-pattern tokens without explicit roles, including generated token kinds and display values.
  • Bug Fixes

    • Prevented line-pattern variants from being incorrectly included in character-level lexer generation.
    • Improved collision checks so they apply only to character-level candidates.
  • Tests

    • Added coverage for line lexer generation, matching behavior, modes, escaping, fallbacks, and invalid annotation combinations.

…production (#561)

Adds #loom.line_pattern (on token variants) and #loom.line_mode (on
#loom.lexmode term variants) annotations for generating line-mode lexer
functions. Each #loom.line_mode mode gets a generated lex function that
matches #loom.line_pattern regexes against the current line (pos→newline)
and produces tokens via #loom.custom_lex payload extraction.

New:
- loomgen/emit_line_lexer.mbt — line-mode lexer code generation
- loomgen/fixtures/line_pattern_fixture.mbt — test fixture

Modified:
- parse_annotations.mbt — parse/validate line_pattern + line_mode
- emit_lexer.mbt — exclude line_pattern tokens from char-level lexer
- emit_syntax_kind.mbt — handle None-role line_pattern tokens in kind entries
- emit_token_impls.mbt — handle None-role line_pattern tokens in Show/ToRawKind
- main.mbt — --line-lexer CLI option, preflight, and write

Tests: 161 loomgen tests, 3315 project-wide — all pass.
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@dowdiness, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 18 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 83aedfd7-eb52-45d9-b398-73546349bc0b

📥 Commits

Reviewing files that changed from the base of the PR and between a10fd51 and 5b95eca.

📒 Files selected for processing (7)
  • loomgen/README.md
  • loomgen/emit_lexer.mbt
  • loomgen/emit_lexer_wbtest.mbt
  • loomgen/emit_line_lexer.mbt
  • loomgen/fixtures/line_pattern_fixture.g.mbt
  • loomgen/parse_annotations.mbt
  • loomgen/regenerate_fixtures.mbt
📝 Walkthrough

Walkthrough

Adds #loom.line_pattern and #loom.line_mode parsing, validation, line-mode lexer generation, CLI output support, compatibility with existing token generators, fixtures, and whitebox tests.

Changes

Line-pattern lexer

Layer / File(s) Summary
Annotation parsing and validation
loomgen/parse_annotations.mbt
Adds line_pattern and line_mode metadata and validates their roles, regexes, nullability, lex modes, and custom payload requirements.
Per-mode line lexer generation
loomgen/emit_line_lexer.mbt
Generates ordered line-mode lexer functions that scan the current line, match patterns, construct tokens, and return invalid steps on fallthrough.
Token and character lexer compatibility
loomgen/emit_lexer.mbt, loomgen/emit_syntax_kind.mbt, loomgen/emit_token_impls.mbt
Excludes line-pattern variants from character-level lexer candidates and synthesizes token kind and literal handling for role-less line-pattern variants.
CLI output integration
loomgen/main.mbt
Adds --line-lexer, preflights generation, reports missing annotations, and writes generated output.
Fixtures and validation
loomgen/fixtures/line_pattern_fixture.mbt, loomgen/emit_lexer_wbtest.mbt
Adds line-pattern and line-mode fixtures plus tests for generation, omission cases, escaping, custom lexing, and invalid annotations.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant LineLexerGenerator
  participant EnumMetadata
  participant GeneratedLineLexer
  CLI->>LineLexerGenerator: request --line-lexer output
  LineLexerGenerator->>EnumMetadata: read line_pattern and line_mode annotations
  EnumMetadata-->>LineLexerGenerator: return token and mode metadata
  LineLexerGenerator-->>CLI: return generated lexer content
  CLI->>GeneratedLineLexer: write line_lexer.g.mbt
Loading

Possibly related issues

  • Issue 688: Proposes adding payload handling to the same line-pattern parsing and emission paths introduced here.

Possibly related PRs

  • dowdiness/loom#528: Both changes modify lexer generation around pattern-annotated token variants.
  • dowdiness/loom#641: Both changes modify eligibility and pattern handling in emit_lexer.mbt.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the new #loom.line_pattern support for line-oriented token production and matches the main change.
Description check ✅ Passed The description covers the summary, implementation details, CLI usage, and verification, but omits the template's documentation/ADR checklist.
Linked Issues check ✅ Passed The changes implement the requested line-mode lexer, validation, exclusion from char lexing, and CLI integration for #561.
Out of Scope Changes check ✅ Passed The added fixture, tests, lexer generator, and CLI wiring are all directly related to the line-pattern feature.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/561-line-pattern

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
loomgen/emit_lexer.mbt (1)

57-70: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Line-pattern custom_lex functions leak into character-level lexer hooks.

The first loop in collect_lexer_inputs iterates over all variants (including line_pattern ones) and adds every custom_lex function name to custom_lex_fns. That array is returned unfiltered at line 154, so emit_lexer (lines 257–269) emits character-level hooks for line-pattern custom_lex functions — calling them with (source, pos) instead of the (source, pos, len) signature used by emit_line_lexer, and expecting a LexStep[T] return instead of (LexStep[T], LexMode). This produces a type error in the generated character-level lexer whenever both --lexer and --line-lexer are passed and a line_pattern variant carries #loom.custom_lex.

The second loop (lines 72–76) already skips line_pattern variants; the first loop should do the same.

🐛 Proposed fix: skip `line_pattern` variants in the first loop
   for v in variants {
+    // `#loom.line_pattern` variants are produced by the line-mode lexer, not the
+    // character-level lexer — skip them entirely (mirrors the second loop).
+    if v.line_pattern is Some(_) {
+      continue
+    }
     match v.role {
       Some(Ident) => if v.custom_lex is None { ident_count = ident_count + 1 }
       Some(Keyword(kw)) => keywords.push((kw, v.name))
       _ => ()
     }
     if v.custom_lex is Some(fn_name) {
       custom_lex_fns.push(fn_name)
     }
   }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@loomgen/emit_lexer.mbt` around lines 57 - 70, Update the first variant loop
in collect_lexer_inputs so custom_lex_fns only collects functions from
non-line_pattern variants, matching the filtering already used by the second
loop. Preserve ident counting and keyword collection behavior, and ensure
line-pattern custom lex functions remain available only to emit_line_lexer
rather than character-level hooks.
🧹 Nitpick comments (1)
loomgen/emit_lexer_wbtest.mbt (1)

795-843: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider adding a negative test for the custom_lex requirement on line_pattern variants with constructor arguments.

The upstream validation rejects #loom.line_pattern variants that have constructor arguments but no #loom.custom_lex (parse_annotations.mbt:370-377). The fixture covers the positive case (HeadingMarker(Int) with lex_heading_marker), but no test asserts the rejection when custom_lex is missing. Since custom lex for payload extraction is a key PR feature, a negative test would close this coverage gap.

🧪 Suggested test
///|
test "parse_annotations rejects `#loom.line_pattern` with constructor args but no custom_lex" {
  let src =
    #|`#loom.token`
    #|pub(all) enum T {
    #|  `#loom.line_pattern`("#{1,6} ")
    #|  HeadingMarker(Int)
    #|  `#loom.error`
    #|  Error(String)
    #|  `#loom.eof`
    #|  E
    #|} derive(Eq)
  match parse_annotations(src) {
    Err(msg) => inspect(msg.contains("`#loom.custom_lex`"), content="true")
    Ok(_) => abort("expected rejection")
  }
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@loomgen/emit_lexer_wbtest.mbt` around lines 795 - 843, Add a negative test
alongside the existing parse_annotations validation tests for a
`#loom.line_pattern` variant with constructor arguments but no `#loom.custom_lex`.
Use a payload variant such as HeadingMarker(Int), include the required error and
EOF variants, and assert parse_annotations returns Err with a message containing
"`#loom.custom_lex`".
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@loomgen/emit_line_lexer.mbt`:
- Around line 54-87: Update emit_mode_line_lexer to emit each line-pattern
candidate only when its lexmode_name matches the enclosing mode. In the nullary
token branch, return the candidate’s declared transition target when present
instead of always returning mode_name; preserve mode_name as the fallback when
no target is declared.

---

Outside diff comments:
In `@loomgen/emit_lexer.mbt`:
- Around line 57-70: Update the first variant loop in collect_lexer_inputs so
custom_lex_fns only collects functions from non-line_pattern variants, matching
the filtering already used by the second loop. Preserve ident counting and
keyword collection behavior, and ensure line-pattern custom lex functions remain
available only to emit_line_lexer rather than character-level hooks.

---

Nitpick comments:
In `@loomgen/emit_lexer_wbtest.mbt`:
- Around line 795-843: Add a negative test alongside the existing
parse_annotations validation tests for a `#loom.line_pattern` variant with
constructor arguments but no `#loom.custom_lex`. Use a payload variant such as
HeadingMarker(Int), include the required error and EOF variants, and assert
parse_annotations returns Err with a message containing "`#loom.custom_lex`".
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9dc2de76-6835-416f-ba33-c2f2cf6477c6

📥 Commits

Reviewing files that changed from the base of the PR and between 3f5b62b and a10fd51.

📒 Files selected for processing (8)
  • loomgen/emit_lexer.mbt
  • loomgen/emit_lexer_wbtest.mbt
  • loomgen/emit_line_lexer.mbt
  • loomgen/emit_syntax_kind.mbt
  • loomgen/emit_token_impls.mbt
  • loomgen/fixtures/line_pattern_fixture.mbt
  • loomgen/main.mbt
  • loomgen/parse_annotations.mbt

Comment thread loomgen/emit_line_lexer.mbt
- emit_line_lexer.mbt: filter line-pattern candidates by mode via
  lexmode_name; nullary tokens use declared transition target
- emit_lexer.mbt: exclude line_pattern custom_lex fns from char-level
  lexer collection
- emit_lexer_wbtest.mbt: add negative test for payload+line_pattern
  without #loom.custom_lex
- fixtures/line_pattern_fixture.g.mbt: regenerated golden output
- emit_lexer_wbtest.mbt: golden snapshot, positive test for custom_lex+pattern,
  per-mode filtering test with BlockQuoteLineStart/LineStart mode isolation
- regenerate_fixtures.mbt: add line_pattern fixture regeneration
- README.md: document --line-lexer flag
- emit_line_lexer.mbt: per-mode candidate filtering via lexmode_name,
  nullary tokens use declared transition target
@dowdiness dowdiness merged commit b3eedd5 into main Jul 11, 2026
38 checks passed
@dowdiness dowdiness deleted the feat/561-line-pattern branch July 11, 2026 11:56
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.

loomgen: #loom.line_pattern annotation for line-oriented block-level token production

1 participant