Skip to content

Recover from unexpected token at end of expr#10981

Merged
dhruvmanila merged 1 commit intodhruv/parserfrom
dhruv/expr-end-recovery
Apr 16, 2024
Merged

Recover from unexpected token at end of expr#10981
dhruvmanila merged 1 commit intodhruv/parserfrom
dhruv/expr-end-recovery

Conversation

@dhruvmanila
Copy link
Member

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:

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 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.
@dhruvmanila dhruvmanila added bug Something isn't working parser Related to the parser labels Apr 16, 2024
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

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

@github-actions
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@dhruvmanila dhruvmanila merged commit 38fb3d4 into dhruv/parser Apr 16, 2024
@dhruvmanila dhruvmanila deleted the dhruv/expr-end-recovery branch April 16, 2024 19:01
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants