Skip to content

Introduce macro for AstNode impls#78

Merged
leynos merged 3 commits intomainfrom
codex/add-private-trait-astnode-to-parser
Jul 18, 2025
Merged

Introduce macro for AstNode impls#78
leynos merged 3 commits intomainfrom
codex/add-private-trait-astnode-to-parser

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jul 17, 2025

Summary

  • expose AstNode with documentation
  • generate AstNode impls via impl_ast_node! macro
  • disable strict MD029 and MD039 rules so docs format cleanly

https://chatgpt.com/codex/tasks/task_e_68782d2339148322a35ac82c9a5cf93d

Summary by Sourcery

Introduce AstNode trait and impl_ast_node! macro to unify and simplify AST wrapper implementations, remove redundant syntax() methods, relax markdownlint rules for documentation, and update tests to use the new trait and correct language reference.

New Features:

  • Add AstNode trait to expose underlying SyntaxNode for AST wrappers
  • Introduce impl_ast_node! macro to auto-generate AstNode implementations

Enhancements:

  • Replace manual syntax() methods on AST wrapper types with macro invocations
  • Relax markdownlint MD029 and MD039 rules for cleaner documentation

Tests:

  • Import AstNode in parser tests and update pretty_print to use crate::DdlogLanguage

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jul 17, 2025

Reviewer's Guide

This PR centralizes AST node access by introducing the AstNode trait and an accompanying impl_ast_node! macro to replace repetitive syntax() methods across multiple wrapper types, updates related tests to align with the new trait and language path, and disables strict markdownlint rules for cleaner docs.

Class diagram for AstNode trait and macro-based implementations

classDiagram
    class AstNode {
        +syntax() : &SyntaxNode<DdlogLanguage>
    }
    class Import {
        +syntax: SyntaxNode<DdlogLanguage>
        +... // other methods
    }
    class TypeDef {
        +syntax: SyntaxNode<DdlogLanguage>
        +... // other methods
    }
    class Relation {
        +syntax: SyntaxNode<DdlogLanguage>
        +... // other methods
    }
    class Index {
        +syntax: SyntaxNode<DdlogLanguage>
        +... // other methods
    }
    class Rule {
        +syntax: SyntaxNode<DdlogLanguage>
        +... // other methods
    }
    class Function {
        +syntax: SyntaxNode<DdlogLanguage>
        +... // other methods
    }
    class Transformer {
        +syntax: SyntaxNode<DdlogLanguage>
        +... // other methods
    }
    AstNode <|.. Import
    AstNode <|.. TypeDef
    AstNode <|.. Relation
    AstNode <|.. Index
    AstNode <|.. Rule
    AstNode <|.. Function
    AstNode <|.. Transformer
    note for AstNode "Trait introduced to centralize syntax node access. Implemented via macro for each AST wrapper type."
Loading

File-Level Changes

Change Details Files
Define new AstNode trait and impl_ast_node! macro with documentation
  • Add AstNode trait exposing syntax()
  • Define impl_ast_node! macro to generate trait implementations
  • Annotate trait with docs and cfg_attr for tests
src/parser/mod.rs
Replace manual syntax() methods with macro invocations for AST wrappers
  • Remove individual fn syntax() implementations from Import, TypeDef, Relation, Index, Rule, Function, Transformer
  • Insert impl_ast_node!(...) for each wrapper type
src/parser/mod.rs
Update parser tests to use new trait and correct language path
  • Change pretty_print to reference crate::DdlogLanguage
  • Import AstNode in parse_utils tests
src/parser/tests/parser.rs
src/parser/ast/parse_utils.rs
Adjust markdownlint configuration to disable MD029 and MD039
  • Disable strict list and code block rules in markdownlint config
.markdownlint-cli2.jsonc

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 17, 2025

Summary by CodeRabbit

  • Documentation

    • Added explanatory comments to the configuration file for markdown linting rules.
  • Refactor

    • Updated test code to use local crate references instead of external ones, improving consistency.
    • Adjusted visibility of an internal trait to limit its exposure within the crate.

Walkthrough

Update the .markdownlint-cli2.jsonc configuration with clarifying comments for disabled rules. Restrict the visibility of the AstNode trait in the parser module to pub(crate) and annotate it for dead code warnings outside of tests. Refactor test imports and type references in parser tests to use local crate paths and type aliases.

Changes

File(s) Change Summary
.markdownlint-cli2.jsonc Added explanatory comments to rule exceptions (MD029, MD039); no rule logic changed.
src/parser/mod.rs Changed AstNode trait visibility from pub to pub(crate); added dead code annotation; added mod parser; to tests.
src/parser/tests/parser.rs Replaced external ddlint imports with local crate imports; introduced type aliases; updated usage accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant TestModule as tests::parser
    participant Parser as crate::parser
    participant AST as crate::parser::ast

    TestModule->>Parser: Use parser functionality
    Parser->>AST: Access AstNode trait (now pub(crate))
    AST-->>Parser: Provide AST node logic (internal)
    TestModule->>AST: Access AstNode for tests (permitted by crate visibility)
