Skip to content

Add function parser#39

Open
leynos wants to merge 1 commit intomainfrom
codex/implement-func-parser-for-function-definitions
Open

Add function parser#39
leynos wants to merge 1 commit intomainfrom
codex/implement-func-parser-for-function-definitions

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jul 2, 2025

Summary

  • add parser functions for function definitions
  • handle extern function declarations
  • group function spans into CST nodes and expose AST API
  • test parsing of function declarations and definitions

Testing

  • make lint
  • make test

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

Summary by Sourcery

Add support for parsing Datalog-style functions, including extern function declarations and regular function definitions, and expose them in the CST/AST.

New Features:

  • Collect spans for extern function declarations and function definitions during token parsing.
  • Extend the green-tree builder to include function nodes and integrate function spans into the parse pipeline.
  • Introduce a typed Function AST node with name() and is_extern() methods and a Root::functions() accessor.

Enhancements:

  • Extend parse_tokens and build_green_tree to handle function spans alongside imports, typedefs, relations, and indexes.

Tests:

  • Add unit tests verifying parsing of extern functions, functions with bodies, and functions without return types.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jul 2, 2025

Reviewer's Guide

This PR adds full support for parsing function declarations and definitions by extending the token span collector, integrating function nodes into the CST, exposing a new AST API for functions, and adding corresponding tests.

Class diagram for new and updated AST types for function parsing

classDiagram
    class Root {
        +functions() Vec~Function~
    }
    class Function {
        +syntax() SyntaxNode
        +name() Option~String~
        +is_extern() bool
    }
    Root --> "*" Function : functions()
Loading

File-Level Changes

Change Details Files
Extend token-based parser to identify function declarations and definitions
  • Extend parse() to extract a new function_spans list
  • Update parse_tokens() signature, documentation, and returned tuples to include functions and merge errors
  • Implement parser combinators for function parameters, bodies, and return types
  • Add collect_function_spans to dispatch on extern function and normal function tokens and record spans
src/parser/mod.rs
Integrate function spans into CST construction
  • Assert sorted function spans alongside other statement spans
  • Introduce a function_iter and advance it on token iteration
  • Emit N_FUNCTION nodes via maybe_start and maybe_finish during tree building
src/parser/mod.rs
Expose function nodes in the AST
  • Add Root::functions() to collect all function nodes
  • Define a new Function wrapper with syntax(), name(), and is_extern() methods
src/parser/mod.rs
Add parsing tests for function declarations and definitions
  • Create fixtures for extern functions, functions with bodies, and functions without return types
  • Write rstest-based tests validating parsed function count, names, and extern flags
  • Assert zero parse errors when parsing all new cases
tests/parser.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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 2, 2025

Summary by CodeRabbit

  • New Features

    • Added support for parsing function declarations and definitions, including extern functions and functions with or without return types.
    • Functions are now represented in the syntax tree and can be accessed programmatically.
  • Tests

    • Introduced new tests to verify correct parsing of various function declaration forms, ensuring accurate detection and classification.

Walkthrough

The parser has been enhanced to recognise and process function declarations and definitions, including extern functions, within its parsing framework. New parsers and span collectors for functions have been introduced, the CST builder updated, and typed AST wrappers extended to represent functions. Corresponding tests for function parsing have been added.

Changes

File(s) Change Summary
src/parser/mod.rs Added function span collection, new parsers for function parameters, bodies, and return types. Updated CST builder and AST wrappers to support functions. New Function struct and related methods introduced.
tests/parser.rs Added fixtures and tests for extern functions, functions with bodies (with and without return types), and assertions on parsing results.

Sequence Diagram(s)

sequenceDiagram
    participant Source as Source Code
    participant Lexer as Lexer
    participant Parser as Parser
    participant CST as CST Builder
    participant AST as AST Wrapper

    Source->>Lexer: Tokenise input
    Lexer->>Parser: Provide tokens
    Parser->>Parser: Collect spans (import, typedef, relation, index, function)
    Parser->>Parser: Parse functions (params, body, return type)
    Parser->>CST: Build CST with function nodes
    CST->>AST: Wrap CST nodes as typed AST (Function struct)
    AST->>AST: Provide access to function info (name, is_extern, etc.)
