Skip to content
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

implemented parenthesis in expr #34

Merged
merged 1 commit into from
Feb 6, 2025
Merged

Conversation

ascandone
Copy link
Contributor

@ascandone ascandone commented Feb 3, 2025

This PR allows numscript to use parenthesis in expressions
E.g. $x + ($y - $z)

That's a new (backwards compatible) functionality compared to numscript machine implementation and it's not under a feature flag.
The change only involves the grammar, without introducing new AST nodes

Copy link

coderabbitai bot commented Feb 3, 2025

Walkthrough

This pull request introduces a new production rule to handle parenthesized expressions in the grammar. The changes span grammar updates in Numscript.g4, parser modifications to support a new ParenthesizedExprContext, additions to listener methods, and an updated ATN array value. A new test ensures that expressions with nested parentheses are parsed correctly. The overall functionality remains unchanged while extending parsing capabilities.

Changes

File(s) Summary
Numscript.g4 Added new production rule for parenthesized expressions in valueExpr and removed the semicolon from the infix expression.
internal/parser/antlr/Numscript.interp Updated the ATN numerical value from 217 to 221 to reflect internal parsing state adjustments.
internal/parser/antlr/numscript_base_listener.go
internal/parser/antlr/numscript_listener.go
Added EnterParenthesizedExpr and ExitParenthesizedExpr methods to the listener implementations and interface.
internal/parser/antlr/numscript_parser.go
internal/parser/parser.go
Introduced ParenthesizedExprContext with corresponding methods and integrated new case in the parseValueExpr function to handle parenthesized expressions.
internal/parser/parser_test.go Added new test function TestParensInfixPrec to verify the correct parsing and precedence handling of expressions with parentheses.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client/Test
    participant Parser as NumscriptParser
    participant Listener as NumscriptListener

    Client->>Parser: Request to parse expression e.g. set_tx_meta("k1", 1 + (2 - 3))
    Parser->>Parser: Recognize '(' and initiate ParenthesizedExprContext
    Parser->>Listener: Call EnterParenthesizedExpr
    Parser->>Parser: Parse inner expression (2 - 3)
    Parser->>Listener: Call ExitParenthesizedExpr
    Parser->>Client: Return parsed expression tree
Loading

Poem

Oh, how I hop with joy today,
New parenthesized paths pave my play.
With each "(" and ")" snugly in view,
Parsing expressions feels refreshingly new.
Bounding through grammar with a happy leap,
CodeRabbit cheers—its changes run deep! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

@ascandone ascandone marked this pull request as ready for review February 3, 2025 13:32
Copy link

codecov bot commented Feb 3, 2025

Codecov Report

Attention: Patch coverage is 89.47368% with 28 lines in your changes missing coverage. Please review.

Project coverage is 63.14%. Comparing base (ae202e9) to head (60dd753).

Files with missing lines Patch % Lines
internal/parser/antlr/numscript_parser.go 90.07% 22 Missing and 4 partials ⚠️
internal/parser/antlr/numscript_base_listener.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #34      +/-   ##
==========================================
- Coverage   63.27%   63.14%   -0.14%     
==========================================
  Files          29       29              
  Lines        6462     6520      +58     
==========================================
+ Hits         4089     4117      +28     
- Misses       2191     2217      +26     
- Partials      182      186       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@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: 0

🧹 Nitpick comments (1)
internal/parser/parser_test.go (1)

340-346: Add more test cases for comprehensive coverage.

While the current test case verifies basic parenthesized expression parsing, consider adding more test cases to cover:

  1. Nested parentheses: ((1 + 2) - (3 + 4))
  2. Mixed operators: (1 + 2) - (3 + 4)
  3. Edge cases: (1), ((1)), (1 + (2 + 3))
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ae202e9 and 60dd753.

⛔ Files ignored due to path filters (2)
  • internal/parser/__snapshots__/parser_fault_tolerance_test.snap is excluded by !**/*.snap, !**/*.snap
  • internal/parser/__snapshots__/parser_test.snap is excluded by !**/*.snap, !**/*.snap
