Recover from unexpected token at end of expr#10981
Merged
dhruvmanila merged 1 commit intodhruv/parserfrom Apr 16, 2024
Merged
Conversation
This fixes a bug in the parser when `Mode::Expression` is used where the parser didn't recover from unexpected token after the expression. The current solution isn't ideal as the parser will drop all the remaining tokens after the initial expression has been parsed. This is to avoid panicking especially in the context of forward annotation like: ```python x: Literal["foo", "bar baz"] ``` Here, the parser is being given the `bar baz` as the source to be parsed using `Mode::Expression`. The parser will parse `bar` as a name expression and then stop because the expression ends there. But, it seems like there's more tokens remaining. This specific case could potentially be recovered by using a `ExprTuple` and raising a "missing comma" error but I've avoided that complexity for now.
MichaReiser
approved these changes
Apr 16, 2024
Member
MichaReiser
left a comment
There was a problem hiding this comment.
The way I solved this in RomeJS is by what you describe. It parses an array expression. You could also look at how TypeScript's parseExpression recovers. But I'm okay doing this in a follow up
Contributor
|
18 tasks
dhruvmanila
added a commit
that referenced
this pull request
Apr 17, 2024
## Summary This fixes a bug in the parser when `Mode::Expression` is used where the parser didn't recover from unexpected token after the expression. The current solution isn't ideal as the parser will drop all the remaining tokens after the initial expression has been parsed. This is to avoid panicking especially in the context of forward annotation like: ```python x: Literal["foo", "bar baz"] ``` Here, the parser is being given the `bar baz` as the source to be parsed using `Mode::Expression`. The parser will parse `bar` as a name expression and then stop because the expression ends there. But, it seems like there's more tokens remaining. This specific case could potentially be recovered by using a `ExprTuple` and raising a "missing comma" error but I've avoided that complexity for now. ## Test Plan Add new test cases for `Mode::Expression` and verified the snapshots.
dhruvmanila
added a commit
that referenced
this pull request
Apr 18, 2024
## Summary This fixes a bug in the parser when `Mode::Expression` is used where the parser didn't recover from unexpected token after the expression. The current solution isn't ideal as the parser will drop all the remaining tokens after the initial expression has been parsed. This is to avoid panicking especially in the context of forward annotation like: ```python x: Literal["foo", "bar baz"] ``` Here, the parser is being given the `bar baz` as the source to be parsed using `Mode::Expression`. The parser will parse `bar` as a name expression and then stop because the expression ends there. But, it seems like there's more tokens remaining. This specific case could potentially be recovered by using a `ExprTuple` and raising a "missing comma" error but I've avoided that complexity for now. ## Test Plan Add new test cases for `Mode::Expression` and verified the snapshots.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This fixes a bug in the parser when
Mode::Expressionis used where the parser didn't recover from unexpected token after the expression.The current solution isn't ideal as the parser will drop all the remaining tokens after the initial expression has been parsed. This is to avoid panicking especially in the context of forward annotation like:
Here, the parser is being given the
bar bazas the source to be parsed usingMode::Expression. The parser will parsebaras a name expression and then stop because the expression ends there. But, it seems like there's more tokens remaining. This specific case could potentially be recovered by using aExprTupleand raising a "missing comma" error but I've avoided that complexity for now.Test Plan
Add new test cases for
Mode::Expressionand verified the snapshots.