Skip to content

Reset parser context in parenthesized expression#10994

Merged
dhruvmanila merged 2 commits intodhruv/parserfrom
dhruv/reset-ctx-in-parenthesized
Apr 17, 2024
Merged

Reset parser context in parenthesized expression#10994
dhruvmanila merged 2 commits intodhruv/parserfrom
dhruv/reset-ctx-in-parenthesized

Conversation

@dhruvmanila
Copy link
Copy Markdown
Member

Summary

This PR fixes the bug where the parser would fail to parse the following code:

for (x in y)[0] in z: ...

This is because when parsing the target of a for statement, the context flag for the parser is set to FOR_TARGET and this is used by binary expression parsing to stop the expression parsing when it sees the in keyword. But, in parenthesized context, this is allowed.

The solution implemented in this PR is to reset the parser context when parsing a parenthesized expression and set it back to the original context later.

An alternative solution would be to introduce ExpressionContext but that would require a lot of updates as almost all of expression parsing function would need to be updated to take this context. A benefit of this would be that both AllowStarredExpression and AllowNamedExpression can be merged in the context and everything around ParserCtxFlags can be removed. It could possibly also simplify certain parsing functions.

Test Plan

Add test cases and verified the snapshots.

@dhruvmanila dhruvmanila added bug Something isn't working parser Related to the parser labels Apr 17, 2024
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 17, 2024

CodSpeed Performance Report

Merging #10994 will not alter performance

Comparing dhruv/reset-ctx-in-parenthesized (c4a2752) with dhruv/parser (c30057a)

Summary

✅ 30 untouched benchmarks

@dhruvmanila dhruvmanila force-pushed the dhruv/reset-ctx-in-parenthesized branch from d52a6ce to a4fb8c0 Compare April 17, 2024 11:51
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 17, 2024

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 e7f06eb into dhruv/parser Apr 17, 2024
@dhruvmanila dhruvmanila deleted the dhruv/reset-ctx-in-parenthesized branch April 17, 2024 12:45
dhruvmanila added a commit that referenced this pull request Apr 18, 2024
## Summary

This PR fixes the bug where the parser would fail to parse the following
code:

```python
for (x in y)[0] in z: ...
```

This is because when parsing the target of a `for` statement, the
context flag for the parser is set to `FOR_TARGET` and this is used by
binary expression parsing to stop the expression parsing when it sees
the `in` keyword. But, in parenthesized context, this is allowed.

The solution implemented in this PR is to reset the parser context when
parsing a parenthesized expression and set it back to the original
context later.

An alternative solution would be to introduce
[`ExpressionContext`](https://github.com/biomejs/biome/blob/838ccb442370e72197e625a7d0e4456e6e77e498/crates/biome_js_parser/src/syntax/expr.rs#L42-L43)
but that would require a lot of updates as almost all of expression
parsing function would need to be updated to take this context. A
benefit of this would be that both `AllowStarredExpression` and
`AllowNamedExpression` can be merged in the context and everything
around `ParserCtxFlags` can be removed. It could possibly also simplify
certain parsing functions.

## Test Plan

Add test cases 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