Loading

Possibly related PRs

Poem

In the parser’s code, a subtle shift,
Visibility wrapped, a tidy gift.
Imports now local, no more roam,
Comments explain why rules stay home.
Tests align, the crate feels neat—
A codebase marching on its feet!
🦀✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 54c3eda and 181e7a0.

📒 Files selected for processing (3)
  • .markdownlint-cli2.jsonc (1 hunks)
  • src/parser/mod.rs (2 hunks)
  • src/parser/tests/parser.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
⚙️ CodeRabbit Configuration File

🔇 Additional comments (5)
.markdownlint-cli2.jsonc (1)

10-11: Well-documented rule exceptions.

The inline comments clearly explain the rationale for disabling MD029 and MD039, which helps future maintainers understand the intent and prevents inadvertent re-enabling.

src/parser/mod.rs (1)

1350-1350: Good test organisation.

Adding the parser submodule properly organises the test structure and aligns with the test file structure.

src/parser/tests/parser.rs (3)

7-11: Excellent crate-local import organisation.

The shift from external ddlint imports to local crate references properly aligns with the internal crate structure and the AstNode trait's restricted visibility.


14-15: Type aliases dramatically improve readability.

The concise SyntaxNode and SyntaxElement aliases eliminate the verbose rowan::SyntaxNode<crate::DdlogLanguage> repetition throughout the test code, making it much easier to scan and understand.


23-31: Clean usage of type aliases.

The consistent application of the type aliases in the pretty_print function demonstrates the readability improvement in practice.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/add-private-trait-astnode-to-parser

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @leynos - I've reviewed your changes and found some issues that need to be addressed.

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/parser/mod.rs:404` </location>
<code_context>
+    /// let import = parsed.root().imports().first().unwrap();
+    /// assert_eq!(import.syntax().kind(), SyntaxKind::N_IMPORT_STMT);
+    /// ```
+    #[cfg_attr(not(test), expect(dead_code, reason = "only used in tests"))]
+    // This trait is used exclusively in tests to inspect AST nodes.
+    pub(crate) trait AstNode {
</code_context>

<issue_to_address>
The use of `expect(dead_code)` here may be misleading.

Since `AstNode` is used in the main codebase and not just in tests, the `expect(dead_code)` attribute with this reason may cause confusion. Please clarify the attribute's purpose or remove it if the trait is used outside of tests.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
    #[cfg_attr(not(test), expect(dead_code, reason = "only used in tests"))]
    // This trait is used exclusively in tests to inspect AST nodes.
    pub(crate) trait AstNode {
=======
    // This trait is used to inspect AST nodes and is utilized in both the main codebase and tests.
    pub(crate) trait AstNode {
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/parser/mod.rs Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

♻️ Duplicate comments (1)
src/parser/mod.rs (1)

404-407: Clarify the expect(dead_code) attribute reasoning.

The attribute reason states the trait is "primarily exercised through test modules", but since AstNode is implemented across multiple AST wrapper types and appears to be part of the public interface, this reasoning may be misleading.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 80b863a and 84b5c31.

📒 Files selected for processing (4)
  • .markdownlint-cli2.jsonc (1 hunks)
  • src/parser/ast/parse_utils.rs (1 hunks)
  • src/parser/mod.rs (8 hunks)
  • src/parser/tests/parser.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • AGENTS.md

⚙️ CodeRabbit Configuration File

🔇 Additional comments (5)
src/parser/mod.rs (3)

415-423: Excellent macro design for eliminating code duplication.

The macro effectively unifies the AstNode trait implementation across all AST wrapper types, replacing repetitive manual implementations.


662-662: Clean refactoring using the macro consistently across all AST types.

The consistent application of impl_ast_node! across all AST wrapper types successfully eliminates code duplication whilst providing a unified interface.

Also applies to: 708-708, 960-960, 1083-1083, 1177-1177, 1285-1285, 1345-1345


1350-1350: Appropriate test module organisation.

Adding the parser test module supports the updated trait interface testing.

src/parser/ast/parse_utils.rs (1)

664-664: Necessary import for updated test interface.

The AstNode trait import correctly supports the refactored test code that uses the unified trait interface.

src/parser/tests/parser.rs (1)

7-11: Imports updated correctly

Switching to crate::… keeps the tests in-crate and removes the external dependency. No further action required.

Comment thread .markdownlint-cli2.jsonc Outdated
Comment thread src/parser/tests/parser.rs Outdated
@leynos leynos force-pushed the codex/add-private-trait-astnode-to-parser branch from 54c3eda to 181e7a0 Compare July 18, 2025 17:44
@leynos leynos merged commit 21747b9 into main Jul 18, 2025
2 checks passed
@leynos leynos deleted the codex/add-private-trait-astnode-to-parser branch July 18, 2025 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant