mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-12 23:55:19 +00:00

(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>
99 lines
1.2 KiB
Python
99 lines
1.2 KiB
Python
try:
|
|
...
|
|
except:
|
|
...
|
|
|
|
try:
|
|
...
|
|
except Exception1 as e:
|
|
...
|
|
except Exception2 as e:
|
|
...
|
|
|
|
try:
|
|
...
|
|
except Exception as e:
|
|
...
|
|
except:
|
|
...
|
|
finally:
|
|
...
|
|
|
|
try:
|
|
...
|
|
except:
|
|
...
|
|
else:
|
|
...
|
|
|
|
try:
|
|
...
|
|
except:
|
|
...
|
|
else:
|
|
...
|
|
finally:
|
|
...
|
|
|
|
try:
|
|
...
|
|
finally:
|
|
...
|
|
|
|
try:
|
|
...
|
|
else:
|
|
...
|
|
finally:
|
|
...
|
|
|
|
try:
|
|
...
|
|
except* GroupA as eg:
|
|
...
|
|
except* ExceptionGroup:
|
|
...
|
|
|
|
try:
|
|
raise ValueError(1)
|
|
except TypeError as e:
|
|
print(f"caught {type(e)}")
|
|
except OSError as e:
|
|
print(f"caught {type(e)}")
|
|
|
|
try:
|
|
raise ExceptionGroup("eg", [ValueError(1), TypeError(2), OSError(3), OSError(4)])
|
|
except* TypeError as e:
|
|
print(f"caught {type(e)} with nested {e.exceptions}")
|
|
except* OSError as e:
|
|
print(f"caught {type(e)} with nested {e.exceptions}")
|
|
|
|
try:
|
|
pass
|
|
except "exception":
|
|
pass
|
|
except 1:
|
|
pass
|
|
except True:
|
|
pass
|
|
except 1 + 1:
|
|
pass
|
|
except a | b:
|
|
pass
|
|
except x and y:
|
|
pass
|
|
except await x:
|
|
pass
|
|
except lambda x: x:
|
|
pass
|
|
except x if True else y:
|
|
pass
|
|
|
|
if True:
|
|
try:
|
|
pass
|
|
finally:
|
|
pass
|
|
# This `else` is not part of the `try` statement, so don't raise an error
|
|
else:
|
|
pass
|