Skip to content

Implement delimiter mismatch error#55

Merged
leynos merged 2 commits into
mainfrom
codex/implement-delimitererror-in-parse_utils.rs
Jul 5, 2025
Merged

Implement delimiter mismatch error#55
leynos merged 2 commits into
mainfrom
codex/implement-delimitererror-in-parse_utils.rs

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jul 5, 2025

Summary

  • detect unmatched delimiters while parsing
  • surface DelimiterError for name/type parsing
  • adjust callers to ignore returned errors
  • test new mismatch handling

Testing

  • make fmt
  • make lint
  • make test

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

Summary by Sourcery

Detect and report unmatched delimiters in name/type pair parsing by introducing a DelimiterError type and extending parse_name_type_pairs to collect errors alongside parsed pairs while preserving existing API behavior.

New Features:

  • Add DelimiterError type with Display and Error implementations
  • Extend parse_name_type_pairs to return a tuple of parsed pairs and delimiter errors

Enhancements:

  • Detect and accumulate delimiter mismatch errors for '>', '>>', ']', and '}' tokens during parsing
  • Update AST node methods to extract only the parsed name/type pairs from the new tuple return

Tests:

  • Add test for unmatched '>>' to verify delimiter error handling

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jul 5, 2025

Reviewer's Guide

This PR enhances the CST parser to detect and report unmatched delimiters by introducing a new DelimiterError type, updating the parsing utility to accumulate errors alongside parsed name-type pairs, adjusting downstream callers to ignore the new error vector, and adding tests for mismatch handling.

Sequence diagram for delimiter mismatch detection during parsing

sequenceDiagram
    participant Parser
    participant TokenStream
    participant DelimStack
    participant ErrorVec as DelimiterError[]

    Parser->>TokenStream: next_token()
    loop for each token
        Parser->>DelimStack: open/close based on token
        alt Delimiter mismatch
            DelimStack-->>Parser: close returns < expected
            Parser->>ErrorVec: push DelimiterError
        end
    end
    Parser-->>Parser: return (pairs, errors)
Loading

Class diagram for DelimiterError and DelimStack changes

classDiagram
    class Delim {
        <<enum>>
        Paren
        Angle
        Bracket
        Brace
    }
    class DelimStack {
        +open(delim: Delim, count: usize)
        +close(delim: Delim, count: usize) usize
        +is_empty() bool
    }
    class DelimiterError {
        +expected: Delim
        +found: SyntaxKind
        +span: TextRange
        +fmt(&self, f: &mut Formatter) -> Result
    }
    DelimiterError -- Delim : expected
    DelimiterError -- SyntaxKind : found
    DelimiterError -- TextRange : span
    DelimStack o-- Delim : uses
Loading

Class diagram for updated parse_name_type_pairs return type

classDiagram
    class parse_name_type_pairs {
        <<function>>
        +parse_name_type_pairs<I>(iter: I) : (Vec<(String, String)>, Vec<DelimiterError>)
    }
    parse_name_type_pairs ..> DelimiterError : returns
Loading

Class diagram for downstream callers updated to ignore errors

classDiagram
    class Relation {
        +columns() : Vec<(String, String)>
    }
    class Function {
        +parameters() : Vec<(String, String)>
    }
    Relation ..> parse_name_type_pairs : calls
    Function ..> parse_name_type_pairs : calls
Loading

File-Level Changes

Change Details Files
Introduce DelimiterError for unmatched delimiters
  • Derive Debug on Delim enum
  • Define DelimiterError struct with expected, found, and span
  • Implement Display and Error traits for DelimiterError
src/parser/ast/parse_utils.rs
Update parse_name_type_pairs to return errors
  • Change return type to (Vec<...>, Vec)
  • Initialize and collect errors in parse_name_type_pairs
  • Pass errors vector into handle_token
src/parser/ast/parse_utils.rs
Push DelimiterError in handle_token on close mismatch
  • Wrap close_and_push calls in conditionals
  • Push DelimiterError with expected, found, span on mismatch
src/parser/ast/parse_utils.rs
Adjust parser callers to ignore returned errors
  • Destructure parse_name_type_pairs result and use .0 for pairs
  • Ignore the errors vector in columns() and parameters()
src/parser/mod.rs
Add tests for delimiter mismatch
  • Destructure tuple in existing tests and assert empty errors
  • Add unmatched_shift_errors test case
src/parser/ast/parse_utils.rs

Possibly linked issues


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 5, 2025

Summary by CodeRabbit

  • New Features

    • Improved error tracking for unmatched or incorrectly closed delimiters when parsing name-type pairs, providing more informative error messages for issues with brackets, braces, or angle brackets.
  • Documentation

    • Updated method documentation to note that delimiter errors are currently ignored but may be reported in future versions.
  • Tests

    • Added and updated tests to verify detection of unmatched delimiters and to ensure valid cases remain error-free.

Summary by CodeRabbit

  • New Features

    • Improved error reporting for unmatched or incorrectly closed delimiters during parsing, providing clearer feedback on parsing issues.
  • Bug Fixes

    • Enhanced handling of angle brackets, brackets, and braces to ensure unmatched delimiters are accurately detected and reported.
  • Tests

    • Added and updated tests to verify correct error reporting for unmatched delimiters and to ensure no errors occur in valid cases.

Walkthrough

This change introduces structured error reporting for unmatched or incorrectly closed delimiters in the parsing of name-type pairs. A new DelimiterError struct is added, and the parse_name_type_pairs function now returns both parsed pairs and any delimiter errors. Relevant tests and method usages are updated accordingly.

Changes

File(s) Change Summary
src/parser/ast/parse_utils.rs Added DelimiterError struct, updated parse_name_type_pairs to return errors, enhanced error handling for delimiters, implemented error traits, and updated/added tests.
src/parser/mod.rs Updated Relation::columns() and Function::parameters() to extract only parsed pairs from the tuple returned by parse_name_type_pairs.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant parse_name_type_pairs
    participant DelimiterError

    Caller->>parse_name_type_pairs: Call with token iterator
    parse_name_type_pairs->>parse_name_type_pairs: Parse name-type pairs
    alt Delimiter mismatch
        parse_name_type_pairs->>DelimiterError: Create DelimiterError
        parse_name_type_pairs->>parse_name_type_pairs: Collect errors
    end
    parse_name_type_pairs-->>Caller: Return (pairs, errors)
Loading

Possibly related issues

Possibly related PRs

Poem

A bracket here, a brace askew,
Now errors know just what to do!
With structured notes, the parser sings,
Of angles, shifts, and closing things.
No more mysteries in the code’s domain—
The rabbit hops through error’s lane!
🐇✨


📜 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 dc4c9b1 and 0042034.

📒 Files selected for processing (2)
  • src/parser/ast/parse_utils.rs (9 hunks)
  • src/parser/mod.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.rs`: Comment why, not what. Explain assumptions, edge cases, trade-offs, o...

**/*.rs: Comment why, not what. Explain assumptions, edge cases, trade-offs, or complexity. Don't echo the obvious.
Comments must use en-GB-oxendict spelling and grammar.
Function documentation must include clear examples.
Name things precisely. Use clear, descriptive variable and function names. For booleans, prefer names with is, has, or should.
Each file should encapsulate a coherent module. Group related code (e.g., models + utilities + fixtures) close together.
Group by feature, not layer. Colocate views, logic, fixtures, and helpers related to a domain concept rather than splitting by type.
Every module must begin with a module level (//! ) comment explaining the module's purpose and utility.
Document public APIs using Rustdoc comments (///) so documentation can be generated with cargo doc.
Place function attributes after doc comments.
Do not use return in single-line functions.
Prefer immutable data and avoid unnecessary mut bindings.
Handle errors with the Result type instead of panicking where feasible.
Avoid unsafe code unless absolutely necessary and document any usage clearly.
Clippy warnings MUST be disallowed.
Lints must not be silenced except as a last resort.
Lint rule suppressions must be tightly scoped and include a clear reason.
Prefer expect over allow.
Use predicate functions for conditional criteria with more than two branches.
Where a function is too long, extract meaningfully named helper functions adhering to separation of concerns and CQRS.
Where a function has too many parameters, group related parameters in meaningfully named structs.
Where a function is returning a large error consider using Arc to reduce the amount of data returned.
Prefer .expect() over .unwrap().

📄 Source: CodeRabbit Inference Engine (AGENTS.md)

List of files the instruction was applied to:

  • src/parser/ast/parse_utils.rs
  • src/parser/mod.rs
`**/*.rs`: * Seek to keep the cyclomatic complexity of functions no more than 12...

**/*.rs: * Seek to keep the cyclomatic complexity of functions no more than 12.

  • Adhere to single responsibility and CQRS

  • Place function attributes after doc comments.

  • Do not use return in single-line functions.

  • Move conditionals with >2 branches into a predicate function.

  • Avoid unsafe unless absolutely necessary.

  • Every module must begin with a //! doc comment that explains the module's purpose and utility.

  • Comments must use en-GB-oxendict spelling and grammar.

  • Lints must not be silenced except as a last resort.

    • #[allow] is forbidden.
    • Only narrowly scoped #[expect(lint, reason = "...")] is allowed.
    • No lint groups, no blanket or file-wide suppression.
    • Include FIXME: with link if a fix is expected.
  • Use rstest fixtures for shared setup and to avoid repetition between tests.

  • Replace duplicated tests with #[rstest(...)] parameterised cases.

  • Prefer mockall for mocks/stubs.

  • Prefer .expect() over .unwrap()

  • Ensure that any API or behavioural changes are reflected in the documentation in docs/

  • Ensure that any completed roadmap steps are recorded in the appropriate roadmap in docs/

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • src/parser/ast/parse_utils.rs
  • src/parser/mod.rs
🧬 Code Graph Analysis (1)
src/parser/mod.rs (1)
src/parser/ast/parse_utils.rs (1)
  • parse_name_type_pairs (135-190)
⏰ Context from checks skipped due to timeout of 240000ms (1)
  • GitHub Check: build-test
🔇 Additional comments (2)
src/parser/ast/parse_utils.rs (2)

135-190: Well-implemented delimiter error tracking.

The function signature change and error collection logic are well-designed. The additional loop to capture unmatched closing tokens after the parameter list ends is a thoughtful addition that ensures comprehensive error detection.


214-235: Correct delimiter mismatch detection.

The error detection logic properly checks if the expected number of delimiters were closed and records errors when mismatches occur, while allowing parsing to continue.

✨ Finishing Touches
  • 📝 Docstrings were successfully generated. (🔄 Check again to generate docstrings again)- [ ] Create PR with Unit Tests
  • Post Copyable Unit Tests in a Comment
  • Commit Unit Tests in branch codex/implement-delimitererror-in-parse_utils.rs

🪧 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 - here's some feedback:

  • Extract the repeated errors.push(...) logic into a small helper function to reduce duplication and improve readability.
  • Expand tests to cover unmatched ), ], and } cases (not just >>) so all delimiter mismatches are validated.
  • Fix the Display implementation to correctly interpolate the expected/found values—Rust’s write! macros don’t support {expected}-style interpolation directly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract the repeated `errors.push(...)` logic into a small helper function to reduce duplication and improve readability.
- Expand tests to cover unmatched `)`, `]`, and `}` cases (not just `>>`) so all delimiter mismatches are validated.
- Fix the `Display` implementation to correctly interpolate the `expected`/`found` values—Rust’s `write!` macros don’t support `{expected}`-style interpolation directly.

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.

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

🔭 Outside diff range comments (1)
src/parser/ast/parse_utils.rs (1)

123-163: Handle delimiter errors in all callers of parse_name_type_pairs

The RG results show two call sites still using .0, dropping the errors vector:

• src/parser/mod.rs (two occurrences)

parse_name_type_pairs(self.syntax.children_with_tokens()).0

Action items:

  • Replace both .0 accesses with tuple destructuring:
    let (pairs, errors) = parse_name_type_pairs(self.syntax.children_with_tokens());
    // TODO: propagate or log `errors` as appropriate
  • Ensure any non-empty errors are handled (e.g., bubbled up, logged, or converted to diagnostics).

Tests in parse_utils.rs already destructure and assert on errors, so no changes are needed there.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 031ced9 and dc4c9b1.

📒 Files selected for processing (2)
  • src/parser/ast/parse_utils.rs (9 hunks)
  • src/parser/mod.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.rs`: Comment why, not what. Explain assumptions, edge cases, trade-offs, o...

**/*.rs: Comment why, not what. Explain assumptions, edge cases, trade-offs, or complexity. Don't echo the obvious.
Comments must use en-GB-oxendict spelling and grammar.
Function documentation must include clear examples.
Name things precisely. Use clear, descriptive variable and function names. For booleans, prefer names with is, has, or should.
Each file should encapsulate a coherent module. Group related code (e.g., models + utilities + fixtures) close together.
Group by feature, not layer. Colocate views, logic, fixtures, and helpers related to a domain concept rather than splitting by type.
Every module must begin with a module level (//! ) comment explaining the module's purpose and utility.
Document public APIs using Rustdoc comments (///) so documentation can be generated with cargo doc.
Place function attributes after doc comments.
Do not use return in single-line functions.
Prefer immutable data and avoid unnecessary mut bindings.
Handle errors with the Result type instead of panicking where feasible.
Avoid unsafe code unless absolutely necessary and document any usage clearly.
Clippy warnings MUST be disallowed.
Lints must not be silenced except as a last resort.
Lint rule suppressions must be tightly scoped and include a clear reason.
Prefer expect over allow.
Use predicate functions for conditional criteria with more than two branches.
Where a function is too long, extract meaningfully named helper functions adhering to separation of concerns and CQRS.
Where a function has too many parameters, group related parameters in meaningfully named structs.
Where a function is returning a large error consider using Arc to reduce the amount of data returned.
Prefer .expect() over .unwrap().

📄 Source: CodeRabbit Inference Engine (AGENTS.md)

List of files the instruction was applied to:

  • src/parser/mod.rs
  • src/parser/ast/parse_utils.rs
`**/*.rs`: * Seek to keep the cyclomatic complexity of functions no more than 12...

**/*.rs: * Seek to keep the cyclomatic complexity of functions no more than 12.

  • Adhere to single responsibility and CQRS

  • Place function attributes after doc comments.

  • Do not use return in single-line functions.

  • Move conditionals with >2 branches into a predicate function.

  • Avoid unsafe unless absolutely necessary.

  • Every module must begin with a //! doc comment that explains the module's purpose and utility.

  • Comments must use en-GB-oxendict spelling and grammar.

  • Lints must not be silenced except as a last resort.

    • #[allow] is forbidden.
    • Only narrowly scoped #[expect(lint, reason = "...")] is allowed.
    • No lint groups, no blanket or file-wide suppression.
    • Include FIXME: with link if a fix is expected.
  • Use rstest fixtures for shared setup and to avoid repetition between tests.

  • Replace duplicated tests with #[rstest(...)] parameterised cases.

  • Prefer mockall for mocks/stubs.

  • Prefer .expect() over .unwrap()

  • Ensure that any API or behavioural changes are reflected in the documentation in docs/

  • Ensure that any completed roadmap steps are recorded in the appropriate roadmap in docs/

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • src/parser/mod.rs
  • src/parser/ast/parse_utils.rs
🧬 Code Graph Analysis (1)
src/parser/mod.rs (1)
src/parser/ast/parse_utils.rs (1)
  • parse_name_type_pairs (123-163)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-test
🔇 Additional comments (3)
src/parser/ast/parse_utils.rs (3)

26-31: LGTM! Well-structured error type.

The DelimiterError struct appropriately captures all necessary information for delimiter mismatch errors. The use of Copy trait is suitable for this small error type.


33-55: Display implementation provides clear error messages.

The error message format effectively communicates delimiter mismatches. The mapping from internal Delim enum to string representation is comprehensive.


187-223: Robust error detection for delimiter mismatches.

The error detection logic correctly identifies when fewer delimiters are closed than expected, creating appropriate error objects with precise location information. The pattern is consistently applied across all delimiter types.

Comment thread src/parser/ast/parse_utils.rs
Comment thread src/parser/mod.rs Outdated
@leynos leynos merged commit 1f54028 into main Jul 5, 2025
2 checks passed
@leynos leynos deleted the codex/implement-delimitererror-in-parse_utils.rs branch July 5, 2025 16:48
coderabbitai Bot added a commit that referenced this pull request Jul 5, 2025
Docstrings generation was requested by @leynos.

* #55 (comment)

The following files were modified:

* `src/parser/ast/parse_utils.rs`
* `src/parser/mod.rs`
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 5, 2025

Note

Generated docstrings for this pull request at #56

leynos added a commit that referenced this pull request Jul 5, 2025
#56)

* 📝 Add docstrings to `codex/implement-delimitererror-in-parse_utils.rs`

Docstrings generation was requested by @leynos.

* #55 (comment)

The following files were modified:

* `src/parser/ast/parse_utils.rs`
* `src/parser/mod.rs`

* Better example

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

* Add doc for DelimiterError

* Fix syntax error

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Leynos <leynos@troubledskies.net>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@coderabbitai coderabbitai Bot mentioned this pull request Jul 5, 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