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,32 @@
# This file contains test cases where the with items has an ambiguous left parenthesis.
# These cases should raise the correct syntax error and recover properly.
with (item1, item2),: ...
with (item1, item2), as f: ...
with (item1, item2), item3,: ...
with (*item): ...
with (*item) as f: ...
with (item := 10 as f): ...
with (item1, item2 := 10 as f): ...
with (x for x in range(10), item): ...
with (item, x for x in range(10)): ...
# Make sure the parser doesn't report the same error twice
with ((*item)): ...
with (*x for x in iter, item): ...
with (item1, *x for x in iter, item2): ...
with (x as f, *y): ...
with (*x, y as f): ...
with (x, yield y): ...
with (x, yield y, z): ...
with (x, yield from y): ...
with (x as f, y) as f: ...
with (x for x in iter as y): ...
# The inner `(...)` is parsed as parenthesized expression
with ((item as f)): ...
with (item as f), x: ...
with (item as f1) as f2: ...
with (item1 as f, item2 := 0): ...

View file

@ -0,0 +1,6 @@
# There are no with items present.
# The parser should recover from this syntax error.
with : ...
x + y

View file

@ -0,0 +1,9 @@
# For parenthesized with items test cases, refer to `./ambiguous_lpar_with_items.py`
with item,: pass
with item as x,: pass
with *item: pass
with *item as x: pass
with *item1, item2 as f: pass
with item1 as f, *item2: pass
with item := 0 as f: pass