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

Reset parser context in parenthesized expression #10994

Merged
merged 2 commits into from
Apr 17, 2024

Conversation

dhruvmanila
Copy link
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
Copy link

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

Copy link
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
5 checks passed
@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.

None yet

2 participants