Loading

Poem

In the warren of code, a new path appears,
Where functions now hop—externs and peers!
With tokens and spans, the parser’s delight,
Each function’s discovered, from morning till night.
The tests all agree, with a jubilant cheer:
“Function support’s here—let’s nibble and peer!”
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Clippy (1.86.0)
Updating crates.io index

warning: failed to write cache, path: /usr/local/registry/index/index.crates.io-1949cf8c6b5b557f/.cache/ch/um/chumsky, error: Permission denied (os error 13)
Locking 60 packages to latest Rust 1.86.0 compatible versions
Adding chumsky v0.9.3 (available: v0.10.1)
Adding logos v0.13.0 (available: v0.15.0)
Adding phf v0.11.3 (available: v0.12.1)
Adding rowan v0.15.16 (available: v0.16.1)
Downloading crates ...
Downloaded ahash v0.8.12
error: failed to create directory /usr/local/registry/cache/index.crates.io-1949cf8c6b5b557f

Caused by:
Permission denied (os error 13)

✨ 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/implement-func-parser-for-function-definitions

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

  • The Cell-based function_params and function_body parsers share nearly identical nesting logic—consider extracting a generic balanced-delimiter parser to reduce duplication.
  • The parse_into_span usage and span/error handling in collect_function_spans is duplicated for extern vs normal functions—factoring out the common logic would simplify that function.
  • Since parse_tokens now returns six separate vectors, consider defining a dedicated struct for its return value to improve readability and prevent ordering mistakes.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The Cell-based `function_params` and `function_body` parsers share nearly identical nesting logic—consider extracting a generic balanced-delimiter parser to reduce duplication.
- The `parse_into_span` usage and span/error handling in `collect_function_spans` is duplicated for extern vs normal functions—factoring out the common logic would simplify that function.
- Since `parse_tokens` now returns six separate vectors, consider defining a dedicated struct for its return value to improve readability and prevent ordering mistakes.

## Individual Comments

### Comment 1
<location> `src/parser/mod.rs:673` </location>
<code_context>
+) -> (Vec<Span>, Vec<Simple<SyntaxKind>>) {
+    type State<'a> = SpanCollector<'a, Vec<Simple<SyntaxKind>>>;
+
+    fn parse_into_span(
+        st: &mut State<'_>,
+        parser: impl Parser<SyntaxKind, Span, Error = Simple<SyntaxKind>>,
+        start: usize,
+    ) {
+        let iter = st.stream.tokens().iter().skip(st.stream.cursor()).cloned();
+        let sub = Stream::from_iter(start..st.stream.src().len(), iter);
+        let (res, err) = parser.parse_recovery(sub);
+        if let Some(sp) = res {
+            let end = sp.end;
+            st.spans.push(sp);
</code_context>

<issue_to_address>
parse_into_span() skips to the end of the line on error, which may skip too much in multi-line function declarations.

This approach may cause the parser to miss valid code after a multi-line function declaration. Consider a recovery strategy that skips to the end of the function span instead.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        } else {
            st.extra.extend(err);
            let end = st.stream.line_end(st.stream.cursor());
            st.stream.skip_until(end);
        }
=======
        } else {
            st.extra.extend(err);

            // Try to find the end of the function (matching closing brace)
            let mut depth = 0;
            let mut found_end = false;
            let mut idx = st.stream.cursor();
            let tokens = st.stream.tokens();
            while idx < tokens.len() {
                match tokens[idx].0 {
                    SyntaxKind::LBrace => depth += 1,
                    SyntaxKind::RBrace => {
                        if depth == 0 {
                            // Found a closing brace without opening, treat as end
                            found_end = true;
                            break;
                        } else {
                            depth -= 1;
                            if depth == 0 {
                                found_end = true;
                                break;
                            }
                        }
                    }
                    _ => {}
                }
                idx += 1;
            }
            if found_end {
                let end = tokens[idx].1.end;
                st.stream.skip_until(end);
            } else {
                // Fallback: skip to end of line
                let end = st.stream.line_end(st.stream.cursor());
                st.stream.skip_until(end);
            }
        }
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `tests/parser.rs:114` </location>
<code_context>
     "  index  Idx_User_ws \t on\n  User (\n    username  )  "
 }

+#[fixture]
+fn extern_function() -> &'static str {
+    "extern function hash(data: string): u64"
+}
</code_context>

<issue_to_address>
Consider adding tests for malformed or invalid function declarations.

Adding such tests will help verify that the parser properly rejects invalid syntax and reports errors as expected.

Suggested implementation:

```rust
#[fixture]
fn function_with_body() -> &'static str {
    "function to_uppercase(s: string): string { }"
}

#[fixture]
fn malformed_extern_function_missing_parens() -> &'static str {
    "extern function hash data: string): u64"
}

#[fixture]
fn malformed_extern_function_missing_colon() -> &'static str {
    "extern function hash(data string) u64"
}

#[fixture]
fn malformed_extern_function_no_type() -> &'static str {
    "extern function hash(data):"
}

#[test]
fn test_malformed_extern_function_missing_parens() {
    let input = malformed_extern_function_missing_parens();
    let result = parse_function_declaration(input);
    assert!(result.is_err(), "Parser should error on missing parentheses in extern function declaration");
}

#[test]
fn test_malformed_extern_function_missing_colon() {
    let input = malformed_extern_function_missing_colon();
    let result = parse_function_declaration(input);
    assert!(result.is_err(), "Parser should error on missing colon in extern function declaration");
}

#[test]
fn test_malformed_extern_function_no_type() {
    let input = malformed_extern_function_no_type();
    let result = parse_function_declaration(input);
    assert!(result.is_err(), "Parser should error on missing return type in extern function declaration");
}

```

- Ensure that the function `parse_function_declaration` is available in scope and is the correct function to parse function declarations. If your parser uses a different function or module, adjust the test calls accordingly.
- If you use a test framework other than the standard Rust test harness, you may need to adapt the test attribute or structure.
</issue_to_address>

### Comment 3
<location> `tests/parser.rs:131` </location>
<code_context>
+
 /// Verifies that parsing and pretty-printing preserves the original input text
 /// and produces the expected root node kind.
 #[rstest]
</code_context>

<issue_to_address>
Add tests for functions with complex parameter lists and nested types.

Add tests covering functions with multiple parameters, complex or nested types, and edge cases such as empty parameter lists or unusual whitespace to ensure comprehensive parser coverage.
</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
Comment on lines +685 to +689
} else {
st.extra.extend(err);
let end = st.stream.line_end(st.stream.cursor());
st.stream.skip_until(end);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): parse_into_span() skips to the end of the line on error, which may skip too much in multi-line function declarations.

This approach may cause the parser to miss valid code after a multi-line function declaration. Consider a recovery strategy that skips to the end of the function span instead.

Suggested change
} else {
st.extra.extend(err);
let end = st.stream.line_end(st.stream.cursor());
st.stream.skip_until(end);
}
} else {
st.extra.extend(err);
// Try to find the end of the function (matching closing brace)
let mut depth = 0;
let mut found_end = false;
let mut idx = st.stream.cursor();
let tokens = st.stream.tokens();
while idx < tokens.len() {
match tokens[idx].0 {
SyntaxKind::LBrace => depth += 1,
SyntaxKind::RBrace => {
if depth == 0 {
// Found a closing brace without opening, treat as end
found_end = true;
break;
} else {
depth -= 1;
if depth == 0 {
found_end = true;
break;
}
}
}
_ => {}
}
idx += 1;
}
if found_end {
let end = tokens[idx].1.end;
st.stream.skip_until(end);
} else {
// Fallback: skip to end of line
let end = st.stream.line_end(st.stream.cursor());
st.stream.skip_until(end);
}
}

Comment thread tests/parser.rs
Comment on lines +114 to +115
#[fixture]
fn extern_function() -> &'static str {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (testing): Consider adding tests for malformed or invalid function declarations.

Adding such tests will help verify that the parser properly rejects invalid syntax and reports errors as expected.

Suggested implementation:

#[fixture]
fn function_with_body() -> &'static str {
    "function to_uppercase(s: string): string { }"
}

#[fixture]
fn malformed_extern_function_missing_parens() -> &'static str {
    "extern function hash data: string): u64"
}

#[fixture]
fn malformed_extern_function_missing_colon() -> &'static str {
    "extern function hash(data string) u64"
}

#[fixture]
fn malformed_extern_function_no_type() -> &'static str {
    "extern function hash(data):"
}

#[test]
fn test_malformed_extern_function_missing_parens() {
    let input = malformed_extern_function_missing_parens();
    let result = parse_function_declaration(input);
    assert!(result.is_err(), "Parser should error on missing parentheses in extern function declaration");
}

#[test]
fn test_malformed_extern_function_missing_colon() {
    let input = malformed_extern_function_missing_colon();
    let result = parse_function_declaration(input);
    assert!(result.is_err(), "Parser should error on missing colon in extern function declaration");
}

#[test]
fn test_malformed_extern_function_no_type() {
    let input = malformed_extern_function_no_type();
    let result = parse_function_declaration(input);
    assert!(result.is_err(), "Parser should error on missing return type in extern function declaration");
}
  • Ensure that the function parse_function_declaration is available in scope and is the correct function to parse function declarations. If your parser uses a different function or module, adjust the test calls accordingly.
  • If you use a test framework other than the standard Rust test harness, you may need to adapt the test attribute or structure.

Comment thread tests/parser.rs Outdated
Comment on lines +515 to +535
assert_eq!(idx.relation(), Some("User".into()));
assert_eq!(idx.columns(), vec![String::from("username")]);
}

#[rstest]
fn extern_function_parsed(extern_function: &str) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (testing): Add tests for functions with complex parameter lists and nested types.

Add tests covering functions with multiple parameters, complex or nested types, and edge cases such as empty parameter lists or unusual whitespace to ensure comprehensive parser coverage.

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 a5e5da0 and 6bb3d4e.

📒 Files selected for processing (2)
  • src/parser/mod.rs (10 hunks)
  • tests/parser.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.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:

  • tests/parser.rs
  • src/parser/mod.rs
🧬 Code Graph Analysis (2)
tests/parser.rs (1)
src/parser/mod.rs (8)
  • parse (110-131)
  • errors (99-101)
  • name (1056-1062)
  • name (1133-1144)
  • name (1322-1333)
  • name (1422-1433)
  • is_extern (1066-1070)
  • is_extern (1437-1441)
src/parser/mod.rs (3)
src/parser/token_stream.rs (3)
  • tokens (105-107)
  • src (118-120)
  • new (41-47)
src/parser/span_collector.rs (1)
  • new (44-50)
src/language.rs (1)
  • kind_to_raw (185-190)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-test
🔇 Additional comments (2)
src/parser/mod.rs (1)

1407-1442: LGTM!

The Function AST implementation follows the established pattern and provides appropriate methods for accessing function properties.

tests/parser.rs (1)

114-571: Well-structured tests for function parsing

The test fixtures and corresponding test functions provide comprehensive coverage of the new function parsing capabilities. The tests follow the established patterns and verify all essential properties.

Comment thread src/parser/mod.rs
}
SyntaxKind::T_RPAREN => {
if depth.get() == 0 {
Err(Simple::custom(span, "unexpected ')'"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Clarify the error usage in the parser logic

The error message "unexpected ')'" might be confusing since the closing parenthesis is actually expected to terminate the parameter list. Consider adding a comment to explain that this error is used as a signal to stop parsing rather than indicating an actual syntax error.

                 SyntaxKind::T_RPAREN => {
                     if depth.get() == 0 {
+                        // This error signals the end of the parameter list, not an actual syntax error
                         Err(Simple::custom(span, "unexpected ')'"))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Err(Simple::custom(span, "unexpected ')'"))
SyntaxKind::T_RPAREN => {
if depth.get() == 0 {
// This error signals the end of the parameter list, not an actual syntax error
Err(Simple::custom(span, "unexpected ')'"))
🤖 Prompt for AI Agents
In src/parser/mod.rs at line 276, the error message "unexpected ')'" is used to
signal the end of parameter list parsing rather than an actual syntax error. Add
a clarifying comment above this line explaining that this error is intentionally
used as a control flow mechanism to stop parsing parameters, so readers
understand its purpose and it is not a real parsing error.

Comment thread src/parser/mod.rs
Comment on lines +693 to +706
let mut idx = st.stream.cursor() + 1;
while let Some((kind, sp)) = st.stream.tokens().get(idx) {
if matches!(kind, SyntaxKind::T_WHITESPACE | SyntaxKind::T_COMMENT)
&& !st
.stream
.src()
.get(sp.clone())
.is_some_and(|t| t.contains('\n'))
{
idx += 1;
continue;
}
break;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Extract lookahead logic into a reusable helper method

The token lookahead logic could be extracted into a helper method to improve reusability and adhere to DRY principles. This pattern might be useful elsewhere in the parser.

Consider extracting this into a helper method on TokenStream:

+    /// Look ahead to find the next non-whitespace/comment token without advancing the cursor
+    fn peek_next_significant(&self, from_idx: usize) -> Option<&(SyntaxKind, Span)> {
+        let mut idx = from_idx;
+        while let Some((kind, sp)) = self.tokens.get(idx) {
+            if matches!(kind, SyntaxKind::T_WHITESPACE | SyntaxKind::T_COMMENT)
+                && !self.src.get(sp.clone()).is_some_and(|t| t.contains('\n'))
+            {
+                idx += 1;
+                continue;
+            }
+            return Some((kind, sp));
+        }
+        None
+    }

Then use it in handle_extern:

-        let mut idx = st.stream.cursor() + 1;
-        while let Some((kind, sp)) = st.stream.tokens().get(idx) {
-            if matches!(kind, SyntaxKind::T_WHITESPACE | SyntaxKind::T_COMMENT)
-                && !st
-                    .stream
-                    .src()
-                    .get(sp.clone())
-                    .is_some_and(|t| t.contains('\n'))
-            {
-                idx += 1;
-                continue;
-            }
-            break;
-        }
-
-        let is_func = st
-            .stream
-            .tokens()
-            .get(idx)
-            .is_some_and(|(kind, _)| *kind == SyntaxKind::K_FUNCTION);
+        let next_token = st.stream.peek_next_significant(st.stream.cursor() + 1);
+        let is_func = next_token.is_some_and(|(kind, _)| *kind == SyntaxKind::K_FUNCTION);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let mut idx = st.stream.cursor() + 1;
while let Some((kind, sp)) = st.stream.tokens().get(idx) {
if matches!(kind, SyntaxKind::T_WHITESPACE | SyntaxKind::T_COMMENT)
&& !st
.stream
.src()
.get(sp.clone())
.is_some_and(|t| t.contains('\n'))
{
idx += 1;
continue;
}
break;
}
let next_token = st.stream.peek_next_significant(st.stream.cursor() + 1);
let is_func = next_token.is_some_and(|(kind, _)| *kind == SyntaxKind::K_FUNCTION);
🤖 Prompt for AI Agents
In src/parser/mod.rs around lines 693 to 706, the token lookahead logic is
directly implemented in the function, which reduces reusability and violates DRY
principles. Extract this logic into a helper method on the TokenStream struct
that performs the lookahead while skipping whitespace and comments without
newlines. Then replace the existing inline code in handle_extern with a call to
this new helper method to improve code clarity and reuse.

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