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:
Dhruv Manilawala 2024-04-18 17:57:39 +05:30 committed by GitHub
parent e09180b1df
commit 13ffb5bc19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
852 changed files with 112948 additions and 103620 deletions

View file

@ -0,0 +1,4 @@
a: int # simple
(a): int
a.b: int
a[0]: int

View file

@ -0,0 +1,3 @@
x = y = z = 1; a, b
x = y = z = 1
a, b

View file

@ -0,0 +1 @@
async for target in iter: ...

View file

@ -0,0 +1 @@
async def foo(): ...

View file

@ -0,0 +1 @@
async with item: ...

View file

@ -0,0 +1,2 @@
class Foo: ...
class Foo(): ...

View file

@ -0,0 +1,2 @@
@decorator
async def foo(): ...

View file

@ -0,0 +1,3 @@
del a, b; c, d
del a, b
c, d

View file

@ -0,0 +1,2 @@
import a.b.c
import a . b . c

View file

@ -0,0 +1,2 @@
from.import x
from...import x

View file

@ -0,0 +1,5 @@
from a import (b, c)
from a import (b, c); x, y
from a import b, c; x, y
from a import b, c
x, y

View file

@ -0,0 +1,2 @@
f"hello {x:} world"
f"hello {x:.3f} world"

View file

@ -0,0 +1,4 @@
def foo(
first: int,
second: int,
) -> int: ...

View file

@ -0,0 +1,2 @@
def foo() -> (int,): ...
def foo() -> (int, str): ...

View file

@ -0,0 +1,4 @@
def foo() -> int | str: ...
def foo() -> lambda x: x: ...
def foo() -> (yield x): ...
def foo() -> int if True else str: ...

View file

@ -0,0 +1,2 @@
global x
global x, y, z

View file

@ -0,0 +1,3 @@
import a, b; import c, d
import a, b
c, d

View file

@ -0,0 +1 @@
lambda: 1

View file

@ -0,0 +1,6 @@
lambda x: x
lambda x: x if True else y
lambda x: await x
lambda x: lambda y: x + y
lambda x: (yield x) # Parenthesized `yield` is fine
lambda x: x, *y

View file

@ -0,0 +1,3 @@
match foo:
case foo_bar: ...
case _: ...

View file

@ -0,0 +1,3 @@
match subject:
case [a, b]: ...
case (a, b): ...

View file

@ -0,0 +1,5 @@
match subject:
case a: ...
case a if x: ...
case a, b: ...
case a, b if x: ...

View file

@ -0,0 +1,9 @@
match x := 1:
case _: ...
match (x := 1):
case _: ...
# Starred expressions are only allowed in tuple expression
match *x | y, z:
case _: ...
match await x:
case _: ...

View file

@ -0,0 +1,8 @@
match x:
case y if a := 1: ...
match x:
case y if a if True else b: ...
match x:
case y if lambda a: b: ...
match x:
case y if (yield x): ...

View file

@ -0,0 +1,2 @@
nonlocal x
nonlocal x, y, z

View file

@ -0,0 +1,4 @@
def foo(arg: int): ...
def foo(arg: lambda x: x): ...
def foo(arg: (yield x)): ...
def foo(arg: (x := int)): ...

View file

@ -0,0 +1,4 @@
def foo(x=lambda y: y): ...
def foo(x=1 if True else 2): ...
def foo(x=await y): ...
def foo(x=(yield y)): ...

View file

@ -0,0 +1,2 @@
def foo(*args: *int | str): ...
def foo(*args: *(int or str)): ...

View file

@ -0,0 +1,2 @@
def foo(a=10, *, b, c=11, d): ...
def foo(a=10, *args, b, c=11, d): ...

View file

@ -0,0 +1,2 @@
def foo(*, a, **kwargs): ...
def foo(*, a=10, **kwargs): ...

View file

@ -0,0 +1,2 @@
for (x in y)[0] in iter: ...
for (x in y).attr in iter: ...

View file

@ -0,0 +1,5 @@
if True: pass
if True: pass;
if True: pass; continue
if True: pass; continue;
x = 1

View file

@ -0,0 +1 @@
return; import a; from x import y; z; type T = int