Skip to content

Refactor relation span parsing#72

Merged
leynos merged 6 commits intomainfrom
codex/refactor-collect_relation_spans-for-nested-paren-handling
Jul 16, 2025
Merged

Refactor relation span parsing#72
leynos merged 6 commits intomainfrom
codex/refactor-collect_relation_spans-for-nested-paren-handling

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jul 15, 2025

Summary

  • expose parser::ast::parse_utils to parent module
  • add paren_block_span helper for balanced parentheses
  • parse relation columns and PK clauses using the new helper

Testing

  • make lint
  • make test
  • make markdownlint

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

Summary by Sourcery

Refactor relation span parsing by extracting parentheses handling into a reusable parser helper and replacing manual scanning logic with paren_block_span; expose parse_utils to the parent module

Enhancements:

  • Expose the parse_utils module to its parent AST module
  • Add a paren_block_span parser for balanced parentheses blocks
  • Replace manual parentheses depth-tracking in collect_relation_spans with paren_block_span for relation columns and PK clauses

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 15, 2025

Caution

Review failed

The head commit changed during the review from 0337d58 to 960d0ee.

Summary by CodeRabbit

  • New Features
    • Improved parser handling for parenthesised blocks, now supporting accurate detection of nested and unbalanced parentheses.
  • Bug Fixes
    • Enhanced error reporting when encountering unbalanced parentheses in relation declarations.
  • Tests
    • Added tests to ensure errors are correctly raised and handled for unbalanced parentheses in relations.

Summary by CodeRabbit

  • New Features

    • Improved handling of parenthesised blocks in parsing, including support for nested parentheses.
    • Parser now provides detailed error reporting for unbalanced parentheses in relation declarations.
  • Bug Fixes

    • Parsing errors for unbalanced parentheses in relations are now correctly detected and reported.
  • Tests

    • Added tests to ensure unbalanced parentheses in relation declarations are flagged as errors and not parsed as valid relations.
  • Refactor

    • Updated internal parsing logic for parenthesised blocks to enhance reliability and error reporting.

Walkthrough

Introduce a new parser utility for extracting the span of parenthesised blocks, refactor relation span collection to use this parser, and enhance error collection during relation parsing. Update tests to include a case for unbalanced parentheses in relation declarations, verifying correct error detection and no false positives in recognised relations.

Changes

File(s) Change Summary
src/parser/ast/parse_utils.rs Add paren_block_span parser function; update imports for parser traits and span types.
src/parser/mod.rs Refactor relation span collection to use paren_block_span; accumulate parse errors; update module visibility.
tests/parser.rs Add fixture and test for unbalanced parentheses in relations; assert errors and absence of recognised relations.

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant Parser
    participant ParseUtils
    participant ErrorCollector

    Test->>Parser: parse relation with parentheses
    Parser->>ParseUtils: call paren_block_span()
    ParseUtils-->>Parser: return span or error
    Parser->>ErrorCollector: accumulate errors if any
    Parser-->>Test: return relation spans and errors
Loading

Possibly related PRs

  • Implement relation parser #35: Refactors relation parsing infrastructure, introducing reusable parenthesised block parsing and improved error handling, directly supporting the enhancements in this PR.

Poem

Parentheses gather, some balanced, some wild,
Parsing their secrets, errors compiled.
Spans now returned with a watchful new eye,
Relations more careful, letting no bug slip by.
Testers rejoice, for the errors are caught—
Code grows more clever, as every bug’s fought!
🦀✨

✨ 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/refactor-collect_relation_spans-for-nested-paren-handling

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

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jul 15, 2025

Reviewer's Guide

This PR centralizes parentheses span parsing by exposing and extending parse_utils, introduces a reusable paren_block_span helper built on balanced_block, and updates collect_relation_spans to leverage this helper instead of inline depth tracking.

Class diagram for parser::ast::parse_utils module changes

classDiagram
    class parse_utils {
        +parse_name_type_pairs()
        +parse_output_list()
        +parse_type_after_colon()
        +paren_block_span()  // newly added, public(super)
    }
    class balanced_block
    parse_utils ..> balanced_block : uses
Loading

Class diagram for collect_relation_spans refactor

classDiagram
    class collect_relation_spans {
        +collect_relation_spans(tokens, src)
        // Now uses paren_block_span instead of manual depth tracking
    }
    collect_relation_spans ..> parse_utils : uses paren_block_span()
Loading

File-Level Changes

Change Details Files
Added a reusable paren_block_span parser helper
  • import balanced_block for generic block parsing
  • define paren_block_span that maps balanced_block for parentheses to a Span
  • include Span in crate imports for parse_utils
src/parser/ast/parse_utils.rs
Exposed parse_utils module to parent AST scope
  • change parse_utils visibility to pub(super)
src/parser/ast/mod.rs
Refactored collect_relation_spans to use paren_block_span
  • replace manual depth-based loops with parse_span calls using paren_block_span
  • use skip_until(sp.end) to advance past parsed parentheses
  • apply the new logic to both relation columns and PK clause parsing sites
src/parser/mod.rs

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

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.

  • Consider handling or reporting the parser errors returned by parse_span instead of silently ignoring the _err, so that any malformed parentheses blocks don’t cause silent failures.
  • The pattern of peeking for T_LPAREN, calling parse_span, and then skip_until is repeated—consider extracting it into a dedicated helper on the parser to reduce duplication and improve readability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider handling or reporting the parser errors returned by `parse_span` instead of silently ignoring the `_err`, so that any malformed parentheses blocks don’t cause silent failures.
- The pattern of peeking for `T_LPAREN`, calling `parse_span`, and then `skip_until` is repeated—consider extracting it into a dedicated helper on the parser to reduce duplication and improve readability.

## Individual Comments

### Comment 1
<location> `src/parser/mod.rs:638` </location>
<code_context>
-                    _ => {}
-                }
-                st.stream.advance();
+        if let Some((SyntaxKind::T_LPAREN, span)) = st.stream.peek().cloned() {
+            let (res, _err) = st.parse_span(ast::parse_utils::paren_block_span(), span.start);
+            if let Some(sp) = res {
+                st.stream.skip_until(sp.end);
             }
         }
</code_context>

<issue_to_address>
Consider error handling for parse_span failures.

Ignoring the error from parse_span may cause the stream to stall or parse incorrectly. Please handle the error case to ensure the stream advances or the error is reported.
</issue_to_address>

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
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: 1

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

642-656: Robust error handling in paren block parsing!

The implementation properly collects errors and returns success/failure status, addressing the previous concern about parse_span error handling. The pattern of skipping to line end on failure ensures the parser can continue processing subsequent statements.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0e8a127 and 410255c.

📒 Files selected for processing (3)
  • src/parser/ast/parse_utils.rs (1 hunks)
  • src/parser/mod.rs (4 hunks)
  • 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

🧬 Code Graph Analysis (2)
tests/parser.rs (2)
src/parser/mod.rs (5)
  • parse (358-370)
  • errors (337-339)
  • root (331-333)
  • relations (293-295)
  • relations (1330-1336)
src/parser/ast/parse_utils.rs (1)
  • is_empty (169-171)
src/parser/ast/parse_utils.rs (1)
src/parser/mod.rs (2)
  • balanced_block (190-195)
  • skip_whitespace_and_comments (1582-1592)
🔇 Additional comments (8)
tests/parser.rs (2)

109-112: LGTM - Clean test fixture following established patterns.

The fixture provides appropriate test data for exercising error handling with unbalanced parentheses in relation declarations.


539-544: LGTM - Comprehensive error handling test.

The test properly verifies that unbalanced parentheses produce parsing errors whilst ensuring no relations are incorrectly recognized. This follows the established pattern of similar tests like index_unbalanced_parentheses_is_error.

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

11-11: LGTM - Necessary imports for the new parser function.

The imports correctly bring in the required dependencies for the paren_block_span function.

Also applies to: 13-14

src/parser/mod.rs (5)

390-390: Good error propagation improvement!

The change to return and collect errors from collect_relation_spans aligns well with the error handling pattern used by other span collection functions.

Also applies to: 397-397


630-641: Well-structured error collection approach!

The introduction of the Extras struct cleanly encapsulates the source reference and error collection, improving code organisation.


658-665: Excellent refactoring to use centralised parenthesis parsing!

Both functions now properly indicate success/failure and delegate parenthesis handling to the reusable consume_paren_block function, reducing code duplication and improving maintainability.

Also applies to: 667-683


688-698: Critical correctness improvement!

The function now correctly validates that both columns and primary key clauses parse successfully before recording a relation span. This prevents recording incomplete or malformed relations.


1594-1594: Appropriate visibility change!

Using pub(super) correctly limits the module's visibility to the parent module while enabling the necessary access to paren_block_span.

Comment thread src/parser/ast/parse_utils.rs
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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 410255c and 3f00f26.

📒 Files selected for processing (1)
  • src/parser/ast/parse_utils.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • AGENTS.md

⚙️ CodeRabbit Configuration File

🧬 Code Graph Analysis (1)
src/parser/ast/parse_utils.rs (1)
src/parser/mod.rs (2)
  • balanced_block (190-195)
  • skip_whitespace_and_comments (1582-1592)
🔇 Additional comments (1)
src/parser/ast/parse_utils.rs (1)

11-15: Import additions are sound

The new imports correctly surface balanced_block, Span, and parser traits without widening visibility.

Comment thread src/parser/ast/parse_utils.rs
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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3f00f26 and f26968f.

📒 Files selected for processing (1)
  • src/parser/ast/parse_utils.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • AGENTS.md

⚙️ CodeRabbit Configuration File

🧬 Code Graph Analysis (1)
src/parser/ast/parse_utils.rs (1)
src/parser/mod.rs (2)
  • balanced_block (190-195)
  • skip_whitespace_and_comments (1582-1592)
🔇 Additional comments (1)
src/parser/ast/parse_utils.rs (1)

11-15: Imports look accurate and minimal

The additional balanced_block and chumsky imports are required for the new helper and are correctly scoped. No action needed.

Comment thread src/parser/ast/parse_utils.rs
Comment thread src/parser/ast/parse_utils.rs
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@leynos leynos merged commit 7494062 into main Jul 16, 2025
2 checks passed
@leynos leynos deleted the codex/refactor-collect_relation_spans-for-nested-paren-handling branch July 16, 2025 19:03
This was referenced Jul 16, 2025
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