mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 13:05:58 +00:00
Replace LALRPOP parser with hand-written parser (#10036)
(Supersedes #9152, authored by @LaBatata101) ## Summary This PR replaces the current parser generated from LALRPOP to a hand-written recursive descent parser. It also updates the grammar for [PEP 646](https://peps.python.org/pep-0646/) so that the parser outputs the correct AST. For example, in `data[*x]`, the index expression is now a tuple with a single starred expression instead of just a starred expression. Beyond the performance improvements, the parser is also error resilient and can provide better error messages. The behavior as seen by any downstream tools isn't changed. That is, the linter and formatter can still assume that the parser will _stop_ at the first syntax error. This will be updated in the following months. For more details about the change here, refer to the PR corresponding to the individual commits and the release blog post. ## Test Plan Write _lots_ and _lots_ of tests for both valid and invalid syntax and verify the output. ## Acknowledgements - @MichaReiser for reviewing 100+ parser PRs and continuously providing guidance throughout the project - @LaBatata101 for initiating the transition to a hand-written parser in #9152 - @addisoncrump for implementing the fuzzer which helped [catch](https://github.com/astral-sh/ruff/pull/10903) [a](https://github.com/astral-sh/ruff/pull/10910) [lot](https://github.com/astral-sh/ruff/pull/10966) [of](https://github.com/astral-sh/ruff/pull/10896) [bugs](https://github.com/astral-sh/ruff/pull/10877) --------- Co-authored-by: Victor Hugo Gomes <labatata101@linuxmail.org> Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
e09180b1df
commit
13ffb5bc19
852 changed files with 112948 additions and 103620 deletions
|
@ -0,0 +1,17 @@
|
|||
# Invalid target
|
||||
{x: y for 1 in y}
|
||||
{x: y for 'a' in y}
|
||||
{x: y for call() in y}
|
||||
{x: y for {a, b} in y}
|
||||
|
||||
# Invalid iter
|
||||
{x: y for x in *y}
|
||||
{x: y for x in yield y}
|
||||
{x: y for x in yield from y}
|
||||
{x: y for x in lambda y: y}
|
||||
|
||||
# Invalid if
|
||||
{x: y for x in data if *y}
|
||||
{x: y for x in data if yield y}
|
||||
{x: y for x in data if yield from y}
|
||||
{x: y for x in data if lambda y: y}
|
|
@ -0,0 +1,12 @@
|
|||
# Double star expression starts with bitwise OR precedence. Make sure we don't parse
|
||||
# the ones which are higher than that.
|
||||
|
||||
{**x := 1}
|
||||
{a: 1, **x if True else y}
|
||||
{**lambda x: x, b: 2}
|
||||
{a: 1, **x or y}
|
||||
{**x and y, b: 2}
|
||||
{a: 1, **not x, b: 2}
|
||||
{**x in y}
|
||||
{**x not in y}
|
||||
{**x < y}
|
|
@ -0,0 +1,9 @@
|
|||
# Double star means that the parser will try to parse it as a dictionary expression but
|
||||
# it's actually a comprehension.
|
||||
|
||||
{**x: y for x, y in data}
|
||||
|
||||
# TODO(dhruvmanila): This test case fails because there's no way to represent `**y`
|
||||
# in the AST. The parser tries to parse it as a binary expression but the range isn't
|
||||
# correct.
|
||||
# {x: **y for x, y in data}
|
|
@ -0,0 +1,4 @@
|
|||
{x:
|
||||
|
||||
def foo():
|
||||
pass
|
|
@ -0,0 +1,3 @@
|
|||
{x:
|
||||
|
||||
1 + 2
|
|
@ -0,0 +1,4 @@
|
|||
{x: 1,
|
||||
|
||||
def foo():
|
||||
pass
|
|
@ -0,0 +1,5 @@
|
|||
# Unparenthesized named expression not allowed in key
|
||||
|
||||
{x := 1: y, z := 2: a}
|
||||
|
||||
x + y
|
|
@ -0,0 +1,5 @@
|
|||
# Unparenthesized named expression not allowed in value
|
||||
|
||||
{x: y := 1, z: a := 2}
|
||||
|
||||
x + y
|
|
@ -0,0 +1,24 @@
|
|||
# Test cases for dictionary expressions where the parser recovers from a syntax error.
|
||||
|
||||
{,}
|
||||
|
||||
{1: 2,,3: 4}
|
||||
|
||||
{1: 2,,}
|
||||
|
||||
# Missing comma
|
||||
{1: 2 3: 4}
|
||||
|
||||
# No value
|
||||
{1: }
|
||||
|
||||
# No value for double star unpacking
|
||||
{**}
|
||||
{x: y, **, a: b}
|
||||
|
||||
# This is not a double star unpacking
|
||||
# {* *data}
|
||||
|
||||
# Star expression not allowed here
|
||||
{*x: y, z: a, *b: c}
|
||||
{x: *y, z: *a}
|
Loading…
Add table
Add a link
Reference in a new issue