-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor parse utilities into dedicated modules #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7d0d62c
Format parse utility modules
leynos 56487fd
Refactor parsing helpers to lower complexity
leynos f1b6c7a
Refactor parameter builder
leynos 4fbc87d
Document type parsing helpers
leynos 0423585
Clarify parameter separator and trailing delimiter handling
leynos b74c217
Centralise trivia handling and tighten parameter tests
leynos b797dad
Parametrize parameter error tests
leynos d1d67e2
Harden parse utilities and expand tests
leynos cb2cd9d
Refactor type expression token handling
leynos 4f2f186
Handle unmatched parameter lists
leynos f02ced0
Inline type-expression delimiters and harden parameter recovery
leynos 8f53d11
Refactor type token parsing and guard parameter types
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,23 @@ | ||
| //! Parsing helpers shared across AST modules. | ||
| //! | ||
| //! This module provides small utilities for collecting parameter names and | ||
| //! types from the CST and for recursively parsing type expressions. Both the | ||
| //! `Function` and `Relation` nodes import these helpers so they can share the | ||
| //! same logic when interpreting their declarations. See | ||
| //! types from the CST, recursively parsing type expressions, and parsing | ||
| //! transformer output identifiers. `Function`, `Relation`, and `Transformer` | ||
| //! nodes import these helpers so they can share the same logic when | ||
| //! interpreting their declarations. See | ||
| //! `docs/function-parsing-design.md` for an overview. | ||
|
|
||
| mod delimiter; | ||
| pub(crate) mod errors; | ||
| mod outputs; | ||
| mod params; | ||
| mod token_utils; | ||
| mod type_parsing; | ||
| mod type_expr; | ||
|
|
||
| pub use delimiter::extract_parenthesized; | ||
| pub(crate) use delimiter::paren_block_span; | ||
|
|
||
| pub(crate) use type_parsing::{parse_name_type_pairs, parse_output_list, parse_type_after_colon}; | ||
| pub(crate) use outputs::parse_output_list; | ||
| pub(crate) use params::parse_name_type_pairs; | ||
| pub(crate) use token_utils::is_trivia; | ||
| pub(crate) use type_expr::parse_type_after_colon; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| //! Output list parsing utilities. | ||
| //! | ||
| //! Parses comma-separated output relation names following a transformer | ||
| //! declaration's colon. | ||
|
|
||
| use rowan::SyntaxElement; | ||
|
|
||
| use crate::{DdlogLanguage, SyntaxKind}; | ||
|
|
||
| use super::super::skip_whitespace_and_comments; | ||
|
|
||
| pub(crate) fn skip_to_top_level_colon<I>(iter: &mut std::iter::Peekable<I>) | ||
| where | ||
| I: Iterator<Item = SyntaxElement<DdlogLanguage>>, | ||
| { | ||
| let mut depth = 0usize; | ||
| for e in iter.by_ref() { | ||
| match e.kind() { | ||
| SyntaxKind::T_LPAREN => depth += 1, | ||
| SyntaxKind::T_RPAREN => depth = depth.saturating_sub(1), | ||
| SyntaxKind::T_COLON if depth == 0 => break, | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn try_parse_identifier<I>(iter: &mut std::iter::Peekable<I>) -> Option<String> | ||
| where | ||
| I: Iterator<Item = SyntaxElement<DdlogLanguage>>, | ||
| { | ||
| use rowan::NodeOrToken; | ||
|
|
||
| skip_whitespace_and_comments(iter); | ||
| match iter.next() { | ||
| Some(NodeOrToken::Token(t)) if t.kind() == SyntaxKind::T_IDENT => { | ||
| Some(t.text().to_string()) | ||
| } | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn has_comma_separator<I>(iter: &mut std::iter::Peekable<I>) -> bool | ||
| where | ||
| I: Iterator<Item = SyntaxElement<DdlogLanguage>>, | ||
| { | ||
| use rowan::NodeOrToken; | ||
|
|
||
| skip_whitespace_and_comments(iter); | ||
| if let Some(NodeOrToken::Token(t)) = iter.peek() | ||
| && t.kind() == SyntaxKind::T_COMMA | ||
| { | ||
| iter.next(); | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn parse_output_list<I>(iter: I) -> Vec<String> | ||
| where | ||
| I: Iterator<Item = SyntaxElement<DdlogLanguage>>, | ||
| { | ||
| let mut iter = iter.peekable(); | ||
| skip_to_top_level_colon(&mut iter); | ||
|
|
||
| let mut names = Vec::new(); | ||
| while let Some(name) = try_parse_identifier(&mut iter) { | ||
| names.push(name); | ||
| if !has_comma_separator(&mut iter) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| names | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::parser::ast::AstNode; | ||
| use crate::parser::parse; | ||
|
|
||
| #[test] | ||
| fn collects_output_names() { | ||
| let src = "extern transformer t(a: X): Out1, Out2"; | ||
| let parsed = parse(src); | ||
| #[expect(clippy::expect_used, reason = "Using expect for clearer test failures")] | ||
| let tr = parsed | ||
| .root() | ||
| .transformers() | ||
| .first() | ||
| .cloned() | ||
| .expect("transformer missing"); | ||
| let names = parse_output_list(tr.syntax().children_with_tokens()); | ||
| assert_eq!(names, vec!["Out1".to_string(), "Out2".to_string()]); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ New issue: Bumpy Road Ahead
take_first_ident has 2 blocks with nested conditional logic. Any nesting of 2 or deeper is considered. Threshold is one single, nested block per function
Suppress
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai Please suggest a fix for this issue and supply a prompt for an AI coding agent to enable it to apply the fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leynos, the "Bumpy Road Ahead" issue occurs because
take_first_identnow has nested conditional logic: theforloop contains both anif is_trivia(&e)check and amatchexpression, creating 2 levels of nesting.Here's a fix that uses iterator combinators to eliminate the nested structure:
AI Coding Agent Prompt:
This approach eliminates the nested conditional logic by leveraging Rust's iterator combinators, making the code more declarative and easier to follow.