📒 Files selected for processing (7)
  • Numscript.g4 (1 hunks)
  • internal/parser/antlr/Numscript.interp (1 hunks)
  • internal/parser/antlr/numscript_base_listener.go (1 hunks)
  • internal/parser/antlr/numscript_listener.go (2 hunks)
  • internal/parser/antlr/numscript_parser.go (48 hunks)
  • internal/parser/parser.go (1 hunks)
  • internal/parser/parser_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (32)
internal/parser/antlr/numscript_parser.go (24)

57-57: No concerns about the updated serialized ATN segment.
These changes appear to be auto-generated from the grammar updates.


62-68: No concerns about the updated serialized ATN data.
Again, these lines appear auto-generated.


69-69: No concerns.
This line also appears auto-generated from grammar updates.


74-74: No concerns.
Automatically generated content.


80-81: No concerns.
Auto-generated changes for parser initialization.


90-90: No concerns.
Likewise auto-generated line for the parser.


96-96: No concerns.
Continues the auto-generated changes.


99-99: No concerns.
Auto-generated.


103-105: No concerns.
Parser synchronization steps—auto-generated.


107-107: No concerns.
Auto-generated code.


108-110: No concerns.
Continues standard parser logic.


121-128: No concerns.
These lines define portion/variable syntax, appear to be standard auto-generated code.


140-140: No concerns.
Remains part of the parser's internal data table.


148-148: No concerns.
Automatic table updates.


157-157: No concerns.
Auto-generated indexing for parser data.


166-166: No concerns.
Standard parser code, auto-generated.


177-177: No concerns.
Maintains the parser simulator state.


185-185: No concerns.
Still auto-generated logic.


192-192: No concerns.
Reflects standard parser logic.


199-199: No concerns.
No functional risk identified.


218-218: No concerns.
Continues auto-generated steps.


819-819: No concerns.
Routine parser logic.


909-961: Implementation of ParenthesizedExprContext
These lines correctly introduce a new parse context for parenthesized expressions. The structure appears coherent, with proper getters and rule overrides.


1186-1206: Grammar extension to handle parentheses in valueExpr
This switch case cleanly inserts a new branch for matching LPARENS ... RPARENS. The code checks out logically and helps parse nested expressions.

Numscript.g4 (1)

60-61: LGTM! The grammar changes correctly implement parenthesized expressions.

The changes look good:

  1. The infix expression rule correctly uses left recursion for operator precedence.
  2. The new parenthesized expression rule properly allows wrapping any valueExpr in parentheses, enabling expression grouping.
internal/parser/antlr/Numscript.interp (2)

104-104: LGTM! Auto-generated changes.

The changes to the ATN array are auto-generated by ANTLR and correctly reflect the grammar updates.


1-104: Skip review of auto-generated file.

This is an auto-generated file by ANTLR and should not be modified manually. The changes in the atn array are a result of grammar modifications in Numscript.g4.

internal/parser/antlr/numscript_listener.go (2)

35-37: LGTM! The listener interface is correctly extended.

The new methods for handling parenthesized expressions follow the established pattern and are properly documented.

Also applies to: 158-160


1-257: Skip review of auto-generated file.

This is an auto-generated file by ANTLR and should not be modified manually. The new methods EnterParenthesizedExpr and ExitParenthesizedExpr are correctly generated based on the grammar modifications.

internal/parser/antlr/numscript_base_listener.go (1)

72-76: LGTM!

The new methods for handling parenthesized expressions are correctly implemented following the base listener pattern.

internal/parser/parser.go (1)

307-309: LGTM!

The implementation correctly handles parenthesized expressions by recursively parsing the inner expression. This approach preserves the expression's semantics and follows the established pattern for handling other expression types.

internal/parser/parser_test.go (1)

340-346: LGTM! Test case verifies parentheses support.

The test case is well-structured and effectively verifies the parsing of expressions with parentheses, ensuring correct operator precedence.

@ascandone ascandone requested review from gfyrag, a team and altitude February 3, 2025 14:25
@ascandone ascandone enabled auto-merge (squash) February 6, 2025 17:12
@ascandone ascandone merged commit 4ec528b into main Feb 6, 2025
6 checks passed
@ascandone ascandone deleted the feat/syntax-parenthesis-in-expr branch February 6, 2025 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants