mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 09:31:09 +00:00
Use speculative parsing for with-items (#11770)
## Summary This PR updates the with-items parsing logic to use speculative parsing instead. ### Existing logic First, let's understand the previous logic: 1. The parser sees `(`, it doesn't know whether it's part of a parenthesized with items or a parenthesized expression 2. Consider it a parenthesized with items and perform a hand-rolled speculative parsing 3. Then, verify the assumption and if it's incorrect convert the parsed with items into an appropriate expression which becomes part of the first with item Here, in (3) there are lots of edge cases which we've to deal with: 1. Trailing comma with a single element should be [converted to the expression as is](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2140-L2153)
) 2. Trailing comma with multiple elements should be [converted to a tuple expression](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2155-L2178)
) 3. Limit the allowed expression based on whether it's [(1)](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2144-L2152)
) or [(2)](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2157-L2171)
) 4. [Consider postfix expressions](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2181-L2200)
) after (3) 5. [Consider `if` expressions](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2203-L2208)
) after (3) 6. [Consider binary expressions](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2210-L2228)
) after (3) Consider other cases like * [Single generator expression](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2020-L2035)
) * [Expecting a comma](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2122-L2130)
) And, this is all possible only if we allow parsing these expressions in the [with item parsing logic](9b2cf569b2/crates/ruff_python_parser/src/parser/statement.rs (L2287-L2334)
). ### Speculative parsing With #11457 merged, we can simplify this logic by changing the step (3) from above to just rewind the parser back to the `(` if our assumption (parenthesized with-items) was incorrect and then continue parsing it considering parenthesized expression. This also behaves a lot similar to what a PEG parser does which is to consider the first grammar rule and if it fails consider the second grammar rule and so on. resolves: #11639 ## Test Plan - [x] Verify the updated snapshots - [x] Run the fuzzer on around 3000 valid source code (locally)
This commit is contained in:
parent
5a5a588a72
commit
6c1fa1d440
9 changed files with 556 additions and 731 deletions
|
@ -705,16 +705,6 @@ enum WithItemKind {
|
|||
/// The parentheses belongs to the context expression.
|
||||
ParenthesizedExpression,
|
||||
|
||||
/// A list of `with` items that has only one item which is a parenthesized
|
||||
/// generator expression.
|
||||
///
|
||||
/// ```python
|
||||
/// with (x for x in range(10)): ...
|
||||
/// ```
|
||||
///
|
||||
/// The parentheses belongs to the generator expression.
|
||||
SingleParenthesizedGeneratorExpression,
|
||||
|
||||
/// The `with` items aren't parenthesized in any way.
|
||||
///
|
||||
/// ```python
|
||||
|
@ -732,20 +722,15 @@ impl WithItemKind {
|
|||
const fn list_terminator(self) -> TokenKind {
|
||||
match self {
|
||||
WithItemKind::Parenthesized => TokenKind::Rpar,
|
||||
WithItemKind::Unparenthesized
|
||||
| WithItemKind::ParenthesizedExpression
|
||||
| WithItemKind::SingleParenthesizedGeneratorExpression => TokenKind::Colon,
|
||||
WithItemKind::Unparenthesized | WithItemKind::ParenthesizedExpression => {
|
||||
TokenKind::Colon
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the `with` item is a parenthesized expression i.e., the
|
||||
/// parentheses belong to the context expression.
|
||||
const fn is_parenthesized_expression(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
WithItemKind::ParenthesizedExpression
|
||||
| WithItemKind::SingleParenthesizedGeneratorExpression
|
||||
)
|
||||
/// Returns `true` if the with items are parenthesized.
|
||||
const fn is_parenthesized(self) -> bool {
|
||||
matches!(self, WithItemKind::Parenthesized)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1172,7 +1157,6 @@ bitflags! {
|
|||
const LAMBDA_PARAMETERS = 1 << 24;
|
||||
const WITH_ITEMS_PARENTHESIZED = 1 << 25;
|
||||
const WITH_ITEMS_PARENTHESIZED_EXPRESSION = 1 << 26;
|
||||
const WITH_ITEMS_SINGLE_PARENTHESIZED_GENERATOR_EXPRESSION = 1 << 27;
|
||||
const WITH_ITEMS_UNPARENTHESIZED = 1 << 28;
|
||||
const F_STRING_ELEMENTS = 1 << 29;
|
||||
}
|
||||
|
@ -1225,9 +1209,6 @@ impl RecoveryContext {
|
|||
WithItemKind::ParenthesizedExpression => {
|
||||
RecoveryContext::WITH_ITEMS_PARENTHESIZED_EXPRESSION
|
||||
}
|
||||
WithItemKind::SingleParenthesizedGeneratorExpression => {
|
||||
RecoveryContext::WITH_ITEMS_SINGLE_PARENTHESIZED_GENERATOR_EXPRESSION
|
||||
}
|
||||
WithItemKind::Unparenthesized => RecoveryContext::WITH_ITEMS_UNPARENTHESIZED,
|
||||
},
|
||||
RecoveryContextKind::FStringElements => RecoveryContext::F_STRING_ELEMENTS,
|
||||
|
@ -1294,9 +1275,6 @@ impl RecoveryContext {
|
|||
RecoveryContext::WITH_ITEMS_PARENTHESIZED_EXPRESSION => {
|
||||
RecoveryContextKind::WithItems(WithItemKind::ParenthesizedExpression)
|
||||
}
|
||||
RecoveryContext::WITH_ITEMS_SINGLE_PARENTHESIZED_GENERATOR_EXPRESSION => {
|
||||
RecoveryContextKind::WithItems(WithItemKind::SingleParenthesizedGeneratorExpression)
|
||||
}
|
||||
RecoveryContext::WITH_ITEMS_UNPARENTHESIZED => {
|
||||
RecoveryContextKind::WithItems(WithItemKind::Unparenthesized)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue