ruff/crates/ruff_python_parser
Dhruv Manilawala 8f40928534
Enable token-based rules on source with syntax errors (#11950)
## Summary

This PR updates the linter, specifically the token-based rules, to work
on the tokens that come after a syntax error.

For context, the token-based rules only diagnose the tokens up to the
first lexical error. This PR builds up an error resilience by
introducing a `TokenIterWithContext` which updates the `nesting` level
and tries to reflect it with what the lexer is seeing. This isn't 100%
accurate because if the parser recovered from an unclosed parenthesis in
the middle of the line, the context won't reduce the nesting level until
it sees the newline token at the end of the line.

resolves: #11915

## Test Plan

* Add test cases for a bunch of rules that are affected by this change.
* Run the fuzzer for a long time, making sure to fix any other bugs.
2024-07-02 08:57:46 +00:00
..
resources Consider 2-character EOL before line continuation (#12035) 2024-06-26 14:00:48 +05:30
src Enable token-based rules on source with syntax errors (#11950) 2024-07-02 08:57:46 +00:00
tests Use CompactString for Identifier (#12101) 2024-07-01 10:06:02 +02:00
Cargo.toml Use CompactString for Identifier (#12101) 2024-07-01 10:06:02 +02:00
CONTRIBUTING.md Add basic docs for the parser crate (#11199) 2024-04-29 17:08:07 +00:00
README.md Add basic docs for the parser crate (#11199) 2024-04-29 17:08:07 +00:00

Ruff Python Parser

Ruff's Python parser is a hand-written recursive descent parser which can parse Python source code into an Abstract Syntax Tree (AST). It also utilizes the Pratt parsing technique to parse expressions with different precedence.

Try out the parser in the playground.

Python version support

The parser supports the latest Python syntax, which is currently Python 3.12. It does not throw syntax errors if it encounters a syntax feature that is not supported by the target-version. This will be fixed in a future release (see https://github.com/astral-sh/ruff/issues/6591).

Contributing

Refer to the contributing guidelines to get started and GitHub issues with the parser label for issues that need help.