feat(codegen): expose structured generation diagnostics - #294
Conversation
Retain non-fatal grammar compiler diagnostics on Generation instead of exposing them only as rendered warning strings. Preserve each diagnostic's exact primary UTF-8 byte span so library consumers can select by code and recover the source subject without parsing human-readable messages. Keep antlr4-rust-gen stderr and Generation::warnings() unchanged, document the new API, and cover G4S078 rule-name recovery through Builder. Fixes #293
Copy/Paste DetectionNo duplications found in 9 changed non-generated Rust file(s) (threshold: 100 tokens). |
📝 WalkthroughWalkthroughSuccessful generation now exposes structured compiler diagnostics with codes, severities, paths, positions, and UTF-8 byte spans. Existing rendered warning messages remain available. Tests validate empty diagnostics and unreachable-rule diagnostics. ChangesStructured generation diagnostics
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Builder
participant Compiler
participant Generation
Builder->>Compiler: generate grammar
Compiler-->>Builder: diagnostics with source spans
Builder->>Generation: store diagnostics and rendered warnings
Generation-->>Builder: expose diagnostics()
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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 `@crates/antlr-rust-codegen/tests/antlr4_rust_gen_cli/multi_recognizer.rs`:
- Around line 169-172: Add the missing external Insta snapshot for the
builder_structured_non_fatal_diagnostics_with_exact_spans assertion in the
multi-recognizer tests, using the generated output and the expected
multi_recognizer__builder_structured_non_fatal_diagnostics_with_exact_spans.snap
filename.
🪄 Autofix
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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 2d28c9b3-f9ba-4a92-ac1a-34c25300ed31
⛔ Files ignored due to path filters (1)
crates/antlr-rust-codegen/tests/antlr4_rust_gen_cli/snapshots/antlr4_rust_gen_cli__multi_recognizer__builder_structured_non_fatal_diagnostics_with_exact_spans.snapis excluded by!**/*.snap
📒 Files selected for processing (6)
README.mdcrates/antlr-rust-codegen/README.mdcrates/antlr-rust-codegen/src/artifact.rscrates/antlr-rust-codegen/src/driver.rscrates/antlr-rust-codegen/src/error.rscrates/antlr-rust-codegen/tests/antlr4_rust_gen_cli/multi_recognizer.rs
|
I'll analyze this and get back to you. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 12c1974fad
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
📊 Source Code Metrics (this PR vs
|
| File | Cyclomatic | Cognitive | Functions | LLOC | MI |
|---|---|---|---|---|---|
| crates/antlr-rust-codegen/src/grammar/semantics.rs | 341 (main: 343) 🟢 | 314 ⚪ | 73 ⚪ | 553 ⚪ | 0 ⚪ |
| crates/antlr-rust-codegen/src/grammar/compiler.rs | 62 (main: 63) 🟢 | 22 ⚪ | 19 ⚪ | 86 ⚪ | 5.34 (main: 5.10) 🟢 |
| crates/antlr-rust-codegen/src/error.rs | 30 (main: 29) 🔴 | 6 ⚪ | 17 (main: 16) 🔴 | 25 (main: 24) 🔴 | 21.39 (main: 22.40) 🔴 |
| crates/antlr-rust-codegen/src/grammar/diagnostic.rs | 21 (main: 16) 🔴 | 4 (main: 3) 🔴 | 11 (main: 8) 🔴 | 22 (main: 19) 🔴 | 22.96 (main: 26.30) 🔴 |
| crates/antlr-rust-codegen/src/artifact.rs | 33 (main: 32) 🔴 | 18 ⚪ | 10 (main: 9) 🔴 | 35 (main: 34) 🔴 | 23.31 (main: 24.35) 🔴 |
| crates/antlr-rust-codegen/src/driver.rs | 74 (main: 69) 🔴 | 52 ⚪ | 7 (main: 5) 🔴 | 107 (main: 98) 🔴 | 6.68 (main: 8.91) 🔴 |
| crates/antlr-rust-codegen/src/grammar/validation.rs | 44 (main: 45) 🟢 | 50 ⚪ | 3 ⚪ | 41 ⚪ | 21.08 (main: 20.67) 🟢 |
Generated by mehen v1.8.0 — the code quality watcher.
Track whether compiler diagnostic primary spans came from source text instead of inferring provenance from a resolvable SourceId. Source-less loader and invariant errors keep their existing display fallback but no longer expose fabricated line, column, or byte ranges through Builder. Cover a valid first root followed by a missing second root while retaining exact G4S078 spans for authored diagnostics.

Summary
Generation::diagnostics()Diagnosticprimary subject as an exact half-open UTF-8 byte spanSourceIdantlr4-rust-genstderr andGeneration::warnings()unchangedG4S078rule-name recovery throughBuilderWhy
Library consumers could previously inspect structured diagnostics only when generation failed. Successful analysis warnings were reduced to CLI display strings, forcing callers to parse prose to recover subjects such as unreachable parser rule names.
The successful and failed paths now share the same public diagnostic shape, so callers can filter by code and slice the reported source path at
byte_span(). Internal diagnostics also record whether their primary span came from source text: source-less loader and invariant errors returnNonefor structured coordinates even when their synthetic fallbackSourceIdhappens to resolve.Closes #293
Compatibility
This changes only the
antlr-rust-codegenlibrary API. Generated Rust and the runtime interface are unchanged, so the generated-code API revision remains unchanged.Validation
cargo fmt --all -- --checkcargo test --locked --workspace --all-featurescargo clippy --locked --workspace --all-targets --all-features -- -D warningsSummary by CodeRabbit
New Features
Documentation