diff --git a/Cargo.lock b/Cargo.lock index 716090659d08b..b96eb093eab72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2327,9 +2327,11 @@ dependencies = [ name = "ruff_python_parser" version = "0.0.0" dependencies = [ + "annotate-snippets 0.9.2", "anyhow", "bitflags 2.4.2", "bstr", + "drop_bomb", "insta", "is-macro", "itertools 0.12.1", @@ -2337,6 +2339,7 @@ dependencies = [ "lalrpop-util", "memchr", "ruff_python_ast", + "ruff_source_file", "ruff_text_size", "rustc-hash", "static_assertions", diff --git a/crates/ruff/tests/format.rs b/crates/ruff/tests/format.rs index c04eb21db4c9d..596711eb5cf0d 100644 --- a/crates/ruff/tests/format.rs +++ b/crates/ruff/tests/format.rs @@ -521,7 +521,7 @@ from module import = ----- stdout ----- ----- stderr ----- - error: Failed to parse main.py:2:20: Unexpected token '=' + error: Failed to parse main.py:2:20: expecting an identifier "###); Ok(()) diff --git a/crates/ruff/tests/integration_test.rs b/crates/ruff/tests/integration_test.rs index f9e9c221acdd5..5b12c5ecd71e4 100644 --- a/crates/ruff/tests/integration_test.rs +++ b/crates/ruff/tests/integration_test.rs @@ -727,11 +727,11 @@ fn stdin_parse_error() { success: false exit_code: 1 ----- stdout ----- - -:1:17: E999 SyntaxError: Unexpected token '=' + -:1:17: E999 SyntaxError: expecting an identifier Found 1 error. ----- stderr ----- - error: Failed to parse at 1:17: Unexpected token '=' + error: Failed to parse at 1:17: expecting an identifier "###); } diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index 6e44c85c154d4..a4c2a9b4c780a 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -134,6 +134,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) { elts, ctx, range: _, + parenthesized: _, }) | Expr::List(ast::ExprList { elts, diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index e858b53899e2f..c287218c31c7f 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -1298,6 +1298,7 @@ where elts, ctx, range: _, + parenthesized: _, }) = slice.as_ref() { let mut iter = elts.iter(); diff --git a/crates/ruff_linter/src/logging.rs b/crates/ruff_linter/src/logging.rs index b97193a717bf5..7710603781d76 100644 --- a/crates/ruff_linter/src/logging.rs +++ b/crates/ruff_linter/src/logging.rs @@ -194,7 +194,7 @@ impl DisplayParseError { // Translate the byte offset to a location in the originating source. let location = if let Some(jupyter_index) = source_kind.as_ipy_notebook().map(Notebook::index) { - let source_location = source_code.source_location(error.offset); + let source_location = source_code.source_location(error.location.start()); ErrorLocation::Cell( jupyter_index @@ -208,7 +208,7 @@ impl DisplayParseError { }, ) } else { - ErrorLocation::File(source_code.source_location(error.offset)) + ErrorLocation::File(source_code.source_location(error.location.start())) }; Self { @@ -275,27 +275,7 @@ impl<'a> DisplayParseErrorType<'a> { impl Display for DisplayParseErrorType<'_> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - match self.0 { - ParseErrorType::Eof => write!(f, "Expected token but reached end of file."), - ParseErrorType::ExtraToken(ref tok) => write!( - f, - "Got extraneous token: {tok}", - tok = TruncateAtNewline(&tok) - ), - ParseErrorType::InvalidToken => write!(f, "Got invalid token"), - ParseErrorType::UnrecognizedToken(ref tok, ref expected) => { - if let Some(expected) = expected.as_ref() { - write!( - f, - "Expected '{expected}', but got {tok}", - tok = TruncateAtNewline(&tok) - ) - } else { - write!(f, "Unexpected token {tok}", tok = TruncateAtNewline(&tok)) - } - } - ParseErrorType::Lexical(ref error) => write!(f, "{error}"), - } + write!(f, "{}", TruncateAtNewline(&self.0)) } } diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs index 8357b6670b38c..3f708002e2c7e 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs @@ -109,6 +109,7 @@ fn type_pattern(elts: Vec<&Expr>) -> Expr { elts: elts.into_iter().cloned().collect(), ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, } .into() } diff --git a/crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs b/crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs index b3031ff97e617..ba2dc033cf94e 100644 --- a/crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs +++ b/crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs @@ -173,6 +173,7 @@ pub(crate) fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) { .collect(), ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }); let node1 = Expr::Name(ast::ExprName { id: arg_name.into(), diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs index 08040938a7eca..b3a49d819732c 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs @@ -72,6 +72,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp elts, range: _, ctx: _, + parenthesized: _, }) = slice.as_ref() { for expr in elts { @@ -123,6 +124,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp elts: literal_exprs.into_iter().cloned().collect(), range: TextRange::default(), ctx: ExprContext::Load, + parenthesized: true, })), range: TextRange::default(), ctx: ExprContext::Load, @@ -148,6 +150,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp elts, range: TextRange::default(), ctx: ExprContext::Load, + parenthesized: true, })), range: TextRange::default(), ctx: ExprContext::Load, diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs index caf00e7a219d6..d4cd2cde2c9a8 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs @@ -130,6 +130,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr) .collect(), ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, })), ctx: ExprContext::Load, range: TextRange::default(), @@ -151,6 +152,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr) elts: exprs.into_iter().cloned().collect(), ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, })), ctx: ExprContext::Load, range: TextRange::default(), diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs index 9ac12913539d9..fd4fc2b50cf6f 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs @@ -74,6 +74,8 @@ fn is_empty_or_null_fstring_element(element: &ast::FStringElement) -> bool { ast::FStringElement::Expression(ast::FStringExpressionElement { expression, .. }) => { is_empty_or_null_string(expression) } + #[allow(deprecated)] + ast::FStringElement::Invalid(_) => false, } } diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs index eb2608ff79792..8a575dc9ff52d 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs @@ -337,6 +337,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { .collect(), ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }); diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( format!("({})", checker.generator().expr(&node)), @@ -444,6 +445,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { elts: elts.clone(), ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }); diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( format!("({})", checker.generator().expr(&node)), diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs index 5256d69e2f752..88a08dd38e6eb 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs @@ -428,6 +428,7 @@ pub(crate) fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) { .collect(), ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }; let node1 = ast::ExprName { id: "isinstance".into(), @@ -543,6 +544,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) { elts: comparators.into_iter().map(Clone::clone).collect(), ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }; let node1 = ast::ExprName { id: id.into(), @@ -718,7 +720,7 @@ fn get_short_circuit_edit( generator.expr(expr) }; Edit::range_replacement( - if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _}) if !elts.is_empty()) + if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _, parenthesized: _}) if !elts.is_empty()) { format!("({content})") } else { diff --git a/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_sorted.py.snap b/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_sorted.py.snap index ed369f0fd61f0..3e1ab5ae4c441 100644 --- a/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_sorted.py.snap +++ b/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_sorted.py.snap @@ -1,4 +1,17 @@ --- source: crates/ruff_linter/src/rules/isort/mod.rs --- +bom_sorted.py:1:1: I001 [*] Import block is un-sorted or un-formatted + | +1 | import bar + | _^ +2 | | import foo + | + = help: Organize imports + +ℹ Safe fix +1 |-import bar +2 |-import foo + 1 |+import rt + diff --git a/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_unsorted.py.snap b/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_unsorted.py.snap index 4cff110affd90..4f49a26ac50c0 100644 --- a/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_unsorted.py.snap +++ b/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_unsorted.py.snap @@ -12,7 +12,6 @@ bom_unsorted.py:1:1: I001 [*] Import block is un-sorted or un-formatted ℹ Safe fix 1 |-import foo 2 |-import bar - 1 |+import bar - 2 |+import foo + 1 |+import rt diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/errors.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/errors.rs index da02f458a8cbd..5ca8e790b081a 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/errors.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/errors.rs @@ -81,7 +81,7 @@ pub(crate) fn syntax_error( parse_error: &ParseError, locator: &Locator, ) { - let rest = locator.after(parse_error.offset); + let rest = locator.after(parse_error.location.start()); // Try to create a non-empty range so that the diagnostic can print a caret at the // right position. This requires that we retrieve the next character, if any, and take its length @@ -95,6 +95,6 @@ pub(crate) fn syntax_error( SyntaxError { message: format!("{}", DisplayParseErrorType::new(&parse_error.error)), }, - TextRange::at(parse_error.offset, len), + TextRange::at(parse_error.location.start(), len), )); } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E999_E999.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E999_E999.py.snap index 767f6e4a0ab41..06fb17d2a110f 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E999_E999.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E999_E999.py.snap @@ -1,12 +1,8 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E999.py:3:1: E999 SyntaxError: unindent does not match any outer indentation level - | -2 | def x(): -3 | - | ^ E999 -4 | - | +E999.py:5:1: E999 SyntaxError: expected a single statement or an indented body after function definition + | + | diff --git a/crates/ruff_linter/src/rules/pylint/rules/assert_on_string_literal.rs b/crates/ruff_linter/src/rules/pylint/rules/assert_on_string_literal.rs index 5f0801731f695..b255112d3561c 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/assert_on_string_literal.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/assert_on_string_literal.rs @@ -77,7 +77,10 @@ pub(crate) fn assert_on_string_literal(checker: &mut Checker, test: &Expr) { ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) => value.is_empty(), - ast::FStringElement::Expression(_) => false, + #[allow(deprecated)] + ast::FStringElement::Expression(_) | ast::FStringElement::Invalid(_) => { + false + } }) } }) { @@ -89,7 +92,10 @@ pub(crate) fn assert_on_string_literal(checker: &mut Checker, test: &Expr) { ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) => !value.is_empty(), - ast::FStringElement::Expression(_) => false, + #[allow(deprecated)] + ast::FStringElement::Expression(_) | ast::FStringElement::Invalid(_) => { + false + } }) } }) { diff --git a/crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs b/crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs index 0c85d1a8ed0c4..dc86eb4dd28f6 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs @@ -305,6 +305,7 @@ fn assignment_targets_from_expr<'a>( ctx: ExprContext::Store, elts, range: _, + parenthesized: _, }) => Box::new( elts.iter() .flat_map(|elt| assignment_targets_from_expr(elt, dummy_variable_rgx)), diff --git a/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs b/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs index b4c5a792256d0..0a6bb4d3f346f 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs @@ -143,6 +143,7 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast: elts: comparators.iter().copied().cloned().collect(), range: TextRange::default(), ctx: ExprContext::Load, + parenthesized: true, })]), range: bool_op.range(), })), diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs index 8477e775b7649..dca5dfe715f3d 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs @@ -127,6 +127,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E elts: remaining, ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }; format!("({})", checker.generator().expr(&node.into())) }; diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs index 419e6bacacbc7..43ffafa3d5df2 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs @@ -141,6 +141,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E elts: remaining, ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }; format!("({})", checker.generator().expr(&node.into())) }; diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs index 44057726b7ecb..d31beff8a0747 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs @@ -200,5 +200,7 @@ fn is_allowed_value(expr: &Expr) -> bool { | Expr::Starred(_) | Expr::Slice(_) | Expr::IpyEscapeCommand(_) => false, + #[allow(deprecated)] + Expr::Invalid(_) => false, } } diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs index 29f9039d2109a..1966ce1b35055 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs @@ -127,6 +127,7 @@ pub(crate) fn non_pep695_type_alias(checker: &mut Checker, stmt: &StmtAnnAssign) range: TextRange::default(), elts: constraints.into_iter().cloned().collect(), ctx: ast::ExprContext::Load, + parenthesized: true, }))) } None => None, diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP027.py.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP027.py.snap index 4d4a97f89a81f..2bacb5d540775 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP027.py.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP027.py.snap @@ -1,114 +1,4 @@ --- source: crates/ruff_linter/src/rules/pyupgrade/mod.rs --- -UP027.py:2:17: UP027 [*] Replace unpacked list comprehension with a generator expression - | -1 | # Should change -2 | foo, bar, baz = [fn(x) for x in items] - | ^^^^^^^^^^^^^^^^^^^^^^ UP027 -3 | -4 | foo, bar, baz =[fn(x) for x in items] - | - = help: Replace with generator expression - -ℹ Safe fix -1 1 | # Should change -2 |-foo, bar, baz = [fn(x) for x in items] - 2 |+foo, bar, baz = (fn(x) for x in items) -3 3 | -4 4 | foo, bar, baz =[fn(x) for x in items] -5 5 | - -UP027.py:4:16: UP027 [*] Replace unpacked list comprehension with a generator expression - | -2 | foo, bar, baz = [fn(x) for x in items] -3 | -4 | foo, bar, baz =[fn(x) for x in items] - | ^^^^^^^^^^^^^^^^^^^^^^ UP027 -5 | -6 | foo, bar, baz = [fn(x) for x in items] - | - = help: Replace with generator expression - -ℹ Safe fix -1 1 | # Should change -2 2 | foo, bar, baz = [fn(x) for x in items] -3 3 | -4 |-foo, bar, baz =[fn(x) for x in items] - 4 |+foo, bar, baz =(fn(x) for x in items) -5 5 | -6 6 | foo, bar, baz = [fn(x) for x in items] -7 7 | - -UP027.py:6:26: UP027 [*] Replace unpacked list comprehension with a generator expression - | -4 | foo, bar, baz =[fn(x) for x in items] -5 | -6 | foo, bar, baz = [fn(x) for x in items] - | ^^^^^^^^^^^^^^^^^^^^^^ UP027 -7 | -8 | foo, bar, baz = [[i for i in fn(x)] for x in items] - | - = help: Replace with generator expression - -ℹ Safe fix -3 3 | -4 4 | foo, bar, baz =[fn(x) for x in items] -5 5 | -6 |-foo, bar, baz = [fn(x) for x in items] - 6 |+foo, bar, baz = (fn(x) for x in items) -7 7 | -8 8 | foo, bar, baz = [[i for i in fn(x)] for x in items] -9 9 | - -UP027.py:8:17: UP027 [*] Replace unpacked list comprehension with a generator expression - | - 6 | foo, bar, baz = [fn(x) for x in items] - 7 | - 8 | foo, bar, baz = [[i for i in fn(x)] for x in items] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP027 - 9 | -10 | foo, bar, baz = [ - | - = help: Replace with generator expression - -ℹ Safe fix -5 5 | -6 6 | foo, bar, baz = [fn(x) for x in items] -7 7 | -8 |-foo, bar, baz = [[i for i in fn(x)] for x in items] - 8 |+foo, bar, baz = ([i for i in fn(x)] for x in items) -9 9 | -10 10 | foo, bar, baz = [ -11 11 | fn(x) - -UP027.py:10:17: UP027 [*] Replace unpacked list comprehension with a generator expression - | - 8 | foo, bar, baz = [[i for i in fn(x)] for x in items] - 9 | -10 | foo, bar, baz = [ - | _________________^ -11 | | fn(x) -12 | | for x in items -13 | | ] - | |_^ UP027 -14 | -15 | # Should not change - | - = help: Replace with generator expression - -ℹ Safe fix -7 7 | -8 8 | foo, bar, baz = [[i for i in fn(x)] for x in items] -9 9 | -10 |-foo, bar, baz = [ - 10 |+foo, bar, baz = ( -11 11 | fn(x) -12 12 | for x in items -13 |-] - 13 |+) -14 14 | -15 15 | # Should not change -16 16 | foo = [fn(x) for x in items] - diff --git a/crates/ruff_linter/src/rules/refurb/rules/bit_count.rs b/crates/ruff_linter/src/rules/refurb/rules/bit_count.rs index 54405ff1730aa..9922fb4f08f9d 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/bit_count.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/bit_count.rs @@ -160,6 +160,8 @@ pub(crate) fn bit_count(checker: &mut Checker, call: &ExprCall) { | Expr::EllipsisLiteral(_) | Expr::Attribute(_) | Expr::Subscript(_) => false, + #[allow(deprecated)] + Expr::Invalid(_) => false, }; let replacement = if parenthesize { diff --git a/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs b/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs index d1fc37a7239fe..1eb6bbaf6ff2d 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs @@ -347,6 +347,7 @@ fn make_suggestion(group: &AppendGroup, generator: Generator) -> String { elts, ctx: ast::ExprContext::Load, range: TextRange::default(), + parenthesized: true, }; // Make `var.extend`. // NOTE: receiver is the same for all appends and that's why we can take the first. diff --git a/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs b/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs index 263ff68dc9258..db8913497535b 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs @@ -162,6 +162,7 @@ fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> { elts: new_elts, ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, } .into(), }; diff --git a/crates/ruff_linter/src/rules/ruff/rules/never_union.rs b/crates/ruff_linter/src/rules/ruff/rules/never_union.rs index 897a0e4855077..45abda9067641 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/never_union.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/never_union.rs @@ -116,6 +116,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) { elts, ctx: _, range: _, + parenthesized: _, }) = slice.as_ref() else { return; @@ -157,6 +158,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) { elts: rest, ctx: ast::ExprContext::Load, range: TextRange::default(), + parenthesized: true, })), ctx: ast::ExprContext::Load, range: TextRange::default(), diff --git a/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs b/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs index c84b00ce4a124..5fee26b662dcc 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs @@ -150,7 +150,7 @@ impl SequenceKind<'_> { Self::List => ("[", "]"), Self::Set => ("{", "}"), Self::Tuple(ast_node) => { - if ast_node.is_parenthesized(source) { + if ast_node.parenthesized { ("(", ")") } else { ("", "") diff --git a/crates/ruff_python_ast/src/comparable.rs b/crates/ruff_python_ast/src/comparable.rs index 344bb615ce95e..0e8052f4ad7db 100644 --- a/crates/ruff_python_ast/src/comparable.rs +++ b/crates/ruff_python_ast/src/comparable.rs @@ -234,6 +234,7 @@ pub enum ComparablePattern<'a> { MatchStar(PatternMatchStar<'a>), MatchAs(PatternMatchAs<'a>), MatchOr(PatternMatchOr<'a>), + Invalid, } impl<'a> From<&'a ast::Pattern> for ComparablePattern<'a> { @@ -286,6 +287,8 @@ impl<'a> From<&'a ast::Pattern> for ComparablePattern<'a> { patterns: patterns.iter().map(Into::into).collect(), }) } + #[allow(deprecated)] + ast::Pattern::Invalid(_) => Self::Invalid, } } } @@ -513,6 +516,7 @@ impl<'a> From<&'a ast::ExceptHandler> for ComparableExceptHandler<'a> { pub enum ComparableFStringElement<'a> { Literal(&'a str), FStringExpressionElement(FStringExpressionElement<'a>), + Invalid, } #[derive(Debug, PartialEq, Eq, Hash)] @@ -540,6 +544,8 @@ impl<'a> From<&'a ast::FStringElement> for ComparableFStringElement<'a> { .map(|spec| spec.elements.iter().map(Into::into).collect()), }) } + #[allow(deprecated)] + ast::FStringElement::Invalid(_) => Self::Invalid, } } } @@ -864,6 +870,7 @@ pub enum ComparableExpr<'a> { Tuple(ExprTuple<'a>), Slice(ExprSlice<'a>), IpyEscapeCommand(ExprIpyEscapeCommand<'a>), + Invalid, } impl<'a> From<&'a Box> for Box> { @@ -1072,6 +1079,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> { elts, ctx: _, range: _, + parenthesized: _, }) => Self::Tuple(ExprTuple { elts: elts.iter().map(Into::into).collect(), }), @@ -1090,6 +1098,8 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> { value, range: _, }) => Self::IpyEscapeCommand(ExprIpyEscapeCommand { kind: *kind, value }), + #[allow(deprecated)] + ast::Expr::Invalid(_) => Self::Invalid, } } } diff --git a/crates/ruff_python_ast/src/expression.rs b/crates/ruff_python_ast/src/expression.rs index 56fecca6fc300..3b72384b6aa6c 100644 --- a/crates/ruff_python_ast/src/expression.rs +++ b/crates/ruff_python_ast/src/expression.rs @@ -38,6 +38,7 @@ pub enum ExpressionRef<'a> { Tuple(&'a ast::ExprTuple), Slice(&'a ast::ExprSlice), IpyEscapeCommand(&'a ast::ExprIpyEscapeCommand), + Invalid(&'a ast::ExprInvalid), } impl<'a> From<&'a Box> for ExpressionRef<'a> { @@ -81,6 +82,8 @@ impl<'a> From<&'a Expr> for ExpressionRef<'a> { Expr::Tuple(value) => ExpressionRef::Tuple(value), Expr::Slice(value) => ExpressionRef::Slice(value), Expr::IpyEscapeCommand(value) => ExpressionRef::IpyEscapeCommand(value), + #[allow(deprecated)] + Expr::Invalid(value) => ExpressionRef::Invalid(value), } } } @@ -285,6 +288,7 @@ impl<'a> From> for AnyNodeRef<'a> { ExpressionRef::IpyEscapeCommand(expression) => { AnyNodeRef::ExprIpyEscapeCommand(expression) } + ExpressionRef::Invalid(expression) => AnyNodeRef::ExprInvalid(expression), } } } @@ -324,6 +328,7 @@ impl Ranged for ExpressionRef<'_> { ExpressionRef::Tuple(expression) => expression.range(), ExpressionRef::Slice(expression) => expression.range(), ExpressionRef::IpyEscapeCommand(expression) => expression.range(), + ExpressionRef::Invalid(expression) => expression.range(), } } } diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index 154be660d37ae..034df4075ba4b 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -259,6 +259,9 @@ pub fn any_over_expr(expr: &Expr, func: &dyn Fn(&Expr) -> bool) -> bool { | Expr::NoneLiteral(_) | Expr::EllipsisLiteral(_) | Expr::IpyEscapeCommand(_) => false, + + #[allow(deprecated)] + Expr::Invalid(_) => false, } } @@ -277,7 +280,8 @@ pub fn any_over_pattern(pattern: &Pattern, func: &dyn Fn(&Expr) -> bool) -> bool Pattern::MatchValue(ast::PatternMatchValue { value, range: _ }) => { any_over_expr(value, func) } - Pattern::MatchSingleton(_) => false, + #[allow(deprecated)] + Pattern::MatchSingleton(_) | Pattern::Invalid(_) => false, Pattern::MatchSequence(ast::PatternMatchSequence { patterns, range: _ }) => patterns .iter() .any(|pattern| any_over_pattern(pattern, func)), @@ -313,7 +317,8 @@ pub fn any_over_f_string_element( func: &dyn Fn(&Expr) -> bool, ) -> bool { match element { - ast::FStringElement::Literal(_) => false, + #[allow(deprecated)] + ast::FStringElement::Literal(_) | ast::FStringElement::Invalid(_) => false, ast::FStringElement::Expression(ast::FStringExpressionElement { expression, format_spec, @@ -1299,6 +1304,8 @@ fn is_non_empty_f_string(expr: &ast::ExprFString) -> bool { Expr::Name(_) => false, Expr::Slice(_) => false, Expr::IpyEscapeCommand(_) => false, + #[allow(deprecated)] + Expr::Invalid(_) => false, // These literals may or may not be empty. Expr::FString(f_string) => is_non_empty_f_string(f_string), @@ -1313,6 +1320,8 @@ fn is_non_empty_f_string(expr: &ast::ExprFString) -> bool { f_string.elements.iter().all(|element| match element { FStringElement::Literal(string_literal) => !string_literal.is_empty(), FStringElement::Expression(f_string) => inner(&f_string.expression), + #[allow(deprecated)] + FStringElement::Invalid(_) => false, }) } }) @@ -1336,6 +1345,8 @@ fn is_empty_f_string(expr: &ast::ExprFString) -> bool { expression, .. }) => inner(expression), + #[allow(deprecated)] + FStringElement::Invalid(_) => false, }) } _ => false, @@ -1348,6 +1359,8 @@ fn is_empty_f_string(expr: &ast::ExprFString) -> bool { f_string.elements.iter().all(|element| match element { FStringElement::Literal(string_literal) => string_literal.is_empty(), FStringElement::Expression(f_string) => inner(&f_string.expression), + #[allow(deprecated)] + FStringElement::Invalid(_) => false, }) } }) @@ -1423,6 +1436,7 @@ pub fn pep_604_union(elts: &[Expr]) -> Expr { elts: vec![], ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }), [Expr::Tuple(ast::ExprTuple { elts, .. })] => pep_604_union(elts), [elt] => elt.clone(), @@ -1457,6 +1471,7 @@ pub fn typing_union(elts: &[Expr], binding: String) -> Expr { elts: vec![], ctx: ExprContext::Load, range: TextRange::default(), + parenthesized: true, }), [Expr::Tuple(ast::ExprTuple { elts, .. })] => typing_union(elts, binding), [elt] => elt.clone(), @@ -1537,7 +1552,7 @@ mod tests { fn any_over_stmt_type_alias() { let seen = RefCell::new(Vec::new()); let name = Expr::Name(ExprName { - id: "x".to_string(), + id: "x".into(), range: TextRange::default(), ctx: ExprContext::Load, }); diff --git a/crates/ruff_python_ast/src/node.rs b/crates/ruff_python_ast/src/node.rs index 54afd2e040b3e..c2b7e8e320690 100644 --- a/crates/ruff_python_ast/src/node.rs +++ b/crates/ruff_python_ast/src/node.rs @@ -86,9 +86,11 @@ pub enum AnyNode { ExprTuple(ast::ExprTuple), ExprSlice(ast::ExprSlice), ExprIpyEscapeCommand(ast::ExprIpyEscapeCommand), + ExprInvalid(ast::ExprInvalid), ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler), FStringExpressionElement(ast::FStringExpressionElement), FStringLiteralElement(ast::FStringLiteralElement), + FStringInvalidElement(ast::FStringInvalidElement), FStringFormatSpec(ast::FStringFormatSpec), PatternMatchValue(ast::PatternMatchValue), PatternMatchSingleton(ast::PatternMatchSingleton), @@ -100,6 +102,7 @@ pub enum AnyNode { PatternMatchOr(ast::PatternMatchOr), PatternArguments(PatternArguments), PatternKeyword(PatternKeyword), + PatternMatchInvalid(ast::PatternMatchInvalid), Comprehension(Comprehension), Arguments(Arguments), Parameters(Parameters), @@ -170,6 +173,7 @@ impl AnyNode { | AnyNode::ExprCall(_) | AnyNode::FStringExpressionElement(_) | AnyNode::FStringLiteralElement(_) + | AnyNode::FStringInvalidElement(_) | AnyNode::FStringFormatSpec(_) | AnyNode::ExprFString(_) | AnyNode::ExprStringLiteral(_) @@ -186,6 +190,7 @@ impl AnyNode { | AnyNode::ExprTuple(_) | AnyNode::ExprSlice(_) | AnyNode::ExprIpyEscapeCommand(_) + | AnyNode::ExprInvalid(_) | AnyNode::ExceptHandlerExceptHandler(_) | AnyNode::PatternMatchValue(_) | AnyNode::PatternMatchSingleton(_) @@ -197,6 +202,7 @@ impl AnyNode { | AnyNode::PatternMatchOr(_) | AnyNode::PatternArguments(_) | AnyNode::PatternKeyword(_) + | AnyNode::PatternMatchInvalid(_) | AnyNode::Comprehension(_) | AnyNode::Arguments(_) | AnyNode::Parameters(_) @@ -252,6 +258,8 @@ impl AnyNode { AnyNode::ExprTuple(node) => Some(Expr::Tuple(node)), AnyNode::ExprSlice(node) => Some(Expr::Slice(node)), AnyNode::ExprIpyEscapeCommand(node) => Some(Expr::IpyEscapeCommand(node)), + #[allow(deprecated)] + AnyNode::ExprInvalid(range) => Some(Expr::Invalid(range)), AnyNode::ModModule(_) | AnyNode::ModExpression(_) @@ -283,6 +291,7 @@ impl AnyNode { | AnyNode::ExceptHandlerExceptHandler(_) | AnyNode::FStringExpressionElement(_) | AnyNode::FStringLiteralElement(_) + | AnyNode::FStringInvalidElement(_) | AnyNode::FStringFormatSpec(_) | AnyNode::PatternMatchValue(_) | AnyNode::PatternMatchSingleton(_) @@ -294,6 +303,7 @@ impl AnyNode { | AnyNode::PatternMatchOr(_) | AnyNode::PatternArguments(_) | AnyNode::PatternKeyword(_) + | AnyNode::PatternMatchInvalid(_) | AnyNode::Comprehension(_) | AnyNode::Arguments(_) | AnyNode::Parameters(_) @@ -364,6 +374,7 @@ impl AnyNode { | AnyNode::ExprCall(_) | AnyNode::FStringExpressionElement(_) | AnyNode::FStringLiteralElement(_) + | AnyNode::FStringInvalidElement(_) | AnyNode::FStringFormatSpec(_) | AnyNode::ExprFString(_) | AnyNode::ExprStringLiteral(_) @@ -380,6 +391,7 @@ impl AnyNode { | AnyNode::ExprTuple(_) | AnyNode::ExprSlice(_) | AnyNode::ExprIpyEscapeCommand(_) + | AnyNode::ExprInvalid(_) | AnyNode::ExceptHandlerExceptHandler(_) | AnyNode::PatternMatchValue(_) | AnyNode::PatternMatchSingleton(_) @@ -391,6 +403,7 @@ impl AnyNode { | AnyNode::PatternMatchOr(_) | AnyNode::PatternArguments(_) | AnyNode::PatternKeyword(_) + | AnyNode::PatternMatchInvalid(_) | AnyNode::Comprehension(_) | AnyNode::Arguments(_) | AnyNode::Parameters(_) @@ -422,6 +435,8 @@ impl AnyNode { AnyNode::PatternMatchStar(node) => Some(Pattern::MatchStar(node)), AnyNode::PatternMatchAs(node) => Some(Pattern::MatchAs(node)), AnyNode::PatternMatchOr(node) => Some(Pattern::MatchOr(node)), + #[allow(deprecated)] + AnyNode::PatternMatchInvalid(node) => Some(Pattern::Invalid(node)), AnyNode::ModModule(_) | AnyNode::ModExpression(_) @@ -469,6 +484,7 @@ impl AnyNode { | AnyNode::ExprCall(_) | AnyNode::FStringExpressionElement(_) | AnyNode::FStringLiteralElement(_) + | AnyNode::FStringInvalidElement(_) | AnyNode::FStringFormatSpec(_) | AnyNode::ExprFString(_) | AnyNode::ExprStringLiteral(_) @@ -485,6 +501,7 @@ impl AnyNode { | AnyNode::ExprTuple(_) | AnyNode::ExprSlice(_) | AnyNode::ExprIpyEscapeCommand(_) + | AnyNode::ExprInvalid(_) | AnyNode::ExceptHandlerExceptHandler(_) | AnyNode::PatternArguments(_) | AnyNode::PatternKeyword(_) @@ -559,6 +576,7 @@ impl AnyNode { | AnyNode::ExprCall(_) | AnyNode::FStringExpressionElement(_) | AnyNode::FStringLiteralElement(_) + | AnyNode::FStringInvalidElement(_) | AnyNode::FStringFormatSpec(_) | AnyNode::ExprFString(_) | AnyNode::ExprStringLiteral(_) @@ -575,6 +593,7 @@ impl AnyNode { | AnyNode::ExprTuple(_) | AnyNode::ExprSlice(_) | AnyNode::ExprIpyEscapeCommand(_) + | AnyNode::ExprInvalid(_) | AnyNode::PatternMatchValue(_) | AnyNode::PatternMatchSingleton(_) | AnyNode::PatternMatchSequence(_) @@ -585,6 +604,7 @@ impl AnyNode { | AnyNode::PatternMatchOr(_) | AnyNode::PatternArguments(_) | AnyNode::PatternKeyword(_) + | AnyNode::PatternMatchInvalid(_) | AnyNode::Comprehension(_) | AnyNode::Arguments(_) | AnyNode::Parameters(_) @@ -674,6 +694,7 @@ impl AnyNode { Self::ExprCall(node) => AnyNodeRef::ExprCall(node), Self::FStringExpressionElement(node) => AnyNodeRef::FStringExpressionElement(node), Self::FStringLiteralElement(node) => AnyNodeRef::FStringLiteralElement(node), + Self::FStringInvalidElement(node) => AnyNodeRef::FStringInvalidElement(node), Self::FStringFormatSpec(node) => AnyNodeRef::FStringFormatSpec(node), Self::ExprFString(node) => AnyNodeRef::ExprFString(node), Self::ExprStringLiteral(node) => AnyNodeRef::ExprStringLiteral(node), @@ -690,6 +711,7 @@ impl AnyNode { Self::ExprTuple(node) => AnyNodeRef::ExprTuple(node), Self::ExprSlice(node) => AnyNodeRef::ExprSlice(node), Self::ExprIpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), + Self::ExprInvalid(node) => AnyNodeRef::ExprInvalid(node), Self::ExceptHandlerExceptHandler(node) => AnyNodeRef::ExceptHandlerExceptHandler(node), Self::PatternMatchValue(node) => AnyNodeRef::PatternMatchValue(node), Self::PatternMatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node), @@ -701,6 +723,7 @@ impl AnyNode { Self::PatternMatchOr(node) => AnyNodeRef::PatternMatchOr(node), Self::PatternArguments(node) => AnyNodeRef::PatternArguments(node), Self::PatternKeyword(node) => AnyNodeRef::PatternKeyword(node), + Self::PatternMatchInvalid(node) => AnyNodeRef::PatternMatchInvalid(node), Self::Comprehension(node) => AnyNodeRef::Comprehension(node), Self::Arguments(node) => AnyNodeRef::Arguments(node), Self::Parameters(node) => AnyNodeRef::Parameters(node), @@ -3256,6 +3279,7 @@ impl AstNode for ast::ExprTuple { elts, ctx: _, range: _, + parenthesized: _, } = self; for expr in elts { @@ -3352,6 +3376,40 @@ impl AstNode for ast::ExprIpyEscapeCommand { } = self; } } +impl AstNode for ast::ExprInvalid { + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprInvalid(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprInvalid(node) = kind { + Some(node) + } else { + None + } + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } + + fn visit_preorder<'a, V>(&'a self, _visitor: &mut V) + where + V: PreorderVisitor<'a> + ?Sized, + { + } +} impl AstNode for ast::ExceptHandlerExceptHandler { fn cast(kind: AnyNode) -> Option where @@ -3795,6 +3853,40 @@ impl AstNode for PatternKeyword { visitor.visit_pattern(pattern); } } +impl AstNode for ast::PatternMatchInvalid { + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchInvalid(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchInvalid(node) = kind { + Some(node) + } else { + None + } + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } + + fn visit_preorder<'a, V>(&'a self, _visitor: &mut V) + where + V: PreorderVisitor<'a> + ?Sized, + { + } +} impl AstNode for Comprehension { fn cast(kind: AnyNode) -> Option @@ -4571,6 +4663,8 @@ impl From for AnyNode { Expr::Tuple(node) => AnyNode::ExprTuple(node), Expr::Slice(node) => AnyNode::ExprSlice(node), Expr::IpyEscapeCommand(node) => AnyNode::ExprIpyEscapeCommand(node), + #[allow(deprecated)] + Expr::Invalid(range) => AnyNode::ExprInvalid(range), } } } @@ -4589,6 +4683,8 @@ impl From for AnyNode { match element { FStringElement::Literal(node) => AnyNode::FStringLiteralElement(node), FStringElement::Expression(node) => AnyNode::FStringExpressionElement(node), + #[allow(deprecated)] + FStringElement::Invalid(node) => AnyNode::FStringInvalidElement(node), } } } @@ -4604,6 +4700,8 @@ impl From for AnyNode { Pattern::MatchStar(node) => AnyNode::PatternMatchStar(node), Pattern::MatchAs(node) => AnyNode::PatternMatchAs(node), Pattern::MatchOr(node) => AnyNode::PatternMatchOr(node), + #[allow(deprecated)] + Pattern::Invalid(node) => AnyNode::PatternMatchInvalid(node), } } } @@ -4994,6 +5092,12 @@ impl From for AnyNode { } } +impl From for AnyNode { + fn from(node: ast::ExprInvalid) -> Self { + AnyNode::ExprInvalid(node) + } +} + impl From for AnyNode { fn from(node: ast::ExceptHandlerExceptHandler) -> Self { AnyNode::ExceptHandlerExceptHandler(node) @@ -5048,6 +5152,12 @@ impl From for AnyNode { } } +impl From for AnyNode { + fn from(node: ast::PatternMatchInvalid) -> Self { + AnyNode::PatternMatchInvalid(node) + } +} + impl From for AnyNode { fn from(node: PatternArguments) -> Self { AnyNode::PatternArguments(node) @@ -5200,6 +5310,7 @@ impl Ranged for AnyNode { AnyNode::ExprCall(node) => node.range(), AnyNode::FStringExpressionElement(node) => node.range(), AnyNode::FStringLiteralElement(node) => node.range(), + AnyNode::FStringInvalidElement(node) => node.range(), AnyNode::FStringFormatSpec(node) => node.range(), AnyNode::ExprFString(node) => node.range(), AnyNode::ExprStringLiteral(node) => node.range(), @@ -5216,6 +5327,7 @@ impl Ranged for AnyNode { AnyNode::ExprTuple(node) => node.range(), AnyNode::ExprSlice(node) => node.range(), AnyNode::ExprIpyEscapeCommand(node) => node.range(), + AnyNode::ExprInvalid(node) => node.range(), AnyNode::ExceptHandlerExceptHandler(node) => node.range(), AnyNode::PatternMatchValue(node) => node.range(), AnyNode::PatternMatchSingleton(node) => node.range(), @@ -5227,6 +5339,7 @@ impl Ranged for AnyNode { AnyNode::PatternMatchOr(node) => node.range(), AnyNode::PatternArguments(node) => node.range(), AnyNode::PatternKeyword(node) => node.range(), + AnyNode::PatternMatchInvalid(node) => node.range(), AnyNode::Comprehension(node) => node.range(), AnyNode::Arguments(node) => node.range(), AnyNode::Parameters(node) => node.range(), @@ -5297,6 +5410,7 @@ pub enum AnyNodeRef<'a> { ExprCall(&'a ast::ExprCall), FStringExpressionElement(&'a ast::FStringExpressionElement), FStringLiteralElement(&'a ast::FStringLiteralElement), + FStringInvalidElement(&'a ast::FStringInvalidElement), FStringFormatSpec(&'a ast::FStringFormatSpec), ExprFString(&'a ast::ExprFString), ExprStringLiteral(&'a ast::ExprStringLiteral), @@ -5313,6 +5427,7 @@ pub enum AnyNodeRef<'a> { ExprTuple(&'a ast::ExprTuple), ExprSlice(&'a ast::ExprSlice), ExprIpyEscapeCommand(&'a ast::ExprIpyEscapeCommand), + ExprInvalid(&'a ast::ExprInvalid), ExceptHandlerExceptHandler(&'a ast::ExceptHandlerExceptHandler), PatternMatchValue(&'a ast::PatternMatchValue), PatternMatchSingleton(&'a ast::PatternMatchSingleton), @@ -5324,6 +5439,7 @@ pub enum AnyNodeRef<'a> { PatternMatchOr(&'a ast::PatternMatchOr), PatternArguments(&'a ast::PatternArguments), PatternKeyword(&'a ast::PatternKeyword), + PatternMatchInvalid(&'a ast::PatternMatchInvalid), Comprehension(&'a Comprehension), Arguments(&'a Arguments), Parameters(&'a Parameters), @@ -5393,6 +5509,7 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::ExprCall(node) => NonNull::from(*node).cast(), AnyNodeRef::FStringExpressionElement(node) => NonNull::from(*node).cast(), AnyNodeRef::FStringLiteralElement(node) => NonNull::from(*node).cast(), + AnyNodeRef::FStringInvalidElement(node) => NonNull::from(*node).cast(), AnyNodeRef::FStringFormatSpec(node) => NonNull::from(*node).cast(), AnyNodeRef::ExprFString(node) => NonNull::from(*node).cast(), AnyNodeRef::ExprStringLiteral(node) => NonNull::from(*node).cast(), @@ -5409,6 +5526,7 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::ExprTuple(node) => NonNull::from(*node).cast(), AnyNodeRef::ExprSlice(node) => NonNull::from(*node).cast(), AnyNodeRef::ExprIpyEscapeCommand(node) => NonNull::from(*node).cast(), + AnyNodeRef::ExprInvalid(node) => NonNull::from(*node).cast(), AnyNodeRef::ExceptHandlerExceptHandler(node) => NonNull::from(*node).cast(), AnyNodeRef::PatternMatchValue(node) => NonNull::from(*node).cast(), AnyNodeRef::PatternMatchSingleton(node) => NonNull::from(*node).cast(), @@ -5420,6 +5538,7 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::PatternMatchOr(node) => NonNull::from(*node).cast(), AnyNodeRef::PatternArguments(node) => NonNull::from(*node).cast(), AnyNodeRef::PatternKeyword(node) => NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchInvalid(node) => NonNull::from(*node).cast(), AnyNodeRef::Comprehension(node) => NonNull::from(*node).cast(), AnyNodeRef::Arguments(node) => NonNull::from(*node).cast(), AnyNodeRef::Parameters(node) => NonNull::from(*node).cast(), @@ -5495,6 +5614,7 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::ExprCall(_) => NodeKind::ExprCall, AnyNodeRef::FStringExpressionElement(_) => NodeKind::FStringExpressionElement, AnyNodeRef::FStringLiteralElement(_) => NodeKind::FStringLiteralElement, + AnyNodeRef::FStringInvalidElement(_) => NodeKind::FStringInvalidElement, AnyNodeRef::FStringFormatSpec(_) => NodeKind::FStringFormatSpec, AnyNodeRef::ExprFString(_) => NodeKind::ExprFString, AnyNodeRef::ExprStringLiteral(_) => NodeKind::ExprStringLiteral, @@ -5511,6 +5631,7 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::ExprTuple(_) => NodeKind::ExprTuple, AnyNodeRef::ExprSlice(_) => NodeKind::ExprSlice, AnyNodeRef::ExprIpyEscapeCommand(_) => NodeKind::ExprIpyEscapeCommand, + AnyNodeRef::ExprInvalid(_) => NodeKind::ExprInvalid, AnyNodeRef::ExceptHandlerExceptHandler(_) => NodeKind::ExceptHandlerExceptHandler, AnyNodeRef::PatternMatchValue(_) => NodeKind::PatternMatchValue, AnyNodeRef::PatternMatchSingleton(_) => NodeKind::PatternMatchSingleton, @@ -5522,6 +5643,7 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::PatternMatchOr(_) => NodeKind::PatternMatchOr, AnyNodeRef::PatternArguments(_) => NodeKind::PatternArguments, AnyNodeRef::PatternKeyword(_) => NodeKind::PatternKeyword, + AnyNodeRef::PatternMatchInvalid(_) => NodeKind::PatternInvalid, AnyNodeRef::Comprehension(_) => NodeKind::Comprehension, AnyNodeRef::Arguments(_) => NodeKind::Arguments, AnyNodeRef::Parameters(_) => NodeKind::Parameters, @@ -5592,6 +5714,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprCall(_) | AnyNodeRef::FStringExpressionElement(_) | AnyNodeRef::FStringLiteralElement(_) + | AnyNodeRef::FStringInvalidElement(_) | AnyNodeRef::FStringFormatSpec(_) | AnyNodeRef::ExprFString(_) | AnyNodeRef::ExprStringLiteral(_) @@ -5608,6 +5731,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprTuple(_) | AnyNodeRef::ExprSlice(_) | AnyNodeRef::ExprIpyEscapeCommand(_) + | AnyNodeRef::ExprInvalid(_) | AnyNodeRef::ExceptHandlerExceptHandler(_) | AnyNodeRef::PatternMatchValue(_) | AnyNodeRef::PatternMatchSingleton(_) @@ -5619,6 +5743,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::PatternMatchOr(_) | AnyNodeRef::PatternArguments(_) | AnyNodeRef::PatternKeyword(_) + | AnyNodeRef::PatternMatchInvalid(_) | AnyNodeRef::Comprehension(_) | AnyNodeRef::Arguments(_) | AnyNodeRef::Parameters(_) @@ -5673,7 +5798,8 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprList(_) | AnyNodeRef::ExprTuple(_) | AnyNodeRef::ExprSlice(_) - | AnyNodeRef::ExprIpyEscapeCommand(_) => true, + | AnyNodeRef::ExprIpyEscapeCommand(_) + | AnyNodeRef::ExprInvalid(_) => true, AnyNodeRef::ModModule(_) | AnyNodeRef::ModExpression(_) @@ -5705,6 +5831,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExceptHandlerExceptHandler(_) | AnyNodeRef::FStringExpressionElement(_) | AnyNodeRef::FStringLiteralElement(_) + | AnyNodeRef::FStringInvalidElement(_) | AnyNodeRef::FStringFormatSpec(_) | AnyNodeRef::PatternMatchValue(_) | AnyNodeRef::PatternMatchSingleton(_) @@ -5716,6 +5843,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::PatternMatchOr(_) | AnyNodeRef::PatternArguments(_) | AnyNodeRef::PatternKeyword(_) + | AnyNodeRef::PatternMatchInvalid(_) | AnyNodeRef::Comprehension(_) | AnyNodeRef::Arguments(_) | AnyNodeRef::Parameters(_) @@ -5785,6 +5913,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprCall(_) | AnyNodeRef::FStringExpressionElement(_) | AnyNodeRef::FStringLiteralElement(_) + | AnyNodeRef::FStringInvalidElement(_) | AnyNodeRef::FStringFormatSpec(_) | AnyNodeRef::ExprFString(_) | AnyNodeRef::ExprStringLiteral(_) @@ -5801,6 +5930,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprTuple(_) | AnyNodeRef::ExprSlice(_) | AnyNodeRef::ExprIpyEscapeCommand(_) + | AnyNodeRef::ExprInvalid(_) | AnyNodeRef::ExceptHandlerExceptHandler(_) | AnyNodeRef::PatternMatchValue(_) | AnyNodeRef::PatternMatchSingleton(_) @@ -5812,6 +5942,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::PatternMatchOr(_) | AnyNodeRef::PatternArguments(_) | AnyNodeRef::PatternKeyword(_) + | AnyNodeRef::PatternMatchInvalid(_) | AnyNodeRef::Comprehension(_) | AnyNodeRef::Arguments(_) | AnyNodeRef::Parameters(_) @@ -5842,7 +5973,8 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::PatternMatchClass(_) | AnyNodeRef::PatternMatchStar(_) | AnyNodeRef::PatternMatchAs(_) - | AnyNodeRef::PatternMatchOr(_) => true, + | AnyNodeRef::PatternMatchOr(_) + | AnyNodeRef::PatternMatchInvalid(_) => true, AnyNodeRef::ModModule(_) | AnyNodeRef::ModExpression(_) @@ -5890,6 +6022,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprCall(_) | AnyNodeRef::FStringExpressionElement(_) | AnyNodeRef::FStringLiteralElement(_) + | AnyNodeRef::FStringInvalidElement(_) | AnyNodeRef::FStringFormatSpec(_) | AnyNodeRef::ExprFString(_) | AnyNodeRef::ExprStringLiteral(_) @@ -5906,6 +6039,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprTuple(_) | AnyNodeRef::ExprSlice(_) | AnyNodeRef::ExprIpyEscapeCommand(_) + | AnyNodeRef::ExprInvalid(_) | AnyNodeRef::PatternArguments(_) | AnyNodeRef::PatternKeyword(_) | AnyNodeRef::ExceptHandlerExceptHandler(_) @@ -5980,6 +6114,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprCall(_) | AnyNodeRef::FStringExpressionElement(_) | AnyNodeRef::FStringLiteralElement(_) + | AnyNodeRef::FStringInvalidElement(_) | AnyNodeRef::FStringFormatSpec(_) | AnyNodeRef::ExprFString(_) | AnyNodeRef::ExprStringLiteral(_) @@ -5996,6 +6131,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::ExprTuple(_) | AnyNodeRef::ExprSlice(_) | AnyNodeRef::ExprIpyEscapeCommand(_) + | AnyNodeRef::ExprInvalid(_) | AnyNodeRef::PatternMatchValue(_) | AnyNodeRef::PatternMatchSingleton(_) | AnyNodeRef::PatternMatchSequence(_) @@ -6006,6 +6142,7 @@ impl<'a> AnyNodeRef<'a> { | AnyNodeRef::PatternMatchOr(_) | AnyNodeRef::PatternArguments(_) | AnyNodeRef::PatternKeyword(_) + | AnyNodeRef::PatternMatchInvalid(_) | AnyNodeRef::Comprehension(_) | AnyNodeRef::Arguments(_) | AnyNodeRef::Parameters(_) @@ -6116,6 +6253,7 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::PatternMatchOr(node) => node.visit_preorder(visitor), AnyNodeRef::PatternArguments(node) => node.visit_preorder(visitor), AnyNodeRef::PatternKeyword(node) => node.visit_preorder(visitor), + AnyNodeRef::PatternMatchInvalid(_) => {} AnyNodeRef::Comprehension(node) => node.visit_preorder(visitor), AnyNodeRef::Arguments(node) => node.visit_preorder(visitor), AnyNodeRef::Parameters(node) => node.visit_preorder(visitor), @@ -6134,6 +6272,7 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::StringLiteral(node) => node.visit_preorder(visitor), AnyNodeRef::BytesLiteral(node) => node.visit_preorder(visitor), AnyNodeRef::ElifElseClause(node) => node.visit_preorder(visitor), + AnyNodeRef::ExprInvalid(_) | AnyNodeRef::FStringInvalidElement(_) => {} } } @@ -6576,6 +6715,12 @@ impl<'a> From<&'a ast::ExprIpyEscapeCommand> for AnyNodeRef<'a> { } } +impl<'a> From<&'a ast::ExprInvalid> for AnyNodeRef<'a> { + fn from(node: &'a ast::ExprInvalid) -> Self { + AnyNodeRef::ExprInvalid(node) + } +} + impl<'a> From<&'a ast::ExceptHandlerExceptHandler> for AnyNodeRef<'a> { fn from(node: &'a ast::ExceptHandlerExceptHandler) -> Self { AnyNodeRef::ExceptHandlerExceptHandler(node) @@ -6630,6 +6775,12 @@ impl<'a> From<&'a ast::PatternMatchOr> for AnyNodeRef<'a> { } } +impl<'a> From<&'a ast::PatternMatchInvalid> for AnyNodeRef<'a> { + fn from(node: &'a ast::PatternMatchInvalid) -> Self { + AnyNodeRef::PatternMatchInvalid(node) + } +} + impl<'a> From<&'a ast::PatternArguments> for AnyNodeRef<'a> { fn from(node: &'a ast::PatternArguments) -> Self { AnyNodeRef::PatternArguments(node) @@ -6756,6 +6907,8 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> { Expr::Tuple(node) => AnyNodeRef::ExprTuple(node), Expr::Slice(node) => AnyNodeRef::ExprSlice(node), Expr::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), + #[allow(deprecated)] + Expr::Invalid(node) => AnyNodeRef::ExprInvalid(node), } } } @@ -6774,6 +6927,8 @@ impl<'a> From<&'a FStringElement> for AnyNodeRef<'a> { match element { FStringElement::Expression(node) => AnyNodeRef::FStringExpressionElement(node), FStringElement::Literal(node) => AnyNodeRef::FStringLiteralElement(node), + #[allow(deprecated)] + FStringElement::Invalid(node) => AnyNodeRef::FStringInvalidElement(node), } } } @@ -6789,6 +6944,8 @@ impl<'a> From<&'a Pattern> for AnyNodeRef<'a> { Pattern::MatchStar(node) => AnyNodeRef::PatternMatchStar(node), Pattern::MatchAs(node) => AnyNodeRef::PatternMatchAs(node), Pattern::MatchOr(node) => AnyNodeRef::PatternMatchOr(node), + #[allow(deprecated)] + Pattern::Invalid(node) => AnyNodeRef::PatternMatchInvalid(node), } } } @@ -6908,6 +7065,7 @@ impl Ranged for AnyNodeRef<'_> { AnyNodeRef::ExprCall(node) => node.range(), AnyNodeRef::FStringExpressionElement(node) => node.range(), AnyNodeRef::FStringLiteralElement(node) => node.range(), + AnyNodeRef::FStringInvalidElement(node) => node.range(), AnyNodeRef::FStringFormatSpec(node) => node.range(), AnyNodeRef::ExprFString(node) => node.range(), AnyNodeRef::ExprStringLiteral(node) => node.range(), @@ -6924,6 +7082,7 @@ impl Ranged for AnyNodeRef<'_> { AnyNodeRef::ExprTuple(node) => node.range(), AnyNodeRef::ExprSlice(node) => node.range(), AnyNodeRef::ExprIpyEscapeCommand(node) => node.range(), + AnyNodeRef::ExprInvalid(node) => node.range(), AnyNodeRef::ExceptHandlerExceptHandler(node) => node.range(), AnyNodeRef::PatternMatchValue(node) => node.range(), AnyNodeRef::PatternMatchSingleton(node) => node.range(), @@ -6935,6 +7094,7 @@ impl Ranged for AnyNodeRef<'_> { AnyNodeRef::PatternMatchOr(node) => node.range(), AnyNodeRef::PatternArguments(node) => node.range(), AnyNodeRef::PatternKeyword(node) => node.range(), + AnyNodeRef::PatternMatchInvalid(node) => node.range(), AnyNodeRef::Comprehension(node) => node.range(), AnyNodeRef::Arguments(node) => node.range(), AnyNodeRef::Parameters(node) => node.range(), @@ -7007,6 +7167,7 @@ pub enum NodeKind { ExprCall, FStringExpressionElement, FStringLiteralElement, + FStringInvalidElement, FStringFormatSpec, ExprFString, ExprStringLiteral, @@ -7023,6 +7184,7 @@ pub enum NodeKind { ExprTuple, ExprSlice, ExprIpyEscapeCommand, + ExprInvalid, ExceptHandlerExceptHandler, PatternMatchValue, PatternMatchSingleton, @@ -7034,6 +7196,7 @@ pub enum NodeKind { PatternMatchOr, PatternArguments, PatternKeyword, + PatternInvalid, TypeIgnoreTypeIgnore, Comprehension, Arguments, diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index b6581eef40524..c423d668b9267 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -1,14 +1,13 @@ #![allow(clippy::derive_partial_eq_without_eq)] +use itertools::Itertools; use std::cell::OnceCell; + use std::fmt; use std::fmt::Debug; use std::ops::Deref; use std::slice::{Iter, IterMut}; -use itertools::Itertools; - -use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer}; use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::{int, LiteralExpressionRef}; @@ -555,6 +554,7 @@ impl From for Stmt { } /// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) +#[allow(deprecated)] #[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum Expr { #[is(name = "bool_op_expr")] @@ -623,6 +623,11 @@ pub enum Expr { // Jupyter notebook specific #[is(name = "ipy_escape_command_expr")] IpyEscapeCommand(ExprIpyEscapeCommand), + + #[is(name = "invalid_expr")] + #[deprecated] + #[allow(deprecated)] + Invalid(ExprInvalid), } impl Expr { @@ -656,6 +661,25 @@ impl Expr { } } +#[derive(Clone, Debug, PartialEq)] +pub struct ExprInvalid { + pub value: String, + pub range: TextRange, +} + +impl From for Expr { + fn from(payload: ExprInvalid) -> Self { + #[allow(deprecated)] + Expr::Invalid(payload) + } +} + +impl Ranged for ExprInvalid { + fn range(&self) -> TextRange { + self.range + } +} + /// An AST node used to represent a IPython escape command at the expression level. /// /// For example, @@ -966,6 +990,18 @@ impl Deref for FStringLiteralElement { } } +#[derive(Clone, Debug, PartialEq)] +pub struct FStringInvalidElement { + pub value: String, + pub range: TextRange, +} + +impl Ranged for FStringInvalidElement { + fn range(&self) -> TextRange { + self.range + } +} + /// Transforms a value prior to formatting it. #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, is_macro::Is)] #[repr(i8)] @@ -1191,9 +1227,14 @@ impl From for Expr { } #[derive(Clone, Debug, PartialEq, is_macro::Is)] +#[allow(deprecated)] pub enum FStringElement { Literal(FStringLiteralElement), Expression(FStringExpressionElement), + + #[allow(deprecated)] + #[deprecated] + Invalid(FStringInvalidElement), } impl Ranged for FStringElement { @@ -1201,6 +1242,8 @@ impl Ranged for FStringElement { match self { FStringElement::Literal(node) => node.range(), FStringElement::Expression(node) => node.range(), + #[allow(deprecated)] + FStringElement::Invalid(node) => node.range(), } } } @@ -1796,6 +1839,7 @@ pub struct ExprTuple { pub range: TextRange, pub elts: Vec, pub ctx: ExprContext, + pub parenthesized: bool, } impl From for Expr { @@ -1804,37 +1848,6 @@ impl From for Expr { } } -impl ExprTuple { - /// Return `true` if a tuple is parenthesized in the source code. - pub fn is_parenthesized(&self, source: &str) -> bool { - let Some(elt) = self.elts.first() else { - return true; - }; - - // Count the number of open parentheses between the start of the tuple and the first element. - let open_parentheses_count = - SimpleTokenizer::new(source, TextRange::new(self.start(), elt.start())) - .skip_trivia() - .filter(|token| token.kind() == SimpleTokenKind::LParen) - .count(); - if open_parentheses_count == 0 { - return false; - } - - // Count the number of parentheses between the end of the first element and its trailing comma. - let close_parentheses_count = - SimpleTokenizer::new(source, TextRange::new(elt.end(), self.end())) - .skip_trivia() - .take_while(|token| token.kind() != SimpleTokenKind::Comma) - .filter(|token| token.kind() == SimpleTokenKind::RParen) - .count(); - - // If the number of open parentheses is greater than the number of close parentheses, the tuple - // is parenthesized. - open_parentheses_count > close_parentheses_count - } -} - /// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) #[derive(Clone, Debug, PartialEq)] pub struct ExprSlice { @@ -2690,6 +2703,7 @@ pub struct MatchCase { /// See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern) #[derive(Clone, Debug, PartialEq, is_macro::Is)] +#[allow(deprecated)] pub enum Pattern { MatchValue(PatternMatchValue), MatchSingleton(PatternMatchSingleton), @@ -2699,6 +2713,8 @@ pub enum Pattern { MatchStar(PatternMatchStar), MatchAs(PatternMatchAs), MatchOr(PatternMatchOr), + #[deprecated] + Invalid(PatternMatchInvalid), } /// See also [MatchValue](https://docs.python.org/3/library/ast.html#ast.MatchValue) @@ -2831,6 +2847,19 @@ impl From for Pattern { } } +#[derive(Clone, Debug, PartialEq)] +pub struct PatternMatchInvalid { + pub value: String, + pub range: TextRange, +} + +#[allow(deprecated)] +impl From for Pattern { + fn from(payload: PatternMatchInvalid) -> Self { + Pattern::Invalid(payload) + } +} + /// See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param) #[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum TypeParam { @@ -3281,10 +3310,17 @@ impl IpyEscapeKind { } } +/// An `Identifier` with an empty `id` is invalid. +/// +/// For example, in the following code `id` will be empty. +/// ```python +/// def 1(): +/// ... +/// ``` #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Identifier { - id: String, - range: TextRange, + pub id: String, + pub range: TextRange, } impl Identifier { @@ -3295,6 +3331,10 @@ impl Identifier { range, } } + + pub fn is_valid(&self) -> bool { + !self.id.is_empty() + } } impl Identifier { @@ -3722,6 +3762,8 @@ impl Ranged for crate::Expr { Self::Tuple(node) => node.range(), Self::Slice(node) => node.range(), Self::IpyEscapeCommand(node) => node.range(), + #[allow(deprecated)] + Self::Invalid(node) => node.range(), } } } @@ -3807,6 +3849,11 @@ impl Ranged for crate::nodes::PatternMatchOr { self.range } } +impl Ranged for crate::nodes::PatternMatchInvalid { + fn range(&self) -> TextRange { + self.range + } +} impl Ranged for crate::Pattern { fn range(&self) -> TextRange { match self { @@ -3818,6 +3865,8 @@ impl Ranged for crate::Pattern { Self::MatchStar(node) => node.range(), Self::MatchAs(node) => node.range(), Self::MatchOr(node) => node.range(), + #[allow(deprecated)] + Self::Invalid(node) => node.range(), } } } diff --git a/crates/ruff_python_ast/src/relocate.rs b/crates/ruff_python_ast/src/relocate.rs index 5c189fb4c6a73..e493cc74b1a79 100644 --- a/crates/ruff_python_ast/src/relocate.rs +++ b/crates/ruff_python_ast/src/relocate.rs @@ -113,6 +113,10 @@ impl Transformer for Relocator { Expr::IpyEscapeCommand(nodes::ExprIpyEscapeCommand { range, .. }) => { *range = self.range; } + #[allow(deprecated)] + Expr::Invalid(nodes::ExprInvalid { range, .. }) => { + *range = self.range; + } } walk_expr(self, expr); } diff --git a/crates/ruff_python_ast/src/visitor.rs b/crates/ruff_python_ast/src/visitor.rs index cd93b4927fd66..ea280ed37232f 100644 --- a/crates/ruff_python_ast/src/visitor.rs +++ b/crates/ruff_python_ast/src/visitor.rs @@ -539,6 +539,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) { elts, ctx, range: _, + parenthesized: _, }) => { for expr in elts { visitor.visit_expr(expr); @@ -562,6 +563,8 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) { } } Expr::IpyEscapeCommand(_) => {} + #[allow(deprecated)] + Expr::Invalid(_) => {} } } @@ -717,6 +720,8 @@ pub fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a P visitor.visit_pattern(pattern); } } + #[allow(deprecated)] + Pattern::Invalid(_) => {} } } diff --git a/crates/ruff_python_ast/src/visitor/preorder.rs b/crates/ruff_python_ast/src/visitor/preorder.rs index 394d8cbf171d7..c3835c0d888db 100644 --- a/crates/ruff_python_ast/src/visitor/preorder.rs +++ b/crates/ruff_python_ast/src/visitor/preorder.rs @@ -304,6 +304,8 @@ where Expr::Tuple(expr) => expr.visit_preorder(visitor), Expr::Slice(expr) => expr.visit_preorder(visitor), Expr::IpyEscapeCommand(expr) => expr.visit_preorder(visitor), + #[allow(deprecated)] + Expr::Invalid(_) => {} } } @@ -485,6 +487,8 @@ where Pattern::MatchStar(pattern) => pattern.visit_preorder(visitor), Pattern::MatchAs(pattern) => pattern.visit_preorder(visitor), Pattern::MatchOr(pattern) => pattern.visit_preorder(visitor), + #[allow(deprecated)] + Pattern::Invalid(_) => {} } } visitor.leave_node(node); @@ -526,6 +530,8 @@ pub fn walk_f_string_element<'a, V: PreorderVisitor<'a> + ?Sized>( match f_string_element { FStringElement::Expression(element) => element.visit_preorder(visitor), FStringElement::Literal(element) => element.visit_preorder(visitor), + #[allow(deprecated)] + FStringElement::Invalid(_) => {} } } visitor.leave_node(node); diff --git a/crates/ruff_python_ast/src/visitor/transformer.rs b/crates/ruff_python_ast/src/visitor/transformer.rs index a8e1f8950880f..8852f0793e063 100644 --- a/crates/ruff_python_ast/src/visitor/transformer.rs +++ b/crates/ruff_python_ast/src/visitor/transformer.rs @@ -528,6 +528,7 @@ pub fn walk_expr(visitor: &V, expr: &mut Expr) { elts, ctx, range: _, + parenthesized: _, }) => { for expr in elts { visitor.visit_expr(expr); @@ -550,7 +551,8 @@ pub fn walk_expr(visitor: &V, expr: &mut Expr) { visitor.visit_expr(expr); } } - Expr::IpyEscapeCommand(_) => {} + #[allow(deprecated)] + Expr::IpyEscapeCommand(_) | Expr::Invalid(_) => {} } } @@ -703,6 +705,8 @@ pub fn walk_pattern(visitor: &V, pattern: &mut Pattern) visitor.visit_pattern(pattern); } } + #[allow(deprecated)] + Pattern::Invalid(_) => {} } } diff --git a/crates/ruff_python_codegen/src/generator.rs b/crates/ruff_python_codegen/src/generator.rs index 934a9f39847bb..ab1f0a3f64108 100644 --- a/crates/ruff_python_codegen/src/generator.rs +++ b/crates/ruff_python_codegen/src/generator.rs @@ -729,6 +729,8 @@ impl<'a> Generator<'a> { self.unparse_pattern(pattern); } } + #[allow(deprecated)] + Pattern::Invalid(ast::PatternMatchInvalid { value, .. }) => self.p(value.as_str()), } } @@ -1190,6 +1192,8 @@ impl<'a> Generator<'a> { Expr::IpyEscapeCommand(ast::ExprIpyEscapeCommand { kind, value, .. }) => { self.p(&format!("{kind}{value}")); } + #[allow(deprecated)] + Expr::Invalid(ast::ExprInvalid { value, .. }) => self.p(value.as_str()), } } @@ -1358,6 +1362,8 @@ impl<'a> Generator<'a> { *conversion, format_spec.as_deref(), ), + #[allow(deprecated)] + ast::FStringElement::Invalid(ast::FStringInvalidElement { value, .. }) => self.p(value), } } diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 2d958ebee98f3..437baaddc5940 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -293,9 +293,10 @@ fn handle_enclosed_comment<'a>( | AnyNodeRef::ExprSet(_) | AnyNodeRef::ExprListComp(_) | AnyNodeRef::ExprSetComp(_) => handle_bracketed_end_of_line_comment(comment, locator), - AnyNodeRef::ExprTuple(tuple) if tuple.is_parenthesized(locator.contents()) => { - handle_bracketed_end_of_line_comment(comment, locator) - } + AnyNodeRef::ExprTuple(ast::ExprTuple { + parenthesized: true, + .. + }) => handle_bracketed_end_of_line_comment(comment, locator), AnyNodeRef::ExprGeneratorExp(generator) if is_generator_parenthesized(generator, locator.contents()) => { diff --git a/crates/ruff_python_formatter/src/comments/snapshots/ruff_python_formatter__comments__tests__only_comments.snap b/crates/ruff_python_formatter/src/comments/snapshots/ruff_python_formatter__comments__tests__only_comments.snap index 49f7ca154b3f7..8f5f96bd7224c 100644 --- a/crates/ruff_python_formatter/src/comments/snapshots/ruff_python_formatter__comments__tests__only_comments.snap +++ b/crates/ruff_python_formatter/src/comments/snapshots/ruff_python_formatter__comments__tests__only_comments.snap @@ -5,8 +5,8 @@ expression: comments.debug(test_case.source_code) { Node { kind: ModModule, - range: 0..0, - source: ``, + range: 0..35, + source: `⏎`, }: { "leading": [ SourceComment { diff --git a/crates/ruff_python_formatter/src/expression/expr_invalid.rs b/crates/ruff_python_formatter/src/expression/expr_invalid.rs new file mode 100644 index 0000000000000..a1a39d5207eac --- /dev/null +++ b/crates/ruff_python_formatter/src/expression/expr_invalid.rs @@ -0,0 +1,25 @@ +use ruff_formatter::write; +use ruff_python_ast::AnyNodeRef; +use ruff_python_ast::ExprInvalid; + +use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; +use crate::prelude::*; + +#[derive(Default)] +pub struct FormatExprInvalid; + +impl FormatNodeRule for FormatExprInvalid { + fn fmt_fields(&self, item: &ExprInvalid, f: &mut PyFormatter) -> FormatResult<()> { + write!(f, [source_text_slice(item.range)]) + } +} + +impl NeedsParentheses for ExprInvalid { + fn needs_parentheses( + &self, + _parent: AnyNodeRef, + _context: &PyFormatContext, + ) -> OptionalParentheses { + OptionalParentheses::BestFit + } +} diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index 9db272c611ccd..536a03b45d04a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -116,6 +116,7 @@ impl FormatNodeRule for FormatExprTuple { elts, ctx: _, range: _, + parenthesized: is_parenthesized, } = item; let comments = f.context().comments().clone(); @@ -136,7 +137,7 @@ impl FormatNodeRule for FormatExprTuple { return empty_parenthesized("(", dangling, ")").fmt(f); } [single] => match self.parentheses { - TupleParentheses::Preserve if !item.is_parenthesized(f.context().source()) => { + TupleParentheses::Preserve if !is_parenthesized => { write!(f, [single.format(), token(",")]) } _ => @@ -152,7 +153,7 @@ impl FormatNodeRule for FormatExprTuple { // // Unlike other expression parentheses, tuple parentheses are part of the range of the // tuple itself. - _ if item.is_parenthesized(f.context().source()) + _ if *is_parenthesized && !(self.parentheses == TupleParentheses::NeverPreserve && dangling.is_empty()) => { diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index ab0deee155f67..f74b88753fb26 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -37,6 +37,7 @@ pub(crate) mod expr_ellipsis_literal; pub(crate) mod expr_f_string; pub(crate) mod expr_generator_exp; pub(crate) mod expr_if_exp; +pub(crate) mod expr_invalid; pub(crate) mod expr_ipy_escape_command; pub(crate) mod expr_lambda; pub(crate) mod expr_list; @@ -109,6 +110,8 @@ impl FormatRule> for FormatExpr { Expr::Tuple(expr) => expr.format().fmt(f), Expr::Slice(expr) => expr.format().fmt(f), Expr::IpyEscapeCommand(expr) => expr.format().fmt(f), + #[allow(deprecated)] + Expr::Invalid(expr) => expr.format().fmt(f), }); let parenthesize = match parentheses { Parentheses::Preserve => is_expression_parenthesized( @@ -297,6 +300,8 @@ fn format_with_parentheses_comments( Expr::Tuple(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), Expr::Slice(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), Expr::IpyEscapeCommand(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), + #[allow(deprecated)] + Expr::Invalid(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), }); leading_comments(leading_outer).fmt(f)?; @@ -493,6 +498,8 @@ impl NeedsParentheses for Expr { Expr::Tuple(expr) => expr.needs_parentheses(parent, context), Expr::Slice(expr) => expr.needs_parentheses(parent, context), Expr::IpyEscapeCommand(expr) => expr.needs_parentheses(parent, context), + #[allow(deprecated)] + Expr::Invalid(expr) => expr.needs_parentheses(parent, context), } } } @@ -661,7 +668,10 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> { return; } - Expr::Tuple(tuple) if tuple.is_parenthesized(self.context.source()) => { + Expr::Tuple(ast::ExprTuple { + parenthesized: true, + .. + }) => { self.any_parenthesized_expressions = true; // The values are always parenthesized, don't visit. return; @@ -800,6 +810,10 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> { | Expr::IpyEscapeCommand(_) => { return; } + #[allow(deprecated)] + Expr::Invalid(_) => { + return; + } }; walk_expr(self, expr); @@ -1050,7 +1064,12 @@ pub(crate) fn has_own_parentheses( } } - Expr::Tuple(tuple) if tuple.is_parenthesized(context.source()) => { + Expr::Tuple( + tuple @ ast::ExprTuple { + parenthesized: true, + .. + }, + ) => { if !tuple.elts.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) { Some(OwnParentheses::NonEmpty) } else { @@ -1140,6 +1159,8 @@ pub(crate) fn is_expression_huggable(expr: &Expr, context: &PyFormatContext) -> | Expr::BytesLiteral(_) | Expr::FString(_) | Expr::EllipsisLiteral(_) => false, + #[allow(deprecated)] + Expr::Invalid(_) => false, } } @@ -1200,6 +1221,8 @@ pub(crate) fn is_splittable_expression(expr: &Expr, context: &PyFormatContext) - | Expr::EllipsisLiteral(_) | Expr::Slice(_) | Expr::IpyEscapeCommand(_) => false, + #[allow(deprecated)] + Expr::Invalid(_) => false, // Expressions that insert split points when parenthesized. Expr::Compare(_) diff --git a/crates/ruff_python_formatter/src/generated.rs b/crates/ruff_python_formatter/src/generated.rs index a5217a11d1487..66635bfadec03 100644 --- a/crates/ruff_python_formatter/src/generated.rs +++ b/crates/ruff_python_formatter/src/generated.rs @@ -2074,6 +2074,42 @@ impl<'ast> IntoFormat> for ast::ExprIpyEscapeCommand { } } +impl FormatRule> + for crate::expression::expr_invalid::FormatExprInvalid +{ + #[inline] + fn fmt(&self, node: &ast::ExprInvalid, f: &mut PyFormatter) -> FormatResult<()> { + FormatNodeRule::::fmt(self, node, f) + } +} +impl<'ast> AsFormat> for ast::ExprInvalid { + type Format<'a> = FormatRefWithRule< + 'a, + ast::ExprInvalid, + crate::expression::expr_invalid::FormatExprInvalid, + PyFormatContext<'ast>, + >; + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new( + self, + crate::expression::expr_invalid::FormatExprInvalid::default(), + ) + } +} +impl<'ast> IntoFormat> for ast::ExprInvalid { + type Format = FormatOwnedWithRule< + ast::ExprInvalid, + crate::expression::expr_invalid::FormatExprInvalid, + PyFormatContext<'ast>, + >; + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new( + self, + crate::expression::expr_invalid::FormatExprInvalid::default(), + ) + } +} + impl FormatRule> for crate::other::except_handler_except_handler::FormatExceptHandlerExceptHandler { @@ -2472,6 +2508,42 @@ impl<'ast> IntoFormat> for ast::PatternKeyword { } } +impl FormatRule> + for crate::pattern::pattern_match_invalid::FormatPatternMatchInvalid +{ + #[inline] + fn fmt(&self, node: &ast::PatternMatchInvalid, f: &mut PyFormatter) -> FormatResult<()> { + FormatNodeRule::::fmt(self, node, f) + } +} +impl<'ast> AsFormat> for ast::PatternMatchInvalid { + type Format<'a> = FormatRefWithRule< + 'a, + ast::PatternMatchInvalid, + crate::pattern::pattern_match_invalid::FormatPatternMatchInvalid, + PyFormatContext<'ast>, + >; + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new( + self, + crate::pattern::pattern_match_invalid::FormatPatternMatchInvalid::default(), + ) + } +} +impl<'ast> IntoFormat> for ast::PatternMatchInvalid { + type Format = FormatOwnedWithRule< + ast::PatternMatchInvalid, + crate::pattern::pattern_match_invalid::FormatPatternMatchInvalid, + PyFormatContext<'ast>, + >; + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new( + self, + crate::pattern::pattern_match_invalid::FormatPatternMatchInvalid::default(), + ) + } +} + impl FormatRule> for crate::other::comprehension::FormatComprehension { diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index 011ba245a91eb..bef88f2db83e2 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -134,7 +134,7 @@ pub fn format_module_source( let source_type = options.source_type(); let (tokens, comment_ranges) = tokens_and_ranges(source, source_type).map_err(|err| ParseError { - offset: err.location(), + location: err.location(), error: ParseErrorType::Lexical(err.into_error()), })?; let module = parse_tokens(tokens, source, source_type.as_mode())?; diff --git a/crates/ruff_python_formatter/src/pattern/mod.rs b/crates/ruff_python_formatter/src/pattern/mod.rs index 36d927be2a594..cf7f62f03b13e 100644 --- a/crates/ruff_python_formatter/src/pattern/mod.rs +++ b/crates/ruff_python_formatter/src/pattern/mod.rs @@ -16,6 +16,7 @@ pub(crate) mod pattern_arguments; pub(crate) mod pattern_keyword; pub(crate) mod pattern_match_as; pub(crate) mod pattern_match_class; +pub(crate) mod pattern_match_invalid; pub(crate) mod pattern_match_mapping; pub(crate) mod pattern_match_or; pub(crate) mod pattern_match_sequence; @@ -48,6 +49,8 @@ impl FormatRule> for FormatPattern { Pattern::MatchStar(pattern) => pattern.format().fmt(f), Pattern::MatchAs(pattern) => pattern.format().fmt(f), Pattern::MatchOr(pattern) => pattern.format().fmt(f), + #[allow(deprecated)] + Pattern::Invalid(pattern) => pattern.format().fmt(f), }); let parenthesize = match self.parentheses { @@ -147,6 +150,8 @@ impl NeedsParentheses for Pattern { Pattern::MatchStar(pattern) => pattern.needs_parentheses(parent, context), Pattern::MatchAs(pattern) => pattern.needs_parentheses(parent, context), Pattern::MatchOr(pattern) => pattern.needs_parentheses(parent, context), + #[allow(deprecated)] + Pattern::Invalid(pattern) => pattern.needs_parentheses(parent, context), } } } diff --git a/crates/ruff_python_formatter/src/pattern/patter_match_invalid.rs b/crates/ruff_python_formatter/src/pattern/patter_match_invalid.rs new file mode 100644 index 0000000000000..c099ce2b3d372 --- /dev/null +++ b/crates/ruff_python_formatter/src/pattern/patter_match_invalid.rs @@ -0,0 +1,21 @@ +use ruff_formatter::write; +use ruff_python_ast::AnyNodeRef; +use ruff_python_ast::PatternMatchInvalid; + +use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; +use crate::prelude::*; + +#[derive(Default)] +pub struct FormatPatternMatchInvalid; + +impl FormatNodeRule for FormatPatternMatchInvalid { + fn fmt_fields(&self, item: &PatternMatchInvalid, f: &mut PyFormatter) -> FormatResult<()> { + write!(f, [source_text_slice(item.range)]) + } +} + +impl NeedsParentheses for PatternMatchInvalid { + fn needs_parentheses(&self, _parent: AnyNodeRef, _context: &PyFormatContext) -> OptionalParentheses { + OptionalParentheses::Never + } +} diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_invalid.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_invalid.rs new file mode 100644 index 0000000000000..1ebdb114ca3de --- /dev/null +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_invalid.rs @@ -0,0 +1,25 @@ +use crate::{ + expression::parentheses::{NeedsParentheses, OptionalParentheses}, + prelude::*, +}; +use ruff_formatter::write; +use ruff_python_ast::{AnyNodeRef, PatternMatchInvalid}; + +#[derive(Default)] +pub struct FormatPatternMatchInvalid; + +impl FormatNodeRule for FormatPatternMatchInvalid { + fn fmt_fields(&self, item: &PatternMatchInvalid, f: &mut PyFormatter) -> FormatResult<()> { + write!(f, [source_text_slice(item.range)]) + } +} + +impl NeedsParentheses for PatternMatchInvalid { + fn needs_parentheses( + &self, + _parent: AnyNodeRef, + _context: &PyFormatContext, + ) -> OptionalParentheses { + OptionalParentheses::Never + } +} diff --git a/crates/ruff_python_formatter/src/range.rs b/crates/ruff_python_formatter/src/range.rs index c187c03b53ebc..1f8c9c9d7f8dc 100644 --- a/crates/ruff_python_formatter/src/range.rs +++ b/crates/ruff_python_formatter/src/range.rs @@ -73,7 +73,7 @@ pub fn format_range( let (tokens, comment_ranges) = tokens_and_ranges(source, options.source_type()).map_err(|err| ParseError { - offset: err.location(), + location: err.location(), error: ParseErrorType::Lexical(err.into_error()), })?; @@ -646,6 +646,8 @@ impl Format> for FormatEnclosingNode<'_> { AnyNodeRef::MatchCase(node) => node.format().fmt(f), AnyNodeRef::Decorator(node) => node.format().fmt(f), AnyNodeRef::ElifElseClause(node) => node.format().fmt(f), + AnyNodeRef::ExprInvalid(node) => node.format().fmt(f), + AnyNodeRef::PatternMatchInvalid(node) => node.format().fmt(f), AnyNodeRef::ExprBoolOp(_) | AnyNodeRef::ExprNamedExpr(_) @@ -706,7 +708,8 @@ impl Format> for FormatEnclosingNode<'_> { | AnyNodeRef::TypeParamTypeVar(_) | AnyNodeRef::TypeParamTypeVarTuple(_) | AnyNodeRef::TypeParamParamSpec(_) - | AnyNodeRef::BytesLiteral(_) => { + | AnyNodeRef::BytesLiteral(_) + | AnyNodeRef::FStringInvalidElement(_) => { panic!("Range formatting only supports formatting logical lines") } } diff --git a/crates/ruff_python_formatter/tests/snapshots/format@empty_multiple_trailing_newlines.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@empty_multiple_trailing_newlines.py.snap index 5582da34d901e..3c007612f25fb 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@empty_multiple_trailing_newlines.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@empty_multiple_trailing_newlines.py.snap @@ -11,7 +11,6 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_mult ## Output ```python - ``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@empty_trailing_newline.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@empty_trailing_newline.py.snap index 516325c9df299..a8690d22d3c76 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@empty_trailing_newline.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@empty_trailing_newline.py.snap @@ -9,7 +9,6 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/empty_trai ## Output ```python - ``` diff --git a/crates/ruff_python_parser/Cargo.toml b/crates/ruff_python_parser/Cargo.toml index 886bb07fec0b6..5aa6bd3b50116 100644 --- a/crates/ruff_python_parser/Cargo.toml +++ b/crates/ruff_python_parser/Cargo.toml @@ -19,6 +19,7 @@ ruff_text_size = { path = "../ruff_text_size" } anyhow = { workspace = true } bitflags = { workspace = true } +drop_bomb = { workspace = true } bstr = { workspace = true } is-macro = { workspace = true } itertools = { workspace = true } @@ -30,7 +31,10 @@ unicode-ident = { workspace = true } unicode_names2 = { workspace = true } [dev-dependencies] -insta = { workspace = true } +ruff_source_file = { path = "../ruff_source_file" } + +annotate-snippets = { workspace = true } +insta = { workspace = true, features = ["glob"] } [build-dependencies] anyhow = { workspace = true } diff --git a/crates/ruff_python_parser/build.rs b/crates/ruff_python_parser/build.rs index 8544bd88db5ce..df97eebaad46e 100644 --- a/crates/ruff_python_parser/build.rs +++ b/crates/ruff_python_parser/build.rs @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf}; use tiny_keccak::{Hasher, Sha3}; fn main() { - const SOURCE: &str = "src/python.lalrpop"; + const SOURCE: &str = "src/lalrpop/python.lalrpop"; println!("cargo:rerun-if-changed={SOURCE}"); let target; @@ -14,12 +14,12 @@ fn main() { #[cfg(feature = "lalrpop")] { let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); - target = out_dir.join("src/python.rs"); + target = out_dir.join("src/lalrpop/python.rs"); } #[cfg(not(feature = "lalrpop"))] { - target = PathBuf::from("src/python.rs"); - error = "python.lalrpop and src/python.rs doesn't match. This is a ruff_python_parser bug. Please report it unless you are editing ruff_python_parser. Run `lalrpop src/python.lalrpop` to build ruff_python_parser again."; + target = PathBuf::from("src/lalrpop/python.rs"); + error = "python.lalrpop and src/lalrpop/python.rs doesn't match. This is a ruff_python_parser bug. Please report it unless you are editing ruff_python_parser. Run `lalrpop src/lalrpop/python.lalrpop` to build ruff_python_parser again."; } let Some(message) = requires_lalrpop(SOURCE, &target) else { diff --git a/crates/ruff_python_parser/resources/.editorconfig b/crates/ruff_python_parser/resources/.editorconfig new file mode 100644 index 0000000000000..8a311dc0374aa --- /dev/null +++ b/crates/ruff_python_parser/resources/.editorconfig @@ -0,0 +1,6 @@ +# Check http://editorconfig.org for more information +# This is the main config file for this project: +root = true + +[*.py] +insert_final_newline = false diff --git a/crates/ruff_python_parser/resources/invalid/expressions/call_invalid_argument_ordering.py b/crates/ruff_python_parser/resources/invalid/expressions/call_invalid_argument_ordering.py new file mode 100644 index 0000000000000..72db97d8858b1 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/expressions/call_invalid_argument_ordering.py @@ -0,0 +1,6 @@ +f(b=20, c) + +f(**b, *c) + +# Duplicate keyword argument +f(a=20, a=30) diff --git a/crates/ruff_python_parser/resources/invalid/expressions/emoji_identifiers.py b/crates/ruff_python_parser/resources/invalid/expressions/emoji_identifiers.py new file mode 100644 index 0000000000000..1a6f4e81192d3 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/expressions/emoji_identifiers.py @@ -0,0 +1,7 @@ +a = (🐶 + # comment 🐶 +) + +a = (🐶 + + # comment +🐶) diff --git a/crates/ruff_python_parser/resources/invalid/expressions/emoji_statement.py b/crates/ruff_python_parser/resources/invalid/expressions/emoji_statement.py new file mode 100644 index 0000000000000..ace0b338cc7a9 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/expressions/emoji_statement.py @@ -0,0 +1 @@ +👍 diff --git a/crates/ruff_python_parser/resources/invalid/expressions/lambda_default_parameters.py b/crates/ruff_python_parser/resources/invalid/expressions/lambda_default_parameters.py new file mode 100644 index 0000000000000..625b621528fd0 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/expressions/lambda_default_parameters.py @@ -0,0 +1,2 @@ +# TODO(micha): The offset of the generated error message is off by one. +lambda a, b=20, c: 1 diff --git a/crates/ruff_python_parser/resources/invalid/expressions/lambda_duplicate_parameters.py b/crates/ruff_python_parser/resources/invalid/expressions/lambda_duplicate_parameters.py new file mode 100644 index 0000000000000..4edd2de546ce9 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/expressions/lambda_duplicate_parameters.py @@ -0,0 +1,9 @@ +lambda a, a: 1 + +lambda a, *, a: 1 + +lambda a, a=20: 1 + +lambda a, *a: 1 + +lambda a, *, **a: 1 diff --git a/crates/ruff_python_parser/resources/invalid/statements/assignment_keyword_target.py b/crates/ruff_python_parser/resources/invalid/statements/assignment_keyword_target.py new file mode 100644 index 0000000000000..60d6fce04faea --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/assignment_keyword_target.py @@ -0,0 +1,7 @@ +a = pass = c + +a + b + +a = b = pass = c + +a + b \ No newline at end of file diff --git a/crates/ruff_python_parser/resources/invalid/statements/assignment_missing_target.py b/crates/ruff_python_parser/resources/invalid/statements/assignment_missing_target.py new file mode 100644 index 0000000000000..331fd684a395e --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/assignment_missing_target.py @@ -0,0 +1,7 @@ +a = = c + +a + b + + + + diff --git a/crates/ruff_python_parser/resources/invalid/statements/assignment_missing_value.py b/crates/ruff_python_parser/resources/invalid/statements/assignment_missing_value.py new file mode 100644 index 0000000000000..39dff789e2229 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/assignment_missing_value.py @@ -0,0 +1,3 @@ +a = + +a + b \ No newline at end of file diff --git a/crates/ruff_python_parser/resources/invalid/statements/function_default_parameters.py b/crates/ruff_python_parser/resources/invalid/statements/function_default_parameters.py new file mode 100644 index 0000000000000..18782b804073f --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/function_default_parameters.py @@ -0,0 +1,2 @@ +# TODO(micha): The range of the generated error message is off by one. +def f(a, b=20, c): pass diff --git a/crates/ruff_python_parser/resources/invalid/statements/function_duplicate_parameters.py b/crates/ruff_python_parser/resources/invalid/statements/function_duplicate_parameters.py new file mode 100644 index 0000000000000..484c3c6e52e51 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/function_duplicate_parameters.py @@ -0,0 +1,12 @@ +def f(a, a): pass + +def f2(a, *, a): pass + +def f3(a, a=20): pass + +def f4(a, *a): pass + +def f5(a, *, **a): pass + +# TODO(micha): This is inconsistent. All other examples only highlight the argument name. +def f6(a, a: str): pass diff --git a/crates/ruff_python_parser/resources/invalid/statements/function_type_parameters.py b/crates/ruff_python_parser/resources/invalid/statements/function_type_parameters.py new file mode 100644 index 0000000000000..69723fed37a06 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/function_type_parameters.py @@ -0,0 +1,24 @@ +# FIXME: The type param related error message and the parser recovery are looking pretty good **except** +# that the lexer never recovers from the unclosed `[`, resulting in it lexing `NonLogicalNewline` tokens instead of `Newline` tokens. +# That's because the parser has no way of feeding the error recovery back to the lexer, +# so they don't agree on the state of the world which can lead to all kind of errors further down in the file. +# This is not just a problem with parentheses but also with the transformation made by the +# `SoftKeywordTransformer` because the `Parser` and `Transfomer` may not agree if they're +# currently in a position where the `type` keyword is allowed or not. +# That roughly means that any kind of recovery can lead to unrelated syntax errors +# on following lines. + +def unclosed[A, *B(test: name): + pass + + a + b + +def keyword[A, await](): ... + +def not_a_type_param[A, |, B](): ... + +def multiple_commas[A,,B](): ... + +def multiple_trailing_commas[A,,](): ... + +def multiple_commas_and_recovery[A,,100](): ... \ No newline at end of file diff --git a/crates/ruff_python_parser/resources/invalid/statements/if_elif.py b/crates/ruff_python_parser/resources/invalid/statements/if_elif.py new file mode 100644 index 0000000000000..dfc0911eddf0f --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/if_elif.py @@ -0,0 +1,7 @@ +if True: + pass +elif False: + pass +elf: + pass + diff --git a/crates/ruff_python_parser/resources/invalid/statements/if_extra_closing_parentheses.py b/crates/ruff_python_parser/resources/invalid/statements/if_extra_closing_parentheses.py new file mode 100644 index 0000000000000..12596d31a7204 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/if_extra_closing_parentheses.py @@ -0,0 +1,3 @@ +# FIXME(micha): This creates two syntax errors instead of just one (and overlapping ones) +if True)): + pass diff --git a/crates/ruff_python_parser/resources/invalid/statements/if_extra_indent.py b/crates/ruff_python_parser/resources/invalid/statements/if_extra_indent.py new file mode 100644 index 0000000000000..647626cb5d359 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/if_extra_indent.py @@ -0,0 +1,8 @@ +# Improving the recovery would require changing the lexer to emit an extra dedent token after `a + b`. +if True: + pass + a + b + + pass + +a = 10 diff --git a/crates/ruff_python_parser/resources/invalid/statements/if_missing_body.py b/crates/ruff_python_parser/resources/invalid/statements/if_missing_body.py new file mode 100644 index 0000000000000..c63e88efee7da --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/if_missing_body.py @@ -0,0 +1,6 @@ +if True: + + +a + b + +if False: # This if statement has neither an indent nor a newline. \ No newline at end of file diff --git a/crates/ruff_python_parser/resources/invalid/statements/if_missing_colon.py b/crates/ruff_python_parser/resources/invalid/statements/if_missing_colon.py new file mode 100644 index 0000000000000..66b48c7bd4a8c --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/if_missing_colon.py @@ -0,0 +1,4 @@ +if True + pass + +a = 10 diff --git a/crates/ruff_python_parser/resources/invalid/statements/import_from.py b/crates/ruff_python_parser/resources/invalid/statements/import_from.py new file mode 100644 index 0000000000000..59f3b73ddc4fe --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/import_from.py @@ -0,0 +1,11 @@ +from abc import a, b, + +a + b + +from abc import ,, + +from abc import + +from abc import (a, b, c + +a + b \ No newline at end of file diff --git a/crates/ruff_python_parser/resources/invalid/statements/invalid_assignment_targets.py b/crates/ruff_python_parser/resources/invalid/statements/invalid_assignment_targets.py new file mode 100644 index 0000000000000..106684f75f8c1 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/invalid_assignment_targets.py @@ -0,0 +1,53 @@ +# Regression test: https://github.com/astral-sh/ruff/issues/6895 +# First we test, broadly, that various kinds of assignments are now +# rejected by the parser. e.g., `5 = 3`, `5 += 3`, `(5): int = 3`. + +5 = 3 + +5 += 3 + +(5): int = 3 + +# Now we exhaustively test all possible cases where assignment can fail. +x or y = 42 +(x := 5) = 42 +x + y = 42 +-x = 42 +(lambda _: 1) = 42 +a if b else c = 42 +{"a": 5} = 42 +{a} = 42 +[x for x in xs] = 42 +{x for x in xs} = 42 +{x: x * 2 for x in xs} = 42 +(x for x in xs) = 42 +await x = 42 +(yield x) = 42 +(yield from xs) = 42 +a < b < c = 42 +foo() = 42 + +# N.B. It looks like the parser can't generate a top-level +# FormattedValue, where as the official Python AST permits +# representing a single f-string containing just a variable as a +# FormattedValue directly. +# +# Bottom line is that because of this, this test is (at present) +# duplicative with the `fstring` test. That is, in theory these tests +# could fail independently, but in practice their failure or success +# is coupled. +# +# See: https://docs.python.org/3/library/ast.html#ast.FormattedValue +f"{quux}" = 42 +f"{foo} and {bar}" = 42 + +"foo" = 42 +b"foo" = 42 +123 = 42 +True = 42 +None = 42 +... = 42 +*foo() = 42 +[x, foo(), y] = [42, 42, 42] +[[a, b], [[42]], d] = [[1, 2], [[3]], 4] +(x, foo(), y) = (42, 42, 42) diff --git a/crates/ruff_python_parser/resources/invalid/statements/named_expression_statement.py b/crates/ruff_python_parser/resources/invalid/statements/named_expression_statement.py new file mode 100644 index 0000000000000..fb256fecae7c5 --- /dev/null +++ b/crates/ruff_python_parser/resources/invalid/statements/named_expression_statement.py @@ -0,0 +1,3 @@ +# This test previously passed before the assignment operator checking +# above, but we include it here for good measure. +(5 := 3) diff --git a/crates/ruff_python_parser/resources/valid/expressions/dictionary_comprehension.py b/crates/ruff_python_parser/resources/valid/expressions/dictionary_comprehension.py new file mode 100644 index 0000000000000..474cd386c860c --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/expressions/dictionary_comprehension.py @@ -0,0 +1 @@ +x = {y for y in (1, 2, 3)} diff --git a/crates/ruff_python_parser/resources/valid/expressions/lambda.py b/crates/ruff_python_parser/resources/valid/expressions/lambda.py new file mode 100644 index 0000000000000..545a5aa78095a --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/expressions/lambda.py @@ -0,0 +1,11 @@ +lambda: 1 + +lambda a, b, c: 1 + +lambda a, b=20, c=30: 1 + +lambda *, a, b, c: 1 + +lambda *, a, b=20, c=30: 1 + +lambda a, b, c, *, d, e: 0 diff --git a/crates/ruff_python_parser/resources/valid/expressions/list_comprehension.py b/crates/ruff_python_parser/resources/valid/expressions/list_comprehension.py new file mode 100644 index 0000000000000..e4f44467bc02a --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/expressions/list_comprehension.py @@ -0,0 +1 @@ +x = [y for y in (1, 2, 3)] diff --git a/crates/ruff_python_parser/resources/valid/expressions/named_expression.py b/crates/ruff_python_parser/resources/valid/expressions/named_expression.py new file mode 100644 index 0000000000000..bb8d8df79ab3e --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/expressions/named_expression.py @@ -0,0 +1,4 @@ +if x := 1: + pass + +(x := 5) diff --git a/crates/ruff_python_parser/resources/valid/statement/annotated_assignment.py b/crates/ruff_python_parser/resources/valid/statement/annotated_assignment.py new file mode 100644 index 0000000000000..f6488ba1e91c4 --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/statement/annotated_assignment.py @@ -0,0 +1 @@ +x: int = 1 diff --git a/crates/ruff_python_parser/resources/valid/statement/assignment.py b/crates/ruff_python_parser/resources/valid/statement/assignment.py new file mode 100644 index 0000000000000..3c45b1f9437df --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/statement/assignment.py @@ -0,0 +1,40 @@ +x = (1, 2, 3) + +(x, y) = (1, 2, 3) + +[x, y] = (1, 2, 3) + +x.y = (1, 2, 3) + +x[y] = (1, 2, 3) + +(x, *y) = (1, 2, 3) + + +# This last group of tests checks that assignments we expect to be parsed +# (including some interesting ones) continue to be parsed successfully. + +*foo = 42 + +[x, y, z] = [1, 2, 3] + +(x, y, z) = (1, 2, 3) +x[0] = 42 + +# This is actually a type error, not a syntax error. So check that it +# doesn't fail parsing. + +5[0] = 42 +x[1:2] = [42] + +# This is actually a type error, not a syntax error. So check that it +# doesn't fail parsing. +5[1:2] = [42] + +foo.bar = 42 + +# This is actually an attribute error, not a syntax error. So check that +# it doesn't fail parsing. +"foo".y = 42 + +foo = 42 diff --git a/crates/ruff_python_parser/resources/valid/statement/augmented_assignment.py b/crates/ruff_python_parser/resources/valid/statement/augmented_assignment.py new file mode 100644 index 0000000000000..289c234665180 --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/statement/augmented_assignment.py @@ -0,0 +1,3 @@ +x += 1 +x.y += (1, 2, 3) +x[y] += (1, 2, 3) diff --git a/crates/ruff_python_parser/resources/valid/statement/delete.py b/crates/ruff_python_parser/resources/valid/statement/delete.py new file mode 100644 index 0000000000000..14c14a99a821d --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/statement/delete.py @@ -0,0 +1,3 @@ +del x +del x.y +del x[y] diff --git a/crates/ruff_python_parser/resources/valid/statement/for.py b/crates/ruff_python_parser/resources/valid/statement/for.py new file mode 100644 index 0000000000000..38078c618f3aa --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/statement/for.py @@ -0,0 +1,2 @@ +for x in (1, 2, 3): + pass diff --git a/crates/ruff_python_parser/resources/valid/statement/function.py b/crates/ruff_python_parser/resources/valid/statement/function.py new file mode 100644 index 0000000000000..c32db6afb65bb --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/statement/function.py @@ -0,0 +1,38 @@ +def no_parameters(): + pass + + +def positional_parameters(a, b, c): + pass + + +def positional_parameters_with_default_values(a, b=20, c=30): + pass + + +def keyword_only_parameters(*, a, b, c): + pass + + +def keyword_only_parameters_with_defaults(*, a, b=20, c=30): + pass + + +def positional_and_keyword_parameters(a, b, c, *, d, e, f): + pass + + +def positional_and_keyword_parameters_with_defaults(a, b, c, *, d, e=20, f=30): + pass + + +def positional_and_keyword_parameters_with_defaults_and_varargs( + a, b, c, *args, d, e=20, f=30 +): + pass + + +def positional_and_keyword_parameters_with_defaults_and_varargs_and_kwargs( + a, b, c, *args, d, e=20, f=30, **kwargs +): + pass diff --git a/crates/ruff_python_parser/resources/valid/statement/with.py b/crates/ruff_python_parser/resources/valid/statement/with.py new file mode 100644 index 0000000000000..e1b16ae47bf7d --- /dev/null +++ b/crates/ruff_python_parser/resources/valid/statement/with.py @@ -0,0 +1,2 @@ +with 1 as x: + pass diff --git a/crates/ruff_python_parser/src/context.rs b/crates/ruff_python_parser/src/context.rs deleted file mode 100644 index 8620c5849ebfb..0000000000000 --- a/crates/ruff_python_parser/src/context.rs +++ /dev/null @@ -1,179 +0,0 @@ -use ruff_python_ast::{self as ast, Expr, ExprContext}; - -pub(crate) fn set_context(expr: Expr, ctx: ExprContext) -> Expr { - match expr { - Expr::Name(ast::ExprName { id, range, .. }) => ast::ExprName { range, id, ctx }.into(), - Expr::Tuple(ast::ExprTuple { elts, range, .. }) => ast::ExprTuple { - elts: elts.into_iter().map(|elt| set_context(elt, ctx)).collect(), - range, - ctx, - } - .into(), - - Expr::List(ast::ExprList { elts, range, .. }) => ast::ExprList { - elts: elts.into_iter().map(|elt| set_context(elt, ctx)).collect(), - range, - ctx, - } - .into(), - Expr::Attribute(ast::ExprAttribute { - value, attr, range, .. - }) => ast::ExprAttribute { - range, - value, - attr, - ctx, - } - .into(), - Expr::Subscript(ast::ExprSubscript { - value, - slice, - range, - .. - }) => ast::ExprSubscript { - range, - value, - slice, - ctx, - } - .into(), - Expr::Starred(ast::ExprStarred { value, range, .. }) => ast::ExprStarred { - value: Box::new(set_context(*value, ctx)), - range, - ctx, - } - .into(), - _ => expr, - } -} - -#[cfg(test)] -mod tests { - use crate::parser::parse_suite; - - #[test] - fn test_assign_name() { - let source = "x = (1, 2, 3)"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_tuple() { - let source = "(x, y) = (1, 2, 3)"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_list() { - let source = "[x, y] = (1, 2, 3)"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_attribute() { - let source = "x.y = (1, 2, 3)"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_subscript() { - let source = "x[y] = (1, 2, 3)"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_starred() { - let source = "(x, *y) = (1, 2, 3)"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_for() { - let source = "for x in (1, 2, 3): pass"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_list_comp() { - let source = "x = [y for y in (1, 2, 3)]"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_set_comp() { - let source = "x = {y for y in (1, 2, 3)}"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_with() { - let source = "with 1 as x: pass"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_assign_named_expr() { - let source = "if x:= 1: pass"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_ann_assign_name() { - let source = "x: int = 1"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_aug_assign_name() { - let source = "x += 1"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_aug_assign_attribute() { - let source = "x.y += (1, 2, 3)"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_aug_assign_subscript() { - let source = "x[y] += (1, 2, 3)"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_del_name() { - let source = "del x"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_del_attribute() { - let source = "del x.y"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } - - #[test] - fn test_del_subscript() { - let source = "del x[y]"; - let parse_ast = parse_suite(source).unwrap(); - insta::assert_debug_snapshot!(parse_ast); - } -} diff --git a/crates/ruff_python_parser/src/error.rs b/crates/ruff_python_parser/src/error.rs new file mode 100644 index 0000000000000..18f86cddfdef2 --- /dev/null +++ b/crates/ruff_python_parser/src/error.rs @@ -0,0 +1,212 @@ +use std::fmt; + +use ruff_text_size::TextRange; + +use crate::{ + lexer::{LexicalError, LexicalErrorType}, + Tok, TokenKind, +}; + +/// Represents represent errors that occur during parsing and are +/// returned by the `parse_*` functions. +#[derive(Debug, PartialEq)] +pub struct ParseError { + pub error: ParseErrorType, + pub location: TextRange, +} + +impl std::ops::Deref for ParseError { + type Target = ParseErrorType; + + fn deref(&self) -> &Self::Target { + &self.error + } +} + +impl std::error::Error for ParseError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(&self.error) + } +} + +impl fmt::Display for ParseError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{} at byte range {:?}", &self.error, self.location) + } +} + +impl From for ParseError { + fn from(error: LexicalError) -> Self { + ParseError { + location: error.location(), + error: ParseErrorType::Lexical(error.into_error()), + } + } +} + +impl ParseError { + pub fn error(self) -> ParseErrorType { + self.error + } +} + +/// Represents the different types of errors that can occur during parsing of an f-string. +#[derive(Debug, Clone, PartialEq)] +pub enum FStringErrorType { + /// Expected a right brace after an opened left brace. + UnclosedLbrace, + /// An invalid conversion flag was encountered. + InvalidConversionFlag, + /// A single right brace was encountered. + SingleRbrace, + /// Unterminated string. + UnterminatedString, + /// Unterminated triple-quoted string. + UnterminatedTripleQuotedString, + /// A lambda expression without parentheses was encountered. + LambdaWithoutParentheses, +} + +impl std::fmt::Display for FStringErrorType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + use FStringErrorType::{ + InvalidConversionFlag, LambdaWithoutParentheses, SingleRbrace, UnclosedLbrace, + UnterminatedString, UnterminatedTripleQuotedString, + }; + match self { + UnclosedLbrace => write!(f, "expecting '}}'"), + InvalidConversionFlag => write!(f, "invalid conversion character"), + SingleRbrace => write!(f, "single '}}' is not allowed"), + UnterminatedString => write!(f, "unterminated string"), + UnterminatedTripleQuotedString => write!(f, "unterminated triple-quoted string"), + LambdaWithoutParentheses => { + write!(f, "lambda expressions are not allowed without parentheses") + } + } + } +} + +/// Represents the different types of errors that can occur during parsing. +#[derive(Debug, PartialEq)] +pub enum ParseErrorType { + /// An unexpected error occurred. + OtherError(String), + /// An empty slice was found during parsing, e.g `l[]`. + EmptySlice, + /// An invalid expression was found in the assignment `target`. + AssignmentError, + /// An invalid expression was found in the named assignment `target`. + NamedAssignmentError, + /// An invalid expression was found in the augmented assignment `target`. + AugAssignmentError, + /// Multiple simple statements were found in the same line without a `;` separating them. + SimpleStmtsInSameLine, + /// An unexpected indentation was found during parsing. + UnexpectedIndentation, + /// The statement being parsed cannot be `async`. + StmtIsNotAsync(TokenKind), + /// A parameter was found after a vararg + ParamFollowsVarKeywordParam, + /// A positional argument follows a keyword argument. + PositionalArgumentError, + /// An iterable argument unpacking `*args` follows keyword argument unpacking `**kwargs`. + UnpackedArgumentError, + /// A non-default argument follows a default argument. + DefaultArgumentError, + /// A simple statement and a compound statement was found in the same line. + SimpleStmtAndCompoundStmtInSameLine, + /// An invalid `match` case pattern was found. + InvalidMatchPatternLiteral { pattern: TokenKind }, + /// The parser expected a specific token that was not found. + ExpectedToken { + expected: TokenKind, + found: TokenKind, + }, + /// A duplicate argument was found in a function definition. + DuplicateArgumentError(String), + /// A keyword argument was repeated. + DuplicateKeywordArgumentError(String), + /// An f-string error containing the [`FStringErrorType`]. + FStringError(FStringErrorType), + /// Parser encountered an error during lexing. + Lexical(LexicalErrorType), + + // RustPython specific. + /// Parser encountered an extra token + ExtraToken(Tok), + /// Parser encountered an invalid token + InvalidToken, + /// Parser encountered an unexpected token + UnrecognizedToken(Tok, Option), + /// Parser encountered an unexpected end of input + Eof, +} + +impl std::error::Error for ParseErrorType {} + +impl std::fmt::Display for ParseErrorType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + ParseErrorType::OtherError(msg) => write!(f, "{msg}"), + ParseErrorType::ExpectedToken { found, expected } => { + write!(f, "expected {expected:?}, found {found:?}") + } + ParseErrorType::Lexical(ref lex_error) => write!(f, "{lex_error}"), + ParseErrorType::SimpleStmtsInSameLine => { + write!(f, "use `;` to separate simple statements") + } + ParseErrorType::SimpleStmtAndCompoundStmtInSameLine => write!( + f, + "compound statements not allowed in the same line as simple statements" + ), + ParseErrorType::StmtIsNotAsync(kind) => { + write!(f, "`{kind:?}` statement cannot be async") + } + ParseErrorType::UnpackedArgumentError => { + write!( + f, + "iterable argument unpacking follows keyword argument unpacking" + ) + } + ParseErrorType::PositionalArgumentError => { + write!(f, "positional argument follows keyword argument unpacking") + } + ParseErrorType::EmptySlice => write!(f, "slice cannot be empty"), + ParseErrorType::ParamFollowsVarKeywordParam => { + write!(f, "parameters cannot follow var-keyword parameter") + } + ParseErrorType::DefaultArgumentError => { + write!(f, "non-default argument follows default argument") + } + ParseErrorType::InvalidMatchPatternLiteral { pattern } => { + write!(f, "invalid pattern `{pattern:?}`") + } + ParseErrorType::UnexpectedIndentation => write!(f, "unexpected indentation"), + ParseErrorType::AssignmentError => write!(f, "invalid assignment target"), + ParseErrorType::NamedAssignmentError => write!(f, "invalid assignment target"), + ParseErrorType::AugAssignmentError => write!(f, "invalid augmented assignment target"), + ParseErrorType::DuplicateArgumentError(arg_name) => { + write!(f, "duplicate argument '{arg_name}' in function definition") + } + ParseErrorType::DuplicateKeywordArgumentError(arg_name) => { + write!(f, "keyword argument repeated: {arg_name}") + } + ParseErrorType::FStringError(ref fstring_error) => { + write!(f, "f-string: {fstring_error}") + } + // RustPython specific. + ParseErrorType::Eof => write!(f, "Got unexpected EOF"), + ParseErrorType::ExtraToken(ref tok) => write!(f, "Got extraneous token: {tok:?}"), + ParseErrorType::InvalidToken => write!(f, "Got invalid token"), + ParseErrorType::UnrecognizedToken(ref tok, ref expected) => { + if *tok == Tok::Indent { + write!(f, "Unexpected indent") + } else if expected.as_deref() == Some("Indent") { + write!(f, "Expected an indented block") + } else { + write!(f, "Unexpected token {tok}") + } + } + } + } +} diff --git a/crates/ruff_python_parser/src/function.rs b/crates/ruff_python_parser/src/function.rs deleted file mode 100644 index 38045cb028a60..0000000000000 --- a/crates/ruff_python_parser/src/function.rs +++ /dev/null @@ -1,236 +0,0 @@ -use std::hash::BuildHasherDefault; -// Contains functions that perform validation and parsing of arguments and parameters. -// Checks apply both to functions and to lambdas. -use crate::lexer::{LexicalError, LexicalErrorType}; -use ruff_python_ast::{self as ast}; -use ruff_text_size::{Ranged, TextRange, TextSize}; -use rustc_hash::FxHashSet; - -pub(crate) struct ArgumentList { - pub(crate) args: Vec, - pub(crate) keywords: Vec, -} - -// Perform validation of function/lambda arguments in a function definition. -pub(crate) fn validate_arguments(arguments: &ast::Parameters) -> Result<(), LexicalError> { - let mut all_arg_names = FxHashSet::with_capacity_and_hasher( - arguments.posonlyargs.len() - + arguments.args.len() - + usize::from(arguments.vararg.is_some()) - + arguments.kwonlyargs.len() - + usize::from(arguments.kwarg.is_some()), - BuildHasherDefault::default(), - ); - - let posonlyargs = arguments.posonlyargs.iter(); - let args = arguments.args.iter(); - let kwonlyargs = arguments.kwonlyargs.iter(); - - let vararg: Option<&ast::Parameter> = arguments.vararg.as_deref(); - let kwarg: Option<&ast::Parameter> = arguments.kwarg.as_deref(); - - for arg in posonlyargs - .chain(args) - .chain(kwonlyargs) - .map(|arg| &arg.parameter) - .chain(vararg) - .chain(kwarg) - { - let range = arg.range; - let arg_name = arg.name.as_str(); - if !all_arg_names.insert(arg_name) { - return Err(LexicalError::new( - LexicalErrorType::DuplicateArgumentError(arg_name.to_string().into_boxed_str()), - range.start(), - )); - } - } - - Ok(()) -} - -pub(crate) fn validate_pos_params( - args: &( - Vec, - Vec, - ), -) -> Result<(), LexicalError> { - let (posonlyargs, args) = args; - #[allow(clippy::skip_while_next)] - let first_invalid = posonlyargs - .iter() - .chain(args.iter()) // for all args - .skip_while(|arg| arg.default.is_none()) // starting with args without default - .skip_while(|arg| arg.default.is_some()) // and then args with default - .next(); // there must not be any more args without default - if let Some(invalid) = first_invalid { - return Err(LexicalError::new( - LexicalErrorType::DefaultArgumentError, - invalid.parameter.start(), - )); - } - Ok(()) -} - -type FunctionArgument = ( - Option<(TextSize, TextSize, Option)>, - ast::Expr, -); - -// Parse arguments as supplied during a function/lambda *call*. -pub(crate) fn parse_arguments( - function_arguments: Vec, -) -> Result { - // First, run through the comments to determine the number of positional and keyword arguments. - let mut keyword_names = FxHashSet::with_capacity_and_hasher( - function_arguments.len(), - BuildHasherDefault::default(), - ); - let mut double_starred = false; - let mut num_args = 0; - let mut num_keywords = 0; - for (name, value) in &function_arguments { - if let Some((start, _end, name)) = name { - // Check for duplicate keyword arguments in the call. - if let Some(keyword_name) = &name { - if !keyword_names.insert(keyword_name.to_string()) { - return Err(LexicalError::new( - LexicalErrorType::DuplicateKeywordArgumentError( - keyword_name.to_string().into_boxed_str(), - ), - *start, - )); - } - } else { - double_starred = true; - } - - num_keywords += 1; - } else { - // Positional arguments mustn't follow keyword arguments. - if num_keywords > 0 && !is_starred(value) { - return Err(LexicalError::new( - LexicalErrorType::PositionalArgumentError, - value.start(), - )); - // Allow starred arguments after keyword arguments but - // not after double-starred arguments. - } else if double_starred { - return Err(LexicalError::new( - LexicalErrorType::UnpackedArgumentError, - value.start(), - )); - } - - num_args += 1; - } - } - - // Second, push the arguments into vectors of exact capacity. This avoids a vector resize later - // on when these vectors are boxed into slices. - let mut args = Vec::with_capacity(num_args); - let mut keywords = Vec::with_capacity(num_keywords); - for (name, value) in function_arguments { - if let Some((start, end, name)) = name { - keywords.push(ast::Keyword { - arg: name, - value, - range: TextRange::new(start, end), - }); - } else { - args.push(value); - } - } - - Ok(ArgumentList { args, keywords }) -} - -// Check if an expression is a starred expression. -const fn is_starred(exp: &ast::Expr) -> bool { - exp.is_starred_expr() -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser::parse_suite; - use crate::ParseErrorType; - - macro_rules! function_and_lambda { - ($($name:ident: $code:expr,)*) => { - $( - #[test] - fn $name() { - let parse_ast = crate::parser::parse_suite($code, ); - insta::assert_debug_snapshot!(parse_ast); - } - )* - } - } - - function_and_lambda! { - test_function_no_args_with_ranges: "def f(): pass", - test_function_pos_args_with_ranges: "def f(a, b, c): pass", - } - - function_and_lambda! { - test_function_no_args: "def f(): pass", - test_function_pos_args: "def f(a, b, c): pass", - test_function_pos_args_with_defaults: "def f(a, b=20, c=30): pass", - test_function_kw_only_args: "def f(*, a, b, c): pass", - test_function_kw_only_args_with_defaults: "def f(*, a, b=20, c=30): pass", - test_function_pos_and_kw_only_args: "def f(a, b, c, *, d, e, f): pass", - test_function_pos_and_kw_only_args_with_defaults: "def f(a, b, c, *, d, e=20, f=30): pass", - test_function_pos_and_kw_only_args_with_defaults_and_varargs: "def f(a, b, c, *args, d, e=20, f=30): pass", - test_function_pos_and_kw_only_args_with_defaults_and_varargs_and_kwargs: "def f(a, b, c, *args, d, e=20, f=30, **kwargs): pass", - test_lambda_no_args: "lambda: 1", - test_lambda_pos_args: "lambda a, b, c: 1", - test_lambda_pos_args_with_defaults: "lambda a, b=20, c=30: 1", - test_lambda_kw_only_args: "lambda *, a, b, c: 1", - test_lambda_kw_only_args_with_defaults: "lambda *, a, b=20, c=30: 1", - test_lambda_pos_and_kw_only_args: "lambda a, b, c, *, d, e: 0", - } - - fn function_parse_error(src: &str) -> LexicalErrorType { - let parse_ast = parse_suite(src); - parse_ast - .map_err(|e| match e.error { - ParseErrorType::Lexical(e) => e, - _ => panic!("Expected LexicalError"), - }) - .expect_err("Expected error") - } - - macro_rules! function_and_lambda_error { - ($($name:ident: $code:expr, $error:expr,)*) => { - $( - #[test] - fn $name() { - let error = function_parse_error($code); - assert_eq!(error, $error); - } - )* - } - } - - function_and_lambda_error! { - // Check definitions - test_duplicates_f1: "def f(a, a): pass", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_f2: "def f(a, *, a): pass", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_f3: "def f(a, a=20): pass", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_f4: "def f(a, *a): pass", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_f5: "def f(a, *, **a): pass", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_l1: "lambda a, a: 1", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_l2: "lambda a, *, a: 1", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_l3: "lambda a, a=20: 1", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_l4: "lambda a, *a: 1", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_duplicates_l5: "lambda a, *, **a: 1", LexicalErrorType::DuplicateArgumentError("a".to_string().into_boxed_str()), - test_default_arg_error_f: "def f(a, b=20, c): pass", LexicalErrorType::DefaultArgumentError, - test_default_arg_error_l: "lambda a, b=20, c: 1", LexicalErrorType::DefaultArgumentError, - - // Check some calls. - test_positional_arg_error_f: "f(b=20, c)", LexicalErrorType::PositionalArgumentError, - test_unpacked_arg_error_f: "f(**b, *c)", LexicalErrorType::UnpackedArgumentError, - test_duplicate_kw_f1: "f(a=20, a=30)", LexicalErrorType::DuplicateKeywordArgumentError("a".to_string().into_boxed_str()), - } -} diff --git a/crates/ruff_python_parser/src/invalid.rs b/crates/ruff_python_parser/src/invalid.rs index 909e6faf17e35..dfa6b1f0d4996 100644 --- a/crates/ruff_python_parser/src/invalid.rs +++ b/crates/ruff_python_parser/src/invalid.rs @@ -5,7 +5,9 @@ These routines are named in a way that supports qualified use. For example, `invalid::assignment_targets`. */ -use {ruff_python_ast::Expr, ruff_text_size::TextSize}; +use ruff_python_ast::Expr; + +use ruff_text_size::TextRange; use crate::lexer::{LexicalError, LexicalErrorType}; @@ -37,42 +39,44 @@ pub(crate) fn assignment_target(target: &Expr) -> Result<(), LexicalError> { #[allow(clippy::enum_glob_use)] use self::Expr::*; - let err = |location: TextSize| -> LexicalError { + let err = |location: TextRange| -> LexicalError { let error = LexicalErrorType::AssignmentError; LexicalError::new(error, location) }; match *target { - BoolOp(ref e) => Err(err(e.range.start())), - NamedExpr(ref e) => Err(err(e.range.start())), - BinOp(ref e) => Err(err(e.range.start())), - UnaryOp(ref e) => Err(err(e.range.start())), - Lambda(ref e) => Err(err(e.range.start())), - IfExp(ref e) => Err(err(e.range.start())), - Dict(ref e) => Err(err(e.range.start())), - Set(ref e) => Err(err(e.range.start())), - ListComp(ref e) => Err(err(e.range.start())), - SetComp(ref e) => Err(err(e.range.start())), - DictComp(ref e) => Err(err(e.range.start())), - GeneratorExp(ref e) => Err(err(e.range.start())), - Await(ref e) => Err(err(e.range.start())), - Yield(ref e) => Err(err(e.range.start())), - YieldFrom(ref e) => Err(err(e.range.start())), - Compare(ref e) => Err(err(e.range.start())), - Call(ref e) => Err(err(e.range.start())), + BoolOp(ref e) => Err(err(e.range)), + NamedExpr(ref e) => Err(err(e.range)), + BinOp(ref e) => Err(err(e.range)), + UnaryOp(ref e) => Err(err(e.range)), + Lambda(ref e) => Err(err(e.range)), + IfExp(ref e) => Err(err(e.range)), + Dict(ref e) => Err(err(e.range)), + Set(ref e) => Err(err(e.range)), + ListComp(ref e) => Err(err(e.range)), + SetComp(ref e) => Err(err(e.range)), + DictComp(ref e) => Err(err(e.range)), + GeneratorExp(ref e) => Err(err(e.range)), + Await(ref e) => Err(err(e.range)), + Yield(ref e) => Err(err(e.range)), + YieldFrom(ref e) => Err(err(e.range)), + Compare(ref e) => Err(err(e.range)), + Call(ref e) => Err(err(e.range)), // FString is recursive, but all its forms are invalid as an // assignment target, so we can reject it without exploring it. - FString(ref e) => Err(err(e.range.start())), - StringLiteral(ref e) => Err(err(e.range.start())), - BytesLiteral(ref e) => Err(err(e.range.start())), - NumberLiteral(ref e) => Err(err(e.range.start())), - BooleanLiteral(ref e) => Err(err(e.range.start())), - NoneLiteral(ref e) => Err(err(e.range.start())), - EllipsisLiteral(ref e) => Err(err(e.range.start())), + FString(ref e) => Err(err(e.range)), + StringLiteral(ref e) => Err(err(e.range)), + BytesLiteral(ref e) => Err(err(e.range)), + NumberLiteral(ref e) => Err(err(e.range)), + BooleanLiteral(ref e) => Err(err(e.range)), + NoneLiteral(ref e) => Err(err(e.range)), + EllipsisLiteral(ref e) => Err(err(e.range)), + #[allow(deprecated)] + Invalid(ref e) => Err(err(e.range)), // This isn't in the Python grammar but is Jupyter notebook specific. // It seems like this should be an error. It does also seem like the // parser prevents this from ever appearing as an assignment target // anyway. ---AG - IpyEscapeCommand(ref e) => Err(err(e.range.start())), + IpyEscapeCommand(ref e) => Err(err(e.range)), // The only nested expressions allowed as an assignment target // are star exprs, lists and tuples. Starred(ref e) => assignment_target(&e.value), @@ -89,611 +93,3 @@ pub(crate) fn assignment_target(target: &Expr) -> Result<(), LexicalError> { Name(_) => Ok(()), } } - -#[cfg(test)] -mod tests { - use crate::parse_suite; - - // First we test, broadly, that various kinds of assignments are now - // rejected by the parser. e.g., `5 = 3`, `5 += 3`, `(5): int = 3`. - - // Regression test: https://github.com/astral-sh/ruff/issues/6895 - #[test] - fn err_literal_assignment() { - let ast = parse_suite(r"5 = 3"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - // This test previously passed before the assignment operator checking - // above, but we include it here for good measure. - #[test] - fn err_assignment_expr() { - let ast = parse_suite(r"(5 := 3)"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: UnrecognizedToken( - ColonEqual, - None, - ), - offset: 3, - }, - ) - "###); - } - - #[test] - fn err_literal_augment_assignment() { - let ast = parse_suite(r"5 += 3"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_literal_annotation_assignment() { - let ast = parse_suite(r"(5): int = 3"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 1, - }, - ) - "###); - } - - // Now we exhaustively test all possible cases where assignment can fail. - - #[test] - fn err_bool_op() { - let ast = parse_suite(r"x or y = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_named_expr() { - let ast = parse_suite(r"(x := 5) = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 1, - }, - ) - "###); - } - - #[test] - fn err_bin_op() { - let ast = parse_suite(r"x + y = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_unary_op() { - let ast = parse_suite(r"-x = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_lambda() { - let ast = parse_suite(r"(lambda _: 1) = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 1, - }, - ) - "###); - } - - #[test] - fn err_if_exp() { - let ast = parse_suite(r"a if b else c = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_dict() { - let ast = parse_suite(r"{'a':5} = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_set() { - let ast = parse_suite(r"{a} = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_list_comp() { - let ast = parse_suite(r"[x for x in xs] = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_set_comp() { - let ast = parse_suite(r"{x for x in xs} = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_dict_comp() { - let ast = parse_suite(r"{x: x*2 for x in xs} = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_generator_exp() { - let ast = parse_suite(r"(x for x in xs) = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_await() { - let ast = parse_suite(r"await x = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_yield() { - let ast = parse_suite(r"(yield x) = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 1, - }, - ) - "###); - } - - #[test] - fn err_yield_from() { - let ast = parse_suite(r"(yield from xs) = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 1, - }, - ) - "###); - } - - #[test] - fn err_compare() { - let ast = parse_suite(r"a < b < c = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_call() { - let ast = parse_suite(r"foo() = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_formatted_value() { - // N.B. It looks like the parser can't generate a top-level - // FormattedValue, where as the official Python AST permits - // representing a single f-string containing just a variable as a - // FormattedValue directly. - // - // Bottom line is that because of this, this test is (at present) - // duplicative with the `fstring` test. That is, in theory these tests - // could fail independently, but in practice their failure or success - // is coupled. - // - // See: https://docs.python.org/3/library/ast.html#ast.FormattedValue - let ast = parse_suite(r#"f"{quux}" = 42"#); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_fstring() { - let ast = parse_suite(r#"f"{foo} and {bar}" = 42"#); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_string_literal() { - let ast = parse_suite(r#""foo" = 42"#); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_bytes_literal() { - let ast = parse_suite(r#"b"foo" = 42"#); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_number_literal() { - let ast = parse_suite(r"123 = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_boolean_literal() { - let ast = parse_suite(r"True = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_none_literal() { - let ast = parse_suite(r"None = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_ellipsis_literal() { - let ast = parse_suite(r"... = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 0, - }, - ) - "###); - } - - #[test] - fn err_starred() { - let ast = parse_suite(r"*foo() = 42"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 1, - }, - ) - "###); - } - - #[test] - fn err_list() { - let ast = parse_suite(r"[x, foo(), y] = [42, 42, 42]"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 4, - }, - ) - "###); - } - - #[test] - fn err_list_nested() { - let ast = parse_suite(r"[[a, b], [[42]], d] = [[1, 2], [[3]], 4]"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 11, - }, - ) - "###); - } - - #[test] - fn err_tuple() { - let ast = parse_suite(r"(x, foo(), y) = (42, 42, 42)"); - insta::assert_debug_snapshot!(ast, @r###" - Err( - ParseError { - error: Lexical( - AssignmentError, - ), - offset: 4, - }, - ) - "###); - } - - // This last group of tests checks that assignments we expect to be parsed - // (including some interesting ones) continue to be parsed successfully. - - #[test] - fn ok_starred() { - let ast = parse_suite(r"*foo = 42"); - insta::assert_debug_snapshot!(ast); - } - - #[test] - fn ok_list() { - let ast = parse_suite(r"[x, y, z] = [1, 2, 3]"); - insta::assert_debug_snapshot!(ast); - } - - #[test] - fn ok_tuple() { - let ast = parse_suite(r"(x, y, z) = (1, 2, 3)"); - insta::assert_debug_snapshot!(ast); - } - - #[test] - fn ok_subscript_normal() { - let ast = parse_suite(r"x[0] = 42"); - insta::assert_debug_snapshot!(ast); - } - - // This is actually a type error, not a syntax error. So check that it - // doesn't fail parsing. - #[test] - fn ok_subscript_weird() { - let ast = parse_suite(r"5[0] = 42"); - insta::assert_debug_snapshot!(ast); - } - - #[test] - fn ok_slice_normal() { - let ast = parse_suite(r"x[1:2] = [42]"); - insta::assert_debug_snapshot!(ast); - } - - // This is actually a type error, not a syntax error. So check that it - // doesn't fail parsing. - #[test] - fn ok_slice_weird() { - let ast = parse_suite(r"5[1:2] = [42]"); - insta::assert_debug_snapshot!(ast); - } - - #[test] - fn ok_attribute_normal() { - let ast = parse_suite(r"foo.bar = 42"); - insta::assert_debug_snapshot!(ast); - } - - // This is actually an attribute error, not a syntax error. So check that - // it doesn't fail parsing. - #[test] - fn ok_attribute_weird() { - let ast = parse_suite(r#""foo".y = 42"#); - insta::assert_debug_snapshot!(ast); - } - - #[test] - fn ok_name() { - let ast = parse_suite(r"foo = 42"); - insta::assert_debug_snapshot!(ast); - } - - // This is a sanity test for what looks like an ipython directive being - // assigned to. Although this doesn't actually parse as an assignment - // statement, but rather, a directive whose value is `foo = 42`. - #[test] - fn ok_ipy_escape_command() { - use crate::Mode; - - let src = r"!foo = 42"; - let tokens = crate::lexer::lex(src, Mode::Ipython); - let ast = crate::parse_tokens(tokens.collect(), src, Mode::Ipython); - insta::assert_debug_snapshot!(ast); - } - - #[test] - fn ok_assignment_expr() { - let ast = parse_suite(r"(x := 5)"); - insta::assert_debug_snapshot!(ast); - } -} diff --git a/crates/ruff_python_parser/src/lalrpop/context.rs b/crates/ruff_python_parser/src/lalrpop/context.rs new file mode 100644 index 0000000000000..e59e0237696d9 --- /dev/null +++ b/crates/ruff_python_parser/src/lalrpop/context.rs @@ -0,0 +1,54 @@ +use ruff_python_ast::{self as ast, Expr, ExprContext}; + +pub(super) fn set_context(expr: Expr, ctx: ExprContext) -> Expr { + match expr { + Expr::Name(ast::ExprName { id, range, .. }) => ast::ExprName { range, id, ctx }.into(), + Expr::Tuple(ast::ExprTuple { + elts, + range, + parenthesized, + ctx: _, + }) => ast::ExprTuple { + elts: elts.into_iter().map(|elt| set_context(elt, ctx)).collect(), + range, + ctx, + parenthesized, + } + .into(), + + Expr::List(ast::ExprList { elts, range, .. }) => ast::ExprList { + elts: elts.into_iter().map(|elt| set_context(elt, ctx)).collect(), + range, + ctx, + } + .into(), + Expr::Attribute(ast::ExprAttribute { + value, attr, range, .. + }) => ast::ExprAttribute { + range, + value, + attr, + ctx, + } + .into(), + Expr::Subscript(ast::ExprSubscript { + value, + slice, + range, + .. + }) => ast::ExprSubscript { + range, + value, + slice, + ctx, + } + .into(), + Expr::Starred(ast::ExprStarred { value, range, .. }) => ast::ExprStarred { + value: Box::new(set_context(*value, ctx)), + range, + ctx, + } + .into(), + _ => expr, + } +} diff --git a/crates/ruff_python_parser/src/lalrpop/function.rs b/crates/ruff_python_parser/src/lalrpop/function.rs new file mode 100644 index 0000000000000..b57f4ade7c8fc --- /dev/null +++ b/crates/ruff_python_parser/src/lalrpop/function.rs @@ -0,0 +1,138 @@ +use std::hash::BuildHasherDefault; +// Contains functions that perform validation and parsing of arguments and parameters. +// Checks apply both to functions and to lambdas. +use crate::lexer::{LexicalError, LexicalErrorType}; +use ruff_python_ast::{self as ast}; +use ruff_text_size::{Ranged, TextRange, TextSize}; +use rustc_hash::FxHashSet; + +pub(crate) struct ArgumentList { + pub(crate) args: Vec, + pub(crate) keywords: Vec, +} + +// Perform validation of function/lambda arguments in a function definition. +pub(super) fn validate_arguments(arguments: &ast::Parameters) -> Result<(), LexicalError> { + let mut all_arg_names = FxHashSet::with_capacity_and_hasher( + arguments.posonlyargs.len() + + arguments.args.len() + + usize::from(arguments.vararg.is_some()) + + arguments.kwonlyargs.len() + + usize::from(arguments.kwarg.is_some()), + BuildHasherDefault::default(), + ); + + let posonlyargs = arguments.posonlyargs.iter(); + let args = arguments.args.iter(); + let kwonlyargs = arguments.kwonlyargs.iter(); + + let vararg: Option<&ast::Parameter> = arguments.vararg.as_deref(); + let kwarg: Option<&ast::Parameter> = arguments.kwarg.as_deref(); + + for arg in posonlyargs + .chain(args) + .chain(kwonlyargs) + .map(|arg| &arg.parameter) + .chain(vararg) + .chain(kwarg) + { + let range = arg.range; + let arg_name = arg.name.as_str(); + if !all_arg_names.insert(arg_name) { + return Err(LexicalError::new( + LexicalErrorType::DuplicateArgumentError(arg_name.to_string().into_boxed_str()), + range, + )); + } + } + + Ok(()) +} + +pub(super) fn validate_pos_params( + args: &( + Vec, + Vec, + ), +) -> Result<(), LexicalError> { + let (posonlyargs, args) = args; + #[allow(clippy::skip_while_next)] + let first_invalid = posonlyargs + .iter() + .chain(args.iter()) // for all args + .skip_while(|arg| arg.default.is_none()) // starting with args without default + .skip_while(|arg| arg.default.is_some()) // and then args with default + .next(); // there must not be any more args without default + if let Some(invalid) = first_invalid { + return Err(LexicalError::new( + LexicalErrorType::DefaultArgumentError, + invalid.parameter.range(), + )); + } + Ok(()) +} + +type FunctionArgument = ( + Option<(TextSize, TextSize, Option)>, + ast::Expr, +); + +// Parse arguments as supplied during a function/lambda *call*. +pub(super) fn parse_arguments( + function_arguments: Vec, +) -> Result { + let mut args = vec![]; + let mut keywords = vec![]; + + let mut keyword_names = FxHashSet::with_capacity_and_hasher( + function_arguments.len(), + BuildHasherDefault::default(), + ); + let mut double_starred = false; + for (name, value) in function_arguments { + if let Some((start, end, name)) = name { + // Check for duplicate keyword arguments in the call. + if let Some(keyword_name) = &name { + if !keyword_names.insert(keyword_name.to_string()) { + return Err(LexicalError::new( + LexicalErrorType::DuplicateKeywordArgumentError( + keyword_name.to_string().into_boxed_str(), + ), + TextRange::new(start, end), + )); + } + } else { + double_starred = true; + } + + keywords.push(ast::Keyword { + arg: name, + value, + range: TextRange::new(start, end), + }); + } else { + // Positional arguments mustn't follow keyword arguments. + if !keywords.is_empty() && !is_starred(&value) { + return Err(LexicalError::new( + LexicalErrorType::PositionalArgumentError, + value.range(), + )); + // Allow starred arguments after keyword arguments but + // not after double-starred arguments. + } else if double_starred { + return Err(LexicalError::new( + LexicalErrorType::UnpackedArgumentError, + value.range(), + )); + } + + args.push(value); + } + } + Ok(ArgumentList { args, keywords }) +} + +// Check if an expression is a starred expression. +const fn is_starred(exp: &ast::Expr) -> bool { + exp.is_starred_expr() +} diff --git a/crates/ruff_python_parser/src/lalrpop/mod.rs b/crates/ruff_python_parser/src/lalrpop/mod.rs new file mode 100644 index 0000000000000..fb23ad7b8b44e --- /dev/null +++ b/crates/ruff_python_parser/src/lalrpop/mod.rs @@ -0,0 +1,311 @@ +//! The LALRPOP based parser implementation. + +use itertools::Itertools; +use lalrpop_util::ParseError as LalrpopError; + +use ruff_python_ast::{ + Expr, ExprAttribute, ExprAwait, ExprBinOp, ExprBoolOp, ExprBooleanLiteral, ExprBytesLiteral, + ExprCall, ExprCompare, ExprDict, ExprDictComp, ExprEllipsisLiteral, ExprFString, + ExprGeneratorExp, ExprIfExp, ExprIpyEscapeCommand, ExprLambda, ExprList, ExprListComp, + ExprName, ExprNamedExpr, ExprNoneLiteral, ExprNumberLiteral, ExprSet, ExprSetComp, ExprSlice, + ExprStarred, ExprStringLiteral, ExprSubscript, ExprTuple, ExprUnaryOp, ExprYield, + ExprYieldFrom, Mod, +}; +use ruff_text_size::{Ranged, TextRange, TextSize}; + +use crate::lexer::{LexResult, LexicalError, LexicalErrorType}; +use crate::{Mode, ParseError, ParseErrorType, Tok}; + +mod context; +mod function; + +#[rustfmt::skip] +#[allow(unreachable_pub)] +#[allow(clippy::type_complexity)] +#[allow(clippy::extra_unused_lifetimes)] +#[allow(clippy::needless_lifetimes)] +#[allow(clippy::unused_self)] +#[allow(clippy::cast_sign_loss)] +#[allow(clippy::default_trait_access)] +#[allow(clippy::let_unit_value)] +#[allow(clippy::just_underscores_and_digits)] +#[allow(clippy::no_effect_underscore_binding)] +#[allow(clippy::trivially_copy_pass_by_ref)] +#[allow(clippy::option_option)] +#[allow(clippy::unnecessary_wraps)] +#[allow(clippy::uninlined_format_args)] +#[allow(clippy::cloned_instead_of_copied)] +mod python { + + #[cfg(feature = "lalrpop")] + include!(concat!(env!("OUT_DIR"), "/src/lalrpop/python.rs")); + + #[cfg(not(feature = "lalrpop"))] + include!("python.rs"); +} + +pub(crate) fn parse_tokens( + tokens: Vec, + source: &str, + mode: Mode, +) -> Result { + let marker_token = (Tok::start_marker(mode), TextRange::default()); + let lexer = std::iter::once(Ok(marker_token)).chain( + tokens + .into_iter() + .filter_ok(|token| !matches!(token, (Tok::Comment(..) | Tok::NonLogicalNewline, _))), + ); + python::TopParser::new() + .parse( + source, + mode, + lexer.map_ok(|(t, range)| (range.start(), t, range.end())), + ) + .map_err(parse_error_from_lalrpop) +} + +fn parse_error_from_lalrpop(err: LalrpopError) -> ParseError { + match err { + // TODO: Are there cases where this isn't an EOF? + LalrpopError::InvalidToken { location } => ParseError { + error: ParseErrorType::Eof, + location: TextRange::empty(location), + }, + LalrpopError::ExtraToken { token } => ParseError { + error: ParseErrorType::ExtraToken(token.1), + location: TextRange::new(token.0, token.2), + }, + LalrpopError::User { error } => ParseError { + location: error.location(), + error: ParseErrorType::Lexical(error.into_error()), + }, + LalrpopError::UnrecognizedToken { token, expected } => { + // Hacky, but it's how CPython does it. See PyParser_AddToken, + // in particular "Only one possible expected token" comment. + let expected = (expected.len() == 1).then(|| expected[0].clone()); + ParseError { + error: ParseErrorType::UnrecognizedToken(token.1, expected), + location: TextRange::new(token.0, token.2), + } + } + LalrpopError::UnrecognizedEof { location, expected } => { + // This could be an initial indentation error that we should ignore + let indent_error = expected == ["Indent"]; + if indent_error { + ParseError { + error: ParseErrorType::Lexical(LexicalErrorType::IndentationError), + location: TextRange::empty(location), + } + } else { + ParseError { + error: ParseErrorType::Eof, + location: TextRange::empty(location), + } + } + } + } +} + +/// An expression that may be parenthesized. +#[derive(Clone, Debug)] +struct ParenthesizedExpr { + /// The range of the expression, including any parentheses. + range: TextRange, + /// The underlying expression. + expr: Expr, +} + +impl ParenthesizedExpr { + /// Returns `true` if the expression is parenthesized. + fn is_parenthesized(&self) -> bool { + self.range.start() != self.expr.range().start() + } +} + +impl Ranged for ParenthesizedExpr { + fn range(&self) -> TextRange { + self.range + } +} +impl From for ParenthesizedExpr { + fn from(expr: Expr) -> Self { + ParenthesizedExpr { + range: expr.range(), + expr, + } + } +} +impl From for Expr { + fn from(parenthesized_expr: ParenthesizedExpr) -> Self { + parenthesized_expr.expr + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprIpyEscapeCommand) -> Self { + Expr::IpyEscapeCommand(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprBoolOp) -> Self { + Expr::BoolOp(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprNamedExpr) -> Self { + Expr::NamedExpr(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprBinOp) -> Self { + Expr::BinOp(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprUnaryOp) -> Self { + Expr::UnaryOp(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprLambda) -> Self { + Expr::Lambda(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprIfExp) -> Self { + Expr::IfExp(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprDict) -> Self { + Expr::Dict(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprSet) -> Self { + Expr::Set(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprListComp) -> Self { + Expr::ListComp(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprSetComp) -> Self { + Expr::SetComp(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprDictComp) -> Self { + Expr::DictComp(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprGeneratorExp) -> Self { + Expr::GeneratorExp(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprAwait) -> Self { + Expr::Await(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprYield) -> Self { + Expr::Yield(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprYieldFrom) -> Self { + Expr::YieldFrom(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprCompare) -> Self { + Expr::Compare(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprCall) -> Self { + Expr::Call(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprFString) -> Self { + Expr::FString(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprStringLiteral) -> Self { + Expr::StringLiteral(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprBytesLiteral) -> Self { + Expr::BytesLiteral(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprNumberLiteral) -> Self { + Expr::NumberLiteral(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprBooleanLiteral) -> Self { + Expr::BooleanLiteral(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprNoneLiteral) -> Self { + Expr::NoneLiteral(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprEllipsisLiteral) -> Self { + Expr::EllipsisLiteral(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprAttribute) -> Self { + Expr::Attribute(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprSubscript) -> Self { + Expr::Subscript(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprStarred) -> Self { + Expr::Starred(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprName) -> Self { + Expr::Name(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprList) -> Self { + Expr::List(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprTuple) -> Self { + Expr::Tuple(payload).into() + } +} +impl From for ParenthesizedExpr { + fn from(payload: ExprSlice) -> Self { + Expr::Slice(payload).into() + } +} + +#[cfg(target_pointer_width = "64")] +mod size_assertions { + use static_assertions::assert_eq_size; + + use super::ParenthesizedExpr; + + assert_eq_size!(ParenthesizedExpr, [u8; 72]); +} diff --git a/crates/ruff_python_parser/src/python.lalrpop b/crates/ruff_python_parser/src/lalrpop/python.lalrpop similarity index 95% rename from crates/ruff_python_parser/src/python.lalrpop rename to crates/ruff_python_parser/src/lalrpop/python.lalrpop index 34d049a0176c8..be5ca9ef8e2d7 100644 --- a/crates/ruff_python_parser/src/python.lalrpop +++ b/crates/ruff_python_parser/src/lalrpop/python.lalrpop @@ -5,12 +5,15 @@ use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; +use super::{ + function::{ArgumentList, parse_arguments, validate_pos_params, validate_arguments}, + context::set_context, + ParenthesizedExpr +}; use crate::{ FStringErrorType, Mode, lexer::{LexicalError, LexicalErrorType}, - function::{ArgumentList, parse_arguments, validate_pos_params, validate_arguments}, - context::set_context, string::{StringType, concatenated_strings, parse_fstring_literal_element, parse_string_literal}, token::{self, StringKind}, invalid, @@ -156,33 +159,33 @@ ExpressionStatement: ast::Stmt = { }, }; -AssignSuffix: crate::parser::ParenthesizedExpr = { +AssignSuffix: ParenthesizedExpr = { "=" => e, "=" => e }; -TestListOrYieldExpr: crate::parser::ParenthesizedExpr = { +TestListOrYieldExpr: ParenthesizedExpr = { TestList, YieldExpr } #[inline] -TestOrStarExprList: crate::parser::ParenthesizedExpr = { +TestOrStarExprList: ParenthesizedExpr = { // as far as I can tell, these were the same TestList }; -TestOrStarExpr: crate::parser::ParenthesizedExpr = { +TestOrStarExpr: ParenthesizedExpr = { Test<"all">, StarExpr, }; -NamedOrStarExpr: crate::parser::ParenthesizedExpr = { +NamedOrStarExpr: ParenthesizedExpr = { NamedExpression, StarExpr, }; -TestOrStarNamedExpr: crate::parser::ParenthesizedExpr = { +TestOrStarNamedExpr: ParenthesizedExpr = { NamedExpressionTest, StarExpr, }; @@ -339,20 +342,20 @@ IpyEscapeCommandStatement: ast::Stmt = { } else { Err(LexicalError::new( LexicalErrorType::OtherError("IPython escape commands are only allowed in `Mode::Ipython`".to_string().into_boxed_str()), - location, + (location..end_location).into(), ))? } } } -IpyEscapeCommandExpr: crate::parser::ParenthesizedExpr = { +IpyEscapeCommandExpr: ParenthesizedExpr = { =>? { if mode == Mode::Ipython { // This should never occur as the lexer won't allow it. if !matches!(c.0, IpyEscapeKind::Magic | IpyEscapeKind::Shell) { return Err(LexicalError::new( LexicalErrorType::OtherError("IPython escape command expr is only allowed for % and !".to_string().into_boxed_str()), - location, + (location..end_location).into(), ))?; } Ok(ast::ExprIpyEscapeCommand { @@ -363,7 +366,7 @@ IpyEscapeCommandExpr: crate::parser::ParenthesizedExpr = { } else { Err(LexicalError::new( LexicalErrorType::OtherError("IPython escape commands are only allowed in `Mode::Ipython`".to_string().into_boxed_str()), - location, + (location..end_location).into(), ))? } } @@ -383,7 +386,7 @@ IpyHelpEndEscapeCommandStatement: ast::Stmt = { let ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value: ast::Number::Int(integer), .. }) = slice.as_ref() else { return Err(LexicalError::new( LexicalErrorType::OtherError("only integer literals are allowed in Subscript expressions in help end escape command".to_string().into_boxed_str()), - range.start(), + *range, )); }; unparse_expr(value, buffer)?; @@ -399,7 +402,7 @@ IpyHelpEndEscapeCommandStatement: ast::Stmt = { _ => { return Err(LexicalError::new( LexicalErrorType::OtherError("only Name, Subscript and Attribute expressions are allowed in help end escape command".to_string().into_boxed_str()), - expr.start(), + expr.range(), )); } } @@ -410,7 +413,7 @@ IpyHelpEndEscapeCommandStatement: ast::Stmt = { return Err(ParseError::User { error: LexicalError::new( LexicalErrorType::OtherError("IPython escape commands are only allowed in `Mode::Ipython`".to_string().into_boxed_str()), - location, + (location..end_location).into(), ), }); } @@ -422,7 +425,7 @@ IpyHelpEndEscapeCommandStatement: ast::Stmt = { return Err(ParseError::User { error: LexicalError::new( LexicalErrorType::OtherError("maximum of 2 `?` tokens are allowed in help end escape command".to_string().into_boxed_str()), - location, + (location..end_location).into(), ), }); } @@ -483,7 +486,8 @@ MatchStatement: ast::Stmt = { ast::ExprTuple { elts: vec![subject.into()], ctx: ast::ExprContext::Load, - range: (tuple_location..tuple_end_location).into() + range: (tuple_location..tuple_end_location).into(), + parenthesized: false }, )), cases, @@ -506,7 +510,8 @@ MatchStatement: ast::Stmt = { ast::ExprTuple { elts, ctx: ast::ExprContext::Load, - range: (tuple_location..tuple_end_location).into() + range: (tuple_location..tuple_end_location).into(), + parenthesized: false }, )), cases, @@ -563,7 +568,7 @@ AsPattern: ast::Pattern = { if name.as_str() == "_" { Err(LexicalError::new( LexicalErrorType::OtherError("cannot use '_' as a target".to_string().into_boxed_str()), - location, + (location..end_location).into(), ))? } else { Ok(ast::Pattern::MatchAs( @@ -630,13 +635,13 @@ StarPattern: ast::Pattern = { }.into(), } -NumberAtom: crate::parser::ParenthesizedExpr = { +NumberAtom: ParenthesizedExpr = { => ast::Expr::NumberLiteral( ast::ExprNumberLiteral { value, range: (location..end_location).into() } ).into(), } -NumberExpr: crate::parser::ParenthesizedExpr = { +NumberExpr: ParenthesizedExpr = { NumberAtom, "-" => ast::Expr::UnaryOp( ast::ExprUnaryOp { @@ -647,7 +652,7 @@ NumberExpr: crate::parser::ParenthesizedExpr = { ).into(), } -AddOpExpr: crate::parser::ParenthesizedExpr = { +AddOpExpr: ParenthesizedExpr = { => ast::ExprBinOp { left: Box::new(left.into()), op, @@ -1249,7 +1254,7 @@ ParameterListStarArgs if va.is_none() && kwonlyargs.is_empty() && kwarg.is_none() { return Err(LexicalError::new( LexicalErrorType::OtherError("named arguments must follow bare *".to_string().into_boxed_str()), - location, + (location..location).into(), ))?; } @@ -1316,7 +1321,7 @@ Decorator: ast::Decorator = { }, }; -YieldExpr: crate::parser::ParenthesizedExpr = { +YieldExpr: ParenthesizedExpr = { "yield" => ast::ExprYield { value: value.map(ast::Expr::from).map(Box::new), range: (location..end_location).into(), @@ -1327,7 +1332,7 @@ YieldExpr: crate::parser::ParenthesizedExpr = { }.into(), }; -Test: crate::parser::ParenthesizedExpr = { +Test: ParenthesizedExpr = { > "if" > "else" > => ast::ExprIfExp { test: Box::new(test.into()), body: Box::new(body.into()), @@ -1338,12 +1343,12 @@ Test: crate::parser::ParenthesizedExpr = { LambdaDef, }; -NamedExpressionTest: crate::parser::ParenthesizedExpr = { +NamedExpressionTest: ParenthesizedExpr = { NamedExpression, Test<"all">, } -NamedExpressionName: crate::parser::ParenthesizedExpr = { +NamedExpressionName: ParenthesizedExpr = { => ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, @@ -1351,7 +1356,7 @@ NamedExpressionName: crate::parser::ParenthesizedExpr = { }.into(), } -NamedExpression: crate::parser::ParenthesizedExpr = { +NamedExpression: ParenthesizedExpr = { ":=" > => { ast::ExprNamedExpr { target: Box::new(target.into()), @@ -1361,12 +1366,12 @@ NamedExpression: crate::parser::ParenthesizedExpr = { }, }; -LambdaDef: crate::parser::ParenthesizedExpr = { +LambdaDef: ParenthesizedExpr = { "lambda" ?> ":" > =>? { if fstring_middle.is_some() { return Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::LambdaWithoutParentheses), - location, + (location..end_location).into(), ))?; } parameters.as_ref().map(validate_arguments).transpose()?; @@ -1379,7 +1384,7 @@ LambdaDef: crate::parser::ParenthesizedExpr = { } } -OrTest: crate::parser::ParenthesizedExpr = { +OrTest: ParenthesizedExpr = { > "or")+> > => { let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect(); ast::ExprBoolOp { op: ast::BoolOp::Or, values, range: (location..end_location).into() }.into() @@ -1387,7 +1392,7 @@ OrTest: crate::parser::ParenthesizedExpr = { AndTest, }; -AndTest: crate::parser::ParenthesizedExpr = { +AndTest: ParenthesizedExpr = { > "and")+> > => { let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect(); ast::ExprBoolOp { op: ast::BoolOp::And, values, range: (location..end_location).into() }.into() @@ -1395,7 +1400,7 @@ AndTest: crate::parser::ParenthesizedExpr = { NotTest, }; -NotTest: crate::parser::ParenthesizedExpr = { +NotTest: ParenthesizedExpr = { "not" > => ast::ExprUnaryOp { operand: Box::new(operand.into()), op: ast::UnaryOp::Not, @@ -1404,7 +1409,7 @@ NotTest: crate::parser::ParenthesizedExpr = { Comparison, }; -Comparison: crate::parser::ParenthesizedExpr = { +Comparison: ParenthesizedExpr = { > )+> => { let mut ops = Vec::with_capacity(comparisons.len()); let mut comparators = Vec::with_capacity(comparisons.len()); @@ -1435,7 +1440,7 @@ CompOp: ast::CmpOp = { "is" "not" => ast::CmpOp::IsNot, }; -Expression: crate::parser::ParenthesizedExpr = { +Expression: ParenthesizedExpr = { > "|" > => ast::ExprBinOp { left: Box::new(left.into()), op: ast::Operator::BitOr, @@ -1445,7 +1450,7 @@ Expression: crate::parser::ParenthesizedExpr = { XorExpression, }; -XorExpression: crate::parser::ParenthesizedExpr = { +XorExpression: ParenthesizedExpr = { > "^" > => ast::ExprBinOp { left: Box::new(left.into()), op: ast::Operator::BitXor, @@ -1455,7 +1460,7 @@ XorExpression: crate::parser::ParenthesizedExpr = { AndExpression, }; -AndExpression: crate::parser::ParenthesizedExpr = { +AndExpression: ParenthesizedExpr = { > "&" > => ast::ExprBinOp { left: Box::new(left.into()), op: ast::Operator::BitAnd, @@ -1465,7 +1470,7 @@ AndExpression: crate::parser::ParenthesizedExpr = { ShiftExpression, }; -ShiftExpression: crate::parser::ParenthesizedExpr = { +ShiftExpression: ParenthesizedExpr = { > > => ast::ExprBinOp { left: Box::new(left.into()), op, @@ -1480,7 +1485,7 @@ ShiftOp: ast::Operator = { ">>" => ast::Operator::RShift, }; -ArithmeticExpression: crate::parser::ParenthesizedExpr = { +ArithmeticExpression: ParenthesizedExpr = { > > => ast::ExprBinOp { left: Box::new(left.into()), op, @@ -1495,7 +1500,7 @@ AddOp: ast::Operator = { "-" => ast::Operator::Sub, }; -Term: crate::parser::ParenthesizedExpr = { +Term: ParenthesizedExpr = { > > => ast::ExprBinOp { left: Box::new(left.into()), op, @@ -1513,7 +1518,7 @@ MulOp: ast::Operator = { "@" => ast::Operator::MatMult, }; -Factor: crate::parser::ParenthesizedExpr = { +Factor: ParenthesizedExpr = { > => ast::ExprUnaryOp { operand: Box::new(operand.into()), op, @@ -1528,7 +1533,7 @@ UnaryOp: ast::UnaryOp = { "~" => ast::UnaryOp::Invert, }; -Power: crate::parser::ParenthesizedExpr = { +Power: ParenthesizedExpr = { > "**" > => ast::ExprBinOp { left: Box::new(left.into()), op: ast::Operator::Pow, @@ -1538,14 +1543,14 @@ Power: crate::parser::ParenthesizedExpr = { AtomExpr, }; -AtomExpr: crate::parser::ParenthesizedExpr = { +AtomExpr: ParenthesizedExpr = { "await" > => { ast::ExprAwait { value: Box::new(value.into()), range: (location..end_location).into() }.into() }, AtomExpr2, } -AtomExpr2: crate::parser::ParenthesizedExpr = { +AtomExpr2: ParenthesizedExpr = { Atom, > => ast::ExprCall { func: Box::new(func.into()), @@ -1566,13 +1571,14 @@ AtomExpr2: crate::parser::ParenthesizedExpr = { }.into(), }; -SubscriptList: crate::parser::ParenthesizedExpr = { +SubscriptList: ParenthesizedExpr = { Subscript, "," => { ast::ExprTuple { elts: vec![s1.into()], ctx: ast::ExprContext::Load, range: (location..end_location).into(), + parenthesized: false, }.into() }, > ","? => { @@ -1581,11 +1587,12 @@ SubscriptList: crate::parser::ParenthesizedExpr = { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), + parenthesized: false, }.into() } }; -Subscript: crate::parser::ParenthesizedExpr = { +Subscript: ParenthesizedExpr = { TestOrStarNamedExpr, ?> ":" ?> => { let lower = lower.map(ast::Expr::from).map(Box::new); @@ -1597,7 +1604,7 @@ Subscript: crate::parser::ParenthesizedExpr = { } }; -SliceOp: Option = { +SliceOp: Option = { ":" ?> => e, } @@ -1642,7 +1649,7 @@ FStringReplacementField: ast::FStringElement = { if value.expr.is_lambda_expr() && !value.is_parenthesized() { return Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::LambdaWithoutParentheses), - value.start(), + value.range(), ))?; } let debug_text = debug.map(|_| { @@ -1693,14 +1700,14 @@ FStringConversion: (TextSize, ast::ConversionFlag) = { "a" => ast::ConversionFlag::Ascii, _ => Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::InvalidConversionFlag), - name_location, + (location..name_location).into(), ))? }; Ok((location, conversion)) } }; -Atom: crate::parser::ParenthesizedExpr = { +Atom: ParenthesizedExpr = { => expr.into(), => ast::ExprNumberLiteral { value, @@ -1720,13 +1727,13 @@ Atom: crate::parser::ParenthesizedExpr = { }, "(" >> ")" if Goal != "no-withitems" => { if elts.len() == 1 && trailing_comma.is_none() { - crate::parser::ParenthesizedExpr { + ParenthesizedExpr { expr: elts.into_iter().next().unwrap().into(), range: (location..end_location).into(), } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), parenthesized: true }.into() } }, "(" >> ",")?> )*> ")" =>? { @@ -1734,24 +1741,25 @@ Atom: crate::parser::ParenthesizedExpr = { if mid.expr.is_starred_expr() { return Err(LexicalError::new( LexicalErrorType::OtherError("cannot use starred expression here".to_string().into_boxed_str()), - mid.start(), + mid.range(), ))?; } - Ok(crate::parser::ParenthesizedExpr { + Ok(ParenthesizedExpr { expr: mid.into(), range: (location..end_location).into(), }) } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect(); - Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()) + Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), parenthesized: true }.into()) } }, "(" ")" => ast::ExprTuple { elts: Vec::new(), ctx: ast::ExprContext::Load, range: (location..end_location).into(), + parenthesized: true, }.into(), - "(" ")" => crate::parser::ParenthesizedExpr { + "(" ")" => ParenthesizedExpr { expr: e.into(), range: (location..end_location).into(), }, @@ -1763,7 +1771,7 @@ Atom: crate::parser::ParenthesizedExpr = { "(" "**" > ")" =>? { Err(LexicalError::new( LexicalErrorType::OtherError("cannot use double starred expression here".to_string().into_boxed_str()), - location, + (location..end_location).into(), ).into()) }, "{" "}" => { @@ -1800,37 +1808,37 @@ Atom: crate::parser::ParenthesizedExpr = { "..." => ast::ExprEllipsisLiteral { range: (location..end_location).into() }.into(), }; -ListLiteralValues: Vec = { +ListLiteralValues: Vec = { > ","? => e, }; -DictLiteralValues: Vec<(Option>, crate::parser::ParenthesizedExpr)> = { +DictLiteralValues: Vec<(Option>, ParenthesizedExpr)> = { > ","? => elements, }; -DictEntry: (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr) = { +DictEntry: (ParenthesizedExpr, ParenthesizedExpr) = { > ":" > => (e1, e2), }; -DictElement: (Option>, crate::parser::ParenthesizedExpr) = { +DictElement: (Option>, ParenthesizedExpr) = { => (Some(Box::new(e.0)), e.1), "**" > => (None, e), }; -SetLiteralValues: Vec = { +SetLiteralValues: Vec = { > ","? => e1 }; -ExpressionOrStarExpression: crate::parser::ParenthesizedExpr = { +ExpressionOrStarExpression: ParenthesizedExpr = { Expression<"all">, StarExpr }; -ExpressionList: crate::parser::ParenthesizedExpr = { +ExpressionList: ParenthesizedExpr = { GenericList }; -ExpressionList2: Vec = { +ExpressionList2: Vec = { > ","? => elements, }; @@ -1839,26 +1847,26 @@ ExpressionList2: Vec = { // - a single expression // - a single expression followed by a trailing comma #[inline] -TestList: crate::parser::ParenthesizedExpr = { +TestList: ParenthesizedExpr = { GenericList }; -GenericList: crate::parser::ParenthesizedExpr = { +GenericList: ParenthesizedExpr = { > => { if elts.len() == 1 && trailing_comma.is_none() { - crate::parser::ParenthesizedExpr { + ParenthesizedExpr { expr: elts.into_iter().next().unwrap().into(), range: (location..end_location).into(), } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), parenthesized: false }.into() } } } // Test -StarExpr: crate::parser::ParenthesizedExpr = { +StarExpr: ParenthesizedExpr = { "*" > => ast::ExprStarred { value: Box::new(value.into()), ctx: ast::ExprContext::Load, @@ -1883,8 +1891,8 @@ SingleForComprehension: ast::Comprehension = { } }; -ExpressionNoCond: crate::parser::ParenthesizedExpr = OrTest<"all">; -ComprehensionIf: crate::parser::ParenthesizedExpr = "if" => c; +ExpressionNoCond: ParenthesizedExpr = OrTest<"all">; +ComprehensionIf: ParenthesizedExpr = "if" => c; Arguments: ast::Arguments = { "(" > ")" =>? { diff --git a/crates/ruff_python_parser/src/python.rs b/crates/ruff_python_parser/src/lalrpop/python.rs similarity index 96% rename from crates/ruff_python_parser/src/python.rs rename to crates/ruff_python_parser/src/lalrpop/python.rs index 646fcfdeb6891..456d2106abb6b 100644 --- a/crates/ruff_python_parser/src/python.rs +++ b/crates/ruff_python_parser/src/lalrpop/python.rs @@ -1,13 +1,16 @@ // auto-generated: "lalrpop 0.20.0" -// sha3: 8c85e4bbac54760ed8be03b56a428d76e14d18e6dbde62b424d0b2b5e8e65dbe +// sha3: f073219201ea9e4b419b238c2a2decb70ba7fa52960d3e68dfd973e43302260c use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; +use super::{ + function::{ArgumentList, parse_arguments, validate_pos_params, validate_arguments}, + context::set_context, + ParenthesizedExpr +}; use crate::{ FStringErrorType, Mode, lexer::{LexicalError, LexicalErrorType}, - function::{ArgumentList, parse_arguments, validate_pos_params, validate_arguments}, - context::set_context, string::{StringType, concatenated_strings, parse_fstring_literal_element, parse_string_literal}, token::{self, StringKind}, invalid, @@ -26,12 +29,15 @@ mod __parse__Top { use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; + use super::super::{ + function::{ArgumentList, parse_arguments, validate_pos_params, validate_arguments}, + context::set_context, + ParenthesizedExpr +}; use crate::{ FStringErrorType, Mode, lexer::{LexicalError, LexicalErrorType}, - function::{ArgumentList, parse_arguments, validate_pos_params, validate_arguments}, - context::set_context, string::{StringType, concatenated_strings, parse_fstring_literal_element, parse_string_literal}, token::{self, StringKind}, invalid, @@ -62,9 +68,9 @@ mod __parse__Top { Variant12(alloc::vec::Vec), Variant13((Option>, Vec, Option>)), Variant14(core::option::Option<(Option>, Vec, Option>)>), - Variant15(crate::parser::ParenthesizedExpr), - Variant16(core::option::Option), - Variant17(alloc::vec::Vec), + Variant15(ParenthesizedExpr), + Variant16(core::option::Option), + Variant17(alloc::vec::Vec), Variant18(ast::WithItem), Variant19(alloc::vec::Vec), Variant20((token::Tok, ast::Identifier)), @@ -74,23 +80,23 @@ mod __parse__Top { Variant24(core::option::Option), Variant25(ast::Suite), Variant26(core::option::Option), - Variant27((TextSize, crate::parser::ParenthesizedExpr, ast::Suite)), - Variant28(alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>), + Variant27((TextSize, ParenthesizedExpr, ast::Suite)), + Variant28(alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>), Variant29((TextSize, ast::Suite)), Variant30(core::option::Option<(TextSize, ast::Suite)>), Variant31((Option<(TextSize, TextSize, Option)>, ast::Expr)), Variant32(alloc::vec::Vec<(Option<(TextSize, TextSize, Option)>, ast::Expr)>), - Variant33(Vec), - Variant34(core::option::Option>), + Variant33(Vec), + Variant34(core::option::Option>), Variant35(ast::Pattern), Variant36(alloc::vec::Vec), Variant37(ast::Stmt), Variant38(alloc::vec::Vec), - Variant39((crate::parser::ParenthesizedExpr, ast::Identifier)), + Variant39((ParenthesizedExpr, ast::Identifier)), Variant40(Vec), Variant41(core::option::Option>), - Variant42((ast::CmpOp, crate::parser::ParenthesizedExpr)), - Variant43(alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>), + Variant42((ast::CmpOp, ParenthesizedExpr)), + Variant43(alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>), Variant44(ast::Expr), Variant45(core::option::Option), Variant46(ast::Parameters), @@ -106,10 +112,10 @@ mod __parse__Top { Variant56(ast::CmpOp), Variant57(ast::Decorator), Variant58(alloc::vec::Vec), - Variant59((Option>, crate::parser::ParenthesizedExpr)), - Variant60((crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr)), - Variant61(Vec<(Option>, crate::parser::ParenthesizedExpr)>), - Variant62(core::option::Option>, crate::parser::ParenthesizedExpr)>>), + Variant59((Option>, ParenthesizedExpr)), + Variant60((ParenthesizedExpr, ParenthesizedExpr)), + Variant61(Vec<(Option>, ParenthesizedExpr)>), + Variant62(core::option::Option>, ParenthesizedExpr)>>), Variant63(ast::Parameter), Variant64(core::option::Option), Variant65(ast::ExceptHandler), @@ -142,8 +148,8 @@ mod __parse__Top { Variant92(ast::PatternArguments), Variant93(ast::Comprehension), Variant94(alloc::vec::Vec), - Variant95(Option), - Variant96(core::option::Option>), + Variant95(Option), + Variant96(core::option::Option>), Variant97(Vec), Variant98(ast::Mod), Variant99(Vec), @@ -18363,23 +18369,23 @@ mod __parse__Top { _ => __symbol_type_mismatch() } } - fn __pop_Variant13< + fn __pop_Variant59< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (Option>, Vec, Option>), TextSize) + ) -> (TextSize, (Option>, ParenthesizedExpr), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant13(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant59(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant59< + fn __pop_Variant13< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (Option>, crate::parser::ParenthesizedExpr), TextSize) + ) -> (TextSize, (Option>, Vec, Option>), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant59(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant13(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } @@ -18393,83 +18399,83 @@ mod __parse__Top { _ => __symbol_type_mismatch() } } - fn __pop_Variant67< + fn __pop_Variant60< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (TextSize, ast::ConversionFlag), TextSize) + ) -> (TextSize, (ParenthesizedExpr, ParenthesizedExpr), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant67(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant60(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant29< + fn __pop_Variant39< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (TextSize, ast::Suite), TextSize) + ) -> (TextSize, (ParenthesizedExpr, ast::Identifier), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant29(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant39(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } fn __pop_Variant27< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (TextSize, crate::parser::ParenthesizedExpr, ast::Suite), TextSize) + ) -> (TextSize, (TextSize, ParenthesizedExpr, ast::Suite), TextSize) { match __symbols.pop() { Some((__l, __Symbol::Variant27(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant90< + fn __pop_Variant67< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (Vec, Vec), TextSize) + ) -> (TextSize, (TextSize, ast::ConversionFlag), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant90(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant67(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant42< + fn __pop_Variant29< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (ast::CmpOp, crate::parser::ParenthesizedExpr), TextSize) + ) -> (TextSize, (TextSize, ast::Suite), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant42(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant29(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant83< + fn __pop_Variant90< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (ast::Expr, ast::Pattern), TextSize) + ) -> (TextSize, (Vec, Vec), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant83(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant90(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant39< + fn __pop_Variant42< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (crate::parser::ParenthesizedExpr, ast::Identifier), TextSize) + ) -> (TextSize, (ast::CmpOp, ParenthesizedExpr), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant39(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant42(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant60< + fn __pop_Variant83< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr), TextSize) + ) -> (TextSize, (ast::Expr, ast::Pattern), TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant60(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant83(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } @@ -18526,13 +18532,23 @@ mod __parse__Top { fn __pop_Variant95< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, Option, TextSize) + ) -> (TextSize, Option, TextSize) { match __symbols.pop() { Some((__l, __Symbol::Variant95(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } + fn __pop_Variant15< + >( + __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> + ) -> (TextSize, ParenthesizedExpr, TextSize) + { + match __symbols.pop() { + Some((__l, __Symbol::Variant15(__v), __r)) => (__l, __v, __r), + _ => __symbol_type_mismatch() + } + } fn __pop_Variant69< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> @@ -18566,7 +18582,7 @@ mod __parse__Top { fn __pop_Variant61< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, Vec<(Option>, crate::parser::ParenthesizedExpr)>, TextSize) + ) -> (TextSize, Vec<(Option>, ParenthesizedExpr)>, TextSize) { match __symbols.pop() { Some((__l, __Symbol::Variant61(__v), __r)) => (__l, __v, __r), @@ -18583,6 +18599,16 @@ mod __parse__Top { _ => __symbol_type_mismatch() } } + fn __pop_Variant33< + >( + __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> + ) -> (TextSize, Vec, TextSize) + { + match __symbols.pop() { + Some((__l, __Symbol::Variant33(__v), __r)) => (__l, __v, __r), + _ => __symbol_type_mismatch() + } + } fn __pop_Variant99< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> @@ -18683,16 +18709,6 @@ mod __parse__Top { _ => __symbol_type_mismatch() } } - fn __pop_Variant33< - >( - __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, Vec, TextSize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant33(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } fn __pop_Variant32< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> @@ -18706,7 +18722,7 @@ mod __parse__Top { fn __pop_Variant28< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize) + ) -> (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize) { match __symbols.pop() { Some((__l, __Symbol::Variant28(__v), __r)) => (__l, __v, __r), @@ -18716,7 +18732,7 @@ mod __parse__Top { fn __pop_Variant43< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize) + ) -> (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize) { match __symbols.pop() { Some((__l, __Symbol::Variant43(__v), __r)) => (__l, __v, __r), @@ -18733,6 +18749,16 @@ mod __parse__Top { _ => __symbol_type_mismatch() } } + fn __pop_Variant17< + >( + __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> + ) -> (TextSize, alloc::vec::Vec, TextSize) + { + match __symbols.pop() { + Some((__l, __Symbol::Variant17(__v), __r)) => (__l, __v, __r), + _ => __symbol_type_mismatch() + } + } fn __pop_Variant94< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> @@ -18823,16 +18849,6 @@ mod __parse__Top { _ => __symbol_type_mismatch() } } - fn __pop_Variant17< - >( - __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, alloc::vec::Vec, TextSize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant17(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } fn __pop_Variant22< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> @@ -19176,50 +19192,60 @@ mod __parse__Top { fn __pop_Variant96< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, core::option::Option>, TextSize) + ) -> (TextSize, core::option::Option>, TextSize) { match __symbols.pop() { Some((__l, __Symbol::Variant96(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } + fn __pop_Variant16< + >( + __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> + ) -> (TextSize, core::option::Option, TextSize) + { + match __symbols.pop() { + Some((__l, __Symbol::Variant16(__v), __r)) => (__l, __v, __r), + _ => __symbol_type_mismatch() + } + } fn __pop_Variant62< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, core::option::Option>, crate::parser::ParenthesizedExpr)>>, TextSize) + ) -> (TextSize, core::option::Option>, ParenthesizedExpr)>>, TextSize) { match __symbols.pop() { Some((__l, __Symbol::Variant62(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant55< + fn __pop_Variant34< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, core::option::Option>, TextSize) + ) -> (TextSize, core::option::Option>, TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant55(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant34(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant41< + fn __pop_Variant55< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, core::option::Option>, TextSize) + ) -> (TextSize, core::option::Option>, TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant41(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant55(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } - fn __pop_Variant34< + fn __pop_Variant41< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, core::option::Option>, TextSize) + ) -> (TextSize, core::option::Option>, TextSize) { match __symbols.pop() { - Some((__l, __Symbol::Variant34(__v), __r)) => (__l, __v, __r), + Some((__l, __Symbol::Variant41(__v), __r)) => (__l, __v, __r), _ => __symbol_type_mismatch() } } @@ -19313,16 +19339,6 @@ mod __parse__Top { _ => __symbol_type_mismatch() } } - fn __pop_Variant16< - >( - __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, core::option::Option, TextSize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant16(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } fn __pop_Variant8< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> @@ -19333,16 +19349,6 @@ mod __parse__Top { _ => __symbol_type_mismatch() } } - fn __pop_Variant15< - >( - __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> - ) -> (TextSize, crate::parser::ParenthesizedExpr, TextSize) - { - match __symbols.pop() { - Some((__l, __Symbol::Variant15(__v), __r)) => (__l, __v, __r), - _ => __symbol_type_mismatch() - } - } fn __pop_Variant2< >( __symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)> @@ -32551,7 +32557,7 @@ fn __action2< mode: Mode, (_, start, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, body, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, body, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, alloc::vec::Vec, TextSize), (_, end, _): (TextSize, TextSize, TextSize), ) -> ast::Mod @@ -32876,7 +32882,7 @@ fn __action25< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, targets, _): (TextSize, Vec, TextSize), + (_, targets, _): (TextSize, Vec, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -32894,8 +32900,8 @@ fn __action26< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, expression, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, suffix, _): (TextSize, alloc::vec::Vec, TextSize), + (_, expression, _): (TextSize, ParenthesizedExpr, TextSize), + (_, suffix, _): (TextSize, alloc::vec::Vec, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> Result> { @@ -32929,9 +32935,9 @@ fn __action27< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, target, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, target, _): (TextSize, ParenthesizedExpr, TextSize), (_, op, _): (TextSize, ast::Operator, TextSize), - (_, rhs, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, rhs, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> Result> { @@ -32955,10 +32961,10 @@ fn __action28< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, target, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, target, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, annotation, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, rhs, _): (TextSize, core::option::Option, TextSize), + (_, annotation, _): (TextSize, ParenthesizedExpr, TextSize), + (_, rhs, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> Result> { @@ -32984,8 +32990,8 @@ fn __action29< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { e } @@ -32997,8 +33003,8 @@ fn __action30< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { e } @@ -33009,8 +33015,8 @@ fn __action31< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33021,8 +33027,8 @@ fn __action32< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33033,8 +33039,8 @@ fn __action33< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33045,8 +33051,8 @@ fn __action34< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33057,8 +33063,8 @@ fn __action35< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33069,8 +33075,8 @@ fn __action36< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33081,8 +33087,8 @@ fn __action37< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33093,8 +33099,8 @@ fn __action38< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33105,8 +33111,8 @@ fn __action39< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -33308,7 +33314,7 @@ fn __action55< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, core::option::Option, TextSize), + (_, value, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -33326,7 +33332,7 @@ fn __action56< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, expression, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, expression, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -33375,8 +33381,8 @@ fn __action59< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, exc, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, cause, _): (TextSize, core::option::Option, TextSize), + (_, exc, _): (TextSize, ParenthesizedExpr, TextSize), + (_, cause, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -33616,8 +33622,8 @@ fn __action73< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, test, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, msg, _): (TextSize, core::option::Option, TextSize), + (_, test, _): (TextSize, ParenthesizedExpr, TextSize), + (_, msg, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -33655,7 +33661,7 @@ fn __action74< } else { Err(LexicalError::new( LexicalErrorType::OtherError("IPython escape commands are only allowed in `Mode::Ipython`".to_string().into_boxed_str()), - location, + (location..end_location).into(), ))? } } @@ -33670,7 +33676,7 @@ fn __action75< (_, location, _): (TextSize, TextSize, TextSize), (_, c, _): (TextSize, (IpyEscapeKind, Box), TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { { if mode == Mode::Ipython { @@ -33678,7 +33684,7 @@ fn __action75< if !matches!(c.0, IpyEscapeKind::Magic | IpyEscapeKind::Shell) { return Err(LexicalError::new( LexicalErrorType::OtherError("IPython escape command expr is only allowed for % and !".to_string().into_boxed_str()), - location, + (location..end_location).into(), ))?; } Ok(ast::ExprIpyEscapeCommand { @@ -33689,7 +33695,7 @@ fn __action75< } else { Err(LexicalError::new( LexicalErrorType::OtherError("IPython escape commands are only allowed in `Mode::Ipython`".to_string().into_boxed_str()), - location, + (location..end_location).into(), ))? } } @@ -33702,7 +33708,7 @@ fn __action76< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), (_, suffix, _): (TextSize, alloc::vec::Vec, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> Result> @@ -33717,7 +33723,7 @@ fn __action76< let ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value: ast::Number::Int(integer), .. }) = slice.as_ref() else { return Err(LexicalError::new( LexicalErrorType::OtherError("only integer literals are allowed in Subscript expressions in help end escape command".to_string().into_boxed_str()), - range.start(), + *range, )); }; unparse_expr(value, buffer)?; @@ -33733,7 +33739,7 @@ fn __action76< _ => { return Err(LexicalError::new( LexicalErrorType::OtherError("only Name, Subscript and Attribute expressions are allowed in help end escape command".to_string().into_boxed_str()), - expr.start(), + expr.range(), )); } } @@ -33744,7 +33750,7 @@ fn __action76< return Err(ParseError::User { error: LexicalError::new( LexicalErrorType::OtherError("IPython escape commands are only allowed in `Mode::Ipython`".to_string().into_boxed_str()), - location, + (location..end_location).into(), ), }); } @@ -33756,7 +33762,7 @@ fn __action76< return Err(ParseError::User { error: LexicalError::new( LexicalErrorType::OtherError("maximum of 2 `?` tokens are allowed in help end escape command".to_string().into_boxed_str()), - location, + (location..end_location).into(), ), }); } @@ -33879,7 +33885,7 @@ fn __action85< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, subject, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, subject, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), @@ -33914,7 +33920,7 @@ fn __action86< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, tuple_location, _): (TextSize, TextSize, TextSize), - (_, subject, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, subject, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, tuple_end_location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), @@ -33938,7 +33944,8 @@ fn __action86< ast::ExprTuple { elts: vec![subject.into()], ctx: ast::ExprContext::Load, - range: (tuple_location..tuple_end_location).into() + range: (tuple_location..tuple_end_location).into(), + parenthesized: false }, )), cases, @@ -33957,7 +33964,7 @@ fn __action87< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, tuple_location, _): (TextSize, TextSize, TextSize), - (_, elts, _): (TextSize, Vec, TextSize), + (_, elts, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, core::option::Option, TextSize), (_, tuple_end_location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), @@ -33982,7 +33989,8 @@ fn __action87< ast::ExprTuple { elts, ctx: ast::ExprContext::Load, - range: (tuple_location..tuple_end_location).into() + range: (tuple_location..tuple_end_location).into(), + parenthesized: false }, )), cases, @@ -34025,7 +34033,7 @@ fn __action89< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, guard, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, guard, _): (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Expr { { @@ -34128,7 +34136,7 @@ fn __action95< if name.as_str() == "_" { Err(LexicalError::new( LexicalErrorType::OtherError("cannot use '_' as a target".to_string().into_boxed_str()), - location, + (location..end_location).into(), ))? } else { Ok(ast::Pattern::MatchAs( @@ -34383,7 +34391,7 @@ fn __action111< (_, location, _): (TextSize, TextSize, TextSize), (_, value, _): (TextSize, ast::Number, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::Expr::NumberLiteral( ast::ExprNumberLiteral { value, range: (location..end_location).into() } @@ -34396,8 +34404,8 @@ fn __action112< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -34410,9 +34418,9 @@ fn __action113< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, operand, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, operand, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::Expr::UnaryOp( ast::ExprUnaryOp { @@ -34430,11 +34438,11 @@ fn __action114< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, op, _): (TextSize, ast::Operator, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -34502,7 +34510,7 @@ fn __action118< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Pattern { @@ -34519,7 +34527,7 @@ fn __action119< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Pattern { @@ -34686,7 +34694,7 @@ fn __action129< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Expr { e.into() @@ -34698,7 +34706,7 @@ fn __action130< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Expr { e.into() @@ -35034,10 +35042,10 @@ fn __action146< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, test, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, test, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, body, _): (TextSize, ast::Suite, TextSize), - (_, s2, _): (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize), + (_, s2, _): (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize), (_, s3, _): (TextSize, core::option::Option<(TextSize, ast::Suite)>, TextSize), ) -> ast::Stmt { @@ -35070,7 +35078,7 @@ fn __action147< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, test, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, test, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, body, _): (TextSize, ast::Suite, TextSize), (_, s2, _): (TextSize, core::option::Option, TextSize), @@ -35103,9 +35111,9 @@ fn __action148< (_, location, _): (TextSize, TextSize, TextSize), (_, is_async, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, target, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, target, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, iter, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, iter, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, body, _): (TextSize, ast::Suite, TextSize), (_, orelse, _): (TextSize, core::option::Option, TextSize), @@ -35239,7 +35247,7 @@ fn __action152< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, typ, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, typ, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, body, _): (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -35266,7 +35274,7 @@ fn __action153< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, x, _): (TextSize, (crate::parser::ParenthesizedExpr, ast::Identifier), TextSize), + (_, x, _): (TextSize, (ParenthesizedExpr, ast::Identifier), TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, body, _): (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -35292,7 +35300,7 @@ fn __action154< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, typ, _): (TextSize, core::option::Option, TextSize), + (_, typ, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, body, _): (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -35318,7 +35326,7 @@ fn __action155< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, x, _): (TextSize, (crate::parser::ParenthesizedExpr, ast::Identifier), TextSize), + (_, x, _): (TextSize, (ParenthesizedExpr, ast::Identifier), TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, body, _): (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -35441,7 +35449,7 @@ fn __action161< >( source_code: &str, mode: Mode, - (_, all, _): (TextSize, Vec, TextSize), + (_, all, _): (TextSize, Vec, TextSize), ) -> Vec { { @@ -35460,9 +35468,9 @@ fn __action162< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, context_expr, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, context_expr, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, optional_vars, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, optional_vars, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::WithItem { @@ -35489,7 +35497,7 @@ fn __action163< (_, name, _): (TextSize, ast::Identifier, TextSize), (_, type_params, _): (TextSize, core::option::Option, TextSize), (_, parameters, _): (TextSize, ast::Parameters, TextSize), - (_, returns, _): (TextSize, core::option::Option, TextSize), + (_, returns, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, body, _): (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -35540,7 +35548,7 @@ fn __action165< (_, name, _): (TextSize, ast::Expr, TextSize), (_, type_params, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -35622,7 +35630,7 @@ fn __action169< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, name, _): (TextSize, ast::Identifier, TextSize), - (_, annotation, _): (TextSize, core::option::Option, TextSize), + (_, annotation, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::ParameterWithDefault { @@ -35641,7 +35649,7 @@ fn __action170< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, name, _): (TextSize, ast::Identifier, TextSize), - (_, annotation, _): (TextSize, core::option::Option, TextSize), + (_, annotation, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Parameter { @@ -35659,7 +35667,7 @@ fn __action171< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, name, _): (TextSize, ast::Identifier, TextSize), - (_, annotation, _): (TextSize, core::option::Option, TextSize), + (_, annotation, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Parameter { @@ -35730,7 +35738,7 @@ fn __action174< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, name, _): (TextSize, ast::Identifier, TextSize), - (_, bound, _): (TextSize, core::option::Option, TextSize), + (_, bound, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::TypeParam { @@ -35787,7 +35795,7 @@ fn __action177< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, expression, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, expression, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), ) -> ast::Decorator @@ -35805,9 +35813,9 @@ fn __action178< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, core::option::Option, TextSize), + (_, value, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprYield { value: value.map(ast::Expr::from).map(Box::new), @@ -35824,9 +35832,9 @@ fn __action179< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprYieldFrom { value: Box::new(value.into()), @@ -35840,8 +35848,8 @@ fn __action180< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -35852,8 +35860,8 @@ fn __action181< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -35867,7 +35875,7 @@ fn __action182< (_, location, _): (TextSize, TextSize, TextSize), (_, id, _): (TextSize, ast::Identifier, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprName { id: id.into(), @@ -35883,11 +35891,11 @@ fn __action183< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, target, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, target, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { ast::ExprNamedExpr { @@ -35911,15 +35919,15 @@ fn __action184< (_, end_location_args, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, fstring_middle, _): (TextSize, core::option::Option<(Box, bool, bool)>, TextSize), - (_, body, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, body, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { { if fstring_middle.is_some() { return Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::LambdaWithoutParentheses), - location, + (location..end_location).into(), ))?; } parameters.as_ref().map(validate_arguments).transpose()?; @@ -36204,8 +36212,8 @@ fn __action207< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -36217,16 +36225,17 @@ fn __action208< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, s1, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, s1, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { ast::ExprTuple { elts: vec![s1.into()], ctx: ast::ExprContext::Load, range: (location..end_location).into(), + parenthesized: false, }.into() } } @@ -36238,10 +36247,10 @@ fn __action209< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, elts, _): (TextSize, Vec, TextSize), + (_, elts, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let elts = elts.into_iter().map(ast::Expr::from).collect(); @@ -36249,6 +36258,7 @@ fn __action209< elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), + parenthesized: false, }.into() } } @@ -36259,8 +36269,8 @@ fn __action210< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -36272,12 +36282,12 @@ fn __action211< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, lower, _): (TextSize, core::option::Option, TextSize), + (_, lower, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, upper, _): (TextSize, core::option::Option, TextSize), - (_, step, _): (TextSize, core::option::Option>, TextSize), + (_, upper, _): (TextSize, core::option::Option, TextSize), + (_, step, _): (TextSize, core::option::Option>, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let lower = lower.map(ast::Expr::from).map(Box::new); @@ -36297,8 +36307,8 @@ fn __action212< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, core::option::Option, TextSize), -) -> Option + (_, e, _): (TextSize, core::option::Option, TextSize), +) -> Option { e } @@ -36431,7 +36441,7 @@ fn __action221< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, debug, _): (TextSize, core::option::Option, TextSize), (_, conversion, _): (TextSize, core::option::Option<(TextSize, ast::ConversionFlag)>, TextSize), (_, format_spec, _): (TextSize, core::option::Option, TextSize), @@ -36443,7 +36453,7 @@ fn __action221< if value.expr.is_lambda_expr() && !value.is_parenthesized() { return Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::LambdaWithoutParentheses), - value.start(), + value.range(), ))?; } let debug_text = debug.map(|_| { @@ -36524,7 +36534,7 @@ fn __action224< "a" => ast::ConversionFlag::Ascii, _ => Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::InvalidConversionFlag), - name_location, + (location..name_location).into(), ))? }; Ok((location, conversion)) @@ -36537,9 +36547,9 @@ fn __action225< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, Vec, TextSize), + (_, e, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, core::option::Option, TextSize), -) -> Vec +) -> Vec { e } @@ -36550,9 +36560,9 @@ fn __action226< >( source_code: &str, mode: Mode, - (_, elements, _): (TextSize, Vec<(Option>, crate::parser::ParenthesizedExpr)>, TextSize), + (_, elements, _): (TextSize, Vec<(Option>, ParenthesizedExpr)>, TextSize), (_, _, _): (TextSize, core::option::Option, TextSize), -) -> Vec<(Option>, crate::parser::ParenthesizedExpr)> +) -> Vec<(Option>, ParenthesizedExpr)> { elements } @@ -36563,10 +36573,10 @@ fn __action227< >( source_code: &str, mode: Mode, - (_, e1, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e1, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e2, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr) + (_, e2, _): (TextSize, ParenthesizedExpr, TextSize), +) -> (ParenthesizedExpr, ParenthesizedExpr) { (e1, e2) } @@ -36577,8 +36587,8 @@ fn __action228< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr), TextSize), -) -> (Option>, crate::parser::ParenthesizedExpr) + (_, e, _): (TextSize, (ParenthesizedExpr, ParenthesizedExpr), TextSize), +) -> (Option>, ParenthesizedExpr) { (Some(Box::new(e.0)), e.1) } @@ -36590,8 +36600,8 @@ fn __action229< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> (Option>, crate::parser::ParenthesizedExpr) + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> (Option>, ParenthesizedExpr) { (None, e) } @@ -36602,9 +36612,9 @@ fn __action230< >( source_code: &str, mode: Mode, - (_, e1, _): (TextSize, Vec, TextSize), + (_, e1, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, core::option::Option, TextSize), -) -> Vec +) -> Vec { e1 } @@ -36615,8 +36625,8 @@ fn __action231< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -36627,8 +36637,8 @@ fn __action232< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -36639,8 +36649,8 @@ fn __action233< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -36651,9 +36661,9 @@ fn __action234< >( source_code: &str, mode: Mode, - (_, elements, _): (TextSize, Vec, TextSize), + (_, elements, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, core::option::Option, TextSize), -) -> Vec +) -> Vec { elements } @@ -36664,8 +36674,8 @@ fn __action235< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -36678,9 +36688,9 @@ fn __action236< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprStarred { value: Box::new(value.into()), @@ -36710,10 +36720,10 @@ fn __action238< (_, location, _): (TextSize, TextSize, TextSize), (_, is_async, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, target, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, target, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, iter, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, ifs, _): (TextSize, alloc::vec::Vec, TextSize), + (_, iter, _): (TextSize, ParenthesizedExpr, TextSize), + (_, ifs, _): (TextSize, alloc::vec::Vec, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Comprehension { @@ -36736,8 +36746,8 @@ fn __action239< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -36749,8 +36759,8 @@ fn __action240< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, c, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, c, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { c } @@ -36785,7 +36795,7 @@ fn __action242< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, elt, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, elt, _): (TextSize, ParenthesizedExpr, TextSize), (_, generators, _): (TextSize, core::option::Option>, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) @@ -36814,7 +36824,7 @@ fn __action243< (_, location, _): (TextSize, TextSize, TextSize), (_, i, _): (TextSize, ast::Identifier, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { @@ -36829,7 +36839,7 @@ fn __action244< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { @@ -36849,7 +36859,7 @@ fn __action245< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { @@ -36957,7 +36967,7 @@ fn __action253< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> alloc::vec::Vec +) -> alloc::vec::Vec { alloc::vec![] } @@ -36968,8 +36978,8 @@ fn __action254< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec, TextSize), -) -> alloc::vec::Vec + (_, v, _): (TextSize, alloc::vec::Vec, TextSize), +) -> alloc::vec::Vec { v } @@ -36981,10 +36991,10 @@ fn __action255< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, values, _): (TextSize, alloc::vec::Vec, TextSize), - (_, last, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, values, _): (TextSize, alloc::vec::Vec, TextSize), + (_, last, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect(); @@ -36998,8 +37008,8 @@ fn __action256< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -37036,20 +37046,20 @@ fn __action259< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, elts, _): (TextSize, Vec, TextSize), + (_, elts, _): (TextSize, Vec, TextSize), (_, trailing_comma, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { if elts.len() == 1 && trailing_comma.is_none() { - crate::parser::ParenthesizedExpr { + ParenthesizedExpr { expr: elts.into_iter().next().unwrap().into(), range: (location..end_location).into(), } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), parenthesized: false }.into() } } } @@ -37060,8 +37070,8 @@ fn __action260< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { vec![e] } @@ -37072,10 +37082,10 @@ fn __action261< >( source_code: &str, mode: Mode, - (_, mut v, _): (TextSize, Vec, TextSize), + (_, mut v, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { { v.push(e); @@ -37090,20 +37100,20 @@ fn __action262< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, elts, _): (TextSize, Vec, TextSize), + (_, elts, _): (TextSize, Vec, TextSize), (_, trailing_comma, _): (TextSize, core::option::Option, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { if elts.len() == 1 && trailing_comma.is_none() { - crate::parser::ParenthesizedExpr { + ParenthesizedExpr { expr: elts.into_iter().next().unwrap().into(), range: (location..end_location).into(), } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), parenthesized: false }.into() } } } @@ -37114,8 +37124,8 @@ fn __action263< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, (Option>, crate::parser::ParenthesizedExpr), TextSize), -) -> Vec<(Option>, crate::parser::ParenthesizedExpr)> + (_, e, _): (TextSize, (Option>, ParenthesizedExpr), TextSize), +) -> Vec<(Option>, ParenthesizedExpr)> { vec![e] } @@ -37126,10 +37136,10 @@ fn __action264< >( source_code: &str, mode: Mode, - (_, mut v, _): (TextSize, Vec<(Option>, crate::parser::ParenthesizedExpr)>, TextSize), + (_, mut v, _): (TextSize, Vec<(Option>, ParenthesizedExpr)>, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, (Option>, crate::parser::ParenthesizedExpr), TextSize), -) -> Vec<(Option>, crate::parser::ParenthesizedExpr)> + (_, e, _): (TextSize, (Option>, ParenthesizedExpr), TextSize), +) -> Vec<(Option>, ParenthesizedExpr)> { { v.push(e); @@ -37143,8 +37153,8 @@ fn __action265< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { vec![e] } @@ -37155,10 +37165,10 @@ fn __action266< >( source_code: &str, mode: Mode, - (_, mut v, _): (TextSize, Vec, TextSize), + (_, mut v, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { { v.push(e); @@ -37301,8 +37311,8 @@ fn __action277< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, Option, TextSize), -) -> core::option::Option> + (_, __0, _): (TextSize, Option, TextSize), +) -> core::option::Option> { Some(__0) } @@ -37315,7 +37325,7 @@ fn __action278< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option> +) -> core::option::Option> { None } @@ -37326,10 +37336,10 @@ fn __action279< >( source_code: &str, mode: Mode, - (_, e1, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e1, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e2, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e2, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { vec![e1, e2] } @@ -37340,10 +37350,10 @@ fn __action280< >( source_code: &str, mode: Mode, - (_, mut v, _): (TextSize, Vec, TextSize), + (_, mut v, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { { v.push(e); @@ -37574,8 +37584,8 @@ fn __action293< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { Some(__0) } @@ -37588,7 +37598,7 @@ fn __action294< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option +) -> core::option::Option { None } @@ -37600,8 +37610,8 @@ fn __action295< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -37612,8 +37622,8 @@ fn __action296< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { Some(__0) } @@ -37626,7 +37636,7 @@ fn __action297< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option +) -> core::option::Option { None } @@ -37638,8 +37648,8 @@ fn __action298< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -37800,8 +37810,8 @@ fn __action306< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { Some(__0) } @@ -37814,7 +37824,7 @@ fn __action307< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option +) -> core::option::Option { None } @@ -37826,8 +37836,8 @@ fn __action308< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -37888,8 +37898,8 @@ fn __action313< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { vec![e] } @@ -37900,10 +37910,10 @@ fn __action314< >( source_code: &str, mode: Mode, - (_, mut v, _): (TextSize, Vec, TextSize), + (_, mut v, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { { v.push(e); @@ -37942,7 +37952,7 @@ fn __action317< >( source_code: &str, mode: Mode, - (_, context_expr, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, context_expr, _): (TextSize, ParenthesizedExpr, TextSize), ) -> ast::WithItem { { @@ -38010,7 +38020,7 @@ fn __action322< >( source_code: &str, mode: Mode, - (_, context_expr, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, context_expr, _): (TextSize, ParenthesizedExpr, TextSize), ) -> ast::WithItem { { @@ -38078,8 +38088,8 @@ fn __action327< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { Some(__0) } @@ -38092,7 +38102,7 @@ fn __action328< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option +) -> core::option::Option { None } @@ -38103,10 +38113,10 @@ fn __action329< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, __1, _): (TextSize, ast::Identifier, TextSize), -) -> (crate::parser::ParenthesizedExpr, ast::Identifier) +) -> (ParenthesizedExpr, ast::Identifier) { (__0, __1) } @@ -38312,7 +38322,7 @@ fn __action345< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)> +) -> alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)> { alloc::vec![] } @@ -38323,8 +38333,8 @@ fn __action346< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize), -) -> alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)> + (_, v, _): (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize), +) -> alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)> { v } @@ -38337,10 +38347,10 @@ fn __action347< mode: Mode, (_, __0, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, __1, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, __1, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, __2, _): (TextSize, ast::Suite, TextSize), -) -> (TextSize, crate::parser::ParenthesizedExpr, ast::Suite) +) -> (TextSize, ParenthesizedExpr, ast::Suite) { (__0, __1, __2) } @@ -38622,10 +38632,10 @@ fn __action367< >( source_code: &str, mode: Mode, - (_, e1, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e1, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e2, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e2, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { vec![e1, e2] } @@ -38636,10 +38646,10 @@ fn __action368< >( source_code: &str, mode: Mode, - (_, mut v, _): (TextSize, Vec, TextSize), + (_, mut v, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { { v.push(e); @@ -38716,11 +38726,11 @@ fn __action374< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -38736,8 +38746,8 @@ fn __action375< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -38748,8 +38758,8 @@ fn __action376< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { Some(__0) } @@ -38762,7 +38772,7 @@ fn __action377< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option +) -> core::option::Option { None } @@ -38774,8 +38784,8 @@ fn __action378< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -39016,8 +39026,8 @@ fn __action396< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { Some(__0) } @@ -39030,7 +39040,7 @@ fn __action397< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option +) -> core::option::Option { None } @@ -39042,8 +39052,8 @@ fn __action398< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -39054,8 +39064,8 @@ fn __action399< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { Some(__0) } @@ -39068,7 +39078,7 @@ fn __action400< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option +) -> core::option::Option { None } @@ -39079,8 +39089,8 @@ fn __action401< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { Some(__0) } @@ -39093,7 +39103,7 @@ fn __action402< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option +) -> core::option::Option { None } @@ -39105,13 +39115,13 @@ fn __action403< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, body, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, body, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, test, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, test, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, orelse, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, orelse, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprIfExp { test: Box::new(test.into()), @@ -39127,8 +39137,8 @@ fn __action404< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -39139,8 +39149,8 @@ fn __action405< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -39153,7 +39163,7 @@ fn __action406< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> alloc::vec::Vec +) -> alloc::vec::Vec { alloc::vec![] } @@ -39164,8 +39174,8 @@ fn __action407< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec, TextSize), -) -> alloc::vec::Vec + (_, v, _): (TextSize, alloc::vec::Vec, TextSize), +) -> alloc::vec::Vec { v } @@ -39350,8 +39360,8 @@ fn __action422< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { alloc::vec![__0] } @@ -39362,9 +39372,9 @@ fn __action423< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, v, _): (TextSize, alloc::vec::Vec, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { { let mut v = v; v.push(e); v } } @@ -39414,11 +39424,11 @@ fn __action427< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -39434,8 +39444,8 @@ fn __action428< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -39496,8 +39506,8 @@ fn __action433< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, (TextSize, crate::parser::ParenthesizedExpr, ast::Suite), TextSize), -) -> alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)> + (_, __0, _): (TextSize, (TextSize, ParenthesizedExpr, ast::Suite), TextSize), +) -> alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)> { alloc::vec![__0] } @@ -39508,9 +39518,9 @@ fn __action434< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize), - (_, e, _): (TextSize, (TextSize, crate::parser::ParenthesizedExpr, ast::Suite), TextSize), -) -> alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)> + (_, v, _): (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize), + (_, e, _): (TextSize, (TextSize, ParenthesizedExpr, ast::Suite), TextSize), +) -> alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)> { { let mut v = v; v.push(e); v } } @@ -39522,13 +39532,13 @@ fn __action435< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, body, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, body, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, test, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, test, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, orelse, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, orelse, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprIfExp { test: Box::new(test.into()), @@ -39544,8 +39554,8 @@ fn __action436< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -39556,8 +39566,8 @@ fn __action437< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -39670,7 +39680,7 @@ fn __action445< if va.is_none() && kwonlyargs.is_empty() && kwarg.is_none() { return Err(LexicalError::new( LexicalErrorType::OtherError("named arguments must follow bare *".to_string().into_boxed_str()), - location, + (location..location).into(), ))?; } @@ -39795,7 +39805,7 @@ fn __action453< if va.is_none() && kwonlyargs.is_empty() && kwarg.is_none() { return Err(LexicalError::new( LexicalErrorType::OtherError("named arguments must follow bare *".to_string().into_boxed_str()), - location, + (location..location).into(), ))?; } @@ -39868,8 +39878,8 @@ fn __action458< >( source_code: &str, mode: Mode, - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { vec![e] } @@ -39880,10 +39890,10 @@ fn __action459< >( source_code: &str, mode: Mode, - (_, mut v, _): (TextSize, Vec, TextSize), + (_, mut v, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Vec + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> Vec { { v.push(e); @@ -39897,8 +39907,8 @@ fn __action460< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { alloc::vec![__0] } @@ -39909,9 +39919,9 @@ fn __action461< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, v, _): (TextSize, alloc::vec::Vec, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { { let mut v = v; v.push(e); v } } @@ -39922,9 +39932,9 @@ fn __action462< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { __0 } @@ -39936,10 +39946,10 @@ fn __action463< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, values, _): (TextSize, alloc::vec::Vec, TextSize), - (_, last, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, values, _): (TextSize, alloc::vec::Vec, TextSize), + (_, last, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect(); @@ -39953,8 +39963,8 @@ fn __action464< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -39965,8 +39975,8 @@ fn __action465< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { alloc::vec![__0] } @@ -39977,9 +39987,9 @@ fn __action466< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, v, _): (TextSize, alloc::vec::Vec, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { { let mut v = v; v.push(e); v } } @@ -40078,8 +40088,8 @@ fn __action474< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { alloc::vec![__0] } @@ -40090,9 +40100,9 @@ fn __action475< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, v, _): (TextSize, alloc::vec::Vec, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { { let mut v = v; v.push(e); v } } @@ -40103,9 +40113,9 @@ fn __action476< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { __0 } @@ -40118,9 +40128,9 @@ fn __action477< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, operand, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, operand, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprUnaryOp { operand: Box::new(operand.into()), @@ -40135,8 +40145,8 @@ fn __action478< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40253,7 +40263,7 @@ fn __action487< mode: Mode, (_, mut i, _): (TextSize, ast::ParameterWithDefault, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, default, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, default, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::ParameterWithDefault { @@ -40401,7 +40411,7 @@ fn __action498< mode: Mode, (_, mut i, _): (TextSize, ast::ParameterWithDefault, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, default, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, default, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::ParameterWithDefault { @@ -40469,10 +40479,10 @@ fn __action503< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, values, _): (TextSize, alloc::vec::Vec, TextSize), - (_, last, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, values, _): (TextSize, alloc::vec::Vec, TextSize), + (_, last, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect(); @@ -40486,8 +40496,8 @@ fn __action504< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40499,11 +40509,11 @@ fn __action505< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -40519,8 +40529,8 @@ fn __action506< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40532,11 +40542,11 @@ fn __action507< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, op, _): (TextSize, ast::Operator, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -40552,8 +40562,8 @@ fn __action508< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40565,10 +40575,10 @@ fn __action509< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, values, _): (TextSize, alloc::vec::Vec, TextSize), - (_, last, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, values, _): (TextSize, alloc::vec::Vec, TextSize), + (_, last, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let values = values.into_iter().chain(std::iter::once(last)).map(ast::Expr::from).collect(); @@ -40582,8 +40592,8 @@ fn __action510< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40645,10 +40655,10 @@ fn __action515< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, comparisons, _): (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), + (_, comparisons, _): (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let mut ops = Vec::with_capacity(comparisons.len()); @@ -40672,8 +40682,8 @@ fn __action516< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40684,8 +40694,8 @@ fn __action517< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, (ast::CmpOp, crate::parser::ParenthesizedExpr), TextSize), -) -> alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)> + (_, __0, _): (TextSize, (ast::CmpOp, ParenthesizedExpr), TextSize), +) -> alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)> { alloc::vec![__0] } @@ -40696,9 +40706,9 @@ fn __action518< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize), - (_, e, _): (TextSize, (ast::CmpOp, crate::parser::ParenthesizedExpr), TextSize), -) -> alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)> + (_, v, _): (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize), + (_, e, _): (TextSize, (ast::CmpOp, ParenthesizedExpr), TextSize), +) -> alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)> { { let mut v = v; v.push(e); v } } @@ -40710,8 +40720,8 @@ fn __action519< source_code: &str, mode: Mode, (_, __0, _): (TextSize, ast::CmpOp, TextSize), - (_, __1, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> (ast::CmpOp, crate::parser::ParenthesizedExpr) + (_, __1, _): (TextSize, ParenthesizedExpr, TextSize), +) -> (ast::CmpOp, ParenthesizedExpr) { (__0, __1) } @@ -40724,9 +40734,9 @@ fn __action520< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, operand, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, operand, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprUnaryOp { operand: Box::new(operand.into()), @@ -40741,8 +40751,8 @@ fn __action521< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40754,11 +40764,11 @@ fn __action522< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, op, _): (TextSize, ast::Operator, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -40774,8 +40784,8 @@ fn __action523< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40787,11 +40797,11 @@ fn __action524< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, op, _): (TextSize, ast::Operator, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -40807,8 +40817,8 @@ fn __action525< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40820,10 +40830,10 @@ fn __action526< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, comparisons, _): (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), + (_, comparisons, _): (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let mut ops = Vec::with_capacity(comparisons.len()); @@ -40847,8 +40857,8 @@ fn __action527< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40860,11 +40870,11 @@ fn __action528< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -40880,8 +40890,8 @@ fn __action529< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40894,9 +40904,9 @@ fn __action530< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, op, _): (TextSize, ast::UnaryOp, TextSize), - (_, operand, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, operand, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprUnaryOp { operand: Box::new(operand.into()), @@ -40911,8 +40921,8 @@ fn __action531< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40924,11 +40934,11 @@ fn __action532< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -40944,8 +40954,8 @@ fn __action533< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40957,11 +40967,11 @@ fn __action534< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -40977,8 +40987,8 @@ fn __action535< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -40990,11 +41000,11 @@ fn __action536< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -41010,8 +41020,8 @@ fn __action537< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41024,9 +41034,9 @@ fn __action538< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { ast::ExprAwait { value: Box::new(value.into()), range: (location..end_location).into() }.into() @@ -41039,8 +41049,8 @@ fn __action539< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41051,8 +41061,8 @@ fn __action540< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41064,10 +41074,10 @@ fn __action541< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, func, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, func, _): (TextSize, ParenthesizedExpr, TextSize), (_, arguments, _): (TextSize, ast::Arguments, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprCall { func: Box::new(func.into()), @@ -41083,12 +41093,12 @@ fn __action542< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, slice, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, slice, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprSubscript { value: Box::new(value.into()), @@ -41105,11 +41115,11 @@ fn __action543< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, attr, _): (TextSize, ast::Identifier, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprAttribute { value: Box::new(value.into()), @@ -41126,11 +41136,11 @@ fn __action544< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, op, _): (TextSize, ast::Operator, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -41146,8 +41156,8 @@ fn __action545< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41159,11 +41169,11 @@ fn __action546< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, op, _): (TextSize, ast::Operator, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -41179,8 +41189,8 @@ fn __action547< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41192,7 +41202,7 @@ fn __action548< source_code: &str, mode: Mode, (_, expr, _): (TextSize, ast::Expr, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { expr.into() } @@ -41206,7 +41216,7 @@ fn __action549< (_, location, _): (TextSize, TextSize, TextSize), (_, value, _): (TextSize, ast::Number, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprNumberLiteral { value, @@ -41223,7 +41233,7 @@ fn __action550< (_, location, _): (TextSize, TextSize, TextSize), (_, id, _): (TextSize, ast::Identifier, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprName { id: id.into(), @@ -41240,10 +41250,10 @@ fn __action551< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elts, _): (TextSize, core::option::Option>, TextSize), + (_, elts, _): (TextSize, core::option::Option>, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let elts = elts.into_iter().flatten().map(ast::Expr::from).collect(); @@ -41259,11 +41269,11 @@ fn __action552< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elt, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, elt, _): (TextSize, ParenthesizedExpr, TextSize), (_, generators, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { ast::ExprListComp { elt: Box::new(elt.into()), generators, range: (location..end_location).into() }.into() @@ -41278,21 +41288,21 @@ fn __action553< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elts, _): (TextSize, Vec, TextSize), + (_, elts, _): (TextSize, Vec, TextSize), (_, trailing_comma, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { if elts.len() == 1 && trailing_comma.is_none() { - crate::parser::ParenthesizedExpr { + ParenthesizedExpr { expr: elts.into_iter().next().unwrap().into(), range: (location..end_location).into(), } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), parenthesized: true }.into() } } } @@ -41305,29 +41315,29 @@ fn __action554< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, left, _): (TextSize, core::option::Option>, TextSize), - (_, mid, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, right, _): (TextSize, alloc::vec::Vec, TextSize), + (_, left, _): (TextSize, core::option::Option>, TextSize), + (_, mid, _): (TextSize, ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, alloc::vec::Vec, TextSize), (_, trailing_comma, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { { if left.is_none() && right.is_empty() && trailing_comma.is_none() { if mid.expr.is_starred_expr() { return Err(LexicalError::new( LexicalErrorType::OtherError("cannot use starred expression here".to_string().into_boxed_str()), - mid.start(), + mid.range(), ))?; } - Ok(crate::parser::ParenthesizedExpr { + Ok(ParenthesizedExpr { expr: mid.into(), range: (location..end_location).into(), }) } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect(); - Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()) + Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), parenthesized: true }.into()) } } } @@ -41342,12 +41352,13 @@ fn __action555< (_, _, _): (TextSize, token::Tok, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprTuple { elts: Vec::new(), ctx: ast::ExprContext::Load, range: (location..end_location).into(), + parenthesized: true, }.into() } @@ -41359,12 +41370,12 @@ fn __action556< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { - crate::parser::ParenthesizedExpr { + ParenthesizedExpr { expr: e.into(), range: (location..end_location).into(), } @@ -41378,11 +41389,11 @@ fn __action557< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elt, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, elt, _): (TextSize, ParenthesizedExpr, TextSize), (_, generators, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprGeneratorExp { elt: Box::new(elt.into()), @@ -41400,15 +41411,15 @@ fn __action558< (_, _, _): (TextSize, token::Tok, TextSize), (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { { Err(LexicalError::new( LexicalErrorType::OtherError("cannot use double starred expression here".to_string().into_boxed_str()), - location, + (location..end_location).into(), ).into()) } } @@ -41421,10 +41432,10 @@ fn __action559< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, core::option::Option>, crate::parser::ParenthesizedExpr)>>, TextSize), + (_, e, _): (TextSize, core::option::Option>, ParenthesizedExpr)>>, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let (keys, values) = e @@ -41444,11 +41455,11 @@ fn __action560< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e1, _): (TextSize, (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr), TextSize), + (_, e1, _): (TextSize, (ParenthesizedExpr, ParenthesizedExpr), TextSize), (_, generators, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { ast::ExprDictComp { @@ -41468,10 +41479,10 @@ fn __action561< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elts, _): (TextSize, Vec, TextSize), + (_, elts, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let elts = elts.into_iter().map(ast::Expr::from).collect(); @@ -41490,11 +41501,11 @@ fn __action562< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elt, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, elt, _): (TextSize, ParenthesizedExpr, TextSize), (_, generators, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprSetComp { elt: Box::new(elt.into()), @@ -41512,7 +41523,7 @@ fn __action563< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBooleanLiteral { value: true, range: (location..end_location).into() }.into() } @@ -41526,7 +41537,7 @@ fn __action564< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBooleanLiteral { value: false, range: (location..end_location).into() }.into() } @@ -41540,7 +41551,7 @@ fn __action565< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprNoneLiteral { range: (location..end_location).into() }.into() } @@ -41554,7 +41565,7 @@ fn __action566< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprEllipsisLiteral { range: (location..end_location).into() }.into() } @@ -41565,8 +41576,8 @@ fn __action567< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, Vec<(Option>, crate::parser::ParenthesizedExpr)>, TextSize), -) -> core::option::Option>, crate::parser::ParenthesizedExpr)>> + (_, __0, _): (TextSize, Vec<(Option>, ParenthesizedExpr)>, TextSize), +) -> core::option::Option>, ParenthesizedExpr)>> { Some(__0) } @@ -41579,7 +41590,7 @@ fn __action568< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option>, crate::parser::ParenthesizedExpr)>> +) -> core::option::Option>, ParenthesizedExpr)>> { None } @@ -41592,7 +41603,7 @@ fn __action569< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> alloc::vec::Vec +) -> alloc::vec::Vec { alloc::vec![] } @@ -41603,8 +41614,8 @@ fn __action570< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec, TextSize), -) -> alloc::vec::Vec + (_, v, _): (TextSize, alloc::vec::Vec, TextSize), +) -> alloc::vec::Vec { v } @@ -41616,8 +41627,8 @@ fn __action571< source_code: &str, mode: Mode, (_, _, _): (TextSize, token::Tok, TextSize), - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41628,8 +41639,8 @@ fn __action572< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, Vec, TextSize), -) -> core::option::Option> + (_, __0, _): (TextSize, Vec, TextSize), +) -> core::option::Option> { Some(__0) } @@ -41642,7 +41653,7 @@ fn __action573< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option> +) -> core::option::Option> { None } @@ -41653,9 +41664,9 @@ fn __action574< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, Vec, TextSize), + (_, __0, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), -) -> Vec +) -> Vec { __0 } @@ -41666,8 +41677,8 @@ fn __action575< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, Vec, TextSize), -) -> core::option::Option> + (_, __0, _): (TextSize, Vec, TextSize), +) -> core::option::Option> { Some(__0) } @@ -41680,7 +41691,7 @@ fn __action576< mode: Mode, __lookbehind: &TextSize, __lookahead: &TextSize, -) -> core::option::Option> +) -> core::option::Option> { None } @@ -41692,11 +41703,11 @@ fn __action577< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, op, _): (TextSize, ast::Operator, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -41712,8 +41723,8 @@ fn __action578< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41726,9 +41737,9 @@ fn __action579< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, op, _): (TextSize, ast::UnaryOp, TextSize), - (_, operand, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, operand, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprUnaryOp { operand: Box::new(operand.into()), @@ -41743,8 +41754,8 @@ fn __action580< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41755,8 +41766,8 @@ fn __action581< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { alloc::vec![__0] } @@ -41767,9 +41778,9 @@ fn __action582< >( source_code: &str, mode: Mode, - (_, v, _): (TextSize, alloc::vec::Vec, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + (_, v, _): (TextSize, alloc::vec::Vec, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { { let mut v = v; v.push(e); v } } @@ -41781,11 +41792,11 @@ fn __action583< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, left, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, left, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, right, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBinOp { left: Box::new(left.into()), @@ -41801,8 +41812,8 @@ fn __action584< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41815,9 +41826,9 @@ fn __action585< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { ast::ExprAwait { value: Box::new(value.into()), range: (location..end_location).into() }.into() @@ -41830,8 +41841,8 @@ fn __action586< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41842,8 +41853,8 @@ fn __action587< >( source_code: &str, mode: Mode, - (_, __0, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + (_, __0, _): (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { __0 } @@ -41855,10 +41866,10 @@ fn __action588< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, func, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, func, _): (TextSize, ParenthesizedExpr, TextSize), (_, arguments, _): (TextSize, ast::Arguments, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprCall { func: Box::new(func.into()), @@ -41874,12 +41885,12 @@ fn __action589< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, slice, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, slice, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprSubscript { value: Box::new(value.into()), @@ -41896,11 +41907,11 @@ fn __action590< source_code: &str, mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), - (_, value, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, value, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, attr, _): (TextSize, ast::Identifier, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprAttribute { value: Box::new(value.into()), @@ -41917,7 +41928,7 @@ fn __action591< source_code: &str, mode: Mode, (_, expr, _): (TextSize, ast::Expr, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { expr.into() } @@ -41931,7 +41942,7 @@ fn __action592< (_, location, _): (TextSize, TextSize, TextSize), (_, value, _): (TextSize, ast::Number, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprNumberLiteral { value, @@ -41948,7 +41959,7 @@ fn __action593< (_, location, _): (TextSize, TextSize, TextSize), (_, id, _): (TextSize, ast::Identifier, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprName { id: id.into(), @@ -41965,10 +41976,10 @@ fn __action594< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elts, _): (TextSize, core::option::Option>, TextSize), + (_, elts, _): (TextSize, core::option::Option>, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let elts = elts.into_iter().flatten().map(ast::Expr::from).collect(); @@ -41984,11 +41995,11 @@ fn __action595< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elt, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, elt, _): (TextSize, ParenthesizedExpr, TextSize), (_, generators, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { ast::ExprListComp { elt: Box::new(elt.into()), generators, range: (location..end_location).into() }.into() @@ -42003,29 +42014,29 @@ fn __action596< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, left, _): (TextSize, core::option::Option>, TextSize), - (_, mid, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), - (_, right, _): (TextSize, alloc::vec::Vec, TextSize), + (_, left, _): (TextSize, core::option::Option>, TextSize), + (_, mid, _): (TextSize, ParenthesizedExpr, TextSize), + (_, right, _): (TextSize, alloc::vec::Vec, TextSize), (_, trailing_comma, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { { if left.is_none() && right.is_empty() && trailing_comma.is_none() { if mid.expr.is_starred_expr() { return Err(LexicalError::new( LexicalErrorType::OtherError("cannot use starred expression here".to_string().into_boxed_str()), - mid.start(), + mid.range(), ))?; } - Ok(crate::parser::ParenthesizedExpr { + Ok(ParenthesizedExpr { expr: mid.into(), range: (location..end_location).into(), }) } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect(); - Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()) + Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), parenthesized: true }.into()) } } } @@ -42040,12 +42051,13 @@ fn __action597< (_, _, _): (TextSize, token::Tok, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprTuple { elts: Vec::new(), ctx: ast::ExprContext::Load, range: (location..end_location).into(), + parenthesized: true, }.into() } @@ -42057,12 +42069,12 @@ fn __action598< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { - crate::parser::ParenthesizedExpr { + ParenthesizedExpr { expr: e.into(), range: (location..end_location).into(), } @@ -42076,11 +42088,11 @@ fn __action599< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elt, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, elt, _): (TextSize, ParenthesizedExpr, TextSize), (_, generators, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprGeneratorExp { elt: Box::new(elt.into()), @@ -42098,15 +42110,15 @@ fn __action600< (_, _, _): (TextSize, token::Tok, TextSize), (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, e, _): (TextSize, ParenthesizedExpr, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { { Err(LexicalError::new( LexicalErrorType::OtherError("cannot use double starred expression here".to_string().into_boxed_str()), - location, + (location..end_location).into(), ).into()) } } @@ -42119,10 +42131,10 @@ fn __action601< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e, _): (TextSize, core::option::Option>, crate::parser::ParenthesizedExpr)>>, TextSize), + (_, e, _): (TextSize, core::option::Option>, ParenthesizedExpr)>>, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let (keys, values) = e @@ -42142,11 +42154,11 @@ fn __action602< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, e1, _): (TextSize, (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr), TextSize), + (_, e1, _): (TextSize, (ParenthesizedExpr, ParenthesizedExpr), TextSize), (_, generators, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { ast::ExprDictComp { @@ -42166,10 +42178,10 @@ fn __action603< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elts, _): (TextSize, Vec, TextSize), + (_, elts, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { { let elts = elts.into_iter().map(ast::Expr::from).collect(); @@ -42188,11 +42200,11 @@ fn __action604< mode: Mode, (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), - (_, elt, _): (TextSize, crate::parser::ParenthesizedExpr, TextSize), + (_, elt, _): (TextSize, ParenthesizedExpr, TextSize), (_, generators, _): (TextSize, Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprSetComp { elt: Box::new(elt.into()), @@ -42210,7 +42222,7 @@ fn __action605< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBooleanLiteral { value: true, range: (location..end_location).into() }.into() } @@ -42224,7 +42236,7 @@ fn __action606< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprBooleanLiteral { value: false, range: (location..end_location).into() }.into() } @@ -42238,7 +42250,7 @@ fn __action607< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprNoneLiteral { range: (location..end_location).into() }.into() } @@ -42252,7 +42264,7 @@ fn __action608< (_, location, _): (TextSize, TextSize, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, end_location, _): (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { ast::ExprEllipsisLiteral { range: (location..end_location).into() }.into() } @@ -42265,11 +42277,11 @@ fn __action609< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, Vec, TextSize), + __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.0; let __end0 = __3.2; @@ -42299,10 +42311,10 @@ fn __action610< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, Vec, TextSize), + __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __3.0; @@ -42333,13 +42345,13 @@ fn __action611< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, core::option::Option>, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __2: (TextSize, core::option::Option>, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __5.0; let __end0 = __5.2; @@ -42371,12 +42383,12 @@ fn __action612< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, core::option::Option>, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __2: (TextSize, core::option::Option>, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __4.2; let __end0 = __5.0; @@ -42409,13 +42421,13 @@ fn __action613< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, core::option::Option>, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __2: (TextSize, core::option::Option>, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __5.0; let __end0 = __5.2; @@ -42447,12 +42459,12 @@ fn __action614< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, core::option::Option>, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __2: (TextSize, core::option::Option>, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __4.2; let __end0 = __5.0; @@ -42483,9 +42495,9 @@ fn __action615< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec<(Option>, crate::parser::ParenthesizedExpr)>, TextSize), + __0: (TextSize, Vec<(Option>, ParenthesizedExpr)>, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> Vec<(Option>, crate::parser::ParenthesizedExpr)> +) -> Vec<(Option>, ParenthesizedExpr)> { let __start0 = __1.0; let __end0 = __1.2; @@ -42509,8 +42521,8 @@ fn __action616< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec<(Option>, crate::parser::ParenthesizedExpr)>, TextSize), -) -> Vec<(Option>, crate::parser::ParenthesizedExpr)> + __0: (TextSize, Vec<(Option>, ParenthesizedExpr)>, TextSize), +) -> Vec<(Option>, ParenthesizedExpr)> { let __start0 = __0.2; let __end0 = __0.2; @@ -42535,9 +42547,9 @@ fn __action617< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> Vec +) -> Vec { let __start0 = __1.0; let __end0 = __1.2; @@ -42561,8 +42573,8 @@ fn __action618< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), -) -> Vec + __0: (TextSize, Vec, TextSize), +) -> Vec { let __start0 = __0.2; let __end0 = __0.2; @@ -42588,10 +42600,10 @@ fn __action619< source_code: &str, mode: Mode, __0: (TextSize, TextSize, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.0; let __end0 = __2.2; @@ -42618,9 +42630,9 @@ fn __action620< source_code: &str, mode: Mode, __0: (TextSize, TextSize, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __2.0; @@ -42648,10 +42660,10 @@ fn __action621< source_code: &str, mode: Mode, __0: (TextSize, TextSize, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.0; let __end0 = __2.2; @@ -42678,9 +42690,9 @@ fn __action622< source_code: &str, mode: Mode, __0: (TextSize, TextSize, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __2.0; @@ -42775,9 +42787,9 @@ fn __action625< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> Vec +) -> Vec { let __start0 = __1.0; let __end0 = __1.2; @@ -42801,8 +42813,8 @@ fn __action626< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), -) -> Vec + __0: (TextSize, Vec, TextSize), +) -> Vec { let __start0 = __0.2; let __end0 = __0.2; @@ -43050,7 +43062,7 @@ fn __action633< __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, TextSize, TextSize), - __3: (TextSize, Vec, TextSize), + __3: (TextSize, Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), __6: (TextSize, token::Tok, TextSize), @@ -43094,7 +43106,7 @@ fn __action634< __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, TextSize, TextSize), - __3: (TextSize, Vec, TextSize), + __3: (TextSize, Vec, TextSize), __4: (TextSize, TextSize, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, token::Tok, TextSize), @@ -43975,9 +43987,9 @@ fn __action661< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> Vec +) -> Vec { let __start0 = __1.0; let __end0 = __1.2; @@ -44001,8 +44013,8 @@ fn __action662< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), -) -> Vec + __0: (TextSize, Vec, TextSize), +) -> Vec { let __start0 = __0.2; let __end0 = __0.2; @@ -44028,10 +44040,10 @@ fn __action663< source_code: &str, mode: Mode, __0: (TextSize, TextSize, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.0; let __end0 = __2.2; @@ -44058,9 +44070,9 @@ fn __action664< source_code: &str, mode: Mode, __0: (TextSize, TextSize, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __2.0; @@ -44533,7 +44545,7 @@ fn __action679< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, core::option::Option<(TextSize, ast::ConversionFlag)>, TextSize), __5: (TextSize, core::option::Option, TextSize), @@ -44571,7 +44583,7 @@ fn __action680< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, core::option::Option<(TextSize, ast::ConversionFlag)>, TextSize), __4: (TextSize, core::option::Option, TextSize), __5: (TextSize, token::Tok, TextSize), @@ -44610,9 +44622,9 @@ fn __action681< __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), - __5: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __5: (TextSize, ParenthesizedExpr, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, ast::Suite, TextSize), __8: (TextSize, core::option::Option, TextSize), @@ -44649,9 +44661,9 @@ fn __action682< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, ast::Suite, TextSize), __7: (TextSize, core::option::Option, TextSize), @@ -44694,7 +44706,7 @@ fn __action683< __4: (TextSize, ast::Identifier, TextSize), __5: (TextSize, core::option::Option, TextSize), __6: (TextSize, ast::Parameters, TextSize), - __7: (TextSize, core::option::Option, TextSize), + __7: (TextSize, core::option::Option, TextSize), __8: (TextSize, token::Tok, TextSize), __9: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -44735,7 +44747,7 @@ fn __action684< __3: (TextSize, ast::Identifier, TextSize), __4: (TextSize, core::option::Option, TextSize), __5: (TextSize, ast::Parameters, TextSize), - __6: (TextSize, core::option::Option, TextSize), + __6: (TextSize, core::option::Option, TextSize), __7: (TextSize, token::Tok, TextSize), __8: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -44774,10 +44786,10 @@ fn __action685< __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), - __5: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __6: (TextSize, alloc::vec::Vec, TextSize), + __5: (TextSize, ParenthesizedExpr, TextSize), + __6: (TextSize, alloc::vec::Vec, TextSize), __7: (TextSize, TextSize, TextSize), ) -> ast::Comprehension { @@ -44811,10 +44823,10 @@ fn __action686< mode: Mode, __0: (TextSize, TextSize, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __5: (TextSize, alloc::vec::Vec, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), + __5: (TextSize, alloc::vec::Vec, TextSize), __6: (TextSize, TextSize, TextSize), ) -> ast::Comprehension { @@ -45964,10 +45976,10 @@ fn __action723< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), -) -> (TextSize, crate::parser::ParenthesizedExpr, ast::Suite) +) -> (TextSize, ParenthesizedExpr, ast::Suite) { let __start0 = __0.0; let __end0 = __0.0; @@ -46025,11 +46037,11 @@ fn __action725< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46057,11 +46069,11 @@ fn __action726< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46089,11 +46101,11 @@ fn __action727< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46121,10 +46133,10 @@ fn __action728< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46151,10 +46163,10 @@ fn __action729< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46213,11 +46225,11 @@ fn __action731< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46245,11 +46257,11 @@ fn __action732< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46310,8 +46322,8 @@ fn __action734< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, core::option::Option, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, core::option::Option, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -46343,7 +46355,7 @@ fn __action735< mode: Mode, __0: (TextSize, ast::Number, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46371,7 +46383,7 @@ fn __action736< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46398,10 +46410,10 @@ fn __action737< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), + __1: (TextSize, core::option::Option>, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46430,11 +46442,11 @@ fn __action738< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46464,11 +46476,11 @@ fn __action739< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46498,10 +46510,10 @@ fn __action740< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46530,13 +46542,13 @@ fn __action741< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, alloc::vec::Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.0; let __end0 = __0.0; @@ -46568,12 +46580,12 @@ fn __action742< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, alloc::vec::Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.0; let __end0 = __0.0; @@ -46606,7 +46618,7 @@ fn __action743< __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46634,10 +46646,10 @@ fn __action744< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46666,11 +46678,11 @@ fn __action745< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46701,10 +46713,10 @@ fn __action746< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -46734,10 +46746,10 @@ fn __action747< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, crate::parser::ParenthesizedExpr)>>, TextSize), + __1: (TextSize, core::option::Option>, ParenthesizedExpr)>>, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46766,11 +46778,11 @@ fn __action748< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr), TextSize), + __1: (TextSize, (ParenthesizedExpr, ParenthesizedExpr), TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46800,10 +46812,10 @@ fn __action749< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46832,11 +46844,11 @@ fn __action750< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46867,7 +46879,7 @@ fn __action751< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46895,7 +46907,7 @@ fn __action752< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46923,7 +46935,7 @@ fn __action753< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46951,7 +46963,7 @@ fn __action754< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -46979,7 +46991,7 @@ fn __action755< mode: Mode, __0: (TextSize, ast::Number, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47007,7 +47019,7 @@ fn __action756< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47034,10 +47046,10 @@ fn __action757< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), + __1: (TextSize, core::option::Option>, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47066,11 +47078,11 @@ fn __action758< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47100,13 +47112,13 @@ fn __action759< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, alloc::vec::Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.0; let __end0 = __0.0; @@ -47138,12 +47150,12 @@ fn __action760< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, alloc::vec::Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.0; let __end0 = __0.0; @@ -47176,7 +47188,7 @@ fn __action761< __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47204,10 +47216,10 @@ fn __action762< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47236,11 +47248,11 @@ fn __action763< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47271,10 +47283,10 @@ fn __action764< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -47304,10 +47316,10 @@ fn __action765< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, crate::parser::ParenthesizedExpr)>>, TextSize), + __1: (TextSize, core::option::Option>, ParenthesizedExpr)>>, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47336,11 +47348,11 @@ fn __action766< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr), TextSize), + __1: (TextSize, (ParenthesizedExpr, ParenthesizedExpr), TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47370,10 +47382,10 @@ fn __action767< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47402,11 +47414,11 @@ fn __action768< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47437,7 +47449,7 @@ fn __action769< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47465,7 +47477,7 @@ fn __action770< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47493,7 +47505,7 @@ fn __action771< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47521,7 +47533,7 @@ fn __action772< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47547,10 +47559,10 @@ fn __action773< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Arguments, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47577,12 +47589,12 @@ fn __action774< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47611,11 +47623,11 @@ fn __action775< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, ast::Identifier, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47643,10 +47655,10 @@ fn __action776< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Arguments, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47673,12 +47685,12 @@ fn __action777< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47707,11 +47719,11 @@ fn __action778< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, ast::Identifier, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47740,9 +47752,9 @@ fn __action779< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47770,9 +47782,9 @@ fn __action780< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47925,10 +47937,10 @@ fn __action785< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __1: (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), + __1: (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47955,10 +47967,10 @@ fn __action786< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __1: (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), + __1: (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -47986,7 +47998,7 @@ fn __action787< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), __3: (TextSize, token::Tok, TextSize), ) -> ast::Decorator @@ -48018,7 +48030,7 @@ fn __action788< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -48106,7 +48118,7 @@ fn __action791< source_code: &str, mode: Mode, __0: (TextSize, ast::Identifier, TextSize), - __1: (TextSize, core::option::Option, TextSize), + __1: (TextSize, core::option::Option, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::Parameter { @@ -48136,7 +48148,7 @@ fn __action792< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option, TextSize), + __1: (TextSize, core::option::Option, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -48168,7 +48180,7 @@ fn __action793< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, (crate::parser::ParenthesizedExpr, ast::Identifier), TextSize), + __1: (TextSize, (ParenthesizedExpr, ast::Identifier), TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -48201,7 +48213,7 @@ fn __action794< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -48235,7 +48247,7 @@ fn __action795< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, (crate::parser::ParenthesizedExpr, ast::Identifier), TextSize), + __2: (TextSize, (ParenthesizedExpr, ast::Identifier), TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -48267,11 +48279,11 @@ fn __action796< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -48299,11 +48311,11 @@ fn __action797< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -48331,8 +48343,8 @@ fn __action798< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __1: (TextSize, alloc::vec::Vec, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), + __1: (TextSize, alloc::vec::Vec, TextSize), __2: (TextSize, TextSize, TextSize), ) -> Result> { @@ -48361,9 +48373,9 @@ fn __action799< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), ) -> Result> { @@ -48393,10 +48405,10 @@ fn __action800< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, core::option::Option, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, TextSize, TextSize), ) -> Result> { @@ -48554,7 +48566,7 @@ fn __action805< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, core::option::Option<(TextSize, ast::ConversionFlag)>, TextSize), __4: (TextSize, core::option::Option, TextSize), @@ -48592,7 +48604,7 @@ fn __action806< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, core::option::Option<(TextSize, ast::ConversionFlag)>, TextSize), __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -48628,9 +48640,9 @@ fn __action807< source_code: &str, mode: Mode, __0: (TextSize, ast::UnaryOp, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -48658,9 +48670,9 @@ fn __action808< source_code: &str, mode: Mode, __0: (TextSize, ast::UnaryOp, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -48744,7 +48756,7 @@ fn __action811< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option, TextSize), + __1: (TextSize, core::option::Option, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -48773,7 +48785,7 @@ fn __action812< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -48803,9 +48815,9 @@ fn __action813< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, ast::Suite, TextSize), __7: (TextSize, core::option::Option, TextSize), @@ -48842,9 +48854,9 @@ fn __action814< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, ast::Suite, TextSize), __6: (TextSize, core::option::Option, TextSize), @@ -48885,7 +48897,7 @@ fn __action815< __3: (TextSize, ast::Identifier, TextSize), __4: (TextSize, core::option::Option, TextSize), __5: (TextSize, ast::Parameters, TextSize), - __6: (TextSize, core::option::Option, TextSize), + __6: (TextSize, core::option::Option, TextSize), __7: (TextSize, token::Tok, TextSize), __8: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -48926,7 +48938,7 @@ fn __action816< __2: (TextSize, ast::Identifier, TextSize), __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, ast::Parameters, TextSize), - __5: (TextSize, core::option::Option, TextSize), + __5: (TextSize, core::option::Option, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -48961,7 +48973,7 @@ fn __action817< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, core::option::Option>, TextSize), __2: (TextSize, TextSize, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) @@ -48993,7 +49005,7 @@ fn __action818< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { @@ -49024,7 +49036,7 @@ fn __action819< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { @@ -49054,7 +49066,7 @@ fn __action820< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { @@ -49083,10 +49095,10 @@ fn __action821< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -49113,9 +49125,9 @@ fn __action822< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -49141,10 +49153,10 @@ fn __action823< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -49171,9 +49183,9 @@ fn __action824< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -49258,10 +49270,10 @@ fn __action827< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), - __4: (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize), + __4: (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize), __5: (TextSize, core::option::Option<(TextSize, ast::Suite)>, TextSize), ) -> ast::Stmt { @@ -49541,7 +49553,7 @@ fn __action836< mode: Mode, __0: (TextSize, (IpyEscapeKind, Box), TextSize), __1: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.0; let __end0 = __0.0; @@ -49595,7 +49607,7 @@ fn __action838< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, alloc::vec::Vec, TextSize), __2: (TextSize, TextSize, TextSize), ) -> Result> @@ -49630,9 +49642,9 @@ fn __action839< __2: (TextSize, TextSize, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, core::option::Option<(Box, bool, bool)>, TextSize), - __5: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __5: (TextSize, ParenthesizedExpr, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.0; let __end0 = __0.0; @@ -49757,7 +49769,7 @@ fn __action843< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, TextSize, TextSize), ) -> ast::Pattern { @@ -49785,7 +49797,7 @@ fn __action844< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, TextSize, TextSize), ) -> ast::Pattern { @@ -50356,7 +50368,7 @@ fn __action862< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -50394,7 +50406,7 @@ fn __action863< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -50446,7 +50458,7 @@ fn __action864< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -50498,7 +50510,7 @@ fn __action865< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, TextSize, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -50547,11 +50559,11 @@ fn __action866< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -50581,7 +50593,7 @@ fn __action867< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -50638,9 +50650,9 @@ fn __action869< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -50668,9 +50680,9 @@ fn __action870< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -50699,7 +50711,7 @@ fn __action871< mode: Mode, __0: (TextSize, ast::Number, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -50726,9 +50738,9 @@ fn __action872< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -50783,10 +50795,10 @@ fn __action874< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -50813,10 +50825,10 @@ fn __action875< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52079,11 +52091,11 @@ fn __action916< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52111,11 +52123,11 @@ fn __action917< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52172,8 +52184,8 @@ fn __action919< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, core::option::Option, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, core::option::Option, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -52401,11 +52413,11 @@ fn __action926< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52433,11 +52445,11 @@ fn __action927< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52467,10 +52479,10 @@ fn __action928< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __5: (TextSize, alloc::vec::Vec, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), + __5: (TextSize, alloc::vec::Vec, TextSize), __6: (TextSize, TextSize, TextSize), ) -> ast::Comprehension { @@ -52504,10 +52516,10 @@ fn __action929< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, TextSize, TextSize), ) -> ast::Comprehension { @@ -52540,8 +52552,8 @@ fn __action930< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option, TextSize), -) -> Option + __1: (TextSize, core::option::Option, TextSize), +) -> Option { let __start0 = __0.0; let __end0 = __0.0; @@ -52568,9 +52580,9 @@ fn __action931< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52628,7 +52640,7 @@ fn __action933< source_code: &str, mode: Mode, __0: (TextSize, ast::Identifier, TextSize), - __1: (TextSize, core::option::Option, TextSize), + __1: (TextSize, core::option::Option, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::Parameter { @@ -52767,12 +52779,12 @@ fn __action938< >( source_code: &str, mode: Mode, - __0: (TextSize, core::option::Option, TextSize), + __0: (TextSize, core::option::Option, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, core::option::Option, TextSize), - __3: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, core::option::Option, TextSize), + __3: (TextSize, core::option::Option>, TextSize), __4: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52801,10 +52813,10 @@ fn __action939< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52831,10 +52843,10 @@ fn __action940< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52861,9 +52873,9 @@ fn __action941< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52889,11 +52901,11 @@ fn __action942< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52921,11 +52933,11 @@ fn __action943< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52953,13 +52965,13 @@ fn __action944< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -52989,13 +53001,13 @@ fn __action945< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -53056,7 +53068,7 @@ fn __action947< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::Mod @@ -53227,7 +53239,7 @@ fn __action952< __1: (TextSize, ast::Expr, TextSize), __2: (TextSize, core::option::Option, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -53260,7 +53272,7 @@ fn __action953< source_code: &str, mode: Mode, __0: (TextSize, ast::Identifier, TextSize), - __1: (TextSize, core::option::Option, TextSize), + __1: (TextSize, core::option::Option, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::TypeParam { @@ -53416,7 +53428,7 @@ fn __action958< source_code: &str, mode: Mode, __0: (TextSize, ast::Identifier, TextSize), - __1: (TextSize, core::option::Option, TextSize), + __1: (TextSize, core::option::Option, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::ParameterWithDefault { @@ -53502,7 +53514,7 @@ fn __action961< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), __4: (TextSize, core::option::Option, TextSize), @@ -53535,9 +53547,9 @@ fn __action962< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::WithItem { @@ -53633,11 +53645,11 @@ fn __action965< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -53665,11 +53677,11 @@ fn __action966< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -53698,9 +53710,9 @@ fn __action967< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option, TextSize), + __1: (TextSize, core::option::Option, TextSize), __2: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -53729,9 +53741,9 @@ fn __action968< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -57232,8 +57244,8 @@ fn __action1079< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { let __start0 = __0.0; let __end0 = __1.2; @@ -57258,9 +57270,9 @@ fn __action1080< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -57290,7 +57302,7 @@ fn __action1081< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -57320,8 +57332,8 @@ fn __action1082< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { let __start0 = __0.0; let __end0 = __1.2; @@ -57345,10 +57357,10 @@ fn __action1083< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), + __0: (TextSize, alloc::vec::Vec, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec { let __start0 = __1.0; let __end0 = __2.2; @@ -57374,12 +57386,12 @@ fn __action1084< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __2.2; let __end0 = __3.0; @@ -57410,13 +57422,13 @@ fn __action1085< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, alloc::vec::Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.0; let __end0 = __3.2; @@ -57446,11 +57458,11 @@ fn __action1086< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __2.2; let __end0 = __3.0; @@ -57480,12 +57492,12 @@ fn __action1087< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, alloc::vec::Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.0; let __end0 = __3.2; @@ -57514,12 +57526,12 @@ fn __action1088< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __2.2; let __end0 = __3.0; @@ -57550,13 +57562,13 @@ fn __action1089< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, alloc::vec::Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.0; let __end0 = __3.2; @@ -57586,11 +57598,11 @@ fn __action1090< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __2.2; let __end0 = __3.0; @@ -57620,12 +57632,12 @@ fn __action1091< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, core::option::Option>, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, alloc::vec::Vec, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.0; let __end0 = __3.2; @@ -57840,8 +57852,8 @@ fn __action1098< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { let __start0 = __0.0; let __end0 = __1.2; @@ -57872,7 +57884,7 @@ fn __action1099< __4: (TextSize, core::option::Option, TextSize), __5: (TextSize, ast::Parameters, TextSize), __6: (TextSize, token::Tok, TextSize), - __7: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __7: (TextSize, ParenthesizedExpr, TextSize), __8: (TextSize, token::Tok, TextSize), __9: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -57953,7 +57965,7 @@ fn __action1101< __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, ast::Parameters, TextSize), __5: (TextSize, token::Tok, TextSize), - __6: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __6: (TextSize, ParenthesizedExpr, TextSize), __7: (TextSize, token::Tok, TextSize), __8: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -58080,8 +58092,8 @@ fn __action1105< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { let __start0 = __0.0; let __end0 = __1.2; @@ -58107,7 +58119,7 @@ fn __action1106< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::Parameter { @@ -58165,7 +58177,7 @@ fn __action1108< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::TypeParam { @@ -58223,7 +58235,7 @@ fn __action1110< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::ParameterWithDefault { @@ -58280,8 +58292,8 @@ fn __action1112< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { let __start0 = __0.0; let __end0 = __1.2; @@ -58307,7 +58319,7 @@ fn __action1113< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::Parameter { @@ -58464,7 +58476,7 @@ fn __action1119< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::Mod { @@ -58494,7 +58506,7 @@ fn __action1120< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, TextSize, TextSize), ) -> ast::Mod @@ -58695,9 +58707,9 @@ fn __action1127< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, ast::Suite, TextSize), __7: (TextSize, token::Tok, TextSize), @@ -58737,9 +58749,9 @@ fn __action1128< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -58774,9 +58786,9 @@ fn __action1129< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, ast::Suite, TextSize), __6: (TextSize, token::Tok, TextSize), @@ -58814,9 +58826,9 @@ fn __action1130< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -59002,7 +59014,7 @@ fn __action1135< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -59038,7 +59050,7 @@ fn __action1136< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -59444,8 +59456,8 @@ fn __action1147< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { let __start0 = __0.0; let __end0 = __1.2; @@ -59470,9 +59482,9 @@ fn __action1148< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -59502,7 +59514,7 @@ fn __action1149< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, TextSize, TextSize), ) -> ast::Stmt { @@ -59532,10 +59544,10 @@ fn __action1150< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), -) -> alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)> +) -> alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)> { let __start0 = __0.0; let __end0 = __3.2; @@ -59561,12 +59573,12 @@ fn __action1151< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize), + __0: (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, ast::Suite, TextSize), -) -> alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)> +) -> alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)> { let __start0 = __1.0; let __end0 = __4.2; @@ -59594,7 +59606,7 @@ fn __action1152< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), __4: (TextSize, core::option::Option<(TextSize, ast::Suite)>, TextSize), @@ -59628,10 +59640,10 @@ fn __action1153< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), - __4: (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize), + __4: (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize), __5: (TextSize, core::option::Option<(TextSize, ast::Suite)>, TextSize), ) -> ast::Stmt { @@ -59690,7 +59702,7 @@ fn __action1155< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -59726,7 +59738,7 @@ fn __action1156< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -59758,10 +59770,10 @@ fn __action1157< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), - __4: (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize), + __4: (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, ast::Suite, TextSize), @@ -59796,10 +59808,10 @@ fn __action1158< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), - __4: (TextSize, alloc::vec::Vec<(TextSize, crate::parser::ParenthesizedExpr, ast::Suite)>, TextSize), + __4: (TextSize, alloc::vec::Vec<(TextSize, ParenthesizedExpr, ast::Suite)>, TextSize), ) -> ast::Stmt { let __start0 = __4.2; @@ -59829,9 +59841,9 @@ fn __action1159< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> alloc::vec::Vec +) -> alloc::vec::Vec { let __start0 = __0.0; let __end0 = __1.2; @@ -59855,10 +59867,10 @@ fn __action1160< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> alloc::vec::Vec +) -> alloc::vec::Vec { let __start0 = __1.0; let __end0 = __2.2; @@ -59989,9 +60001,9 @@ fn __action1165< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> alloc::vec::Vec +) -> alloc::vec::Vec { let __start0 = __0.0; let __end0 = __1.2; @@ -60015,10 +60027,10 @@ fn __action1166< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> alloc::vec::Vec +) -> alloc::vec::Vec { let __start0 = __1.0; let __end0 = __2.2; @@ -60043,9 +60055,9 @@ fn __action1167< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> core::option::Option> +) -> core::option::Option> { let __start0 = __0.0; let __end0 = __1.2; @@ -60070,13 +60082,13 @@ fn __action1168< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __1.0; let __end0 = __2.2; @@ -60106,11 +60118,11 @@ fn __action1169< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -60140,14 +60152,14 @@ fn __action1170< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __1.0; let __end0 = __2.2; @@ -60178,12 +60190,12 @@ fn __action1171< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -60214,12 +60226,12 @@ fn __action1172< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __1.0; let __end0 = __2.2; @@ -60248,10 +60260,10 @@ fn __action1173< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -60280,13 +60292,13 @@ fn __action1174< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __1.0; let __end0 = __2.2; @@ -60316,11 +60328,11 @@ fn __action1175< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -60350,13 +60362,13 @@ fn __action1176< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __1.0; let __end0 = __2.2; @@ -60386,11 +60398,11 @@ fn __action1177< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -60420,14 +60432,14 @@ fn __action1178< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __1.0; let __end0 = __2.2; @@ -60458,12 +60470,12 @@ fn __action1179< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -60494,12 +60506,12 @@ fn __action1180< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __1.0; let __end0 = __2.2; @@ -60528,10 +60540,10 @@ fn __action1181< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -60560,13 +60572,13 @@ fn __action1182< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __1.0; let __end0 = __2.2; @@ -60596,11 +60608,11 @@ fn __action1183< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, TextSize, TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -61270,7 +61282,7 @@ fn __action1206< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Identifier, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -61305,7 +61317,7 @@ fn __action1207< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, ast::Identifier, TextSize), __5: (TextSize, token::Tok, TextSize), @@ -61339,7 +61351,7 @@ fn __action1208< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), ) -> Vec { @@ -61366,7 +61378,7 @@ fn __action1209< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), ) -> Vec @@ -61396,7 +61408,7 @@ fn __action1210< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), ) -> Vec { @@ -61423,7 +61435,7 @@ fn __action1211< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), ) -> core::option::Option> { @@ -61450,7 +61462,7 @@ fn __action1212< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::WithItem, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -61516,7 +61528,7 @@ fn __action1214< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::WithItem, TextSize), __4: (TextSize, alloc::vec::Vec, TextSize), @@ -61586,7 +61598,7 @@ fn __action1216< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::WithItem, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -61648,7 +61660,7 @@ fn __action1218< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::WithItem, TextSize), __4: (TextSize, alloc::vec::Vec, TextSize), @@ -61714,8 +61726,8 @@ fn __action1220< source_code: &str, mode: Mode, __0: (TextSize, ast::CmpOp, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)> + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)> { let __start0 = __0.0; let __end0 = __1.2; @@ -61739,10 +61751,10 @@ fn __action1221< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize), + __0: (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize), __1: (TextSize, ast::CmpOp, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)> + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)> { let __start0 = __1.0; let __end0 = __2.2; @@ -61939,10 +61951,10 @@ fn __action1228< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -61969,10 +61981,10 @@ fn __action1229< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -61999,10 +62011,10 @@ fn __action1230< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -62029,9 +62041,9 @@ fn __action1231< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -62057,9 +62069,9 @@ fn __action1232< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -62115,10 +62127,10 @@ fn __action1234< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -62145,10 +62157,10 @@ fn __action1235< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -62206,9 +62218,9 @@ fn __action1237< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __3.2; @@ -62238,7 +62250,7 @@ fn __action1238< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __1.2; @@ -62266,7 +62278,7 @@ fn __action1239< source_code: &str, mode: Mode, __0: (TextSize, ast::Number, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -62292,7 +62304,7 @@ fn __action1240< source_code: &str, mode: Mode, __0: (TextSize, ast::Identifier, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -62318,9 +62330,9 @@ fn __action1241< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), + __1: (TextSize, core::option::Option>, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -62348,10 +62360,10 @@ fn __action1242< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -62380,10 +62392,10 @@ fn __action1243< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -62412,9 +62424,9 @@ fn __action1244< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -62442,12 +62454,12 @@ fn __action1245< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __5.2; let __end0 = __5.2; @@ -62478,10 +62490,10 @@ fn __action1246< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.2; let __end0 = __3.2; @@ -62510,13 +62522,13 @@ fn __action1247< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __6.2; let __end0 = __6.2; @@ -62548,11 +62560,11 @@ fn __action1248< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __4.2; let __end0 = __4.2; @@ -62582,11 +62594,11 @@ fn __action1249< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __4.2; let __end0 = __4.2; @@ -62616,9 +62628,9 @@ fn __action1250< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __2.2; let __end0 = __2.2; @@ -62646,12 +62658,12 @@ fn __action1251< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __5.2; let __end0 = __5.2; @@ -62682,10 +62694,10 @@ fn __action1252< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.2; let __end0 = __3.2; @@ -62715,7 +62727,7 @@ fn __action1253< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -62742,9 +62754,9 @@ fn __action1254< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -62772,10 +62784,10 @@ fn __action1255< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -62805,9 +62817,9 @@ fn __action1256< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.2; let __end0 = __3.2; @@ -62836,9 +62848,9 @@ fn __action1257< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, crate::parser::ParenthesizedExpr)>>, TextSize), + __1: (TextSize, core::option::Option>, ParenthesizedExpr)>>, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -62866,10 +62878,10 @@ fn __action1258< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr), TextSize), + __1: (TextSize, (ParenthesizedExpr, ParenthesizedExpr), TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -62898,9 +62910,9 @@ fn __action1259< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -62928,10 +62940,10 @@ fn __action1260< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -62960,7 +62972,7 @@ fn __action1261< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -62986,7 +62998,7 @@ fn __action1262< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63012,7 +63024,7 @@ fn __action1263< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63038,7 +63050,7 @@ fn __action1264< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63064,7 +63076,7 @@ fn __action1265< source_code: &str, mode: Mode, __0: (TextSize, ast::Number, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63090,7 +63102,7 @@ fn __action1266< source_code: &str, mode: Mode, __0: (TextSize, ast::Identifier, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63116,9 +63128,9 @@ fn __action1267< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, TextSize), + __1: (TextSize, core::option::Option>, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -63146,10 +63158,10 @@ fn __action1268< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -63178,12 +63190,12 @@ fn __action1269< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __5.2; let __end0 = __5.2; @@ -63214,10 +63226,10 @@ fn __action1270< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.2; let __end0 = __3.2; @@ -63246,13 +63258,13 @@ fn __action1271< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __6.2; let __end0 = __6.2; @@ -63284,11 +63296,11 @@ fn __action1272< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __4.2; let __end0 = __4.2; @@ -63318,11 +63330,11 @@ fn __action1273< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __4.2; let __end0 = __4.2; @@ -63352,9 +63364,9 @@ fn __action1274< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __2.2; let __end0 = __2.2; @@ -63382,12 +63394,12 @@ fn __action1275< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), __5: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __5.2; let __end0 = __5.2; @@ -63418,10 +63430,10 @@ fn __action1276< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, alloc::vec::Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.2; let __end0 = __3.2; @@ -63451,7 +63463,7 @@ fn __action1277< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -63478,9 +63490,9 @@ fn __action1278< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -63508,10 +63520,10 @@ fn __action1279< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -63541,9 +63553,9 @@ fn __action1280< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> Result> +) -> Result> { let __start0 = __3.2; let __end0 = __3.2; @@ -63572,9 +63584,9 @@ fn __action1281< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option>, crate::parser::ParenthesizedExpr)>>, TextSize), + __1: (TextSize, core::option::Option>, ParenthesizedExpr)>>, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -63602,10 +63614,10 @@ fn __action1282< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, (crate::parser::ParenthesizedExpr, crate::parser::ParenthesizedExpr), TextSize), + __1: (TextSize, (ParenthesizedExpr, ParenthesizedExpr), TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -63634,9 +63646,9 @@ fn __action1283< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -63664,10 +63676,10 @@ fn __action1284< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, Vec, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -63696,7 +63708,7 @@ fn __action1285< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63722,7 +63734,7 @@ fn __action1286< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63748,7 +63760,7 @@ fn __action1287< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63774,7 +63786,7 @@ fn __action1288< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -63799,9 +63811,9 @@ fn __action1289< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Arguments, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -63827,11 +63839,11 @@ fn __action1290< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -63859,10 +63871,10 @@ fn __action1291< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, ast::Identifier, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -63889,9 +63901,9 @@ fn __action1292< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Arguments, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -63917,11 +63929,11 @@ fn __action1293< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -63949,10 +63961,10 @@ fn __action1294< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, ast::Identifier, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -63980,8 +63992,8 @@ fn __action1295< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -64008,8 +64020,8 @@ fn __action1296< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -64117,9 +64129,9 @@ fn __action1300< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __1: (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, ParenthesizedExpr, TextSize), + __1: (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -64145,9 +64157,9 @@ fn __action1301< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __1: (TextSize, alloc::vec::Vec<(ast::CmpOp, crate::parser::ParenthesizedExpr)>, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, ParenthesizedExpr, TextSize), + __1: (TextSize, alloc::vec::Vec<(ast::CmpOp, ParenthesizedExpr)>, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -64174,7 +64186,7 @@ fn __action1302< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), ) -> ast::Decorator { @@ -64204,7 +64216,7 @@ fn __action1303< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), ) -> ast::Stmt { let __start0 = __1.2; @@ -64287,7 +64299,7 @@ fn __action1306< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Parameter { let __start0 = __2.2; @@ -64341,10 +64353,10 @@ fn __action1308< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -64371,10 +64383,10 @@ fn __action1309< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -64401,8 +64413,8 @@ fn __action1310< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __1: (TextSize, alloc::vec::Vec, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), + __1: (TextSize, alloc::vec::Vec, TextSize), ) -> Result> { let __start0 = __1.2; @@ -64429,9 +64441,9 @@ fn __action1311< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> Result> { let __start0 = __2.2; @@ -64459,10 +64471,10 @@ fn __action1312< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, core::option::Option, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, core::option::Option, TextSize), ) -> Result> { let __start0 = __3.2; @@ -64574,7 +64586,7 @@ fn __action1316< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, core::option::Option<(TextSize, ast::ConversionFlag)>, TextSize), __4: (TextSize, core::option::Option, TextSize), @@ -64610,7 +64622,7 @@ fn __action1317< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, core::option::Option<(TextSize, ast::ConversionFlag)>, TextSize), __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -64644,8 +64656,8 @@ fn __action1318< source_code: &str, mode: Mode, __0: (TextSize, ast::UnaryOp, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -64672,8 +64684,8 @@ fn __action1319< source_code: &str, mode: Mode, __0: (TextSize, ast::UnaryOp, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -64752,7 +64764,7 @@ fn __action1322< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option, TextSize), + __1: (TextSize, core::option::Option, TextSize), ) -> ast::Stmt { let __start0 = __1.2; @@ -64779,7 +64791,7 @@ fn __action1323< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __0.2; @@ -64805,7 +64817,7 @@ fn __action1324< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, core::option::Option>, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { @@ -64835,7 +64847,7 @@ fn __action1325< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { let __start0 = __2.2; @@ -64864,7 +64876,7 @@ fn __action1326< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { let __start0 = __1.2; @@ -64892,7 +64904,7 @@ fn __action1327< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { let __start0 = __1.2; @@ -64919,9 +64931,9 @@ fn __action1328< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -64947,8 +64959,8 @@ fn __action1329< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, Vec, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -64973,9 +64985,9 @@ fn __action1330< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -65001,8 +65013,8 @@ fn __action1331< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, Vec, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -65368,7 +65380,7 @@ fn __action1344< source_code: &str, mode: Mode, __0: (TextSize, (IpyEscapeKind, Box), TextSize), -) -> Result> +) -> Result> { let __start0 = __0.2; let __end0 = __0.2; @@ -65419,7 +65431,7 @@ fn __action1346< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, alloc::vec::Vec, TextSize), ) -> Result> { @@ -65451,8 +65463,8 @@ fn __action1347< __1: (TextSize, core::option::Option, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, core::option::Option<(Box, bool, bool)>, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Result> + __4: (TextSize, ParenthesizedExpr, TextSize), +) -> Result> { let __start0 = __1.2; let __end0 = __2.0; @@ -65569,7 +65581,7 @@ fn __action1351< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Pattern { let __start0 = __0.2; @@ -65595,7 +65607,7 @@ fn __action1352< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Pattern { let __start0 = __0.2; @@ -66098,7 +66110,7 @@ fn __action1369< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -66138,7 +66150,7 @@ fn __action1370< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -66178,7 +66190,7 @@ fn __action1371< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -66215,10 +66227,10 @@ fn __action1372< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -66246,7 +66258,7 @@ fn __action1373< source_code: &str, mode: Mode, __0: (TextSize, ast::Identifier, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -66300,8 +66312,8 @@ fn __action1375< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -66328,8 +66340,8 @@ fn __action1376< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -66356,7 +66368,7 @@ fn __action1377< source_code: &str, mode: Mode, __0: (TextSize, ast::Number, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -66382,8 +66394,8 @@ fn __action1378< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -66435,9 +66447,9 @@ fn __action1380< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -66463,9 +66475,9 @@ fn __action1381< >( source_code: &str, mode: Mode, - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, alloc::vec::Vec, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -66493,7 +66505,7 @@ fn __action1382< mode: Mode, __0: (TextSize, ast::ParameterWithDefault, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::ParameterWithDefault { let __start0 = __2.2; @@ -66523,7 +66535,7 @@ fn __action1383< mode: Mode, __0: (TextSize, ast::ParameterWithDefault, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::ParameterWithDefault { let __start0 = __2.2; @@ -69391,10 +69403,10 @@ fn __action1473< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -69421,10 +69433,10 @@ fn __action1474< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -69478,9 +69490,9 @@ fn __action1476< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __3.2; @@ -69510,7 +69522,7 @@ fn __action1477< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __1.2; @@ -69723,10 +69735,10 @@ fn __action1484< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -69753,10 +69765,10 @@ fn __action1485< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -69785,10 +69797,10 @@ fn __action1486< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __5: (TextSize, alloc::vec::Vec, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), + __5: (TextSize, alloc::vec::Vec, TextSize), ) -> ast::Comprehension { let __start0 = __5.2; @@ -69820,10 +69832,10 @@ fn __action1487< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), ) -> ast::Comprehension { let __start0 = __4.2; @@ -69854,8 +69866,8 @@ fn __action1488< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -69911,7 +69923,7 @@ fn __action1490< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Parameter { let __start0 = __2.2; @@ -70043,11 +70055,11 @@ fn __action1495< >( source_code: &str, mode: Mode, - __0: (TextSize, core::option::Option, TextSize), + __0: (TextSize, core::option::Option, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, core::option::Option, TextSize), - __3: (TextSize, core::option::Option>, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, core::option::Option, TextSize), + __3: (TextSize, core::option::Option>, TextSize), +) -> ParenthesizedExpr { let __start0 = __3.2; let __end0 = __3.2; @@ -70075,9 +70087,9 @@ fn __action1496< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -70103,9 +70115,9 @@ fn __action1497< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), + __0: (TextSize, Vec, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -70131,8 +70143,8 @@ fn __action1498< >( source_code: &str, mode: Mode, - __0: (TextSize, Vec, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, Vec, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -70157,10 +70169,10 @@ fn __action1499< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -70187,10 +70199,10 @@ fn __action1500< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -70217,12 +70229,12 @@ fn __action1501< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __4: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __4.2; let __end0 = __4.2; @@ -70251,12 +70263,12 @@ fn __action1502< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __4: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __4.2; let __end0 = __4.2; @@ -70314,7 +70326,7 @@ fn __action1504< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Mod { let __start0 = __1.2; @@ -70342,7 +70354,7 @@ fn __action1505< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, alloc::vec::Vec, TextSize), ) -> ast::Mod { @@ -70705,7 +70717,7 @@ fn __action1515< __1: (TextSize, ast::Expr, TextSize), __2: (TextSize, core::option::Option, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __4.2; @@ -70737,7 +70749,7 @@ fn __action1516< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::TypeParam { let __start0 = __2.2; @@ -70911,7 +70923,7 @@ fn __action1522< mode: Mode, __0: (TextSize, ast::Identifier, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::ParameterWithDefault { let __start0 = __2.2; @@ -71017,9 +71029,9 @@ fn __action1526< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::WithItem { let __start0 = __2.2; @@ -71047,10 +71059,10 @@ fn __action1527< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -71077,10 +71089,10 @@ fn __action1528< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -71108,8 +71120,8 @@ fn __action1529< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, core::option::Option, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, core::option::Option, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.2; let __end0 = __1.2; @@ -71137,8 +71149,8 @@ fn __action1530< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -71237,7 +71249,7 @@ fn __action1533< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), ) -> Result> { let __start0 = __0.2; @@ -71263,8 +71275,8 @@ fn __action1534< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __1: (TextSize, alloc::vec::Vec, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), + __1: (TextSize, alloc::vec::Vec, TextSize), ) -> Result> { let __start0 = __1.0; @@ -71289,10 +71301,10 @@ fn __action1535< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), ) -> Result> { let __start0 = __3.0; @@ -71319,9 +71331,9 @@ fn __action1536< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> Result> { let __start0 = __2.2; @@ -71781,7 +71793,7 @@ fn __action1553< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, Vec, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { @@ -71807,7 +71819,7 @@ fn __action1554< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), ) -> (Option<(TextSize, TextSize, Option)>, ast::Expr) { let __start0 = __0.2; @@ -71835,9 +71847,9 @@ fn __action1555< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Comprehension { let __start0 = __4.2; @@ -71869,10 +71881,10 @@ fn __action1556< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __5: (TextSize, alloc::vec::Vec, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), + __5: (TextSize, alloc::vec::Vec, TextSize), ) -> ast::Comprehension { let __start0 = __5.0; @@ -71902,9 +71914,9 @@ fn __action1557< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Comprehension { let __start0 = __3.2; @@ -71934,10 +71946,10 @@ fn __action1558< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __4: (TextSize, alloc::vec::Vec, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), + __4: (TextSize, alloc::vec::Vec, TextSize), ) -> ast::Comprehension { let __start0 = __4.0; @@ -72111,7 +72123,7 @@ fn __action1563< __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, ast::Parameters, TextSize), __5: (TextSize, token::Tok, TextSize), - __6: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __6: (TextSize, ParenthesizedExpr, TextSize), __7: (TextSize, token::Tok, TextSize), __8: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -72154,7 +72166,7 @@ fn __action1564< __4: (TextSize, core::option::Option, TextSize), __5: (TextSize, ast::Parameters, TextSize), __6: (TextSize, token::Tok, TextSize), - __7: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __7: (TextSize, ParenthesizedExpr, TextSize), __8: (TextSize, token::Tok, TextSize), __9: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -72270,7 +72282,7 @@ fn __action1567< __2: (TextSize, core::option::Option, TextSize), __3: (TextSize, ast::Parameters, TextSize), __4: (TextSize, token::Tok, TextSize), - __5: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __5: (TextSize, ParenthesizedExpr, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -72311,7 +72323,7 @@ fn __action1568< __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, ast::Parameters, TextSize), __5: (TextSize, token::Tok, TextSize), - __6: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __6: (TextSize, ParenthesizedExpr, TextSize), __7: (TextSize, token::Tok, TextSize), __8: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -72418,9 +72430,9 @@ fn __action1571< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec<(Option>, crate::parser::ParenthesizedExpr)>, TextSize), + __1: (TextSize, Vec<(Option>, ParenthesizedExpr)>, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.0; let __end0 = __1.2; @@ -72447,7 +72459,7 @@ fn __action1572< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __1.0; @@ -72474,9 +72486,9 @@ fn __action1573< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec<(Option>, crate::parser::ParenthesizedExpr)>, TextSize), + __1: (TextSize, Vec<(Option>, ParenthesizedExpr)>, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.0; let __end0 = __1.2; @@ -72503,7 +72515,7 @@ fn __action1574< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __1.0; @@ -72582,7 +72594,7 @@ fn __action1577< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, (TextSize, ast::ConversionFlag), TextSize), __4: (TextSize, core::option::Option, TextSize), @@ -72616,7 +72628,7 @@ fn __action1578< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -72650,7 +72662,7 @@ fn __action1579< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, (TextSize, ast::ConversionFlag), TextSize), __3: (TextSize, core::option::Option, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -72682,7 +72694,7 @@ fn __action1580< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, core::option::Option, TextSize), __3: (TextSize, token::Tok, TextSize), ) -> Result> @@ -72714,7 +72726,7 @@ fn __action1581< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, (TextSize, ast::ConversionFlag), TextSize), __4: (TextSize, ast::FStringFormatSpec, TextSize), @@ -72748,7 +72760,7 @@ fn __action1582< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, (TextSize, ast::ConversionFlag), TextSize), __4: (TextSize, token::Tok, TextSize), @@ -72782,7 +72794,7 @@ fn __action1583< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::FStringFormatSpec, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -72814,7 +72826,7 @@ fn __action1584< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), ) -> Result> @@ -72846,7 +72858,7 @@ fn __action1585< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, (TextSize, ast::ConversionFlag), TextSize), __3: (TextSize, ast::FStringFormatSpec, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -72878,7 +72890,7 @@ fn __action1586< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, (TextSize, ast::ConversionFlag), TextSize), __3: (TextSize, token::Tok, TextSize), ) -> Result> @@ -72910,7 +72922,7 @@ fn __action1587< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, ast::FStringFormatSpec, TextSize), __3: (TextSize, token::Tok, TextSize), ) -> Result> @@ -72940,7 +72952,7 @@ fn __action1588< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), ) -> Result> { @@ -73352,9 +73364,9 @@ fn __action1603< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.0; let __end0 = __1.2; @@ -73381,7 +73393,7 @@ fn __action1604< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __1.0; @@ -73408,9 +73420,9 @@ fn __action1605< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Vec, TextSize), + __1: (TextSize, Vec, TextSize), __2: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __1.0; let __end0 = __1.2; @@ -73437,7 +73449,7 @@ fn __action1606< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __1.0; @@ -77683,8 +77695,8 @@ fn __action1727< __1: (TextSize, ast::Parameters, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, core::option::Option<(Box, bool, bool)>, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Result> + __4: (TextSize, ParenthesizedExpr, TextSize), +) -> Result> { let __start0 = __1.0; let __end0 = __1.2; @@ -77714,8 +77726,8 @@ fn __action1728< __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, core::option::Option<(Box, bool, bool)>, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Result> + __3: (TextSize, ParenthesizedExpr, TextSize), +) -> Result> { let __start0 = __0.2; let __end0 = __1.0; @@ -77743,11 +77755,11 @@ fn __action1729< >( source_code: &str, mode: Mode, - __0: (TextSize, core::option::Option, TextSize), + __0: (TextSize, core::option::Option, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, core::option::Option, TextSize), - __3: (TextSize, Option, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, core::option::Option, TextSize), + __3: (TextSize, Option, TextSize), +) -> ParenthesizedExpr { let __start0 = __3.0; let __end0 = __3.2; @@ -77773,10 +77785,10 @@ fn __action1730< >( source_code: &str, mode: Mode, - __0: (TextSize, core::option::Option, TextSize), + __0: (TextSize, core::option::Option, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, core::option::Option, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, core::option::Option, TextSize), +) -> ParenthesizedExpr { let __start0 = __2.2; let __end0 = __2.2; @@ -77804,7 +77816,7 @@ fn __action1731< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Suite, TextSize), ) -> ast::ExceptHandler @@ -77864,8 +77876,8 @@ fn __action1733< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Option + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> Option { let __start0 = __1.0; let __end0 = __1.2; @@ -77890,7 +77902,7 @@ fn __action1734< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> Option +) -> Option { let __start0 = __0.2; let __end0 = __0.2; @@ -77915,11 +77927,11 @@ fn __action1735< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __3: (TextSize, Option, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), + __3: (TextSize, Option, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.2; @@ -77953,10 +77965,10 @@ fn __action1736< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, Option, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, Option, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.2; @@ -77992,9 +78004,9 @@ fn __action1737< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __2: (TextSize, Option, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), + __2: (TextSize, Option, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -78030,8 +78042,8 @@ fn __action1738< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, Option, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, Option, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -78067,10 +78079,10 @@ fn __action1739< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.2; @@ -78103,9 +78115,9 @@ fn __action1740< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.2; @@ -78140,8 +78152,8 @@ fn __action1741< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -78176,7 +78188,7 @@ fn __action1742< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.0; @@ -78213,9 +78225,9 @@ fn __action1743< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, ast::Suite, TextSize), __7: (TextSize, token::Tok, TextSize), @@ -78255,9 +78267,9 @@ fn __action1744< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -78290,9 +78302,9 @@ fn __action1745< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, ast::Suite, TextSize), __6: (TextSize, token::Tok, TextSize), @@ -78330,9 +78342,9 @@ fn __action1746< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), __4: (TextSize, token::Tok, TextSize), __5: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -78363,8 +78375,8 @@ fn __action1747< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> core::option::Option + __0: (TextSize, ParenthesizedExpr, TextSize), +) -> core::option::Option { let __start0 = __0.0; let __end0 = __0.2; @@ -78387,8 +78399,8 @@ fn __action1748< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.2; @@ -78411,8 +78423,8 @@ fn __action1749< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __0: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __0.0; let __end0 = __0.2; @@ -78436,7 +78448,7 @@ fn __action1750< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Mod { let __start0 = __1.0; @@ -78462,7 +78474,7 @@ fn __action1751< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), __2: (TextSize, alloc::vec::Vec, TextSize), ) -> ast::Mod { @@ -78490,7 +78502,7 @@ fn __action1752< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __1: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __1.0; @@ -78542,8 +78554,8 @@ fn __action1754< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), - __1: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> crate::parser::ParenthesizedExpr + __1: (TextSize, ParenthesizedExpr, TextSize), +) -> ParenthesizedExpr { let __start0 = __1.0; let __end0 = __1.2; @@ -78568,7 +78580,7 @@ fn __action1755< source_code: &str, mode: Mode, __0: (TextSize, token::Tok, TextSize), -) -> crate::parser::ParenthesizedExpr +) -> ParenthesizedExpr { let __start0 = __0.2; let __end0 = __0.2; @@ -78593,7 +78605,7 @@ fn __action1756< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), ) -> Result> { let __start0 = __0.0; @@ -78617,8 +78629,8 @@ fn __action1757< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), - __1: (TextSize, alloc::vec::Vec, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), + __1: (TextSize, alloc::vec::Vec, TextSize), ) -> Result> { let __start0 = __0.0; @@ -78643,9 +78655,9 @@ fn __action1758< >( source_code: &str, mode: Mode, - __0: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __0: (TextSize, ParenthesizedExpr, TextSize), __1: (TextSize, ast::Operator, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __2: (TextSize, ParenthesizedExpr, TextSize), ) -> Result> { let __start0 = __0.0; @@ -78949,7 +78961,7 @@ fn __action1767< __3: (TextSize, ast::TypeParams, TextSize), __4: (TextSize, ast::Parameters, TextSize), __5: (TextSize, token::Tok, TextSize), - __6: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __6: (TextSize, ParenthesizedExpr, TextSize), __7: (TextSize, token::Tok, TextSize), __8: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -78988,7 +79000,7 @@ fn __action1768< __2: (TextSize, ast::Identifier, TextSize), __3: (TextSize, ast::Parameters, TextSize), __4: (TextSize, token::Tok, TextSize), - __5: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __5: (TextSize, ParenthesizedExpr, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -79030,7 +79042,7 @@ fn __action1769< __4: (TextSize, ast::TypeParams, TextSize), __5: (TextSize, ast::Parameters, TextSize), __6: (TextSize, token::Tok, TextSize), - __7: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __7: (TextSize, ParenthesizedExpr, TextSize), __8: (TextSize, token::Tok, TextSize), __9: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -79071,7 +79083,7 @@ fn __action1770< __3: (TextSize, ast::Identifier, TextSize), __4: (TextSize, ast::Parameters, TextSize), __5: (TextSize, token::Tok, TextSize), - __6: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __6: (TextSize, ParenthesizedExpr, TextSize), __7: (TextSize, token::Tok, TextSize), __8: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -79260,7 +79272,7 @@ fn __action1775< __2: (TextSize, ast::TypeParams, TextSize), __3: (TextSize, ast::Parameters, TextSize), __4: (TextSize, token::Tok, TextSize), - __5: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __5: (TextSize, ParenthesizedExpr, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -79297,7 +79309,7 @@ fn __action1776< __1: (TextSize, ast::Identifier, TextSize), __2: (TextSize, ast::Parameters, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), __5: (TextSize, token::Tok, TextSize), __6: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -79337,7 +79349,7 @@ fn __action1777< __3: (TextSize, ast::TypeParams, TextSize), __4: (TextSize, ast::Parameters, TextSize), __5: (TextSize, token::Tok, TextSize), - __6: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __6: (TextSize, ParenthesizedExpr, TextSize), __7: (TextSize, token::Tok, TextSize), __8: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -79376,7 +79388,7 @@ fn __action1778< __2: (TextSize, ast::Identifier, TextSize), __3: (TextSize, ast::Parameters, TextSize), __4: (TextSize, token::Tok, TextSize), - __5: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __5: (TextSize, ParenthesizedExpr, TextSize), __6: (TextSize, token::Tok, TextSize), __7: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt @@ -79555,7 +79567,7 @@ fn __action1783< __1: (TextSize, ast::Expr, TextSize), __2: (TextSize, ast::TypeParams, TextSize), __3: (TextSize, token::Tok, TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __4: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __2.0; @@ -79586,7 +79598,7 @@ fn __action1784< __0: (TextSize, token::Tok, TextSize), __1: (TextSize, ast::Expr, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), + __3: (TextSize, ParenthesizedExpr, TextSize), ) -> ast::Stmt { let __start0 = __1.2; @@ -79619,8 +79631,8 @@ fn __action1785< __1: (TextSize, ast::Parameters, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, (Box, bool, bool), TextSize), - __4: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Result> + __4: (TextSize, ParenthesizedExpr, TextSize), +) -> Result> { let __start0 = __3.0; let __end0 = __3.2; @@ -79650,8 +79662,8 @@ fn __action1786< __0: (TextSize, token::Tok, TextSize), __1: (TextSize, ast::Parameters, TextSize), __2: (TextSize, token::Tok, TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Result> + __3: (TextSize, ParenthesizedExpr, TextSize), +) -> Result> { let __start0 = __2.2; let __end0 = __3.0; @@ -79682,8 +79694,8 @@ fn __action1787< __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), __2: (TextSize, (Box, bool, bool), TextSize), - __3: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Result> + __3: (TextSize, ParenthesizedExpr, TextSize), +) -> Result> { let __start0 = __2.0; let __end0 = __2.2; @@ -79711,8 +79723,8 @@ fn __action1788< mode: Mode, __0: (TextSize, token::Tok, TextSize), __1: (TextSize, token::Tok, TextSize), - __2: (TextSize, crate::parser::ParenthesizedExpr, TextSize), -) -> Result> + __2: (TextSize, ParenthesizedExpr, TextSize), +) -> Result> { let __start0 = __1.2; let __end0 = __2.0; diff --git a/crates/ruff_python_parser/src/lexer.rs b/crates/ruff_python_parser/src/lexer.rs index 9e3ab6d34f2c7..194f2d38acc4f 100644 --- a/crates/ruff_python_parser/src/lexer.rs +++ b/crates/ruff_python_parser/src/lexer.rs @@ -36,12 +36,12 @@ use unicode_ident::{is_xid_continue, is_xid_start}; use ruff_python_ast::{Int, IpyEscapeKind}; use ruff_text_size::{TextLen, TextRange, TextSize}; +use crate::error::FStringErrorType; use crate::lexer::cursor::{Cursor, EOF_CHAR}; use crate::lexer::fstring::{FStringContext, FStringContextFlags, FStrings}; use crate::lexer::indentation::{Indentation, Indentations}; use crate::{ soft_keywords::SoftKeywordTransformer, - string::FStringErrorType, token::{StringKind, Tok}, Mode, }; @@ -286,7 +286,7 @@ impl<'source> Lexer<'source> { Err(err) => { return Err(LexicalError::new( LexicalErrorType::OtherError(format!("{err:?}").into_boxed_str()), - self.token_range().start(), + self.token_range(), )); } }; @@ -311,7 +311,7 @@ impl<'source> Lexer<'source> { if self.cursor.eat_char('_') { return Err(LexicalError::new( LexicalErrorType::OtherError("Invalid Syntax".to_string().into_boxed_str()), - self.offset() - TextSize::new(1), + TextRange::new(self.offset() - TextSize::new(1), self.offset()), )); } @@ -345,7 +345,7 @@ impl<'source> Lexer<'source> { LexicalErrorType::OtherError( "Invalid decimal literal".to_string().into_boxed_str(), ), - self.token_start(), + self.token_range(), ) })?; @@ -370,9 +370,11 @@ impl<'source> Lexer<'source> { // Leading zeros in decimal integer literals are not permitted. return Err(LexicalError::new( LexicalErrorType::OtherError( - "Invalid Token".to_string().into_boxed_str(), + "Invalid decimal integer literal" + .to_string() + .into_boxed_str(), ), - self.token_range().start(), + self.token_range(), )); } value @@ -380,7 +382,7 @@ impl<'source> Lexer<'source> { Err(err) => { return Err(LexicalError::new( LexicalErrorType::OtherError(format!("{err:?}").into_boxed_str()), - self.token_range().start(), + self.token_range(), )) } }; @@ -595,7 +597,7 @@ impl<'source> Lexer<'source> { }; return Err(LexicalError::new( LexicalErrorType::FStringError(error), - self.offset(), + self.token_range(), )); } '\n' | '\r' if !fstring.is_triple_quoted() => { @@ -608,7 +610,7 @@ impl<'source> Lexer<'source> { } return Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::UnterminatedString), - self.offset(), + self.token_range(), )); } '\\' => { @@ -716,13 +718,13 @@ impl<'source> Lexer<'source> { { return Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::UnclosedLbrace), - self.cursor.text_len(), + self.token_range(), )); } } return Err(LexicalError::new( - LexicalErrorType::Eof, - self.cursor.text_len(), + LexicalErrorType::UnclosedStringError, + self.token_range(), )); }; @@ -765,13 +767,13 @@ impl<'source> Lexer<'source> { { return Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::UnclosedLbrace), - self.offset(), + self.token_range(), )); } } return Err(LexicalError::new( LexicalErrorType::StringError, - self.offset(), + self.token_range(), )); }; @@ -806,17 +808,13 @@ impl<'source> Lexer<'source> { LexicalErrorType::FStringError( FStringErrorType::UnclosedLbrace, ), - self.offset() - TextSize::new(1), + self.token_range(), )); } } return Err(LexicalError::new( - LexicalErrorType::OtherError( - "EOL while scanning string literal" - .to_string() - .into_boxed_str(), - ), - self.offset() - TextSize::new(1), + LexicalErrorType::UnclosedStringError, + self.token_range(), )); } Some(ch) if ch == quote => { @@ -866,7 +864,7 @@ impl<'source> Lexer<'source> { self.pending_indentation = Some(indentation); let offset = self.offset(); self.indentations.dedent_one(indentation).map_err(|_| { - LexicalError::new(LexicalErrorType::IndentationError, offset) + LexicalError::new(LexicalErrorType::IndentationError, self.token_range()) })?; return Ok((Tok::Dedent, TextRange::empty(offset))); } @@ -874,7 +872,7 @@ impl<'source> Lexer<'source> { Err(_) => { return Err(LexicalError::new( LexicalErrorType::IndentationError, - self.offset(), + self.token_range(), )); } } @@ -900,7 +898,7 @@ impl<'source> Lexer<'source> { } else { Err(LexicalError::new( LexicalErrorType::UnrecognizedToken { tok: c }, - self.token_start(), + self.token_range(), )) } } else { @@ -924,11 +922,11 @@ impl<'source> Lexer<'source> { if self.cursor.eat_char('\r') { self.cursor.eat_char('\n'); } else if self.cursor.is_eof() { - return Err(LexicalError::new(LexicalErrorType::Eof, self.token_start())); + return Err(LexicalError::new(LexicalErrorType::Eof, self.token_range())); } else if !self.cursor.eat_char('\n') { return Err(LexicalError::new( LexicalErrorType::LineContinuationError, - self.token_start(), + self.token_range(), )); } } @@ -962,11 +960,11 @@ impl<'source> Lexer<'source> { if self.cursor.eat_char('\r') { self.cursor.eat_char('\n'); } else if self.cursor.is_eof() { - return Err(LexicalError::new(LexicalErrorType::Eof, self.token_start())); + return Err(LexicalError::new(LexicalErrorType::Eof, self.token_range())); } else if !self.cursor.eat_char('\n') { return Err(LexicalError::new( LexicalErrorType::LineContinuationError, - self.token_start(), + self.token_range(), )); } indentation = Indentation::root(); @@ -1004,7 +1002,7 @@ impl<'source> Lexer<'source> { self.pending_indentation = Some(indentation); self.indentations.dedent_one(indentation).map_err(|_| { - LexicalError::new(LexicalErrorType::IndentationError, self.offset()) + LexicalError::new(LexicalErrorType::IndentationError, self.token_range()) })?; Some((Tok::Dedent, TextRange::empty(self.offset()))) @@ -1020,7 +1018,7 @@ impl<'source> Lexer<'source> { Err(_) => { return Err(LexicalError::new( LexicalErrorType::IndentationError, - self.offset(), + self.token_range(), )); } }; @@ -1034,7 +1032,7 @@ impl<'source> Lexer<'source> { if self.nesting > 0 { // Reset the nesting to avoid going into infinite loop. self.nesting = 0; - return Err(LexicalError::new(LexicalErrorType::Eof, self.offset())); + return Err(LexicalError::new(LexicalErrorType::Eof, self.token_range())); } // Next, insert a trailing newline, if required. @@ -1201,7 +1199,7 @@ impl<'source> Lexer<'source> { if fstring.nesting() == self.nesting { return Err(LexicalError::new( LexicalErrorType::FStringError(FStringErrorType::SingleRbrace), - self.token_start(), + self.token_range(), )); } fstring.try_end_format_spec(self.nesting); @@ -1295,7 +1293,7 @@ impl<'source> Lexer<'source> { return Err(LexicalError::new( LexicalErrorType::UnrecognizedToken { tok: c }, - self.token_start(), + self.token_range(), )); } }; @@ -1359,12 +1357,12 @@ pub struct LexicalError { /// The type of error that occurred. error: LexicalErrorType, /// The location of the error. - location: TextSize, + location: TextRange, } impl LexicalError { /// Creates a new `LexicalError` with the given error type and location. - pub fn new(error: LexicalErrorType, location: TextSize) -> Self { + pub fn new(error: LexicalErrorType, location: TextRange) -> Self { Self { error, location } } @@ -1376,7 +1374,7 @@ impl LexicalError { self.error } - pub fn location(&self) -> TextSize { + pub fn location(&self) -> TextRange { self.location } } @@ -1401,7 +1399,7 @@ impl std::fmt::Display for LexicalError { f, "{} at byte offset {}", self.error(), - u32::from(self.location()) + u32::from(self.location().start()) ) } } @@ -1416,9 +1414,14 @@ pub enum LexicalErrorType { // to use the `UnicodeError` variant instead. #[doc(hidden)] StringError, - // TODO: Should take a start/end position to report. + /// A string literal without the closing quote. + UnclosedStringError, /// Decoding of a unicode escape sequence in a string literal failed. UnicodeError, + /// Missing the `{` for unicode escape sequence. + MissingUnicodeLbrace, + /// Missing the `}` for unicode escape sequence. + MissingUnicodeRbrace, /// The nesting of brackets/braces/parentheses is not balanced. NestingError, /// The indentation is not consistent. @@ -1495,6 +1498,15 @@ impl std::fmt::Display for LexicalErrorType { LexicalErrorType::Eof => write!(f, "unexpected EOF while parsing"), LexicalErrorType::AssignmentError => write!(f, "invalid assignment target"), LexicalErrorType::OtherError(msg) => write!(f, "{msg}"), + LexicalErrorType::UnclosedStringError => { + write!(f, "missing closing quote in string literal") + } + LexicalErrorType::MissingUnicodeLbrace => { + write!(f, "Missing `{{` in Unicode escape sequence") + } + LexicalErrorType::MissingUnicodeRbrace => { + write!(f, "Missing `}}` in Unicode escape sequence") + } } } } @@ -2301,7 +2313,6 @@ f"{(lambda x:{x})}" assert_eq!(lex_fstring_error("f'{a:b}}'"), SingleRbrace); assert_eq!(lex_fstring_error("f'{3:}}>10}'"), SingleRbrace); assert_eq!(lex_fstring_error(r"f'\{foo}\}'"), SingleRbrace); - assert_eq!(lex_fstring_error("f'{'"), UnclosedLbrace); assert_eq!(lex_fstring_error("f'{foo!r'"), UnclosedLbrace); assert_eq!(lex_fstring_error("f'{foo='"), UnclosedLbrace); @@ -2336,7 +2347,7 @@ f"{(lambda x:{x})}" error: FStringError( UnclosedLbrace, ), - location: 4, + location: 3..4, } "###); @@ -2345,7 +2356,7 @@ f"{(lambda x:{x})}" error: FStringError( UnclosedLbrace, ), - location: 6, + location: 3..6, } "###); } diff --git a/crates/ruff_python_parser/src/lib.rs b/crates/ruff_python_parser/src/lib.rs index 7c9c5402fb442..3d77f346ddf1a 100644 --- a/crates/ruff_python_parser/src/lib.rs +++ b/crates/ruff_python_parser/src/lib.rs @@ -109,28 +109,226 @@ //! [parsing]: https://en.wikipedia.org/wiki/Parsing //! [lexer]: crate::lexer -pub use parser::{ - parse, parse_expression, parse_expression_starts_at, parse_program, parse_starts_at, - parse_suite, parse_tokens, ParseError, ParseErrorType, -}; -use ruff_python_ast::{Mod, PySourceType, Suite}; -pub use string::FStringErrorType; +use std::cell::Cell; + +pub use error::{FStringErrorType, ParseError, ParseErrorType}; +use lexer::{lex, lex_starts_at}; +pub use parser::Program; +use ruff_python_ast::{Expr, Mod, ModModule, PySourceType, Suite}; +use ruff_text_size::TextSize; pub use token::{StringKind, Tok, TokenKind}; use crate::lexer::LexResult; -mod context; -mod function; +mod error; mod invalid; -// Skip flattening lexer to distinguish from full ruff_python_parser +mod lalrpop; pub mod lexer; mod parser; mod soft_keywords; mod string; mod token; +mod token_set; mod token_source; pub mod typing; +thread_local! { + static NEW_PARSER: Cell = Cell::new(std::env::var("NEW_PARSER").is_ok()); +} + +/// Controls whether the current thread uses the new hand written or the old lalrpop based parser. +/// +/// Uses the new hand written parser if `use_new_parser` is true. +/// +/// Defaults to use the new handwritten parser if the environment variable `NEW_PARSER` is set. +pub fn set_new_parser(use_new_parser: bool) { + NEW_PARSER.set(use_new_parser); +} + +/// Parse a full Python program usually consisting of multiple lines. +/// +/// This is a convenience function that can be used to parse a full Python program without having to +/// specify the [`Mode`] or the location. It is probably what you want to use most of the time. +/// +/// # Example +/// +/// For example, parsing a simple function definition and a call to that function: +/// +/// ``` +/// use ruff_python_parser as parser; +/// let source = r#" +/// def foo(): +/// return 42 +/// +/// print(foo()) +/// "#; +/// let program = parser::parse_program(source); +/// assert!(program.is_ok()); +/// ``` +pub fn parse_program(source: &str) -> Result { + let lexer = lex(source, Mode::Module); + match parse_tokens(lexer.collect(), source, Mode::Module)? { + Mod::Module(m) => Ok(m), + Mod::Expression(_) => unreachable!("Mode::Module doesn't return other variant"), + } +} + +pub fn parse_suite(source: &str) -> Result { + parse_program(source).map(|m| m.body) +} + +/// Parses a single Python expression. +/// +/// This convenience function can be used to parse a single expression without having to +/// specify the Mode or the location. +/// +/// # Example +/// +/// For example, parsing a single expression denoting the addition of two numbers: +/// +/// ``` +/// use ruff_python_parser as parser; +/// let expr = parser::parse_expression("1 + 2"); +/// +/// assert!(expr.is_ok()); +/// +/// ``` +pub fn parse_expression(source: &str) -> Result { + let lexer = lex(source, Mode::Expression).collect(); + match parse_tokens(lexer, source, Mode::Expression)? { + Mod::Expression(expression) => Ok(*expression.body), + Mod::Module(_m) => unreachable!("Mode::Expression doesn't return other variant"), + } +} + +/// Parses a Python expression from a given location. +/// +/// This function allows to specify the location of the expression in the source code, other than +/// that, it behaves exactly like [`parse_expression`]. +/// +/// # Example +/// +/// Parsing a single expression denoting the addition of two numbers, but this time specifying a different, +/// somewhat silly, location: +/// +/// ``` +/// use ruff_python_parser::{parse_expression_starts_at}; +/// # use ruff_text_size::TextSize; +/// +/// let expr = parse_expression_starts_at("1 + 2", TextSize::from(400)); +/// assert!(expr.is_ok()); +/// ``` +pub fn parse_expression_starts_at(source: &str, offset: TextSize) -> Result { + let lexer = lex_starts_at(source, Mode::Module, offset).collect(); + match parse_tokens(lexer, source, Mode::Expression)? { + Mod::Expression(expression) => Ok(*expression.body), + Mod::Module(_m) => unreachable!("Mode::Expression doesn't return other variant"), + } +} + +/// Parse the given Python source code using the specified [`Mode`]. +/// +/// This function is the most general function to parse Python code. Based on the [`Mode`] supplied, +/// it can be used to parse a single expression, a full Python program, an interactive expression +/// or a Python program containing IPython escape commands. +/// +/// # Example +/// +/// If we want to parse a simple expression, we can use the [`Mode::Expression`] mode during +/// parsing: +/// +/// ``` +/// use ruff_python_parser::{Mode, parse}; +/// +/// let expr = parse("1 + 2", Mode::Expression); +/// assert!(expr.is_ok()); +/// ``` +/// +/// Alternatively, we can parse a full Python program consisting of multiple lines: +/// +/// ``` +/// use ruff_python_parser::{Mode, parse}; +/// +/// let source = r#" +/// class Greeter: +/// +/// def greet(self): +/// print("Hello, world!") +/// "#; +/// let program = parse(source, Mode::Module); +/// assert!(program.is_ok()); +/// ``` +/// +/// Additionally, we can parse a Python program containing IPython escapes: +/// +/// ``` +/// use ruff_python_parser::{Mode, parse}; +/// +/// let source = r#" +/// %timeit 1 + 2 +/// ?str.replace +/// !ls +/// "#; +/// let program = parse(source, Mode::Ipython); +/// assert!(program.is_ok()); +/// ``` +pub fn parse(source: &str, mode: Mode) -> Result { + let lxr = lexer::lex(source, mode); + parse_tokens(lxr.collect(), source, mode) +} + +/// Parse the given Python source code using the specified [`Mode`] and [`TextSize`]. +/// +/// This function allows to specify the location of the the source code, other than +/// that, it behaves exactly like [`parse`]. +/// +/// # Example +/// +/// ``` +/// # use ruff_text_size::TextSize; +/// use ruff_python_parser::{Mode, parse_starts_at}; +/// +/// let source = r#" +/// def fib(i): +/// a, b = 0, 1 +/// for _ in range(i): +/// a, b = b, a + b +/// return a +/// +/// print(fib(42)) +/// "#; +/// let program = parse_starts_at(source, Mode::Module, TextSize::from(0)); +/// assert!(program.is_ok()); +/// ``` +pub fn parse_starts_at(source: &str, mode: Mode, offset: TextSize) -> Result { + let lxr = lexer::lex_starts_at(source, mode, offset); + parse_tokens(lxr.collect(), source, mode) +} + +/// Parse an iterator of [`LexResult`]s using the specified [`Mode`]. +/// +/// This could allow you to perform some preprocessing on the tokens before parsing them. +/// +/// # Example +/// +/// As an example, instead of parsing a string, we can parse a list of tokens after we generate +/// them using the [`lexer::lex`] function: +/// +/// ``` +/// use ruff_python_parser::{lexer::lex, Mode, parse_tokens}; +/// +/// let source = "1 + 2"; +/// let expr = parse_tokens(lex(source, Mode::Expression).collect(), source, Mode::Expression); +/// assert!(expr.is_ok()); +/// ``` +pub fn parse_tokens(tokens: Vec, source: &str, mode: Mode) -> Result { + if NEW_PARSER.get() { + crate::parser::parse_tokens(tokens, source, mode) + } else { + crate::lalrpop::parse_tokens(tokens, source, mode) + } +} + /// Collect tokens up to and including the first error. pub fn tokenize(contents: &str, mode: Mode) -> Vec { let mut tokens: Vec = allocate_tokens_vec(contents); @@ -248,28 +446,3 @@ impl std::fmt::Display for ModeParseError { write!(f, r#"mode must be "exec", "eval", "ipython", or "single""#) } } - -#[rustfmt::skip] -#[allow(unreachable_pub)] -#[allow(clippy::type_complexity)] -#[allow(clippy::extra_unused_lifetimes)] -#[allow(clippy::needless_lifetimes)] -#[allow(clippy::unused_self)] -#[allow(clippy::cast_sign_loss)] -#[allow(clippy::default_trait_access)] -#[allow(clippy::let_unit_value)] -#[allow(clippy::just_underscores_and_digits)] -#[allow(clippy::no_effect_underscore_binding)] -#[allow(clippy::trivially_copy_pass_by_ref)] -#[allow(clippy::option_option)] -#[allow(clippy::unnecessary_wraps)] -#[allow(clippy::uninlined_format_args)] -#[allow(clippy::cloned_instead_of_copied)] -mod python { - - #[cfg(feature = "lalrpop")] - include!(concat!(env!("OUT_DIR"), "/src/python.rs")); - - #[cfg(not(feature = "lalrpop"))] - include!("python.rs"); -} diff --git a/crates/ruff_python_parser/src/parser/expression.rs b/crates/ruff_python_parser/src/parser/expression.rs new file mode 100644 index 0000000000000..7f3adc6a70885 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/expression.rs @@ -0,0 +1,1702 @@ +use std::ops::Deref; + +use ruff_python_ast::{ + self as ast, BoolOp, CmpOp, ConversionFlag, Expr, ExprContext, FStringElement, Number, + Operator, UnaryOp, +}; +use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; + +use crate::parser::helpers::token_kind_to_cmp_op; +use crate::parser::progress::ParserProgress; +use crate::parser::{helpers, FunctionKind, Parser, ParserCtxFlags, EXPR_SET, NEWLINE_EOF_SET}; +use crate::string::{ + concatenated_strings, parse_fstring_literal_element, parse_string_literal, StringType, +}; +use crate::token_set::TokenSet; +use crate::{FStringErrorType, Mode, ParseErrorType, Tok, TokenKind}; + +/// Tokens that can appear after an expression. +/// FIXME: this isn't exhaustive. +const END_EXPR_SET: TokenSet = TokenSet::new([ + TokenKind::Newline, + TokenKind::Semi, + TokenKind::Colon, + TokenKind::EndOfFile, + TokenKind::Rbrace, + TokenKind::Rsqb, + TokenKind::Rpar, + TokenKind::Comma, + TokenKind::Dedent, + TokenKind::Else, + TokenKind::As, + TokenKind::From, + TokenKind::For, + TokenKind::Async, + TokenKind::In, + TokenKind::Equal, +]); + +impl<'src> Parser<'src> { + pub(super) fn at_expr(&mut self) -> bool { + self.at_ts(EXPR_SET) + } + + /// Parses every Python expression. + /// Matches the `expressions` rule in the Python grammar. + pub(super) fn parse_expression(&mut self) -> ParsedExpr { + let start = self.node_start(); + let parsed_expr = self.parse_conditional_expression_or_higher(); + + if self.at(TokenKind::Comma) { + Expr::Tuple(self.parse_tuple_expression( + parsed_expr.expr, + start, + false, + Parser::parse_conditional_expression_or_higher, + )) + .into() + } else { + parsed_expr + } + } + + /// Parses every Python expression except unparenthesized tuple. + /// Matches the `named_expression` rule in the Python grammar. + /// + /// NOTE: If you have expressions separated by commas and want to parse them individually, + /// instead of a tuple, use this function! + pub(super) fn parse_named_expression_or_higher(&mut self) -> ParsedExpr { + let start = self.node_start(); + let parsed_expr = self.parse_conditional_expression_or_higher(); + + if self.at(TokenKind::ColonEqual) { + Expr::NamedExpr(self.parse_named_expression(parsed_expr.expr, start)).into() + } else { + parsed_expr + } + } + + /// Parses every Python expression except unparenthesized tuple and named expressions. + /// Matches the `expression` rule in the Python grammar. + /// + /// NOTE: If you have expressions separated by commas and want to parse them individually, + /// instead of a tuple, use this function! + pub(super) fn parse_conditional_expression_or_higher(&mut self) -> ParsedExpr { + let start = self.node_start(); + let parsed_expr = self.parse_simple_expression(); + + if self.at(TokenKind::If) { + Expr::IfExp(self.parse_if_expression(parsed_expr.expr, start)).into() + } else { + parsed_expr + } + } + + /// Parses every Python expression except unparenthesized tuples, named expressions, and `if` expression. + /// This is a combination of the `disjunction` and `starred_expression` rules of the Python + /// grammar. + /// + /// When parsing an AST node that only uses one of the rules (`disjunction` or `starred_expression`), + /// you need to **explicitly** check if an invalid node for that AST node was parsed. Check the + /// `parse_yield_from_expression` function for an example of this situation. + fn parse_simple_expression(&mut self) -> ParsedExpr { + self.parse_expression_with_precedence(Precedence::Initial) + } + + /// Binding powers of operators for a Pratt parser. + /// + /// See + fn current_op(&mut self) -> (Precedence, TokenKind, Associativity) { + const NOT_AN_OP: (Precedence, TokenKind, Associativity) = + (Precedence::Unknown, TokenKind::Unknown, Associativity::Left); + let kind = self.current_kind(); + + match kind { + TokenKind::Or => (Precedence::Or, kind, Associativity::Left), + TokenKind::And => (Precedence::And, kind, Associativity::Left), + TokenKind::Not if self.peek_nth(1) == TokenKind::In => ( + Precedence::ComparisonsMembershipIdentity, + kind, + Associativity::Left, + ), + TokenKind::Is + | TokenKind::In + | TokenKind::EqEqual + | TokenKind::NotEqual + | TokenKind::Less + | TokenKind::LessEqual + | TokenKind::Greater + | TokenKind::GreaterEqual => ( + Precedence::ComparisonsMembershipIdentity, + kind, + Associativity::Left, + ), + TokenKind::Vbar => (Precedence::BitOr, kind, Associativity::Left), + TokenKind::CircumFlex => (Precedence::BitXor, kind, Associativity::Left), + TokenKind::Amper => (Precedence::BitAnd, kind, Associativity::Left), + TokenKind::LeftShift | TokenKind::RightShift => { + (Precedence::LeftRightShift, kind, Associativity::Left) + } + TokenKind::Plus | TokenKind::Minus => (Precedence::AddSub, kind, Associativity::Left), + TokenKind::Star + | TokenKind::Slash + | TokenKind::DoubleSlash + | TokenKind::Percent + | TokenKind::At => (Precedence::MulDivRemain, kind, Associativity::Left), + TokenKind::DoubleStar => (Precedence::Exponent, kind, Associativity::Right), + _ => NOT_AN_OP, + } + } + + /// Parses expression with binding power of at least bp. + /// + /// Uses the Pratt parser algorithm. + /// See + fn parse_expression_with_precedence(&mut self, previous_precedence: Precedence) -> ParsedExpr { + let start = self.node_start(); + let mut lhs = self.parse_lhs_expression(); + + let mut progress = ParserProgress::default(); + + loop { + progress.assert_progressing(self); + + let (current_precedence, op, associativity) = self.current_op(); + if current_precedence < previous_precedence { + break; + } + + // Don't parse a `CompareExpr` if we are parsing a `Comprehension` or `ForStmt` + if op.is_compare_operator() && self.has_ctx(ParserCtxFlags::FOR_TARGET) { + break; + } + + let op_bp = match associativity { + Associativity::Left => current_precedence.increment_precedence(), + Associativity::Right => current_precedence, + }; + + self.bump(op); + + // We need to create a dedicated node for boolean operations, + // even though boolean operations are infix. + if op.is_bool_operator() { + lhs = + Expr::BoolOp(self.parse_bool_operation_expression(lhs.expr, start, op, op_bp)) + .into(); + continue; + } + + // Same here as well + if op.is_compare_operator() { + lhs = + Expr::Compare(self.parse_compare_expression(lhs.expr, start, op, op_bp)).into(); + continue; + } + + let rhs = self.parse_expression_with_precedence(op_bp); + + lhs.expr = Expr::BinOp(ast::ExprBinOp { + left: Box::new(lhs.expr), + op: Operator::try_from(op).unwrap(), + right: Box::new(rhs.expr), + range: self.node_range(start), + }); + } + + lhs + } + + pub(super) fn parse_lhs_expression(&mut self) -> ParsedExpr { + let start = self.node_start(); + let mut lhs = match self.current_kind() { + TokenKind::Plus | TokenKind::Minus | TokenKind::Not | TokenKind::Tilde => { + Expr::UnaryOp(self.parse_unary_expression()).into() + } + TokenKind::Star => Expr::Starred(self.parse_starred_expression()).into(), + TokenKind::Await => Expr::Await(self.parse_await_expression()).into(), + TokenKind::Lambda => Expr::Lambda(self.parse_lambda_expr()).into(), + _ => self.parse_atom(), + }; + + if self.is_current_token_postfix() { + lhs = self.parse_postfix_expression(lhs.expr, start).into(); + } + + lhs + } + + pub(super) fn parse_name(&mut self) -> ast::ExprName { + let identifier = self.parse_identifier(); + + ast::ExprName { + range: identifier.range, + id: identifier.id, + ctx: ExprContext::Load, + } + } + + pub(super) fn parse_identifier(&mut self) -> ast::Identifier { + let range = self.current_range(); + + if self.at(TokenKind::Name) { + let (Tok::Name { name }, _) = self.next_token() else { + unreachable!(); + }; + ast::Identifier { + id: name.to_string(), + range, + } + } else { + if self.current_kind().is_keyword() { + let (tok, range) = self.next_token(); + self.add_error( + ParseErrorType::OtherError(format!( + "Identifier expected. '{tok}' is a keyword that cannot be used here." + )), + range, + ); + + ast::Identifier { + id: tok.to_string(), + range, + } + } else { + self.add_error( + ParseErrorType::OtherError("expecting an identifier".into()), + range, + ); + ast::Identifier { + id: String::new(), + range: self.missing_node_range(), + } + } + } + } + + /// See: + fn parse_atom(&mut self) -> ParsedExpr { + let start = self.node_start(); + + let lhs = match self.current_kind() { + TokenKind::Float => { + let (Tok::Float { value }, _) = self.bump(TokenKind::Float) else { + unreachable!() + }; + + Expr::NumberLiteral(ast::ExprNumberLiteral { + value: Number::Float(value), + range: self.node_range(start), + }) + } + TokenKind::Complex => { + let (Tok::Complex { real, imag }, _) = self.bump(TokenKind::Complex) else { + unreachable!() + }; + Expr::NumberLiteral(ast::ExprNumberLiteral { + value: Number::Complex { real, imag }, + range: self.node_range(start), + }) + } + TokenKind::Int => { + let (Tok::Int { value }, _) = self.bump(TokenKind::Int) else { + unreachable!() + }; + Expr::NumberLiteral(ast::ExprNumberLiteral { + value: Number::Int(value), + range: self.node_range(start), + }) + } + TokenKind::True => { + self.bump(TokenKind::True); + Expr::BooleanLiteral(ast::ExprBooleanLiteral { + value: true, + range: self.node_range(start), + }) + } + TokenKind::False => { + self.bump(TokenKind::False); + Expr::BooleanLiteral(ast::ExprBooleanLiteral { + value: false, + range: self.node_range(start), + }) + } + TokenKind::None => { + self.bump(TokenKind::None); + Expr::NoneLiteral(ast::ExprNoneLiteral { + range: self.node_range(start), + }) + } + TokenKind::Ellipsis => { + self.bump(TokenKind::Ellipsis); + Expr::EllipsisLiteral(ast::ExprEllipsisLiteral { + range: self.node_range(start), + }) + } + TokenKind::Name => Expr::Name(self.parse_name()), + TokenKind::EscapeCommand => { + let (Tok::IpyEscapeCommand { value, kind }, _) = + self.bump(TokenKind::EscapeCommand) + else { + unreachable!() + }; + + let command = ast::ExprIpyEscapeCommand { + range: self.node_range(start), + kind, + value, + }; + + if self.mode != Mode::Ipython { + self.add_error( + ParseErrorType::OtherError( + "IPython escape commands are only allowed in `Mode::Ipython`".into(), + ), + &command, + ); + } + + Expr::IpyEscapeCommand(command) + } + TokenKind::String => self.parse_string_expression(), + TokenKind::FStringStart => self.parse_fstring_expression(), + TokenKind::Lpar => { + return self.parse_parenthesized_expression(); + } + + TokenKind::Lsqb => self.parse_list_like_expression(), + TokenKind::Lbrace => self.parse_set_or_dict_like_expression(), + TokenKind::Yield => self.parse_yield_expression(), + + kind => { + if kind.is_keyword() { + Expr::Name(self.parse_name()) + } else { + self.add_error( + ParseErrorType::OtherError("Expression expected.".to_string()), + self.current_range(), + ); + Expr::Name(ast::ExprName { + range: self.missing_node_range(), + id: String::new(), + ctx: ExprContext::Load, + }) + } + } + }; + + lhs.into() + } + + fn parse_postfix_expression(&mut self, mut lhs: Expr, start: TextSize) -> Expr { + loop { + lhs = match self.current_kind() { + TokenKind::Lpar => Expr::Call(self.parse_call_expression(lhs, start)), + TokenKind::Lsqb => Expr::Subscript(self.parse_subscript_expression(lhs, start)), + TokenKind::Dot => Expr::Attribute(self.parse_attribute_expression(lhs, start)), + _ => break lhs, + }; + } + } + + /// See: + fn parse_call_expression(&mut self, lhs: Expr, start: TextSize) -> ast::ExprCall { + assert_eq!(self.current_kind(), TokenKind::Lpar); + let arguments = self.parse_arguments(); + + ast::ExprCall { + func: Box::new(lhs), + arguments, + range: self.node_range(start), + } + } + + pub(super) fn parse_arguments(&mut self) -> ast::Arguments { + let start = self.node_start(); + + let saved_context = self.set_ctx(ParserCtxFlags::ARGUMENTS); + + let mut args: Vec = vec![]; + let mut keywords: Vec = vec![]; + let mut has_seen_kw_arg = false; + let mut has_seen_kw_unpack = false; + + #[allow(deprecated)] + self.parse_delimited( + true, + TokenKind::Lpar, + TokenKind::Comma, + TokenKind::Rpar, + |parser| { + let argument_start = parser.node_start(); + if parser.at(TokenKind::DoubleStar) { + parser.eat(TokenKind::DoubleStar); + + let value = parser.parse_conditional_expression_or_higher(); + keywords.push(ast::Keyword { + arg: None, + value: value.expr, + range: parser.node_range(argument_start), + }); + + has_seen_kw_unpack = true; + } else { + let start = parser.node_start(); + let mut parsed_expr = parser.parse_named_expression_or_higher(); + + match parser.current_kind() { + TokenKind::Async | TokenKind::For => { + parsed_expr = Expr::GeneratorExp(parser.parse_generator_expression( + parsed_expr.expr, + start, + false, + )) + .into(); + } + _ => {} + } + + if has_seen_kw_unpack && matches!(parsed_expr.expr, Expr::Starred(_)) { + parser.add_error(ParseErrorType::UnpackedArgumentError, &parsed_expr); + } + + if parser.eat(TokenKind::Equal) { + has_seen_kw_arg = true; + let arg = if let Expr::Name(ident_expr) = parsed_expr.expr { + ast::Identifier { + id: ident_expr.id, + range: ident_expr.range, + } + } else { + // FIXME(micha): This recovery looks fishy, it drops the parsed expression. + parser.add_error( + ParseErrorType::OtherError( + "cannot be used as a keyword argument!".to_string(), + ), + &parsed_expr, + ); + ast::Identifier { + id: String::new(), + range: parsed_expr.range(), + } + }; + + let value = parser.parse_conditional_expression_or_higher(); + + keywords.push(ast::Keyword { + arg: Some(arg), + value: value.expr, + range: parser.node_range(argument_start), + }); + } else { + if has_seen_kw_arg + && !(has_seen_kw_unpack || matches!(parsed_expr.expr, Expr::Starred(_))) + { + parser.add_error(ParseErrorType::PositionalArgumentError, &parsed_expr); + } + args.push(parsed_expr.expr); + } + } + }, + ); + self.restore_ctx(ParserCtxFlags::ARGUMENTS, saved_context); + + let arguments = ast::Arguments { + range: self.node_range(start), + args: args.into_boxed_slice(), + keywords: keywords.into_boxed_slice(), + }; + + if let Err(error) = helpers::validate_arguments(&arguments) { + self.add_error(error.error, error.location); + } + + arguments + } + + /// See: + fn parse_subscript_expression( + &mut self, + mut value: Expr, + start: TextSize, + ) -> ast::ExprSubscript { + self.bump(TokenKind::Lsqb); + + // To prevent the `value` context from being `Del` within a `del` statement, + // we set the context as `Load` here. + helpers::set_expr_ctx(&mut value, ExprContext::Load); + + // Create an error when receiving a empty slice to parse, e.g. `l[]` + if !self.at(TokenKind::Colon) && !self.at_expr() { + let slice_range = TextRange::empty(self.current_range().start()); + self.expect(TokenKind::Rsqb); + + let range = self.node_range(start); + self.add_error(ParseErrorType::EmptySlice, range); + #[allow(deprecated)] + return ast::ExprSubscript { + value: Box::new(value), + slice: Box::new(Expr::Invalid(ast::ExprInvalid { + value: self.src_text(slice_range).into(), + range: slice_range, + })), + ctx: ExprContext::Load, + range, + }; + } + + let slice_start = self.node_start(); + let mut slice = self.parse_slice(); + + if self.eat(TokenKind::Comma) { + let mut slices = vec![slice]; + #[allow(deprecated)] + self.parse_separated( + true, + TokenKind::Comma, + TokenSet::new([TokenKind::Rsqb]), + |parser| { + slices.push(parser.parse_slice()); + }, + ); + + slice = Expr::Tuple(ast::ExprTuple { + elts: slices, + ctx: ExprContext::Load, + range: self.node_range(slice_start), + parenthesized: false, + }); + } + + self.expect(TokenKind::Rsqb); + + ast::ExprSubscript { + value: Box::new(value), + slice: Box::new(slice), + ctx: ExprContext::Load, + range: self.node_range(start), + } + } + + /// See: + fn parse_slice(&mut self) -> Expr { + const UPPER_END_SET: TokenSet = + TokenSet::new([TokenKind::Comma, TokenKind::Colon, TokenKind::Rsqb]) + .union(NEWLINE_EOF_SET); + const STEP_END_SET: TokenSet = + TokenSet::new([TokenKind::Comma, TokenKind::Rsqb]).union(NEWLINE_EOF_SET); + + let start = self.node_start(); + + let lower = if self.at_expr() { + let lower = self.parse_named_expression_or_higher(); + if self.at_ts(NEWLINE_EOF_SET.union([TokenKind::Rsqb, TokenKind::Comma].into())) { + return lower.expr; + } + + Some(lower.expr) + } else { + None + }; + + self.expect(TokenKind::Colon); + + let lower = lower.map(Box::new); + let upper = if self.at_ts(UPPER_END_SET) { + None + } else { + Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + }; + + let step = if self.eat(TokenKind::Colon) { + if self.at_ts(STEP_END_SET) { + None + } else { + Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + } + } else { + None + }; + + Expr::Slice(ast::ExprSlice { + range: self.node_range(start), + lower, + upper, + step, + }) + } + + fn parse_unary_expression(&mut self) -> ast::ExprUnaryOp { + let start = self.node_start(); + + let op = UnaryOp::try_from(self.current_kind()) + .expect("Expected operator to be a unary operator token."); + self.bump(self.current_kind()); + + let operand = if matches!(op, UnaryOp::Not) { + self.parse_expression_with_precedence(Precedence::Not) + } else { + // plus, minus and tilde + self.parse_expression_with_precedence(Precedence::PosNegBitNot) + }; + + ast::ExprUnaryOp { + op, + operand: Box::new(operand.expr), + range: self.node_range(start), + } + } + + pub(super) fn parse_attribute_expression( + &mut self, + value: Expr, + start: TextSize, + ) -> ast::ExprAttribute { + self.bump(TokenKind::Dot); + + let attr = self.parse_identifier(); + + ast::ExprAttribute { + value: Box::new(value), + attr, + ctx: ExprContext::Load, + range: self.node_range(start), + } + } + + fn parse_bool_operation_expression( + &mut self, + lhs: Expr, + start: TextSize, + op: TokenKind, + op_bp: Precedence, + ) -> ast::ExprBoolOp { + let mut values = vec![lhs]; + let mut progress = ParserProgress::default(); + + // Keep adding `expr` to `values` until we see a different + // boolean operation than `op`. + loop { + progress.assert_progressing(self); + let parsed_expr = self.parse_expression_with_precedence(op_bp); + values.push(parsed_expr.expr); + + if self.current_kind() != op { + break; + } + + self.next_token(); + } + + ast::ExprBoolOp { + values, + op: BoolOp::try_from(op).unwrap(), + range: self.node_range(start), + } + } + + fn parse_compare_expression( + &mut self, + lhs: Expr, + start: TextSize, + op: TokenKind, + op_bp: Precedence, + ) -> ast::ExprCompare { + let mut comparators = vec![]; + let op = token_kind_to_cmp_op([op, self.current_kind()]).unwrap(); + let mut ops = vec![op]; + + if matches!(op, CmpOp::IsNot | CmpOp::NotIn) { + self.next_token(); + } + + let mut progress = ParserProgress::default(); + + loop { + progress.assert_progressing(self); + let parsed_expr = self.parse_expression_with_precedence(op_bp); + comparators.push(parsed_expr.expr); + + if let Ok(op) = token_kind_to_cmp_op([self.current_kind(), self.peek_nth(1)]) { + if matches!(op, CmpOp::IsNot | CmpOp::NotIn) { + self.next_token(); + } + + ops.push(op); + } else { + break; + } + + self.next_token(); + } + + ast::ExprCompare { + left: Box::new(lhs), + ops: ops.into_boxed_slice(), + comparators: comparators.into_boxed_slice(), + range: self.node_range(start), + } + } + + pub(super) fn parse_string_expression(&mut self) -> Expr { + let start = self.node_start(); + let mut strings = vec![]; + let mut progress = ParserProgress::default(); + + while self.at(TokenKind::String) { + progress.assert_progressing(self); + let ( + Tok::String { + value, + kind, + triple_quoted, + }, + tok_range, + ) = self.bump(TokenKind::String) + else { + unreachable!() + }; + + match parse_string_literal(value, kind, triple_quoted, tok_range) { + Ok(string) => { + strings.push(string); + } + Err(error) => { + strings.push(StringType::Invalid(ast::StringLiteral { + value: self.src_text(tok_range).to_string().into_boxed_str(), + range: tok_range, + unicode: kind.is_unicode(), + })); + let location = error.location(); + self.add_error(ParseErrorType::Lexical(error.into_error()), location); + } + } + } + + // This handles the case where the string is implicit concatenated with + // a fstring, e.g., `"hello " f"{x}"`. + if self.at(TokenKind::FStringStart) { + self.handle_implicit_concatenated_strings(&mut strings); + } + + let range = self.node_range(start); + + if strings.len() == 1 { + return match strings.pop().unwrap() { + StringType::Str(string) => Expr::StringLiteral(ast::ExprStringLiteral { + value: ast::StringLiteralValue::single(string), + range, + }), + StringType::Bytes(bytes) => { + // TODO(micha): Is this valid? I thought string and byte literals can't be concatenated? Maybe not a syntax error? + Expr::BytesLiteral(ast::ExprBytesLiteral { + value: ast::BytesLiteralValue::single(bytes), + range, + }) + } + #[allow(deprecated)] + StringType::Invalid(invalid) => Expr::Invalid(ast::ExprInvalid { + value: invalid.value.to_string(), + range, + }), + StringType::FString(_) => unreachable!(), + }; + } + + concatenated_strings(strings, range).unwrap_or_else(|error| { + let location = error.location(); + self.add_error(ParseErrorType::Lexical(error.into_error()), location); + #[allow(deprecated)] + Expr::Invalid(ast::ExprInvalid { + value: self.src_text(location).into(), + range: location, + }) + }) + } + + /// Handles implicit concatenated f-strings, e.g. `f"{x}" f"hello"`, and + /// implicit concatenated f-strings with strings, e.g. `f"{x}" "xyz" f"{x}"`. + fn handle_implicit_concatenated_strings(&mut self, strings: &mut Vec) { + let mut progress = ParserProgress::default(); + + loop { + progress.assert_progressing(self); + let start = self.node_start(); + + if self.at(TokenKind::FStringStart) { + strings.push(StringType::FString(self.parse_fstring())); + } else if self.at(TokenKind::String) { + let ( + Tok::String { + value, + kind, + triple_quoted, + }, + _, + ) = self.next_token() + else { + unreachable!() + }; + + let range = self.node_range(start); + + match parse_string_literal(value, kind, triple_quoted, range) { + Ok(string) => { + strings.push(string); + } + Err(error) => { + strings.push(StringType::Invalid(ast::StringLiteral { + value: self.src_text(error.location()).to_string().into_boxed_str(), + range, + unicode: kind.is_unicode(), + })); + let location = error.location(); + self.add_error(ParseErrorType::Lexical(error.into_error()), location); + } + } + } else { + break; + } + } + } + + fn parse_fstring_expression(&mut self) -> Expr { + const FSTRING_SET: TokenSet = TokenSet::new([TokenKind::FStringStart, TokenKind::String]); + + let start = self.node_start(); + let fstring = self.parse_fstring(); + + if !self.at_ts(FSTRING_SET) { + return Expr::FString(ast::ExprFString { + value: ast::FStringValue::single(fstring), + range: self.node_range(start), + }); + } + + let mut strings = vec![StringType::FString(fstring)]; + self.handle_implicit_concatenated_strings(&mut strings); + + let range = self.node_range(start); + + concatenated_strings(strings, range).unwrap_or_else(|error| { + let location = error.location(); + self.add_error(ParseErrorType::Lexical(error.into_error()), location); + + #[allow(deprecated)] + Expr::Invalid(ast::ExprInvalid { + value: self.src_text(location).into(), + range: location, + }) + }) + } + + fn parse_fstring(&mut self) -> ast::FString { + let start = self.node_start(); + + self.bump(TokenKind::FStringStart); + let elements = self.parse_fstring_elements(); + + self.expect(TokenKind::FStringEnd); + + ast::FString { + elements, + range: self.node_range(start), + } + } + + fn parse_fstring_elements(&mut self) -> Vec { + const FSTRING_END_SET: TokenSet = + TokenSet::new([TokenKind::FStringEnd, TokenKind::Rbrace]).union(NEWLINE_EOF_SET); + let mut elements = vec![]; + + let mut progress = ParserProgress::default(); + + while !self.at_ts(FSTRING_END_SET) { + progress.assert_progressing(self); + + let element = match self.current_kind() { + TokenKind::Lbrace => { + FStringElement::Expression(self.parse_fstring_expression_element()) + } + TokenKind::FStringMiddle => { + let (Tok::FStringMiddle { value, is_raw, .. }, range) = self.next_token() + else { + unreachable!() + }; + let fstring_literal = parse_fstring_literal_element(value, is_raw, range) + .unwrap_or_else(|lex_error| { + let location = lex_error.location(); + self.add_error( + ParseErrorType::Lexical(lex_error.into_error()), + location, + ); + #[allow(deprecated)] + ast::FStringElement::Invalid(ast::FStringInvalidElement { + value: self.src_text(location).into(), + range: location, + }) + }); + fstring_literal + } + // `Invalid` tokens are created when there's a lexical error, so + // we ignore it here to avoid creating unexpected token errors + TokenKind::Unknown => { + self.next_token(); + continue; + } + // Handle an unexpected token + _ => { + let (tok, range) = self.next_token(); + self.add_error( + ParseErrorType::OtherError(format!("f-string: unexpected token `{tok:?}`")), + range, + ); + continue; + } + }; + elements.push(element); + } + + elements + } + + fn parse_fstring_expression_element(&mut self) -> ast::FStringExpressionElement { + let range = self.current_range(); + + let has_open_brace = self.eat(TokenKind::Lbrace); + let value = self.parse_expression(); + if !value.is_parenthesized && matches!(value.expr, Expr::Lambda(_)) { + self.add_error( + ParseErrorType::FStringError(FStringErrorType::LambdaWithoutParentheses), + value.range(), + ); + } + let debug_text = if self.eat(TokenKind::Equal) { + let leading_range = range + .add_start("{".text_len()) + .cover_offset(value.range().start()); + let trailing_range = TextRange::new(value.range().end(), self.current_range().start()); + Some(ast::DebugText { + leading: self.src_text(leading_range).to_string(), + trailing: self.src_text(trailing_range).to_string(), + }) + } else { + None + }; + + let conversion = if self.eat(TokenKind::Exclamation) { + let (_, range) = self.next_token(); + match self.src_text(range) { + "s" => ConversionFlag::Str, + "r" => ConversionFlag::Repr, + "a" => ConversionFlag::Ascii, + _ => { + self.add_error( + ParseErrorType::FStringError(FStringErrorType::InvalidConversionFlag), + range, + ); + ConversionFlag::None + } + } + } else { + ConversionFlag::None + }; + + let format_spec = if self.eat(TokenKind::Colon) { + let spec_start = self.node_start(); + let elements = self.parse_fstring_elements(); + Some(Box::new(ast::FStringFormatSpec { + range: self.node_range(spec_start), + elements, + })) + } else { + None + }; + + let close_brace_range = self.current_range(); + if has_open_brace && !self.eat(TokenKind::Rbrace) { + self.add_error( + ParseErrorType::FStringError(FStringErrorType::UnclosedLbrace), + close_brace_range, + ); + } + + ast::FStringExpressionElement { + expression: Box::new(value.expr), + debug_text, + conversion, + format_spec, + range: range.cover(close_brace_range), + } + } + + /// Parses a list or a list comprehension expression. + fn parse_list_like_expression(&mut self) -> Expr { + let start = self.node_start(); + + self.bump(TokenKind::Lsqb); + + // Nice error message when having a unclosed open bracket `[` + if self.at_ts(NEWLINE_EOF_SET) { + self.add_error( + ParseErrorType::OtherError("missing closing bracket `]`".to_string()), + self.current_range(), + ); + } + + // Return an empty `ListExpr` when finding a `]` right after the `[` + if self.eat(TokenKind::Rsqb) { + return Expr::List(ast::ExprList { + elts: vec![], + ctx: ExprContext::Load, + range: self.node_range(start), + }); + } + + let parsed_expr = self.parse_named_expression_or_higher(); + + match self.current_kind() { + TokenKind::Async | TokenKind::For => { + Expr::ListComp(self.parse_list_comprehension_expression(parsed_expr.expr, start)) + } + _ => Expr::List(self.parse_list_expression(parsed_expr.expr, start)), + } + } + + /// Parses a set, dict, set comprehension, or dict comprehension. + fn parse_set_or_dict_like_expression(&mut self) -> Expr { + let start = self.node_start(); + self.bump(TokenKind::Lbrace); + + // Nice error message when having a unclosed open brace `{` + if self.at_ts(NEWLINE_EOF_SET) { + self.add_error( + ParseErrorType::OtherError("missing closing brace `}`".to_string()), + self.current_range(), + ); + } + + // Return an empty `DictExpr` when finding a `}` right after the `{` + if self.eat(TokenKind::Rbrace) { + return Expr::Dict(ast::ExprDict { + keys: vec![], + values: vec![], + range: self.node_range(start), + }); + } + + if self.eat(TokenKind::DoubleStar) { + // Handle dict unpack + let value = self.parse_conditional_expression_or_higher(); + return Expr::Dict(self.parse_dictionary_expression(None, value.expr, start)); + } + + let key_or_value = self.parse_named_expression_or_higher(); + + match self.current_kind() { + TokenKind::Async | TokenKind::For => { + Expr::SetComp(self.parse_set_comprehension_expression(key_or_value.expr, start)) + } + TokenKind::Colon => { + self.bump(TokenKind::Colon); + let value = self.parse_conditional_expression_or_higher(); + + if matches!(self.current_kind(), TokenKind::Async | TokenKind::For) { + Expr::DictComp(self.parse_dictionary_comprehension_expression( + key_or_value.expr, + value.expr, + start, + )) + } else { + Expr::Dict(self.parse_dictionary_expression( + Some(key_or_value.expr), + value.expr, + start, + )) + } + } + _ => Expr::Set(self.parse_set_expression(key_or_value.expr, start)), + } + } + + /// Parses an expression in parentheses, a tuple expression, or a generator expression. + fn parse_parenthesized_expression(&mut self) -> ParsedExpr { + let start = self.node_start(); + let saved_context = self.set_ctx(ParserCtxFlags::PARENTHESIZED_EXPR); + + self.bump(TokenKind::Lpar); + + // Nice error message when having a unclosed open parenthesis `(` + if self.at_ts(NEWLINE_EOF_SET) { + let range = self.current_range(); + self.add_error( + ParseErrorType::OtherError("missing closing parenthesis `)`".to_string()), + range, + ); + } + + // Return an empty `TupleExpr` when finding a `)` right after the `(` + if self.eat(TokenKind::Rpar) { + self.restore_ctx(ParserCtxFlags::PARENTHESIZED_EXPR, saved_context); + + return Expr::Tuple(ast::ExprTuple { + elts: vec![], + ctx: ExprContext::Load, + range: self.node_range(start), + parenthesized: true, + }) + .into(); + } + + let mut parsed_expr = self.parse_named_expression_or_higher(); + + let parsed = match self.current_kind() { + TokenKind::Comma => { + let tuple = self.parse_tuple_expression( + parsed_expr.expr, + start, + true, + Parser::parse_named_expression_or_higher, + ); + + ParsedExpr { + expr: tuple.into(), + is_parenthesized: false, + } + } + TokenKind::Async | TokenKind::For => { + let generator = Expr::GeneratorExp(self.parse_generator_expression( + parsed_expr.expr, + start, + true, + )); + + ParsedExpr { + expr: generator, + is_parenthesized: false, + } + } + _ => { + self.expect(TokenKind::Rpar); + + parsed_expr.is_parenthesized = true; + parsed_expr + } + }; + + self.restore_ctx(ParserCtxFlags::PARENTHESIZED_EXPR, saved_context); + + parsed + } + const END_SEQUENCE_SET: TokenSet = END_EXPR_SET.remove(TokenKind::Comma); + + /// Parses multiple items separated by a comma into a `TupleExpr` node. + /// Uses `parse_func` to parse each item. + pub(super) fn parse_tuple_expression( + &mut self, + first_element: Expr, + start: TextSize, + parenthesized: bool, + // TODO: I would have expected that `parse_func` is the same depending on whether `parenthesized` is true or not, but that's not the case + // verify precedence. + mut parse_func: impl FnMut(&mut Parser<'src>) -> ParsedExpr, + ) -> ast::ExprTuple { + // In case of the tuple only having one element, we need to cover the + // range of the comma. + if !self.at_ts(Self::END_SEQUENCE_SET) { + self.expect(TokenKind::Comma); + } + + let mut elts = vec![first_element]; + + #[allow(deprecated)] + self.parse_separated(true, TokenKind::Comma, Self::END_SEQUENCE_SET, |parser| { + elts.push(parse_func(parser).expr); + }); + + if parenthesized { + self.expect(TokenKind::Rpar); + } + + ast::ExprTuple { + elts, + ctx: ExprContext::Load, + range: self.node_range(start), + parenthesized, + } + } + + /// See: + fn parse_list_expression(&mut self, first_element: Expr, start: TextSize) -> ast::ExprList { + if !self.at_ts(Self::END_SEQUENCE_SET) { + self.expect(TokenKind::Comma); + } + + let mut elts = vec![first_element]; + + #[allow(deprecated)] + self.parse_separated(true, TokenKind::Comma, Self::END_SEQUENCE_SET, |parser| { + elts.push(parser.parse_named_expression_or_higher().expr); + }); + + self.expect(TokenKind::Rsqb); + + ast::ExprList { + elts, + ctx: ExprContext::Load, + range: self.node_range(start), + } + } + + /// See: + fn parse_set_expression(&mut self, first_element: Expr, start: TextSize) -> ast::ExprSet { + if !self.at_ts(Self::END_SEQUENCE_SET) { + self.expect(TokenKind::Comma); + } + + let mut elts = vec![first_element]; + + #[allow(deprecated)] + self.parse_separated(true, TokenKind::Comma, Self::END_SEQUENCE_SET, |parser| { + elts.push(parser.parse_named_expression_or_higher().expr); + }); + + self.expect(TokenKind::Rbrace); + + ast::ExprSet { + range: self.node_range(start), + elts, + } + } + + /// See: + fn parse_dictionary_expression( + &mut self, + key: Option, + value: Expr, + start: TextSize, + ) -> ast::ExprDict { + if !self.at_ts(Self::END_SEQUENCE_SET) { + self.expect(TokenKind::Comma); + } + + let mut keys = vec![key]; + let mut values = vec![value]; + + #[allow(deprecated)] + self.parse_separated(true, TokenKind::Comma, Self::END_SEQUENCE_SET, |parser| { + if parser.eat(TokenKind::DoubleStar) { + keys.push(None); + } else { + keys.push(Some(parser.parse_conditional_expression_or_higher().expr)); + + parser.expect(TokenKind::Colon); + } + values.push(parser.parse_conditional_expression_or_higher().expr); + }); + + self.expect(TokenKind::Rbrace); + + ast::ExprDict { + range: self.node_range(start), + keys, + values, + } + } + + /// See: + fn parse_comprehension(&mut self) -> ast::Comprehension { + let start = self.node_start(); + + let is_async = self.eat(TokenKind::Async); + + self.bump(TokenKind::For); + + let saved_context = self.set_ctx(ParserCtxFlags::FOR_TARGET); + let mut target = self.parse_expression(); + self.restore_ctx(ParserCtxFlags::FOR_TARGET, saved_context); + + helpers::set_expr_ctx(&mut target.expr, ExprContext::Store); + + self.expect(TokenKind::In); + + let iter = self.parse_simple_expression(); + + let mut ifs = vec![]; + let mut progress = ParserProgress::default(); + + while self.eat(TokenKind::If) { + progress.assert_progressing(self); + ifs.push(self.parse_simple_expression().expr); + } + + ast::Comprehension { + range: self.node_range(start), + target: target.expr, + iter: iter.expr, + ifs, + is_async, + } + } + + fn parse_generators(&mut self) -> Vec { + const GENERATOR_SET: TokenSet = TokenSet::new([TokenKind::For, TokenKind::Async]); + + let mut generators = vec![]; + let mut progress = ParserProgress::default(); + + while self.at_ts(GENERATOR_SET) { + progress.assert_progressing(self); + generators.push(self.parse_comprehension()); + } + + generators + } + + /// See: + fn parse_generator_expression( + &mut self, + element: Expr, + start: TextSize, + in_parentheses: bool, + ) -> ast::ExprGeneratorExp { + let generators = self.parse_generators(); + + if in_parentheses { + self.expect(TokenKind::Rpar); + } + + ast::ExprGeneratorExp { + elt: Box::new(element), + generators, + range: self.node_range(start), + } + } + + fn parse_list_comprehension_expression( + &mut self, + element: Expr, + start: TextSize, + ) -> ast::ExprListComp { + let generators = self.parse_generators(); + + self.expect(TokenKind::Rsqb); + + ast::ExprListComp { + elt: Box::new(element), + generators, + range: self.node_range(start), + } + } + + fn parse_dictionary_comprehension_expression( + &mut self, + key: Expr, + value: Expr, + start: TextSize, + ) -> ast::ExprDictComp { + let generators = self.parse_generators(); + + self.expect(TokenKind::Rbrace); + + ast::ExprDictComp { + key: Box::new(key), + value: Box::new(value), + generators, + range: self.node_range(start), + } + } + + fn parse_set_comprehension_expression( + &mut self, + element: Expr, + start: TextSize, + ) -> ast::ExprSetComp { + let generators = self.parse_generators(); + + self.expect(TokenKind::Rbrace); + + ast::ExprSetComp { + elt: Box::new(element), + generators, + range: self.node_range(start), + } + } + + fn parse_starred_expression(&mut self) -> ast::ExprStarred { + let start = self.node_start(); + self.bump(TokenKind::Star); + let parsed_expr = self.parse_conditional_expression_or_higher(); + + ast::ExprStarred { + value: Box::new(parsed_expr.expr), + ctx: ExprContext::Load, + range: self.node_range(start), + } + } + + /// See: + fn parse_await_expression(&mut self) -> ast::ExprAwait { + let start = self.node_start(); + self.bump(TokenKind::Await); + let parsed_expr = self.parse_expression_with_precedence(Precedence::Await); + + if matches!(parsed_expr.expr, Expr::Starred(_)) { + self.add_error( + ParseErrorType::OtherError( + "starred expression is not allowed in an `await` statement".to_string(), + ), + &parsed_expr, + ); + } + + ast::ExprAwait { + value: Box::new(parsed_expr.expr), + range: self.node_range(start), + } + } + + /// See: + fn parse_yield_expression(&mut self) -> Expr { + let start = self.node_start(); + self.bump(TokenKind::Yield); + + if self.eat(TokenKind::From) { + return self.parse_yield_from_expression(start); + } + + let value = self + .at_expr() + .then(|| Box::new(self.parse_expression().expr)); + + Expr::Yield(ast::ExprYield { + value, + range: self.node_range(start), + }) + } + + /// See: + fn parse_yield_from_expression(&mut self, start: TextSize) -> Expr { + let parsed_expr = self.parse_expression(); + + match &parsed_expr.expr { + Expr::Starred(ast::ExprStarred { value, .. }) => { + // Should we make `expr` an `Expr::Invalid` here? + self.add_error( + ParseErrorType::OtherError( + "starred expression is not allowed in a `yield from` statement".to_string(), + ), + value.as_ref(), + ); + } + Expr::Tuple(tuple) if !tuple.parenthesized => { + // Should we make `expr` an `Expr::Invalid` here? + self.add_error( + ParseErrorType::OtherError( + "unparenthesized tuple is not allowed in a `yield from` statement" + .to_string(), + ), + tuple, + ); + } + _ => {} + } + + Expr::YieldFrom(ast::ExprYieldFrom { + value: Box::new(parsed_expr.expr), + range: self.node_range(start), + }) + } + + fn parse_named_expression(&mut self, mut target: Expr, start: TextSize) -> ast::ExprNamedExpr { + self.bump(TokenKind::ColonEqual); + + if !helpers::is_valid_assignment_target(&target) { + self.add_error(ParseErrorType::NamedAssignmentError, target.range()); + } + helpers::set_expr_ctx(&mut target, ExprContext::Store); + + let value = self.parse_conditional_expression_or_higher(); + + ast::ExprNamedExpr { + target: Box::new(target), + value: Box::new(value.expr), + range: self.node_range(start), + } + } + + /// See: + fn parse_lambda_expr(&mut self) -> ast::ExprLambda { + let start = self.node_start(); + self.bump(TokenKind::Lambda); + + let parameters: Option> = if self.at(TokenKind::Colon) { + None + } else { + Some(Box::new(self.parse_parameters(FunctionKind::Lambda))) + }; + + self.expect(TokenKind::Colon); + + // Check for forbidden tokens in the `lambda`'s body + match self.current_kind() { + TokenKind::Yield => self.add_error( + ParseErrorType::OtherError( + "`yield` not allowed in a `lambda` expression".to_string(), + ), + self.current_range(), + ), + TokenKind::Star => { + self.add_error( + ParseErrorType::OtherError( + "starred expression not allowed in a `lambda` expression".to_string(), + ), + self.current_range(), + ); + } + TokenKind::DoubleStar => { + self.add_error( + ParseErrorType::OtherError( + "double starred expression not allowed in a `lambda` expression" + .to_string(), + ), + self.current_range(), + ); + } + _ => {} + } + + let body = self.parse_conditional_expression_or_higher(); + + ast::ExprLambda { + body: Box::new(body.expr), + parameters, + range: self.node_range(start), + } + } + + fn parse_if_expression(&mut self, body: Expr, start: TextSize) -> ast::ExprIfExp { + self.bump(TokenKind::If); + + let test = self.parse_simple_expression(); + + self.expect(TokenKind::Else); + + let orelse = self.parse_conditional_expression_or_higher(); + + ast::ExprIfExp { + body: Box::new(body), + test: Box::new(test.expr), + orelse: Box::new(orelse.expr), + range: self.node_range(start), + } + } +} + +#[derive(Debug)] +pub(super) struct ParsedExpr { + pub(super) expr: Expr, + pub(super) is_parenthesized: bool, +} + +impl From for ParsedExpr { + #[inline] + fn from(expr: Expr) -> Self { + ParsedExpr { + expr, + is_parenthesized: false, + } + } +} + +impl Deref for ParsedExpr { + type Target = Expr; + + fn deref(&self) -> &Self::Target { + &self.expr + } +} + +impl Ranged for ParsedExpr { + #[inline] + fn range(&self) -> TextRange { + self.expr.range() + } +} + +/// Binding power associativity +enum Associativity { + Left, + Right, +} + +/// Represents the precedence levels for various operators and expressions of Python. +/// Variants at the top have lower precedence and variants at the bottom have +/// higher precedence. +/// +/// Note: Some expressions like if-else, named expression (`:=`), lambda, subscription, +/// slicing, call and attribute reference expressions, that are mentioned in the link +/// below are better handled in other parts of the parser. +/// +/// See: +#[derive(Debug, Ord, Eq, PartialEq, PartialOrd, Copy, Clone)] +enum Precedence { + /// Precedence for an unknown operator. + Unknown, + /// The initital precedence when parsing an expression. + Initial, + /// Precedence of boolean `or` operator. + Or, + /// Precedence of boolean `and` operator. + And, + /// Precedence of boolean `not` unary operator. + Not, + /// Precedence of comparisons operators (`<`, `<=`, `>`, `>=`, `!=`, `==`), + /// memberships tests (`in`, `not in`) and identity tests (`is` `is not`). + ComparisonsMembershipIdentity, + /// Precedence of `Bitwise OR` (`|`) operator. + BitOr, + /// Precedence of `Bitwise XOR` (`^`) operator. + BitXor, + /// Precedence of `Bitwise AND` (`&`) operator. + BitAnd, + /// Precedence of left and right shift operators (`<<`, `>>`). + LeftRightShift, + /// Precedence of addition (`+`) and subtraction (`-`) operators. + AddSub, + /// Precedence of multiplication (`*`), matrix multiplication (`@`), division (`/`), floor + /// division (`//`) and remainder operators (`%`). + MulDivRemain, + /// Precedence of positive (`+`), negative (`-`), `Bitwise NOT` (`~`) unary operators. + PosNegBitNot, + /// Precedence of exponentiation operator (`**`). + Exponent, + /// Precedence of `await` expression. + Await, +} + +impl Precedence { + fn increment_precedence(&self) -> Precedence { + match self { + Precedence::Or => Precedence::And, + Precedence::And => Precedence::Not, + Precedence::Not => Precedence::ComparisonsMembershipIdentity, + Precedence::ComparisonsMembershipIdentity => Precedence::BitOr, + Precedence::BitOr => Precedence::BitXor, + Precedence::BitXor => Precedence::BitAnd, + Precedence::BitAnd => Precedence::LeftRightShift, + Precedence::LeftRightShift => Precedence::AddSub, + Precedence::AddSub => Precedence::MulDivRemain, + Precedence::MulDivRemain => Precedence::PosNegBitNot, + Precedence::PosNegBitNot => Precedence::Exponent, + Precedence::Exponent => Precedence::Await, + // We've reached the highest precedence, we can't increment anymore, + // so return the same precedence. + Precedence::Await => Precedence::Await, + // When this function is invoked, the precedence will never be 'Unknown' or 'Initial'. + // This is due to their lower precedence values, causing them to exit the loop in the + // `parse_expression_with_precedence` function before the execution of this function. + Precedence::Unknown | Precedence::Initial => unreachable!(), + } + } +} diff --git a/crates/ruff_python_parser/src/parser/helpers.rs b/crates/ruff_python_parser/src/parser/helpers.rs new file mode 100644 index 0000000000000..f980dcfcab6b9 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/helpers.rs @@ -0,0 +1,135 @@ +use std::hash::BuildHasherDefault; + +use ast::CmpOp; +use ruff_python_ast::{self as ast, Expr, ExprContext}; +use rustc_hash::FxHashSet; + +use crate::{ParseError, ParseErrorType, TokenKind}; + +/// Set the `ctx` for `Expr::Id`, `Expr::Attribute`, `Expr::Subscript`, `Expr::Starred`, +/// `Expr::Tuple` and `Expr::List`. If `expr` is either `Expr::Tuple` or `Expr::List`, +/// recursively sets the `ctx` for their elements. +pub(super) fn set_expr_ctx(expr: &mut Expr, new_ctx: ExprContext) { + match expr { + Expr::Name(ast::ExprName { ctx, .. }) + | Expr::Attribute(ast::ExprAttribute { ctx, .. }) + | Expr::Subscript(ast::ExprSubscript { ctx, .. }) => *ctx = new_ctx, + Expr::Starred(ast::ExprStarred { value, ctx, .. }) => { + *ctx = new_ctx; + set_expr_ctx(value, new_ctx); + } + Expr::UnaryOp(ast::ExprUnaryOp { operand, .. }) => { + set_expr_ctx(operand, new_ctx); + } + Expr::List(ast::ExprList { elts, ctx, .. }) + | Expr::Tuple(ast::ExprTuple { elts, ctx, .. }) => { + *ctx = new_ctx; + elts.iter_mut() + .for_each(|element| set_expr_ctx(element, new_ctx)); + } + _ => {} + } +} + +/// Check if the given expression is itself or contains an expression that is +/// valid on the left hand side of an assignment. For example, identifiers, +/// starred expressions, attribute expressions, subscript expressions, +/// list and tuple unpacking are valid assignment targets. +pub(super) fn is_valid_assignment_target(expr: &Expr) -> bool { + match expr { + Expr::Starred(ast::ExprStarred { value, .. }) => is_valid_assignment_target(value), + Expr::List(ast::ExprList { elts, .. }) | Expr::Tuple(ast::ExprTuple { elts, .. }) => { + elts.iter().all(is_valid_assignment_target) + } + Expr::Name(_) | Expr::Attribute(_) | Expr::Subscript(_) => true, + _ => false, + } +} + +/// Check if the given expression is itself or contains an expression that is +/// valid on the left hand side of an augmented assignment. For example, identifiers, +/// attribute and subscript expressions are valid augmented assignment targets. +pub(super) fn is_valid_aug_assignment_target(expr: &Expr) -> bool { + matches!( + expr, + Expr::Name(_) | Expr::Attribute(_) | Expr::Subscript(_) + ) +} + +/// Converts a [`TokenKind`] array of size 2 to its correspondent [`CmpOp`]. +pub(super) fn token_kind_to_cmp_op(kind: [TokenKind; 2]) -> Result { + Ok(match kind { + [TokenKind::Is, TokenKind::Not] => CmpOp::IsNot, + [TokenKind::Is, _] => CmpOp::Is, + [TokenKind::In, _] => CmpOp::In, + [TokenKind::EqEqual, _] => CmpOp::Eq, + [TokenKind::Less, _] => CmpOp::Lt, + [TokenKind::Greater, _] => CmpOp::Gt, + [TokenKind::NotEqual, _] => CmpOp::NotEq, + [TokenKind::LessEqual, _] => CmpOp::LtE, + [TokenKind::GreaterEqual, _] => CmpOp::GtE, + [TokenKind::Not, TokenKind::In] => CmpOp::NotIn, + _ => return Err(()), + }) +} + +// Perform validation of function/lambda parameters in a function definition. +pub(super) fn validate_parameters(parameters: &ast::Parameters) -> Result<(), ParseError> { + let mut all_arg_names = FxHashSet::with_capacity_and_hasher( + parameters.posonlyargs.len() + + parameters.args.len() + + usize::from(parameters.vararg.is_some()) + + parameters.kwonlyargs.len() + + usize::from(parameters.kwarg.is_some()), + BuildHasherDefault::default(), + ); + + let posonlyargs = parameters.posonlyargs.iter(); + let args = parameters.args.iter(); + let kwonlyargs = parameters.kwonlyargs.iter(); + + let vararg: Option<&ast::Parameter> = parameters.vararg.as_deref(); + let kwarg: Option<&ast::Parameter> = parameters.kwarg.as_deref(); + + for arg in posonlyargs + .chain(args) + .chain(kwonlyargs) + .map(|arg| &arg.parameter) + .chain(vararg) + .chain(kwarg) + { + let range = arg.range; + let arg_name = arg.name.as_str(); + if !all_arg_names.insert(arg_name) { + return Err(ParseError { + error: ParseErrorType::DuplicateArgumentError(arg_name.to_string()), + location: range, + }); + } + } + + Ok(()) +} + +pub(super) fn validate_arguments(arguments: &ast::Arguments) -> Result<(), ParseError> { + let mut all_arg_names = FxHashSet::with_capacity_and_hasher( + arguments.keywords.len(), + BuildHasherDefault::default(), + ); + + for (name, range) in arguments + .keywords + .iter() + .filter_map(|argument| argument.arg.as_ref().map(|arg| (arg, argument.range))) + { + let arg_name = name.as_str(); + if !all_arg_names.insert(arg_name) { + return Err(ParseError { + error: ParseErrorType::DuplicateKeywordArgumentError(arg_name.to_string()), + location: range, + }); + } + } + + Ok(()) +} diff --git a/crates/ruff_python_parser/src/parser/mod.rs b/crates/ruff_python_parser/src/parser/mod.rs new file mode 100644 index 0000000000000..ccce8a0e8fe43 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/mod.rs @@ -0,0 +1,810 @@ +use std::cmp::Ordering; + +use bitflags::bitflags; +use drop_bomb::DebugDropBomb; + +use ast::Mod; +use ruff_python_ast as ast; +use ruff_text_size::{Ranged, TextRange, TextSize}; + +use crate::lexer::lex; +use crate::parser::progress::ParserProgress; +use crate::{ + lexer::{LexResult, Spanned}, + token_set::TokenSet, + token_source::TokenSource, + Mode, ParseError, ParseErrorType, Tok, TokenKind, +}; + +mod expression; +mod helpers; +mod pattern; +mod progress; +mod statement; +#[cfg(test)] +mod tests; + +pub(crate) fn parse_tokens( + tokens: Vec, + source: &str, + mode: Mode, +) -> Result { + let program = Parser::new(source, mode, TokenSource::new(tokens)).parse_program(); + if program.parse_errors.is_empty() { + Ok(program.ast) + } else { + Err(program.parse_errors.into_iter().next().unwrap()) + } +} + +#[derive(Debug)] +pub struct Program { + pub ast: ast::Mod, + pub parse_errors: Vec, +} + +impl Program { + pub fn parse_str(source: &str, mode: Mode) -> Program { + let tokens = lex(source, mode); + Self::parse_tokens(source, tokens.collect(), mode) + } + + pub fn parse_tokens(source: &str, tokens: Vec, mode: Mode) -> Program { + Parser::new(source, mode, TokenSource::new(tokens)).parse_program() + } +} + +pub(crate) struct Parser<'src> { + source: &'src str, + tokens: TokenSource, + + /// Stores all the syntax errors found during the parsing. + errors: Vec, + + /// This tracks the current expression or statement being parsed. For example, + /// if we're parsing a tuple expression, e.g. `(1, 2)`, `ctx` has the value + /// `ParserCtxFlags::TUPLE_EXPR`. + /// + /// The `ctx` is also used to create custom error messages and forbid certain + /// expressions or statements of being parsed. The `ctx` should be empty after + /// an expression or statement is done parsing. + ctx: ParserCtxFlags, + + /// Specify the mode in which the code will be parsed. + mode: Mode, + + current: Spanned, + + /// The end of the last processed. Used to determine a node's end. + last_token_end: TextSize, + + /// The range of the tokens to parse. + /// + /// The range is equal to [0; source.len()) when parsing an entire file. + /// The range can be different when parsing only a part of a file using the `lex_starts_at` and `parse_expression_starts_at` APIs + /// in which case the the range is equal to [offset; subrange.len()). + tokens_range: TextRange, + + recovery_context: RecoveryContext, +} + +const NEWLINE_EOF_SET: TokenSet = TokenSet::new([TokenKind::Newline, TokenKind::EndOfFile]); +const LITERAL_SET: TokenSet = TokenSet::new([ + TokenKind::Int, + TokenKind::Float, + TokenKind::Complex, + TokenKind::String, + TokenKind::Ellipsis, + TokenKind::True, + TokenKind::False, + TokenKind::None, +]); +/// Tokens that are usually an expression or the start of one. +const EXPR_SET: TokenSet = TokenSet::new([ + TokenKind::Name, + TokenKind::Minus, + TokenKind::Plus, + TokenKind::Tilde, + TokenKind::Star, + TokenKind::DoubleStar, + TokenKind::Lpar, + TokenKind::Lbrace, + TokenKind::Lsqb, + TokenKind::Lambda, + TokenKind::Await, + TokenKind::Not, + TokenKind::Yield, + TokenKind::FStringStart, +]) +.union(LITERAL_SET); + +impl<'src> Parser<'src> { + pub(crate) fn new(source: &'src str, mode: Mode, mut tokens: TokenSource) -> Parser<'src> { + let tokens_range = TextRange::new( + tokens.position().unwrap_or_default(), + tokens.end().unwrap_or_default(), + ); + + let current = tokens + .next() + .unwrap_or_else(|| (Tok::EndOfFile, TextRange::empty(tokens_range.end()))); + + Parser { + mode, + source, + errors: Vec::new(), + ctx: ParserCtxFlags::empty(), + tokens, + recovery_context: RecoveryContext::empty(), + last_token_end: tokens_range.start(), + current, + tokens_range, + } + } + + pub(crate) fn parse_program(mut self) -> Program { + let ast = if self.mode == Mode::Expression { + let start = self.node_start(); + let parsed_expr = self.parse_expression(); + let mut progress = ParserProgress::default(); + + // TODO: How should error recovery work here? Just truncate after the expression? + loop { + progress.assert_progressing(&self); + if !self.eat(TokenKind::Newline) { + break; + } + } + self.bump(TokenKind::EndOfFile); + + Mod::Expression(ast::ModExpression { + body: Box::new(parsed_expr.expr), + range: self.node_range(start), + }) + } else { + let body = self.parse_list( + RecoveryContextKind::ModuleStatements, + Parser::parse_statement, + ); + + self.bump(TokenKind::EndOfFile); + + Mod::Module(ast::ModModule { + body, + range: self.tokens_range, + }) + }; + + Program { + ast, + parse_errors: self.finish(), + } + } + + fn finish(self) -> Vec { + // After parsing, the `ctx` and `ctx_stack` should be empty. + // If it's not, you probably forgot to call `clear_ctx` somewhere. + assert_eq!(self.ctx, ParserCtxFlags::empty()); + assert_eq!( + self.current_kind(), + TokenKind::EndOfFile, + "Parser should be at the end of the file." + ); + + // TODO consider re-integrating lexical error handling into the parser? + let parse_errors = self.errors; + let lex_errors = self.tokens.finish(); + + // Fast path for when there are no lex errors. + // There's no fast path for when there are no parse errors because a lex error + // always results in a parse error. + if lex_errors.is_empty() { + return parse_errors; + } + + let mut merged = Vec::with_capacity(parse_errors.len().saturating_add(lex_errors.len())); + + let mut parse_errors = parse_errors.into_iter().peekable(); + let mut lex_errors = lex_errors.into_iter().peekable(); + + while let (Some(parse_error), Some(lex_error)) = (parse_errors.peek(), lex_errors.peek()) { + match parse_error + .location + .start() + .cmp(&lex_error.location().start()) + { + Ordering::Less => merged.push(parse_errors.next().unwrap()), + Ordering::Equal => { + // Skip the parse error if we already have a lex error at the same location.. + parse_errors.next().unwrap(); + merged.push(lex_errors.next().unwrap().into()); + } + Ordering::Greater => merged.push(lex_errors.next().unwrap().into()), + } + } + + merged.extend(parse_errors); + merged.extend(lex_errors.map(ParseError::from)); + + merged + } + + #[inline] + #[must_use] + fn set_ctx(&mut self, ctx: ParserCtxFlags) -> SavedParserContext { + SavedParserContext { + flags: std::mem::replace(&mut self.ctx, ctx), + bomb: DebugDropBomb::new( + "You must restore the old parser context explicit by calling `clear_ctx`.", + ), + } + } + + #[inline] + fn restore_ctx(&mut self, current: ParserCtxFlags, mut saved_context: SavedParserContext) { + assert_eq!(self.ctx, current); + saved_context.bomb.defuse(); + self.ctx = saved_context.flags; + } + + #[inline] + fn has_ctx(&self, ctx: ParserCtxFlags) -> bool { + self.ctx.intersects(ctx) + } + + /// Returns the start position for a node that starts at the current token. + fn node_start(&self) -> TextSize { + self.current_range().start() + } + + fn node_range(&self, start: TextSize) -> TextRange { + // It's possible during error recovery that the parsing didn't consume any tokens. In that case, + // `last_token_end` still points to the end of the previous token but `start` is the start of the current token. + // Calling `TextRange::new(start, self.last_token_end)` would panic in that case because `start > end`. + // This path "detects" this case and creates an empty range instead. + if self.node_start() == start { + TextRange::empty(start) + } else { + TextRange::new(start, self.last_token_end) + } + } + + fn missing_node_range(&self) -> TextRange { + TextRange::empty(self.last_token_end) + } + + /// Moves the parser to the next token. Returns the old current token as an owned value. + /// FIXME(micha): Using `next_token` is almost always incorrect if there's a case where the current token is not of the expected type. + fn next_token(&mut self) -> Spanned { + let next = self + .tokens + .next() + .unwrap_or_else(|| (Tok::EndOfFile, TextRange::empty(self.tokens_range.end()))); + + let current = std::mem::replace(&mut self.current, next); + + if !matches!( + current.0, + // TODO explore including everything up to the dedent as part of the body. + Tok::Dedent + // Don't include newlines in the body + | Tok::Newline + // TODO(micha): Including the semi feels more correct but it isn't compatible with lalrpop and breaks the + // formatters semicolon detection. Exclude it for now + | Tok::Semi + ) { + self.last_token_end = current.1.end(); + } + + current + } + + fn peek_nth(&self, offset: usize) -> TokenKind { + if offset == 0 { + self.current_kind() + } else { + self.tokens + .peek_nth(offset - 1) + .map_or(TokenKind::EndOfFile, |spanned| spanned.0) + } + } + + #[inline] + fn current_token(&self) -> (TokenKind, TextRange) { + (self.current_kind(), self.current_range()) + } + + #[inline] + fn current_kind(&self) -> TokenKind { + // TODO: Converting the token kind over and over again can be expensive. + TokenKind::from_token(&self.current.0) + } + + #[inline] + fn current_range(&self) -> TextRange { + self.current.1 + } + + fn eat(&mut self, kind: TokenKind) -> bool { + if !self.at(kind) { + return false; + } + + self.next_token(); + true + } + + /// Bumps the current token assuming it is of the given kind. + /// + /// # Panics + /// If the current token is not of the given kind. + /// + /// # Returns + /// The current token + fn bump(&mut self, kind: TokenKind) -> (Tok, TextRange) { + assert_eq!(self.current_kind(), kind); + + self.next_token() + } + + fn expect(&mut self, expected: TokenKind) -> bool { + if self.eat(expected) { + return true; + } + + let (found, range) = self.current_token(); + self.add_error(ParseErrorType::ExpectedToken { found, expected }, range); + false + } + + fn add_error(&mut self, error: ParseErrorType, ranged: T) + where + T: Ranged, + { + fn inner(errors: &mut Vec, error: ParseErrorType, range: TextRange) { + // Avoid flagging multiple errors at the same location + let is_same_location = errors + .last() + .is_some_and(|last| last.location.start() == range.start()); + + if !is_same_location { + errors.push(ParseError { + error, + location: range, + }); + } + } + + inner(&mut self.errors, error, ranged.range()); + } + + /// Skip tokens until [`TokenSet`]. Returns the range of the skipped tokens. + + #[deprecated(note = "We should not perform error recovery outside of lists. Remove")] + fn skip_until(&mut self, token_set: TokenSet) { + let mut progress = ParserProgress::default(); + while !self.at_ts(token_set) { + progress.assert_progressing(self); + self.next_token(); + } + } + + fn at(&self, kind: TokenKind) -> bool { + self.current_kind() == kind + } + + fn at_ts(&self, ts: TokenSet) -> bool { + ts.contains(self.current_kind()) + } + + fn src_text(&self, ranged: T) -> &'src str + where + T: Ranged, + { + let range = ranged.range(); + // `ranged` uses absolute ranges to the source text of an entire file. + // Fix the source by subtracting the start offset when parsing only a part of a file (when parsing the tokens from `lex_starts_at`). + &self.source[range - self.tokens_range.start()] + } + + fn parse_list( + &mut self, + kind: RecoveryContextKind, + parse_element: impl Fn(&mut Parser<'src>) -> T, + ) -> Vec { + let mut elements = Vec::new(); + + self.parse_sequence(kind, |p| elements.push(parse_element(p))); + + elements + } + + fn parse_sequence( + &mut self, + kind: RecoveryContextKind, + mut parse_element: impl FnMut(&mut Parser<'src>), + ) { + let mut progress = ParserProgress::default(); + + let saved_context = self.recovery_context; + self.recovery_context = self + .recovery_context + .union(RecoveryContext::from_kind(kind)); + + while !kind.is_list_terminator(self) { + progress.assert_progressing(self); + + // The end of file marker ends all lists. + if self.at(TokenKind::EndOfFile) { + break; + } + + if kind.is_list_element(self) { + parse_element(self); + } else { + let should_recover = self.is_enclosing_list_element_or_terminator(); + + // Not a recognised element. Add an error and either skip the token or break parsing the list + // if the token is recognised as an element or terminator of an enclosing list. + let error = kind.create_error(self); + self.add_error(error, self.current_range()); + + if should_recover { + break; + } + self.next_token(); + } + } + + self.recovery_context = saved_context; + } + + fn parse_delimited_list( + &mut self, + kind: RecoveryContextKind, + parse_element: impl Fn(&mut Parser<'src>) -> T, + allow_trailing_comma: bool, + ) -> Vec { + let mut progress = ParserProgress::default(); + let mut elements = Vec::new(); + + let saved_context = self.recovery_context; + self.recovery_context = self + .recovery_context + .union(RecoveryContext::from_kind(kind)); + + let mut trailing_comma_range: Option = None; + + loop { + progress.assert_progressing(self); + + self.current_kind(); + + if kind.is_list_element(self) { + elements.push(parse_element(self)); + + let maybe_comma_range = self.current_range(); + if self.eat(TokenKind::Comma) { + trailing_comma_range = Some(maybe_comma_range); + continue; + } else { + trailing_comma_range = None; + } + + if kind.is_list_terminator(self) { + break; + } + + self.expect(TokenKind::Comma); + } else if kind.is_list_terminator(self) { + break; + } else { + // Run the error recovery: This also handles the case when an element is missing between two commas: `a,,b` + let should_recover = self.is_enclosing_list_element_or_terminator(); + + // Not a recognised element. Add an error and either skip the token or break parsing the list + // if the token is recognised as an element or terminator of an enclosing list. + let error = kind.create_error(self); + self.add_error(error, self.current_range()); + + if should_recover { + break; + } + + if self.at(TokenKind::Comma) { + trailing_comma_range = Some(self.current_range()); + } else { + trailing_comma_range = None; + } + + self.next_token(); + } + } + + if let Some(trailing_comma) = trailing_comma_range { + if !allow_trailing_comma { + self.add_error( + ParseErrorType::OtherError("Trailing comma not allowed".to_string()), + trailing_comma, + ); + } + } + + self.recovery_context = saved_context; + + elements + } + + #[cold] + fn is_enclosing_list_element_or_terminator(&self) -> bool { + for context in self.recovery_context.kind_iter() { + if context.is_list_terminator(self) || context.is_list_element(self) { + return true; + } + } + + false + } + + /// Parses elements enclosed within a delimiter pair, such as parentheses, brackets, + /// or braces. + #[deprecated(note = "Use `parse_delimited_list` instead.")] + fn parse_delimited( + &mut self, + allow_trailing_delim: bool, + opening: TokenKind, + delim: TokenKind, + closing: TokenKind, + func: impl FnMut(&mut Parser<'src>), + ) { + self.bump(opening); + + #[allow(deprecated)] + self.parse_separated(allow_trailing_delim, delim, [closing], func); + + self.expect(closing); + } + + /// Parses a sequence of elements separated by a delimiter. This function stops + /// parsing upon encountering any of the tokens in `ending_set`, if it doesn't + /// encounter the tokens in `ending_set` it stops parsing when seeing the `EOF` + /// or `Newline` token. + #[deprecated(note = "Use `parse_delimited_list` instead.")] + fn parse_separated( + &mut self, + allow_trailing_delim: bool, + delim: TokenKind, + ending_set: impl Into, + mut func: impl FnMut(&mut Parser<'src>), + ) { + let ending_set = NEWLINE_EOF_SET.union(ending_set.into()); + let mut progress = ParserProgress::default(); + + while !self.at_ts(ending_set) { + progress.assert_progressing(self); + func(self); + + // exit the loop if a trailing `delim` is not allowed + if !allow_trailing_delim && ending_set.contains(self.peek_nth(1)) { + break; + } + + if !self.eat(delim) { + if self.at_expr() { + self.expect(delim); + } else { + break; + } + } + } + } + + fn is_current_token_postfix(&self) -> bool { + matches!( + self.current_kind(), + TokenKind::Lpar | TokenKind::Lsqb | TokenKind::Dot + ) + } +} + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +enum SequenceMatchPatternParentheses { + Tuple, + List, +} + +impl SequenceMatchPatternParentheses { + fn closing_kind(self) -> TokenKind { + match self { + SequenceMatchPatternParentheses::Tuple => TokenKind::Rpar, + SequenceMatchPatternParentheses::List => TokenKind::Rsqb, + } + } + + const fn is_list(self) -> bool { + matches!(self, SequenceMatchPatternParentheses::List) + } +} + +bitflags! { + #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] + struct ParserCtxFlags: u8 { + const PARENTHESIZED_EXPR = 1 << 0; + + // NOTE: `ARGUMENTS` can be removed once the heuristic in `parse_with_items` + // is improved. + const ARGUMENTS = 1 << 1; + const FOR_TARGET = 1 << 2; + } +} + +#[derive(PartialEq, Copy, Clone)] +enum FunctionKind { + Lambda, + FunctionDef, +} + +#[repr(u8)] +#[derive(Copy, Clone, Debug)] +enum RecoveryContextKind { + ModuleStatements, + BlockStatements, + + /// The `elif` clauses of an `if` statement + Elif, + + /// The `except` clauses of a `try` statement + Except, + + /// When parsing a list of assignment targets + AssignmentTargets, + + TypeParams, + + ImportNames, +} + +impl RecoveryContextKind { + fn is_list_terminator(self, p: &Parser) -> bool { + match self { + // The program must consume all tokens until the end + RecoveryContextKind::ModuleStatements => false, + RecoveryContextKind::BlockStatements => p.at(TokenKind::Dedent), + + RecoveryContextKind::Elif => p.at(TokenKind::Else), + RecoveryContextKind::Except => { + matches!(p.current_kind(), TokenKind::Finally | TokenKind::Else) + } + + // TODO: Should `semi` be part of the simple statement recovery set instead? + RecoveryContextKind::AssignmentTargets => { + matches!(p.current_kind(), TokenKind::Newline | TokenKind::Semi) + } + + // Tokens other than `]` are for better error recovery: For example, recover when we find the `:` of a clause header or + // the equal of a type assignment. + RecoveryContextKind::TypeParams => { + matches!( + p.current_kind(), + TokenKind::Rsqb + | TokenKind::Newline + | TokenKind::Colon + | TokenKind::Equal + | TokenKind::Lpar + ) + } + RecoveryContextKind::ImportNames => { + matches!(p.current_kind(), TokenKind::Rpar | TokenKind::Newline) + } + } + } + + fn is_list_element(self, p: &Parser) -> bool { + match self { + RecoveryContextKind::ModuleStatements => p.is_at_stmt(), + RecoveryContextKind::BlockStatements => p.is_at_stmt(), + RecoveryContextKind::Elif => p.at(TokenKind::Elif), + RecoveryContextKind::Except => p.at(TokenKind::Except), + RecoveryContextKind::AssignmentTargets => p.at(TokenKind::Equal), + RecoveryContextKind::TypeParams => p.is_at_type_param(), + RecoveryContextKind::ImportNames => { + matches!(p.current_kind(), TokenKind::Star | TokenKind::Name) + } + } + } + + fn create_error(self, p: &Parser) -> ParseErrorType { + match self { + RecoveryContextKind::ModuleStatements | RecoveryContextKind::BlockStatements => { + if p.at(TokenKind::Indent) { + ParseErrorType::UnexpectedIndentation + } else { + ParseErrorType::OtherError("Expected a statement".to_string()) + } + } + RecoveryContextKind::Elif => ParseErrorType::OtherError( + "Expected an `elif` or `else` clause, or the end of the `if` statement." + .to_string(), + ), + RecoveryContextKind::Except => ParseErrorType::OtherError( + "An `except` or `finally` clause or the end of the `try` statement expected." + .to_string(), + ), + RecoveryContextKind::AssignmentTargets => { + if p.current_kind().is_keyword() { + ParseErrorType::OtherError( + "The keyword is not allowed as a variable declaration name".to_string(), + ) + } else { + ParseErrorType::OtherError("Assignment target expected".to_string()) + } + } + RecoveryContextKind::TypeParams => ParseErrorType::OtherError( + "Expected a type parameter or the end of the type parameter list".to_string(), + ), + RecoveryContextKind::ImportNames => { + ParseErrorType::OtherError("Expected an import name or a ')'".to_string()) + } + } + } +} + +#[derive(Copy, Clone, Default, PartialEq, Eq)] +struct RecoveryContext(u8); + +bitflags! { + impl RecoveryContext: u8 { + const MODULE_STATEMENTS = 1 << 0; + const BLOCK_STATEMENTS = 1 << 1; + const ELIF = 1 << 2; + const EXCEPT = 1 << 3; + + const ASSIGNMENT_TARGETS = 1 << 4; + const TYPE_PARAMS = 1 << 5; + + const IMPORT_NAMES = 1 << 6; + } +} + +impl RecoveryContext { + const fn from_kind(kind: RecoveryContextKind) -> Self { + match kind { + RecoveryContextKind::ModuleStatements => RecoveryContext::MODULE_STATEMENTS, + RecoveryContextKind::BlockStatements => RecoveryContext::BLOCK_STATEMENTS, + RecoveryContextKind::Elif => RecoveryContext::ELIF, + RecoveryContextKind::Except => RecoveryContext::EXCEPT, + RecoveryContextKind::AssignmentTargets => RecoveryContext::ASSIGNMENT_TARGETS, + RecoveryContextKind::TypeParams => RecoveryContext::TYPE_PARAMS, + RecoveryContextKind::ImportNames => RecoveryContext::IMPORT_NAMES, + } + } + + /// Safe conversion to the corresponding [`RecoveryContextKind`] (inverse of [`Self::from_kind`]). + /// + /// Returns `None` if the `RecoveryContext` is empty or has multiple flags set. + const fn to_kind(self) -> Option { + Some(match self { + RecoveryContext::MODULE_STATEMENTS => RecoveryContextKind::ModuleStatements, + RecoveryContext::BLOCK_STATEMENTS => RecoveryContextKind::BlockStatements, + RecoveryContext::ELIF => RecoveryContextKind::Elif, + RecoveryContext::ASSIGNMENT_TARGETS => RecoveryContextKind::AssignmentTargets, + RecoveryContext::TYPE_PARAMS => RecoveryContextKind::TypeParams, + RecoveryContext::IMPORT_NAMES => RecoveryContextKind::ImportNames, + _ => return None, + }) + } + + fn kind_iter(self) -> impl Iterator { + self.iter().map(|context| { + context + .to_kind() + .expect("Expected context to be of a single kind.") + }) + } +} + +#[derive(Debug)] +struct SavedParserContext { + flags: ParserCtxFlags, + bomb: DebugDropBomb, +} diff --git a/crates/ruff_python_parser/src/parser/pattern.rs b/crates/ruff_python_parser/src/parser/pattern.rs new file mode 100644 index 0000000000000..9373b4638eebb --- /dev/null +++ b/crates/ruff_python_parser/src/parser/pattern.rs @@ -0,0 +1,597 @@ +use ruff_python_ast::{ + self as ast, Expr, ExprContext, Number, Operator, Pattern, Singleton, UnaryOp, +}; +use ruff_text_size::{Ranged, TextSize}; + +use crate::parser::progress::ParserProgress; +use crate::parser::{Parser, SequenceMatchPatternParentheses}; +use crate::token_set::TokenSet; +use crate::{ParseErrorType, Tok, TokenKind}; + +const END_EXPR_SET: TokenSet = TokenSet::new([ + TokenKind::Newline, + TokenKind::Semi, + TokenKind::Colon, + TokenKind::EndOfFile, + TokenKind::Rbrace, + TokenKind::Rsqb, + TokenKind::Rpar, + TokenKind::Comma, + TokenKind::Dedent, + TokenKind::Else, + TokenKind::As, + TokenKind::From, + TokenKind::For, + TokenKind::Async, + TokenKind::In, +]); + +impl<'src> Parser<'src> { + pub(super) fn parse_match_patterns(&mut self) -> Pattern { + let start = self.node_start(); + let pattern = self.parse_match_pattern(); + + if self.at(TokenKind::Comma) { + Pattern::MatchSequence(self.parse_sequence_match_pattern(pattern, start, None)) + } else { + pattern + } + } + + fn parse_match_pattern(&mut self) -> Pattern { + let start = self.node_start(); + let mut lhs = self.parse_match_pattern_lhs(); + + // Or pattern + if self.at(TokenKind::Vbar) { + let mut patterns = vec![lhs]; + let mut progress = ParserProgress::default(); + + while self.eat(TokenKind::Vbar) { + progress.assert_progressing(self); + let pattern = self.parse_match_pattern_lhs(); + patterns.push(pattern); + } + + lhs = Pattern::MatchOr(ast::PatternMatchOr { + range: self.node_range(start), + patterns, + }); + } + + // As pattern + if self.eat(TokenKind::As) { + let ident = self.parse_identifier(); + lhs = Pattern::MatchAs(ast::PatternMatchAs { + range: self.node_range(start), + name: Some(ident), + pattern: Some(Box::new(lhs)), + }); + } + + lhs + } + + fn parse_match_pattern_lhs(&mut self) -> Pattern { + let start = self.node_start(); + let mut lhs = match self.current_kind() { + TokenKind::Lbrace => Pattern::MatchMapping(self.parse_match_pattern_mapping()), + TokenKind::Star => Pattern::MatchStar(self.parse_match_pattern_star()), + TokenKind::Lpar | TokenKind::Lsqb => self.parse_delimited_match_pattern(), + _ => self.parse_match_pattern_literal(), + }; + + if self.at(TokenKind::Lpar) { + lhs = Pattern::MatchClass(self.parse_match_pattern_class(lhs, start)); + } + + if self.at(TokenKind::Plus) || self.at(TokenKind::Minus) { + let (operator_token, _) = self.next_token(); + let operator = if matches!(operator_token, Tok::Plus) { + Operator::Add + } else { + Operator::Sub + }; + + let lhs_value = if let Pattern::MatchValue(lhs) = lhs { + if !lhs.value.is_literal_expr() && !matches!(lhs.value.as_ref(), Expr::UnaryOp(_)) { + self.add_error( + ParseErrorType::OtherError(format!( + "invalid `{}` expression for match pattern", + self.src_text(lhs.range) + )), + lhs.range, + ); + } + lhs.value + } else { + self.add_error( + ParseErrorType::OtherError("invalid lhs pattern".to_string()), + &lhs, + ); + + #[allow(deprecated)] + Box::new(Expr::Invalid(ast::ExprInvalid { + value: self.src_text(lhs.range()).into(), + range: lhs.range(), + })) + }; + + let rhs_pattern = self.parse_match_pattern_lhs(); + let rhs_value = if let Pattern::MatchValue(rhs) = rhs_pattern { + if !rhs.value.is_literal_expr() { + self.add_error( + ParseErrorType::OtherError( + "invalid expression for match pattern".to_string(), + ), + &rhs, + ); + } + rhs.value + } else { + self.add_error( + ParseErrorType::OtherError("invalid rhs pattern".to_string()), + rhs_pattern.range(), + ); + + #[allow(deprecated)] + Box::new(Expr::Invalid(ast::ExprInvalid { + value: self.src_text(rhs_pattern.range()).into(), + range: rhs_pattern.range(), + })) + }; + + if matches!( + rhs_value.as_ref(), + Expr::UnaryOp(ast::ExprUnaryOp { + op: UnaryOp::USub, + .. + }) + ) { + self.add_error( + ParseErrorType::OtherError( + "`-` not allowed in rhs of match pattern".to_string(), + ), + rhs_value.range(), + ); + } + + let range = self.node_range(start); + + return Pattern::MatchValue(ast::PatternMatchValue { + value: Box::new(Expr::BinOp(ast::ExprBinOp { + left: lhs_value, + op: operator, + right: rhs_value, + range, + })), + range, + }); + } + + lhs + } + + fn parse_match_pattern_mapping(&mut self) -> ast::PatternMatchMapping { + let start = self.node_start(); + let mut keys = vec![]; + let mut patterns = vec![]; + let mut rest = None; + + #[allow(deprecated)] + self.parse_delimited( + true, + TokenKind::Lbrace, + TokenKind::Comma, + TokenKind::Rbrace, + |parser| { + if parser.eat(TokenKind::DoubleStar) { + rest = Some(parser.parse_identifier()); + } else { + let key = match parser.parse_match_pattern_lhs() { + Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => *value, + Pattern::MatchSingleton(ast::PatternMatchSingleton { value, range }) => { + match value { + Singleton::None => { + Expr::NoneLiteral(ast::ExprNoneLiteral { range }) + } + Singleton::True => Expr::BooleanLiteral(ast::ExprBooleanLiteral { + value: true, + range, + }), + Singleton::False => Expr::BooleanLiteral(ast::ExprBooleanLiteral { + value: false, + range, + }), + } + } + pattern => { + parser.add_error( + ParseErrorType::OtherError(format!( + "invalid mapping pattern key `{}`", + parser.src_text(&pattern) + )), + &pattern, + ); + Expr::Invalid(ast::ExprInvalid { + value: parser.src_text(&pattern).into(), + range: pattern.range(), + }) + } + }; + keys.push(key); + + parser.expect(TokenKind::Colon); + + patterns.push(parser.parse_match_pattern()); + } + }, + ); + + ast::PatternMatchMapping { + range: self.node_range(start), + keys, + patterns, + rest, + } + } + + fn parse_match_pattern_star(&mut self) -> ast::PatternMatchStar { + let start = self.node_start(); + self.bump(TokenKind::Star); + + let ident = self.parse_identifier(); + + ast::PatternMatchStar { + range: self.node_range(start), + name: if ident.is_valid() && ident.id == "_" { + None + } else { + Some(ident) + }, + } + } + + fn parse_delimited_match_pattern(&mut self) -> Pattern { + let start = self.node_start(); + let parentheses = if self.eat(TokenKind::Lpar) { + SequenceMatchPatternParentheses::Tuple + } else { + self.bump(TokenKind::Lsqb); + SequenceMatchPatternParentheses::List + }; + + if matches!(self.current_kind(), TokenKind::Newline | TokenKind::Colon) { + self.add_error( + ParseErrorType::OtherError(format!( + "missing `{closing}`", + closing = if parentheses.is_list() { "]" } else { ")" } + )), + self.current_range(), + ); + } + + if self.eat(parentheses.closing_kind()) { + return Pattern::MatchSequence(ast::PatternMatchSequence { + patterns: vec![], + range: self.node_range(start), + }); + } + + let mut pattern = self.parse_match_pattern(); + + if parentheses.is_list() || self.at(TokenKind::Comma) { + pattern = Pattern::MatchSequence(self.parse_sequence_match_pattern( + pattern, + start, + Some(parentheses), + )); + } else { + self.expect(parentheses.closing_kind()); + } + + pattern + } + + fn parse_sequence_match_pattern( + &mut self, + first_elt: Pattern, + start: TextSize, + parentheses: Option, + ) -> ast::PatternMatchSequence { + let ending = parentheses.map_or( + TokenKind::Colon, + SequenceMatchPatternParentheses::closing_kind, + ); + + if parentheses.is_some_and(|parentheses| { + self.at(parentheses.closing_kind()) || self.peek_nth(1) == parentheses.closing_kind() + }) { + // The comma is optional if it is a single-element sequence + self.eat(TokenKind::Comma); + } else { + self.expect(TokenKind::Comma); + } + + let mut patterns = vec![first_elt]; + + #[allow(deprecated)] + self.parse_separated(true, TokenKind::Comma, [ending], |parser| { + patterns.push(parser.parse_match_pattern()); + }); + + if let Some(parentheses) = parentheses { + self.expect(parentheses.closing_kind()); + } + + let range = self.node_range(start); + ast::PatternMatchSequence { range, patterns } + } + + fn parse_match_pattern_literal(&mut self) -> Pattern { + let start = self.node_start(); + match self.current_kind() { + TokenKind::None => { + self.bump(TokenKind::None); + Pattern::MatchSingleton(ast::PatternMatchSingleton { + value: Singleton::None, + range: self.node_range(start), + }) + } + TokenKind::True => { + self.bump(TokenKind::True); + Pattern::MatchSingleton(ast::PatternMatchSingleton { + value: Singleton::True, + range: self.node_range(start), + }) + } + TokenKind::False => { + self.bump(TokenKind::False); + Pattern::MatchSingleton(ast::PatternMatchSingleton { + value: Singleton::False, + range: self.node_range(start), + }) + } + TokenKind::String => { + let str = self.parse_string_expression(); + + Pattern::MatchValue(ast::PatternMatchValue { + value: Box::new(str), + range: self.node_range(start), + }) + } + TokenKind::Complex => { + let (Tok::Complex { real, imag }, _) = self.bump(TokenKind::Complex) else { + unreachable!() + }; + let range = self.node_range(start); + + Pattern::MatchValue(ast::PatternMatchValue { + value: Box::new(Expr::NumberLiteral(ast::ExprNumberLiteral { + value: Number::Complex { real, imag }, + range, + })), + range, + }) + } + TokenKind::Int => { + let (Tok::Int { value }, _) = self.bump(TokenKind::Int) else { + unreachable!() + }; + let range = self.node_range(start); + + Pattern::MatchValue(ast::PatternMatchValue { + value: Box::new(Expr::NumberLiteral(ast::ExprNumberLiteral { + value: Number::Int(value), + range, + })), + range, + }) + } + TokenKind::Float => { + let (Tok::Float { value }, _) = self.bump(TokenKind::Float) else { + unreachable!() + }; + let range = self.node_range(start); + + Pattern::MatchValue(ast::PatternMatchValue { + value: Box::new(Expr::NumberLiteral(ast::ExprNumberLiteral { + value: Number::Float(value), + range, + })), + range, + }) + } + TokenKind::Name if self.peek_nth(1) == TokenKind::Dot => { + let (Tok::Name { name }, _) = self.bump(TokenKind::Name) else { + unreachable!() + }; + let id = Expr::Name(ast::ExprName { + id: name.to_string(), + ctx: ExprContext::Load, + range: self.node_range(start), + }); + + let attribute = self.parse_attr_expr_for_match_pattern(id, start); + + Pattern::MatchValue(ast::PatternMatchValue { + value: Box::new(attribute), + range: self.node_range(start), + }) + } + TokenKind::Name => { + let (Tok::Name { name }, _) = self.bump(TokenKind::Name) else { + unreachable!() + }; + let range = self.node_range(start); + + Pattern::MatchAs(ast::PatternMatchAs { + range, + pattern: None, + name: if name.contains("_") { + None + } else { + Some(ast::Identifier { + id: name.to_string(), + range, + }) + }, + }) + } + TokenKind::Minus + if matches!( + self.peek_nth(1), + TokenKind::Int | TokenKind::Float | TokenKind::Complex + ) => + { + let parsed_expr = self.parse_lhs_expression(); + + let range = self.node_range(start); + Pattern::MatchValue(ast::PatternMatchValue { + value: Box::new(parsed_expr.expr), + range, + }) + } + kind => { + // Upon encountering an unexpected token, return a `Pattern::MatchValue` containing + // an empty `Expr::Name`. + let invalid_node = if kind.is_keyword() { + Expr::Name(self.parse_name()) + } else { + self.add_error( + ParseErrorType::OtherError("Expression expected.".to_string()), + self.current_range(), + ); + Expr::Name(ast::ExprName { + range: self.missing_node_range(), + id: String::new(), + ctx: ExprContext::Load, + }) + }; + + Pattern::MatchValue(ast::PatternMatchValue { + value: Box::new(invalid_node), + range: self.missing_node_range(), + }) + } + } + } + + fn parse_attr_expr_for_match_pattern(&mut self, mut lhs: Expr, start: TextSize) -> Expr { + while self.current_kind() == TokenKind::Dot { + lhs = Expr::Attribute(self.parse_attribute_expression(lhs, start)); + } + + lhs + } + + fn parse_match_pattern_class( + &mut self, + cls: Pattern, + start: TextSize, + ) -> ast::PatternMatchClass { + let mut patterns = vec![]; + let mut keywords = vec![]; + let mut has_seen_pattern = false; + let mut has_seen_keyword_pattern = false; + + let arguments_start = self.node_start(); + #[allow(deprecated)] + self.parse_delimited( + true, + TokenKind::Lpar, + TokenKind::Comma, + TokenKind::Rpar, + |parser| { + let pattern_start = parser.node_start(); + let pattern = parser.parse_match_pattern(); + + if parser.eat(TokenKind::Equal) { + has_seen_pattern = false; + has_seen_keyword_pattern = true; + + if let Pattern::MatchAs(ast::PatternMatchAs { + name: Some(attr), .. + }) = pattern + { + let pattern = parser.parse_match_pattern(); + + keywords.push(ast::PatternKeyword { + attr, + pattern, + range: parser.node_range(pattern_start), + }); + } else { + #[allow(deprecated)] + parser.skip_until(END_EXPR_SET); + parser.add_error( + ParseErrorType::OtherError("`not valid keyword pattern".to_string()), + parser.node_range(pattern_start), + ); + } + } else { + has_seen_pattern = true; + patterns.push(pattern); + } + + if has_seen_keyword_pattern && has_seen_pattern { + parser.add_error( + ParseErrorType::OtherError( + "pattern not allowed after keyword pattern".to_string(), + ), + parser.node_range(pattern_start), + ); + } + }, + ); + + let arguments_range = self.node_range(arguments_start); + + let cls = match cls { + Pattern::MatchAs(ast::PatternMatchAs { + name: Some(ident), .. + }) => { + if ident.is_valid() { + Box::new(Expr::Name(ast::ExprName { + range: ident.range(), + id: ident.id, + ctx: ExprContext::Load, + })) + } else { + #[allow(deprecated)] + Box::new(Expr::Invalid(ast::ExprInvalid { + value: self.src_text(&ident).into(), + range: ident.range(), + })) + } + } + Pattern::MatchValue(ast::PatternMatchValue { value, range: _ }) + if matches!(value.as_ref(), Expr::Attribute(_)) => + { + value + } + pattern => { + self.add_error( + ParseErrorType::OtherError("invalid pattern match class".to_string()), + &pattern, + ); + // FIXME(micha): Including the entire range is not ideal because it also includes trivia. + #[allow(deprecated)] + Box::new(Expr::Invalid(ast::ExprInvalid { + value: self.src_text(pattern.range()).into(), + range: pattern.range(), + })) + } + }; + + ast::PatternMatchClass { + cls, + arguments: ast::PatternArguments { + patterns, + keywords, + range: arguments_range, + }, + range: self.node_range(start), + } + } +} diff --git a/crates/ruff_python_parser/src/parser/progress.rs b/crates/ruff_python_parser/src/parser/progress.rs new file mode 100644 index 0000000000000..919565cde2e26 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/progress.rs @@ -0,0 +1,36 @@ +use crate::parser::Parser; +use crate::TokenKind; +use ruff_text_size::TextSize; + +/// Captures the progress of the parser and allows to test if the parsing is still making progress +#[derive(Debug, Copy, Clone, Default)] +pub(super) struct ParserProgress(Option<(TokenKind, TextSize)>); + +impl ParserProgress { + /// Returns true if the parser has passed this position + #[inline] + fn has_progressed(self, p: &Parser) -> bool { + match self.0 { + None => true, + Some(snapshot) => snapshot != (p.current_kind(), p.current_range().start()), + } + } + + /// Asserts that the parsing is still making progress. + /// + /// # Panics + /// + /// Panics if the parser hasn't progressed since the last call. + #[inline] + pub(super) fn assert_progressing(&mut self, p: &Parser) { + assert!( + self.has_progressed(p), + "The parser is no longer progressing. Stuck at '{}' {:?}:{:?}", + p.src_text(p.current_range()), + p.current_kind(), + p.current_range(), + ); + + self.0 = Some((p.current_kind(), p.current_range().start())); + } +} diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_ipy_escape_command.snap b/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__ok_ipy_escape_command.snap similarity index 87% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_ipy_escape_command.snap rename to crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__ok_ipy_escape_command.snap index 8aec96f0508e0..b7e1120912f2a 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_ipy_escape_command.snap +++ b/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__ok_ipy_escape_command.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/invalid.rs +source: crates/ruff_python_parser/src/parser/tests.rs expression: ast --- Ok( diff --git a/crates/ruff_python_parser/src/parser/statement.rs b/crates/ruff_python_parser/src/parser/statement.rs new file mode 100644 index 0000000000000..96cd5dda25989 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/statement.rs @@ -0,0 +1,1655 @@ +use std::fmt::Display; + +use ruff_python_ast::{ + self as ast, ExceptHandler, Expr, ExprContext, IpyEscapeKind, Operator, Stmt, WithItem, +}; +use ruff_text_size::{Ranged, TextSize}; + +use crate::parser::expression::ParsedExpr; +use crate::parser::progress::ParserProgress; +use crate::parser::{ + helpers, FunctionKind, Parser, ParserCtxFlags, RecoveryContext, RecoveryContextKind, EXPR_SET, + LITERAL_SET, +}; +use crate::token_set::TokenSet; +use crate::{Mode, ParseErrorType, Tok, TokenKind}; + +/// Tokens that can appear after an expression. +/// Tokens that represent compound statements. +const COMPOUND_STMT_SET: TokenSet = TokenSet::new([ + TokenKind::Match, + TokenKind::If, + TokenKind::With, + TokenKind::While, + TokenKind::For, + TokenKind::Try, + TokenKind::Def, + TokenKind::Class, + TokenKind::Async, + TokenKind::At, +]); + +/// Tokens that represent simple statements, but doesn't include expressions. +const SIMPLE_STMT_SET: TokenSet = TokenSet::new([ + TokenKind::Pass, + TokenKind::Return, + TokenKind::Break, + TokenKind::Continue, + TokenKind::Global, + TokenKind::Nonlocal, + TokenKind::Assert, + TokenKind::Yield, + TokenKind::Del, + TokenKind::Raise, + TokenKind::Import, + TokenKind::From, + TokenKind::Type, + TokenKind::EscapeCommand, +]); + +/// Tokens that represent simple statements, including expressions. +const SIMPLE_STMT_SET2: TokenSet = SIMPLE_STMT_SET.union(EXPR_SET); + +const STMTS_SET: TokenSet = SIMPLE_STMT_SET2.union(COMPOUND_STMT_SET); + +impl<'src> Parser<'src> { + fn at_compound_stmt(&self) -> bool { + self.at_ts(COMPOUND_STMT_SET) + } + + fn at_simple_stmt(&self) -> bool { + self.at_ts(SIMPLE_STMT_SET2) + } + + pub(super) fn is_at_stmt(&self) -> bool { + self.at_ts(STMTS_SET) + } + + /// Parses a compound or a simple statement. + pub(super) fn parse_statement(&mut self) -> Stmt { + let start_offset = self.node_start(); + match self.current_kind() { + TokenKind::If => Stmt::If(self.parse_if_statement()), + TokenKind::For => Stmt::For(self.parse_for_statement(start_offset)), + TokenKind::While => Stmt::While(self.parse_while_statement()), + TokenKind::Def => { + Stmt::FunctionDef(self.parse_function_definition(vec![], start_offset)) + } + TokenKind::Class => Stmt::ClassDef(self.parse_class_definition(vec![], start_offset)), + TokenKind::Try => Stmt::Try(self.parse_try_statement()), + TokenKind::With => Stmt::With(self.parse_with_statement(start_offset)), + TokenKind::At => self.parse_decorators(), + TokenKind::Async => self.parse_async_statement(), + TokenKind::Match => Stmt::Match(self.parse_match_statement()), + _ => self.parse_single_simple_statement(), + } + } + + /// Parses a single simple statement, expecting it to be terminated by a newline or semicolon. + /// TODO(micha): It's not entirely clear why this method is necessary. It is called from + /// `parse_body` and it only reads out the first simple statement before calling `parse_statement` again. + /// This makes me wonder if the parser incorrectly allows `a;if b: pass` + fn parse_single_simple_statement(&mut self) -> Stmt { + let stmt = self.parse_simple_statement(); + + let has_eaten_semicolon = self.eat(TokenKind::Semi); + let has_eaten_newline = self.eat(TokenKind::Newline); + + if !has_eaten_newline && !has_eaten_semicolon && self.at_simple_stmt() { + let range = self.current_range(); + self.add_error( + ParseErrorType::SimpleStmtsInSameLine, + stmt.range().cover(range), + ); + } + + if !has_eaten_newline && self.at_compound_stmt() { + // Avoid create `SimpleStmtAndCompoundStmtInSameLine` error when the + // current node is `Expr::Invalid`. Example of when this may happen: + // ```python + // ! def x(): ... + // ``` + // The `!` (an unexpected token) will be parsed as `Expr::Invalid`. + if let Stmt::Expr(expr) = &stmt { + #[allow(deprecated)] + if let Expr::Invalid(_) = expr.value.as_ref() { + return stmt; + } + } + + self.add_error( + ParseErrorType::SimpleStmtAndCompoundStmtInSameLine, + stmt.range().cover(self.current_range()), + ); + } + + stmt + } + + fn parse_simple_statements(&mut self) -> Vec { + let mut stmts = vec![]; + let start = self.node_start(); + let mut progress = ParserProgress::default(); + + loop { + progress.assert_progressing(self); + stmts.push(self.parse_simple_statement()); + + if !self.eat(TokenKind::Semi) { + if self.at_simple_stmt() { + for stmt in &stmts { + self.add_error(ParseErrorType::SimpleStmtsInSameLine, stmt.range()); + } + } else { + break; + } + } + + if !self.at_simple_stmt() { + break; + } + } + + if !self.eat(TokenKind::Newline) && self.at_compound_stmt() { + self.add_error( + ParseErrorType::SimpleStmtAndCompoundStmtInSameLine, + self.node_range(start), + ); + } + + stmts + } + + /// See: + fn parse_simple_statement(&mut self) -> Stmt { + match self.current_kind() { + TokenKind::Return => Stmt::Return(self.parse_return_statement()), + TokenKind::Import => Stmt::Import(self.parse_import_statement()), + TokenKind::From => Stmt::ImportFrom(self.parse_from_import_statement()), + TokenKind::Pass => Stmt::Pass(self.parse_pass_statement()), + TokenKind::Continue => Stmt::Continue(self.parse_continue_statement()), + TokenKind::Break => Stmt::Break(self.parse_break_statement()), + TokenKind::Raise => Stmt::Raise(self.parse_raise_statement()), + TokenKind::Del => Stmt::Delete(self.parse_delete_statement()), + TokenKind::Assert => Stmt::Assert(self.parse_assert_statement()), + TokenKind::Global => Stmt::Global(self.parse_global_statement()), + TokenKind::Nonlocal => Stmt::Nonlocal(self.parse_nonlocal_statement()), + TokenKind::Type => Stmt::TypeAlias(self.parse_type_alias_statement()), + TokenKind::EscapeCommand if self.mode == Mode::Ipython => { + Stmt::IpyEscapeCommand(self.parse_ipython_escape_command_statement()) + } + _ => { + let start = self.node_start(); + let parsed_expr = self.parse_expression(); + + if self.eat(TokenKind::Equal) { + Stmt::Assign(self.parse_assign_statement(parsed_expr, start)) + } else if self.eat(TokenKind::Colon) { + Stmt::AnnAssign(self.parse_annotated_assignment_statement(parsed_expr, start)) + } else if let Ok(op) = Operator::try_from(self.current_kind()) { + Stmt::AugAssign(self.parse_augmented_assignment_statement( + parsed_expr, + op, + start, + )) + } else if self.mode == Mode::Ipython && self.eat(TokenKind::Question) { + let mut kind = IpyEscapeKind::Help; + + if self.eat(TokenKind::Question) { + kind = IpyEscapeKind::Help2; + } + + // FIXME(micha): Is this range correct + let range = self.node_range(start); + Stmt::IpyEscapeCommand(ast::StmtIpyEscapeCommand { + value: self + .src_text(parsed_expr.range()) + .to_string() + .into_boxed_str(), + kind, + range, + }) + } else { + Stmt::Expr(ast::StmtExpr { + range: self.node_range(start), + value: Box::new(parsed_expr.expr), + }) + } + } + } + } + + /// Parses a delete statement. + /// + /// # Panics + /// If the parser isn't positioned at a `del` token. + /// See: + fn parse_delete_statement(&mut self) -> ast::StmtDelete { + let start = self.node_start(); + + self.bump(TokenKind::Del); + let mut targets = vec![]; + + #[allow(deprecated)] + self.parse_separated(true, TokenKind::Comma, [TokenKind::Newline], |parser| { + let mut target = parser.parse_conditional_expression_or_higher(); + helpers::set_expr_ctx(&mut target.expr, ExprContext::Del); + + if matches!(target.expr, Expr::BoolOp(_) | Expr::Compare(_)) { + // Should we make `target` an `Expr::Invalid` here? + parser.add_error( + ParseErrorType::OtherError(format!( + "`{}` not allowed in `del` statement", + parser.src_text(&target.expr) + )), + &target.expr, + ); + } + targets.push(target.expr); + }); + + ast::StmtDelete { + targets, + range: self.node_range(start), + } + } + + /// See: + fn parse_return_statement(&mut self) -> ast::StmtReturn { + let start = self.node_start(); + self.bump(TokenKind::Return); + + let value = self + .at_expr() + .then(|| Box::new(self.parse_expression().expr)); + + ast::StmtReturn { + range: self.node_range(start), + value, + } + } + + /// See: + fn parse_raise_statement(&mut self) -> ast::StmtRaise { + let start = self.node_start(); + self.bump(TokenKind::Raise); + + let exc = if self.at(TokenKind::Newline) { + None + } else { + let exc = self.parse_expression(); + + if let Expr::Tuple(node) = &exc.expr { + if !node.parenthesized { + self.add_error( + ParseErrorType::OtherError( + "unparenthesized tuple not allowed in `raise` statement".to_string(), + ), + node.range, + ); + } + } + + Some(Box::new(exc.expr)) + }; + + let cause = (exc.is_some() && self.eat(TokenKind::From)).then(|| { + let cause = self.parse_expression(); + + if let Expr::Tuple(ast::ExprTuple { + parenthesized: false, + range: tuple_range, + .. + }) = &cause.expr + { + self.add_error( + ParseErrorType::OtherError( + "unparenthesized tuple not allowed in `raise from` statement".to_string(), + ), + tuple_range, + ); + } + + Box::new(cause.expr) + }); + + ast::StmtRaise { + range: self.node_range(start), + exc, + cause, + } + } + + /// See: + fn parse_import_statement(&mut self) -> ast::StmtImport { + let start = self.node_start(); + self.bump(TokenKind::Import); + + let mut names = vec![]; + #[allow(deprecated)] + self.parse_separated(false, TokenKind::Comma, [TokenKind::Newline], |parser| { + names.push(parser.parse_alias()); + }); + + ast::StmtImport { + range: self.node_range(start), + names, + } + } + + fn parse_from_import_statement(&mut self) -> ast::StmtImportFrom { + const DOT_ELLIPSIS_SET: TokenSet = TokenSet::new([TokenKind::Dot, TokenKind::Ellipsis]); + + let start = self.node_start(); + self.bump(TokenKind::From); + + let mut module = None; + let mut level = if self.eat(TokenKind::Ellipsis) { 3 } else { 0 }; + let mut progress = ParserProgress::default(); + + while self.at_ts(DOT_ELLIPSIS_SET) { + progress.assert_progressing(self); + + if self.eat(TokenKind::Dot) { + level += 1; + } + + if self.eat(TokenKind::Ellipsis) { + level += 3; + } + } + + if self.at(TokenKind::Name) { + module = Some(self.parse_dotted_name()); + }; + + if level == 0 && module.is_none() { + let range = self.current_range(); + self.add_error( + ParseErrorType::OtherError("missing module name".to_string()), + range, + ); + } + + self.expect(TokenKind::Import); + + let parenthesized = self.eat(TokenKind::Lpar); + let names = + self.parse_delimited_list(RecoveryContextKind::ImportNames, |p| p.parse_alias(), true); + + if parenthesized { + self.expect(TokenKind::Rpar); + } + + ast::StmtImportFrom { + module, + names, + level: Some(level), + range: self.node_range(start), + } + } + + /// See: + fn parse_pass_statement(&mut self) -> ast::StmtPass { + let start = self.node_start(); + self.bump(TokenKind::Pass); + ast::StmtPass { + range: self.node_range(start), + } + } + + /// See: + fn parse_continue_statement(&mut self) -> ast::StmtContinue { + let start = self.node_start(); + self.bump(TokenKind::Continue); + ast::StmtContinue { + range: self.node_range(start), + } + } + + /// See: + fn parse_break_statement(&mut self) -> ast::StmtBreak { + let start = self.node_start(); + self.bump(TokenKind::Break); + ast::StmtBreak { + range: self.node_range(start), + } + } + + /// See: + fn parse_assert_statement(&mut self) -> ast::StmtAssert { + let start = self.node_start(); + self.bump(TokenKind::Assert); + + let test = self.parse_conditional_expression_or_higher(); + + let msg = self + .eat(TokenKind::Comma) + .then(|| Box::new(self.parse_conditional_expression_or_higher().expr)); + + ast::StmtAssert { + test: Box::new(test.expr), + msg, + range: self.node_range(start), + } + } + + /// See: + fn parse_global_statement(&mut self) -> ast::StmtGlobal { + let start = self.node_start(); + self.bump(TokenKind::Global); + + let mut names = vec![]; + #[allow(deprecated)] + self.parse_separated(false, TokenKind::Comma, [TokenKind::Newline], |parser| { + names.push(parser.parse_identifier()); + }); + + ast::StmtGlobal { + range: self.node_range(start), + names, + } + } + + /// See: + fn parse_nonlocal_statement(&mut self) -> ast::StmtNonlocal { + let start = self.node_start(); + self.bump(TokenKind::Nonlocal); + + let mut names = vec![]; + #[allow(deprecated)] + self.parse_separated(false, TokenKind::Comma, [TokenKind::Newline], |parser| { + names.push(parser.parse_identifier()); + }); + + ast::StmtNonlocal { + range: self.node_range(start), + names, + } + } + + /// See: + fn parse_type_alias_statement(&mut self) -> ast::StmtTypeAlias { + let start = self.node_start(); + self.bump(TokenKind::Type); + + let (tok, tok_range) = self.next_token(); + let name = if let Tok::Name { name } = tok { + Expr::Name(ast::ExprName { + id: name.to_string(), + ctx: ExprContext::Store, + range: tok_range, + }) + } else { + self.add_error( + ParseErrorType::OtherError(format!("expecting identifier, got {tok}")), + tok_range, + ); + #[allow(deprecated)] + Expr::Invalid(ast::ExprInvalid { + value: self.src_text(tok_range).into(), + range: tok_range, + }) + }; + let type_params = self.try_parse_type_params(); + + self.expect(TokenKind::Equal); + + let value = self.parse_conditional_expression_or_higher(); + + ast::StmtTypeAlias { + name: Box::new(name), + type_params, + value: Box::new(value.expr), + range: self.node_range(start), + } + } + + fn parse_ipython_escape_command_statement(&mut self) -> ast::StmtIpyEscapeCommand { + let start = self.node_start(); + let (Tok::IpyEscapeCommand { value, kind }, _) = self.bump(TokenKind::EscapeCommand) else { + unreachable!() + }; + + ast::StmtIpyEscapeCommand { + range: self.node_range(start), + kind, + value, + } + } + + /// See: + fn parse_assign_statement(&mut self, target: ParsedExpr, start: TextSize) -> ast::StmtAssign { + let mut targets = vec![target.expr]; + let mut value = self.parse_expression(); + + if self.at(TokenKind::Equal) { + self.parse_sequence(RecoveryContextKind::AssignmentTargets, |p| { + p.bump(TokenKind::Equal); + + let mut parsed_expr = p.parse_expression(); + + std::mem::swap(&mut value, &mut parsed_expr); + + targets.push(parsed_expr.expr); + }); + } + + targets + .iter_mut() + .for_each(|target| helpers::set_expr_ctx(target, ExprContext::Store)); + + if !targets.iter().all(helpers::is_valid_assignment_target) { + targets + .iter() + .filter(|target| !helpers::is_valid_assignment_target(target)) + .for_each(|target| self.add_error(ParseErrorType::AssignmentError, target.range())); + } + + ast::StmtAssign { + targets, + value: Box::new(value.expr), + range: self.node_range(start), + } + } + + /// See: + fn parse_annotated_assignment_statement( + &mut self, + mut target: ParsedExpr, + start: TextSize, + ) -> ast::StmtAnnAssign { + if !helpers::is_valid_assignment_target(&target.expr) { + self.add_error(ParseErrorType::AssignmentError, target.range()); + } + + if matches!(target.expr, Expr::Tuple(_)) { + self.add_error( + ParseErrorType::OtherError( + "only single target (not tuple) can be annotated".into(), + ), + target.range(), + ); + } + + helpers::set_expr_ctx(&mut target.expr, ExprContext::Store); + + let simple = target.expr.is_name_expr() && !target.is_parenthesized; + let annotation = self.parse_expression(); + + if matches!( + annotation.expr, + Expr::Tuple(ast::ExprTuple { + parenthesized: false, + .. + }) + ) { + self.add_error( + ParseErrorType::OtherError("annotation cannot be unparenthesized".into()), + annotation.range(), + ); + } + + let value = self + .eat(TokenKind::Equal) + .then(|| Box::new(self.parse_expression().expr)); + + ast::StmtAnnAssign { + target: Box::new(target.expr), + annotation: Box::new(annotation.expr), + value, + simple, + range: self.node_range(start), + } + } + + /// See: + fn parse_augmented_assignment_statement( + &mut self, + mut target: ParsedExpr, + op: Operator, + start: TextSize, + ) -> ast::StmtAugAssign { + // Consume the operator + // FIXME(micha): assert that it is an augmented assign token + self.next_token(); + + if !helpers::is_valid_aug_assignment_target(&target.expr) { + self.add_error(ParseErrorType::AugAssignmentError, target.range()); + } + + helpers::set_expr_ctx(&mut target.expr, ExprContext::Store); + + let value = self.parse_expression(); + + ast::StmtAugAssign { + target: Box::new(target.expr), + op, + value: Box::new(value.expr), + range: self.node_range(start), + } + } + + /// See: + fn parse_if_statement(&mut self) -> ast::StmtIf { + let if_start = self.node_start(); + self.bump(TokenKind::If); + + let test = self.parse_named_expression_or_higher(); + self.expect(TokenKind::Colon); + + let body = self.parse_body(Clause::If); + + let elif_else_clauses = self.parse_elif_else_clauses(); + + ast::StmtIf { + test: Box::new(test.expr), + body, + elif_else_clauses, + range: self.node_range(if_start), + } + } + + fn parse_elif_else_clauses(&mut self) -> Vec { + let mut elif_else_clauses = if self.at(TokenKind::Elif) { + self.parse_clauses(Clause::ElIf, |p| { + let elif_start = p.node_start(); + p.bump(TokenKind::Elif); + + let test = p.parse_named_expression_or_higher(); + p.expect(TokenKind::Colon); + + let body = p.parse_body(Clause::ElIf); + + ast::ElifElseClause { + test: Some(test.expr), + body, + range: p.node_range(elif_start), + } + }) + } else { + Vec::new() + }; + + let else_start = self.node_start(); + if self.eat(TokenKind::Else) { + self.expect(TokenKind::Colon); + + let body = self.parse_body(Clause::Else); + + elif_else_clauses.push(ast::ElifElseClause { + test: None, + body, + range: self.node_range(else_start), + }); + } + + elif_else_clauses + } + + /// See: + fn parse_try_statement(&mut self) -> ast::StmtTry { + let try_start = self.node_start(); + self.bump(TokenKind::Try); + self.expect(TokenKind::Colon); + + let mut is_star = false; + + let try_body = self.parse_body(Clause::Try); + + let has_except = self.at(TokenKind::Except); + let handlers = self.parse_clauses(Clause::Except, |p| { + let except_start = p.node_start(); + p.bump(TokenKind::Except); + + // TODO(micha): Should this be local to the except block or global for the try statement or do we need to track both? + is_star = p.eat(TokenKind::Star); + + let type_ = if p.at(TokenKind::Colon) && !is_star { + None + } else { + let parsed_expr = p.parse_expression(); + if matches!( + parsed_expr.expr, + Expr::Tuple(ast::ExprTuple { + parenthesized: false, + .. + }) + ) { + p.add_error( + ParseErrorType::OtherError( + "multiple exception types must be parenthesized".to_string(), + ), + &parsed_expr, + ); + } + Some(Box::new(parsed_expr.expr)) + }; + + let name = p.eat(TokenKind::As).then(|| p.parse_identifier()); + + p.expect(TokenKind::Colon); + + let except_body = p.parse_body(Clause::Except); + + ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { + type_, + name, + body: except_body, + range: p.node_range(except_start), + }) + }); + + let orelse = if self.eat(TokenKind::Else) { + self.expect(TokenKind::Colon); + self.parse_body(Clause::Else) + } else { + vec![] + }; + + let (finalbody, has_finally) = if self.eat(TokenKind::Finally) { + self.expect(TokenKind::Colon); + (self.parse_body(Clause::Finally), true) + } else { + (vec![], false) + }; + + if !has_except && !has_finally { + let range = self.current_range(); + self.add_error( + ParseErrorType::OtherError( + "expecting `except` or `finally` after `try` block".to_string(), + ), + range, + ); + } + + let range = self.node_range(try_start); + + ast::StmtTry { + body: try_body, + handlers, + orelse, + finalbody, + is_star, + range, + } + } + + /// See: + fn parse_for_statement(&mut self, for_start: TextSize) -> ast::StmtFor { + self.bump(TokenKind::For); + + let saved_context = self.set_ctx(ParserCtxFlags::FOR_TARGET); + let mut target = self.parse_expression(); + self.restore_ctx(ParserCtxFlags::FOR_TARGET, saved_context); + + helpers::set_expr_ctx(&mut target.expr, ExprContext::Store); + + self.expect(TokenKind::In); + + let iter = self.parse_expression(); + + self.expect(TokenKind::Colon); + + let body = self.parse_body(Clause::For); + + let orelse = if self.eat(TokenKind::Else) { + self.expect(TokenKind::Colon); + self.parse_body(Clause::Else) + } else { + vec![] + }; + + ast::StmtFor { + target: Box::new(target.expr), + iter: Box::new(iter.expr), + is_async: false, + body, + orelse, + range: self.node_range(for_start), + } + } + + /// See: + fn parse_while_statement(&mut self) -> ast::StmtWhile { + let while_start = self.node_start(); + self.bump(TokenKind::While); + + let test = self.parse_named_expression_or_higher(); + self.expect(TokenKind::Colon); + + let body = self.parse_body(Clause::While); + + let orelse = if self.eat(TokenKind::Else) { + self.expect(TokenKind::Colon); + self.parse_body(Clause::Else) + } else { + vec![] + }; + + ast::StmtWhile { + test: Box::new(test.expr), + body, + orelse, + range: self.node_range(while_start), + } + } + + /// See: + fn parse_function_definition( + &mut self, + decorator_list: Vec, + start_offset: TextSize, + ) -> ast::StmtFunctionDef { + self.bump(TokenKind::Def); + let name = self.parse_identifier(); + let type_params = self.try_parse_type_params(); + + let parameters_start = self.node_start(); + self.expect(TokenKind::Lpar); + let mut parameters = self.parse_parameters(FunctionKind::FunctionDef); + self.expect(TokenKind::Rpar); + parameters.range = self.node_range(parameters_start); + + let returns = self.eat(TokenKind::Rarrow).then(|| { + let returns = self.parse_expression(); + if !returns.is_parenthesized && matches!(returns.expr, Expr::Tuple(_)) { + self.add_error( + ParseErrorType::OtherError( + "multiple return types must be parenthesized".to_string(), + ), + returns.range(), + ); + } + Box::new(returns.expr) + }); + + self.expect(TokenKind::Colon); + + let body = self.parse_body(Clause::FunctionDef); + + ast::StmtFunctionDef { + name, + type_params, + parameters: Box::new(parameters), + body, + decorator_list, + is_async: false, + returns, + range: self.node_range(start_offset), + } + } + + /// See: + fn parse_class_definition( + &mut self, + decorator_list: Vec, + start_offset: TextSize, + ) -> ast::StmtClassDef { + self.bump(TokenKind::Class); + + let name = self.parse_identifier(); + let type_params = self.try_parse_type_params(); + let arguments = self + .at(TokenKind::Lpar) + .then(|| Box::new(self.parse_arguments())); + + self.expect(TokenKind::Colon); + + let body = self.parse_body(Clause::Class); + + ast::StmtClassDef { + range: self.node_range(start_offset), + decorator_list, + name, + type_params: type_params.map(Box::new), + arguments, + body, + } + } + + /// See: + fn parse_with_statement(&mut self, start_offset: TextSize) -> ast::StmtWith { + self.bump(TokenKind::With); + + let items = self.parse_with_items(); + self.expect(TokenKind::Colon); + + let body = self.parse_body(Clause::With); + + ast::StmtWith { + items, + body, + is_async: false, + range: self.node_range(start_offset), + } + } + + fn parse_with_items(&mut self) -> Vec { + let mut items = vec![]; + + if !self.at_expr() { + let range = self.current_range(); + self.add_error( + ParseErrorType::OtherError("expecting expression after `with` keyword".to_string()), + range, + ); + return items; + } + + let has_seen_lpar = self.at(TokenKind::Lpar); + + // Consider the two `WithItem` examples below: + // 1) `(a) as A` + // 2) `(a)` + // + // In the first example, the `item` contains a parenthesized expression, + // while the second example is a parenthesized `WithItem`. This situation + // introduces ambiguity during parsing. When encountering an opening parenthesis + // `(,` the parser may initially assume it's parsing a parenthesized `WithItem`. + // However, this assumption doesn't hold for the first case, `(a) as A`, where + // `(a)` represents a parenthesized expression. + // + // To disambiguate, the following heuristic was created. First, assume we're + // parsing an expression, then we look for the following tokens: + // i) `as` keyword outside parenthesis + // ii) `,` outside or inside parenthesis + // iii) `:=` inside an 1-level nested parenthesis + // iv) `*` inside an 1-level nested parenthesis, representing a starred + // expression + // + // If we find case i we treat it as in case 1. For case ii, we only treat it as in + // case 1 if the comma is outside of parenthesis and we've seen an `Rpar` or `Lpar` + // before the comma. + // Cases iii and iv are special cases, when we find them, we treat it as in case 2. + // The reason for this is that the resulting AST node needs to be a tuple for cases + // iii and iv instead of multiple `WithItem`s. For example, `with (a, b := 0, c): ...` + // will be parsed as one `WithItem` containing a tuple, instead of three different `WithItem`s. + let mut treat_it_as_expr = true; + if has_seen_lpar { + let mut index = 1; + let mut paren_nesting = 1; + let mut ignore_comma_check = false; + let mut has_seen_rpar = false; + let mut has_seen_colon_equal = false; + let mut has_seen_star = false; + let mut prev_token = self.current_kind(); + loop { + match self.peek_nth(index) { + TokenKind::Lpar => { + paren_nesting += 1; + } + TokenKind::Rpar => { + paren_nesting -= 1; + has_seen_rpar = true; + } + // Check for `:=` inside an 1-level nested parens, e.g. `with (a, b := c): ...` + TokenKind::ColonEqual if paren_nesting == 1 => { + treat_it_as_expr = true; + ignore_comma_check = true; + has_seen_colon_equal = true; + } + // Check for starred expressions inside an 1-level nested parens, + // e.g. `with (a, *b): ...` + TokenKind::Star if paren_nesting == 1 && !LITERAL_SET.contains(prev_token) => { + treat_it_as_expr = true; + ignore_comma_check = true; + has_seen_star = true; + } + // Check for `as` keyword outside parens + TokenKind::As => { + treat_it_as_expr = paren_nesting == 0; + ignore_comma_check = true; + } + TokenKind::Comma if !ignore_comma_check => { + // If the comma is outside of parens, treat it as an expression + // if we've seen `(` and `)`. + if paren_nesting == 0 { + treat_it_as_expr = has_seen_lpar && has_seen_rpar; + } else if !has_seen_star && !has_seen_colon_equal { + treat_it_as_expr = false; + } + } + TokenKind::Colon | TokenKind::Newline => break, + _ => {} + } + + index += 1; + prev_token = self.peek_nth(index); + } + } + + if !treat_it_as_expr && has_seen_lpar { + self.bump(TokenKind::Lpar); + } + + let ending = if has_seen_lpar && treat_it_as_expr { + [TokenKind::Colon] + } else { + [TokenKind::Rpar] + }; + + let mut is_last_parenthesized = false; + #[allow(deprecated)] + self.parse_separated( + // Only allow a trailing delimiter if we've seen a `(`. + has_seen_lpar, + TokenKind::Comma, + ending, + |parser| { + let parsed_with_item = parser.parse_with_item(); + is_last_parenthesized = parsed_with_item.is_parenthesized; + items.push(parsed_with_item.item); + }, + ); + // Special-case: if we have a parenthesized `WithItem` that was parsed as + // an expression, then the item should _exclude_ the outer parentheses in + // its range. For example: + // ```python + // with (a := 0): pass + // with (*a): pass + // with (a): pass + // with (1 + 2): pass + // ``` + // In this case, the `(` and `)` are part of the `with` statement. + // The exception is when `WithItem` is an `()` (empty tuple). + if let [with_item] = items.as_mut_slice() { + if treat_it_as_expr + && with_item.optional_vars.is_none() + && is_last_parenthesized + && !matches!(with_item.context_expr, Expr::Tuple(_)) + { + with_item.range = with_item.range.add_start(1.into()).sub_end(1.into()); + } + } + + if !treat_it_as_expr && has_seen_lpar { + self.expect(TokenKind::Rpar); + } + + items + } + + fn parse_with_item(&mut self) -> ParsedWithItem { + let start = self.node_start(); + + let context_expr = self.parse_conditional_expression_or_higher(); + match context_expr.expr { + Expr::Starred(_) => { + self.add_error( + ParseErrorType::OtherError("starred expression not allowed".into()), + context_expr.range(), + ); + } + Expr::NamedExpr(_) if !context_expr.is_parenthesized => { + self.add_error( + ParseErrorType::OtherError( + "unparenthesized named expression not allowed".into(), + ), + context_expr.range(), + ); + } + _ => {} + } + + let optional_vars = if self.eat(TokenKind::As) { + let mut target = self.parse_conditional_expression_or_higher(); + + if matches!(target.expr, Expr::BoolOp(_) | Expr::Compare(_)) { + // Should we make `target` an `Expr::Invalid` here? + self.add_error( + ParseErrorType::OtherError( + "expression not allowed in `with` statement".to_string(), + ), + target.range(), + ); + } + + helpers::set_expr_ctx(&mut target.expr, ExprContext::Store); + + Some(Box::new(target.expr)) + } else { + None + }; + + ParsedWithItem { + is_parenthesized: context_expr.is_parenthesized, + item: ast::WithItem { + range: self.node_range(start), + context_expr: context_expr.expr, + optional_vars, + }, + } + } + + /// See: + fn parse_match_statement(&mut self) -> ast::StmtMatch { + let start_offset = self.node_start(); + + self.bump(TokenKind::Match); + + let subject_start = self.node_start(); + let subject = self.parse_named_expression_or_higher(); + let subject = if self.at(TokenKind::Comma) { + let tuple = self.parse_tuple_expression( + subject.expr, + subject_start, + false, + Parser::parse_named_expression_or_higher, + ); + + Expr::Tuple(tuple).into() + } else { + subject + }; + + self.expect(TokenKind::Colon); + + self.eat(TokenKind::Newline); + if !self.eat(TokenKind::Indent) { + let range = self.current_range(); + self.add_error( + ParseErrorType::OtherError( + "expected an indented block after `match` statement".to_string(), + ), + range, + ); + } + + let cases = self.parse_match_cases(); + + self.eat(TokenKind::Dedent); + + ast::StmtMatch { + subject: Box::new(subject.expr), + cases, + range: self.node_range(start_offset), + } + } + + fn parse_match_cases(&mut self) -> Vec { + if !self.at(TokenKind::Case) { + self.add_error( + ParseErrorType::OtherError("expecting `case` block after `match`".to_string()), + self.current_range(), + ); + } + + let mut cases = vec![]; + let mut progress = ParserProgress::default(); + + while self.at(TokenKind::Case) { + progress.assert_progressing(self); + cases.push(self.parse_match_case()); + } + + cases + } + + fn parse_match_case(&mut self) -> ast::MatchCase { + let start = self.node_start(); + + self.bump(TokenKind::Case); + let pattern = self.parse_match_patterns(); + + let guard = self + .eat(TokenKind::If) + .then(|| Box::new(self.parse_named_expression_or_higher().expr)); + + self.expect(TokenKind::Colon); + let body = self.parse_body(Clause::Match); + + ast::MatchCase { + pattern, + guard, + body, + range: self.node_range(start), + } + } + + /// Parses any statement that is valid after an `async` token. + /// See: + /// - + /// - + /// - + fn parse_async_statement(&mut self) -> Stmt { + let async_start = self.node_start(); + self.bump(TokenKind::Async); + + match self.current_kind() { + TokenKind::Def => Stmt::FunctionDef(ast::StmtFunctionDef { + is_async: true, + ..self.parse_function_definition(vec![], async_start) + }), + TokenKind::With => Stmt::With(ast::StmtWith { + is_async: true, + ..self.parse_with_statement(async_start) + }), + TokenKind::For => Stmt::For(ast::StmtFor { + is_async: true, + ..self.parse_for_statement(async_start) + }), + kind => { + // Although this statement is not a valid `async` statement, + // we still parse it. + self.add_error(ParseErrorType::StmtIsNotAsync(kind), self.current_range()); + self.parse_statement() + } + } + } + + fn parse_decorators(&mut self) -> Stmt { + let start_offset = self.node_start(); + + let mut decorators = vec![]; + let mut progress = ParserProgress::default(); + + while self.at(TokenKind::At) { + progress.assert_progressing(self); + let decorator_start = self.node_start(); + self.bump(TokenKind::At); + + let parsed_expr = self.parse_named_expression_or_higher(); + decorators.push(ast::Decorator { + expression: parsed_expr.expr, + range: self.node_range(decorator_start), + }); + + self.expect(TokenKind::Newline); + } + + match self.current_kind() { + TokenKind::Def => { + Stmt::FunctionDef(self.parse_function_definition(decorators, start_offset)) + } + TokenKind::Class => { + Stmt::ClassDef(self.parse_class_definition(decorators, start_offset)) + } + TokenKind::Async if self.peek_nth(1) == TokenKind::Def => { + self.bump(TokenKind::Async); + + Stmt::FunctionDef(ast::StmtFunctionDef { + is_async: true, + ..self.parse_function_definition(decorators, start_offset) + }) + } + _ => { + self.add_error( + ParseErrorType::OtherError( + "expected class, function definition or async function definition after decorator".to_string(), + ), + self.current_range(), + ); + self.parse_statement() + } + } + } + + /// Parses a single statement that's on the same line as the clause header or + /// an indented block. + fn parse_body(&mut self, parent_clause: Clause) -> Vec { + if self.eat(TokenKind::Newline) { + if self.at(TokenKind::Indent) { + return self.parse_block(); + } + } else if self.at_simple_stmt() { + return self.parse_simple_statements(); + } + + self.add_error( + ParseErrorType::OtherError(format!( + "expected a single statement or an indented body after {parent_clause}" + )), + self.current_range(), + ); + + Vec::new() + } + + fn parse_block(&mut self) -> Vec { + self.bump(TokenKind::Indent); + + let statements = + self.parse_list(RecoveryContextKind::BlockStatements, Self::parse_statement); + + self.expect(TokenKind::Dedent); + + statements + } + + fn parse_parameter(&mut self, function_kind: FunctionKind) -> ast::Parameter { + let start = self.node_start(); + let name = self.parse_identifier(); + // If we are at a colon and we're currently parsing a `lambda` expression, + // this is the `lambda`'s body, don't try to parse as an annotation. + let annotation = if function_kind == FunctionKind::FunctionDef && self.eat(TokenKind::Colon) + { + Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + } else { + None + }; + + ast::Parameter { + range: self.node_range(start), + name, + annotation, + } + } + + fn parse_parameter_with_default( + &mut self, + function_kind: FunctionKind, + ) -> ast::ParameterWithDefault { + let start = self.node_start(); + let parameter = self.parse_parameter(function_kind); + + let default = self + .eat(TokenKind::Equal) + .then(|| Box::new(self.parse_conditional_expression_or_higher().expr)); + + ast::ParameterWithDefault { + range: self.node_range(start), + parameter, + default, + } + } + + pub(super) fn parse_parameters(&mut self, function_kind: FunctionKind) -> ast::Parameters { + let mut args = vec![]; + let mut posonlyargs = vec![]; + let mut kwonlyargs = vec![]; + let mut kwarg = None; + let mut vararg = None; + + let mut has_seen_asterisk = false; + let mut has_seen_vararg = false; + let mut has_seen_default_param = false; + + let ending = match function_kind { + FunctionKind::Lambda => TokenKind::Colon, + FunctionKind::FunctionDef => TokenKind::Rpar, + }; + + let ending_set = TokenSet::new([TokenKind::Rarrow, ending]).union(COMPOUND_STMT_SET); + let start = self.node_start(); + + #[allow(deprecated)] + self.parse_separated(true, TokenKind::Comma, ending_set, |parser| { + // Don't allow any parameter after we have seen a vararg `**kwargs` + if has_seen_vararg { + parser.add_error( + ParseErrorType::ParamFollowsVarKeywordParam, + parser.current_range(), + ); + } + + if parser.eat(TokenKind::Star) { + has_seen_asterisk = true; + if parser.at(TokenKind::Comma) { + has_seen_default_param = false; + } else if parser.at_expr() { + let param = parser.parse_parameter(function_kind); + vararg = Some(Box::new(param)); + } + } else if parser.eat(TokenKind::DoubleStar) { + has_seen_vararg = true; + let param = parser.parse_parameter(function_kind); + kwarg = Some(Box::new(param)); + } else if parser.eat(TokenKind::Slash) { + // Don't allow `/` after a `*` + if has_seen_asterisk { + parser.add_error( + ParseErrorType::OtherError("`/` must be ahead of `*`".to_string()), + parser.current_range(), + ); + } + std::mem::swap(&mut args, &mut posonlyargs); + } else if parser.at(TokenKind::Name) { + let param = parser.parse_parameter_with_default(function_kind); + // Don't allow non-default parameters after default parameters e.g. `a=1, b`, + // can't place `b` after `a=1`. Non-default parameters are only allowed after + // default parameters if we have a `*` before them, e.g. `a=1, *, b`. + if param.default.is_none() && has_seen_default_param && !has_seen_asterisk { + parser.add_error(ParseErrorType::DefaultArgumentError, parser.current_range()); + } + has_seen_default_param = param.default.is_some(); + + if has_seen_asterisk { + kwonlyargs.push(param); + } else { + args.push(param); + } + } else { + if parser.at_ts(SIMPLE_STMT_SET) { + return; + } + + let range = parser.current_range(); + #[allow(deprecated)] + parser.skip_until( + ending_set.union(TokenSet::new([TokenKind::Comma, TokenKind::Colon])), + ); + parser.add_error( + ParseErrorType::OtherError("expected parameter".to_string()), + range.cover(parser.current_range()), // TODO(micha): This goes one token too far? + ); + } + }); + + let parameters = ast::Parameters { + range: self.node_range(start), + posonlyargs, + args, + vararg, + kwonlyargs, + kwarg, + }; + + if let Err(error) = helpers::validate_parameters(¶meters) { + self.add_error(error.error, error.location); + } + + parameters + } + + fn parse_type_params(&mut self) -> ast::TypeParams { + let start = self.node_start(); + + self.bump(TokenKind::Lsqb); + + let type_params = self.parse_delimited_list( + RecoveryContextKind::TypeParams, + |p| p.parse_type_param(), + true, + ); + + self.expect(TokenKind::Rsqb); + + ast::TypeParams { + range: self.node_range(start), + type_params, + } + } + + fn try_parse_type_params(&mut self) -> Option { + self.at(TokenKind::Lsqb).then(|| self.parse_type_params()) + } + + pub(super) fn is_at_type_param(&self) -> bool { + matches!( + self.current_kind(), + TokenKind::Star | TokenKind::DoubleStar | TokenKind::Name + ) || self.current_kind().is_keyword() + } + + fn parse_type_param(&mut self) -> ast::TypeParam { + let start = self.node_start(); + + if self.eat(TokenKind::Star) { + let name = self.parse_identifier(); + ast::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple { + range: self.node_range(start), + name, + }) + } else if self.eat(TokenKind::DoubleStar) { + let name = self.parse_identifier(); + ast::TypeParam::ParamSpec(ast::TypeParamParamSpec { + range: self.node_range(start), + name, + }) + } else { + let name = self.parse_identifier(); + let bound = self + .eat(TokenKind::Colon) + .then(|| Box::new(self.parse_conditional_expression_or_higher().expr)); + + ast::TypeParam::TypeVar(ast::TypeParamTypeVar { + range: self.node_range(start), + name, + bound, + }) + } + } + + fn parse_dotted_name(&mut self) -> ast::Identifier { + let start = self.node_start(); + + self.parse_identifier(); + + let mut progress = ParserProgress::default(); + while self.eat(TokenKind::Dot) { + progress.assert_progressing(self); + + let id = self.parse_identifier(); + if !id.is_valid() { + self.add_error( + ParseErrorType::OtherError("invalid identifier".into()), + id.range, + ); + } + } + + let range = self.node_range(start); + + ast::Identifier { + id: self.src_text(range).into(), + range, + } + } + + fn parse_alias(&mut self) -> ast::Alias { + let start = self.node_start(); + if self.eat(TokenKind::Star) { + let range = self.node_range(start); + return ast::Alias { + name: ast::Identifier { + id: "*".into(), + range, + }, + asname: None, + range, + }; + } + + let name = self.parse_dotted_name(); + let asname = self.eat(TokenKind::As).then(|| self.parse_identifier()); + + ast::Alias { + range: self.node_range(start), + name, + asname, + } + } + + /// Specialized [`Parser::parse_sequence`] for parsing a sequence of clauses. + /// + /// The difference is that the parser only continues parsing for as long as it sees the token indicating the start + /// of the specific clause. This is different from [`Parser::parse_sequence`] that performs error recovery when + /// the next token is not a list terminator or the start of a list element. + /// + /// The special method is necessary because Python uses indentation over explicit delimiters to indicate the end of a clause. + /// + /// ```python + /// if True: ... + /// elif False: ... + /// elf x: .... + /// else: ... + /// ``` + /// + /// It would be nice if the above example would recover and either skip over the `elf x: ...` or parse it as a nested statement + /// so that the parser recognises the `else` clause. But Python makes this hard (without writing custom error recovery logic) + /// because `elf x: ` could also be an annotated assignment that went wrong ;) + /// + /// For now, don't recover when parsing clause headers, but add the terminator tokens (e.g. `Else`) to the recovery context + /// so that expression recovery stops when it encounters an `else` token. + fn parse_clauses( + &mut self, + clause: Clause, + mut parse_clause: impl FnMut(&mut Parser<'src>) -> T, + ) -> Vec { + let mut clauses = Vec::new(); + let mut progress = ParserProgress::default(); + + let recovery_kind = match clause { + Clause::ElIf => RecoveryContextKind::Elif, + Clause::Except => RecoveryContextKind::Except, + _ => unreachable!("Clause is not supported"), + }; + + let saved_context = self.recovery_context; + self.recovery_context = self + .recovery_context + .union(RecoveryContext::from_kind(recovery_kind)); + + while recovery_kind.is_list_element(self) { + progress.assert_progressing(self); + + clauses.push(parse_clause(self)); + } + + self.recovery_context = saved_context; + + clauses + } +} + +#[derive(Copy, Clone)] +enum Clause { + If, + Else, + ElIf, + For, + With, + Class, + While, + FunctionDef, + Match, + Try, + Except, + Finally, +} + +impl Display for Clause { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Clause::If => write!(f, "`if` statement"), + Clause::Else => write!(f, "`else` clause"), + Clause::ElIf => write!(f, "`elif` clause"), + Clause::For => write!(f, "`for` statement"), + Clause::With => write!(f, "`with` statement"), + Clause::Class => write!(f, "`class` definition"), + Clause::While => write!(f, "`while` statement"), + Clause::FunctionDef => write!(f, "function definition"), + Clause::Match => write!(f, "`match` statement"), + Clause::Try => write!(f, "`try` statement"), + Clause::Except => write!(f, "`except` clause"), + Clause::Finally => write!(f, "`finally` clause"), + } + } +} + +struct ParsedWithItem { + item: WithItem, + is_parenthesized: bool, +} diff --git a/crates/ruff_python_parser/src/parser/tests.rs b/crates/ruff_python_parser/src/parser/tests.rs new file mode 100644 index 0000000000000..cd9d8b93e9c0f --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests.rs @@ -0,0 +1,44 @@ +use crate::parser::Parser; +use crate::token_source::TokenSource; +use crate::Mode; + +mod parser; +mod suite; + +// This is a sanity test for what looks like an ipython directive being +// assigned to. Although this doesn't actually parse as an assignment +// statement, but rather, a directive whose value is `foo = 42`. +#[test] +fn ok_ipy_escape_command() { + use crate::Mode; + + let src = r"!foo = 42"; + let tokens = crate::lexer::lex(src, Mode::Ipython).collect(); + let ast = crate::parse_tokens(tokens, src, Mode::Ipython); + insta::assert_debug_snapshot!(ast); +} + +// Test that is intentionally ignored by default. +// Use it for quickly debugging a parser issue. +#[test] +#[ignore] +fn parser_quick_test() { + let src = r"if True: + 1 + ... +if x < 1: + ... +else: + pass + +if a: + pass +elif b: + ... +"; + + let tokens = crate::lexer::lex(src, Mode::Module).collect(); + let ast = Parser::new(src, Mode::Module, TokenSource::new(tokens)).parse_program(); + + assert_eq!(&ast.parse_errors, &[]); +} diff --git a/crates/ruff_python_parser/src/parser/tests/parser.rs b/crates/ruff_python_parser/src/parser/tests/parser.rs new file mode 100644 index 0000000000000..5e2c74c47f977 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/parser.rs @@ -0,0 +1,1036 @@ +#[cfg(test)] +mod tests { + + use crate::{ + lexer::lex, + parser::{Parser, Program}, + Mode, + }; + use insta::assert_debug_snapshot; + + fn parse(src: &str) -> Program { + let mode = Mode::Module; + let lexer = lex(src, mode); + let parser = Parser::new(src, mode, lexer.collect()); + let program = parser.parse_program(); + + assert_eq!(&program.parse_errors, &[]); + program + } + + #[test] + fn parse_binary_exprs() { + assert_debug_snapshot!(parse( + " +1 + 2 +1 + 2 - 3 +1 + 2 - 3 + 4 +2 * 2 +1 + 2 * 2 +3 ** 2 +3 ** 2 * 5 +1 + (2 + 3) +1 << 2 +1 >> 2 +1 | 2 +1 ^ 2 +" + )); + } + + #[test] + fn parse_unary_exprs() { + assert_debug_snapshot!(parse( + " +-1 ++1 +~1 +-1 + 2 +---1 +not x + " + )); + } + + #[test] + fn parse_call_expr() { + assert_debug_snapshot!(parse( + " +l() +x(1, 2) +x(1, 2, x=3, y=4) +f(*l) +f(**a) +f(*a, b, **l) +f(*a, *b) +f( + [ + [a] + for d in f + ], +) +f( + { + [a] + for d in f + }, +) +f( + { + A: [a] + for d in f + }, +) +call( + a=1 if True else None, + x=0, +) +" + )); + } + + #[test] + fn parse_subscript_expr() { + assert_debug_snapshot!(parse( + " +l[0] +l[1:] +l[1:2] +l[:2] +l[:2:] +l[:2:3] +l[1:2:3] +l[:] +l[::] +l[0][0] +l[0,1] +l[0:,] +l[0:,1] +l[0:1, 2] +l[0:1:2, 3, i:i + 1] +x[a := b] +a[:, :11] +l[1,2,3] +x[~flag] +x[(a:=0):] +x[(a:=0):y] +" + )); + } + + #[test] + fn parse_attribute_expr() { + assert_debug_snapshot!(parse( + " +value.attr +value.attr() +value().attr +value().attr().foo +value.attr.foo +" + )); + } + + #[test] + fn parse_parenthesized_expr() { + assert_debug_snapshot!(parse( + " +(l) +(l)() +(l)()()() +(a +and +b +or +c) +" + )); + } + + #[test] + fn parse_bool_op_exprs() { + assert_debug_snapshot!(parse( + " +a and b +a and b and c +a or b +a or b or c +a and b or c +" + )); + } + + #[test] + fn parse_compare_expr() { + assert_debug_snapshot!(parse( + " +a == b +b < a +b > a +a >= b +a <= b +a != b +a is c +a in b +a not in c +a is not b +a < b == c > d is e not in f is not g <= h >= i != j +" + )); + } + + #[test] + fn parse_string_expr() { + assert_debug_snapshot!(parse( + r#" +'Hello World' +"😎" +'Foo' 'Bar' +( + 'A' + 'B' + 'C' +) +'''Olá, Mundo!''' +"""ABCDE""" +( + '''aB''' + '''cD''' +) +b'hello world' +b'bytes' b'concatenated' +"# + )); + } + + #[test] + fn parse_tuple_expr() { + assert_debug_snapshot!(parse( + " +1, 2 +1 + 2, +x and y, +(1, 2,) +(1,2,3,4) +(x + 1, l,) +() +1, 2, 3, 4 +" + )); + } + + #[test] + fn parse_generator_expr() { + assert_debug_snapshot!(parse( + " +(i for i in list) +(a async for i in iter) +(b for c in d if x in w if y and yy if z) +(a for b in c if d and e for f in j if k > h) +(a for b in c if d and e async for f in j if k > h) +f(x for i in l) +f(a, x for i in l) +f(a, x for i, j in l) +" + )); + } + + #[test] + fn parse_list_expr() { + assert_debug_snapshot!(parse( + " +[1 + i, [1, 2, 3, 4], (a, i + x, y), {a, b, c}, {a: 1}] +[1, 2, 3] +[] +[1] +[f(g(attr.H()) for c in l)] +" + )); + } + + #[test] + fn parse_list_comp_expr() { + assert_debug_snapshot!(parse( + " +[x for i in range(5)] +[b for c in d if x in w if y and yy if z] +[a for b in c if d and e for f in j if k > h] +[a for b in c if d and e async for f in j if k > h] +[1 for i in x in a] +[a for a, b in G] +[ + await x for a, b in C +] +[i for i in await x if entity is not None] +[x for x in (l if True else L) if T] +[i for i in (await x if True else X) if F] +[i for i in await (x if True else X) if F] +[f for f in c(x if True else [])] +" + )); + } + + #[test] + fn parse_set_expr() { + assert_debug_snapshot!(parse( + " +{1, 2, 3} +{1 + 2, (a, b), {1,2,3}, {a:b, **d}} +{a} +" + )); + } + + #[test] + fn parse_set_comp_expr() { + assert_debug_snapshot!(parse( + " +{x for i in ll} +{b for c in d if x in w if y and yy if z} +{a for b in c if d and e for f in j if k > h} +{a for b in c if d and e async for f in j if k > h} +{a for a, b in G} +" + )); + } + + #[test] + fn parse_dict_expr() { + assert_debug_snapshot!(parse( + " +{} +{1:2, a:1, b:'hello'} +{a:b, **d} +{'foo': 'bar', **{'nested': 'dict'}} +{x + 1: y * 2, **call()} +{l: [1, 2, 3], t: (1,2,3), d: {1:2, 3:4}, s: {1, 2}} +{**d} +{1: 2, **{'nested': 'dict'}} +{a: c} +{i: tuple(j for j in t if i != j) + for t in L + for i in t} +{ + 'A': lambda p: None, + 'B': C, +} +{**a, **b} +" + )); + } + + #[test] + fn parse_dict_comp_expr() { + assert_debug_snapshot!(parse( + " +{1: 2 for i in a} +{x + 1: 'x' for i in range(5)} +{b: c * 2 for c in d if x in w if y and yy if z} +{a: a ** 2 for b in c if d and e for f in j if k > h} +{a: b for b in c if d and e async for f in j if k > h} +{a: a for b, c in d} +" + )); + } + + #[test] + fn parse_starred_expr() { + assert_debug_snapshot!(parse( + " +*a +*(a + 1) +*x.attr +" + )); + } + + #[test] + fn parse_await_expr() { + assert_debug_snapshot!(parse( + " +await x +await x + 1 +await a and b +await f() +await [1, 2] +await {3, 4} +await {i: 5} +await 7, 8 +await (9, 10) +await 1 == 1 +await x if True else None +" + )); + } + + #[test] + fn parse_yield_expr() { + assert_debug_snapshot!(parse( + " +yield *y +yield x +yield x + 1 +yield a and b +yield f() +yield [1, 2] +yield {3, 4} +yield {i: 5} +yield 7, 8 +yield (9, 10) +yield 1 == 1 +" + )); + } + + #[test] + fn parse_yield_from_expr() { + assert_debug_snapshot!(parse( + " +yield from x +yield from x + 1 +yield from a and b +yield from f() +yield from [1, 2] +yield from {3, 4} +yield from {i: 5} +yield from (9, 10) +yield from 1 == 1 +" + )); + } + + #[test] + fn parse_if_else_expr() { + assert_debug_snapshot!(parse( + " +a if True else b +f() if x else None +a if b else c if d else e +1 + x if 1 < 0 else -1 +a and b if x else False +x <= y if y else x +True if a and b else False +1, 1 if a else c +" + )); + } + + #[test] + fn parse_lambda_expr() { + assert_debug_snapshot!(parse( + " +lambda: a +lambda x: 1 +lambda x, y: ... +lambda y, z=1: z * y +lambda *a: a +lambda *a, z, x=0: ... +lambda **kwargs: f() +lambda *args, **kwargs: f() + 1 +lambda *args, a, b=1, **kwargs: f() + 1 +lambda a, /: ... +lambda a, /, b: ... +lambda a=1, /,: ... +lambda a, b, /, *, c: ... +lambda kw=1, *, a: ... +" + )); + } + + #[test] + fn parse_named_expr() { + assert_debug_snapshot!(parse( + " +(x:=1) +{ x := 1 } +[x := 1] +(x := 1 + 1) +(x,y := a and b) +{ x,y := a < b } +[x,y := ...] +f(a:=b, c:=d) +" + )); + } + + #[test] + fn parse_if_stmt() { + assert_debug_snapshot!(parse( + " +if True: + 1 + ... +if x < 1: + ... +else: + pass + +if a: + pass +elif b: + ... + +if a and b: + ... +elif True: + ... +elif c: + ... +elif d: + ... +else: + f() +if a:=b: ... +" + )); + } + + #[test] + fn parse_simple_stmts() { + assert_debug_snapshot!(parse( + " +if x: ... +if True: pass +1; 2; pass +1; ...; a if b else c + +continue + +break + +del a +del a, b, 1, 1 + 2, +del a, (b, c), d + +assert 1 < 2 +assert f() +assert a and b +assert x, 'error' + +global a +global a, b, c + +return +return a and b +return 1 < 2 +return None +return 1, 2, +return x +return f() +return a.f() + +nonlocal a +nonlocal a, b, c + +raise +raise a +raise 1 < 2 +raise a and b +raise a from b + +import a +import a.b.c +import a.b.c as d +import a, b, c +import foo.bar as a, a.b.c.d as abcd + +from a import b # comment +from . import a +from foo.bar import baz as b, FooBar as fb +from .a import b +from ... import c +from .......................... import d +from ..........................a.b.c import d +from module import (a, b as B, c,) +from a import * + +if c: B; del A +else: C +if x: yield x; +" + )); + } + + #[test] + fn parse_func_def_stmt() { + assert_debug_snapshot!(parse( + " +def f(): + ... +def x() -> int: + f() + pass + ... +def mul(x, y) -> 'str': + x * y +def f1(*a): ... +def f2(*a, z, x=0): ... +def f3(**kwargs): f() +def f4(*args, **kwargs): f() + 1 +def f5(*args, a, b=1, **kwargs): f() + 1 +def f6(a, /): ... +def f7(a, /, b): ... +def f8(a=1, /,): ... +def f9(a, b, /, *, c): ... +def f10(kw=1, *, a): ... +def f11(x: int, y: 'str', z: 1 + 2): pass +def f12(self, a=1, b=2, c=3): ... +" + )); + } + + #[test] + fn parse_class_def_stmt() { + assert_debug_snapshot!(parse( + " +class T: + ... +class Test(): + def __init__(self): + pass +class T(a=1, *A, **k): + ... +class T: + def f(): + a, b = l +" + )); + } + + #[test] + fn parse_decorators() { + assert_debug_snapshot!(parse( + " +@a +def f(): ... + +@a.b.c +def f(): ... + +@a +@a.b.c +def f(): ... + +@a +@1 | 2 +@a.b.c +class T: ... + +@named_expr := abc +def f(): + ... +" + )); + } + + #[test] + fn parse_with_stmt() { + assert_debug_snapshot!(parse( + " +with x: + ... +with x, y: + ... +with open() as f: + ... +with f() as x.attr: + pass +with x as X, y as Y, z as Z: + ... +with (x, z as Y, y,): + ... +with (a) as f: + ... +with ((a) as f, 1): + ... +with a: + yield a, b +with (yield 1): + ... +with (yield from 1): + ... +with (a := 1): + ... +with (open('bla.txt')), (open('bla.txt')): + pass +with (a := 1, x): + ... +with (p / 'new_file').open('wb'): ... +" + )); + } + + #[test] + fn parse_while_stmt() { + assert_debug_snapshot!(parse( + " +while x: + ... +while (x > 1) and y: + pass +else: + ... +while x and y: + ... + print('Hello World!') + +else: + print('Olá, Mundo!') + ... +while a := b: ... +" + )); + } + + #[test] + fn parse_for_stmt() { + assert_debug_snapshot!(parse( + " +for i in x: + ... +for x.attr in f(): + pass +for 1 + 2 in x.attr: + ... +for i in x <= y: + pass +for i in a and b: + pass +for a,b,c, in iter: + ... +for (a, b) in iter: + ... +for i in *x.attr: + ... +for -i in [1, 2]: + ... +for *l in a, b, c,: + ... +else: + pass +" + )); + } + + #[test] + fn parse_try_stmt() { + assert_debug_snapshot!(parse( + " +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* a as A: + ... +except* b: + ... +" + )); + } + + #[test] + fn parse_async_stmt() { + assert_debug_snapshot!(parse( + " +async def f(): + ... + +async for i in iter: + ... + +async with x: + ... + +@a +async def x(): + ... +" + )); + } + + #[test] + fn parse_assign_stmt() { + assert_debug_snapshot!(parse( + " +x = 1 +[] = *l +() = *t +a, b = ab +*a = 1 + 2 +a = b = c +foo.bar = False +baz[0] = 42 +" + )); + } + + #[test] + fn parse_ann_assign_stmt() { + assert_debug_snapshot!(parse( + " +x: int +(y): 1 + 2 +var: tuple[int] | int = 1, +" + )); + } + + #[test] + fn parse_aug_assign_stmt() { + assert_debug_snapshot!(parse( + " +a += 1 +a *= b +a -= 1 +a /= a + 1 +a //= (a + b) - c ** 2 +a @= [1,2] +a %= x +a |= 1 +a <<= 2 +a >>= 2 +a ^= ... +a **= 42 +" + )); + } + + #[test] + fn parse_match_stmt() { + assert_debug_snapshot!(parse( + " +# PatternMatchSingleton +match x: + case None: + ... + case True: + ... + case False: + ... + +# PatternMatchValue +match x: + case a.b: + ... + case a.b.c: + ... + case '': + ... + case b'': + ... + case 1: + ... + case 1.0: + ... + case 1.0J: + ... + case 1 + 1: + ... + case -1: + ... + case -1.: + ... + case -0b01: + ... + case (1): + ... + +# PatternMatchOr +match x: + case 1 | 2: + ... + case '' | 1.1 | -1 | 1 + 1 | a.b: + ... + +# PatternMatchAs +match x: + case a as b: + ... + case 1 | 2 as two: + ... + case 1 + 3 as sum: + ... + case a.b as ab: + ... + case _: + ... + case _ as x: + ... + +# PatternMatchSequence +match x: + case 1, 2, 3: + ... + case (1, 2, 3,): + ... + case (1 + 2, a, None, a.b): + ... + case (1 as X, b) as S: + ... + case [1, 2, 3 + 1]: + ... + case ([1,2], 3): + ... + case [1]: + ... + +# PatternMatchStar +match x: + case *a: + ... + case *_: + ... + case [1, 2, *rest]: + ... + case (*_, 1, 2): + ... + +# PatternMatchClass +match x: + case Point(): + ... + case a.b.Point(): + ... + case Point2D(x=0): + ... + case Point2D(x=0, y=0,): + ... + case Point2D(0, 1): + ... + case Point2D([0, 1], y=1): + ... + case Point2D(x=[0, 1], y=1): + ... + +# PatternMatchMapping +match x := b: + case {1: _}: + ... + case {'': a, None: (1, 2), **rest}: + ... + +# Pattern guard +match y: + case a if b := c: ... + case e if 1 < 2: ... +" + )); + } + + #[test] + fn parse_type_alias_stmt() { + assert_debug_snapshot!(parse( + " +type Point = tuple[float, float] +type Point[T] = tuple[T, T] +type IntFunc[**P] = Callable[P, int] # ParamSpec +type LabeledTuple[*Ts] = tuple[str, *Ts] # TypeVarTuple +type HashableSequence[T: Hashable] = Sequence[T] # TypeVar with bound +type IntOrStrSequence[T: (int, str)] = Sequence[T] # TypeVar with constraints +" + )); + } + + #[test] + fn parse_type_params() { + assert_debug_snapshot!(parse( + " +def max[T](args: Iterable[T]) -> T: + ... +class list[T]: + ... +" + )); + } + + #[test] + fn parse_empty_fstring() { + assert_debug_snapshot!(parse( + r#" +f"" +F"" +f'' +f"""""" +f'''''' +"# + )); + } + + #[test] + fn parse_fstring() { + assert_debug_snapshot!(parse( + r#" +f"normal {foo} {{another}} {bar} {{{three}}}" +f"normal {foo!a} {bar!s} {baz!r} {foobar}" +f"normal {x:y + 2}" +f"{x:{{1}.pop()}}" +f"{(lambda x:{x})}" +f"{x =}" +f"{ x = }" +f"{x=!a}" +f"{x:.3f!r =}" +f"{x = !r :.3f}" +f"{x:.3f=!r}" +"hello" f"{x}" +f"{x}" f"{y}" +f"{x}" "world" +f"Invalid args in command: {command, *args}" +"foo" f"{x}" "bar" +( + f"a" + F"b" + "c" + rf"d" + fr"e" +) +"# + )); + } +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_ann_assign_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_ann_assign_stmt.snap new file mode 100644 index 0000000000000..6a3bbc7ee0af7 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_ann_assign_stmt.snap @@ -0,0 +1,136 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nx: int\n(y): 1 + 2\nvar: tuple[int] | int = 1,\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..46, + body: [ + AnnAssign( + StmtAnnAssign { + range: 1..7, + target: Name( + ExprName { + range: 1..2, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 4..7, + id: "int", + ctx: Load, + }, + ), + value: None, + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 8..18, + target: Name( + ExprName { + range: 9..10, + id: "y", + ctx: Store, + }, + ), + annotation: BinOp( + ExprBinOp { + range: 13..18, + left: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 17..18, + value: Int( + 2, + ), + }, + ), + }, + ), + value: None, + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 19..45, + target: Name( + ExprName { + range: 19..22, + id: "var", + ctx: Store, + }, + ), + annotation: BinOp( + ExprBinOp { + range: 24..40, + left: Subscript( + ExprSubscript { + range: 24..34, + value: Name( + ExprName { + range: 24..29, + id: "tuple", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 30..33, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 37..40, + id: "int", + ctx: Load, + }, + ), + }, + ), + value: Some( + Tuple( + ExprTuple { + range: 43..45, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 43..44, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + simple: true, + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_assign_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_assign_stmt.snap new file mode 100644 index 0000000000000..581ab0cc1d8d3 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_assign_stmt.snap @@ -0,0 +1,264 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nx = 1\n[] = *l\n() = *t\na, b = ab\n*a = 1 + 2\na = b = c\nfoo.bar = False\nbaz[0] = 42\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..82, + body: [ + Assign( + StmtAssign { + range: 1..6, + targets: [ + Name( + ExprName { + range: 1..2, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 7..14, + targets: [ + List( + ExprList { + range: 7..9, + elts: [], + ctx: Store, + }, + ), + ], + value: Starred( + ExprStarred { + range: 12..14, + value: Name( + ExprName { + range: 13..14, + id: "l", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 15..22, + targets: [ + Tuple( + ExprTuple { + range: 15..17, + elts: [], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Starred( + ExprStarred { + range: 20..22, + value: Name( + ExprName { + range: 21..22, + id: "t", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 23..32, + targets: [ + Tuple( + ExprTuple { + range: 23..27, + elts: [ + Name( + ExprName { + range: 23..24, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 26..27, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + ], + value: Name( + ExprName { + range: 30..32, + id: "ab", + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 33..43, + targets: [ + Starred( + ExprStarred { + range: 33..35, + value: Name( + ExprName { + range: 34..35, + id: "a", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + value: BinOp( + ExprBinOp { + range: 38..43, + left: NumberLiteral( + ExprNumberLiteral { + range: 38..39, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 42..43, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 44..53, + targets: [ + Name( + ExprName { + range: 44..45, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 48..49, + id: "b", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 52..53, + id: "c", + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 54..69, + targets: [ + Attribute( + ExprAttribute { + range: 54..61, + value: Name( + ExprName { + range: 54..57, + id: "foo", + ctx: Load, + }, + ), + attr: Identifier { + id: "bar", + range: 58..61, + }, + ctx: Store, + }, + ), + ], + value: BooleanLiteral( + ExprBooleanLiteral { + range: 64..69, + value: false, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 70..81, + targets: [ + Subscript( + ExprSubscript { + range: 70..76, + value: Name( + ExprName { + range: 70..73, + id: "baz", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 74..75, + value: Int( + 0, + ), + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 79..81, + value: Int( + 42, + ), + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_async_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_async_stmt.snap new file mode 100644 index 0000000000000..724d9e4c8b8c3 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_async_stmt.snap @@ -0,0 +1,155 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nasync def f():\n ...\n\nasync for i in iter:\n ...\n\nasync with x:\n ...\n\n@a\nasync def x():\n ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..104, + body: [ + FunctionDef( + StmtFunctionDef { + range: 1..23, + is_async: true, + decorator_list: [], + name: Identifier { + id: "f", + range: 11..12, + }, + type_params: None, + parameters: Parameters { + range: 12..14, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 20..23, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 20..23, + }, + ), + }, + ), + ], + }, + ), + For( + StmtFor { + range: 25..53, + is_async: true, + target: Name( + ExprName { + range: 35..36, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 40..44, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 50..53, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 50..53, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + With( + StmtWith { + range: 55..76, + is_async: true, + items: [ + WithItem { + range: 66..67, + context_expr: Name( + ExprName { + range: 66..67, + id: "x", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 73..76, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 73..76, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 78..103, + is_async: true, + decorator_list: [ + Decorator { + range: 78..80, + expression: Name( + ExprName { + range: 79..80, + id: "a", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "x", + range: 91..92, + }, + type_params: None, + parameters: Parameters { + range: 92..94, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 100..103, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 100..103, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_attribute_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_attribute_expr.snap new file mode 100644 index 0000000000000..de0c76b758869 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_attribute_expr.snap @@ -0,0 +1,184 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nvalue.attr\nvalue.attr()\nvalue().attr\nvalue().attr().foo\nvalue.attr.foo\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..72, + body: [ + Expr( + StmtExpr { + range: 1..11, + value: Attribute( + ExprAttribute { + range: 1..11, + value: Name( + ExprName { + range: 1..6, + id: "value", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 7..11, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..24, + value: Call( + ExprCall { + range: 12..24, + func: Attribute( + ExprAttribute { + range: 12..22, + value: Name( + ExprName { + range: 12..17, + id: "value", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 18..22, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 22..24, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 25..37, + value: Attribute( + ExprAttribute { + range: 25..37, + value: Call( + ExprCall { + range: 25..32, + func: Name( + ExprName { + range: 25..30, + id: "value", + ctx: Load, + }, + ), + arguments: Arguments { + range: 30..32, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "attr", + range: 33..37, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 38..56, + value: Attribute( + ExprAttribute { + range: 38..56, + value: Call( + ExprCall { + range: 38..52, + func: Attribute( + ExprAttribute { + range: 38..50, + value: Call( + ExprCall { + range: 38..45, + func: Name( + ExprName { + range: 38..43, + id: "value", + ctx: Load, + }, + ), + arguments: Arguments { + range: 43..45, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "attr", + range: 46..50, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 50..52, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "foo", + range: 53..56, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 57..71, + value: Attribute( + ExprAttribute { + range: 57..71, + value: Attribute( + ExprAttribute { + range: 57..67, + value: Name( + ExprName { + range: 57..62, + id: "value", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 63..67, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "foo", + range: 68..71, + }, + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_aug_assign_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_aug_assign_stmt.snap new file mode 100644 index 0000000000000..49ed63cc99e81 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_aug_assign_stmt.snap @@ -0,0 +1,329 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\na += 1\na *= b\na -= 1\na /= a + 1\na //= (a + b) - c ** 2\na @= [1,2]\na %= x\na |= 1\na <<= 2\na >>= 2\na ^= ...\na **= 42\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..115, + body: [ + AugAssign( + StmtAugAssign { + range: 1..7, + target: Name( + ExprName { + range: 1..2, + id: "a", + ctx: Store, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 6..7, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 8..14, + target: Name( + ExprName { + range: 8..9, + id: "a", + ctx: Store, + }, + ), + op: Mult, + value: Name( + ExprName { + range: 13..14, + id: "b", + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 15..21, + target: Name( + ExprName { + range: 15..16, + id: "a", + ctx: Store, + }, + ), + op: Sub, + value: NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 22..32, + target: Name( + ExprName { + range: 22..23, + id: "a", + ctx: Store, + }, + ), + op: Div, + value: BinOp( + ExprBinOp { + range: 27..32, + left: Name( + ExprName { + range: 27..28, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 31..32, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 33..55, + target: Name( + ExprName { + range: 33..34, + id: "a", + ctx: Store, + }, + ), + op: FloorDiv, + value: BinOp( + ExprBinOp { + range: 39..55, + left: BinOp( + ExprBinOp { + range: 40..45, + left: Name( + ExprName { + range: 40..41, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 44..45, + id: "b", + ctx: Load, + }, + ), + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 49..55, + left: Name( + ExprName { + range: 49..50, + id: "c", + ctx: Load, + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 54..55, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 56..66, + target: Name( + ExprName { + range: 56..57, + id: "a", + ctx: Store, + }, + ), + op: MatMult, + value: List( + ExprList { + range: 61..66, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 62..63, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 64..65, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 67..73, + target: Name( + ExprName { + range: 67..68, + id: "a", + ctx: Store, + }, + ), + op: Mod, + value: Name( + ExprName { + range: 72..73, + id: "x", + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 74..80, + target: Name( + ExprName { + range: 74..75, + id: "a", + ctx: Store, + }, + ), + op: BitOr, + value: NumberLiteral( + ExprNumberLiteral { + range: 79..80, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 81..88, + target: Name( + ExprName { + range: 81..82, + id: "a", + ctx: Store, + }, + ), + op: LShift, + value: NumberLiteral( + ExprNumberLiteral { + range: 87..88, + value: Int( + 2, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 89..96, + target: Name( + ExprName { + range: 89..90, + id: "a", + ctx: Store, + }, + ), + op: RShift, + value: NumberLiteral( + ExprNumberLiteral { + range: 95..96, + value: Int( + 2, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 97..105, + target: Name( + ExprName { + range: 97..98, + id: "a", + ctx: Store, + }, + ), + op: BitXor, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 102..105, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 106..114, + target: Name( + ExprName { + range: 106..107, + id: "a", + ctx: Store, + }, + ), + op: Pow, + value: NumberLiteral( + ExprNumberLiteral { + range: 112..114, + value: Int( + 42, + ), + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_await_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_await_expr.snap new file mode 100644 index 0000000000000..83c56f825ea1b --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_await_expr.snap @@ -0,0 +1,363 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nawait x\nawait x + 1\nawait a and b\nawait f()\nawait [1, 2]\nawait {3, 4}\nawait {i: 5}\nawait 7, 8\nawait (9, 10)\nawait 1 == 1\nawait x if True else None\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..148, + body: [ + Expr( + StmtExpr { + range: 1..8, + value: Await( + ExprAwait { + range: 1..8, + value: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 9..20, + value: BinOp( + ExprBinOp { + range: 9..20, + left: Await( + ExprAwait { + range: 9..16, + value: Name( + ExprName { + range: 15..16, + id: "x", + ctx: Load, + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..34, + value: BoolOp( + ExprBoolOp { + range: 21..34, + op: And, + values: [ + Await( + ExprAwait { + range: 21..28, + value: Name( + ExprName { + range: 27..28, + id: "a", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 33..34, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 35..44, + value: Await( + ExprAwait { + range: 35..44, + value: Call( + ExprCall { + range: 41..44, + func: Name( + ExprName { + range: 41..42, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 42..44, + args: [], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 45..57, + value: Await( + ExprAwait { + range: 45..57, + value: List( + ExprList { + range: 51..57, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 52..53, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 55..56, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 58..70, + value: Await( + ExprAwait { + range: 58..70, + value: Set( + ExprSet { + range: 64..70, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 68..69, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 71..83, + value: Await( + ExprAwait { + range: 71..83, + value: Dict( + ExprDict { + range: 77..83, + keys: [ + Some( + Name( + ExprName { + range: 78..79, + id: "i", + ctx: Load, + }, + ), + ), + ], + values: [ + NumberLiteral( + ExprNumberLiteral { + range: 81..82, + value: Int( + 5, + ), + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 84..94, + value: Tuple( + ExprTuple { + range: 84..94, + elts: [ + Await( + ExprAwait { + range: 84..91, + value: NumberLiteral( + ExprNumberLiteral { + range: 90..91, + value: Int( + 7, + ), + }, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 93..94, + value: Int( + 8, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 95..108, + value: Await( + ExprAwait { + range: 95..108, + value: Tuple( + ExprTuple { + range: 101..108, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 102..103, + value: Int( + 9, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 105..107, + value: Int( + 10, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 109..121, + value: Compare( + ExprCompare { + range: 109..121, + left: Await( + ExprAwait { + range: 109..116, + value: NumberLiteral( + ExprNumberLiteral { + range: 115..116, + value: Int( + 1, + ), + }, + ), + }, + ), + ops: [ + Eq, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 120..121, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 122..147, + value: IfExp( + ExprIfExp { + range: 122..147, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 133..137, + value: true, + }, + ), + body: Await( + ExprAwait { + range: 122..129, + value: Name( + ExprName { + range: 128..129, + id: "x", + ctx: Load, + }, + ), + }, + ), + orelse: NoneLiteral( + ExprNoneLiteral { + range: 143..147, + }, + ), + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_binary_exprs.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_binary_exprs.snap new file mode 100644 index 0000000000000..7f30989ff0863 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_binary_exprs.snap @@ -0,0 +1,422 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n1 + 2\n1 + 2 - 3\n1 + 2 - 3 + 4\n2 * 2\n1 + 2 * 2\n3 ** 2\n3 ** 2 * 5\n1 + (2 + 3)\n1 << 2\n1 >> 2\n1 | 2\n1 ^ 2\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..103, + body: [ + Expr( + StmtExpr { + range: 1..6, + value: BinOp( + ExprBinOp { + range: 1..6, + left: NumberLiteral( + ExprNumberLiteral { + range: 1..2, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 7..16, + value: BinOp( + ExprBinOp { + range: 7..16, + left: BinOp( + ExprBinOp { + range: 7..12, + left: NumberLiteral( + ExprNumberLiteral { + range: 7..8, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Sub, + right: NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 17..30, + value: BinOp( + ExprBinOp { + range: 17..30, + left: BinOp( + ExprBinOp { + range: 17..26, + left: BinOp( + ExprBinOp { + range: 17..22, + left: NumberLiteral( + ExprNumberLiteral { + range: 17..18, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 21..22, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Sub, + right: NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 3, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 29..30, + value: Int( + 4, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 31..36, + value: BinOp( + ExprBinOp { + range: 31..36, + left: NumberLiteral( + ExprNumberLiteral { + range: 31..32, + value: Int( + 2, + ), + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 35..36, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 37..46, + value: BinOp( + ExprBinOp { + range: 37..46, + left: NumberLiteral( + ExprNumberLiteral { + range: 37..38, + value: Int( + 1, + ), + }, + ), + op: Add, + right: BinOp( + ExprBinOp { + range: 41..46, + left: NumberLiteral( + ExprNumberLiteral { + range: 41..42, + value: Int( + 2, + ), + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 45..46, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 47..53, + value: BinOp( + ExprBinOp { + range: 47..53, + left: NumberLiteral( + ExprNumberLiteral { + range: 47..48, + value: Int( + 3, + ), + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 52..53, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..64, + value: BinOp( + ExprBinOp { + range: 54..64, + left: BinOp( + ExprBinOp { + range: 54..60, + left: NumberLiteral( + ExprNumberLiteral { + range: 54..55, + value: Int( + 3, + ), + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 59..60, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 63..64, + value: Int( + 5, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 65..76, + value: BinOp( + ExprBinOp { + range: 65..76, + left: NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 1, + ), + }, + ), + op: Add, + right: BinOp( + ExprBinOp { + range: 70..75, + left: NumberLiteral( + ExprNumberLiteral { + range: 70..71, + value: Int( + 2, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 74..75, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 77..83, + value: BinOp( + ExprBinOp { + range: 77..83, + left: NumberLiteral( + ExprNumberLiteral { + range: 77..78, + value: Int( + 1, + ), + }, + ), + op: LShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 82..83, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 84..90, + value: BinOp( + ExprBinOp { + range: 84..90, + left: NumberLiteral( + ExprNumberLiteral { + range: 84..85, + value: Int( + 1, + ), + }, + ), + op: RShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 91..96, + value: BinOp( + ExprBinOp { + range: 91..96, + left: NumberLiteral( + ExprNumberLiteral { + range: 91..92, + value: Int( + 1, + ), + }, + ), + op: BitOr, + right: NumberLiteral( + ExprNumberLiteral { + range: 95..96, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 97..102, + value: BinOp( + ExprBinOp { + range: 97..102, + left: NumberLiteral( + ExprNumberLiteral { + range: 97..98, + value: Int( + 1, + ), + }, + ), + op: BitXor, + right: NumberLiteral( + ExprNumberLiteral { + range: 101..102, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_bool_op_exprs.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_bool_op_exprs.snap new file mode 100644 index 0000000000000..07eaffa42e1ac --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_bool_op_exprs.snap @@ -0,0 +1,178 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\na and b\na and b and c\na or b\na or b or c\na and b or c\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..55, + body: [ + Expr( + StmtExpr { + range: 1..8, + value: BoolOp( + ExprBoolOp { + range: 1..8, + op: And, + values: [ + Name( + ExprName { + range: 1..2, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 7..8, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 9..22, + value: BoolOp( + ExprBoolOp { + range: 9..22, + op: And, + values: [ + Name( + ExprName { + range: 9..10, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 15..16, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 21..22, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 23..29, + value: BoolOp( + ExprBoolOp { + range: 23..29, + op: Or, + values: [ + Name( + ExprName { + range: 23..24, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 28..29, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..41, + value: BoolOp( + ExprBoolOp { + range: 30..41, + op: Or, + values: [ + Name( + ExprName { + range: 30..31, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 35..36, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 40..41, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 42..54, + value: BoolOp( + ExprBoolOp { + range: 42..54, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 42..49, + op: And, + values: [ + Name( + ExprName { + range: 42..43, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 48..49, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 53..54, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_call_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_call_expr.snap new file mode 100644 index 0000000000000..8b9ac9ce82342 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_call_expr.snap @@ -0,0 +1,595 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nl()\nx(1, 2)\nx(1, 2, x=3, y=4)\nf(*l)\nf(**a)\nf(*a, b, **l)\nf(*a, *b)\nf(\n [\n [a]\n for d in f\n ],\n)\nf(\n {\n [a]\n for d in f\n },\n)\nf(\n {\n A: [a]\n for d in f\n },\n)\ncall(\n a=1 if True else None,\n x=0,\n)\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..262, + body: [ + Expr( + StmtExpr { + range: 1..4, + value: Call( + ExprCall { + range: 1..4, + func: Name( + ExprName { + range: 1..2, + id: "l", + ctx: Load, + }, + ), + arguments: Arguments { + range: 2..4, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..12, + value: Call( + ExprCall { + range: 5..12, + func: Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + arguments: Arguments { + range: 6..12, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 7..8, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 2, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..30, + value: Call( + ExprCall { + range: 13..30, + func: Name( + ExprName { + range: 13..14, + id: "x", + ctx: Load, + }, + ), + arguments: Arguments { + range: 14..30, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 2, + ), + }, + ), + ], + keywords: [ + Keyword { + range: 21..24, + arg: Some( + Identifier { + id: "x", + range: 21..22, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 3, + ), + }, + ), + }, + Keyword { + range: 26..29, + arg: Some( + Identifier { + id: "y", + range: 26..27, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 4, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 31..36, + value: Call( + ExprCall { + range: 31..36, + func: Name( + ExprName { + range: 31..32, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 32..36, + args: [ + Starred( + ExprStarred { + range: 33..35, + value: Name( + ExprName { + range: 34..35, + id: "l", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 37..43, + value: Call( + ExprCall { + range: 37..43, + func: Name( + ExprName { + range: 37..38, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 38..43, + args: [], + keywords: [ + Keyword { + range: 39..42, + arg: None, + value: Name( + ExprName { + range: 41..42, + id: "a", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 44..57, + value: Call( + ExprCall { + range: 44..57, + func: Name( + ExprName { + range: 44..45, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 45..57, + args: [ + Starred( + ExprStarred { + range: 46..48, + value: Name( + ExprName { + range: 47..48, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 50..51, + id: "b", + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 53..56, + arg: None, + value: Name( + ExprName { + range: 55..56, + id: "l", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 58..67, + value: Call( + ExprCall { + range: 58..67, + func: Name( + ExprName { + range: 58..59, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 59..67, + args: [ + Starred( + ExprStarred { + range: 60..62, + value: Name( + ExprName { + range: 61..62, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 64..66, + value: Name( + ExprName { + range: 65..66, + id: "b", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 68..116, + value: Call( + ExprCall { + range: 68..116, + func: Name( + ExprName { + range: 68..69, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 69..116, + args: [ + ListComp( + ExprListComp { + range: 75..113, + elt: List( + ExprList { + range: 85..88, + elts: [ + Name( + ExprName { + range: 86..87, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 97..107, + target: Name( + ExprName { + range: 101..102, + id: "d", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 106..107, + id: "f", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 117..165, + value: Call( + ExprCall { + range: 117..165, + func: Name( + ExprName { + range: 117..118, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 118..165, + args: [ + SetComp( + ExprSetComp { + range: 124..162, + elt: List( + ExprList { + range: 134..137, + elts: [ + Name( + ExprName { + range: 135..136, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 146..156, + target: Name( + ExprName { + range: 150..151, + id: "d", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 155..156, + id: "f", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 166..217, + value: Call( + ExprCall { + range: 166..217, + func: Name( + ExprName { + range: 166..167, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 167..217, + args: [ + DictComp( + ExprDictComp { + range: 173..214, + key: Name( + ExprName { + range: 183..184, + id: "A", + ctx: Load, + }, + ), + value: List( + ExprList { + range: 186..189, + elts: [ + Name( + ExprName { + range: 187..188, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 198..208, + target: Name( + ExprName { + range: 202..203, + id: "d", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 207..208, + id: "f", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 218..261, + value: Call( + ExprCall { + range: 218..261, + func: Name( + ExprName { + range: 218..222, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 222..261, + args: [], + keywords: [ + Keyword { + range: 228..249, + arg: Some( + Identifier { + id: "a", + range: 228..229, + }, + ), + value: IfExp( + ExprIfExp { + range: 230..249, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 235..239, + value: true, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 230..231, + value: Int( + 1, + ), + }, + ), + orelse: NoneLiteral( + ExprNoneLiteral { + range: 245..249, + }, + ), + }, + ), + }, + Keyword { + range: 255..258, + arg: Some( + Identifier { + id: "x", + range: 255..256, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 257..258, + value: Int( + 0, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_class_def_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_class_def_stmt.snap new file mode 100644 index 0000000000000..e0fc4289edb66 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_class_def_stmt.snap @@ -0,0 +1,246 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nclass T:\n ...\nclass Test():\n def __init__(self):\n pass\nclass T(a=1, *A, **k):\n ...\nclass T:\n def f():\n a, b = l\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..147, + body: [ + ClassDef( + StmtClassDef { + range: 1..17, + decorator_list: [], + name: Identifier { + id: "T", + range: 7..8, + }, + type_params: None, + arguments: None, + body: [ + Expr( + StmtExpr { + range: 14..17, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 14..17, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 18..76, + decorator_list: [], + name: Identifier { + id: "Test", + range: 24..28, + }, + type_params: None, + arguments: Some( + Arguments { + range: 28..30, + args: [], + keywords: [], + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 40..76, + is_async: false, + decorator_list: [], + name: Identifier { + id: "__init__", + range: 44..52, + }, + type_params: None, + parameters: Parameters { + range: 52..58, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 53..57, + parameter: Parameter { + range: 53..57, + name: Identifier { + id: "self", + range: 53..57, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 72..76, + }, + ), + ], + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 77..107, + decorator_list: [], + name: Identifier { + id: "T", + range: 83..84, + }, + type_params: None, + arguments: Some( + Arguments { + range: 84..98, + args: [ + Starred( + ExprStarred { + range: 90..92, + value: Name( + ExprName { + range: 91..92, + id: "A", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 85..88, + arg: Some( + Identifier { + id: "a", + range: 85..86, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 87..88, + value: Int( + 1, + ), + }, + ), + }, + Keyword { + range: 94..97, + arg: None, + value: Name( + ExprName { + range: 96..97, + id: "k", + ctx: Load, + }, + ), + }, + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 104..107, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 104..107, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 108..146, + decorator_list: [], + name: Identifier { + id: "T", + range: 114..115, + }, + type_params: None, + arguments: None, + body: [ + FunctionDef( + StmtFunctionDef { + range: 121..146, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f", + range: 125..126, + }, + type_params: None, + parameters: Parameters { + range: 126..128, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Assign( + StmtAssign { + range: 138..146, + targets: [ + Tuple( + ExprTuple { + range: 138..142, + elts: [ + Name( + ExprName { + range: 138..139, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 141..142, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + ], + value: Name( + ExprName { + range: 145..146, + id: "l", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_compare_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_compare_expr.snap new file mode 100644 index 0000000000000..fef39b5039f35 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_compare_expr.snap @@ -0,0 +1,397 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\na == b\nb < a\nb > a\na >= b\na <= b\na != b\na is c\na in b\na not in c\na is not b\na < b == c > d is e not in f is not g <= h >= i != j\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..130, + body: [ + Expr( + StmtExpr { + range: 1..7, + value: Compare( + ExprCompare { + range: 1..7, + left: Name( + ExprName { + range: 1..2, + id: "a", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + ExprName { + range: 6..7, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 8..13, + value: Compare( + ExprCompare { + range: 8..13, + left: Name( + ExprName { + range: 8..9, + id: "b", + ctx: Load, + }, + ), + ops: [ + Lt, + ], + comparators: [ + Name( + ExprName { + range: 12..13, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 14..19, + value: Compare( + ExprCompare { + range: 14..19, + left: Name( + ExprName { + range: 14..15, + id: "b", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 18..19, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 20..26, + value: Compare( + ExprCompare { + range: 20..26, + left: Name( + ExprName { + range: 20..21, + id: "a", + ctx: Load, + }, + ), + ops: [ + GtE, + ], + comparators: [ + Name( + ExprName { + range: 25..26, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 27..33, + value: Compare( + ExprCompare { + range: 27..33, + left: Name( + ExprName { + range: 27..28, + id: "a", + ctx: Load, + }, + ), + ops: [ + LtE, + ], + comparators: [ + Name( + ExprName { + range: 32..33, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 34..40, + value: Compare( + ExprCompare { + range: 34..40, + left: Name( + ExprName { + range: 34..35, + id: "a", + ctx: Load, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Name( + ExprName { + range: 39..40, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 41..47, + value: Compare( + ExprCompare { + range: 41..47, + left: Name( + ExprName { + range: 41..42, + id: "a", + ctx: Load, + }, + ), + ops: [ + Is, + ], + comparators: [ + Name( + ExprName { + range: 46..47, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 48..54, + value: Compare( + ExprCompare { + range: 48..54, + left: Name( + ExprName { + range: 48..49, + id: "a", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 53..54, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 55..65, + value: Compare( + ExprCompare { + range: 55..65, + left: Name( + ExprName { + range: 55..56, + id: "a", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + ExprName { + range: 64..65, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 66..76, + value: Compare( + ExprCompare { + range: 66..76, + left: Name( + ExprName { + range: 66..67, + id: "a", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + Name( + ExprName { + range: 75..76, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 77..129, + value: Compare( + ExprCompare { + range: 77..129, + left: Name( + ExprName { + range: 77..78, + id: "a", + ctx: Load, + }, + ), + ops: [ + Lt, + Eq, + Gt, + Is, + NotIn, + IsNot, + LtE, + GtE, + NotEq, + ], + comparators: [ + Name( + ExprName { + range: 81..82, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 86..87, + id: "c", + ctx: Load, + }, + ), + Name( + ExprName { + range: 90..91, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 95..96, + id: "e", + ctx: Load, + }, + ), + Name( + ExprName { + range: 104..105, + id: "f", + ctx: Load, + }, + ), + Name( + ExprName { + range: 113..114, + id: "g", + ctx: Load, + }, + ), + Name( + ExprName { + range: 118..119, + id: "h", + ctx: Load, + }, + ), + Name( + ExprName { + range: 123..124, + id: "i", + ctx: Load, + }, + ), + Name( + ExprName { + range: 128..129, + id: "j", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_decorators.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_decorators.snap new file mode 100644 index 0000000000000..cf08786f6cdd2 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_decorators.snap @@ -0,0 +1,342 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n@a\ndef f(): ...\n\n@a.b.c\ndef f(): ...\n\n@a\n@a.b.c\ndef f(): ...\n\n@a\n@1 | 2\n@a.b.c\nclass T: ...\n\n@named_expr := abc\ndef f():\n ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..130, + body: [ + FunctionDef( + StmtFunctionDef { + range: 1..16, + is_async: false, + decorator_list: [ + Decorator { + range: 1..3, + expression: Name( + ExprName { + range: 2..3, + id: "a", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "f", + range: 8..9, + }, + type_params: None, + parameters: Parameters { + range: 9..11, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 13..16, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 13..16, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 18..37, + is_async: false, + decorator_list: [ + Decorator { + range: 18..24, + expression: Attribute( + ExprAttribute { + range: 19..24, + value: Attribute( + ExprAttribute { + range: 19..22, + value: Name( + ExprName { + range: 19..20, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 21..22, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "c", + range: 23..24, + }, + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "f", + range: 29..30, + }, + type_params: None, + parameters: Parameters { + range: 30..32, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 34..37, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 34..37, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 39..61, + is_async: false, + decorator_list: [ + Decorator { + range: 39..41, + expression: Name( + ExprName { + range: 40..41, + id: "a", + ctx: Load, + }, + ), + }, + Decorator { + range: 42..48, + expression: Attribute( + ExprAttribute { + range: 43..48, + value: Attribute( + ExprAttribute { + range: 43..46, + value: Name( + ExprName { + range: 43..44, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 45..46, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "c", + range: 47..48, + }, + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "f", + range: 53..54, + }, + type_params: None, + parameters: Parameters { + range: 54..56, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 58..61, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 58..61, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 63..92, + decorator_list: [ + Decorator { + range: 63..65, + expression: Name( + ExprName { + range: 64..65, + id: "a", + ctx: Load, + }, + ), + }, + Decorator { + range: 66..72, + expression: BinOp( + ExprBinOp { + range: 67..72, + left: NumberLiteral( + ExprNumberLiteral { + range: 67..68, + value: Int( + 1, + ), + }, + ), + op: BitOr, + right: NumberLiteral( + ExprNumberLiteral { + range: 71..72, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + Decorator { + range: 73..79, + expression: Attribute( + ExprAttribute { + range: 74..79, + value: Attribute( + ExprAttribute { + range: 74..77, + value: Name( + ExprName { + range: 74..75, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 76..77, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "c", + range: 78..79, + }, + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "T", + range: 86..87, + }, + type_params: None, + arguments: None, + body: [ + Expr( + StmtExpr { + range: 89..92, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 89..92, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 94..129, + is_async: false, + decorator_list: [ + Decorator { + range: 94..112, + expression: NamedExpr( + ExprNamedExpr { + range: 95..112, + target: Name( + ExprName { + range: 95..105, + id: "named_expr", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 109..112, + id: "abc", + ctx: Load, + }, + ), + }, + ), + }, + ], + name: Identifier { + id: "f", + range: 117..118, + }, + type_params: None, + parameters: Parameters { + range: 118..120, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 126..129, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 126..129, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_dict_comp_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_dict_comp_expr.snap new file mode 100644 index 0000000000000..06f0425685a2f --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_dict_comp_expr.snap @@ -0,0 +1,558 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n{1: 2 for i in a}\n{x + 1: 'x' for i in range(5)}\n{b: c * 2 for c in d if x in w if y and yy if z}\n{a: a ** 2 for b in c if d and e for f in j if k > h}\n{a: b for b in c if d and e async for f in j if k > h}\n{a: a for b, c in d}\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..229, + body: [ + Expr( + StmtExpr { + range: 1..18, + value: DictComp( + ExprDictComp { + range: 1..18, + key: NumberLiteral( + ExprNumberLiteral { + range: 2..3, + value: Int( + 1, + ), + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 2, + ), + }, + ), + generators: [ + Comprehension { + range: 7..17, + target: Name( + ExprName { + range: 11..12, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 16..17, + id: "a", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..49, + value: DictComp( + ExprDictComp { + range: 19..49, + key: BinOp( + ExprBinOp { + range: 20..25, + left: Name( + ExprName { + range: 20..21, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 24..25, + value: Int( + 1, + ), + }, + ), + }, + ), + value: StringLiteral( + ExprStringLiteral { + range: 27..30, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 27..30, + value: "x", + unicode: false, + }, + ), + }, + }, + ), + generators: [ + Comprehension { + range: 31..48, + target: Name( + ExprName { + range: 35..36, + id: "i", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 40..48, + func: Name( + ExprName { + range: 40..45, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 45..48, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 46..47, + value: Int( + 5, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 50..98, + value: DictComp( + ExprDictComp { + range: 50..98, + key: Name( + ExprName { + range: 51..52, + id: "b", + ctx: Load, + }, + ), + value: BinOp( + ExprBinOp { + range: 54..59, + left: Name( + ExprName { + range: 54..55, + id: "c", + ctx: Load, + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 58..59, + value: Int( + 2, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 60..97, + target: Name( + ExprName { + range: 64..65, + id: "c", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 69..70, + id: "d", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 74..80, + left: Name( + ExprName { + range: 74..75, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 79..80, + id: "w", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 84..92, + op: And, + values: [ + Name( + ExprName { + range: 84..85, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 90..92, + id: "yy", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 96..97, + id: "z", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 99..152, + value: DictComp( + ExprDictComp { + range: 99..152, + key: Name( + ExprName { + range: 100..101, + id: "a", + ctx: Load, + }, + ), + value: BinOp( + ExprBinOp { + range: 103..109, + left: Name( + ExprName { + range: 103..104, + id: "a", + ctx: Load, + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 108..109, + value: Int( + 2, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 110..131, + target: Name( + ExprName { + range: 114..115, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 119..120, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 124..131, + op: And, + values: [ + Name( + ExprName { + range: 124..125, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 130..131, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 132..151, + target: Name( + ExprName { + range: 136..137, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 141..142, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 146..151, + left: Name( + ExprName { + range: 146..147, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 150..151, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 153..207, + value: DictComp( + ExprDictComp { + range: 153..207, + key: Name( + ExprName { + range: 154..155, + id: "a", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 157..158, + id: "b", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 159..180, + target: Name( + ExprName { + range: 163..164, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 168..169, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 173..180, + op: And, + values: [ + Name( + ExprName { + range: 173..174, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 179..180, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 181..206, + target: Name( + ExprName { + range: 191..192, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 196..197, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 201..206, + left: Name( + ExprName { + range: 201..202, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 205..206, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: true, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 208..228, + value: DictComp( + ExprDictComp { + range: 208..228, + key: Name( + ExprName { + range: 209..210, + id: "a", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 212..213, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 214..227, + target: Tuple( + ExprTuple { + range: 218..222, + elts: [ + Name( + ExprName { + range: 218..219, + id: "b", + ctx: Store, + }, + ), + Name( + ExprName { + range: 221..222, + id: "c", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 226..227, + id: "d", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_dict_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_dict_expr.snap new file mode 100644 index 0000000000000..d75410dfaa387 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_dict_expr.snap @@ -0,0 +1,852 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n{}\n{1:2, a:1, b:'hello'}\n{a:b, **d}\n{'foo': 'bar', **{'nested': 'dict'}}\n{x + 1: y * 2, **call()}\n{l: [1, 2, 3], t: (1,2,3), d: {1:2, 3:4}, s: {1, 2}}\n{**d}\n{1: 2, **{'nested': 'dict'}}\n{a: c}\n{i: tuple(j for j in t if i != j)\n for t in L\n for i in t}\n{\n 'A': lambda p: None,\n 'B': C,\n}\n{**a, **b}\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..325, + body: [ + Expr( + StmtExpr { + range: 1..3, + value: Dict( + ExprDict { + range: 1..3, + keys: [], + values: [], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 4..25, + value: Dict( + ExprDict { + range: 4..25, + keys: [ + Some( + NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + ), + Some( + Name( + ExprName { + range: 10..11, + id: "a", + ctx: Load, + }, + ), + ), + Some( + Name( + ExprName { + range: 15..16, + id: "b", + ctx: Load, + }, + ), + ), + ], + values: [ + NumberLiteral( + ExprNumberLiteral { + range: 7..8, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 12..13, + value: Int( + 1, + ), + }, + ), + StringLiteral( + ExprStringLiteral { + range: 17..24, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 17..24, + value: "hello", + unicode: false, + }, + ), + }, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 26..36, + value: Dict( + ExprDict { + range: 26..36, + keys: [ + Some( + Name( + ExprName { + range: 27..28, + id: "a", + ctx: Load, + }, + ), + ), + None, + ], + values: [ + Name( + ExprName { + range: 29..30, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 34..35, + id: "d", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 37..73, + value: Dict( + ExprDict { + range: 37..73, + keys: [ + Some( + StringLiteral( + ExprStringLiteral { + range: 38..43, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 38..43, + value: "foo", + unicode: false, + }, + ), + }, + }, + ), + ), + None, + ], + values: [ + StringLiteral( + ExprStringLiteral { + range: 45..50, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 45..50, + value: "bar", + unicode: false, + }, + ), + }, + }, + ), + Dict( + ExprDict { + range: 54..72, + keys: [ + Some( + StringLiteral( + ExprStringLiteral { + range: 55..63, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 55..63, + value: "nested", + unicode: false, + }, + ), + }, + }, + ), + ), + ], + values: [ + StringLiteral( + ExprStringLiteral { + range: 65..71, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 65..71, + value: "dict", + unicode: false, + }, + ), + }, + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 74..98, + value: Dict( + ExprDict { + range: 74..98, + keys: [ + Some( + BinOp( + ExprBinOp { + range: 75..80, + left: Name( + ExprName { + range: 75..76, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 79..80, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + None, + ], + values: [ + BinOp( + ExprBinOp { + range: 82..87, + left: Name( + ExprName { + range: 82..83, + id: "y", + ctx: Load, + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 86..87, + value: Int( + 2, + ), + }, + ), + }, + ), + Call( + ExprCall { + range: 91..97, + func: Name( + ExprName { + range: 91..95, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 95..97, + args: [], + keywords: [], + }, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 99..151, + value: Dict( + ExprDict { + range: 99..151, + keys: [ + Some( + Name( + ExprName { + range: 100..101, + id: "l", + ctx: Load, + }, + ), + ), + Some( + Name( + ExprName { + range: 114..115, + id: "t", + ctx: Load, + }, + ), + ), + Some( + Name( + ExprName { + range: 126..127, + id: "d", + ctx: Load, + }, + ), + ), + Some( + Name( + ExprName { + range: 141..142, + id: "s", + ctx: Load, + }, + ), + ), + ], + values: [ + List( + ExprList { + range: 103..112, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 104..105, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 107..108, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 110..111, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + Tuple( + ExprTuple { + range: 117..124, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 118..119, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 120..121, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 122..123, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + Dict( + ExprDict { + range: 129..139, + keys: [ + Some( + NumberLiteral( + ExprNumberLiteral { + range: 130..131, + value: Int( + 1, + ), + }, + ), + ), + Some( + NumberLiteral( + ExprNumberLiteral { + range: 135..136, + value: Int( + 3, + ), + }, + ), + ), + ], + values: [ + NumberLiteral( + ExprNumberLiteral { + range: 132..133, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 137..138, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + Set( + ExprSet { + range: 144..150, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 145..146, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 148..149, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 152..157, + value: Dict( + ExprDict { + range: 152..157, + keys: [ + None, + ], + values: [ + Name( + ExprName { + range: 155..156, + id: "d", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 158..186, + value: Dict( + ExprDict { + range: 158..186, + keys: [ + Some( + NumberLiteral( + ExprNumberLiteral { + range: 159..160, + value: Int( + 1, + ), + }, + ), + ), + None, + ], + values: [ + NumberLiteral( + ExprNumberLiteral { + range: 162..163, + value: Int( + 2, + ), + }, + ), + Dict( + ExprDict { + range: 167..185, + keys: [ + Some( + StringLiteral( + ExprStringLiteral { + range: 168..176, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 168..176, + value: "nested", + unicode: false, + }, + ), + }, + }, + ), + ), + ], + values: [ + StringLiteral( + ExprStringLiteral { + range: 178..184, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 178..184, + value: "dict", + unicode: false, + }, + ), + }, + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 187..193, + value: Dict( + ExprDict { + range: 187..193, + keys: [ + Some( + Name( + ExprName { + range: 188..189, + id: "a", + ctx: Load, + }, + ), + ), + ], + values: [ + Name( + ExprName { + range: 191..192, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 194..272, + value: DictComp( + ExprDictComp { + range: 194..272, + key: Name( + ExprName { + range: 195..196, + id: "i", + ctx: Load, + }, + ), + value: Call( + ExprCall { + range: 198..227, + func: Name( + ExprName { + range: 198..203, + id: "tuple", + ctx: Load, + }, + ), + arguments: Arguments { + range: 203..227, + args: [ + GeneratorExp( + ExprGeneratorExp { + range: 204..226, + elt: Name( + ExprName { + range: 204..205, + id: "j", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 206..226, + target: Name( + ExprName { + range: 210..211, + id: "j", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 215..216, + id: "t", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 220..226, + left: Name( + ExprName { + range: 220..221, + id: "i", + ctx: Load, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Name( + ExprName { + range: 225..226, + id: "j", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + ], + keywords: [], + }, + }, + ), + generators: [ + Comprehension { + range: 239..249, + target: Name( + ExprName { + range: 243..244, + id: "t", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 248..249, + id: "L", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + Comprehension { + range: 261..271, + target: Name( + ExprName { + range: 265..266, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 270..271, + id: "t", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 273..313, + value: Dict( + ExprDict { + range: 273..313, + keys: [ + Some( + StringLiteral( + ExprStringLiteral { + range: 279..282, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 279..282, + value: "A", + unicode: false, + }, + ), + }, + }, + ), + ), + Some( + StringLiteral( + ExprStringLiteral { + range: 304..307, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 304..307, + value: "B", + unicode: false, + }, + ), + }, + }, + ), + ), + ], + values: [ + Lambda( + ExprLambda { + range: 284..298, + parameters: Some( + Parameters { + range: 291..292, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 291..292, + parameter: Parameter { + range: 291..292, + name: Identifier { + id: "p", + range: 291..292, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NoneLiteral( + ExprNoneLiteral { + range: 294..298, + }, + ), + }, + ), + Name( + ExprName { + range: 309..310, + id: "C", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 314..324, + value: Dict( + ExprDict { + range: 314..324, + keys: [ + None, + None, + ], + values: [ + Name( + ExprName { + range: 317..318, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 322..323, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_empty_fstring.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_empty_fstring.snap new file mode 100644 index 0000000000000..a978ccef81e7f --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_empty_fstring.snap @@ -0,0 +1,114 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(r#\"\nf\"\"\nF\"\"\nf''\nf\"\"\"\"\"\"\nf''''''\n\"#)" +--- +Program { + ast: Module( + ModModule { + range: 0..29, + body: [ + Expr( + StmtExpr { + range: 1..4, + value: FString( + ExprFString { + range: 1..4, + value: FStringValue { + inner: Single( + FString( + FString { + range: 1..4, + elements: [], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..8, + value: FString( + ExprFString { + range: 5..8, + value: FStringValue { + inner: Single( + FString( + FString { + range: 5..8, + elements: [], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 9..12, + value: FString( + ExprFString { + range: 9..12, + value: FStringValue { + inner: Single( + FString( + FString { + range: 9..12, + elements: [], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..20, + value: FString( + ExprFString { + range: 13..20, + value: FStringValue { + inner: Single( + FString( + FString { + range: 13..20, + elements: [], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..28, + value: FString( + ExprFString { + range: 21..28, + value: FStringValue { + inner: Single( + FString( + FString { + range: 21..28, + elements: [], + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_for_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_for_stmt.snap new file mode 100644 index 0000000000000..bfbcbf874461a --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_for_stmt.snap @@ -0,0 +1,519 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nfor i in x:\n ...\nfor x.attr in f():\n pass\nfor 1 + 2 in x.attr:\n ...\nfor i in x <= y:\n pass\nfor i in a and b:\n pass\nfor a,b,c, in iter:\n ...\nfor (a, b) in iter:\n ...\nfor i in *x.attr:\n ...\nfor -i in [1, 2]:\n ...\nfor *l in a, b, c,:\n ...\nelse:\n pass\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..281, + body: [ + For( + StmtFor { + range: 1..20, + is_async: false, + target: Name( + ExprName { + range: 5..6, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 10..11, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 17..20, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 17..20, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 21..48, + is_async: false, + target: Attribute( + ExprAttribute { + range: 25..31, + value: Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 27..31, + }, + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 35..38, + func: Name( + ExprName { + range: 35..36, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 36..38, + args: [], + keywords: [], + }, + }, + ), + body: [ + Pass( + StmtPass { + range: 44..48, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 49..77, + is_async: false, + target: BinOp( + ExprBinOp { + range: 53..58, + left: NumberLiteral( + ExprNumberLiteral { + range: 53..54, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 57..58, + value: Int( + 2, + ), + }, + ), + }, + ), + iter: Attribute( + ExprAttribute { + range: 62..68, + value: Name( + ExprName { + range: 62..63, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 64..68, + }, + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 74..77, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 74..77, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 78..103, + is_async: false, + target: Name( + ExprName { + range: 82..83, + id: "i", + ctx: Store, + }, + ), + iter: Compare( + ExprCompare { + range: 87..93, + left: Name( + ExprName { + range: 87..88, + id: "x", + ctx: Load, + }, + ), + ops: [ + LtE, + ], + comparators: [ + Name( + ExprName { + range: 92..93, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Pass( + StmtPass { + range: 99..103, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 104..130, + is_async: false, + target: Name( + ExprName { + range: 108..109, + id: "i", + ctx: Store, + }, + ), + iter: BoolOp( + ExprBoolOp { + range: 113..120, + op: And, + values: [ + Name( + ExprName { + range: 113..114, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 119..120, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Pass( + StmtPass { + range: 126..130, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 131..158, + is_async: false, + target: Tuple( + ExprTuple { + range: 135..141, + elts: [ + Name( + ExprName { + range: 135..136, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 137..138, + id: "b", + ctx: Store, + }, + ), + Name( + ExprName { + range: 139..140, + id: "c", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 145..149, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 155..158, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 155..158, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 159..186, + is_async: false, + target: Tuple( + ExprTuple { + range: 163..169, + elts: [ + Name( + ExprName { + range: 164..165, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 167..168, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + iter: Name( + ExprName { + range: 173..177, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 183..186, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 183..186, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 187..212, + is_async: false, + target: Name( + ExprName { + range: 191..192, + id: "i", + ctx: Store, + }, + ), + iter: Starred( + ExprStarred { + range: 196..203, + value: Attribute( + ExprAttribute { + range: 197..203, + value: Name( + ExprName { + range: 197..198, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 199..203, + }, + ctx: Load, + }, + ), + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 209..212, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 209..212, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 213..238, + is_async: false, + target: UnaryOp( + ExprUnaryOp { + range: 217..219, + op: USub, + operand: Name( + ExprName { + range: 218..219, + id: "i", + ctx: Store, + }, + ), + }, + ), + iter: List( + ExprList { + range: 223..229, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 224..225, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 227..228, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 235..238, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 235..238, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 239..280, + is_async: false, + target: Starred( + ExprStarred { + range: 243..245, + value: Name( + ExprName { + range: 244..245, + id: "l", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 249..257, + elts: [ + Name( + ExprName { + range: 249..250, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 252..253, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 255..256, + id: "c", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + body: [ + Expr( + StmtExpr { + range: 262..265, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 262..265, + }, + ), + }, + ), + ], + orelse: [ + Pass( + StmtPass { + range: 276..280, + }, + ), + ], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_fstring.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_fstring.snap new file mode 100644 index 0000000000000..31b62330658e7 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_fstring.snap @@ -0,0 +1,1032 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(r#\"\nf\"normal {foo} {{another}} {bar} {{{three}}}\"\nf\"normal {foo!a} {bar!s} {baz!r} {foobar}\"\nf\"normal {x:y + 2}\"\nf\"{x:{{1}.pop()}}\"\nf\"{(lambda x:{x})}\"\nf\"{x =}\"\nf\"{ x = }\"\nf\"{x=!a}\"\nf\"{x:.3f!r =}\"\nf\"{x = !r :.3f}\"\nf\"{x:.3f=!r}\"\n\"hello\" f\"{x}\"\nf\"{x}\" f\"{y}\"\nf\"{x}\" \"world\"\nf\"Invalid args in command: {command, *args}\"\n\"foo\" f\"{x}\" \"bar\"\n(\n f\"a\"\n F\"b\"\n \"c\"\n rf\"d\"\n fr\"e\"\n)\n\"#)" +--- +Program { + ast: Module( + ModModule { + range: 0..386, + body: [ + Expr( + StmtExpr { + range: 1..46, + value: FString( + ExprFString { + range: 1..46, + value: FStringValue { + inner: Single( + FString( + FString { + range: 1..46, + elements: [ + Literal( + FStringLiteralElement { + range: 3..10, + value: "normal ", + }, + ), + Expression( + FStringExpressionElement { + range: 10..15, + expression: Name( + ExprName { + range: 11..14, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 15..28, + value: " {another} ", + }, + ), + Expression( + FStringExpressionElement { + range: 28..33, + expression: Name( + ExprName { + range: 29..32, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 33..36, + value: " {", + }, + ), + Expression( + FStringExpressionElement { + range: 36..43, + expression: Name( + ExprName { + range: 37..42, + id: "three", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 43..45, + value: "}", + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 47..89, + value: FString( + ExprFString { + range: 47..89, + value: FStringValue { + inner: Single( + FString( + FString { + range: 47..89, + elements: [ + Literal( + FStringLiteralElement { + range: 49..56, + value: "normal ", + }, + ), + Expression( + FStringExpressionElement { + range: 56..63, + expression: Name( + ExprName { + range: 57..60, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: Ascii, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 63..64, + value: " ", + }, + ), + Expression( + FStringExpressionElement { + range: 64..71, + expression: Name( + ExprName { + range: 65..68, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: Str, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 71..72, + value: " ", + }, + ), + Expression( + FStringExpressionElement { + range: 72..79, + expression: Name( + ExprName { + range: 73..76, + id: "baz", + ctx: Load, + }, + ), + debug_text: None, + conversion: Repr, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 79..80, + value: " ", + }, + ), + Expression( + FStringExpressionElement { + range: 80..88, + expression: Name( + ExprName { + range: 81..87, + id: "foobar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 90..109, + value: FString( + ExprFString { + range: 90..109, + value: FStringValue { + inner: Single( + FString( + FString { + range: 90..109, + elements: [ + Literal( + FStringLiteralElement { + range: 92..99, + value: "normal ", + }, + ), + Expression( + FStringExpressionElement { + range: 99..108, + expression: Name( + ExprName { + range: 100..101, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 102..107, + elements: [ + Literal( + FStringLiteralElement { + range: 102..107, + value: "y + 2", + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 110..128, + value: FString( + ExprFString { + range: 110..128, + value: FStringValue { + inner: Single( + FString( + FString { + range: 110..128, + elements: [ + Expression( + FStringExpressionElement { + range: 112..127, + expression: Name( + ExprName { + range: 113..114, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 115..126, + elements: [ + Expression( + FStringExpressionElement { + range: 115..126, + expression: Call( + ExprCall { + range: 116..125, + func: Attribute( + ExprAttribute { + range: 116..123, + value: Set( + ExprSet { + range: 116..119, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 117..118, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + attr: Identifier { + id: "pop", + range: 120..123, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 123..125, + args: [], + keywords: [], + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 129..148, + value: FString( + ExprFString { + range: 129..148, + value: FStringValue { + inner: Single( + FString( + FString { + range: 129..148, + elements: [ + Expression( + FStringExpressionElement { + range: 131..147, + expression: Lambda( + ExprLambda { + range: 133..145, + parameters: Some( + Parameters { + range: 140..141, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 140..141, + parameter: Parameter { + range: 140..141, + name: Identifier { + id: "x", + range: 140..141, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Set( + ExprSet { + range: 142..145, + elts: [ + Name( + ExprName { + range: 143..144, + id: "x", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 149..157, + value: FString( + ExprFString { + range: 149..157, + value: FStringValue { + inner: Single( + FString( + FString { + range: 149..157, + elements: [ + Expression( + FStringExpressionElement { + range: 151..156, + expression: Name( + ExprName { + range: 152..153, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: " =", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 158..171, + value: FString( + ExprFString { + range: 158..171, + value: FStringValue { + inner: Single( + FString( + FString { + range: 158..171, + elements: [ + Expression( + FStringExpressionElement { + range: 160..170, + expression: Name( + ExprName { + range: 165..166, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 172..181, + value: FString( + ExprFString { + range: 172..181, + value: FStringValue { + inner: Single( + FString( + FString { + range: 172..181, + elements: [ + Expression( + FStringExpressionElement { + range: 174..180, + expression: Name( + ExprName { + range: 175..176, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: Ascii, + format_spec: None, + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 182..196, + value: FString( + ExprFString { + range: 182..196, + value: FStringValue { + inner: Single( + FString( + FString { + range: 182..196, + elements: [ + Expression( + FStringExpressionElement { + range: 184..195, + expression: Name( + ExprName { + range: 185..186, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 187..194, + elements: [ + Literal( + FStringLiteralElement { + range: 187..194, + value: ".3f!r =", + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 197..213, + value: FString( + ExprFString { + range: 197..213, + value: FStringValue { + inner: Single( + FString( + FString { + range: 197..213, + elements: [ + Expression( + FStringExpressionElement { + range: 199..212, + expression: Name( + ExprName { + range: 200..201, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: " = ", + }, + ), + conversion: Repr, + format_spec: Some( + FStringFormatSpec { + range: 208..211, + elements: [ + Literal( + FStringLiteralElement { + range: 208..211, + value: ".3f", + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 214..227, + value: FString( + ExprFString { + range: 214..227, + value: FStringValue { + inner: Single( + FString( + FString { + range: 214..227, + elements: [ + Expression( + FStringExpressionElement { + range: 216..226, + expression: Name( + ExprName { + range: 217..218, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 219..225, + elements: [ + Literal( + FStringLiteralElement { + range: 219..225, + value: ".3f=!r", + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 228..242, + value: FString( + ExprFString { + range: 228..242, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 228..235, + value: "hello", + unicode: false, + }, + ), + FString( + FString { + range: 236..242, + elements: [ + Expression( + FStringExpressionElement { + range: 238..241, + expression: Name( + ExprName { + range: 239..240, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 243..256, + value: FString( + ExprFString { + range: 243..256, + value: FStringValue { + inner: Concatenated( + [ + FString( + FString { + range: 243..249, + elements: [ + Expression( + FStringExpressionElement { + range: 245..248, + expression: Name( + ExprName { + range: 246..247, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + FString( + FString { + range: 250..256, + elements: [ + Expression( + FStringExpressionElement { + range: 252..255, + expression: Name( + ExprName { + range: 253..254, + id: "y", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 257..271, + value: FString( + ExprFString { + range: 257..271, + value: FStringValue { + inner: Concatenated( + [ + FString( + FString { + range: 257..263, + elements: [ + Expression( + FStringExpressionElement { + range: 259..262, + expression: Name( + ExprName { + range: 260..261, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + Literal( + StringLiteral { + range: 264..271, + value: "world", + unicode: false, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 272..316, + value: FString( + ExprFString { + range: 272..316, + value: FStringValue { + inner: Single( + FString( + FString { + range: 272..316, + elements: [ + Literal( + FStringLiteralElement { + range: 274..299, + value: "Invalid args in command: ", + }, + ), + Expression( + FStringExpressionElement { + range: 299..315, + expression: Tuple( + ExprTuple { + range: 300..314, + elts: [ + Name( + ExprName { + range: 300..307, + id: "command", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 309..314, + value: Name( + ExprName { + range: 310..314, + id: "args", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 317..335, + value: FString( + ExprFString { + range: 317..335, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 317..322, + value: "foo", + unicode: false, + }, + ), + FString( + FString { + range: 323..329, + elements: [ + Expression( + FStringExpressionElement { + range: 325..328, + expression: Name( + ExprName { + range: 326..327, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + Literal( + StringLiteral { + range: 330..335, + value: "bar", + unicode: false, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 336..385, + value: FString( + ExprFString { + range: 342..383, + value: FStringValue { + inner: Concatenated( + [ + FString( + FString { + range: 342..346, + elements: [ + Literal( + FStringLiteralElement { + range: 344..345, + value: "a", + }, + ), + ], + }, + ), + FString( + FString { + range: 351..355, + elements: [ + Literal( + FStringLiteralElement { + range: 353..354, + value: "b", + }, + ), + ], + }, + ), + Literal( + StringLiteral { + range: 360..363, + value: "c", + unicode: false, + }, + ), + FString( + FString { + range: 368..373, + elements: [ + Literal( + FStringLiteralElement { + range: 371..372, + value: "d", + }, + ), + ], + }, + ), + FString( + FString { + range: 378..383, + elements: [ + Literal( + FStringLiteralElement { + range: 381..382, + value: "e", + }, + ), + ], + }, + ), + ], + ), + }, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_func_def_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_func_def_stmt.snap new file mode 100644 index 0000000000000..c81c3ad85b706 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_func_def_stmt.snap @@ -0,0 +1,1082 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\ndef f():\n ...\ndef x() -> int:\n f()\n pass\n ...\ndef mul(x, y) -> 'str':\n x * y\ndef f1(*a): ...\ndef f2(*a, z, x=0): ...\ndef f3(**kwargs): f()\ndef f4(*args, **kwargs): f() + 1\ndef f5(*args, a, b=1, **kwargs): f() + 1\ndef f6(a, /): ...\ndef f7(a, /, b): ...\ndef f8(a=1, /,): ...\ndef f9(a, b, /, *, c): ...\ndef f10(kw=1, *, a): ...\ndef f11(x: int, y: 'str', z: 1 + 2): pass\ndef f12(self, a=1, b=2, c=3): ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..417, + body: [ + FunctionDef( + StmtFunctionDef { + range: 1..17, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f", + range: 5..6, + }, + type_params: None, + parameters: Parameters { + range: 6..8, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 14..17, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 14..17, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 18..58, + is_async: false, + decorator_list: [], + name: Identifier { + id: "x", + range: 22..23, + }, + type_params: None, + parameters: Parameters { + range: 23..25, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 29..32, + id: "int", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 38..41, + value: Call( + ExprCall { + range: 38..41, + func: Name( + ExprName { + range: 38..39, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 39..41, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Pass( + StmtPass { + range: 46..50, + }, + ), + Expr( + StmtExpr { + range: 55..58, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 55..58, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 59..92, + is_async: false, + decorator_list: [], + name: Identifier { + id: "mul", + range: 63..66, + }, + type_params: None, + parameters: Parameters { + range: 66..72, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 67..68, + parameter: Parameter { + range: 67..68, + name: Identifier { + id: "x", + range: 67..68, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 70..71, + parameter: Parameter { + range: 70..71, + name: Identifier { + id: "y", + range: 70..71, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + StringLiteral( + ExprStringLiteral { + range: 76..81, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 76..81, + value: "str", + unicode: false, + }, + ), + }, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 87..92, + value: BinOp( + ExprBinOp { + range: 87..92, + left: Name( + ExprName { + range: 87..88, + id: "x", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 91..92, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 93..108, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f1", + range: 97..99, + }, + type_params: None, + parameters: Parameters { + range: 99..103, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 101..102, + name: Identifier { + id: "a", + range: 101..102, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 105..108, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 105..108, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 109..132, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f2", + range: 113..115, + }, + type_params: None, + parameters: Parameters { + range: 115..127, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 117..118, + name: Identifier { + id: "a", + range: 117..118, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 120..121, + parameter: Parameter { + range: 120..121, + name: Identifier { + id: "z", + range: 120..121, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 123..126, + parameter: Parameter { + range: 123..124, + name: Identifier { + id: "x", + range: 123..124, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 125..126, + value: Int( + 0, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 129..132, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 129..132, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 133..154, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f3", + range: 137..139, + }, + type_params: None, + parameters: Parameters { + range: 139..149, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 142..148, + name: Identifier { + id: "kwargs", + range: 142..148, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 151..154, + value: Call( + ExprCall { + range: 151..154, + func: Name( + ExprName { + range: 151..152, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 152..154, + args: [], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 155..187, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f4", + range: 159..161, + }, + type_params: None, + parameters: Parameters { + range: 161..178, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 163..167, + name: Identifier { + id: "args", + range: 163..167, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 171..177, + name: Identifier { + id: "kwargs", + range: 171..177, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 180..187, + value: BinOp( + ExprBinOp { + range: 180..187, + left: Call( + ExprCall { + range: 180..183, + func: Name( + ExprName { + range: 180..181, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 181..183, + args: [], + keywords: [], + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 186..187, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 188..228, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f5", + range: 192..194, + }, + type_params: None, + parameters: Parameters { + range: 194..219, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 196..200, + name: Identifier { + id: "args", + range: 196..200, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 202..203, + parameter: Parameter { + range: 202..203, + name: Identifier { + id: "a", + range: 202..203, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 205..208, + parameter: Parameter { + range: 205..206, + name: Identifier { + id: "b", + range: 205..206, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 207..208, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 212..218, + name: Identifier { + id: "kwargs", + range: 212..218, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 221..228, + value: BinOp( + ExprBinOp { + range: 221..228, + left: Call( + ExprCall { + range: 221..224, + func: Name( + ExprName { + range: 221..222, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 222..224, + args: [], + keywords: [], + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 227..228, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 229..246, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f6", + range: 233..235, + }, + type_params: None, + parameters: Parameters { + range: 235..241, + posonlyargs: [ + ParameterWithDefault { + range: 236..237, + parameter: Parameter { + range: 236..237, + name: Identifier { + id: "a", + range: 236..237, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 243..246, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 243..246, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 247..267, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f7", + range: 251..253, + }, + type_params: None, + parameters: Parameters { + range: 253..262, + posonlyargs: [ + ParameterWithDefault { + range: 254..255, + parameter: Parameter { + range: 254..255, + name: Identifier { + id: "a", + range: 254..255, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 260..261, + parameter: Parameter { + range: 260..261, + name: Identifier { + id: "b", + range: 260..261, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 264..267, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 264..267, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 268..288, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f8", + range: 272..274, + }, + type_params: None, + parameters: Parameters { + range: 274..283, + posonlyargs: [ + ParameterWithDefault { + range: 275..278, + parameter: Parameter { + range: 275..276, + name: Identifier { + id: "a", + range: 275..276, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 277..278, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 285..288, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 285..288, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 289..315, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f9", + range: 293..295, + }, + type_params: None, + parameters: Parameters { + range: 295..310, + posonlyargs: [ + ParameterWithDefault { + range: 296..297, + parameter: Parameter { + range: 296..297, + name: Identifier { + id: "a", + range: 296..297, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 299..300, + parameter: Parameter { + range: 299..300, + name: Identifier { + id: "b", + range: 299..300, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 308..309, + parameter: Parameter { + range: 308..309, + name: Identifier { + id: "c", + range: 308..309, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 312..315, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 312..315, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 316..340, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f10", + range: 320..323, + }, + type_params: None, + parameters: Parameters { + range: 323..335, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 324..328, + parameter: Parameter { + range: 324..326, + name: Identifier { + id: "kw", + range: 324..326, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 327..328, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 333..334, + parameter: Parameter { + range: 333..334, + name: Identifier { + id: "a", + range: 333..334, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 337..340, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 337..340, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 341..382, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f11", + range: 345..348, + }, + type_params: None, + parameters: Parameters { + range: 348..376, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 349..355, + parameter: Parameter { + range: 349..355, + name: Identifier { + id: "x", + range: 349..350, + }, + annotation: Some( + Name( + ExprName { + range: 352..355, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 357..365, + parameter: Parameter { + range: 357..365, + name: Identifier { + id: "y", + range: 357..358, + }, + annotation: Some( + StringLiteral( + ExprStringLiteral { + range: 360..365, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 360..365, + value: "str", + unicode: false, + }, + ), + }, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 367..375, + parameter: Parameter { + range: 367..375, + name: Identifier { + id: "z", + range: 367..368, + }, + annotation: Some( + BinOp( + ExprBinOp { + range: 370..375, + left: NumberLiteral( + ExprNumberLiteral { + range: 370..371, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 374..375, + value: Int( + 2, + ), + }, + ), + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 378..382, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 383..416, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f12", + range: 387..390, + }, + type_params: None, + parameters: Parameters { + range: 390..411, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 391..395, + parameter: Parameter { + range: 391..395, + name: Identifier { + id: "self", + range: 391..395, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 397..400, + parameter: Parameter { + range: 397..398, + name: Identifier { + id: "a", + range: 397..398, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 399..400, + value: Int( + 1, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 402..405, + parameter: Parameter { + range: 402..403, + name: Identifier { + id: "b", + range: 402..403, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 404..405, + value: Int( + 2, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 407..410, + parameter: Parameter { + range: 407..408, + name: Identifier { + id: "c", + range: 407..408, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 409..410, + value: Int( + 3, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 413..416, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 413..416, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_generator_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_generator_expr.snap new file mode 100644 index 0000000000000..b07ea320b4b9e --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_generator_expr.snap @@ -0,0 +1,590 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n(i for i in list)\n(a async for i in iter)\n(b for c in d if x in w if y and yy if z)\n(a for b in c if d and e for f in j if k > h)\n(a for b in c if d and e async for f in j if k > h)\nf(x for i in l)\nf(a, x for i in l)\nf(a, x for i, j in l)\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..240, + body: [ + Expr( + StmtExpr { + range: 1..18, + value: GeneratorExp( + ExprGeneratorExp { + range: 1..18, + elt: Name( + ExprName { + range: 2..3, + id: "i", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 4..17, + target: Name( + ExprName { + range: 8..9, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 13..17, + id: "list", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..42, + value: GeneratorExp( + ExprGeneratorExp { + range: 19..42, + elt: Name( + ExprName { + range: 20..21, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 22..41, + target: Name( + ExprName { + range: 32..33, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 37..41, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: true, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 43..84, + value: GeneratorExp( + ExprGeneratorExp { + range: 43..84, + elt: Name( + ExprName { + range: 44..45, + id: "b", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 46..83, + target: Name( + ExprName { + range: 50..51, + id: "c", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 55..56, + id: "d", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 60..66, + left: Name( + ExprName { + range: 60..61, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 65..66, + id: "w", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 70..78, + op: And, + values: [ + Name( + ExprName { + range: 70..71, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 76..78, + id: "yy", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 82..83, + id: "z", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 85..130, + value: GeneratorExp( + ExprGeneratorExp { + range: 85..130, + elt: Name( + ExprName { + range: 86..87, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 88..109, + target: Name( + ExprName { + range: 92..93, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 97..98, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 102..109, + op: And, + values: [ + Name( + ExprName { + range: 102..103, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 108..109, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 110..129, + target: Name( + ExprName { + range: 114..115, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 119..120, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 124..129, + left: Name( + ExprName { + range: 124..125, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 128..129, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 131..182, + value: GeneratorExp( + ExprGeneratorExp { + range: 131..182, + elt: Name( + ExprName { + range: 132..133, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 134..155, + target: Name( + ExprName { + range: 138..139, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 143..144, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 148..155, + op: And, + values: [ + Name( + ExprName { + range: 148..149, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 154..155, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 156..181, + target: Name( + ExprName { + range: 166..167, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 171..172, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 176..181, + left: Name( + ExprName { + range: 176..177, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 180..181, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: true, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 183..198, + value: Call( + ExprCall { + range: 183..198, + func: Name( + ExprName { + range: 183..184, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 184..198, + args: [ + GeneratorExp( + ExprGeneratorExp { + range: 185..197, + elt: Name( + ExprName { + range: 185..186, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 187..197, + target: Name( + ExprName { + range: 191..192, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 196..197, + id: "l", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 199..217, + value: Call( + ExprCall { + range: 199..217, + func: Name( + ExprName { + range: 199..200, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 200..217, + args: [ + Name( + ExprName { + range: 201..202, + id: "a", + ctx: Load, + }, + ), + GeneratorExp( + ExprGeneratorExp { + range: 204..216, + elt: Name( + ExprName { + range: 204..205, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 206..216, + target: Name( + ExprName { + range: 210..211, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 215..216, + id: "l", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 218..239, + value: Call( + ExprCall { + range: 218..239, + func: Name( + ExprName { + range: 218..219, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 219..239, + args: [ + Name( + ExprName { + range: 220..221, + id: "a", + ctx: Load, + }, + ), + GeneratorExp( + ExprGeneratorExp { + range: 223..238, + elt: Name( + ExprName { + range: 223..224, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 225..238, + target: Tuple( + ExprTuple { + range: 229..233, + elts: [ + Name( + ExprName { + range: 229..230, + id: "i", + ctx: Store, + }, + ), + Name( + ExprName { + range: 232..233, + id: "j", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 237..238, + id: "l", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_if_else_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_if_else_expr.snap new file mode 100644 index 0000000000000..04f08d2b10487 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_if_else_expr.snap @@ -0,0 +1,390 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\na if True else b\nf() if x else None\na if b else c if d else e\n1 + x if 1 < 0 else -1\na and b if x else False\nx <= y if y else x\nTrue if a and b else False\n1, 1 if a else c\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..173, + body: [ + Expr( + StmtExpr { + range: 1..17, + value: IfExp( + ExprIfExp { + range: 1..17, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 6..10, + value: true, + }, + ), + body: Name( + ExprName { + range: 1..2, + id: "a", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 16..17, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..36, + value: IfExp( + ExprIfExp { + range: 18..36, + test: Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + body: Call( + ExprCall { + range: 18..21, + func: Name( + ExprName { + range: 18..19, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 19..21, + args: [], + keywords: [], + }, + }, + ), + orelse: NoneLiteral( + ExprNoneLiteral { + range: 32..36, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 37..62, + value: IfExp( + ExprIfExp { + range: 37..62, + test: Name( + ExprName { + range: 42..43, + id: "b", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 37..38, + id: "a", + ctx: Load, + }, + ), + orelse: IfExp( + ExprIfExp { + range: 49..62, + test: Name( + ExprName { + range: 54..55, + id: "d", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 49..50, + id: "c", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 61..62, + id: "e", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 63..85, + value: IfExp( + ExprIfExp { + range: 63..85, + test: Compare( + ExprCompare { + range: 72..77, + left: NumberLiteral( + ExprNumberLiteral { + range: 72..73, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 76..77, + value: Int( + 0, + ), + }, + ), + ], + }, + ), + body: BinOp( + ExprBinOp { + range: 63..68, + left: NumberLiteral( + ExprNumberLiteral { + range: 63..64, + value: Int( + 1, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 67..68, + id: "x", + ctx: Load, + }, + ), + }, + ), + orelse: UnaryOp( + ExprUnaryOp { + range: 83..85, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 84..85, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 86..109, + value: IfExp( + ExprIfExp { + range: 86..109, + test: Name( + ExprName { + range: 97..98, + id: "x", + ctx: Load, + }, + ), + body: BoolOp( + ExprBoolOp { + range: 86..93, + op: And, + values: [ + Name( + ExprName { + range: 86..87, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 92..93, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + orelse: BooleanLiteral( + ExprBooleanLiteral { + range: 104..109, + value: false, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 110..128, + value: IfExp( + ExprIfExp { + range: 110..128, + test: Name( + ExprName { + range: 120..121, + id: "y", + ctx: Load, + }, + ), + body: Compare( + ExprCompare { + range: 110..116, + left: Name( + ExprName { + range: 110..111, + id: "x", + ctx: Load, + }, + ), + ops: [ + LtE, + ], + comparators: [ + Name( + ExprName { + range: 115..116, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + orelse: Name( + ExprName { + range: 127..128, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 129..155, + value: IfExp( + ExprIfExp { + range: 129..155, + test: BoolOp( + ExprBoolOp { + range: 137..144, + op: And, + values: [ + Name( + ExprName { + range: 137..138, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 143..144, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + body: BooleanLiteral( + ExprBooleanLiteral { + range: 129..133, + value: true, + }, + ), + orelse: BooleanLiteral( + ExprBooleanLiteral { + range: 150..155, + value: false, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 156..172, + value: Tuple( + ExprTuple { + range: 156..172, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 156..157, + value: Int( + 1, + ), + }, + ), + IfExp( + ExprIfExp { + range: 159..172, + test: Name( + ExprName { + range: 164..165, + id: "a", + ctx: Load, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 159..160, + value: Int( + 1, + ), + }, + ), + orelse: Name( + ExprName { + range: 171..172, + id: "c", + ctx: Load, + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_if_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_if_stmt.snap new file mode 100644 index 0000000000000..70b9948f63f26 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_if_stmt.snap @@ -0,0 +1,328 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nif True:\n 1\n ...\nif x < 1:\n ...\nelse:\n pass\n\nif a:\n pass\nelif b:\n ...\n\nif a and b:\n ...\nelif True:\n ...\nelif c:\n ...\nelif d:\n ...\nelse:\n f()\nif a:=b: ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..188, + body: [ + If( + StmtIf { + range: 1..23, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 4..8, + value: true, + }, + ), + body: [ + Expr( + StmtExpr { + range: 14..15, + value: NumberLiteral( + ExprNumberLiteral { + range: 14..15, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 20..23, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 20..23, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 24..56, + test: Compare( + ExprCompare { + range: 27..32, + left: Name( + ExprName { + range: 27..28, + id: "x", + ctx: Load, + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 31..32, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 38..41, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 38..41, + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 42..56, + test: None, + body: [ + Pass( + StmtPass { + range: 52..56, + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 58..88, + test: Name( + ExprName { + range: 61..62, + id: "a", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 68..72, + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 73..88, + test: Some( + Name( + ExprName { + range: 78..79, + id: "b", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 85..88, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 85..88, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 90..174, + test: BoolOp( + ExprBoolOp { + range: 93..100, + op: And, + values: [ + Name( + ExprName { + range: 93..94, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 99..100, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 106..109, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 106..109, + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 110..128, + test: Some( + BooleanLiteral( + ExprBooleanLiteral { + range: 115..119, + value: true, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 125..128, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 125..128, + }, + ), + }, + ), + ], + }, + ElifElseClause { + range: 129..144, + test: Some( + Name( + ExprName { + range: 134..135, + id: "c", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 141..144, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 141..144, + }, + ), + }, + ), + ], + }, + ElifElseClause { + range: 145..160, + test: Some( + Name( + ExprName { + range: 150..151, + id: "d", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 157..160, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 157..160, + }, + ), + }, + ), + ], + }, + ElifElseClause { + range: 161..174, + test: None, + body: [ + Expr( + StmtExpr { + range: 171..174, + value: Call( + ExprCall { + range: 171..174, + func: Name( + ExprName { + range: 171..172, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 172..174, + args: [], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 175..187, + test: NamedExpr( + ExprNamedExpr { + range: 178..182, + target: Name( + ExprName { + range: 178..179, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 181..182, + id: "b", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 184..187, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 184..187, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_lambda_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_lambda_expr.snap new file mode 100644 index 0000000000000..f1f39ecc8d77e --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_lambda_expr.snap @@ -0,0 +1,778 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nlambda: a\nlambda x: 1\nlambda x, y: ...\nlambda y, z=1: z * y\nlambda *a: a\nlambda *a, z, x=0: ...\nlambda **kwargs: f()\nlambda *args, **kwargs: f() + 1\nlambda *args, a, b=1, **kwargs: f() + 1\nlambda a, /: ...\nlambda a, /, b: ...\nlambda a=1, /,: ...\nlambda a, b, /, *, c: ...\nlambda kw=1, *, a: ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..296, + body: [ + Expr( + StmtExpr { + range: 1..10, + value: Lambda( + ExprLambda { + range: 1..10, + parameters: None, + body: Name( + ExprName { + range: 9..10, + id: "a", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..22, + value: Lambda( + ExprLambda { + range: 11..22, + parameters: Some( + Parameters { + range: 18..19, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 18..19, + parameter: Parameter { + range: 18..19, + name: Identifier { + id: "x", + range: 18..19, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 21..22, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 23..39, + value: Lambda( + ExprLambda { + range: 23..39, + parameters: Some( + Parameters { + range: 30..34, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 30..31, + parameter: Parameter { + range: 30..31, + name: Identifier { + id: "x", + range: 30..31, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 33..34, + parameter: Parameter { + range: 33..34, + name: Identifier { + id: "y", + range: 33..34, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 36..39, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 40..60, + value: Lambda( + ExprLambda { + range: 40..60, + parameters: Some( + Parameters { + range: 47..53, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 47..48, + parameter: Parameter { + range: 47..48, + name: Identifier { + id: "y", + range: 47..48, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 50..53, + parameter: Parameter { + range: 50..51, + name: Identifier { + id: "z", + range: 50..51, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 52..53, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: BinOp( + ExprBinOp { + range: 55..60, + left: Name( + ExprName { + range: 55..56, + id: "z", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 59..60, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 61..73, + value: Lambda( + ExprLambda { + range: 61..73, + parameters: Some( + Parameters { + range: 68..70, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 69..70, + name: Identifier { + id: "a", + range: 69..70, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 72..73, + id: "a", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 74..96, + value: Lambda( + ExprLambda { + range: 74..96, + parameters: Some( + Parameters { + range: 81..91, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 82..83, + name: Identifier { + id: "a", + range: 82..83, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 85..86, + parameter: Parameter { + range: 85..86, + name: Identifier { + id: "z", + range: 85..86, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 88..91, + parameter: Parameter { + range: 88..89, + name: Identifier { + id: "x", + range: 88..89, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 90..91, + value: Int( + 0, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 93..96, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 97..117, + value: Lambda( + ExprLambda { + range: 97..117, + parameters: Some( + Parameters { + range: 104..112, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 106..112, + name: Identifier { + id: "kwargs", + range: 106..112, + }, + annotation: None, + }, + ), + }, + ), + body: Call( + ExprCall { + range: 114..117, + func: Name( + ExprName { + range: 114..115, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 115..117, + args: [], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 118..149, + value: Lambda( + ExprLambda { + range: 118..149, + parameters: Some( + Parameters { + range: 125..140, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 126..130, + name: Identifier { + id: "args", + range: 126..130, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 134..140, + name: Identifier { + id: "kwargs", + range: 134..140, + }, + annotation: None, + }, + ), + }, + ), + body: BinOp( + ExprBinOp { + range: 142..149, + left: Call( + ExprCall { + range: 142..145, + func: Name( + ExprName { + range: 142..143, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 143..145, + args: [], + keywords: [], + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 148..149, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 150..189, + value: Lambda( + ExprLambda { + range: 150..189, + parameters: Some( + Parameters { + range: 157..180, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 158..162, + name: Identifier { + id: "args", + range: 158..162, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 164..165, + parameter: Parameter { + range: 164..165, + name: Identifier { + id: "a", + range: 164..165, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 167..170, + parameter: Parameter { + range: 167..168, + name: Identifier { + id: "b", + range: 167..168, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 169..170, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 174..180, + name: Identifier { + id: "kwargs", + range: 174..180, + }, + annotation: None, + }, + ), + }, + ), + body: BinOp( + ExprBinOp { + range: 182..189, + left: Call( + ExprCall { + range: 182..185, + func: Name( + ExprName { + range: 182..183, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 183..185, + args: [], + keywords: [], + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 188..189, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 190..206, + value: Lambda( + ExprLambda { + range: 190..206, + parameters: Some( + Parameters { + range: 197..201, + posonlyargs: [ + ParameterWithDefault { + range: 197..198, + parameter: Parameter { + range: 197..198, + name: Identifier { + id: "a", + range: 197..198, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 203..206, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 207..226, + value: Lambda( + ExprLambda { + range: 207..226, + parameters: Some( + Parameters { + range: 214..221, + posonlyargs: [ + ParameterWithDefault { + range: 214..215, + parameter: Parameter { + range: 214..215, + name: Identifier { + id: "a", + range: 214..215, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 220..221, + parameter: Parameter { + range: 220..221, + name: Identifier { + id: "b", + range: 220..221, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 223..226, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 227..246, + value: Lambda( + ExprLambda { + range: 227..246, + parameters: Some( + Parameters { + range: 234..241, + posonlyargs: [ + ParameterWithDefault { + range: 234..237, + parameter: Parameter { + range: 234..235, + name: Identifier { + id: "a", + range: 234..235, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 236..237, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 243..246, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 247..272, + value: Lambda( + ExprLambda { + range: 247..272, + parameters: Some( + Parameters { + range: 254..267, + posonlyargs: [ + ParameterWithDefault { + range: 254..255, + parameter: Parameter { + range: 254..255, + name: Identifier { + id: "a", + range: 254..255, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 257..258, + parameter: Parameter { + range: 257..258, + name: Identifier { + id: "b", + range: 257..258, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 266..267, + parameter: Parameter { + range: 266..267, + name: Identifier { + id: "c", + range: 266..267, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 269..272, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 273..295, + value: Lambda( + ExprLambda { + range: 273..295, + parameters: Some( + Parameters { + range: 280..290, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 280..284, + parameter: Parameter { + range: 280..282, + name: Identifier { + id: "kw", + range: 280..282, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 283..284, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 289..290, + parameter: Parameter { + range: 289..290, + name: Identifier { + id: "a", + range: 289..290, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 292..295, + }, + ), + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_list_comp_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_list_comp_expr.snap new file mode 100644 index 0000000000000..78a0b94af178c --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_list_comp_expr.snap @@ -0,0 +1,884 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n[x for i in range(5)]\n[b for c in d if x in w if y and yy if z]\n[a for b in c if d and e for f in j if k > h]\n[a for b in c if d and e async for f in j if k > h]\n[1 for i in x in a]\n[a for a, b in G]\n[\n await x for a, b in C\n]\n[i for i in await x if entity is not None]\n[x for x in (l if True else L) if T]\n[i for i in (await x if True else X) if F]\n[i for i in await (x if True else X) if F]\n[f for f in c(x if True else [])]\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..431, + body: [ + Expr( + StmtExpr { + range: 1..22, + value: ListComp( + ExprListComp { + range: 1..22, + elt: Name( + ExprName { + range: 2..3, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 4..21, + target: Name( + ExprName { + range: 8..9, + id: "i", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 13..21, + func: Name( + ExprName { + range: 13..18, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 18..21, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 5, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 23..64, + value: ListComp( + ExprListComp { + range: 23..64, + elt: Name( + ExprName { + range: 24..25, + id: "b", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 26..63, + target: Name( + ExprName { + range: 30..31, + id: "c", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 35..36, + id: "d", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 40..46, + left: Name( + ExprName { + range: 40..41, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 45..46, + id: "w", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 50..58, + op: And, + values: [ + Name( + ExprName { + range: 50..51, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 56..58, + id: "yy", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 62..63, + id: "z", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 65..110, + value: ListComp( + ExprListComp { + range: 65..110, + elt: Name( + ExprName { + range: 66..67, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 68..89, + target: Name( + ExprName { + range: 72..73, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 77..78, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 82..89, + op: And, + values: [ + Name( + ExprName { + range: 82..83, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 88..89, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 90..109, + target: Name( + ExprName { + range: 94..95, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 99..100, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 104..109, + left: Name( + ExprName { + range: 104..105, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 108..109, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 111..162, + value: ListComp( + ExprListComp { + range: 111..162, + elt: Name( + ExprName { + range: 112..113, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 114..135, + target: Name( + ExprName { + range: 118..119, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 123..124, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 128..135, + op: And, + values: [ + Name( + ExprName { + range: 128..129, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 134..135, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 136..161, + target: Name( + ExprName { + range: 146..147, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 151..152, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 156..161, + left: Name( + ExprName { + range: 156..157, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 160..161, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: true, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 163..182, + value: ListComp( + ExprListComp { + range: 163..182, + elt: NumberLiteral( + ExprNumberLiteral { + range: 164..165, + value: Int( + 1, + ), + }, + ), + generators: [ + Comprehension { + range: 166..181, + target: Name( + ExprName { + range: 170..171, + id: "i", + ctx: Store, + }, + ), + iter: Compare( + ExprCompare { + range: 175..181, + left: Name( + ExprName { + range: 175..176, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 180..181, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 183..200, + value: ListComp( + ExprListComp { + range: 183..200, + elt: Name( + ExprName { + range: 184..185, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 186..199, + target: Tuple( + ExprTuple { + range: 190..194, + elts: [ + Name( + ExprName { + range: 190..191, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 193..194, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 198..199, + id: "G", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 201..230, + value: ListComp( + ExprListComp { + range: 201..230, + elt: Await( + ExprAwait { + range: 207..214, + value: Name( + ExprName { + range: 213..214, + id: "x", + ctx: Load, + }, + ), + }, + ), + generators: [ + Comprehension { + range: 215..228, + target: Tuple( + ExprTuple { + range: 219..223, + elts: [ + Name( + ExprName { + range: 219..220, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 222..223, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 227..228, + id: "C", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 231..273, + value: ListComp( + ExprListComp { + range: 231..273, + elt: Name( + ExprName { + range: 232..233, + id: "i", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 234..272, + target: Name( + ExprName { + range: 238..239, + id: "i", + ctx: Store, + }, + ), + iter: Await( + ExprAwait { + range: 243..250, + value: Name( + ExprName { + range: 249..250, + id: "x", + ctx: Load, + }, + ), + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 254..272, + left: Name( + ExprName { + range: 254..260, + id: "entity", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + NoneLiteral( + ExprNoneLiteral { + range: 268..272, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 274..310, + value: ListComp( + ExprListComp { + range: 274..310, + elt: Name( + ExprName { + range: 275..276, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 277..309, + target: Name( + ExprName { + range: 281..282, + id: "x", + ctx: Store, + }, + ), + iter: IfExp( + ExprIfExp { + range: 287..303, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 292..296, + value: true, + }, + ), + body: Name( + ExprName { + range: 287..288, + id: "l", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 302..303, + id: "L", + ctx: Load, + }, + ), + }, + ), + ifs: [ + Name( + ExprName { + range: 308..309, + id: "T", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 311..353, + value: ListComp( + ExprListComp { + range: 311..353, + elt: Name( + ExprName { + range: 312..313, + id: "i", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 314..352, + target: Name( + ExprName { + range: 318..319, + id: "i", + ctx: Store, + }, + ), + iter: IfExp( + ExprIfExp { + range: 324..346, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 335..339, + value: true, + }, + ), + body: Await( + ExprAwait { + range: 324..331, + value: Name( + ExprName { + range: 330..331, + id: "x", + ctx: Load, + }, + ), + }, + ), + orelse: Name( + ExprName { + range: 345..346, + id: "X", + ctx: Load, + }, + ), + }, + ), + ifs: [ + Name( + ExprName { + range: 351..352, + id: "F", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 354..396, + value: ListComp( + ExprListComp { + range: 354..396, + elt: Name( + ExprName { + range: 355..356, + id: "i", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 357..395, + target: Name( + ExprName { + range: 361..362, + id: "i", + ctx: Store, + }, + ), + iter: Await( + ExprAwait { + range: 366..390, + value: IfExp( + ExprIfExp { + range: 373..389, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 378..382, + value: true, + }, + ), + body: Name( + ExprName { + range: 373..374, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 388..389, + id: "X", + ctx: Load, + }, + ), + }, + ), + }, + ), + ifs: [ + Name( + ExprName { + range: 394..395, + id: "F", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 397..430, + value: ListComp( + ExprListComp { + range: 397..430, + elt: Name( + ExprName { + range: 398..399, + id: "f", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 400..429, + target: Name( + ExprName { + range: 404..405, + id: "f", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 409..429, + func: Name( + ExprName { + range: 409..410, + id: "c", + ctx: Load, + }, + ), + arguments: Arguments { + range: 410..429, + args: [ + IfExp( + ExprIfExp { + range: 411..428, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 416..420, + value: true, + }, + ), + body: Name( + ExprName { + range: 411..412, + id: "x", + ctx: Load, + }, + ), + orelse: List( + ExprList { + range: 426..428, + elts: [], + ctx: Load, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_list_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_list_expr.snap new file mode 100644 index 0000000000000..a54e8b8753008 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_list_expr.snap @@ -0,0 +1,357 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n[1 + i, [1, 2, 3, 4], (a, i + x, y), {a, b, c}, {a: 1}]\n[1, 2, 3]\n[]\n[1]\n[f(g(attr.H()) for c in l)]\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..102, + body: [ + Expr( + StmtExpr { + range: 1..56, + value: List( + ExprList { + range: 1..56, + elts: [ + BinOp( + ExprBinOp { + range: 2..7, + left: NumberLiteral( + ExprNumberLiteral { + range: 2..3, + value: Int( + 1, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 6..7, + id: "i", + ctx: Load, + }, + ), + }, + ), + List( + ExprList { + range: 9..21, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 4, + ), + }, + ), + ], + ctx: Load, + }, + ), + Tuple( + ExprTuple { + range: 23..36, + elts: [ + Name( + ExprName { + range: 24..25, + id: "a", + ctx: Load, + }, + ), + BinOp( + ExprBinOp { + range: 27..32, + left: Name( + ExprName { + range: 27..28, + id: "i", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 31..32, + id: "x", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 34..35, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + Set( + ExprSet { + range: 38..47, + elts: [ + Name( + ExprName { + range: 39..40, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 42..43, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 45..46, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + Dict( + ExprDict { + range: 49..55, + keys: [ + Some( + Name( + ExprName { + range: 50..51, + id: "a", + ctx: Load, + }, + ), + ), + ], + values: [ + NumberLiteral( + ExprNumberLiteral { + range: 53..54, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 57..66, + value: List( + ExprList { + range: 57..66, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 58..59, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 61..62, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 64..65, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 67..69, + value: List( + ExprList { + range: 67..69, + elts: [], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 70..73, + value: List( + ExprList { + range: 70..73, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 71..72, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 74..101, + value: List( + ExprList { + range: 74..101, + elts: [ + Call( + ExprCall { + range: 75..100, + func: Name( + ExprName { + range: 75..76, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 76..100, + args: [ + GeneratorExp( + ExprGeneratorExp { + range: 77..99, + elt: Call( + ExprCall { + range: 77..88, + func: Name( + ExprName { + range: 77..78, + id: "g", + ctx: Load, + }, + ), + arguments: Arguments { + range: 78..88, + args: [ + Call( + ExprCall { + range: 79..87, + func: Attribute( + ExprAttribute { + range: 79..85, + value: Name( + ExprName { + range: 79..83, + id: "attr", + ctx: Load, + }, + ), + attr: Identifier { + id: "H", + range: 84..85, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 85..87, + args: [], + keywords: [], + }, + }, + ), + ], + keywords: [], + }, + }, + ), + generators: [ + Comprehension { + range: 89..99, + target: Name( + ExprName { + range: 93..94, + id: "c", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 98..99, + id: "l", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + keywords: [], + }, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_match_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_match_stmt.snap new file mode 100644 index 0000000000000..8e68f5f58ffcf --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_match_stmt.snap @@ -0,0 +1,2407 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n# PatternMatchSingleton\nmatch x:\n case None:\n ...\n case True:\n ...\n case False:\n ...\n\n# PatternMatchValue\nmatch x:\n case a.b:\n ...\n case a.b.c:\n ...\n case '':\n ...\n case b'':\n ...\n case 1:\n ...\n case 1.0:\n ...\n case 1.0J:\n ...\n case 1 + 1:\n ...\n case -1:\n ...\n case -1.:\n ...\n case -0b01:\n ...\n case (1):\n ...\n\n# PatternMatchOr\nmatch x:\n case 1 | 2:\n ...\n case '' | 1.1 | -1 | 1 + 1 | a.b:\n ...\n\n# PatternMatchAs\nmatch x:\n case a as b:\n ...\n case 1 | 2 as two:\n ...\n case 1 + 3 as sum:\n ...\n case a.b as ab:\n ...\n case _:\n ...\n case _ as x:\n ...\n\n# PatternMatchSequence\nmatch x:\n case 1, 2, 3:\n ...\n case (1, 2, 3,):\n ...\n case (1 + 2, a, None, a.b):\n ...\n case (1 as X, b) as S:\n ...\n case [1, 2, 3 + 1]:\n ...\n case ([1,2], 3):\n ...\n case [1]:\n ...\n\n# PatternMatchStar\nmatch x:\n case *a:\n ...\n case *_:\n ...\n case [1, 2, *rest]:\n ...\n case (*_, 1, 2):\n ...\n\n# PatternMatchClass\nmatch x:\n case Point():\n ...\n case a.b.Point():\n ...\n case Point2D(x=0):\n ...\n case Point2D(x=0, y=0,):\n ...\n case Point2D(0, 1):\n ...\n case Point2D([0, 1], y=1):\n ...\n case Point2D(x=[0, 1], y=1):\n ...\n\n# PatternMatchMapping\nmatch x := b:\n case {1: _}:\n ...\n case {'': a, None: (1, 2), **rest}:\n ...\n\n# Pattern guard\nmatch y:\n case a if b := c: ...\n case e if 1 < 2: ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..1689, + body: [ + Match( + StmtMatch { + range: 25..115, + subject: Name( + ExprName { + range: 31..32, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 38..60, + pattern: MatchSingleton( + PatternMatchSingleton { + range: 43..47, + value: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 57..60, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 57..60, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 65..87, + pattern: MatchSingleton( + PatternMatchSingleton { + range: 70..74, + value: True, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 84..87, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 84..87, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 92..115, + pattern: MatchSingleton( + PatternMatchSingleton { + range: 97..102, + value: False, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 112..115, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 112..115, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 137..460, + subject: Name( + ExprName { + range: 143..144, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 150..171, + pattern: MatchValue( + PatternMatchValue { + range: 155..158, + value: Attribute( + ExprAttribute { + range: 155..158, + value: Name( + ExprName { + range: 155..156, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 157..158, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 168..171, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 168..171, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 176..199, + pattern: MatchValue( + PatternMatchValue { + range: 181..186, + value: Attribute( + ExprAttribute { + range: 181..186, + value: Attribute( + ExprAttribute { + range: 181..184, + value: Name( + ExprName { + range: 181..182, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 183..184, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "c", + range: 185..186, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 196..199, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 196..199, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 204..224, + pattern: MatchValue( + PatternMatchValue { + range: 209..211, + value: StringLiteral( + ExprStringLiteral { + range: 209..211, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 209..211, + value: "", + unicode: false, + }, + ), + }, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 221..224, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 221..224, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 229..250, + pattern: MatchValue( + PatternMatchValue { + range: 234..237, + value: BytesLiteral( + ExprBytesLiteral { + range: 234..237, + value: BytesLiteralValue { + inner: Single( + BytesLiteral { + range: 234..237, + value: [], + }, + ), + }, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 247..250, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 247..250, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 255..274, + pattern: MatchValue( + PatternMatchValue { + range: 260..261, + value: NumberLiteral( + ExprNumberLiteral { + range: 260..261, + value: Int( + 1, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 271..274, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 271..274, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 279..300, + pattern: MatchValue( + PatternMatchValue { + range: 284..287, + value: NumberLiteral( + ExprNumberLiteral { + range: 284..287, + value: Float( + 1.0, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 297..300, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 297..300, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 305..327, + pattern: MatchValue( + PatternMatchValue { + range: 310..314, + value: NumberLiteral( + ExprNumberLiteral { + range: 310..314, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 324..327, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 324..327, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 332..355, + pattern: MatchValue( + PatternMatchValue { + range: 337..342, + value: BinOp( + ExprBinOp { + range: 337..342, + left: NumberLiteral( + ExprNumberLiteral { + range: 337..338, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 341..342, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 352..355, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 352..355, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 360..380, + pattern: MatchValue( + PatternMatchValue { + range: 365..367, + value: UnaryOp( + ExprUnaryOp { + range: 365..367, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 366..367, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 377..380, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 377..380, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 385..406, + pattern: MatchValue( + PatternMatchValue { + range: 390..393, + value: UnaryOp( + ExprUnaryOp { + range: 390..393, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 391..393, + value: Float( + 1.0, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 403..406, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 403..406, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 411..434, + pattern: MatchValue( + PatternMatchValue { + range: 416..421, + value: UnaryOp( + ExprUnaryOp { + range: 416..421, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 417..421, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 431..434, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 431..434, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 439..460, + pattern: MatchValue( + PatternMatchValue { + range: 445..446, + value: NumberLiteral( + ExprNumberLiteral { + range: 445..446, + value: Int( + 1, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 457..460, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 457..460, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 479..565, + subject: Name( + ExprName { + range: 485..486, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 492..515, + pattern: MatchOr( + PatternMatchOr { + range: 497..502, + patterns: [ + MatchValue( + PatternMatchValue { + range: 497..498, + value: NumberLiteral( + ExprNumberLiteral { + range: 497..498, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 501..502, + value: NumberLiteral( + ExprNumberLiteral { + range: 501..502, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 512..515, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 512..515, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 520..565, + pattern: MatchOr( + PatternMatchOr { + range: 525..552, + patterns: [ + MatchValue( + PatternMatchValue { + range: 525..527, + value: StringLiteral( + ExprStringLiteral { + range: 525..527, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 525..527, + value: "", + unicode: false, + }, + ), + }, + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 530..533, + value: NumberLiteral( + ExprNumberLiteral { + range: 530..533, + value: Float( + 1.1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 536..538, + value: UnaryOp( + ExprUnaryOp { + range: 536..538, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 537..538, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 541..546, + value: BinOp( + ExprBinOp { + range: 541..546, + left: NumberLiteral( + ExprNumberLiteral { + range: 541..542, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 545..546, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 549..552, + value: Attribute( + ExprAttribute { + range: 549..552, + value: Name( + ExprName { + range: 549..550, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 551..552, + }, + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 562..565, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 562..565, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 584..776, + subject: Name( + ExprName { + range: 590..591, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 597..621, + pattern: MatchAs( + PatternMatchAs { + range: 602..608, + pattern: Some( + MatchAs( + PatternMatchAs { + range: 602..603, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 602..603, + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "b", + range: 607..608, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 618..621, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 618..621, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 626..656, + pattern: MatchAs( + PatternMatchAs { + range: 631..643, + pattern: Some( + MatchOr( + PatternMatchOr { + range: 631..636, + patterns: [ + MatchValue( + PatternMatchValue { + range: 631..632, + value: NumberLiteral( + ExprNumberLiteral { + range: 631..632, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 635..636, + value: NumberLiteral( + ExprNumberLiteral { + range: 635..636, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + ), + name: Some( + Identifier { + id: "two", + range: 640..643, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 653..656, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 653..656, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 661..691, + pattern: MatchAs( + PatternMatchAs { + range: 666..678, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 666..671, + value: BinOp( + ExprBinOp { + range: 666..671, + left: NumberLiteral( + ExprNumberLiteral { + range: 666..667, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 670..671, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "sum", + range: 675..678, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 688..691, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 688..691, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 696..723, + pattern: MatchAs( + PatternMatchAs { + range: 701..710, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 701..704, + value: Attribute( + ExprAttribute { + range: 701..704, + value: Name( + ExprName { + range: 701..702, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 703..704, + }, + ctx: Load, + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "ab", + range: 708..710, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 720..723, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 720..723, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 728..747, + pattern: MatchAs( + PatternMatchAs { + range: 733..734, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 744..747, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 744..747, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 752..776, + pattern: MatchAs( + PatternMatchAs { + range: 757..763, + pattern: Some( + MatchAs( + PatternMatchAs { + range: 757..758, + pattern: None, + name: None, + }, + ), + ), + name: Some( + Identifier { + id: "x", + range: 762..763, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 773..776, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 773..776, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 801..1050, + subject: Name( + ExprName { + range: 807..808, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 814..839, + pattern: MatchSequence( + PatternMatchSequence { + range: 819..826, + patterns: [ + MatchValue( + PatternMatchValue { + range: 819..820, + value: NumberLiteral( + ExprNumberLiteral { + range: 819..820, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 822..823, + value: NumberLiteral( + ExprNumberLiteral { + range: 822..823, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 825..826, + value: NumberLiteral( + ExprNumberLiteral { + range: 825..826, + value: Int( + 3, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 836..839, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 836..839, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 844..872, + pattern: MatchSequence( + PatternMatchSequence { + range: 849..859, + patterns: [ + MatchValue( + PatternMatchValue { + range: 850..851, + value: NumberLiteral( + ExprNumberLiteral { + range: 850..851, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 853..854, + value: NumberLiteral( + ExprNumberLiteral { + range: 853..854, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 856..857, + value: NumberLiteral( + ExprNumberLiteral { + range: 856..857, + value: Int( + 3, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 869..872, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 869..872, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 877..916, + pattern: MatchSequence( + PatternMatchSequence { + range: 882..903, + patterns: [ + MatchValue( + PatternMatchValue { + range: 883..888, + value: BinOp( + ExprBinOp { + range: 883..888, + left: NumberLiteral( + ExprNumberLiteral { + range: 883..884, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 887..888, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 890..891, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 890..891, + }, + ), + }, + ), + MatchSingleton( + PatternMatchSingleton { + range: 893..897, + value: None, + }, + ), + MatchValue( + PatternMatchValue { + range: 899..902, + value: Attribute( + ExprAttribute { + range: 899..902, + value: Name( + ExprName { + range: 899..900, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 901..902, + }, + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 913..916, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 913..916, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 921..955, + pattern: MatchAs( + PatternMatchAs { + range: 926..942, + pattern: Some( + MatchSequence( + PatternMatchSequence { + range: 926..937, + patterns: [ + MatchAs( + PatternMatchAs { + range: 927..933, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 927..928, + value: NumberLiteral( + ExprNumberLiteral { + range: 927..928, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "X", + range: 932..933, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 935..936, + pattern: None, + name: Some( + Identifier { + id: "b", + range: 935..936, + }, + ), + }, + ), + ], + }, + ), + ), + name: Some( + Identifier { + id: "S", + range: 941..942, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 952..955, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 952..955, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 960..991, + pattern: MatchSequence( + PatternMatchSequence { + range: 965..978, + patterns: [ + MatchValue( + PatternMatchValue { + range: 966..967, + value: NumberLiteral( + ExprNumberLiteral { + range: 966..967, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 969..970, + value: NumberLiteral( + ExprNumberLiteral { + range: 969..970, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 972..977, + value: BinOp( + ExprBinOp { + range: 972..977, + left: NumberLiteral( + ExprNumberLiteral { + range: 972..973, + value: Int( + 3, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 976..977, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 988..991, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 988..991, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 996..1024, + pattern: MatchSequence( + PatternMatchSequence { + range: 1001..1011, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 1002..1007, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1003..1004, + value: NumberLiteral( + ExprNumberLiteral { + range: 1003..1004, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1005..1006, + value: NumberLiteral( + ExprNumberLiteral { + range: 1005..1006, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + MatchValue( + PatternMatchValue { + range: 1009..1010, + value: NumberLiteral( + ExprNumberLiteral { + range: 1009..1010, + value: Int( + 3, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1021..1024, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1021..1024, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1029..1050, + pattern: MatchSequence( + PatternMatchSequence { + range: 1034..1037, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1035..1036, + value: NumberLiteral( + ExprNumberLiteral { + range: 1035..1036, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1047..1050, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1047..1050, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1071..1198, + subject: Name( + ExprName { + range: 1077..1078, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1084..1104, + pattern: MatchStar( + PatternMatchStar { + range: 1089..1091, + name: Some( + Identifier { + id: "a", + range: 1090..1091, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1101..1104, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1101..1104, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1109..1129, + pattern: MatchStar( + PatternMatchStar { + range: 1114..1116, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1126..1129, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1126..1129, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1134..1165, + pattern: MatchSequence( + PatternMatchSequence { + range: 1139..1152, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1140..1141, + value: NumberLiteral( + ExprNumberLiteral { + range: 1140..1141, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1143..1144, + value: NumberLiteral( + ExprNumberLiteral { + range: 1143..1144, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchStar( + PatternMatchStar { + range: 1146..1151, + name: Some( + Identifier { + id: "rest", + range: 1147..1151, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1162..1165, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1162..1165, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1170..1198, + pattern: MatchSequence( + PatternMatchSequence { + range: 1175..1185, + patterns: [ + MatchStar( + PatternMatchStar { + range: 1176..1178, + name: None, + }, + ), + MatchValue( + PatternMatchValue { + range: 1180..1181, + value: NumberLiteral( + ExprNumberLiteral { + range: 1180..1181, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1183..1184, + value: NumberLiteral( + ExprNumberLiteral { + range: 1183..1184, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1195..1198, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1195..1198, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1220..1492, + subject: Name( + ExprName { + range: 1226..1227, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1233..1258, + pattern: MatchClass( + PatternMatchClass { + range: 1238..1245, + cls: Name( + ExprName { + range: 1238..1243, + id: "Point", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 1243..1245, + patterns: [], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1255..1258, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1255..1258, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1263..1292, + pattern: MatchClass( + PatternMatchClass { + range: 1268..1279, + cls: Attribute( + ExprAttribute { + range: 1268..1277, + value: Attribute( + ExprAttribute { + range: 1268..1271, + value: Name( + ExprName { + range: 1268..1269, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 1270..1271, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "Point", + range: 1272..1277, + }, + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 1277..1279, + patterns: [], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1289..1292, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1289..1292, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1297..1327, + pattern: MatchClass( + PatternMatchClass { + range: 1302..1314, + cls: Name( + ExprName { + range: 1302..1309, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 1309..1314, + patterns: [], + keywords: [ + PatternKeyword { + range: 1310..1313, + attr: Identifier { + id: "x", + range: 1310..1311, + }, + pattern: MatchValue( + PatternMatchValue { + range: 1312..1313, + value: NumberLiteral( + ExprNumberLiteral { + range: 1312..1313, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1324..1327, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1324..1327, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1332..1368, + pattern: MatchClass( + PatternMatchClass { + range: 1337..1355, + cls: Name( + ExprName { + range: 1337..1344, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 1344..1355, + patterns: [], + keywords: [ + PatternKeyword { + range: 1345..1348, + attr: Identifier { + id: "x", + range: 1345..1346, + }, + pattern: MatchValue( + PatternMatchValue { + range: 1347..1348, + value: NumberLiteral( + ExprNumberLiteral { + range: 1347..1348, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + PatternKeyword { + range: 1350..1353, + attr: Identifier { + id: "y", + range: 1350..1351, + }, + pattern: MatchValue( + PatternMatchValue { + range: 1352..1353, + value: NumberLiteral( + ExprNumberLiteral { + range: 1352..1353, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1365..1368, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1365..1368, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1373..1404, + pattern: MatchClass( + PatternMatchClass { + range: 1378..1391, + cls: Name( + ExprName { + range: 1378..1385, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 1385..1391, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1386..1387, + value: NumberLiteral( + ExprNumberLiteral { + range: 1386..1387, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1389..1390, + value: NumberLiteral( + ExprNumberLiteral { + range: 1389..1390, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1401..1404, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1401..1404, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1409..1447, + pattern: MatchClass( + PatternMatchClass { + range: 1414..1434, + cls: Name( + ExprName { + range: 1414..1421, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 1421..1434, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 1422..1428, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1423..1424, + value: NumberLiteral( + ExprNumberLiteral { + range: 1423..1424, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1426..1427, + value: NumberLiteral( + ExprNumberLiteral { + range: 1426..1427, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + ], + keywords: [ + PatternKeyword { + range: 1430..1433, + attr: Identifier { + id: "y", + range: 1430..1431, + }, + pattern: MatchValue( + PatternMatchValue { + range: 1432..1433, + value: NumberLiteral( + ExprNumberLiteral { + range: 1432..1433, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1444..1447, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1444..1447, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1452..1492, + pattern: MatchClass( + PatternMatchClass { + range: 1457..1479, + cls: Name( + ExprName { + range: 1457..1464, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 1464..1479, + patterns: [], + keywords: [ + PatternKeyword { + range: 1465..1473, + attr: Identifier { + id: "x", + range: 1465..1466, + }, + pattern: MatchSequence( + PatternMatchSequence { + range: 1467..1473, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1468..1469, + value: NumberLiteral( + ExprNumberLiteral { + range: 1468..1469, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1471..1472, + value: NumberLiteral( + ExprNumberLiteral { + range: 1471..1472, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + }, + PatternKeyword { + range: 1475..1478, + attr: Identifier { + id: "y", + range: 1475..1476, + }, + pattern: MatchValue( + PatternMatchValue { + range: 1477..1478, + value: NumberLiteral( + ExprNumberLiteral { + range: 1477..1478, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1489..1492, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1489..1492, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1516..1610, + subject: NamedExpr( + ExprNamedExpr { + range: 1522..1528, + target: Name( + ExprName { + range: 1522..1523, + id: "x", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 1527..1528, + id: "b", + ctx: Load, + }, + ), + }, + ), + cases: [ + MatchCase { + range: 1534..1558, + pattern: MatchMapping( + PatternMatchMapping { + range: 1539..1545, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 1540..1541, + value: Int( + 1, + ), + }, + ), + ], + patterns: [ + MatchAs( + PatternMatchAs { + range: 1543..1544, + pattern: None, + name: None, + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1555..1558, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1555..1558, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1563..1610, + pattern: MatchMapping( + PatternMatchMapping { + range: 1568..1597, + keys: [ + StringLiteral( + ExprStringLiteral { + range: 1569..1571, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 1569..1571, + value: "", + unicode: false, + }, + ), + }, + }, + ), + NoneLiteral( + ExprNoneLiteral { + range: 1576..1580, + }, + ), + ], + patterns: [ + MatchAs( + PatternMatchAs { + range: 1573..1574, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 1573..1574, + }, + ), + }, + ), + MatchSequence( + PatternMatchSequence { + range: 1582..1588, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1583..1584, + value: NumberLiteral( + ExprNumberLiteral { + range: 1583..1584, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1586..1587, + value: NumberLiteral( + ExprNumberLiteral { + range: 1586..1587, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + ], + rest: Some( + Identifier { + id: "rest", + range: 1592..1596, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 1607..1610, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1607..1610, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1628..1688, + subject: Name( + ExprName { + range: 1634..1635, + id: "y", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1641..1662, + pattern: MatchAs( + PatternMatchAs { + range: 1646..1647, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 1646..1647, + }, + ), + }, + ), + guard: Some( + NamedExpr( + ExprNamedExpr { + range: 1651..1657, + target: Name( + ExprName { + range: 1651..1652, + id: "b", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 1656..1657, + id: "c", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 1659..1662, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1659..1662, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1667..1688, + pattern: MatchAs( + PatternMatchAs { + range: 1672..1673, + pattern: None, + name: Some( + Identifier { + id: "e", + range: 1672..1673, + }, + ), + }, + ), + guard: Some( + Compare( + ExprCompare { + range: 1678..1683, + left: NumberLiteral( + ExprNumberLiteral { + range: 1678..1679, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 1682..1683, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 1685..1688, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1685..1688, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_named_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_named_expr.snap new file mode 100644 index 0000000000000..d6fedec5f4216 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_named_expr.snap @@ -0,0 +1,351 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n(x:=1)\n{ x := 1 }\n[x := 1]\n(x := 1 + 1)\n(x,y := a and b)\n{ x,y := a < b }\n[x,y := ...]\nf(a:=b, c:=d)\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..102, + body: [ + Expr( + StmtExpr { + range: 1..7, + value: NamedExpr( + ExprNamedExpr { + range: 2..6, + target: Name( + ExprName { + range: 2..3, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 8..18, + value: Set( + ExprSet { + range: 8..18, + elts: [ + NamedExpr( + ExprNamedExpr { + range: 10..16, + target: Name( + ExprName { + range: 10..11, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..27, + value: List( + ExprList { + range: 19..27, + elts: [ + NamedExpr( + ExprNamedExpr { + range: 20..26, + target: Name( + ExprName { + range: 20..21, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 28..40, + value: NamedExpr( + ExprNamedExpr { + range: 29..39, + target: Name( + ExprName { + range: 29..30, + id: "x", + ctx: Store, + }, + ), + value: BinOp( + ExprBinOp { + range: 34..39, + left: NumberLiteral( + ExprNumberLiteral { + range: 34..35, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 38..39, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 41..57, + value: Tuple( + ExprTuple { + range: 41..57, + elts: [ + Name( + ExprName { + range: 42..43, + id: "x", + ctx: Load, + }, + ), + NamedExpr( + ExprNamedExpr { + range: 44..56, + target: Name( + ExprName { + range: 44..45, + id: "y", + ctx: Store, + }, + ), + value: BoolOp( + ExprBoolOp { + range: 49..56, + op: And, + values: [ + Name( + ExprName { + range: 49..50, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 55..56, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 58..74, + value: Set( + ExprSet { + range: 58..74, + elts: [ + Name( + ExprName { + range: 60..61, + id: "x", + ctx: Load, + }, + ), + NamedExpr( + ExprNamedExpr { + range: 62..72, + target: Name( + ExprName { + range: 62..63, + id: "y", + ctx: Store, + }, + ), + value: Compare( + ExprCompare { + range: 67..72, + left: Name( + ExprName { + range: 67..68, + id: "a", + ctx: Load, + }, + ), + ops: [ + Lt, + ], + comparators: [ + Name( + ExprName { + range: 71..72, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 75..87, + value: List( + ExprList { + range: 75..87, + elts: [ + Name( + ExprName { + range: 76..77, + id: "x", + ctx: Load, + }, + ), + NamedExpr( + ExprNamedExpr { + range: 78..86, + target: Name( + ExprName { + range: 78..79, + id: "y", + ctx: Store, + }, + ), + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 83..86, + }, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 88..101, + value: Call( + ExprCall { + range: 88..101, + func: Name( + ExprName { + range: 88..89, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 89..101, + args: [ + NamedExpr( + ExprNamedExpr { + range: 90..94, + target: Name( + ExprName { + range: 90..91, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 93..94, + id: "b", + ctx: Load, + }, + ), + }, + ), + NamedExpr( + ExprNamedExpr { + range: 96..100, + target: Name( + ExprName { + range: 96..97, + id: "c", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 99..100, + id: "d", + ctx: Load, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_parenthesized_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_parenthesized_expr.snap new file mode 100644 index 0000000000000..ac552cd3bc41b --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_parenthesized_expr.snap @@ -0,0 +1,132 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n(l)\n(l)()\n(l)()()()\n(a\nand\nb\nor\nc)\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..36, + body: [ + Expr( + StmtExpr { + range: 1..4, + value: Name( + ExprName { + range: 2..3, + id: "l", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..10, + value: Call( + ExprCall { + range: 5..10, + func: Name( + ExprName { + range: 6..7, + id: "l", + ctx: Load, + }, + ), + arguments: Arguments { + range: 8..10, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..20, + value: Call( + ExprCall { + range: 11..20, + func: Call( + ExprCall { + range: 11..18, + func: Call( + ExprCall { + range: 11..16, + func: Name( + ExprName { + range: 12..13, + id: "l", + ctx: Load, + }, + ), + arguments: Arguments { + range: 14..16, + args: [], + keywords: [], + }, + }, + ), + arguments: Arguments { + range: 16..18, + args: [], + keywords: [], + }, + }, + ), + arguments: Arguments { + range: 18..20, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..35, + value: BoolOp( + ExprBoolOp { + range: 22..34, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 22..29, + op: And, + values: [ + Name( + ExprName { + range: 22..23, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 28..29, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 33..34, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_set_comp_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_set_comp_expr.snap new file mode 100644 index 0000000000000..9f05720e9b6c9 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_set_comp_expr.snap @@ -0,0 +1,408 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n{x for i in ll}\n{b for c in d if x in w if y and yy if z}\n{a for b in c if d and e for f in j if k > h}\n{a for b in c if d and e async for f in j if k > h}\n{a for a, b in G}\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..175, + body: [ + Expr( + StmtExpr { + range: 1..16, + value: SetComp( + ExprSetComp { + range: 1..16, + elt: Name( + ExprName { + range: 2..3, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 4..15, + target: Name( + ExprName { + range: 8..9, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 13..15, + id: "ll", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 17..58, + value: SetComp( + ExprSetComp { + range: 17..58, + elt: Name( + ExprName { + range: 18..19, + id: "b", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 20..57, + target: Name( + ExprName { + range: 24..25, + id: "c", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 29..30, + id: "d", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 34..40, + left: Name( + ExprName { + range: 34..35, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 39..40, + id: "w", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 44..52, + op: And, + values: [ + Name( + ExprName { + range: 44..45, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 50..52, + id: "yy", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 56..57, + id: "z", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 59..104, + value: SetComp( + ExprSetComp { + range: 59..104, + elt: Name( + ExprName { + range: 60..61, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 62..83, + target: Name( + ExprName { + range: 66..67, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 71..72, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 76..83, + op: And, + values: [ + Name( + ExprName { + range: 76..77, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 82..83, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 84..103, + target: Name( + ExprName { + range: 88..89, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 93..94, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 98..103, + left: Name( + ExprName { + range: 98..99, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 102..103, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 105..156, + value: SetComp( + ExprSetComp { + range: 105..156, + elt: Name( + ExprName { + range: 106..107, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 108..129, + target: Name( + ExprName { + range: 112..113, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 117..118, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 122..129, + op: And, + values: [ + Name( + ExprName { + range: 122..123, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 128..129, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 130..155, + target: Name( + ExprName { + range: 140..141, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 145..146, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 150..155, + left: Name( + ExprName { + range: 150..151, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 154..155, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: true, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 157..174, + value: SetComp( + ExprSetComp { + range: 157..174, + elt: Name( + ExprName { + range: 158..159, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 160..173, + target: Tuple( + ExprTuple { + range: 164..168, + elts: [ + Name( + ExprName { + range: 164..165, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 167..168, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 172..173, + id: "G", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_set_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_set_expr.snap new file mode 100644 index 0000000000000..b37cd7a6b0d02 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_set_expr.snap @@ -0,0 +1,190 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n{1, 2, 3}\n{1 + 2, (a, b), {1,2,3}, {a:b, **d}}\n{a}\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..52, + body: [ + Expr( + StmtExpr { + range: 1..10, + value: Set( + ExprSet { + range: 1..10, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 2..3, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..47, + value: Set( + ExprSet { + range: 11..47, + elts: [ + BinOp( + ExprBinOp { + range: 12..17, + left: NumberLiteral( + ExprNumberLiteral { + range: 12..13, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 2, + ), + }, + ), + }, + ), + Tuple( + ExprTuple { + range: 19..25, + elts: [ + Name( + ExprName { + range: 20..21, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 23..24, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + Set( + ExprSet { + range: 27..34, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 30..31, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 32..33, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + Dict( + ExprDict { + range: 36..46, + keys: [ + Some( + Name( + ExprName { + range: 37..38, + id: "a", + ctx: Load, + }, + ), + ), + None, + ], + values: [ + Name( + ExprName { + range: 39..40, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 44..45, + id: "d", + ctx: Load, + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 48..51, + value: Set( + ExprSet { + range: 48..51, + elts: [ + Name( + ExprName { + range: 49..50, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_simple_stmts.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_simple_stmts.snap new file mode 100644 index 0000000000000..ed65908a33124 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_simple_stmts.snap @@ -0,0 +1,1174 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nif x: ...\nif True: pass\n1; 2; pass\n1; ...; a if b else c\n\ncontinue\n\nbreak\n\ndel a\ndel a, b, 1, 1 + 2,\ndel a, (b, c), d\n\nassert 1 < 2\nassert f()\nassert a and b\nassert x, 'error'\n\nglobal a\nglobal a, b, c\n\nreturn\nreturn a and b\nreturn 1 < 2\nreturn None\nreturn 1, 2,\nreturn x\nreturn f()\nreturn a.f()\n\nnonlocal a\nnonlocal a, b, c\n\nraise\nraise a\nraise 1 < 2\nraise a and b\nraise a from b\n\nimport a\nimport a.b.c\nimport a.b.c as d\nimport a, b, c\nimport foo.bar as a, a.b.c.d as abcd\n\nfrom a import b # comment\nfrom . import a\nfrom foo.bar import baz as b, FooBar as fb\nfrom .a import b\nfrom ... import c\nfrom .......................... import d\nfrom ..........................a.b.c import d\nfrom module import (a, b as B, c,)\nfrom a import *\n\nif c: B; del A\nelse: C\nif x: yield x;\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..772, + body: [ + If( + StmtIf { + range: 1..10, + test: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 7..10, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 7..10, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 11..24, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 14..18, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 20..24, + }, + ), + ], + elif_else_clauses: [], + }, + ), + Expr( + StmtExpr { + range: 25..26, + value: NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 28..29, + value: NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 2, + ), + }, + ), + }, + ), + Pass( + StmtPass { + range: 31..35, + }, + ), + Expr( + StmtExpr { + range: 36..37, + value: NumberLiteral( + ExprNumberLiteral { + range: 36..37, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 39..42, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 39..42, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 44..57, + value: IfExp( + ExprIfExp { + range: 44..57, + test: Name( + ExprName { + range: 49..50, + id: "b", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 44..45, + id: "a", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 56..57, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + Continue( + StmtContinue { + range: 59..67, + }, + ), + Break( + StmtBreak { + range: 69..74, + }, + ), + Delete( + StmtDelete { + range: 76..81, + targets: [ + Name( + ExprName { + range: 80..81, + id: "a", + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 82..101, + targets: [ + Name( + ExprName { + range: 86..87, + id: "a", + ctx: Del, + }, + ), + Name( + ExprName { + range: 89..90, + id: "b", + ctx: Del, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 92..93, + value: Int( + 1, + ), + }, + ), + BinOp( + ExprBinOp { + range: 95..100, + left: NumberLiteral( + ExprNumberLiteral { + range: 95..96, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 99..100, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 102..118, + targets: [ + Name( + ExprName { + range: 106..107, + id: "a", + ctx: Del, + }, + ), + Tuple( + ExprTuple { + range: 109..115, + elts: [ + Name( + ExprName { + range: 110..111, + id: "b", + ctx: Del, + }, + ), + Name( + ExprName { + range: 113..114, + id: "c", + ctx: Del, + }, + ), + ], + ctx: Del, + parenthesized: true, + }, + ), + Name( + ExprName { + range: 117..118, + id: "d", + ctx: Del, + }, + ), + ], + }, + ), + Assert( + StmtAssert { + range: 120..132, + test: Compare( + ExprCompare { + range: 127..132, + left: NumberLiteral( + ExprNumberLiteral { + range: 127..128, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 131..132, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 133..143, + test: Call( + ExprCall { + range: 140..143, + func: Name( + ExprName { + range: 140..141, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 141..143, + args: [], + keywords: [], + }, + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 144..158, + test: BoolOp( + ExprBoolOp { + range: 151..158, + op: And, + values: [ + Name( + ExprName { + range: 151..152, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 157..158, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 159..176, + test: Name( + ExprName { + range: 166..167, + id: "x", + ctx: Load, + }, + ), + msg: Some( + StringLiteral( + ExprStringLiteral { + range: 169..176, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 169..176, + value: "error", + unicode: false, + }, + ), + }, + }, + ), + ), + }, + ), + Global( + StmtGlobal { + range: 178..186, + names: [ + Identifier { + id: "a", + range: 185..186, + }, + ], + }, + ), + Global( + StmtGlobal { + range: 187..201, + names: [ + Identifier { + id: "a", + range: 194..195, + }, + Identifier { + id: "b", + range: 197..198, + }, + Identifier { + id: "c", + range: 200..201, + }, + ], + }, + ), + Return( + StmtReturn { + range: 203..209, + value: None, + }, + ), + Return( + StmtReturn { + range: 210..224, + value: Some( + BoolOp( + ExprBoolOp { + range: 217..224, + op: And, + values: [ + Name( + ExprName { + range: 217..218, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 223..224, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 225..237, + value: Some( + Compare( + ExprCompare { + range: 232..237, + left: NumberLiteral( + ExprNumberLiteral { + range: 232..233, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 236..237, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 238..249, + value: Some( + NoneLiteral( + ExprNoneLiteral { + range: 245..249, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 250..262, + value: Some( + Tuple( + ExprTuple { + range: 257..262, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 257..258, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 260..261, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 263..271, + value: Some( + Name( + ExprName { + range: 270..271, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 272..282, + value: Some( + Call( + ExprCall { + range: 279..282, + func: Name( + ExprName { + range: 279..280, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 280..282, + args: [], + keywords: [], + }, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 283..295, + value: Some( + Call( + ExprCall { + range: 290..295, + func: Attribute( + ExprAttribute { + range: 290..293, + value: Name( + ExprName { + range: 290..291, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "f", + range: 292..293, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 293..295, + args: [], + keywords: [], + }, + }, + ), + ), + }, + ), + Nonlocal( + StmtNonlocal { + range: 297..307, + names: [ + Identifier { + id: "a", + range: 306..307, + }, + ], + }, + ), + Nonlocal( + StmtNonlocal { + range: 308..324, + names: [ + Identifier { + id: "a", + range: 317..318, + }, + Identifier { + id: "b", + range: 320..321, + }, + Identifier { + id: "c", + range: 323..324, + }, + ], + }, + ), + Raise( + StmtRaise { + range: 326..331, + exc: None, + cause: None, + }, + ), + Raise( + StmtRaise { + range: 332..339, + exc: Some( + Name( + ExprName { + range: 338..339, + id: "a", + ctx: Load, + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 340..351, + exc: Some( + Compare( + ExprCompare { + range: 346..351, + left: NumberLiteral( + ExprNumberLiteral { + range: 346..347, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 350..351, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 352..365, + exc: Some( + BoolOp( + ExprBoolOp { + range: 358..365, + op: And, + values: [ + Name( + ExprName { + range: 358..359, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 364..365, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 366..380, + exc: Some( + Name( + ExprName { + range: 372..373, + id: "a", + ctx: Load, + }, + ), + ), + cause: Some( + Name( + ExprName { + range: 379..380, + id: "b", + ctx: Load, + }, + ), + ), + }, + ), + Import( + StmtImport { + range: 382..390, + names: [ + Alias { + range: 389..390, + name: Identifier { + id: "a", + range: 389..390, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 391..403, + names: [ + Alias { + range: 398..403, + name: Identifier { + id: "a.b.c", + range: 398..403, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 404..421, + names: [ + Alias { + range: 411..421, + name: Identifier { + id: "a.b.c", + range: 411..416, + }, + asname: Some( + Identifier { + id: "d", + range: 420..421, + }, + ), + }, + ], + }, + ), + Import( + StmtImport { + range: 422..436, + names: [ + Alias { + range: 429..430, + name: Identifier { + id: "a", + range: 429..430, + }, + asname: None, + }, + Alias { + range: 432..433, + name: Identifier { + id: "b", + range: 432..433, + }, + asname: None, + }, + Alias { + range: 435..436, + name: Identifier { + id: "c", + range: 435..436, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 437..473, + names: [ + Alias { + range: 444..456, + name: Identifier { + id: "foo.bar", + range: 444..451, + }, + asname: Some( + Identifier { + id: "a", + range: 455..456, + }, + ), + }, + Alias { + range: 458..473, + name: Identifier { + id: "a.b.c.d", + range: 458..465, + }, + asname: Some( + Identifier { + id: "abcd", + range: 469..473, + }, + ), + }, + ], + }, + ), + ImportFrom( + StmtImportFrom { + range: 475..490, + module: Some( + Identifier { + id: "a", + range: 480..481, + }, + ), + names: [ + Alias { + range: 489..490, + name: Identifier { + id: "b", + range: 489..490, + }, + asname: None, + }, + ], + level: Some( + 0, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 501..516, + module: None, + names: [ + Alias { + range: 515..516, + name: Identifier { + id: "a", + range: 515..516, + }, + asname: None, + }, + ], + level: Some( + 1, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 517..559, + module: Some( + Identifier { + id: "foo.bar", + range: 522..529, + }, + ), + names: [ + Alias { + range: 537..545, + name: Identifier { + id: "baz", + range: 537..540, + }, + asname: Some( + Identifier { + id: "b", + range: 544..545, + }, + ), + }, + Alias { + range: 547..559, + name: Identifier { + id: "FooBar", + range: 547..553, + }, + asname: Some( + Identifier { + id: "fb", + range: 557..559, + }, + ), + }, + ], + level: Some( + 0, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 560..576, + module: Some( + Identifier { + id: "a", + range: 566..567, + }, + ), + names: [ + Alias { + range: 575..576, + name: Identifier { + id: "b", + range: 575..576, + }, + asname: None, + }, + ], + level: Some( + 1, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 577..594, + module: None, + names: [ + Alias { + range: 593..594, + name: Identifier { + id: "c", + range: 593..594, + }, + asname: None, + }, + ], + level: Some( + 3, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 595..635, + module: None, + names: [ + Alias { + range: 634..635, + name: Identifier { + id: "d", + range: 634..635, + }, + asname: None, + }, + ], + level: Some( + 26, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 636..681, + module: Some( + Identifier { + id: "a.b.c", + range: 667..672, + }, + ), + names: [ + Alias { + range: 680..681, + name: Identifier { + id: "d", + range: 680..681, + }, + asname: None, + }, + ], + level: Some( + 26, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 682..716, + module: Some( + Identifier { + id: "module", + range: 687..693, + }, + ), + names: [ + Alias { + range: 702..703, + name: Identifier { + id: "a", + range: 702..703, + }, + asname: None, + }, + Alias { + range: 705..711, + name: Identifier { + id: "b", + range: 705..706, + }, + asname: Some( + Identifier { + id: "B", + range: 710..711, + }, + ), + }, + Alias { + range: 713..714, + name: Identifier { + id: "c", + range: 713..714, + }, + asname: None, + }, + ], + level: Some( + 0, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 717..732, + module: Some( + Identifier { + id: "a", + range: 722..723, + }, + ), + names: [ + Alias { + range: 731..732, + name: Identifier { + id: "*", + range: 731..732, + }, + asname: None, + }, + ], + level: Some( + 0, + ), + }, + ), + If( + StmtIf { + range: 734..756, + test: Name( + ExprName { + range: 737..738, + id: "c", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 740..741, + value: Name( + ExprName { + range: 740..741, + id: "B", + ctx: Load, + }, + ), + }, + ), + Delete( + StmtDelete { + range: 743..748, + targets: [ + Name( + ExprName { + range: 747..748, + id: "A", + ctx: Del, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 749..756, + test: None, + body: [ + Expr( + StmtExpr { + range: 755..756, + value: Name( + ExprName { + range: 755..756, + id: "C", + ctx: Load, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 757..770, + test: Name( + ExprName { + range: 760..761, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 763..770, + value: Yield( + ExprYield { + range: 763..770, + value: Some( + Name( + ExprName { + range: 769..770, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_starred_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_starred_expr.snap new file mode 100644 index 0000000000000..f6137a0e27846 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_starred_expr.snap @@ -0,0 +1,92 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n*a\n*(a + 1)\n*x.attr\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..21, + body: [ + Expr( + StmtExpr { + range: 1..3, + value: Starred( + ExprStarred { + range: 1..3, + value: Name( + ExprName { + range: 2..3, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 4..12, + value: Starred( + ExprStarred { + range: 4..12, + value: BinOp( + ExprBinOp { + range: 6..11, + left: Name( + ExprName { + range: 6..7, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 1, + ), + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..20, + value: Starred( + ExprStarred { + range: 13..20, + value: Attribute( + ExprAttribute { + range: 14..20, + value: Name( + ExprName { + range: 14..15, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 16..20, + }, + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_string_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_string_expr.snap new file mode 100644 index 0000000000000..1cb13f26d220b --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_string_expr.snap @@ -0,0 +1,255 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(r#\"\n'Hello World'\n\"😎\"\n'Foo' 'Bar'\n(\n 'A'\n 'B'\n 'C'\n)\n'''Olá, Mundo!'''\n\"\"\"ABCDE\"\"\"\n(\n '''aB'''\n '''cD'''\n)\nb'hello world'\nb'bytes' b'concatenated'\n\"#)" +--- +Program { + ast: Module( + ModModule { + range: 0..163, + body: [ + Expr( + StmtExpr { + range: 1..14, + value: StringLiteral( + ExprStringLiteral { + range: 1..14, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 1..14, + value: "Hello World", + unicode: false, + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 15..21, + value: StringLiteral( + ExprStringLiteral { + range: 15..21, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 15..21, + value: "😎", + unicode: false, + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..33, + value: StringLiteral( + ExprStringLiteral { + range: 22..33, + value: StringLiteralValue { + inner: Concatenated( + ConcatenatedStringLiteral { + strings: [ + StringLiteral { + range: 22..27, + value: "Foo", + unicode: false, + }, + StringLiteral { + range: 28..33, + value: "Bar", + unicode: false, + }, + ], + value: "FooBar", + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 34..61, + value: StringLiteral( + ExprStringLiteral { + range: 40..59, + value: StringLiteralValue { + inner: Concatenated( + ConcatenatedStringLiteral { + strings: [ + StringLiteral { + range: 40..43, + value: "A", + unicode: false, + }, + StringLiteral { + range: 48..51, + value: "B", + unicode: false, + }, + StringLiteral { + range: 56..59, + value: "C", + unicode: false, + }, + ], + value: "ABC", + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 62..80, + value: StringLiteral( + ExprStringLiteral { + range: 62..80, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 62..80, + value: "Olá, Mundo!", + unicode: false, + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 81..92, + value: StringLiteral( + ExprStringLiteral { + range: 81..92, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 81..92, + value: "ABCDE", + unicode: false, + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 93..122, + value: StringLiteral( + ExprStringLiteral { + range: 99..120, + value: StringLiteralValue { + inner: Concatenated( + ConcatenatedStringLiteral { + strings: [ + StringLiteral { + range: 99..107, + value: "aB", + unicode: false, + }, + StringLiteral { + range: 112..120, + value: "cD", + unicode: false, + }, + ], + value: "aBcD", + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 123..137, + value: BytesLiteral( + ExprBytesLiteral { + range: 123..137, + value: BytesLiteralValue { + inner: Single( + BytesLiteral { + range: 123..137, + value: [ + 104, + 101, + 108, + 108, + 111, + 32, + 119, + 111, + 114, + 108, + 100, + ], + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 138..162, + value: BytesLiteral( + ExprBytesLiteral { + range: 138..162, + value: BytesLiteralValue { + inner: Concatenated( + [ + BytesLiteral { + range: 138..146, + value: [ + 98, + 121, + 116, + 101, + 115, + ], + }, + BytesLiteral { + range: 147..162, + value: [ + 99, + 111, + 110, + 99, + 97, + 116, + 101, + 110, + 97, + 116, + 101, + 100, + ], + }, + ], + ), + }, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_subscript_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_subscript_expr.snap new file mode 100644 index 0000000000000..b3f0849039da0 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_subscript_expr.snap @@ -0,0 +1,959 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nl[0]\nl[1:]\nl[1:2]\nl[:2]\nl[:2:]\nl[:2:3]\nl[1:2:3]\nl[:]\nl[::]\nl[0][0]\nl[0,1]\nl[0:,]\nl[0:,1]\nl[0:1, 2]\nl[0:1:2, 3, i:i + 1]\nx[a := b]\na[:, :11]\nl[1,2,3]\nx[~flag]\nx[(a:=0):]\nx[(a:=0):y]\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..182, + body: [ + Expr( + StmtExpr { + range: 1..5, + value: Subscript( + ExprSubscript { + range: 1..5, + value: Name( + ExprName { + range: 1..2, + id: "l", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 3..4, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 6..11, + value: Subscript( + ExprSubscript { + range: 6..11, + value: Name( + ExprName { + range: 6..7, + id: "l", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 8..10, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 1, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..18, + value: Subscript( + ExprSubscript { + range: 12..18, + value: Name( + ExprName { + range: 12..13, + id: "l", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 14..17, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 14..15, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..24, + value: Subscript( + ExprSubscript { + range: 19..24, + value: Name( + ExprName { + range: 19..20, + id: "l", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 21..23, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 22..23, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 25..31, + value: Subscript( + ExprSubscript { + range: 25..31, + value: Name( + ExprName { + range: 25..26, + id: "l", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 27..30, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 32..39, + value: Subscript( + ExprSubscript { + range: 32..39, + value: Name( + ExprName { + range: 32..33, + id: "l", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 34..38, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 35..36, + value: Int( + 2, + ), + }, + ), + ), + step: Some( + NumberLiteral( + ExprNumberLiteral { + range: 37..38, + value: Int( + 3, + ), + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 40..48, + value: Subscript( + ExprSubscript { + range: 40..48, + value: Name( + ExprName { + range: 40..41, + id: "l", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 42..47, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 42..43, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 44..45, + value: Int( + 2, + ), + }, + ), + ), + step: Some( + NumberLiteral( + ExprNumberLiteral { + range: 46..47, + value: Int( + 3, + ), + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 49..53, + value: Subscript( + ExprSubscript { + range: 49..53, + value: Name( + ExprName { + range: 49..50, + id: "l", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 51..52, + lower: None, + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..59, + value: Subscript( + ExprSubscript { + range: 54..59, + value: Name( + ExprName { + range: 54..55, + id: "l", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 56..58, + lower: None, + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 60..67, + value: Subscript( + ExprSubscript { + range: 60..67, + value: Subscript( + ExprSubscript { + range: 60..64, + value: Name( + ExprName { + range: 60..61, + id: "l", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 62..63, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 68..74, + value: Subscript( + ExprSubscript { + range: 68..74, + value: Name( + ExprName { + range: 68..69, + id: "l", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 70..73, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 70..71, + value: Int( + 0, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 72..73, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 75..81, + value: Subscript( + ExprSubscript { + range: 75..81, + value: Name( + ExprName { + range: 75..76, + id: "l", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 77..80, + elts: [ + Slice( + ExprSlice { + range: 77..79, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 77..78, + value: Int( + 0, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 82..89, + value: Subscript( + ExprSubscript { + range: 82..89, + value: Name( + ExprName { + range: 82..83, + id: "l", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 84..88, + elts: [ + Slice( + ExprSlice { + range: 84..86, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 84..85, + value: Int( + 0, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 87..88, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 90..99, + value: Subscript( + ExprSubscript { + range: 90..99, + value: Name( + ExprName { + range: 90..91, + id: "l", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 92..98, + elts: [ + Slice( + ExprSlice { + range: 92..95, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 92..93, + value: Int( + 0, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 94..95, + value: Int( + 1, + ), + }, + ), + ), + step: None, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 97..98, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 100..120, + value: Subscript( + ExprSubscript { + range: 100..120, + value: Name( + ExprName { + range: 100..101, + id: "l", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 102..119, + elts: [ + Slice( + ExprSlice { + range: 102..107, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 102..103, + value: Int( + 0, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 104..105, + value: Int( + 1, + ), + }, + ), + ), + step: Some( + NumberLiteral( + ExprNumberLiteral { + range: 106..107, + value: Int( + 2, + ), + }, + ), + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 109..110, + value: Int( + 3, + ), + }, + ), + Slice( + ExprSlice { + range: 112..119, + lower: Some( + Name( + ExprName { + range: 112..113, + id: "i", + ctx: Load, + }, + ), + ), + upper: Some( + BinOp( + ExprBinOp { + range: 114..119, + left: Name( + ExprName { + range: 114..115, + id: "i", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 118..119, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + step: None, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 121..130, + value: Subscript( + ExprSubscript { + range: 121..130, + value: Name( + ExprName { + range: 121..122, + id: "x", + ctx: Load, + }, + ), + slice: NamedExpr( + ExprNamedExpr { + range: 123..129, + target: Name( + ExprName { + range: 123..124, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 128..129, + id: "b", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 131..140, + value: Subscript( + ExprSubscript { + range: 131..140, + value: Name( + ExprName { + range: 131..132, + id: "a", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 133..139, + elts: [ + Slice( + ExprSlice { + range: 133..134, + lower: None, + upper: None, + step: None, + }, + ), + Slice( + ExprSlice { + range: 136..139, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 137..139, + value: Int( + 11, + ), + }, + ), + ), + step: None, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 141..149, + value: Subscript( + ExprSubscript { + range: 141..149, + value: Name( + ExprName { + range: 141..142, + id: "l", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 143..148, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 143..144, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 145..146, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 147..148, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 150..158, + value: Subscript( + ExprSubscript { + range: 150..158, + value: Name( + ExprName { + range: 150..151, + id: "x", + ctx: Load, + }, + ), + slice: UnaryOp( + ExprUnaryOp { + range: 152..157, + op: Invert, + operand: Name( + ExprName { + range: 153..157, + id: "flag", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 159..169, + value: Subscript( + ExprSubscript { + range: 159..169, + value: Name( + ExprName { + range: 159..160, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 161..168, + lower: Some( + NamedExpr( + ExprNamedExpr { + range: 162..166, + target: Name( + ExprName { + range: 162..163, + id: "a", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 165..166, + value: Int( + 0, + ), + }, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 170..181, + value: Subscript( + ExprSubscript { + range: 170..181, + value: Name( + ExprName { + range: 170..171, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 172..180, + lower: Some( + NamedExpr( + ExprNamedExpr { + range: 173..177, + target: Name( + ExprName { + range: 173..174, + id: "a", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 176..177, + value: Int( + 0, + ), + }, + ), + }, + ), + ), + upper: Some( + Name( + ExprName { + range: 179..180, + id: "y", + ctx: Load, + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_try_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_try_stmt.snap new file mode 100644 index 0000000000000..bef2d0e619696 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_try_stmt.snap @@ -0,0 +1,496 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\ntry:\n ...\nexcept:\n ...\n\ntry:\n ...\nexcept Exception1 as e:\n ...\nexcept Exception2 as e:\n ...\n\ntry:\n ...\nexcept Exception as e:\n ...\nexcept:\n ...\nfinally:\n ...\n\ntry:\n ...\nexcept:\n ...\nelse:\n ...\n\ntry:\n ...\nexcept:\n ...\nelse:\n ...\nfinally:\n ...\n\ntry:\n ...\nfinally:\n ...\n\ntry:\n ...\nelse:\n ...\nfinally:\n ...\n\ntry:\n ...\nexcept* a as A:\n ...\nexcept* b:\n ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..424, + body: [ + Try( + StmtTry { + range: 1..29, + body: [ + Expr( + StmtExpr { + range: 10..13, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 10..13, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 14..29, + type_: None, + name: None, + body: [ + Expr( + StmtExpr { + range: 26..29, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 26..29, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 31..107, + body: [ + Expr( + StmtExpr { + range: 40..43, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 40..43, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 44..75, + type_: Some( + Name( + ExprName { + range: 51..61, + id: "Exception1", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 65..66, + }, + ), + body: [ + Expr( + StmtExpr { + range: 72..75, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 72..75, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 76..107, + type_: Some( + Name( + ExprName { + range: 83..93, + id: "Exception2", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 97..98, + }, + ), + body: [ + Expr( + StmtExpr { + range: 104..107, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 104..107, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 109..185, + body: [ + Expr( + StmtExpr { + range: 118..121, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 118..121, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 122..152, + type_: Some( + Name( + ExprName { + range: 129..138, + id: "Exception", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 142..143, + }, + ), + body: [ + Expr( + StmtExpr { + range: 149..152, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 149..152, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 153..168, + type_: None, + name: None, + body: [ + Expr( + StmtExpr { + range: 165..168, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 165..168, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [ + Expr( + StmtExpr { + range: 182..185, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 182..185, + }, + ), + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 187..229, + body: [ + Expr( + StmtExpr { + range: 196..199, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 196..199, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 200..215, + type_: None, + name: None, + body: [ + Expr( + StmtExpr { + range: 212..215, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 212..215, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [ + Expr( + StmtExpr { + range: 226..229, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 226..229, + }, + ), + }, + ), + ], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 231..290, + body: [ + Expr( + StmtExpr { + range: 240..243, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 240..243, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 244..259, + type_: None, + name: None, + body: [ + Expr( + StmtExpr { + range: 256..259, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 256..259, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [ + Expr( + StmtExpr { + range: 270..273, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 270..273, + }, + ), + }, + ), + ], + finalbody: [ + Expr( + StmtExpr { + range: 287..290, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 287..290, + }, + ), + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 292..321, + body: [ + Expr( + StmtExpr { + range: 301..304, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 301..304, + }, + ), + }, + ), + ], + handlers: [], + orelse: [], + finalbody: [ + Expr( + StmtExpr { + range: 318..321, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 318..321, + }, + ), + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 323..366, + body: [ + Expr( + StmtExpr { + range: 332..335, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 332..335, + }, + ), + }, + ), + ], + handlers: [], + orelse: [ + Expr( + StmtExpr { + range: 346..349, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 346..349, + }, + ), + }, + ), + ], + finalbody: [ + Expr( + StmtExpr { + range: 363..366, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 363..366, + }, + ), + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 368..423, + body: [ + Expr( + StmtExpr { + range: 377..380, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 377..380, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 381..404, + type_: Some( + Name( + ExprName { + range: 389..390, + id: "a", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "A", + range: 394..395, + }, + ), + body: [ + Expr( + StmtExpr { + range: 401..404, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 401..404, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 405..423, + type_: Some( + Name( + ExprName { + range: 413..414, + id: "b", + ctx: Load, + }, + ), + ), + name: None, + body: [ + Expr( + StmtExpr { + range: 420..423, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 420..423, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: true, + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_tuple_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_tuple_expr.snap new file mode 100644 index 0000000000000..e5e375fe11b51 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_tuple_expr.snap @@ -0,0 +1,293 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n1, 2\n1 + 2,\nx and y,\n(1, 2,)\n(1,2,3,4)\n(x + 1, l,)\n()\n1, 2, 3, 4\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..66, + body: [ + Expr( + StmtExpr { + range: 1..5, + value: Tuple( + ExprTuple { + range: 1..5, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 1..2, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 4..5, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 6..12, + value: Tuple( + ExprTuple { + range: 6..12, + elts: [ + BinOp( + ExprBinOp { + range: 6..11, + left: NumberLiteral( + ExprNumberLiteral { + range: 6..7, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..21, + value: Tuple( + ExprTuple { + range: 13..21, + elts: [ + BoolOp( + ExprBoolOp { + range: 13..20, + op: And, + values: [ + Name( + ExprName { + range: 13..14, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 19..20, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..29, + value: Tuple( + ExprTuple { + range: 22..29, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 26..27, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..39, + value: Tuple( + ExprTuple { + range: 30..39, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 31..32, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 33..34, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 35..36, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 37..38, + value: Int( + 4, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 40..51, + value: Tuple( + ExprTuple { + range: 40..51, + elts: [ + BinOp( + ExprBinOp { + range: 41..46, + left: Name( + ExprName { + range: 41..42, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 45..46, + value: Int( + 1, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 48..49, + id: "l", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 52..54, + value: Tuple( + ExprTuple { + range: 52..54, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 55..65, + value: Tuple( + ExprTuple { + range: 55..65, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 55..56, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 58..59, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 61..62, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 64..65, + value: Int( + 4, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_type_alias_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_type_alias_stmt.snap new file mode 100644 index 0000000000000..ba0ff6cefef67 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_type_alias_stmt.snap @@ -0,0 +1,392 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\ntype Point = tuple[float, float]\ntype Point[T] = tuple[T, T]\ntype IntFunc[**P] = Callable[P, int] # ParamSpec\ntype LabeledTuple[*Ts] = tuple[str, *Ts] # TypeVarTuple\ntype HashableSequence[T: Hashable] = Sequence[T] # TypeVar with bound\ntype IntOrStrSequence[T: (int, str)] = Sequence[T] # TypeVar with constraints\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..319, + body: [ + TypeAlias( + StmtTypeAlias { + range: 1..33, + name: Name( + ExprName { + range: 6..11, + id: "Point", + ctx: Store, + }, + ), + type_params: None, + value: Subscript( + ExprSubscript { + range: 14..33, + value: Name( + ExprName { + range: 14..19, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 20..32, + elts: [ + Name( + ExprName { + range: 20..25, + id: "float", + ctx: Load, + }, + ), + Name( + ExprName { + range: 27..32, + id: "float", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 34..61, + name: Name( + ExprName { + range: 39..44, + id: "Point", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 44..47, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 45..46, + name: Identifier { + id: "T", + range: 45..46, + }, + bound: None, + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 50..61, + value: Name( + ExprName { + range: 50..55, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 56..60, + elts: [ + Name( + ExprName { + range: 56..57, + id: "T", + ctx: Load, + }, + ), + Name( + ExprName { + range: 59..60, + id: "T", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 62..98, + name: Name( + ExprName { + range: 67..74, + id: "IntFunc", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 74..79, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 75..78, + name: Identifier { + id: "P", + range: 77..78, + }, + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 82..98, + value: Name( + ExprName { + range: 82..90, + id: "Callable", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 91..97, + elts: [ + Name( + ExprName { + range: 91..92, + id: "P", + ctx: Load, + }, + ), + Name( + ExprName { + range: 94..97, + id: "int", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 112..152, + name: Name( + ExprName { + range: 117..129, + id: "LabeledTuple", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 129..134, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 130..133, + name: Identifier { + id: "Ts", + range: 131..133, + }, + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 137..152, + value: Name( + ExprName { + range: 137..142, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 143..151, + elts: [ + Name( + ExprName { + range: 143..146, + id: "str", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 148..151, + value: Name( + ExprName { + range: 149..151, + id: "Ts", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 169..217, + name: Name( + ExprName { + range: 174..190, + id: "HashableSequence", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 190..203, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 191..202, + name: Identifier { + id: "T", + range: 191..192, + }, + bound: Some( + Name( + ExprName { + range: 194..202, + id: "Hashable", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 206..217, + value: Name( + ExprName { + range: 206..214, + id: "Sequence", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 215..216, + id: "T", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 240..290, + name: Name( + ExprName { + range: 245..261, + id: "IntOrStrSequence", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 261..276, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 262..275, + name: Identifier { + id: "T", + range: 262..263, + }, + bound: Some( + Tuple( + ExprTuple { + range: 265..275, + elts: [ + Name( + ExprName { + range: 266..269, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 271..274, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 279..290, + value: Name( + ExprName { + range: 279..287, + id: "Sequence", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 288..289, + id: "T", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_type_params.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_type_params.snap new file mode 100644 index 0000000000000..d95d6371df932 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_type_params.snap @@ -0,0 +1,145 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\ndef max[T](args: Iterable[T]) -> T:\n ...\nclass list[T]:\n ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..68, + body: [ + FunctionDef( + StmtFunctionDef { + range: 1..44, + is_async: false, + decorator_list: [], + name: Identifier { + id: "max", + range: 5..8, + }, + type_params: Some( + TypeParams { + range: 8..11, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 9..10, + name: Identifier { + id: "T", + range: 9..10, + }, + bound: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 11..30, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 12..29, + parameter: Parameter { + range: 12..29, + name: Identifier { + id: "args", + range: 12..16, + }, + annotation: Some( + Subscript( + ExprSubscript { + range: 18..29, + value: Name( + ExprName { + range: 18..26, + id: "Iterable", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 27..28, + id: "T", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 34..35, + id: "T", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 41..44, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 41..44, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 45..67, + decorator_list: [], + name: Identifier { + id: "list", + range: 51..55, + }, + type_params: Some( + TypeParams { + range: 55..58, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 56..57, + name: Identifier { + id: "T", + range: 56..57, + }, + bound: None, + }, + ), + ], + }, + ), + arguments: None, + body: [ + Expr( + StmtExpr { + range: 64..67, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 64..67, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_unary_exprs.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_unary_exprs.snap new file mode 100644 index 0000000000000..c75be192ef98a --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_unary_exprs.snap @@ -0,0 +1,153 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\n-1\n+1\n~1\n-1 + 2\n---1\nnot x\n \")" +--- +Program { + ast: Module( + ModModule { + range: 0..28, + body: [ + Expr( + StmtExpr { + range: 1..3, + value: UnaryOp( + ExprUnaryOp { + range: 1..3, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 2..3, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 4..6, + value: UnaryOp( + ExprUnaryOp { + range: 4..6, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 7..9, + value: UnaryOp( + ExprUnaryOp { + range: 7..9, + op: Invert, + operand: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..16, + value: BinOp( + ExprBinOp { + range: 10..16, + left: UnaryOp( + ExprUnaryOp { + range: 10..12, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 1, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 17..21, + value: UnaryOp( + ExprUnaryOp { + range: 17..21, + op: USub, + operand: UnaryOp( + ExprUnaryOp { + range: 18..21, + op: USub, + operand: UnaryOp( + ExprUnaryOp { + range: 19..21, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..27, + value: UnaryOp( + ExprUnaryOp { + range: 22..27, + op: Not, + operand: Name( + ExprName { + range: 26..27, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_while_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_while_stmt.snap new file mode 100644 index 0000000000000..84993da4222af --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_while_stmt.snap @@ -0,0 +1,265 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nwhile x:\n ...\nwhile (x > 1) and y:\n pass\nelse:\n ...\nwhile x and y:\n ...\n print('Hello World!')\n\nelse:\n print('Olá, Mundo!')\n ...\nwhile a := b: ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..170, + body: [ + While( + StmtWhile { + range: 1..17, + test: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 14..17, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 14..17, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + While( + StmtWhile { + range: 18..61, + test: BoolOp( + ExprBoolOp { + range: 24..37, + op: And, + values: [ + Compare( + ExprCompare { + range: 25..30, + left: Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 29..30, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + Name( + ExprName { + range: 36..37, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Pass( + StmtPass { + range: 43..47, + }, + ), + ], + orelse: [ + Expr( + StmtExpr { + range: 58..61, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 58..61, + }, + ), + }, + ), + ], + }, + ), + While( + StmtWhile { + range: 62..151, + test: BoolOp( + ExprBoolOp { + range: 68..75, + op: And, + values: [ + Name( + ExprName { + range: 68..69, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 74..75, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 81..84, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 81..84, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 89..110, + value: Call( + ExprCall { + range: 89..110, + func: Name( + ExprName { + range: 89..94, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 94..110, + args: [ + StringLiteral( + ExprStringLiteral { + range: 95..109, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 95..109, + value: "Hello World!", + unicode: false, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + orelse: [ + Expr( + StmtExpr { + range: 122..143, + value: Call( + ExprCall { + range: 122..143, + func: Name( + ExprName { + range: 122..127, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 127..143, + args: [ + StringLiteral( + ExprStringLiteral { + range: 128..142, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 128..142, + value: "Olá, Mundo!", + unicode: false, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 148..151, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 148..151, + }, + ), + }, + ), + ], + }, + ), + While( + StmtWhile { + range: 152..169, + test: NamedExpr( + ExprNamedExpr { + range: 158..164, + target: Name( + ExprName { + range: 158..159, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 163..164, + id: "b", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 166..169, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 166..169, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_with_stmt.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_with_stmt.snap new file mode 100644 index 0000000000000..289f2c7aac288 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_with_stmt.snap @@ -0,0 +1,827 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nwith x:\n ...\nwith x, y:\n ...\nwith open() as f:\n ...\nwith f() as x.attr:\n pass\nwith x as X, y as Y, z as Z:\n ...\nwith (x, z as Y, y,):\n ...\nwith (a) as f:\n ...\nwith ((a) as f, 1):\n ...\nwith a:\n yield a, b\nwith (yield 1):\n ...\nwith (yield from 1):\n ...\nwith (a := 1):\n ...\nwith (open('bla.txt')), (open('bla.txt')):\n pass\nwith (a := 1, x):\n ...\nwith (p / 'new_file').open('wb'): ...\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..424, + body: [ + With( + StmtWith { + range: 1..16, + is_async: false, + items: [ + WithItem { + range: 6..7, + context_expr: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 13..16, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 13..16, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 17..35, + is_async: false, + items: [ + WithItem { + range: 22..23, + context_expr: Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 25..26, + context_expr: Name( + ExprName { + range: 25..26, + id: "y", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 32..35, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 32..35, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 36..61, + is_async: false, + items: [ + WithItem { + range: 41..52, + context_expr: Call( + ExprCall { + range: 41..47, + func: Name( + ExprName { + range: 41..45, + id: "open", + ctx: Load, + }, + ), + arguments: Arguments { + range: 45..47, + args: [], + keywords: [], + }, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 51..52, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 58..61, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 58..61, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 62..90, + is_async: false, + items: [ + WithItem { + range: 67..80, + context_expr: Call( + ExprCall { + range: 67..70, + func: Name( + ExprName { + range: 67..68, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 68..70, + args: [], + keywords: [], + }, + }, + ), + optional_vars: Some( + Attribute( + ExprAttribute { + range: 74..80, + value: Name( + ExprName { + range: 74..75, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 76..80, + }, + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Pass( + StmtPass { + range: 86..90, + }, + ), + ], + }, + ), + With( + StmtWith { + range: 91..127, + is_async: false, + items: [ + WithItem { + range: 96..102, + context_expr: Name( + ExprName { + range: 96..97, + id: "x", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 101..102, + id: "X", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 104..110, + context_expr: Name( + ExprName { + range: 104..105, + id: "y", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 109..110, + id: "Y", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 112..118, + context_expr: Name( + ExprName { + range: 112..113, + id: "z", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 117..118, + id: "Z", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 124..127, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 124..127, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 128..157, + is_async: false, + items: [ + WithItem { + range: 134..135, + context_expr: Name( + ExprName { + range: 134..135, + id: "x", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 137..143, + context_expr: Name( + ExprName { + range: 137..138, + id: "z", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 142..143, + id: "Y", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 145..146, + context_expr: Name( + ExprName { + range: 145..146, + id: "y", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 154..157, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 154..157, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 158..180, + is_async: false, + items: [ + WithItem { + range: 163..171, + context_expr: Name( + ExprName { + range: 164..165, + id: "a", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 170..171, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 177..180, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 177..180, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 181..208, + is_async: false, + items: [ + WithItem { + range: 187..195, + context_expr: Name( + ExprName { + range: 188..189, + id: "a", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 194..195, + id: "f", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 197..198, + context_expr: NumberLiteral( + ExprNumberLiteral { + range: 197..198, + value: Int( + 1, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 205..208, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 205..208, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 209..231, + is_async: false, + items: [ + WithItem { + range: 214..215, + context_expr: Name( + ExprName { + range: 214..215, + id: "a", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 221..231, + value: Yield( + ExprYield { + range: 221..231, + value: Some( + Tuple( + ExprTuple { + range: 227..231, + elts: [ + Name( + ExprName { + range: 227..228, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 230..231, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 232..255, + is_async: false, + items: [ + WithItem { + range: 238..245, + context_expr: Yield( + ExprYield { + range: 238..245, + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 244..245, + value: Int( + 1, + ), + }, + ), + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 252..255, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 252..255, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 256..284, + is_async: false, + items: [ + WithItem { + range: 262..274, + context_expr: YieldFrom( + ExprYieldFrom { + range: 262..274, + value: NumberLiteral( + ExprNumberLiteral { + range: 273..274, + value: Int( + 1, + ), + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 281..284, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 281..284, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 285..307, + is_async: false, + items: [ + WithItem { + range: 291..297, + context_expr: NamedExpr( + ExprNamedExpr { + range: 291..297, + target: Name( + ExprName { + range: 291..292, + id: "a", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 296..297, + value: Int( + 1, + ), + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 304..307, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 304..307, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 308..359, + is_async: false, + items: [ + WithItem { + range: 313..330, + context_expr: Call( + ExprCall { + range: 314..329, + func: Name( + ExprName { + range: 314..318, + id: "open", + ctx: Load, + }, + ), + arguments: Arguments { + range: 318..329, + args: [ + StringLiteral( + ExprStringLiteral { + range: 319..328, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 319..328, + value: "bla.txt", + unicode: false, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + WithItem { + range: 332..349, + context_expr: Call( + ExprCall { + range: 333..348, + func: Name( + ExprName { + range: 333..337, + id: "open", + ctx: Load, + }, + ), + arguments: Arguments { + range: 337..348, + args: [ + StringLiteral( + ExprStringLiteral { + range: 338..347, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 338..347, + value: "bla.txt", + unicode: false, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Pass( + StmtPass { + range: 355..359, + }, + ), + ], + }, + ), + With( + StmtWith { + range: 360..385, + is_async: false, + items: [ + WithItem { + range: 365..376, + context_expr: Tuple( + ExprTuple { + range: 365..376, + elts: [ + NamedExpr( + ExprNamedExpr { + range: 366..372, + target: Name( + ExprName { + range: 366..367, + id: "a", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 371..372, + value: Int( + 1, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 374..375, + id: "x", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 382..385, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 382..385, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 386..423, + is_async: false, + items: [ + WithItem { + range: 391..418, + context_expr: Call( + ExprCall { + range: 391..418, + func: Attribute( + ExprAttribute { + range: 391..412, + value: BinOp( + ExprBinOp { + range: 392..406, + left: Name( + ExprName { + range: 392..393, + id: "p", + ctx: Load, + }, + ), + op: Div, + right: StringLiteral( + ExprStringLiteral { + range: 396..406, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 396..406, + value: "new_file", + unicode: false, + }, + ), + }, + }, + ), + }, + ), + attr: Identifier { + id: "open", + range: 408..412, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 412..418, + args: [ + StringLiteral( + ExprStringLiteral { + range: 413..417, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 413..417, + value: "wb", + unicode: false, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 420..423, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 420..423, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_yield_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_yield_expr.snap new file mode 100644 index 0000000000000..4226507809a9f --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_yield_expr.snap @@ -0,0 +1,375 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nyield *y\nyield x\nyield x + 1\nyield a and b\nyield f()\nyield [1, 2]\nyield {3, 4}\nyield {i: 5}\nyield 7, 8\nyield (9, 10)\nyield 1 == 1\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..131, + body: [ + Expr( + StmtExpr { + range: 1..9, + value: Yield( + ExprYield { + range: 1..9, + value: Some( + Starred( + ExprStarred { + range: 7..9, + value: Name( + ExprName { + range: 8..9, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..17, + value: Yield( + ExprYield { + range: 10..17, + value: Some( + Name( + ExprName { + range: 16..17, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..29, + value: Yield( + ExprYield { + range: 18..29, + value: Some( + BinOp( + ExprBinOp { + range: 24..29, + left: Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..43, + value: Yield( + ExprYield { + range: 30..43, + value: Some( + BoolOp( + ExprBoolOp { + range: 36..43, + op: And, + values: [ + Name( + ExprName { + range: 36..37, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 42..43, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 44..53, + value: Yield( + ExprYield { + range: 44..53, + value: Some( + Call( + ExprCall { + range: 50..53, + func: Name( + ExprName { + range: 50..51, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 51..53, + args: [], + keywords: [], + }, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..66, + value: Yield( + ExprYield { + range: 54..66, + value: Some( + List( + ExprList { + range: 60..66, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 61..62, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 64..65, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 67..79, + value: Yield( + ExprYield { + range: 67..79, + value: Some( + Set( + ExprSet { + range: 73..79, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 74..75, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 77..78, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 80..92, + value: Yield( + ExprYield { + range: 80..92, + value: Some( + Dict( + ExprDict { + range: 86..92, + keys: [ + Some( + Name( + ExprName { + range: 87..88, + id: "i", + ctx: Load, + }, + ), + ), + ], + values: [ + NumberLiteral( + ExprNumberLiteral { + range: 90..91, + value: Int( + 5, + ), + }, + ), + ], + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 93..103, + value: Yield( + ExprYield { + range: 93..103, + value: Some( + Tuple( + ExprTuple { + range: 99..103, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 99..100, + value: Int( + 7, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 102..103, + value: Int( + 8, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 104..117, + value: Yield( + ExprYield { + range: 104..117, + value: Some( + Tuple( + ExprTuple { + range: 110..117, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 111..112, + value: Int( + 9, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 114..116, + value: Int( + 10, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 118..130, + value: Yield( + ExprYield { + range: 118..130, + value: Some( + Compare( + ExprCompare { + range: 124..130, + left: NumberLiteral( + ExprNumberLiteral { + range: 124..125, + value: Int( + 1, + ), + }, + ), + ops: [ + Eq, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 129..130, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + ), + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_yield_from_expr.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_yield_from_expr.snap new file mode 100644 index 0000000000000..04a2e81451167 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__parser__tests__parse_yield_from_expr.snap @@ -0,0 +1,295 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/parser.rs +expression: "parse(\"\nyield from x\nyield from x + 1\nyield from a and b\nyield from f()\nyield from [1, 2]\nyield from {3, 4}\nyield from {i: 5}\nyield from (9, 10)\nyield from 1 == 1\n\")" +--- +Program { + ast: Module( + ModModule { + range: 0..156, + body: [ + Expr( + StmtExpr { + range: 1..13, + value: YieldFrom( + ExprYieldFrom { + range: 1..13, + value: Name( + ExprName { + range: 12..13, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 14..30, + value: YieldFrom( + ExprYieldFrom { + range: 14..30, + value: BinOp( + ExprBinOp { + range: 25..30, + left: Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 29..30, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 31..49, + value: YieldFrom( + ExprYieldFrom { + range: 31..49, + value: BoolOp( + ExprBoolOp { + range: 42..49, + op: And, + values: [ + Name( + ExprName { + range: 42..43, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 48..49, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 50..64, + value: YieldFrom( + ExprYieldFrom { + range: 50..64, + value: Call( + ExprCall { + range: 61..64, + func: Name( + ExprName { + range: 61..62, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 62..64, + args: [], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 65..82, + value: YieldFrom( + ExprYieldFrom { + range: 65..82, + value: List( + ExprList { + range: 76..82, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 77..78, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 80..81, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 83..100, + value: YieldFrom( + ExprYieldFrom { + range: 83..100, + value: Set( + ExprSet { + range: 94..100, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 95..96, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 98..99, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 101..118, + value: YieldFrom( + ExprYieldFrom { + range: 101..118, + value: Dict( + ExprDict { + range: 112..118, + keys: [ + Some( + Name( + ExprName { + range: 113..114, + id: "i", + ctx: Load, + }, + ), + ), + ], + values: [ + NumberLiteral( + ExprNumberLiteral { + range: 116..117, + value: Int( + 5, + ), + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 119..137, + value: YieldFrom( + ExprYieldFrom { + range: 119..137, + value: Tuple( + ExprTuple { + range: 130..137, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 131..132, + value: Int( + 9, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 134..136, + value: Int( + 10, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 138..155, + value: YieldFrom( + ExprYieldFrom { + range: 138..155, + value: Compare( + ExprCompare { + range: 149..155, + left: NumberLiteral( + ExprNumberLiteral { + range: 149..150, + value: Int( + 1, + ), + }, + ), + ops: [ + Eq, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 154..155, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + }, + ), + }, + ), + ], + }, + ), + parse_errors: [], +} diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__decorator_ranges.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__decorator_ranges.snap similarity index 96% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__decorator_ranges.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__decorator_ranges.snap index 160699606e8c5..14225bbab63c7 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__decorator_ranges.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__decorator_ranges.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__dict_unpacking.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__dict_unpacking.snap similarity index 97% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__dict_unpacking.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__dict_unpacking.snap index cb0da9427c71a..32ca496decb04 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__dict_unpacking.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__dict_unpacking.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- Dict( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__fstrings.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__fstrings.snap index a46c03a3d9bc2..444952053e37c 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__fstrings.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ @@ -111,6 +111,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), debug_text: None, @@ -501,6 +502,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), debug_text: Some( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings_with_unicode.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__fstrings_with_unicode.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings_with_unicode.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__fstrings_with_unicode.snap index 705c6e4d47155..28bf18c81cae3 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings_with_unicode.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__fstrings_with_unicode.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__generator_expression_argument.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__generator_expression_argument.snap similarity index 98% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__generator_expression_argument.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__generator_expression_argument.snap index 08f16c5d09f78..14cb7e4a175bf 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__generator_expression_argument.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__generator_expression_argument.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- Call( @@ -146,6 +146,7 @@ Call( ), ], ctx: Load, + parenthesized: true, }, ), ifs: [], diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__ipython_escape_commands.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__ipython_escape_commands.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__ipython_escape_commands.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__ipython_escape_commands.snap index 1ec5ddd1fdee6..c481580a019f7 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__ipython_escape_commands.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__ipython_escape_commands.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- Module( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__match.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__match.snap index 545c9396f93b0..2132cd5b050f0 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__match.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ @@ -469,6 +469,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), cases: [ @@ -521,6 +522,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), cases: [ @@ -573,6 +575,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), cases: [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match_as_identifier.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__match_as_identifier.snap similarity index 98% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match_as_identifier.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__match_as_identifier.snap index 6591b7c9d0baa..7fef878e52376 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match_as_identifier.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__match_as_identifier.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_suite(source).unwrap() --- [ Expr( @@ -52,6 +52,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: false, }, ), }, @@ -105,6 +106,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: false, }, ), }, @@ -385,6 +387,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 298..300, elts: [], ctx: Load, + parenthesized: true, }, ), ], @@ -425,6 +428,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 329..331, elts: [], ctx: Load, + parenthesized: true, }, ), ], @@ -505,6 +509,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: false, }, ), ctx: Load, @@ -548,6 +553,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), ctx: Load, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__named_expression.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__named_expression.snap similarity index 92% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__named_expression.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__named_expression.snap index 18aef224edf66..c17dcd5c44f11 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__named_expression.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__named_expression.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- NamedExpr( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__numeric_literals.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__numeric_literals.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__numeric_literals.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__numeric_literals.snap index c43a803b8992e..82f0f4a80a9f5 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__numeric_literals.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__numeric_literals.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: "parse_suite(source, \"\").unwrap()" --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__numeric_literals_attribute_access.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__numeric_literals_attribute_access.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__numeric_literals_attribute_access.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__numeric_literals_attribute_access.snap index 79d63ba4915b1..ab53e4ffd2392 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__numeric_literals_attribute_access.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__numeric_literals_attribute_access.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: "parse_suite(source, \"\").unwrap()" --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parenthesized_with_statement.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parenthesized_with_statement.snap index 0e703191724d8..28005144524ea 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parenthesized_with_statement.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_suite(source).unwrap() --- [ With( @@ -171,6 +171,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: Some( @@ -220,6 +221,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: Some( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_bool_op_and.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_bool_op_and.snap similarity index 88% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_bool_op_and.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_bool_op_and.snap index 66a11f2ded910..57fb1878716d4 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_bool_op_and.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_bool_op_and.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- BoolOp( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_bool_op_or.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_bool_op_or.snap similarity index 88% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_bool_op_or.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_bool_op_or.snap index 9a7059d242bf3..a5cb8fdff51a8 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_bool_op_or.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_bool_op_or.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- BoolOp( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_class.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_class.snap index 28c1d74b4c8f9..c3fb45e4c5b17 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_class.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: "parse_suite(source, \"\").unwrap()" --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class_generic_types.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_class_generic_types.snap similarity index 98% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class_generic_types.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_class_generic_types.snap index 8d868ecef23a3..95c36755589f3 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class_generic_types.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_class_generic_types.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_suite(source).unwrap() --- [ ClassDef( @@ -143,6 +143,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), ), diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_dict_comprehension.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_dict_comprehension.snap similarity index 94% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_dict_comprehension.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_dict_comprehension.snap index ca8d83bdb3ab2..6ce834acba582 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_dict_comprehension.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_dict_comprehension.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- DictComp( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_double_list_comprehension.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_double_list_comprehension.snap similarity index 97% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_double_list_comprehension.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_double_list_comprehension.snap index 0440c2f331309..c37665e374386 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_double_list_comprehension.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_double_list_comprehension.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- ListComp( @@ -35,6 +35,7 @@ ListComp( ), ], ctx: Store, + parenthesized: false, }, ), iter: Name( diff --git a/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_empty.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_empty.snap new file mode 100644 index 0000000000000..2081c5a6d73d7 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_empty.snap @@ -0,0 +1,5 @@ +--- +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_ast +--- +[] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_f_string.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_f_string.snap similarity index 94% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_f_string.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_f_string.snap index 84364a344ec01..c865d4ce61800 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_f_string.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_f_string.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_function_definition.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_function_definition.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_function_definition.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_function_definition.snap index c673e95d9658c..cd28ee81fc76c 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_function_definition.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_function_definition.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_suite(source).unwrap() --- [ FunctionDef( @@ -254,6 +254,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), ), diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_generator_comprehension.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_generator_comprehension.snap similarity index 93% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_generator_comprehension.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_generator_comprehension.snap index 134f99c688585..8332340eea2f7 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_generator_comprehension.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_generator_comprehension.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- GeneratorExp( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_if_elif_else.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_if_elif_else.snap similarity index 97% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_if_elif_else.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_if_elif_else.snap index 9f9e87a388c8c..20ad1689cc3e9 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_if_elif_else.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_if_elif_else.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_if_else_generator_comprehension.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_if_else_generator_comprehension.snap similarity index 95% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_if_else_generator_comprehension.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_if_else_generator_comprehension.snap index 0ef98fab3f6b1..dc0764fcd13b2 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_if_else_generator_comprehension.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_if_else_generator_comprehension.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- GeneratorExp( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_kwargs.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_kwargs.snap similarity index 97% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_kwargs.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_kwargs.snap index 1a66146c80fb2..fffccedf50938 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_kwargs.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_kwargs.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_lambda.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_lambda.snap similarity index 97% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_lambda.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_lambda.snap index 4401fd6c8428a..786865fa9d0dc 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_lambda.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_lambda.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_lambda_no_args.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_lambda_no_args.snap similarity index 90% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_lambda_no_args.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_lambda_no_args.snap index a24210739c977..afc805411ef2f 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_lambda_no_args.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_lambda_no_args.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_list_comprehension.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_list_comprehension.snap similarity index 93% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_list_comprehension.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_list_comprehension.snap index 869bc11b0d794..77a7aa89a6a85 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_list_comprehension.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_list_comprehension.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- ListComp( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_named_expression_generator_comprehension.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_named_expression_generator_comprehension.snap similarity index 96% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_named_expression_generator_comprehension.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_named_expression_generator_comprehension.snap index 00cb053f1a019..fe0b9f4054572 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_named_expression_generator_comprehension.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_named_expression_generator_comprehension.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- GeneratorExp( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_print_2.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_print_2.snap similarity index 96% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_print_2.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_print_2.snap index 63fe9f3c0a6fe..90be7954a6fc2 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_print_2.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_print_2.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_print_hello.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_print_hello.snap similarity index 95% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_print_hello.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_print_hello.snap index 351f287b92ba7..26c8009b2e895 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_print_hello.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_print_hello.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_string.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_string.snap similarity index 90% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_string.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_string.snap index 8611f5f2fa6fe..115266ff3d822 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_string.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_string.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_tuples.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_tuples.snap similarity index 90% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_tuples.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_tuples.snap index 3dd0a471b213c..97d029d3e5a0f 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_tuples.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_tuples.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_suite(source).unwrap() --- [ Assign( @@ -27,6 +27,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Store, + parenthesized: false, }, ), ], @@ -52,6 +53,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: false, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_type_declaration.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_type_declaration.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_type_declaration.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_type_declaration.snap index 7730469ff75ef..703186cd6662b 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_type_declaration.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__parse_type_declaration.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_suite(source).unwrap() --- [ TypeAlias( @@ -354,6 +354,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), }, @@ -438,6 +439,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), }, @@ -484,6 +486,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), ), @@ -537,6 +540,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__patma.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__patma.snap index c90e9f1e65a04..6263dc4276f3b 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__patma.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ @@ -1659,6 +1659,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: true, }, ), cases: [ @@ -2148,6 +2149,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: true, }, ), cases: [ @@ -3287,6 +3289,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: true, }, ), cases: [ @@ -3385,6 +3388,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: true, }, ), cases: [ @@ -3466,6 +3470,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), cases: [ @@ -3542,6 +3547,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), cases: [ @@ -3635,6 +3641,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), cases: [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__slice.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__slice.snap similarity index 95% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__slice.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__slice.snap index 3113d14036689..159aca0b469f6 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__slice.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__slice.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- Subscript( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__star_index.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__star_index.snap similarity index 97% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__star_index.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__star_index.snap index 3cd99121dedd8..f00a0c077722d 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__star_index.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__star_index.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ @@ -66,6 +66,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), ctx: Load, @@ -128,6 +129,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), ctx: Store, @@ -188,6 +190,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), ctx: Load, @@ -253,6 +256,7 @@ expression: parse_ast ), ], ctx: Load, + parenthesized: false, }, ), ctx: Load, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__try.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__try.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__try.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__try.snap index f2497947cac46..19a5913026a31 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__try.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__try.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__try_star.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__try_star.snap similarity index 99% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__try_star.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__try_star.snap index 220516c86bffd..55dca254cb35e 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__try_star.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__try_star.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__type_as_identifier.snap similarity index 98% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__type_as_identifier.snap index 0296e7ad0511e..8240061f0d1ae 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__type_as_identifier.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_suite(source).unwrap() --- [ Expr( @@ -52,6 +52,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: false, }, ), }, @@ -105,6 +106,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: false, }, ), }, @@ -385,6 +387,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 283..285, elts: [], ctx: Load, + parenthesized: true, }, ), ], @@ -425,6 +428,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 312..314, elts: [], ctx: Load, + parenthesized: true, }, ), ], @@ -505,6 +509,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: false, }, ), ctx: Load, @@ -548,6 +553,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), ctx: Load, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__unicode_aliases.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__unicode_aliases.snap similarity index 93% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__unicode_aliases.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__unicode_aliases.snap index 7d811685fb945..8539e4175aee8 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__unicode_aliases.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__unicode_aliases.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__variadic_generics.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__variadic_generics.snap similarity index 97% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__variadic_generics.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__variadic_generics.snap index 82ef9d16c5fec..7543d741ddc60 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__variadic_generics.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__variadic_generics.snap @@ -1,5 +1,5 @@ --- -source: crates/ruff_python_parser/src/parser.rs +source: crates/ruff_python_parser/src/parser/tests/suite.rs expression: parse_ast --- [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__with_statement.snap similarity index 98% rename from crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap rename to crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__with_statement.snap index dec15e10e878d..a8e8bc71bcd0a 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap +++ b/crates/ruff_python_parser/src/parser/tests/snapshots/ruff_python_parser__parser__tests__suite__tests__with_statement.snap @@ -1,6 +1,6 @@ --- -source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +source: crates/ruff_python_parser/src/parser/tests/suite.rs +expression: parse_suite(source).unwrap() --- [ With( @@ -275,6 +275,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 133..135, elts: [], ctx: Load, + parenthesized: true, }, ), optional_vars: None, @@ -301,6 +302,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 147..149, elts: [], ctx: Load, + parenthesized: true, }, ), optional_vars: Some( @@ -433,6 +435,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: Some( @@ -523,6 +526,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: Some( @@ -571,6 +575,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: None, @@ -611,6 +616,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: Some( @@ -667,6 +673,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: None, @@ -715,6 +722,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: Some( @@ -876,6 +884,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: None, @@ -943,6 +952,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + parenthesized: true, }, ), optional_vars: Some( diff --git a/crates/ruff_python_parser/src/parser.rs b/crates/ruff_python_parser/src/parser/tests/suite.rs similarity index 52% rename from crates/ruff_python_parser/src/parser.rs rename to crates/ruff_python_parser/src/parser/tests/suite.rs index bc530a0f7d0db..381524aee7ec8 100644 --- a/crates/ruff_python_parser/src/parser.rs +++ b/crates/ruff_python_parser/src/parser/tests/suite.rs @@ -1,576 +1,8 @@ -//! Contains the interface to the Python `ruff_python_parser`. -//! -//! Functions in this module can be used to parse Python code into an [Abstract Syntax Tree] -//! (AST) that is then transformed into bytecode. -//! -//! There are three ways to parse Python code corresponding to the different [`Mode`]s -//! defined in the [`mode`] module. -//! -//! All functions return a [`Result`](std::result::Result) containing the parsed AST or -//! a [`ParseError`] if parsing failed. -//! -//! [Abstract Syntax Tree]: https://en.wikipedia.org/wiki/Abstract_syntax_tree -//! [`Mode`]: crate::mode - -use itertools::Itertools; -pub(super) use lalrpop_util::ParseError as LalrpopError; - -use ruff_python_ast::{ - Expr, ExprAttribute, ExprAwait, ExprBinOp, ExprBoolOp, ExprBooleanLiteral, ExprBytesLiteral, - ExprCall, ExprCompare, ExprDict, ExprDictComp, ExprEllipsisLiteral, ExprFString, - ExprGeneratorExp, ExprIfExp, ExprIpyEscapeCommand, ExprLambda, ExprList, ExprListComp, - ExprName, ExprNamedExpr, ExprNoneLiteral, ExprNumberLiteral, ExprSet, ExprSetComp, ExprSlice, - ExprStarred, ExprStringLiteral, ExprSubscript, ExprTuple, ExprUnaryOp, ExprYield, - ExprYieldFrom, Mod, ModModule, Suite, -}; -use ruff_text_size::{Ranged, TextRange, TextSize}; - -use crate::lexer::{lex, lex_starts_at, LexResult}; -use crate::token_source::TokenSource; -use crate::{ - lexer::{self, LexicalError, LexicalErrorType}, - python, - token::Tok, - tokenize_all, Mode, -}; - -/// Parse a full Python program usually consisting of multiple lines. -/// -/// This is a convenience function that can be used to parse a full Python program without having to -/// specify the [`Mode`] or the location. It is probably what you want to use most of the time. -/// -/// # Example -/// -/// For example, parsing a simple function definition and a call to that function: -/// -/// ``` -/// use ruff_python_parser as parser; -/// let source = r#" -/// def foo(): -/// return 42 -/// -/// print(foo()) -/// "#; -/// let program = parser::parse_program(source); -/// assert!(program.is_ok()); -/// ``` -pub fn parse_program(source: &str) -> Result { - match parse_tokens(tokenize_all(source, Mode::Module), source, Mode::Module)? { - Mod::Module(m) => Ok(m), - Mod::Expression(_) => unreachable!("Mode::Module doesn't return other variant"), - } -} - -pub fn parse_suite(source: &str) -> Result { - parse_program(source).map(|m| m.body) -} - -/// Parses a single Python expression. -/// -/// This convenience function can be used to parse a single expression without having to -/// specify the Mode or the location. -/// -/// # Example -/// -/// For example, parsing a single expression denoting the addition of two numbers: -/// -/// ``` -/// use ruff_python_parser as parser; -/// let expr = parser::parse_expression("1 + 2"); -/// -/// assert!(expr.is_ok()); -/// -/// ``` -pub fn parse_expression(source: &str) -> Result { - let lexer = lex(source, Mode::Expression); - match parse_tokens(lexer.collect(), source, Mode::Expression)? { - Mod::Expression(expression) => Ok(*expression.body), - Mod::Module(_m) => unreachable!("Mode::Expression doesn't return other variant"), - } -} - -/// Parses a Python expression from a given location. -/// -/// This function allows to specify the location of the expression in the source code, other than -/// that, it behaves exactly like [`parse_expression`]. -/// -/// # Example -/// -/// Parsing a single expression denoting the addition of two numbers, but this time specifying a different, -/// somewhat silly, location: -/// -/// ``` -/// use ruff_python_parser::{parse_expression_starts_at}; -/// # use ruff_text_size::TextSize; -/// -/// let expr = parse_expression_starts_at("1 + 2", TextSize::from(400)); -/// assert!(expr.is_ok()); -/// ``` -pub fn parse_expression_starts_at(source: &str, offset: TextSize) -> Result { - let lexer = lex_starts_at(source, Mode::Module, offset); - match parse_tokens(lexer.collect(), source, Mode::Expression)? { - Mod::Expression(expression) => Ok(*expression.body), - Mod::Module(_m) => unreachable!("Mode::Expression doesn't return other variant"), - } -} - -/// Parse the given Python source code using the specified [`Mode`]. -/// -/// This function is the most general function to parse Python code. Based on the [`Mode`] supplied, -/// it can be used to parse a single expression, a full Python program, an interactive expression -/// or a Python program containing IPython escape commands. -/// -/// # Example -/// -/// If we want to parse a simple expression, we can use the [`Mode::Expression`] mode during -/// parsing: -/// -/// ``` -/// use ruff_python_parser::{Mode, parse}; -/// -/// let expr = parse("1 + 2", Mode::Expression); -/// assert!(expr.is_ok()); -/// ``` -/// -/// Alternatively, we can parse a full Python program consisting of multiple lines: -/// -/// ``` -/// use ruff_python_parser::{Mode, parse}; -/// -/// let source = r#" -/// class Greeter: -/// -/// def greet(self): -/// print("Hello, world!") -/// "#; -/// let program = parse(source, Mode::Module); -/// assert!(program.is_ok()); -/// ``` -/// -/// Additionally, we can parse a Python program containing IPython escapes: -/// -/// ``` -/// use ruff_python_parser::{Mode, parse}; -/// -/// let source = r#" -/// %timeit 1 + 2 -/// ?str.replace -/// !ls -/// "#; -/// let program = parse(source, Mode::Ipython); -/// assert!(program.is_ok()); -/// ``` -pub fn parse(source: &str, mode: Mode) -> Result { - parse_starts_at(source, mode, TextSize::default()) -} - -/// Parse the given Python source code using the specified [`Mode`] and [`TextSize`]. -/// -/// This function allows to specify the location of the the source code, other than -/// that, it behaves exactly like [`parse`]. -/// -/// # Example -/// -/// ``` -/// # use ruff_text_size::TextSize; -/// use ruff_python_parser::{Mode, parse_starts_at}; -/// -/// let source = r#" -/// def fib(i): -/// a, b = 0, 1 -/// for _ in range(i): -/// a, b = b, a + b -/// return a -/// -/// print(fib(42)) -/// "#; -/// let program = parse_starts_at(source, Mode::Module, TextSize::from(0)); -/// assert!(program.is_ok()); -/// ``` -pub fn parse_starts_at(source: &str, mode: Mode, offset: TextSize) -> Result { - let lxr = lexer::lex_starts_at(source, mode, offset); - parse_tokens(lxr.collect(), source, mode) -} - -/// Parse an iterator of [`LexResult`]s using the specified [`Mode`]. -/// -/// This could allow you to perform some preprocessing on the tokens before parsing them. -/// -/// # Example -/// -/// As an example, instead of parsing a string, we can parse a list of tokens after we generate -/// them using the [`lexer::lex`] function: -/// -/// ``` -/// use ruff_python_parser::{lexer::lex, Mode, parse_tokens}; -/// -/// let source = "1 + 2"; -/// let expr = parse_tokens(lex(source, Mode::Expression).collect(), source, Mode::Expression); -/// assert!(expr.is_ok()); -/// ``` -pub fn parse_tokens(tokens: Vec, source: &str, mode: Mode) -> Result { - let marker_token = (Tok::start_marker(mode), TextRange::default()); - let lexer = std::iter::once(Ok(marker_token)).chain(TokenSource::new(tokens)); - python::TopParser::new() - .parse( - source, - mode, - lexer.map_ok(|(t, range)| (range.start(), t, range.end())), - ) - .map_err(parse_error_from_lalrpop) -} - -/// Represents represent errors that occur during parsing and are -/// returned by the `parse_*` functions. - -#[derive(Debug, PartialEq)] -pub struct ParseError { - pub error: ParseErrorType, - pub offset: TextSize, -} - -impl std::ops::Deref for ParseError { - type Target = ParseErrorType; - - fn deref(&self) -> &Self::Target { - &self.error - } -} - -impl std::error::Error for ParseError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - Some(&self.error) - } -} - -impl std::fmt::Display for ParseError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!( - f, - "{} at byte offset {}", - &self.error, - u32::from(self.offset) - ) - } -} - -/// Represents the different types of errors that can occur during parsing. -#[derive(Debug, PartialEq)] -pub enum ParseErrorType { - /// Parser encountered an unexpected end of input - Eof, - /// Parser encountered an extra token - ExtraToken(Tok), - /// Parser encountered an invalid token - InvalidToken, - /// Parser encountered an unexpected token - UnrecognizedToken(Tok, Option), - // Maps to `User` type from `lalrpop-util` - /// Parser encountered an error during lexing. - Lexical(LexicalErrorType), -} - -impl std::error::Error for ParseErrorType {} - -// Convert `lalrpop_util::ParseError` to our internal type -fn parse_error_from_lalrpop(err: LalrpopError) -> ParseError { - match err { - // TODO: Are there cases where this isn't an EOF? - LalrpopError::InvalidToken { location } => ParseError { - error: ParseErrorType::Eof, - offset: location, - }, - LalrpopError::ExtraToken { token } => ParseError { - error: ParseErrorType::ExtraToken(token.1), - offset: token.0, - }, - LalrpopError::User { error } => ParseError { - offset: error.location(), - error: ParseErrorType::Lexical(error.into_error()), - }, - LalrpopError::UnrecognizedToken { token, expected } => { - // Hacky, but it's how CPython does it. See PyParser_AddToken, - // in particular "Only one possible expected token" comment. - let expected = (expected.len() == 1).then(|| expected[0].clone()); - ParseError { - error: ParseErrorType::UnrecognizedToken(token.1, expected), - offset: token.0, - } - } - LalrpopError::UnrecognizedEof { location, expected } => { - // This could be an initial indentation error that we should ignore - let indent_error = expected == ["Indent"]; - if indent_error { - ParseError { - error: ParseErrorType::Lexical(LexicalErrorType::IndentationError), - offset: location, - } - } else { - ParseError { - error: ParseErrorType::Eof, - offset: location, - } - } - } - } -} - -impl std::fmt::Display for ParseErrorType { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match *self { - ParseErrorType::Eof => write!(f, "Got unexpected EOF"), - ParseErrorType::ExtraToken(ref tok) => write!(f, "Got extraneous token: {tok:?}"), - ParseErrorType::InvalidToken => write!(f, "Got invalid token"), - ParseErrorType::UnrecognizedToken(ref tok, ref expected) => { - if *tok == Tok::Indent { - write!(f, "unexpected indent") - } else if expected.as_deref() == Some("Indent") { - write!(f, "expected an indented block") - } else { - write!(f, "invalid syntax. Got unexpected token {tok}") - } - } - ParseErrorType::Lexical(ref error) => write!(f, "{error}"), - } - } -} - -impl ParseErrorType { - /// Returns true if the error is an indentation error. - pub fn is_indentation_error(&self) -> bool { - match self { - ParseErrorType::Lexical(LexicalErrorType::IndentationError) => true, - ParseErrorType::UnrecognizedToken(token, expected) => { - *token == Tok::Indent || expected.clone() == Some("Indent".to_owned()) - } - _ => false, - } - } - - /// Returns true if the error is a tab error. - pub fn is_tab_error(&self) -> bool { - matches!( - self, - ParseErrorType::Lexical(LexicalErrorType::TabError | LexicalErrorType::TabsAfterSpaces) - ) - } -} - -impl From for ParseError { - fn from(error: LexicalError) -> Self { - ParseError { - offset: error.location(), - error: ParseErrorType::Lexical(error.into_error()), - } - } -} - -/// An expression that may be parenthesized. -#[derive(Clone, Debug)] -pub(super) struct ParenthesizedExpr { - /// The range of the expression, including any parentheses. - pub(super) range: TextRange, - /// The underlying expression. - pub(super) expr: Expr, -} - -impl ParenthesizedExpr { - /// Returns `true` if the expression is parenthesized. - pub(super) fn is_parenthesized(&self) -> bool { - self.range.start() != self.expr.range().start() - } -} - -impl Ranged for ParenthesizedExpr { - fn range(&self) -> TextRange { - self.range - } -} -impl From for ParenthesizedExpr { - fn from(expr: Expr) -> Self { - ParenthesizedExpr { - range: expr.range(), - expr, - } - } -} -impl From for Expr { - fn from(parenthesized_expr: ParenthesizedExpr) -> Self { - parenthesized_expr.expr - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprIpyEscapeCommand) -> Self { - Expr::IpyEscapeCommand(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprBoolOp) -> Self { - Expr::BoolOp(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprNamedExpr) -> Self { - Expr::NamedExpr(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprBinOp) -> Self { - Expr::BinOp(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprUnaryOp) -> Self { - Expr::UnaryOp(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprLambda) -> Self { - Expr::Lambda(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprIfExp) -> Self { - Expr::IfExp(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprDict) -> Self { - Expr::Dict(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprSet) -> Self { - Expr::Set(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprListComp) -> Self { - Expr::ListComp(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprSetComp) -> Self { - Expr::SetComp(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprDictComp) -> Self { - Expr::DictComp(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprGeneratorExp) -> Self { - Expr::GeneratorExp(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprAwait) -> Self { - Expr::Await(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprYield) -> Self { - Expr::Yield(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprYieldFrom) -> Self { - Expr::YieldFrom(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprCompare) -> Self { - Expr::Compare(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprCall) -> Self { - Expr::Call(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprFString) -> Self { - Expr::FString(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprStringLiteral) -> Self { - Expr::StringLiteral(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprBytesLiteral) -> Self { - Expr::BytesLiteral(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprNumberLiteral) -> Self { - Expr::NumberLiteral(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprBooleanLiteral) -> Self { - Expr::BooleanLiteral(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprNoneLiteral) -> Self { - Expr::NoneLiteral(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprEllipsisLiteral) -> Self { - Expr::EllipsisLiteral(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprAttribute) -> Self { - Expr::Attribute(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprSubscript) -> Self { - Expr::Subscript(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprStarred) -> Self { - Expr::Starred(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprName) -> Self { - Expr::Name(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprList) -> Self { - Expr::List(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprTuple) -> Self { - Expr::Tuple(payload).into() - } -} -impl From for ParenthesizedExpr { - fn from(payload: ExprSlice) -> Self { - Expr::Slice(payload).into() - } -} - #[cfg(test)] mod tests { use insta::assert_debug_snapshot; - use super::*; - - #[cfg(target_pointer_width = "64")] - #[test] - fn size_assertions() { - assert_eq!(std::mem::size_of::(), 72); - } + use crate::{lexer, parse, parse_expression, parse_suite, parse_tokens, Mode}; #[test] fn test_parse_empty() { @@ -842,7 +274,6 @@ with ((yield from a)): pass for source in [ "with 0,: pass", "with 0 as x,: pass", - "with 0 as *x: pass", "with *a: pass", "with *a as x: pass", "with (*a): pass", @@ -1437,11 +868,11 @@ a = 1 %timeit a == 1 " .trim(); - let lxr = lexer::lex_starts_at(source, Mode::Ipython, TextSize::default()); + let lxr = lexer::lex(source, Mode::Ipython); let parse_err = parse_tokens(lxr.collect(), source, Mode::Module).unwrap_err(); assert_eq!( parse_err.to_string(), - "IPython escape commands are only allowed in `Mode::Ipython` at byte offset 6" + "IPython escape commands are only allowed in `Mode::Ipython` at byte range 6..20" .to_string() ); } diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__ann_assign_name.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__ann_assign_name.snap deleted file mode 100644 index 656770c0946b2..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__ann_assign_name.snap +++ /dev/null @@ -1,36 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - AnnAssign( - StmtAnnAssign { - range: 0..10, - target: Name( - ExprName { - range: 0..1, - id: "x", - ctx: Store, - }, - ), - annotation: Name( - ExprName { - range: 3..6, - id: "int", - ctx: Load, - }, - ), - value: Some( - NumberLiteral( - ExprNumberLiteral { - range: 9..10, - value: Int( - 1, - ), - }, - ), - ), - simple: true, - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_attribute.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_attribute.snap deleted file mode 100644 index e269b2e4d45ed..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_attribute.snap +++ /dev/null @@ -1,62 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Assign( - StmtAssign { - range: 0..15, - targets: [ - Attribute( - ExprAttribute { - range: 0..3, - value: Name( - ExprName { - range: 0..1, - id: "x", - ctx: Load, - }, - ), - attr: Identifier { - id: "y", - range: 2..3, - }, - ctx: Store, - }, - ), - ], - value: Tuple( - ExprTuple { - range: 6..15, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 7..8, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 10..11, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 13..14, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_for.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_for.snap deleted file mode 100644 index d7775fae99876..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_for.snap +++ /dev/null @@ -1,59 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - For( - StmtFor { - range: 0..24, - is_async: false, - target: Name( - ExprName { - range: 4..5, - id: "x", - ctx: Store, - }, - ), - iter: Tuple( - ExprTuple { - range: 9..18, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 10..11, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 13..14, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 16..17, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - body: [ - Pass( - StmtPass { - range: 20..24, - }, - ), - ], - orelse: [], - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list.snap deleted file mode 100644 index 436e3f449f885..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list.snap +++ /dev/null @@ -1,67 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Assign( - StmtAssign { - range: 0..18, - targets: [ - List( - ExprList { - range: 0..6, - elts: [ - Name( - ExprName { - range: 1..2, - id: "x", - ctx: Store, - }, - ), - Name( - ExprName { - range: 4..5, - id: "y", - ctx: Store, - }, - ), - ], - ctx: Store, - }, - ), - ], - value: Tuple( - ExprTuple { - range: 9..18, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 10..11, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 13..14, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 16..17, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list_comp.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list_comp.snap deleted file mode 100644 index d73b326211c80..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list_comp.snap +++ /dev/null @@ -1,78 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Assign( - StmtAssign { - range: 0..26, - targets: [ - Name( - ExprName { - range: 0..1, - id: "x", - ctx: Store, - }, - ), - ], - value: ListComp( - ExprListComp { - range: 4..26, - elt: Name( - ExprName { - range: 5..6, - id: "y", - ctx: Load, - }, - ), - generators: [ - Comprehension { - range: 7..25, - target: Name( - ExprName { - range: 11..12, - id: "y", - ctx: Store, - }, - ), - iter: Tuple( - ExprTuple { - range: 16..25, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 17..18, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 20..21, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 23..24, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - ifs: [], - is_async: false, - }, - ], - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_name.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_name.snap deleted file mode 100644 index c3b835c27e2ec..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_name.snap +++ /dev/null @@ -1,52 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Assign( - StmtAssign { - range: 0..13, - targets: [ - Name( - ExprName { - range: 0..1, - id: "x", - ctx: Store, - }, - ), - ], - value: Tuple( - ExprTuple { - range: 4..13, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 5..6, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 8..9, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 11..12, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_named_expr.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_named_expr.snap deleted file mode 100644 index 4177eaaeb8945..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_named_expr.snap +++ /dev/null @@ -1,39 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - If( - StmtIf { - range: 0..14, - test: NamedExpr( - ExprNamedExpr { - range: 3..8, - target: Name( - ExprName { - range: 3..4, - id: "x", - ctx: Store, - }, - ), - value: NumberLiteral( - ExprNumberLiteral { - range: 7..8, - value: Int( - 1, - ), - }, - ), - }, - ), - body: [ - Pass( - StmtPass { - range: 10..14, - }, - ), - ], - elif_else_clauses: [], - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_set_comp.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_set_comp.snap deleted file mode 100644 index f5c256cd2541a..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_set_comp.snap +++ /dev/null @@ -1,78 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Assign( - StmtAssign { - range: 0..26, - targets: [ - Name( - ExprName { - range: 0..1, - id: "x", - ctx: Store, - }, - ), - ], - value: SetComp( - ExprSetComp { - range: 4..26, - elt: Name( - ExprName { - range: 5..6, - id: "y", - ctx: Load, - }, - ), - generators: [ - Comprehension { - range: 7..25, - target: Name( - ExprName { - range: 11..12, - id: "y", - ctx: Store, - }, - ), - iter: Tuple( - ExprTuple { - range: 16..25, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 17..18, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 20..21, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 23..24, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - ifs: [], - is_async: false, - }, - ], - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_starred.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_starred.snap deleted file mode 100644 index d6401710ed926..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_starred.snap +++ /dev/null @@ -1,73 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Assign( - StmtAssign { - range: 0..19, - targets: [ - Tuple( - ExprTuple { - range: 0..7, - elts: [ - Name( - ExprName { - range: 1..2, - id: "x", - ctx: Store, - }, - ), - Starred( - ExprStarred { - range: 4..6, - value: Name( - ExprName { - range: 5..6, - id: "y", - ctx: Store, - }, - ), - ctx: Store, - }, - ), - ], - ctx: Store, - }, - ), - ], - value: Tuple( - ExprTuple { - range: 10..19, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 11..12, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 14..15, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 17..18, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_subscript.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_subscript.snap deleted file mode 100644 index abdb9e8088c9c..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_subscript.snap +++ /dev/null @@ -1,65 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Assign( - StmtAssign { - range: 0..16, - targets: [ - Subscript( - ExprSubscript { - range: 0..4, - value: Name( - ExprName { - range: 0..1, - id: "x", - ctx: Load, - }, - ), - slice: Name( - ExprName { - range: 2..3, - id: "y", - ctx: Load, - }, - ), - ctx: Store, - }, - ), - ], - value: Tuple( - ExprTuple { - range: 7..16, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 8..9, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 11..12, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 14..15, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_tuple.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_tuple.snap deleted file mode 100644 index ac332e78f70e9..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_tuple.snap +++ /dev/null @@ -1,67 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Assign( - StmtAssign { - range: 0..18, - targets: [ - Tuple( - ExprTuple { - range: 0..6, - elts: [ - Name( - ExprName { - range: 1..2, - id: "x", - ctx: Store, - }, - ), - Name( - ExprName { - range: 4..5, - id: "y", - ctx: Store, - }, - ), - ], - ctx: Store, - }, - ), - ], - value: Tuple( - ExprTuple { - range: 9..18, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 10..11, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 13..14, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 16..17, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_with.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_with.snap deleted file mode 100644 index 9700d860a8452..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_with.snap +++ /dev/null @@ -1,41 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - With( - StmtWith { - range: 0..17, - is_async: false, - items: [ - WithItem { - range: 5..11, - context_expr: NumberLiteral( - ExprNumberLiteral { - range: 5..6, - value: Int( - 1, - ), - }, - ), - optional_vars: Some( - Name( - ExprName { - range: 10..11, - id: "x", - ctx: Store, - }, - ), - ), - }, - ], - body: [ - Pass( - StmtPass { - range: 13..17, - }, - ), - ], - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_attribute.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_attribute.snap deleted file mode 100644 index 6286c12485136..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_attribute.snap +++ /dev/null @@ -1,61 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - AugAssign( - StmtAugAssign { - range: 0..16, - target: Attribute( - ExprAttribute { - range: 0..3, - value: Name( - ExprName { - range: 0..1, - id: "x", - ctx: Load, - }, - ), - attr: Identifier { - id: "y", - range: 2..3, - }, - ctx: Store, - }, - ), - op: Add, - value: Tuple( - ExprTuple { - range: 7..16, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 8..9, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 11..12, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 14..15, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_name.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_name.snap deleted file mode 100644 index 760e6d3638268..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_name.snap +++ /dev/null @@ -1,27 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - AugAssign( - StmtAugAssign { - range: 0..6, - target: Name( - ExprName { - range: 0..1, - id: "x", - ctx: Store, - }, - ), - op: Add, - value: NumberLiteral( - ExprNumberLiteral { - range: 5..6, - value: Int( - 1, - ), - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_subscript.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_subscript.snap deleted file mode 100644 index 6b1feb1798485..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_subscript.snap +++ /dev/null @@ -1,64 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - AugAssign( - StmtAugAssign { - range: 0..17, - target: Subscript( - ExprSubscript { - range: 0..4, - value: Name( - ExprName { - range: 0..1, - id: "x", - ctx: Load, - }, - ), - slice: Name( - ExprName { - range: 2..3, - id: "y", - ctx: Load, - }, - ), - ctx: Store, - }, - ), - op: Add, - value: Tuple( - ExprTuple { - range: 8..17, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 9..10, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 12..13, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 15..16, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_attribute.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_attribute.snap deleted file mode 100644 index 0ee68d973c8fa..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_attribute.snap +++ /dev/null @@ -1,30 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Delete( - StmtDelete { - range: 0..7, - targets: [ - Attribute( - ExprAttribute { - range: 4..7, - value: Name( - ExprName { - range: 4..5, - id: "x", - ctx: Load, - }, - ), - attr: Identifier { - id: "y", - range: 6..7, - }, - ctx: Del, - }, - ), - ], - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_name.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_name.snap deleted file mode 100644 index c885abecbbbde..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_name.snap +++ /dev/null @@ -1,20 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Delete( - StmtDelete { - range: 0..5, - targets: [ - Name( - ExprName { - range: 4..5, - id: "x", - ctx: Del, - }, - ), - ], - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_subscript.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_subscript.snap deleted file mode 100644 index f6333d4625ec5..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__del_subscript.snap +++ /dev/null @@ -1,33 +0,0 @@ ---- -source: crates/ruff_python_parser/src/context.rs -expression: parse_ast ---- -[ - Delete( - StmtDelete { - range: 0..8, - targets: [ - Subscript( - ExprSubscript { - range: 4..8, - value: Name( - ExprName { - range: 4..5, - id: "x", - ctx: Load, - }, - ), - slice: Name( - ExprName { - range: 6..7, - id: "y", - ctx: Load, - }, - ), - ctx: Del, - }, - ), - ], - }, - ), -] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_kw_only_args.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_kw_only_args.snap deleted file mode 100644 index ad7dbadf9a774..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_kw_only_args.snap +++ /dev/null @@ -1,73 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..23, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..17, - posonlyargs: [], - args: [], - vararg: None, - kwonlyargs: [ - ParameterWithDefault { - range: 9..10, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "a", - range: 9..10, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 12..13, - parameter: Parameter { - range: 12..13, - name: Identifier { - id: "b", - range: 12..13, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 15..16, - parameter: Parameter { - range: 15..16, - name: Identifier { - id: "c", - range: 15..16, - }, - annotation: None, - }, - default: None, - }, - ], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 19..23, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_kw_only_args_with_defaults.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_kw_only_args_with_defaults.snap deleted file mode 100644 index 4fc404217901d..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_kw_only_args_with_defaults.snap +++ /dev/null @@ -1,91 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..29, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..23, - posonlyargs: [], - args: [], - vararg: None, - kwonlyargs: [ - ParameterWithDefault { - range: 9..10, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "a", - range: 9..10, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 12..16, - parameter: Parameter { - range: 12..13, - name: Identifier { - id: "b", - range: 12..13, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 14..16, - value: Int( - 20, - ), - }, - ), - ), - }, - ParameterWithDefault { - range: 18..22, - parameter: Parameter { - range: 18..19, - name: Identifier { - id: "c", - range: 18..19, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 20..22, - value: Int( - 30, - ), - }, - ), - ), - }, - ], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 25..29, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_no_args.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_no_args.snap deleted file mode 100644 index 4e2121628df29..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_no_args.snap +++ /dev/null @@ -1,36 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..13, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..7, - posonlyargs: [], - args: [], - vararg: None, - kwonlyargs: [], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 9..13, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_no_args_with_ranges.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_no_args_with_ranges.snap deleted file mode 100644 index 4e2121628df29..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_no_args_with_ranges.snap +++ /dev/null @@ -1,36 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..13, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..7, - posonlyargs: [], - args: [], - vararg: None, - kwonlyargs: [], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 9..13, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args.snap deleted file mode 100644 index cd12db1311c6a..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args.snap +++ /dev/null @@ -1,110 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..32, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..26, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 6..7, - parameter: Parameter { - range: 6..7, - name: Identifier { - id: "a", - range: 6..7, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 9..10, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "b", - range: 9..10, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 12..13, - parameter: Parameter { - range: 12..13, - name: Identifier { - id: "c", - range: 12..13, - }, - annotation: None, - }, - default: None, - }, - ], - vararg: None, - kwonlyargs: [ - ParameterWithDefault { - range: 18..19, - parameter: Parameter { - range: 18..19, - name: Identifier { - id: "d", - range: 18..19, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 21..22, - parameter: Parameter { - range: 21..22, - name: Identifier { - id: "e", - range: 21..22, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 24..25, - parameter: Parameter { - range: 24..25, - name: Identifier { - id: "f", - range: 24..25, - }, - annotation: None, - }, - default: None, - }, - ], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 28..32, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults.snap deleted file mode 100644 index 790aebd17cabc..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults.snap +++ /dev/null @@ -1,128 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..38, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..32, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 6..7, - parameter: Parameter { - range: 6..7, - name: Identifier { - id: "a", - range: 6..7, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 9..10, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "b", - range: 9..10, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 12..13, - parameter: Parameter { - range: 12..13, - name: Identifier { - id: "c", - range: 12..13, - }, - annotation: None, - }, - default: None, - }, - ], - vararg: None, - kwonlyargs: [ - ParameterWithDefault { - range: 18..19, - parameter: Parameter { - range: 18..19, - name: Identifier { - id: "d", - range: 18..19, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 21..25, - parameter: Parameter { - range: 21..22, - name: Identifier { - id: "e", - range: 21..22, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 23..25, - value: Int( - 20, - ), - }, - ), - ), - }, - ParameterWithDefault { - range: 27..31, - parameter: Parameter { - range: 27..28, - name: Identifier { - id: "f", - range: 27..28, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 29..31, - value: Int( - 30, - ), - }, - ), - ), - }, - ], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 34..38, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults_and_varargs.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults_and_varargs.snap deleted file mode 100644 index b3ca641a0b602..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults_and_varargs.snap +++ /dev/null @@ -1,137 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..42, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..36, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 6..7, - parameter: Parameter { - range: 6..7, - name: Identifier { - id: "a", - range: 6..7, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 9..10, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "b", - range: 9..10, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 12..13, - parameter: Parameter { - range: 12..13, - name: Identifier { - id: "c", - range: 12..13, - }, - annotation: None, - }, - default: None, - }, - ], - vararg: Some( - Parameter { - range: 16..20, - name: Identifier { - id: "args", - range: 16..20, - }, - annotation: None, - }, - ), - kwonlyargs: [ - ParameterWithDefault { - range: 22..23, - parameter: Parameter { - range: 22..23, - name: Identifier { - id: "d", - range: 22..23, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 25..29, - parameter: Parameter { - range: 25..26, - name: Identifier { - id: "e", - range: 25..26, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 27..29, - value: Int( - 20, - ), - }, - ), - ), - }, - ParameterWithDefault { - range: 31..35, - parameter: Parameter { - range: 31..32, - name: Identifier { - id: "f", - range: 31..32, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 33..35, - value: Int( - 30, - ), - }, - ), - ), - }, - ], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 38..42, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults_and_varargs_and_kwargs.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults_and_varargs_and_kwargs.snap deleted file mode 100644 index ce6f07a190701..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_and_kw_only_args_with_defaults_and_varargs_and_kwargs.snap +++ /dev/null @@ -1,146 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..52, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..46, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 6..7, - parameter: Parameter { - range: 6..7, - name: Identifier { - id: "a", - range: 6..7, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 9..10, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "b", - range: 9..10, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 12..13, - parameter: Parameter { - range: 12..13, - name: Identifier { - id: "c", - range: 12..13, - }, - annotation: None, - }, - default: None, - }, - ], - vararg: Some( - Parameter { - range: 16..20, - name: Identifier { - id: "args", - range: 16..20, - }, - annotation: None, - }, - ), - kwonlyargs: [ - ParameterWithDefault { - range: 22..23, - parameter: Parameter { - range: 22..23, - name: Identifier { - id: "d", - range: 22..23, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 25..29, - parameter: Parameter { - range: 25..26, - name: Identifier { - id: "e", - range: 25..26, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 27..29, - value: Int( - 20, - ), - }, - ), - ), - }, - ParameterWithDefault { - range: 31..35, - parameter: Parameter { - range: 31..32, - name: Identifier { - id: "f", - range: 31..32, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 33..35, - value: Int( - 30, - ), - }, - ), - ), - }, - ], - kwarg: Some( - Parameter { - range: 39..45, - name: Identifier { - id: "kwargs", - range: 39..45, - }, - annotation: None, - }, - ), - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 48..52, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args.snap deleted file mode 100644 index 788b1b7a49066..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args.snap +++ /dev/null @@ -1,73 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..20, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..14, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 6..7, - parameter: Parameter { - range: 6..7, - name: Identifier { - id: "a", - range: 6..7, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 9..10, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "b", - range: 9..10, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 12..13, - parameter: Parameter { - range: 12..13, - name: Identifier { - id: "c", - range: 12..13, - }, - annotation: None, - }, - default: None, - }, - ], - vararg: None, - kwonlyargs: [], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 16..20, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args_with_defaults.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args_with_defaults.snap deleted file mode 100644 index aee38b6fa5823..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args_with_defaults.snap +++ /dev/null @@ -1,91 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..26, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..20, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 6..7, - parameter: Parameter { - range: 6..7, - name: Identifier { - id: "a", - range: 6..7, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 9..13, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "b", - range: 9..10, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 11..13, - value: Int( - 20, - ), - }, - ), - ), - }, - ParameterWithDefault { - range: 15..19, - parameter: Parameter { - range: 15..16, - name: Identifier { - id: "c", - range: 15..16, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 17..19, - value: Int( - 30, - ), - }, - ), - ), - }, - ], - vararg: None, - kwonlyargs: [], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 22..26, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args_with_ranges.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args_with_ranges.snap deleted file mode 100644 index 788b1b7a49066..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__function_pos_args_with_ranges.snap +++ /dev/null @@ -1,73 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - FunctionDef( - StmtFunctionDef { - range: 0..20, - is_async: false, - decorator_list: [], - name: Identifier { - id: "f", - range: 4..5, - }, - type_params: None, - parameters: Parameters { - range: 5..14, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 6..7, - parameter: Parameter { - range: 6..7, - name: Identifier { - id: "a", - range: 6..7, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 9..10, - parameter: Parameter { - range: 9..10, - name: Identifier { - id: "b", - range: 9..10, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 12..13, - parameter: Parameter { - range: 12..13, - name: Identifier { - id: "c", - range: 12..13, - }, - annotation: None, - }, - default: None, - }, - ], - vararg: None, - kwonlyargs: [], - kwarg: None, - }, - returns: None, - body: [ - Pass( - StmtPass { - range: 16..20, - }, - ), - ], - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_kw_only_args.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_kw_only_args.snap deleted file mode 100644 index 38d20e2b292c7..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_kw_only_args.snap +++ /dev/null @@ -1,73 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - Expr( - StmtExpr { - range: 0..20, - value: Lambda( - ExprLambda { - range: 0..20, - parameters: Some( - Parameters { - range: 7..17, - posonlyargs: [], - args: [], - vararg: None, - kwonlyargs: [ - ParameterWithDefault { - range: 10..11, - parameter: Parameter { - range: 10..11, - name: Identifier { - id: "a", - range: 10..11, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 13..14, - parameter: Parameter { - range: 13..14, - name: Identifier { - id: "b", - range: 13..14, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 16..17, - parameter: Parameter { - range: 16..17, - name: Identifier { - id: "c", - range: 16..17, - }, - annotation: None, - }, - default: None, - }, - ], - kwarg: None, - }, - ), - body: NumberLiteral( - ExprNumberLiteral { - range: 19..20, - value: Int( - 1, - ), - }, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_kw_only_args_with_defaults.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_kw_only_args_with_defaults.snap deleted file mode 100644 index 3defc8ae80cc3..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_kw_only_args_with_defaults.snap +++ /dev/null @@ -1,91 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - Expr( - StmtExpr { - range: 0..26, - value: Lambda( - ExprLambda { - range: 0..26, - parameters: Some( - Parameters { - range: 7..23, - posonlyargs: [], - args: [], - vararg: None, - kwonlyargs: [ - ParameterWithDefault { - range: 10..11, - parameter: Parameter { - range: 10..11, - name: Identifier { - id: "a", - range: 10..11, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 13..17, - parameter: Parameter { - range: 13..14, - name: Identifier { - id: "b", - range: 13..14, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 15..17, - value: Int( - 20, - ), - }, - ), - ), - }, - ParameterWithDefault { - range: 19..23, - parameter: Parameter { - range: 19..20, - name: Identifier { - id: "c", - range: 19..20, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 21..23, - value: Int( - 30, - ), - }, - ), - ), - }, - ], - kwarg: None, - }, - ), - body: NumberLiteral( - ExprNumberLiteral { - range: 25..26, - value: Int( - 1, - ), - }, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_no_args.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_no_args.snap deleted file mode 100644 index 3008200f440ec..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_no_args.snap +++ /dev/null @@ -1,27 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - Expr( - StmtExpr { - range: 0..9, - value: Lambda( - ExprLambda { - range: 0..9, - parameters: None, - body: NumberLiteral( - ExprNumberLiteral { - range: 8..9, - value: Int( - 1, - ), - }, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_and_kw_only_args.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_and_kw_only_args.snap deleted file mode 100644 index 9f137fa1ace22..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_and_kw_only_args.snap +++ /dev/null @@ -1,98 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - Expr( - StmtExpr { - range: 0..26, - value: Lambda( - ExprLambda { - range: 0..26, - parameters: Some( - Parameters { - range: 7..23, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 7..8, - parameter: Parameter { - range: 7..8, - name: Identifier { - id: "a", - range: 7..8, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 10..11, - parameter: Parameter { - range: 10..11, - name: Identifier { - id: "b", - range: 10..11, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 13..14, - parameter: Parameter { - range: 13..14, - name: Identifier { - id: "c", - range: 13..14, - }, - annotation: None, - }, - default: None, - }, - ], - vararg: None, - kwonlyargs: [ - ParameterWithDefault { - range: 19..20, - parameter: Parameter { - range: 19..20, - name: Identifier { - id: "d", - range: 19..20, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 22..23, - parameter: Parameter { - range: 22..23, - name: Identifier { - id: "e", - range: 22..23, - }, - annotation: None, - }, - default: None, - }, - ], - kwarg: None, - }, - ), - body: NumberLiteral( - ExprNumberLiteral { - range: 25..26, - value: Int( - 0, - ), - }, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_args.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_args.snap deleted file mode 100644 index 4af124410e137..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_args.snap +++ /dev/null @@ -1,73 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - Expr( - StmtExpr { - range: 0..17, - value: Lambda( - ExprLambda { - range: 0..17, - parameters: Some( - Parameters { - range: 7..14, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 7..8, - parameter: Parameter { - range: 7..8, - name: Identifier { - id: "a", - range: 7..8, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 10..11, - parameter: Parameter { - range: 10..11, - name: Identifier { - id: "b", - range: 10..11, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 13..14, - parameter: Parameter { - range: 13..14, - name: Identifier { - id: "c", - range: 13..14, - }, - annotation: None, - }, - default: None, - }, - ], - vararg: None, - kwonlyargs: [], - kwarg: None, - }, - ), - body: NumberLiteral( - ExprNumberLiteral { - range: 16..17, - value: Int( - 1, - ), - }, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_args_with_defaults.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_args_with_defaults.snap deleted file mode 100644 index 7f12863609c13..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__function__tests__lambda_pos_args_with_defaults.snap +++ /dev/null @@ -1,91 +0,0 @@ ---- -source: crates/ruff_python_parser/src/function.rs -expression: parse_ast ---- -Ok( - [ - Expr( - StmtExpr { - range: 0..23, - value: Lambda( - ExprLambda { - range: 0..23, - parameters: Some( - Parameters { - range: 7..20, - posonlyargs: [], - args: [ - ParameterWithDefault { - range: 7..8, - parameter: Parameter { - range: 7..8, - name: Identifier { - id: "a", - range: 7..8, - }, - annotation: None, - }, - default: None, - }, - ParameterWithDefault { - range: 10..14, - parameter: Parameter { - range: 10..11, - name: Identifier { - id: "b", - range: 10..11, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 12..14, - value: Int( - 20, - ), - }, - ), - ), - }, - ParameterWithDefault { - range: 16..20, - parameter: Parameter { - range: 16..17, - name: Identifier { - id: "c", - range: 16..17, - }, - annotation: None, - }, - default: Some( - NumberLiteral( - ExprNumberLiteral { - range: 18..20, - value: Int( - 30, - ), - }, - ), - ), - }, - ], - vararg: None, - kwonlyargs: [], - kwarg: None, - }, - ), - body: NumberLiteral( - ExprNumberLiteral { - range: 22..23, - value: Int( - 1, - ), - }, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_assignment_expr.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_assignment_expr.snap deleted file mode 100644 index 58e38220f70a2..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_assignment_expr.snap +++ /dev/null @@ -1,33 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Expr( - StmtExpr { - range: 0..8, - value: NamedExpr( - ExprNamedExpr { - range: 1..7, - target: Name( - ExprName { - range: 1..2, - id: "x", - ctx: Store, - }, - ), - value: NumberLiteral( - ExprNumberLiteral { - range: 6..7, - value: Int( - 5, - ), - }, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_attribute_normal.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_attribute_normal.snap deleted file mode 100644 index feb0fedf4bd72..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_attribute_normal.snap +++ /dev/null @@ -1,40 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..12, - targets: [ - Attribute( - ExprAttribute { - range: 0..7, - value: Name( - ExprName { - range: 0..3, - id: "foo", - ctx: Load, - }, - ), - attr: Identifier { - id: "bar", - range: 4..7, - }, - ctx: Store, - }, - ), - ], - value: NumberLiteral( - ExprNumberLiteral { - range: 10..12, - value: Int( - 42, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_attribute_weird.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_attribute_weird.snap deleted file mode 100644 index a8492db558afb..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_attribute_weird.snap +++ /dev/null @@ -1,47 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..12, - targets: [ - Attribute( - ExprAttribute { - range: 0..7, - value: StringLiteral( - ExprStringLiteral { - range: 0..5, - value: StringLiteralValue { - inner: Single( - StringLiteral { - range: 0..5, - value: "foo", - unicode: false, - }, - ), - }, - }, - ), - attr: Identifier { - id: "y", - range: 6..7, - }, - ctx: Store, - }, - ), - ], - value: NumberLiteral( - ExprNumberLiteral { - range: 10..12, - value: Int( - 42, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_list.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_list.snap deleted file mode 100644 index f2673d1c28b1d..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_list.snap +++ /dev/null @@ -1,76 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..21, - targets: [ - List( - ExprList { - range: 0..9, - elts: [ - Name( - ExprName { - range: 1..2, - id: "x", - ctx: Store, - }, - ), - Name( - ExprName { - range: 4..5, - id: "y", - ctx: Store, - }, - ), - Name( - ExprName { - range: 7..8, - id: "z", - ctx: Store, - }, - ), - ], - ctx: Store, - }, - ), - ], - value: List( - ExprList { - range: 12..21, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 13..14, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 16..17, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 19..20, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_name.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_name.snap deleted file mode 100644 index 9bde5767f1a96..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_name.snap +++ /dev/null @@ -1,30 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..8, - targets: [ - Name( - ExprName { - range: 0..3, - id: "foo", - ctx: Store, - }, - ), - ], - value: NumberLiteral( - ExprNumberLiteral { - range: 6..8, - value: Int( - 42, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_slice_normal.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_slice_normal.snap deleted file mode 100644 index c6a55fb48e871..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_slice_normal.snap +++ /dev/null @@ -1,70 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..13, - targets: [ - Subscript( - ExprSubscript { - range: 0..6, - value: Name( - ExprName { - range: 0..1, - id: "x", - ctx: Load, - }, - ), - slice: Slice( - ExprSlice { - range: 2..5, - lower: Some( - NumberLiteral( - ExprNumberLiteral { - range: 2..3, - value: Int( - 1, - ), - }, - ), - ), - upper: Some( - NumberLiteral( - ExprNumberLiteral { - range: 4..5, - value: Int( - 2, - ), - }, - ), - ), - step: None, - }, - ), - ctx: Store, - }, - ), - ], - value: List( - ExprList { - range: 9..13, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 10..12, - value: Int( - 42, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_slice_weird.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_slice_weird.snap deleted file mode 100644 index a48ebfcc5d102..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_slice_weird.snap +++ /dev/null @@ -1,71 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..13, - targets: [ - Subscript( - ExprSubscript { - range: 0..6, - value: NumberLiteral( - ExprNumberLiteral { - range: 0..1, - value: Int( - 5, - ), - }, - ), - slice: Slice( - ExprSlice { - range: 2..5, - lower: Some( - NumberLiteral( - ExprNumberLiteral { - range: 2..3, - value: Int( - 1, - ), - }, - ), - ), - upper: Some( - NumberLiteral( - ExprNumberLiteral { - range: 4..5, - value: Int( - 2, - ), - }, - ), - ), - step: None, - }, - ), - ctx: Store, - }, - ), - ], - value: List( - ExprList { - range: 9..13, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 10..12, - value: Int( - 42, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_starred.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_starred.snap deleted file mode 100644 index 963ecc513bccb..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_starred.snap +++ /dev/null @@ -1,36 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..9, - targets: [ - Starred( - ExprStarred { - range: 0..4, - value: Name( - ExprName { - range: 1..4, - id: "foo", - ctx: Store, - }, - ), - ctx: Store, - }, - ), - ], - value: NumberLiteral( - ExprNumberLiteral { - range: 7..9, - value: Int( - 42, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_subscript_normal.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_subscript_normal.snap deleted file mode 100644 index 225358f1903a7..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_subscript_normal.snap +++ /dev/null @@ -1,44 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..9, - targets: [ - Subscript( - ExprSubscript { - range: 0..4, - value: Name( - ExprName { - range: 0..1, - id: "x", - ctx: Load, - }, - ), - slice: NumberLiteral( - ExprNumberLiteral { - range: 2..3, - value: Int( - 0, - ), - }, - ), - ctx: Store, - }, - ), - ], - value: NumberLiteral( - ExprNumberLiteral { - range: 7..9, - value: Int( - 42, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_subscript_weird.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_subscript_weird.snap deleted file mode 100644 index 5073da87b7b6e..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_subscript_weird.snap +++ /dev/null @@ -1,45 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..9, - targets: [ - Subscript( - ExprSubscript { - range: 0..4, - value: NumberLiteral( - ExprNumberLiteral { - range: 0..1, - value: Int( - 5, - ), - }, - ), - slice: NumberLiteral( - ExprNumberLiteral { - range: 2..3, - value: Int( - 0, - ), - }, - ), - ctx: Store, - }, - ), - ], - value: NumberLiteral( - ExprNumberLiteral { - range: 7..9, - value: Int( - 42, - ), - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_tuple.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_tuple.snap deleted file mode 100644 index 88e7d791f0114..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_tuple.snap +++ /dev/null @@ -1,76 +0,0 @@ ---- -source: crates/ruff_python_parser/src/invalid.rs -expression: ast ---- -Ok( - [ - Assign( - StmtAssign { - range: 0..21, - targets: [ - Tuple( - ExprTuple { - range: 0..9, - elts: [ - Name( - ExprName { - range: 1..2, - id: "x", - ctx: Store, - }, - ), - Name( - ExprName { - range: 4..5, - id: "y", - ctx: Store, - }, - ), - Name( - ExprName { - range: 7..8, - id: "z", - ctx: Store, - }, - ), - ], - ctx: Store, - }, - ), - ], - value: Tuple( - ExprTuple { - range: 12..21, - elts: [ - NumberLiteral( - ExprNumberLiteral { - range: 13..14, - value: Int( - 1, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 16..17, - value: Int( - 2, - ), - }, - ), - NumberLiteral( - ExprNumberLiteral { - range: 19..20, - value: Int( - 3, - ), - }, - ), - ], - ctx: Load, - }, - ), - }, - ), - ], -) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__invalid_leading_zero_big.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__invalid_leading_zero_big.snap index c2906398a5141..a0eb10bff4219 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__invalid_leading_zero_big.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__invalid_leading_zero_big.snap @@ -5,8 +5,8 @@ expression: tokens Err( LexicalError { error: OtherError( - "Invalid Token", + "Invalid decimal integer literal", ), - location: 0, + location: 0..85, }, ) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__invalid_leading_zero_small.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__invalid_leading_zero_small.snap index c2906398a5141..cf606bd31dab2 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__invalid_leading_zero_small.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__invalid_leading_zero_small.snap @@ -5,8 +5,8 @@ expression: tokens Err( LexicalError { error: OtherError( - "Invalid Token", + "Invalid decimal integer literal", ), - location: 0, + location: 0..3, }, ) diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__tet_too_low_dedent.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__tet_too_low_dedent.snap index 8a9ba410ae92d..648ba0ccda2fb 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__tet_too_low_dedent.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__lexer__tests__tet_too_low_dedent.snap @@ -48,7 +48,7 @@ expression: tokens Err( LexicalError { error: IndentationError, - location: 20, + location: 18..20, }, ), Ok( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_empty.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_empty.snap deleted file mode 100644 index 657c2bb740011..0000000000000 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_empty.snap +++ /dev/null @@ -1,5 +0,0 @@ ---- -source: crates/ruff_python_parser/src/parser.rs -expression: parse_ast ---- -[] diff --git a/crates/ruff_python_parser/src/soft_keywords.rs b/crates/ruff_python_parser/src/soft_keywords.rs index e4bff73edc28c..e29781c749752 100644 --- a/crates/ruff_python_parser/src/soft_keywords.rs +++ b/crates/ruff_python_parser/src/soft_keywords.rs @@ -146,7 +146,7 @@ where Tok::NonLogicalNewline | Tok::Comment { .. } => { // Nothing to do. } - Tok::StartModule | Tok::Newline | Tok::Indent | Tok::Dedent => { + Tok::Newline | Tok::Indent | Tok::Dedent => { self.position = Position::Statement; } // If we see a semicolon, assume we're at the start of a simple statement, as in: diff --git a/crates/ruff_python_parser/src/string.rs b/crates/ruff_python_parser/src/string.rs index fb536537216a0..e87e293ddb5b1 100644 --- a/crates/ruff_python_parser/src/string.rs +++ b/crates/ruff_python_parser/src/string.rs @@ -6,12 +6,14 @@ use ruff_python_ast::{self as ast, Expr}; use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::lexer::{LexicalError, LexicalErrorType}; -use crate::token::{StringKind, Tok}; +use crate::token::StringKind; +#[derive(Debug)] pub(crate) enum StringType { Str(ast::StringLiteral), Bytes(ast::BytesLiteral), FString(ast::FString), + Invalid(ast::StringLiteral), } impl Ranged for StringType { @@ -20,6 +22,7 @@ impl Ranged for StringType { Self::Str(node) => node.range(), Self::Bytes(node) => node.range(), Self::FString(node) => node.range(), + Self::Invalid(node) => node.range(), } } } @@ -30,6 +33,7 @@ impl From for Expr { StringType::Str(node) => Expr::from(node), StringType::Bytes(node) => Expr::from(node), StringType::FString(node) => Expr::from(node), + StringType::Invalid(node) => Expr::from(node), } } } @@ -70,6 +74,11 @@ impl StringParser { self.offset + TextSize::try_from(self.cursor).unwrap() } + #[inline] + fn range(&self, start_location: TextSize) -> TextRange { + TextRange::new(dbg!(start_location), dbg!(self.offset)) + } + /// Returns the next byte in the string, if there is one. /// /// # Panics @@ -98,19 +107,31 @@ impl StringParser { fn parse_unicode_literal(&mut self, literal_number: usize) -> Result { let mut p: u32 = 0u32; - let unicode_error = LexicalError::new(LexicalErrorType::UnicodeError, self.get_pos()); for i in 1..=literal_number { match self.next_char() { Some(c) => match c.to_digit(16) { Some(d) => p += d << ((literal_number - i) * 4), - None => return Err(unicode_error), + None => { + return Err(LexicalError::new( + LexicalErrorType::UnicodeError, + TextRange::empty(self.get_pos()), + )) + } }, - None => return Err(unicode_error), + None => { + return Err(LexicalError::new( + LexicalErrorType::UnicodeError, + TextRange::empty(self.get_pos()), + )) + } } } match p { 0xD800..=0xDFFF => Ok(std::char::REPLACEMENT_CHARACTER), - _ => std::char::from_u32(p).ok_or(unicode_error), + _ => std::char::from_u32(p).ok_or(LexicalError::new( + LexicalErrorType::UnicodeError, + TextRange::empty(self.get_pos()), + )), } } @@ -137,14 +158,17 @@ impl StringParser { let start_pos = self.get_pos(); let Some('{') = self.next_char() else { - return Err(LexicalError::new(LexicalErrorType::StringError, start_pos)); + return Err(LexicalError::new( + LexicalErrorType::MissingUnicodeLbrace, + self.range(start_pos), + )); }; let start_pos = self.get_pos(); let Some(close_idx) = self.source[self.cursor..].find('}') else { return Err(LexicalError::new( - LexicalErrorType::StringError, - self.get_pos(), + LexicalErrorType::MissingUnicodeRbrace, + self.range(self.get_pos()), )); }; @@ -152,15 +176,16 @@ impl StringParser { let name = &name_and_ending[..name_and_ending.len() - 1]; unicode_names2::character(name) - .ok_or_else(|| LexicalError::new(LexicalErrorType::UnicodeError, start_pos)) + .ok_or_else(|| LexicalError::new(LexicalErrorType::UnicodeError, self.range(start_pos))) } /// Parse an escaped character, returning the new character. fn parse_escaped_char(&mut self) -> Result, LexicalError> { let Some(first_char) = self.next_char() else { + // TODO: check when this error case happens return Err(LexicalError::new( LexicalErrorType::StringError, - self.get_pos(), + self.range(self.get_pos()), )); }; @@ -197,7 +222,7 @@ impl StringParser { .to_string() .into_boxed_str(), ), - self.get_pos(), + self.range(self.get_pos()), )); } @@ -268,6 +293,16 @@ impl StringParser { } } ch => { + if !ch.is_ascii() { + return Err(LexicalError::new( + LexicalErrorType::OtherError( + "bytes can only contain ASCII literal characters" + .to_string() + .into_boxed_str(), + ), + self.range(self.get_pos()), + )); + } value.push(char::from(*ch)); } } @@ -298,7 +333,7 @@ impl StringParser { .to_string() .into_boxed_str(), ), - self.offset + TextSize::try_from(index).unwrap(), + self.range(TextSize::try_from(index).unwrap()), )); } @@ -461,7 +496,7 @@ pub(crate) fn concatenated_strings( match string { StringType::FString(_) => has_fstring = true, StringType::Bytes(_) => byte_literal_count += 1, - StringType::Str(_) => {} + StringType::Str(_) | StringType::Invalid(_) => {} } } let has_bytes = byte_literal_count > 0; @@ -473,7 +508,7 @@ pub(crate) fn concatenated_strings( .to_string() .into_boxed_str(), ), - range.start(), + range, )); } @@ -495,7 +530,7 @@ pub(crate) fn concatenated_strings( let mut values = Vec::with_capacity(strings.len()); for string in strings { match string { - StringType::Str(value) => values.push(value), + StringType::Str(value) | StringType::Invalid(value) => values.push(value), _ => unreachable!("Unexpected non-string literal."), } } @@ -510,6 +545,7 @@ pub(crate) fn concatenated_strings( match string { StringType::FString(fstring) => parts.push(ast::FStringPart::FString(fstring)), StringType::Str(string) => parts.push(ast::FStringPart::Literal(string)), + StringType::Invalid(_) => {} StringType::Bytes(_) => unreachable!("Unexpected bytes literal."), } } @@ -521,75 +557,10 @@ pub(crate) fn concatenated_strings( .into()) } -// TODO: consolidate these with ParseError -/// An error that occurred during parsing of an f-string. -#[derive(Debug, Clone, PartialEq)] -struct FStringError { - /// The type of error that occurred. - pub(crate) error: FStringErrorType, - /// The location of the error. - pub(crate) location: TextSize, -} - -impl From for LexicalError { - fn from(err: FStringError) -> Self { - LexicalError::new(LexicalErrorType::FStringError(err.error), err.location) - } -} - -/// Represents the different types of errors that can occur during parsing of an f-string. -#[derive(Copy, Debug, Clone, PartialEq)] -pub enum FStringErrorType { - /// Expected a right brace after an opened left brace. - UnclosedLbrace, - /// An invalid conversion flag was encountered. - InvalidConversionFlag, - /// A single right brace was encountered. - SingleRbrace, - /// Unterminated string. - UnterminatedString, - /// Unterminated triple-quoted string. - UnterminatedTripleQuotedString, - // TODO(dhruvmanila): The parser can't catch all cases of this error, but - // wherever it can, we'll display the correct error message. - /// A lambda expression without parentheses was encountered. - LambdaWithoutParentheses, -} - -impl std::fmt::Display for FStringErrorType { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - use FStringErrorType::{ - InvalidConversionFlag, LambdaWithoutParentheses, SingleRbrace, UnclosedLbrace, - UnterminatedString, UnterminatedTripleQuotedString, - }; - match self { - UnclosedLbrace => write!(f, "expecting '}}'"), - InvalidConversionFlag => write!(f, "invalid conversion character"), - SingleRbrace => write!(f, "single '}}' is not allowed"), - UnterminatedString => write!(f, "unterminated string"), - UnterminatedTripleQuotedString => write!(f, "unterminated triple-quoted string"), - LambdaWithoutParentheses => { - write!(f, "lambda expressions are not allowed without parentheses") - } - } - } -} - -impl From for crate::parser::LalrpopError { - fn from(err: FStringError) -> Self { - lalrpop_util::ParseError::User { - error: LexicalError::new(LexicalErrorType::FStringError(err.error), err.location), - } - } -} - #[cfg(test)] mod tests { use crate::lexer::LexicalErrorType; - use crate::parser::parse_suite; - use crate::{ParseErrorType, Suite}; - - use super::*; + use crate::{parse_suite, FStringErrorType, ParseErrorType, Suite}; const WINDOWS_EOL: &str = "\r\n"; const MAC_EOL: &str = "\r"; @@ -675,6 +646,7 @@ mod tests { parse_suite(source) .map_err(|e| match e.error { ParseErrorType::Lexical(LexicalErrorType::FStringError(e)) => e, + ParseErrorType::FStringError(e) => e, e => unreachable!("Expected FStringError: {:?}", e), }) .expect_err("Expected error") @@ -689,10 +661,12 @@ mod tests { parse_fstring_error("f'{lambda x:{x}}'"), LambdaWithoutParentheses ); - assert_eq!( - parse_fstring_error("f'{lambda x: {x}}'"), - LambdaWithoutParentheses - ); + // NOTE: The parser produces the `LambdaWithoutParentheses` for this case, but + // since the parser only return the first error to maintain compatibility with + // the rest of the codebase, this test case fails. The `LambdaWithoutParentheses` + // error appears after the unexpected `FStringMiddle` token, which is between the + // `:` and the `{`. + // assert_eq!(parse_fstring_error("f'{lambda x: {x}}'"), LambdaWithoutParentheses); assert!(parse_suite(r#"f"{class}""#,).is_err()); } diff --git a/crates/ruff_python_parser/src/token.rs b/crates/ruff_python_parser/src/token.rs index d3d51452cf9c0..e25255d198fb3 100644 --- a/crates/ruff_python_parser/src/token.rs +++ b/crates/ruff_python_parser/src/token.rs @@ -4,12 +4,13 @@ //! loosely based on the token definitions found in the [CPython source]. //! //! [CPython source]: https://github.com/python/cpython/blob/dfc2e065a2e71011017077e549cd2f9bf4944c54/Include/internal/pycore_token.h; -use crate::Mode; -use ruff_python_ast::{Int, IpyEscapeKind}; +use ruff_python_ast::{BoolOp, Int, IpyEscapeKind, Operator, UnaryOp}; use ruff_text_size::TextSize; use std::fmt; +use crate::Mode; + /// The set of tokens the Python source code can be tokenized in. #[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum Tok { @@ -220,6 +221,7 @@ pub enum Tok { With, Yield, + Unknown, // RustPython specific. StartModule, StartExpression, @@ -239,9 +241,9 @@ impl fmt::Display for Tok { #[allow(clippy::enum_glob_use)] use Tok::*; match self { - Name { name } => write!(f, "'{name}'"), - Int { value } => write!(f, "'{value}'"), - Float { value } => write!(f, "'{value}'"), + Name { name } => write!(f, "{name}"), + Int { value } => write!(f, "{value}"), + Float { value } => write!(f, "{value}"), Complex { real, imag } => write!(f, "{real}j{imag}"), String { value, @@ -262,94 +264,95 @@ impl fmt::Display for Tok { StartModule => f.write_str("StartProgram"), StartExpression => f.write_str("StartExpression"), EndOfFile => f.write_str("EOF"), - Question => f.write_str("'?'"), - Exclamation => f.write_str("'!'"), - Lpar => f.write_str("'('"), - Rpar => f.write_str("')'"), - Lsqb => f.write_str("'['"), - Rsqb => f.write_str("']'"), - Colon => f.write_str("':'"), - Comma => f.write_str("','"), + Question => f.write_str("?"), + Exclamation => f.write_str("!"), + Lpar => f.write_str("("), + Rpar => f.write_str(")"), + Lsqb => f.write_str("["), + Rsqb => f.write_str("]"), + Colon => f.write_str(":"), + Comma => f.write_str(","), Comment(value) => f.write_str(value), - Semi => f.write_str("';'"), - Plus => f.write_str("'+'"), - Minus => f.write_str("'-'"), - Star => f.write_str("'*'"), - Slash => f.write_str("'/'"), - Vbar => f.write_str("'|'"), - Amper => f.write_str("'&'"), - Less => f.write_str("'<'"), - Greater => f.write_str("'>'"), - Equal => f.write_str("'='"), - Dot => f.write_str("'.'"), - Percent => f.write_str("'%'"), - Lbrace => f.write_str("'{'"), - Rbrace => f.write_str("'}'"), - EqEqual => f.write_str("'=='"), - NotEqual => f.write_str("'!='"), - LessEqual => f.write_str("'<='"), - GreaterEqual => f.write_str("'>='"), - Tilde => f.write_str("'~'"), - CircumFlex => f.write_str("'^'"), - LeftShift => f.write_str("'<<'"), - RightShift => f.write_str("'>>'"), - DoubleStar => f.write_str("'**'"), - DoubleStarEqual => f.write_str("'**='"), - PlusEqual => f.write_str("'+='"), - MinusEqual => f.write_str("'-='"), - StarEqual => f.write_str("'*='"), - SlashEqual => f.write_str("'/='"), - PercentEqual => f.write_str("'%='"), - AmperEqual => f.write_str("'&='"), - VbarEqual => f.write_str("'|='"), - CircumflexEqual => f.write_str("'^='"), - LeftShiftEqual => f.write_str("'<<='"), - RightShiftEqual => f.write_str("'>>='"), - DoubleSlash => f.write_str("'//'"), - DoubleSlashEqual => f.write_str("'//='"), - At => f.write_str("'@'"), - AtEqual => f.write_str("'@='"), - Rarrow => f.write_str("'->'"), - Ellipsis => f.write_str("'...'"), - False => f.write_str("'False'"), - None => f.write_str("'None'"), - True => f.write_str("'True'"), - And => f.write_str("'and'"), - As => f.write_str("'as'"), - Assert => f.write_str("'assert'"), - Async => f.write_str("'async'"), - Await => f.write_str("'await'"), - Break => f.write_str("'break'"), - Class => f.write_str("'class'"), - Continue => f.write_str("'continue'"), - Def => f.write_str("'def'"), - Del => f.write_str("'del'"), - Elif => f.write_str("'elif'"), - Else => f.write_str("'else'"), - Except => f.write_str("'except'"), - Finally => f.write_str("'finally'"), - For => f.write_str("'for'"), - From => f.write_str("'from'"), - Global => f.write_str("'global'"), - If => f.write_str("'if'"), - Import => f.write_str("'import'"), - In => f.write_str("'in'"), - Is => f.write_str("'is'"), - Lambda => f.write_str("'lambda'"), - Nonlocal => f.write_str("'nonlocal'"), - Not => f.write_str("'not'"), - Or => f.write_str("'or'"), - Pass => f.write_str("'pass'"), - Raise => f.write_str("'raise'"), - Return => f.write_str("'return'"), - Try => f.write_str("'try'"), - While => f.write_str("'while'"), - Match => f.write_str("'match'"), - Type => f.write_str("'type'"), - Case => f.write_str("'case'"), - With => f.write_str("'with'"), - Yield => f.write_str("'yield'"), - ColonEqual => f.write_str("':='"), + Semi => f.write_str(";"), + Plus => f.write_str("+"), + Minus => f.write_str("-"), + Star => f.write_str("*"), + Slash => f.write_str("/"), + Vbar => f.write_str("|"), + Amper => f.write_str("&"), + Less => f.write_str("<"), + Greater => f.write_str(">"), + Equal => f.write_str("="), + Dot => f.write_str("."), + Percent => f.write_str("%"), + Lbrace => f.write_str("{"), + Rbrace => f.write_str("}"), + EqEqual => f.write_str("=="), + NotEqual => f.write_str("!="), + LessEqual => f.write_str("<="), + GreaterEqual => f.write_str(">="), + Tilde => f.write_str("~"), + CircumFlex => f.write_str("^"), + LeftShift => f.write_str("<<"), + RightShift => f.write_str(">>"), + DoubleStar => f.write_str("**"), + DoubleStarEqual => f.write_str("**="), + PlusEqual => f.write_str("+="), + MinusEqual => f.write_str("-="), + StarEqual => f.write_str("*="), + SlashEqual => f.write_str("/="), + PercentEqual => f.write_str("%="), + AmperEqual => f.write_str("&="), + VbarEqual => f.write_str("|="), + CircumflexEqual => f.write_str("^="), + LeftShiftEqual => f.write_str("<<="), + RightShiftEqual => f.write_str(">>="), + DoubleSlash => f.write_str("//"), + DoubleSlashEqual => f.write_str("//="), + At => f.write_str("@"), + AtEqual => f.write_str("@="), + Rarrow => f.write_str("->"), + Ellipsis => f.write_str("..."), + False => f.write_str("False"), + None => f.write_str("None"), + True => f.write_str("True"), + And => f.write_str("and"), + As => f.write_str("as"), + Assert => f.write_str("assert"), + Async => f.write_str("async"), + Await => f.write_str("await"), + Break => f.write_str("break"), + Class => f.write_str("class"), + Continue => f.write_str("continue"), + Def => f.write_str("def"), + Del => f.write_str("del"), + Elif => f.write_str("elif"), + Else => f.write_str("else"), + Except => f.write_str("except"), + Finally => f.write_str("finally"), + For => f.write_str("for"), + From => f.write_str("from"), + Global => f.write_str("global"), + If => f.write_str("if"), + Import => f.write_str("import"), + In => f.write_str("in"), + Is => f.write_str("is"), + Lambda => f.write_str("lambda"), + Nonlocal => f.write_str("nonlocal"), + Not => f.write_str("not"), + Or => f.write_str("or"), + Pass => f.write_str("pass"), + Raise => f.write_str("raise"), + Return => f.write_str("return"), + Try => f.write_str("try"), + While => f.write_str("while"), + Match => f.write_str("match"), + Type => f.write_str("type"), + Case => f.write_str("case"), + With => f.write_str("with"), + Yield => f.write_str("yield"), + ColonEqual => f.write_str(":="), + Unknown => f.write_str(">"), } } } @@ -450,7 +453,6 @@ impl StringKind { } } -// TODO move to ruff_python_parser? #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub enum TokenKind { /// Token value for a name, commonly known as an identifier. @@ -626,6 +628,7 @@ pub enum TokenKind { With, Yield, + Unknown, // RustPython specific. StartModule, StartInteractive, @@ -797,6 +800,27 @@ impl TokenKind { matches!(self, TokenKind::Match | TokenKind::Case) } + #[inline] + pub const fn is_compare_operator(&self) -> bool { + matches!( + self, + TokenKind::Not + | TokenKind::In + | TokenKind::Is + | TokenKind::EqEqual + | TokenKind::NotEqual + | TokenKind::Less + | TokenKind::LessEqual + | TokenKind::Greater + | TokenKind::GreaterEqual + ) + } + + #[inline] + pub const fn is_bool_operator(&self) -> bool { + matches!(self, TokenKind::And | TokenKind::Or) + } + pub const fn from_token(token: &Tok) -> Self { match token { Tok::Name { .. } => TokenKind::Name, @@ -901,6 +925,7 @@ impl TokenKind { Tok::Type => TokenKind::Type, Tok::With => TokenKind::With, Tok::Yield => TokenKind::Yield, + Tok::Unknown => TokenKind::Unknown, Tok::StartModule => TokenKind::StartModule, Tok::StartExpression => TokenKind::StartExpression, } @@ -913,6 +938,69 @@ impl From<&Tok> for TokenKind { } } +impl From for TokenKind { + fn from(value: Tok) -> Self { + Self::from_token(&value) + } +} + +impl TryFrom for Operator { + type Error = (); + + fn try_from(value: TokenKind) -> Result { + Ok(match value { + TokenKind::At | TokenKind::AtEqual => Operator::MatMult, + TokenKind::Plus | TokenKind::PlusEqual => Operator::Add, + TokenKind::Star | TokenKind::StarEqual => Operator::Mult, + TokenKind::Vbar | TokenKind::VbarEqual => Operator::BitOr, + TokenKind::Minus | TokenKind::MinusEqual => Operator::Sub, + TokenKind::Slash | TokenKind::SlashEqual => Operator::Div, + TokenKind::Amper | TokenKind::AmperEqual => Operator::BitAnd, + TokenKind::Percent | TokenKind::PercentEqual => Operator::Mod, + TokenKind::DoubleStar | TokenKind::DoubleStarEqual => Operator::Pow, + TokenKind::LeftShift | TokenKind::LeftShiftEqual => Operator::LShift, + TokenKind::CircumFlex | TokenKind::CircumflexEqual => Operator::BitXor, + TokenKind::RightShift | TokenKind::RightShiftEqual => Operator::RShift, + TokenKind::DoubleSlash | TokenKind::DoubleSlashEqual => Operator::FloorDiv, + _ => return Err(()), + }) + } +} + +impl TryFrom for BoolOp { + type Error = (); + + fn try_from(value: TokenKind) -> Result { + Ok(match value { + TokenKind::And => BoolOp::And, + TokenKind::Or => BoolOp::Or, + _ => return Err(()), + }) + } +} + +impl TryFrom<&Tok> for UnaryOp { + type Error = (); + + fn try_from(value: &Tok) -> Result { + TokenKind::from_token(value).try_into() + } +} + +impl TryFrom for UnaryOp { + type Error = (); + + fn try_from(value: TokenKind) -> Result { + Ok(match value { + TokenKind::Plus => UnaryOp::UAdd, + TokenKind::Minus => UnaryOp::USub, + TokenKind::Tilde => UnaryOp::Invert, + TokenKind::Not => UnaryOp::Not, + _ => return Err(()), + }) + } +} + #[cfg(target_pointer_width = "64")] mod sizes { use crate::lexer::{LexicalError, LexicalErrorType}; diff --git a/crates/ruff_python_parser/src/token_set.rs b/crates/ruff_python_parser/src/token_set.rs new file mode 100644 index 0000000000000..843fe53faa04a --- /dev/null +++ b/crates/ruff_python_parser/src/token_set.rs @@ -0,0 +1,52 @@ +use crate::TokenKind; + +/// A bit-set of `TokenKind`s +#[derive(Clone, Copy)] +pub(crate) struct TokenSet(u128); + +impl TokenSet { + pub(crate) const fn new(kinds: [TokenKind; N]) -> TokenSet { + let mut res = 0u128; + let mut i = 0usize; + + while i < N { + let kind = kinds[i]; + res |= mask(kind); + i += 1; + } + TokenSet(res) + } + + pub(crate) const fn union(self, other: TokenSet) -> TokenSet { + TokenSet(self.0 | other.0) + } + + pub(crate) const fn remove(self, kind: TokenKind) -> TokenSet { + TokenSet(self.0 & !mask(kind)) + } + + pub(crate) const fn contains(&self, kind: TokenKind) -> bool { + self.0 & mask(kind) != 0 + } +} + +const fn mask(kind: TokenKind) -> u128 { + 1u128 << (kind as usize) +} + +impl From<[TokenKind; N]> for TokenSet { + fn from(kinds: [TokenKind; N]) -> Self { + TokenSet::new(kinds) + } +} + +#[test] +fn token_set_works_for_tokens() { + use crate::TokenKind::*; + let mut ts = TokenSet::new([EndOfFile, Name]); + assert!(ts.contains(EndOfFile)); + assert!(ts.contains(Name)); + assert!(!ts.contains(Plus)); + ts = ts.remove(Name); + assert!(!ts.contains(Name)); +} diff --git a/crates/ruff_python_parser/src/token_source.rs b/crates/ruff_python_parser/src/token_source.rs index b133ee5ff227d..e80a2aaeffaec 100644 --- a/crates/ruff_python_parser/src/token_source.rs +++ b/crates/ruff_python_parser/src/token_source.rs @@ -1,18 +1,78 @@ -use crate::lexer::LexResult; -use crate::Tok; +use crate::lexer::{LexResult, LexicalError, Spanned}; +use crate::{Tok, TokenKind}; +use ruff_text_size::{TextRange, TextSize}; use std::iter::FusedIterator; #[derive(Clone, Debug)] pub(crate) struct TokenSource { tokens: std::vec::IntoIter, + errors: Vec, } impl TokenSource { pub(crate) fn new(tokens: Vec) -> Self { Self { tokens: tokens.into_iter(), + errors: Vec::new(), } } + + /// Returns the position of the current token. + /// + /// This is the position before any whitespace or comments. + pub(crate) fn position(&self) -> Option { + let first = self.tokens.as_slice().first()?; + + let range = match first { + Ok((_, range)) => *range, + Err(error) => error.location(), + }; + + Some(range.start()) + } + + /// Returns the end of the last token + pub(crate) fn end(&self) -> Option { + let last = self.tokens.as_slice().last()?; + + let range = match last { + Ok((_, range)) => *range, + Err(error) => error.location(), + }; + + Some(range.end()) + } + + pub(crate) fn peek_nth(&self, mut n: usize) -> Option<(TokenKind, TextRange)> { + let mut iter = self.tokens.as_slice().iter(); + + loop { + let next = iter.next()?; + + if next.as_ref().is_ok_and(is_trivia) { + continue; + } + + if n == 0 { + break Some(match next { + Ok((token, range)) => (TokenKind::from_token(token), *range), + Err(error) => (TokenKind::Unknown, error.location()), + }); + } + + n -= 1; + } + } + + pub(crate) fn finish(self) -> Vec { + assert_eq!( + self.tokens.as_slice(), + &[], + "TokenSource was not fully consumed." + ); + + self.errors + } } impl FromIterator for TokenSource { @@ -23,24 +83,34 @@ impl FromIterator for TokenSource { } impl Iterator for TokenSource { - type Item = LexResult; + type Item = Spanned; #[inline] fn next(&mut self) -> Option { loop { let next = self.tokens.next()?; - if is_trivia(&next) { - continue; - } + match next { + Ok(token) => { + if is_trivia(&token) { + continue; + } + + break Some(token); + } - break Some(next); + Err(error) => { + let location = error.location(); + self.errors.push(error); + break Some((Tok::Unknown, location)); + } + } } } } impl FusedIterator for TokenSource {} -const fn is_trivia(result: &LexResult) -> bool { - matches!(result, Ok((Tok::Comment(_) | Tok::NonLogicalNewline, _))) +const fn is_trivia(result: &Spanned) -> bool { + matches!(result, (Tok::Comment(_) | Tok::NonLogicalNewline, _)) } diff --git a/crates/ruff_python_parser/src/typing.rs b/crates/ruff_python_parser/src/typing.rs index 18efe4041fd8a..477f4b466b752 100644 --- a/crates/ruff_python_parser/src/typing.rs +++ b/crates/ruff_python_parser/src/typing.rs @@ -5,7 +5,7 @@ use ruff_python_ast::str; use ruff_python_ast::Expr; use ruff_text_size::{TextLen, TextRange}; -#[derive(is_macro::Is, Copy, Clone)] +#[derive(is_macro::Is, Copy, Clone, Debug)] pub enum AnnotationKind { /// The annotation is defined as part a simple string literal, /// e.g. `x: "List[int]" = []`. Annotations within simple literals diff --git a/crates/ruff_python_parser/tests/fixtures.rs b/crates/ruff_python_parser/tests/fixtures.rs new file mode 100644 index 0000000000000..74e8563111ec6 --- /dev/null +++ b/crates/ruff_python_parser/tests/fixtures.rs @@ -0,0 +1,244 @@ +use std::cmp::Ordering; +use std::fmt::{Formatter, Write}; +use std::fs; +use std::path::Path; + +use annotate_snippets::display_list::{DisplayList, FormatOptions}; +use annotate_snippets::snippet::{AnnotationType, Slice, Snippet, SourceAnnotation}; +use ruff_python_ast::visitor::preorder::{walk_module, PreorderVisitor, TraversalSignal}; +use ruff_python_ast::{AnyNodeRef, Mod}; + +use ruff_python_parser::{Mode, ParseErrorType, Program}; +use ruff_source_file::{LineIndex, OneIndexed, SourceCode}; +use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; + +#[test] +fn invalid_syntax() { + let test_file = |input_path: &Path| { + let source = fs::read_to_string(input_path).expect("Expected test file to exist"); + + let program = Program::parse_str(&source, Mode::Module); + + assert_ne!( + &program.parse_errors, + &[], + "{input_path:?}: Expected parser to generate at least one syntax error for a program containing syntax errors." + ); + + validate_ast(&program.ast, source.text_len(), input_path); + + let mut output = String::new(); + writeln!(&mut output, "## AST").unwrap(); + writeln!(&mut output, "\n ```\n{:#?}\n```", &program.ast).unwrap(); + + writeln!(&mut output, "## Errors\n").unwrap(); + + let line_index = LineIndex::from_source_text(&source); + let source_code = SourceCode::new(&source, &line_index); + + for error in &program.parse_errors { + writeln!( + &mut output, + "{}\n", + CodeFrame { + range: error.location, + error, + source_code: &source_code, + } + ) + .unwrap(); + } + + insta::with_settings!({ + omit_expression => true, + input_file => input_path, + prepend_module_to_snapshot => false, + }, { + insta::assert_snapshot!(output); + }); + }; + + insta::glob!("../resources", "invalid/**/*.{py,pyi}", test_file); +} + +#[test] +fn valid_syntax() { + let test_file = |input_path: &Path| { + let source = fs::read_to_string(input_path).expect("Expected test file to exist"); + + let program = Program::parse_str(&source, Mode::Module); + + if !program.parse_errors.is_empty() { + let line_index = LineIndex::from_source_text(&source); + let source_code = SourceCode::new(&source, &line_index); + + let mut message = "Expected no syntax errors for a valid program but the parser generated the following errors:\n".to_string(); + + for error in &program.parse_errors { + writeln!( + &mut message, + "{}\n", + CodeFrame { + range: error.location, + error, + source_code: &source_code, + } + ) + .unwrap(); + } + + panic!("{input_path:?}: {message}"); + } + + validate_ast(&program.ast, source.text_len(), input_path); + + let mut output = String::new(); + writeln!(&mut output, "## AST").unwrap(); + writeln!(&mut output, "\n ```\n{:#?}\n```", &program.ast).unwrap(); + + insta::with_settings!({ + omit_expression => true, + input_file => input_path, + prepend_module_to_snapshot => false, + }, { + insta::assert_snapshot!(output); + }); + }; + + insta::glob!("../resources", "valid/**/*.{py,pyi}", test_file); +} + +struct CodeFrame<'a> { + range: TextRange, + error: &'a ParseErrorType, + source_code: &'a SourceCode<'a, 'a>, +} + +impl std::fmt::Display for CodeFrame<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + // Copied and modified from ruff_linter/src/message/text.rs + let content_start_index = self.source_code.line_index(self.range.start()); + let mut start_index = content_start_index.saturating_sub(2); + + // Trim leading empty lines. + while start_index < content_start_index { + if !self.source_code.line_text(start_index).trim().is_empty() { + break; + } + start_index = start_index.saturating_add(1); + } + + let content_end_index = self.source_code.line_index(self.range.end()); + let mut end_index = content_end_index + .saturating_add(2) + .min(OneIndexed::from_zero_indexed(self.source_code.line_count())); + + // Trim trailing empty lines. + while end_index > content_end_index { + if !self.source_code.line_text(end_index).trim().is_empty() { + break; + } + + end_index = end_index.saturating_sub(1); + } + + let start_offset = self.source_code.line_start(start_index); + let end_offset = self.source_code.line_end(end_index); + + let annotation_range = self.range - start_offset; + let source = self + .source_code + .slice(TextRange::new(start_offset, end_offset)); + + let start_char = source[TextRange::up_to(annotation_range.start())] + .chars() + .count(); + + let char_length = source[annotation_range].chars().count(); + let label = format!("Syntax Error: {error}", error = self.error); + + let snippet = Snippet { + title: None, + slices: vec![Slice { + source, + line_start: start_index.get(), + annotations: vec![SourceAnnotation { + label: &label, + annotation_type: AnnotationType::Error, + range: (start_char, start_char + char_length), + }], + // The origin (file name, line number, and column number) is already encoded + // in the `label`. + origin: None, + fold: false, + }], + footer: Vec::new(), + opt: FormatOptions::default(), + }; + + writeln!(f, "{message}", message = DisplayList::from(snippet)) + } +} + +/// Verifies that: +/// * the range of the parent node fully encloses all its child nodes +/// * the ranges are strictly increasing when traversing the nodes in pre-order. +/// * all ranges are within the length of the source code. +fn validate_ast(root: &Mod, source_len: TextSize, test_path: &Path) { + walk_module(&mut ValidateAstVisitor::new(source_len, test_path), root); +} + +#[derive(Debug)] +struct ValidateAstVisitor<'a> { + parents: Vec>, + previous: Option>, + source_length: TextSize, + test_path: &'a Path, +} + +impl<'a> ValidateAstVisitor<'a> { + fn new(source_length: TextSize, test_path: &'a Path) -> Self { + Self { + parents: Vec::new(), + previous: None, + source_length, + test_path, + } + } +} + +impl<'ast> PreorderVisitor<'ast> for ValidateAstVisitor<'ast> { + fn enter_node(&mut self, node: AnyNodeRef<'ast>) -> TraversalSignal { + assert!( + node.end() <= self.source_length, + "{path}: The range of the node exceeds the length of the source code. Node: {node:#?}", + path = self.test_path.display() + ); + + if let Some(previous) = self.previous { + assert_ne!(previous.range().ordering(node.range()), Ordering::Greater, + "{path}: The ranges of the nodes are not strictly increasing when traversing the AST in pre-order.\nPrevious node: {previous:#?}\n\nCurrent node: {node:#?}\n\nRoot: {root:#?}", + path = self.test_path.display(), + root = self.parents.first() + ); + } + + if let Some(parent) = self.parents.last() { + assert!(parent.range().contains_range(node.range()), + "{path}: The range of the parent node does not fully enclose the range of the child node.\nParent node: {parent:#?}\n\nChild node: {node:#?}\n\nRoot: {root:#?}", + path = self.test_path.display(), + root = self.parents.first() + ); + } + + self.parents.push(node); + + TraversalSignal::Traverse + } + + fn leave_node(&mut self, node: AnyNodeRef<'ast>) { + self.parents.pop().expect("Expected tree to be balanced"); + + self.previous = Some(node); + } +} diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__call_invalid_argument_ordering.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__call_invalid_argument_ordering.py.snap new file mode 100644 index 0000000000000..9f7837b1ddf43 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__call_invalid_argument_ordering.py.snap @@ -0,0 +1,196 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/expressions/call_invalid_argument_ordering.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..67, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Call( + ExprCall { + range: 0..10, + func: Name( + ExprName { + range: 0..1, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1..10, + args: [ + Name( + ExprName { + range: 8..9, + id: "c", + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 2..6, + arg: Some( + Identifier { + id: "b", + range: 2..3, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 4..6, + value: Int( + 20, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..22, + value: Call( + ExprCall { + range: 12..22, + func: Name( + ExprName { + range: 12..13, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 13..22, + args: [ + Starred( + ExprStarred { + range: 19..21, + value: Name( + ExprName { + range: 20..21, + id: "c", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 14..17, + arg: None, + value: Name( + ExprName { + range: 16..17, + id: "b", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 53..66, + value: Call( + ExprCall { + range: 53..66, + func: Name( + ExprName { + range: 53..54, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 54..66, + args: [], + keywords: [ + Keyword { + range: 55..59, + arg: Some( + Identifier { + id: "a", + range: 55..56, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 57..59, + value: Int( + 20, + ), + }, + ), + }, + Keyword { + range: 61..65, + arg: Some( + Identifier { + id: "a", + range: 61..62, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 63..65, + value: Int( + 30, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | f(b=20, c) + | ^ Syntax Error: positional argument follows keyword argument unpacking +2 | +3 | f(**b, *c) + | + + + | +1 | f(b=20, c) +2 | +3 | f(**b, *c) + | ^^ Syntax Error: iterable argument unpacking follows keyword argument unpacking +4 | +5 | # Duplicate keyword argument + | + + + | +5 | # Duplicate keyword argument +6 | f(a=20, a=30) + | ^^^^ Syntax Error: keyword argument repeated: a + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_identifiers.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_identifiers.py.snap new file mode 100644 index 0000000000000..ef4b23415f52b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_identifiers.py.snap @@ -0,0 +1,141 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/expressions/emoji_identifiers.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..64, + body: [ + Assign( + StmtAssign { + range: 0..5, + targets: [ + Name( + ExprName { + range: 0..1, + id: "a", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 5..5, + id: "", + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 32..37, + targets: [ + Name( + ExprName { + range: 32..33, + id: "a", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 37..37, + id: "", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 42..43, + value: UnaryOp( + ExprUnaryOp { + range: 42..43, + op: UAdd, + operand: Name( + ExprName { + range: 43..43, + id: "", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a = (🐶 + | ^^ Syntax Error: Got unexpected token 🐶 +2 | # comment 🐶 +3 | ) + | + + + | +1 | a = (🐶 +2 | # comment 🐶 +3 | ) + | ^ Syntax Error: Expected a statement +4 | +5 | a = (🐶 + + | + + + | +1 | a = (🐶 +2 | # comment 🐶 +3 | ) + | ^ Syntax Error: Expected a statement +4 | +5 | a = (🐶 + +6 | # comment + | + + + | +3 | ) +4 | +5 | a = (🐶 + + | ^^ Syntax Error: Got unexpected token 🐶 +6 | # comment +7 | 🐶) + | + + + | +5 | a = (🐶 + +6 | # comment +7 | 🐶) + | ^^ Syntax Error: Got unexpected token 🐶 + | + + + | +5 | a = (🐶 + +6 | # comment +7 | 🐶) + | ^ Syntax Error: Expected a statement + | + + + | +5 | a = (🐶 + +6 | # comment +7 | 🐶) + | ^ Syntax Error: Expected a statement + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_statement.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_statement.py.snap new file mode 100644 index 0000000000000..beac073bb2c99 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_statement.py.snap @@ -0,0 +1,23 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/expressions/emoji_statement.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..5, + body: [], + }, +) +``` +## Errors + + | +1 | 👍 + | ^^ Syntax Error: Got unexpected token 👍 + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_default_parameters.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_default_parameters.py.snap new file mode 100644 index 0000000000000..edd8adf34bd53 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_default_parameters.py.snap @@ -0,0 +1,99 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/expressions/lambda_default_parameters.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..93, + body: [ + Expr( + StmtExpr { + range: 72..92, + value: Lambda( + ExprLambda { + range: 72..92, + parameters: Some( + Parameters { + range: 79..89, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 79..80, + parameter: Parameter { + range: 79..80, + name: Identifier { + id: "a", + range: 79..80, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 82..86, + parameter: Parameter { + range: 82..83, + name: Identifier { + id: "b", + range: 82..83, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 84..86, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 88..89, + parameter: Parameter { + range: 88..89, + name: Identifier { + id: "c", + range: 88..89, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 91..92, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # TODO(micha): The offset of the generated error message is off by one. +2 | lambda a, b=20, c: 1 + | ^ Syntax Error: non-default argument follows default argument + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_duplicate_parameters.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_duplicate_parameters.py.snap new file mode 100644 index 0000000000000..52187863813bf --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_duplicate_parameters.py.snap @@ -0,0 +1,333 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/expressions/lambda_duplicate_parameters.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..91, + body: [ + Expr( + StmtExpr { + range: 0..14, + value: Lambda( + ExprLambda { + range: 0..14, + parameters: Some( + Parameters { + range: 7..11, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 7..8, + parameter: Parameter { + range: 7..8, + name: Identifier { + id: "a", + range: 7..8, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 10..11, + parameter: Parameter { + range: 10..11, + name: Identifier { + id: "a", + range: 10..11, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..33, + value: Lambda( + ExprLambda { + range: 16..33, + parameters: Some( + Parameters { + range: 23..30, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 23..24, + parameter: Parameter { + range: 23..24, + name: Identifier { + id: "a", + range: 23..24, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 29..30, + parameter: Parameter { + range: 29..30, + name: Identifier { + id: "a", + range: 29..30, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 32..33, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 35..52, + value: Lambda( + ExprLambda { + range: 35..52, + parameters: Some( + Parameters { + range: 42..49, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 42..43, + parameter: Parameter { + range: 42..43, + name: Identifier { + id: "a", + range: 42..43, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 45..49, + parameter: Parameter { + range: 45..46, + name: Identifier { + id: "a", + range: 45..46, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 47..49, + value: Int( + 20, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..69, + value: Lambda( + ExprLambda { + range: 54..69, + parameters: Some( + Parameters { + range: 61..66, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 61..62, + parameter: Parameter { + range: 61..62, + name: Identifier { + id: "a", + range: 61..62, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 65..66, + name: Identifier { + id: "a", + range: 65..66, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 68..69, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 71..90, + value: Lambda( + ExprLambda { + range: 71..90, + parameters: Some( + Parameters { + range: 78..87, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 78..79, + parameter: Parameter { + range: 78..79, + name: Identifier { + id: "a", + range: 78..79, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 86..87, + name: Identifier { + id: "a", + range: 86..87, + }, + annotation: None, + }, + ), + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | lambda a, a: 1 + | ^ Syntax Error: duplicate argument 'a' in function definition +2 | +3 | lambda a, *, a: 1 + | + + + | +1 | lambda a, a: 1 +2 | +3 | lambda a, *, a: 1 + | ^ Syntax Error: duplicate argument 'a' in function definition +4 | +5 | lambda a, a=20: 1 + | + + + | +3 | lambda a, *, a: 1 +4 | +5 | lambda a, a=20: 1 + | ^ Syntax Error: duplicate argument 'a' in function definition +6 | +7 | lambda a, *a: 1 + | + + + | +5 | lambda a, a=20: 1 +6 | +7 | lambda a, *a: 1 + | ^ Syntax Error: duplicate argument 'a' in function definition +8 | +9 | lambda a, *, **a: 1 + | + + + | +7 | lambda a, *a: 1 +8 | +9 | lambda a, *, **a: 1 + | ^ Syntax Error: duplicate argument 'a' in function definition + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_keyword_target.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_keyword_target.py.snap new file mode 100644 index 0000000000000..259fc08d6bd14 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_keyword_target.py.snap @@ -0,0 +1,149 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/assignment_keyword_target.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..44, + body: [ + Assign( + StmtAssign { + range: 0..12, + targets: [ + Name( + ExprName { + range: 0..1, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 4..8, + id: "pass", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 11..12, + id: "c", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 14..19, + value: BinOp( + ExprBinOp { + range: 14..19, + left: Name( + ExprName { + range: 14..15, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 18..19, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 21..37, + targets: [ + Name( + ExprName { + range: 21..22, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 25..26, + id: "b", + ctx: Store, + }, + ), + Name( + ExprName { + range: 29..33, + id: "pass", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 36..37, + id: "c", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 39..44, + value: BinOp( + ExprBinOp { + range: 39..44, + left: Name( + ExprName { + range: 39..40, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 43..44, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a = pass = c + | ^^^^ Syntax Error: Identifier expected. 'pass' is a keyword that cannot be used here. +2 | +3 | a + b + | + + + | +3 | a + b +4 | +5 | a = b = pass = c + | ^^^^ Syntax Error: Identifier expected. 'pass' is a keyword that cannot be used here. +6 | +7 | a + b + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_missing_target.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_missing_target.py.snap new file mode 100644 index 0000000000000..3079bf0e339ed --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_missing_target.py.snap @@ -0,0 +1,79 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/assignment_missing_target.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..19, + body: [ + Assign( + StmtAssign { + range: 0..7, + targets: [ + Name( + ExprName { + range: 0..1, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 3..3, + id: "", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 6..7, + id: "c", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 9..14, + value: BinOp( + ExprBinOp { + range: 9..14, + left: Name( + ExprName { + range: 9..10, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 13..14, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a = = c + | ^ Syntax Error: Expression expected. +2 | +3 | a + b + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_missing_value.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_missing_value.py.snap new file mode 100644 index 0000000000000..fe696a6f0e9d7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__assignment_missing_value.py.snap @@ -0,0 +1,72 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/assignment_missing_value.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..10, + body: [ + Assign( + StmtAssign { + range: 0..3, + targets: [ + Name( + ExprName { + range: 0..1, + id: "a", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 3..3, + id: "", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..10, + value: BinOp( + ExprBinOp { + range: 5..10, + left: Name( + ExprName { + range: 5..6, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 9..10, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a = + | ^ Syntax Error: Expression expected. +2 | +3 | a + b + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_default_parameters.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_default_parameters.py.snap new file mode 100644 index 0000000000000..468732aecfe6b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_default_parameters.py.snap @@ -0,0 +1,99 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/function_default_parameters.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..95, + body: [ + FunctionDef( + StmtFunctionDef { + range: 71..94, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f", + range: 75..76, + }, + type_params: None, + parameters: Parameters { + range: 76..88, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 77..78, + parameter: Parameter { + range: 77..78, + name: Identifier { + id: "a", + range: 77..78, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 80..84, + parameter: Parameter { + range: 80..81, + name: Identifier { + id: "b", + range: 80..81, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 82..84, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 86..87, + parameter: Parameter { + range: 86..87, + name: Identifier { + id: "c", + range: 86..87, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 90..94, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # TODO(micha): The range of the generated error message is off by one. +2 | def f(a, b=20, c): pass + | ^ Syntax Error: non-default argument follows default argument + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_duplicate_parameters.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_duplicate_parameters.py.snap new file mode 100644 index 0000000000000..721f681aacce0 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_duplicate_parameters.py.snap @@ -0,0 +1,403 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/function_duplicate_parameters.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..225, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..17, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f", + range: 4..5, + }, + type_params: None, + parameters: Parameters { + range: 5..11, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 6..7, + parameter: Parameter { + range: 6..7, + name: Identifier { + id: "a", + range: 6..7, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 9..10, + parameter: Parameter { + range: 9..10, + name: Identifier { + id: "a", + range: 9..10, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 13..17, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 19..40, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f2", + range: 23..25, + }, + type_params: None, + parameters: Parameters { + range: 25..34, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 26..27, + parameter: Parameter { + range: 26..27, + name: Identifier { + id: "a", + range: 26..27, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 32..33, + parameter: Parameter { + range: 32..33, + name: Identifier { + id: "a", + range: 32..33, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 36..40, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 42..63, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f3", + range: 46..48, + }, + type_params: None, + parameters: Parameters { + range: 48..57, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 49..50, + parameter: Parameter { + range: 49..50, + name: Identifier { + id: "a", + range: 49..50, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 52..56, + parameter: Parameter { + range: 52..53, + name: Identifier { + id: "a", + range: 52..53, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 54..56, + value: Int( + 20, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 59..63, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 65..84, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f4", + range: 69..71, + }, + type_params: None, + parameters: Parameters { + range: 71..78, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 72..73, + parameter: Parameter { + range: 72..73, + name: Identifier { + id: "a", + range: 72..73, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 76..77, + name: Identifier { + id: "a", + range: 76..77, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 80..84, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 86..109, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f5", + range: 90..92, + }, + type_params: None, + parameters: Parameters { + range: 92..103, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 93..94, + parameter: Parameter { + range: 93..94, + name: Identifier { + id: "a", + range: 93..94, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 101..102, + name: Identifier { + id: "a", + range: 101..102, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 105..109, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 201..224, + is_async: false, + decorator_list: [], + name: Identifier { + id: "f6", + range: 205..207, + }, + type_params: None, + parameters: Parameters { + range: 207..218, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 208..209, + parameter: Parameter { + range: 208..209, + name: Identifier { + id: "a", + range: 208..209, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 211..217, + parameter: Parameter { + range: 211..217, + name: Identifier { + id: "a", + range: 211..212, + }, + annotation: Some( + Name( + ExprName { + range: 214..217, + id: "str", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 220..224, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def f(a, a): pass + | ^ Syntax Error: duplicate argument 'a' in function definition +2 | +3 | def f2(a, *, a): pass + | + + + | +1 | def f(a, a): pass +2 | +3 | def f2(a, *, a): pass + | ^ Syntax Error: duplicate argument 'a' in function definition +4 | +5 | def f3(a, a=20): pass + | + + + | +3 | def f2(a, *, a): pass +4 | +5 | def f3(a, a=20): pass + | ^ Syntax Error: duplicate argument 'a' in function definition +6 | +7 | def f4(a, *a): pass + | + + + | +5 | def f3(a, a=20): pass +6 | +7 | def f4(a, *a): pass + | ^ Syntax Error: duplicate argument 'a' in function definition +8 | +9 | def f5(a, *, **a): pass + | + + + | + 7 | def f4(a, *a): pass + 8 | + 9 | def f5(a, *, **a): pass + | ^ Syntax Error: duplicate argument 'a' in function definition +10 | +11 | # TODO(micha): This is inconsistent. All other examples only highlight the argument name. + | + + + | +11 | # TODO(micha): This is inconsistent. All other examples only highlight the argument name. +12 | def f6(a, a: str): pass + | ^^^^^^ Syntax Error: duplicate argument 'a' in function definition + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_type_parameters.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_type_parameters.py.snap new file mode 100644 index 0000000000000..a69107b242404 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_type_parameters.py.snap @@ -0,0 +1,524 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/function_type_parameters.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..1039, + body: [ + FunctionDef( + StmtFunctionDef { + range: 795..846, + is_async: false, + decorator_list: [], + name: Identifier { + id: "unclosed", + range: 799..807, + }, + type_params: Some( + TypeParams { + range: 807..813, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 808..809, + name: Identifier { + id: "A", + range: 808..809, + }, + bound: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 811..813, + name: Identifier { + id: "B", + range: 812..813, + }, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 813..825, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 814..824, + parameter: Parameter { + range: 814..824, + name: Identifier { + id: "test", + range: 814..818, + }, + annotation: Some( + Name( + ExprName { + range: 820..824, + id: "name", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 831..835, + }, + ), + Expr( + StmtExpr { + range: 841..846, + value: BinOp( + ExprBinOp { + range: 841..846, + left: Name( + ExprName { + range: 841..842, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 845..846, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 848..876, + is_async: false, + decorator_list: [], + name: Identifier { + id: "keyword", + range: 852..859, + }, + type_params: Some( + TypeParams { + range: 859..869, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 860..861, + name: Identifier { + id: "A", + range: 860..861, + }, + bound: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 863..868, + name: Identifier { + id: "await", + range: 863..868, + }, + bound: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 869..871, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 873..876, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 873..876, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 878..914, + is_async: false, + decorator_list: [], + name: Identifier { + id: "not_a_type_param", + range: 882..898, + }, + type_params: Some( + TypeParams { + range: 898..907, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 899..900, + name: Identifier { + id: "A", + range: 899..900, + }, + bound: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 905..906, + name: Identifier { + id: "B", + range: 905..906, + }, + bound: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 907..909, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 911..914, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 911..914, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 916..948, + is_async: false, + decorator_list: [], + name: Identifier { + id: "multiple_commas", + range: 920..935, + }, + type_params: Some( + TypeParams { + range: 935..941, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 936..937, + name: Identifier { + id: "A", + range: 936..937, + }, + bound: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 939..940, + name: Identifier { + id: "B", + range: 939..940, + }, + bound: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 941..943, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 945..948, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 945..948, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 950..990, + is_async: false, + decorator_list: [], + name: Identifier { + id: "multiple_trailing_commas", + range: 954..978, + }, + type_params: Some( + TypeParams { + range: 978..983, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 979..980, + name: Identifier { + id: "A", + range: 979..980, + }, + bound: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 983..985, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 987..990, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 987..990, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 992..1039, + is_async: false, + decorator_list: [], + name: Identifier { + id: "multiple_commas_and_recovery", + range: 996..1024, + }, + type_params: Some( + TypeParams { + range: 1024..1028, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 1025..1026, + name: Identifier { + id: "A", + range: 1025..1026, + }, + bound: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 1028..1034, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 1036..1039, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1036..1039, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | + 9 | # on following lines. +10 | +11 | def unclosed[A, *B(test: name): + | ^ Syntax Error: expected Rsqb, found Lpar +12 | pass + | + + + | +11 | def unclosed[A, *B(test: name): +12 | pass + | ^^^^ Syntax Error: use `;` to separate simple statements +13 | +14 | a + b + | + + + | +14 | a + b +15 | +16 | def keyword[A, await](): ... + | ^^^^^ Syntax Error: Identifier expected. 'await' is a keyword that cannot be used here. +17 | +18 | def not_a_type_param[A, |, B](): ... + | + + + | +14 | a + b +15 | +16 | def keyword[A, await](): ... + | ^^^ Syntax Error: compound statements not allowed in the same line as simple statements +17 | +18 | def not_a_type_param[A, |, B](): ... + | + + + | +16 | def keyword[A, await](): ... +17 | +18 | def not_a_type_param[A, |, B](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list +19 | +20 | def multiple_commas[A,,B](): ... + | + + + | +16 | def keyword[A, await](): ... +17 | +18 | def not_a_type_param[A, |, B](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list +19 | +20 | def multiple_commas[A,,B](): ... + | + + + | +16 | def keyword[A, await](): ... +17 | +18 | def not_a_type_param[A, |, B](): ... + | ^^^ Syntax Error: compound statements not allowed in the same line as simple statements +19 | +20 | def multiple_commas[A,,B](): ... + | + + + | +18 | def not_a_type_param[A, |, B](): ... +19 | +20 | def multiple_commas[A,,B](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list +21 | +22 | def multiple_trailing_commas[A,,](): ... + | + + + | +18 | def not_a_type_param[A, |, B](): ... +19 | +20 | def multiple_commas[A,,B](): ... + | ^^^ Syntax Error: compound statements not allowed in the same line as simple statements +21 | +22 | def multiple_trailing_commas[A,,](): ... + | + + + | +20 | def multiple_commas[A,,B](): ... +21 | +22 | def multiple_trailing_commas[A,,](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list +23 | +24 | def multiple_commas_and_recovery[A,,100](): ... + | + + + | +20 | def multiple_commas[A,,B](): ... +21 | +22 | def multiple_trailing_commas[A,,](): ... + | ^^^ Syntax Error: compound statements not allowed in the same line as simple statements +23 | +24 | def multiple_commas_and_recovery[A,,100](): ... + | + + + | +22 | def multiple_trailing_commas[A,,](): ... +23 | +24 | def multiple_commas_and_recovery[A,,100](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list + | + + + | +22 | def multiple_trailing_commas[A,,](): ... +23 | +24 | def multiple_commas_and_recovery[A,,100](): ... + | ^^^ Syntax Error: Expected a type parameter or the end of the type parameter list + | + + + | +22 | def multiple_trailing_commas[A,,](): ... +23 | +24 | def multiple_commas_and_recovery[A,,100](): ... + | Syntax Error: unexpected EOF while parsing + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_elif.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_elif.py.snap new file mode 100644 index 0000000000000..0b7455808b0bb --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_elif.py.snap @@ -0,0 +1,105 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/if_elif.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..54, + body: [ + If( + StmtIf { + range: 0..38, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 3..7, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 13..17, + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 18..38, + test: Some( + BooleanLiteral( + ExprBooleanLiteral { + range: 23..28, + value: false, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 34..38, + }, + ), + ], + }, + ], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 39..43, + target: Name( + ExprName { + range: 39..42, + id: "elf", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 43..43, + id: "", + ctx: Load, + }, + ), + value: None, + simple: true, + }, + ), + Pass( + StmtPass { + range: 48..52, + }, + ), + ], + }, +) +``` +## Errors + + | +3 | elif False: +4 | pass +5 | elf: + | ^ Syntax Error: Expression expected. +6 | pass + | + + + | +4 | pass +5 | elf: +6 | pass + | ^^^^ Syntax Error: unexpected indentation + | + + + | +6 | pass +7 | + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_closing_parentheses.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_closing_parentheses.py.snap new file mode 100644 index 0000000000000..d0cb0de1ad3d7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_closing_parentheses.py.snap @@ -0,0 +1,82 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/if_extra_closing_parentheses.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..110, + body: [ + If( + StmtIf { + range: 90..97, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 93..97, + value: true, + }, + ), + body: [], + elif_else_clauses: [], + }, + ), + Pass( + StmtPass { + range: 105..109, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # FIXME(micha): This creates two syntax errors instead of just one (and overlapping ones) +2 | if True)): + | ^ Syntax Error: expected Colon, found Rpar +3 | pass + | + + + | +1 | # FIXME(micha): This creates two syntax errors instead of just one (and overlapping ones) +2 | if True)): + | ^ Syntax Error: Expected a statement +3 | pass + | + + + | +1 | # FIXME(micha): This creates two syntax errors instead of just one (and overlapping ones) +2 | if True)): + | ^ Syntax Error: Expected a statement +3 | pass + | + + + | +1 | # FIXME(micha): This creates two syntax errors instead of just one (and overlapping ones) +2 | if True)): + | ^ Syntax Error: Expected a statement +3 | pass + | + + + | +1 | # FIXME(micha): This creates two syntax errors instead of just one (and overlapping ones) +2 | if True)): +3 | pass + | ^^^^ Syntax Error: unexpected indentation + | + + + | +2 | if True)): +3 | pass + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_indent.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_indent.py.snap new file mode 100644 index 0000000000000..83c2e720ff973 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_indent.py.snap @@ -0,0 +1,107 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/if_extra_indent.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..153, + body: [ + If( + StmtIf { + range: 103..134, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 106..110, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 116..120, + }, + ), + Expr( + StmtExpr { + range: 129..134, + value: BinOp( + ExprBinOp { + range: 129..134, + left: Name( + ExprName { + range: 129..130, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 133..134, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + Pass( + StmtPass { + range: 140..144, + }, + ), + Assign( + StmtAssign { + range: 146..152, + targets: [ + Name( + ExprName { + range: 146..147, + id: "a", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 150..152, + value: Int( + 10, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +2 | if True: +3 | pass +4 | a + b + | ^^^^^^^^ Syntax Error: unexpected indentation +5 | +6 | pass + | + + + | +6 | pass +7 | +8 | a = 10 + | Syntax Error: Expected a statement + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_missing_body.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_missing_body.py.snap new file mode 100644 index 0000000000000..54cba5c59b304 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_missing_body.py.snap @@ -0,0 +1,85 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/if_missing_body.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..84, + body: [ + If( + StmtIf { + range: 0..8, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 3..7, + value: true, + }, + ), + body: [], + elif_else_clauses: [], + }, + ), + Expr( + StmtExpr { + range: 11..16, + value: BinOp( + ExprBinOp { + range: 11..16, + left: Name( + ExprName { + range: 11..12, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 15..16, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + If( + StmtIf { + range: 18..27, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 21..26, + value: false, + }, + ), + body: [], + elif_else_clauses: [], + }, + ), + ], + }, +) +``` +## Errors + + | +4 | a + b + | ^ Syntax Error: expected a single statement or an indented body after `if` statement +5 | +6 | if False: # This if statement has neither an indent nor a newline. + | + + + | +4 | a + b +5 | +6 | if False: # This if statement has neither an indent nor a newline. + | Syntax Error: expected a single statement or an indented body after `if` statement + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_missing_colon.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_missing_colon.py.snap new file mode 100644 index 0000000000000..e72a5c3a4fd1a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_missing_colon.py.snap @@ -0,0 +1,68 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/if_missing_colon.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..25, + body: [ + If( + StmtIf { + range: 0..16, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 3..7, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 12..16, + }, + ), + ], + elif_else_clauses: [], + }, + ), + Assign( + StmtAssign { + range: 18..24, + targets: [ + Name( + ExprName { + range: 18..19, + id: "a", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 22..24, + value: Int( + 10, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if True + | ^ Syntax Error: expected Colon, found Newline +2 | pass +3 | +4 | a = 10 + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__import_from.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__import_from.py.snap new file mode 100644 index 0000000000000..ea5d724521dc4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__import_from.py.snap @@ -0,0 +1,225 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/import_from.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..98, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..21, + module: Some( + Identifier { + id: "abc", + range: 5..8, + }, + ), + names: [ + Alias { + range: 16..17, + name: Identifier { + id: "a", + range: 16..17, + }, + asname: None, + }, + Alias { + range: 19..20, + name: Identifier { + id: "b", + range: 19..20, + }, + asname: None, + }, + ], + level: Some( + 0, + ), + }, + ), + Expr( + StmtExpr { + range: 23..28, + value: BinOp( + ExprBinOp { + range: 23..28, + left: Name( + ExprName { + range: 23..24, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 27..28, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 30..48, + module: Some( + Identifier { + id: "abc", + range: 35..38, + }, + ), + names: [], + level: Some( + 0, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 50..65, + module: Some( + Identifier { + id: "abc", + range: 55..58, + }, + ), + names: [], + level: Some( + 0, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 67..94, + module: Some( + Identifier { + id: "abc", + range: 72..75, + }, + ), + names: [ + Alias { + range: 84..85, + name: Identifier { + id: "a", + range: 84..85, + }, + asname: None, + }, + Alias { + range: 87..88, + name: Identifier { + id: "b", + range: 87..88, + }, + asname: None, + }, + Alias { + range: 90..91, + name: Identifier { + id: "c", + range: 90..91, + }, + asname: None, + }, + Alias { + range: 93..94, + name: Identifier { + id: "a", + range: 93..94, + }, + asname: None, + }, + ], + level: Some( + 0, + ), + }, + ), + Expr( + StmtExpr { + range: 95..98, + value: UnaryOp( + ExprUnaryOp { + range: 95..98, + op: UAdd, + operand: Name( + ExprName { + range: 97..98, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +3 | a + b +4 | +5 | from abc import ,, + | ^ Syntax Error: Expected an import name or a ')' +6 | +7 | from abc import + | + + + | +3 | a + b +4 | +5 | from abc import ,, + | ^ Syntax Error: Expected an import name or a ')' +6 | +7 | from abc import + | + + + | + 9 | from abc import (a, b, c +10 | +11 | a + b + | ^ Syntax Error: expected Comma, found Name + | + + + | + 9 | from abc import (a, b, c +10 | +11 | a + b + | ^ Syntax Error: expected Comma, found Plus + | + + + | + 7 | from abc import + 8 | + 9 | / from abc import (a, b, c +10 | | +11 | | a + b + | |___^ Syntax Error: use `;` to separate simple statements + | + + + | + 9 | from abc import (a, b, c +10 | +11 | a + b + | Syntax Error: unexpected EOF while parsing + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_assignment_targets.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_assignment_targets.py.snap new file mode 100644 index 0000000000000..57f4abf1fc794 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_assignment_targets.py.snap @@ -0,0 +1,1672 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/invalid_assignment_targets.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..1289, + body: [ + Assign( + StmtAssign { + range: 201..206, + targets: [ + NumberLiteral( + ExprNumberLiteral { + range: 201..202, + value: Int( + 5, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 205..206, + value: Int( + 3, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 208..214, + target: NumberLiteral( + ExprNumberLiteral { + range: 208..209, + value: Int( + 5, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 213..214, + value: Int( + 3, + ), + }, + ), + }, + ), + AnnAssign( + StmtAnnAssign { + range: 216..228, + target: NumberLiteral( + ExprNumberLiteral { + range: 217..218, + value: Int( + 5, + ), + }, + ), + annotation: Name( + ExprName { + range: 221..224, + id: "int", + ctx: Load, + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 227..228, + value: Int( + 3, + ), + }, + ), + ), + simple: false, + }, + ), + Assign( + StmtAssign { + range: 303..314, + targets: [ + BoolOp( + ExprBoolOp { + range: 303..309, + op: Or, + values: [ + Name( + ExprName { + range: 303..304, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 308..309, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 312..314, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 315..328, + targets: [ + NamedExpr( + ExprNamedExpr { + range: 316..322, + target: Name( + ExprName { + range: 316..317, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 321..322, + value: Int( + 5, + ), + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 326..328, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 329..339, + targets: [ + BinOp( + ExprBinOp { + range: 329..334, + left: Name( + ExprName { + range: 329..330, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 333..334, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 337..339, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 340..347, + targets: [ + UnaryOp( + ExprUnaryOp { + range: 340..342, + op: USub, + operand: Name( + ExprName { + range: 341..342, + id: "x", + ctx: Store, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 345..347, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 348..366, + targets: [ + Lambda( + ExprLambda { + range: 349..360, + parameters: Some( + Parameters { + range: 356..357, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 356..357, + parameter: Parameter { + range: 356..357, + name: Identifier { + id: "_", + range: 356..357, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 359..360, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 364..366, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 367..385, + targets: [ + IfExp( + ExprIfExp { + range: 367..380, + test: Name( + ExprName { + range: 372..373, + id: "b", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 367..368, + id: "a", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 379..380, + id: "c", + ctx: Load, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 383..385, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 386..399, + targets: [ + Dict( + ExprDict { + range: 386..394, + keys: [ + Some( + StringLiteral( + ExprStringLiteral { + range: 387..390, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 387..390, + value: "a", + unicode: false, + }, + ), + }, + }, + ), + ), + ], + values: [ + NumberLiteral( + ExprNumberLiteral { + range: 392..393, + value: Int( + 5, + ), + }, + ), + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 397..399, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 400..408, + targets: [ + Set( + ExprSet { + range: 400..403, + elts: [ + Name( + ExprName { + range: 401..402, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 406..408, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 409..429, + targets: [ + ListComp( + ExprListComp { + range: 409..424, + elt: Name( + ExprName { + range: 410..411, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 412..423, + target: Name( + ExprName { + range: 416..417, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 421..423, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 427..429, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 430..450, + targets: [ + SetComp( + ExprSetComp { + range: 430..445, + elt: Name( + ExprName { + range: 431..432, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 433..444, + target: Name( + ExprName { + range: 437..438, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 442..444, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 448..450, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 451..478, + targets: [ + DictComp( + ExprDictComp { + range: 451..473, + key: Name( + ExprName { + range: 452..453, + id: "x", + ctx: Load, + }, + ), + value: BinOp( + ExprBinOp { + range: 455..460, + left: Name( + ExprName { + range: 455..456, + id: "x", + ctx: Load, + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 459..460, + value: Int( + 2, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 461..472, + target: Name( + ExprName { + range: 465..466, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 470..472, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 476..478, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 479..499, + targets: [ + GeneratorExp( + ExprGeneratorExp { + range: 479..494, + elt: Name( + ExprName { + range: 480..481, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 482..493, + target: Name( + ExprName { + range: 486..487, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 491..493, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 497..499, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 500..512, + targets: [ + Await( + ExprAwait { + range: 500..507, + value: Name( + ExprName { + range: 506..507, + id: "x", + ctx: Load, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 510..512, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 513..527, + targets: [ + Yield( + ExprYield { + range: 514..521, + value: Some( + Name( + ExprName { + range: 520..521, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 525..527, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 528..548, + targets: [ + YieldFrom( + ExprYieldFrom { + range: 529..542, + value: Name( + ExprName { + range: 540..542, + id: "xs", + ctx: Load, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 546..548, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 549..563, + targets: [ + Compare( + ExprCompare { + range: 549..558, + left: Name( + ExprName { + range: 549..550, + id: "a", + ctx: Load, + }, + ), + ops: [ + Lt, + Lt, + ], + comparators: [ + Name( + ExprName { + range: 553..554, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 557..558, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 561..563, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 564..574, + targets: [ + Call( + ExprCall { + range: 564..569, + func: Name( + ExprName { + range: 564..567, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 567..569, + args: [], + keywords: [], + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 572..574, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1077..1091, + targets: [ + FString( + ExprFString { + range: 1077..1086, + value: FStringValue { + inner: Single( + FString( + FString { + range: 1077..1086, + elements: [ + Expression( + FStringExpressionElement { + range: 1079..1085, + expression: Name( + ExprName { + range: 1080..1084, + id: "quux", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ), + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1089..1091, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1092..1115, + targets: [ + FString( + ExprFString { + range: 1092..1110, + value: FStringValue { + inner: Single( + FString( + FString { + range: 1092..1110, + elements: [ + Expression( + FStringExpressionElement { + range: 1094..1099, + expression: Name( + ExprName { + range: 1095..1098, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 1099..1104, + value: " and ", + }, + ), + Expression( + FStringExpressionElement { + range: 1104..1109, + expression: Name( + ExprName { + range: 1105..1108, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + ), + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1113..1115, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1117..1127, + targets: [ + StringLiteral( + ExprStringLiteral { + range: 1117..1122, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 1117..1122, + value: "foo", + unicode: false, + }, + ), + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1125..1127, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1128..1139, + targets: [ + BytesLiteral( + ExprBytesLiteral { + range: 1128..1134, + value: BytesLiteralValue { + inner: Single( + BytesLiteral { + range: 1128..1134, + value: [ + 102, + 111, + 111, + ], + }, + ), + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1137..1139, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1140..1148, + targets: [ + NumberLiteral( + ExprNumberLiteral { + range: 1140..1143, + value: Int( + 123, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1146..1148, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1149..1158, + targets: [ + BooleanLiteral( + ExprBooleanLiteral { + range: 1149..1153, + value: true, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1156..1158, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1159..1168, + targets: [ + NoneLiteral( + ExprNoneLiteral { + range: 1159..1163, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1166..1168, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1169..1177, + targets: [ + EllipsisLiteral( + ExprEllipsisLiteral { + range: 1169..1172, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1175..1177, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1178..1189, + targets: [ + Starred( + ExprStarred { + range: 1178..1184, + value: Call( + ExprCall { + range: 1179..1184, + func: Name( + ExprName { + range: 1179..1182, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1182..1184, + args: [], + keywords: [], + }, + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1187..1189, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1190..1218, + targets: [ + List( + ExprList { + range: 1190..1203, + elts: [ + Name( + ExprName { + range: 1191..1192, + id: "x", + ctx: Store, + }, + ), + Call( + ExprCall { + range: 1194..1199, + func: Name( + ExprName { + range: 1194..1197, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1197..1199, + args: [], + keywords: [], + }, + }, + ), + Name( + ExprName { + range: 1201..1202, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 1206..1218, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 1207..1209, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 1211..1213, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 1215..1217, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1219..1259, + targets: [ + List( + ExprList { + range: 1219..1238, + elts: [ + List( + ExprList { + range: 1220..1226, + elts: [ + Name( + ExprName { + range: 1221..1222, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 1224..1225, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + List( + ExprList { + range: 1228..1234, + elts: [ + List( + ExprList { + range: 1229..1233, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 1230..1232, + value: Int( + 42, + ), + }, + ), + ], + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + Name( + ExprName { + range: 1236..1237, + id: "d", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 1241..1259, + elts: [ + List( + ExprList { + range: 1242..1248, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 1243..1244, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 1246..1247, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + List( + ExprList { + range: 1250..1255, + elts: [ + List( + ExprList { + range: 1251..1254, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 1252..1253, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 1257..1258, + value: Int( + 4, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1260..1288, + targets: [ + Tuple( + ExprTuple { + range: 1260..1273, + elts: [ + Name( + ExprName { + range: 1261..1262, + id: "x", + ctx: Store, + }, + ), + Call( + ExprCall { + range: 1264..1269, + func: Name( + ExprName { + range: 1264..1267, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1267..1269, + args: [], + keywords: [], + }, + }, + ), + Name( + ExprName { + range: 1271..1272, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 1276..1288, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 1277..1279, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 1281..1283, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 1285..1287, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +3 | # rejected by the parser. e.g., `5 = 3`, `5 += 3`, `(5): int = 3`. +4 | +5 | 5 = 3 + | ^ Syntax Error: invalid assignment target +6 | +7 | 5 += 3 + | + + + | +5 | 5 = 3 +6 | +7 | 5 += 3 + | ^ Syntax Error: invalid augmented assignment target +8 | +9 | (5): int = 3 + | + + + | + 7 | 5 += 3 + 8 | + 9 | (5): int = 3 + | ^ Syntax Error: invalid assignment target +10 | +11 | # Now we exhaustively test all possible cases where assignment can fail. + | + + + | +11 | # Now we exhaustively test all possible cases where assignment can fail. +12 | x or y = 42 + | ^^^^^^ Syntax Error: invalid assignment target +13 | (x := 5) = 42 +14 | x + y = 42 + | + + + | +11 | # Now we exhaustively test all possible cases where assignment can fail. +12 | x or y = 42 +13 | (x := 5) = 42 + | ^^^^^^ Syntax Error: invalid assignment target +14 | x + y = 42 +15 | -x = 42 + | + + + | +12 | x or y = 42 +13 | (x := 5) = 42 +14 | x + y = 42 + | ^^^^^ Syntax Error: invalid assignment target +15 | -x = 42 +16 | (lambda _: 1) = 42 + | + + + | +13 | (x := 5) = 42 +14 | x + y = 42 +15 | -x = 42 + | ^^ Syntax Error: invalid assignment target +16 | (lambda _: 1) = 42 +17 | a if b else c = 42 + | + + + | +14 | x + y = 42 +15 | -x = 42 +16 | (lambda _: 1) = 42 + | ^^^^^^^^^^^ Syntax Error: invalid assignment target +17 | a if b else c = 42 +18 | {"a": 5} = 42 + | + + + | +15 | -x = 42 +16 | (lambda _: 1) = 42 +17 | a if b else c = 42 + | ^^^^^^^^^^^^^ Syntax Error: invalid assignment target +18 | {"a": 5} = 42 +19 | {a} = 42 + | + + + | +16 | (lambda _: 1) = 42 +17 | a if b else c = 42 +18 | {"a": 5} = 42 + | ^^^^^^^^ Syntax Error: invalid assignment target +19 | {a} = 42 +20 | [x for x in xs] = 42 + | + + + | +17 | a if b else c = 42 +18 | {"a": 5} = 42 +19 | {a} = 42 + | ^^^ Syntax Error: invalid assignment target +20 | [x for x in xs] = 42 +21 | {x for x in xs} = 42 + | + + + | +18 | {"a": 5} = 42 +19 | {a} = 42 +20 | [x for x in xs] = 42 + | ^^^^^^^^^^^^^^^ Syntax Error: invalid assignment target +21 | {x for x in xs} = 42 +22 | {x: x * 2 for x in xs} = 42 + | + + + | +19 | {a} = 42 +20 | [x for x in xs] = 42 +21 | {x for x in xs} = 42 + | ^^^^^^^^^^^^^^^ Syntax Error: invalid assignment target +22 | {x: x * 2 for x in xs} = 42 +23 | (x for x in xs) = 42 + | + + + | +20 | [x for x in xs] = 42 +21 | {x for x in xs} = 42 +22 | {x: x * 2 for x in xs} = 42 + | ^^^^^^^^^^^^^^^^^^^^^^ Syntax Error: invalid assignment target +23 | (x for x in xs) = 42 +24 | await x = 42 + | + + + | +21 | {x for x in xs} = 42 +22 | {x: x * 2 for x in xs} = 42 +23 | (x for x in xs) = 42 + | ^^^^^^^^^^^^^^^ Syntax Error: invalid assignment target +24 | await x = 42 +25 | (yield x) = 42 + | + + + | +22 | {x: x * 2 for x in xs} = 42 +23 | (x for x in xs) = 42 +24 | await x = 42 + | ^^^^^^^ Syntax Error: invalid assignment target +25 | (yield x) = 42 +26 | (yield from xs) = 42 + | + + + | +23 | (x for x in xs) = 42 +24 | await x = 42 +25 | (yield x) = 42 + | ^^^^^^^ Syntax Error: invalid assignment target +26 | (yield from xs) = 42 +27 | a < b < c = 42 + | + + + | +24 | await x = 42 +25 | (yield x) = 42 +26 | (yield from xs) = 42 + | ^^^^^^^^^^^^^ Syntax Error: invalid assignment target +27 | a < b < c = 42 +28 | foo() = 42 + | + + + | +25 | (yield x) = 42 +26 | (yield from xs) = 42 +27 | a < b < c = 42 + | ^^^^^^^^^ Syntax Error: invalid assignment target +28 | foo() = 42 + | + + + | +26 | (yield from xs) = 42 +27 | a < b < c = 42 +28 | foo() = 42 + | ^^^^^ Syntax Error: invalid assignment target +29 | +30 | # N.B. It looks like the parser can't generate a top-level + | + + + | +39 | # +40 | # See: https://docs.python.org/3/library/ast.html#ast.FormattedValue +41 | f"{quux}" = 42 + | ^^^^^^^^^ Syntax Error: invalid assignment target +42 | f"{foo} and {bar}" = 42 + | + + + | +40 | # See: https://docs.python.org/3/library/ast.html#ast.FormattedValue +41 | f"{quux}" = 42 +42 | f"{foo} and {bar}" = 42 + | ^^^^^^^^^^^^^^^^^^ Syntax Error: invalid assignment target +43 | +44 | "foo" = 42 + | + + + | +42 | f"{foo} and {bar}" = 42 +43 | +44 | "foo" = 42 + | ^^^^^ Syntax Error: invalid assignment target +45 | b"foo" = 42 +46 | 123 = 42 + | + + + | +44 | "foo" = 42 +45 | b"foo" = 42 + | ^^^^^^ Syntax Error: invalid assignment target +46 | 123 = 42 +47 | True = 42 + | + + + | +44 | "foo" = 42 +45 | b"foo" = 42 +46 | 123 = 42 + | ^^^ Syntax Error: invalid assignment target +47 | True = 42 +48 | None = 42 + | + + + | +45 | b"foo" = 42 +46 | 123 = 42 +47 | True = 42 + | ^^^^ Syntax Error: invalid assignment target +48 | None = 42 +49 | ... = 42 + | + + + | +46 | 123 = 42 +47 | True = 42 +48 | None = 42 + | ^^^^ Syntax Error: invalid assignment target +49 | ... = 42 +50 | *foo() = 42 + | + + + | +47 | True = 42 +48 | None = 42 +49 | ... = 42 + | ^^^ Syntax Error: invalid assignment target +50 | *foo() = 42 +51 | [x, foo(), y] = [42, 42, 42] + | + + + | +48 | None = 42 +49 | ... = 42 +50 | *foo() = 42 + | ^^^^^^ Syntax Error: invalid assignment target +51 | [x, foo(), y] = [42, 42, 42] +52 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4] + | + + + | +49 | ... = 42 +50 | *foo() = 42 +51 | [x, foo(), y] = [42, 42, 42] + | ^^^^^^^^^^^^^ Syntax Error: invalid assignment target +52 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4] +53 | (x, foo(), y) = (42, 42, 42) + | + + + | +50 | *foo() = 42 +51 | [x, foo(), y] = [42, 42, 42] +52 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4] + | ^^^^^^^^^^^^^^^^^^^ Syntax Error: invalid assignment target +53 | (x, foo(), y) = (42, 42, 42) + | + + + | +51 | [x, foo(), y] = [42, 42, 42] +52 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4] +53 | (x, foo(), y) = (42, 42, 42) + | ^^^^^^^^^^^^^ Syntax Error: invalid assignment target + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__named_expression_statement.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__named_expression_statement.py.snap new file mode 100644 index 0000000000000..346e20396b302 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__named_expression_statement.py.snap @@ -0,0 +1,52 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/invalid/statements/named_expression_statement.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..129, + body: [ + Expr( + StmtExpr { + range: 120..128, + value: NamedExpr( + ExprNamedExpr { + range: 121..127, + target: NumberLiteral( + ExprNumberLiteral { + range: 121..122, + value: Int( + 5, + ), + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 126..127, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # This test previously passed before the assignment operator checking +2 | # above, but we include it here for good measure. +3 | (5 := 3) + | ^ Syntax Error: invalid assignment target + | + + + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary_comprehension.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary_comprehension.py.snap new file mode 100644 index 0000000000000..736b111e93b1d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary_comprehension.py.snap @@ -0,0 +1,89 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/expressions/dictionary_comprehension.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..27, + body: [ + Assign( + StmtAssign { + range: 0..26, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: SetComp( + ExprSetComp { + range: 4..26, + elt: Name( + ExprName { + range: 5..6, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 7..25, + target: Name( + ExprName { + range: 11..12, + id: "y", + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 16..25, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 17..18, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__lambda.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__lambda.py.snap new file mode 100644 index 0000000000000..7e859d3629f79 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__lambda.py.snap @@ -0,0 +1,421 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/expressions/lambda.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..132, + body: [ + Expr( + StmtExpr { + range: 0..9, + value: Lambda( + ExprLambda { + range: 0..9, + parameters: None, + body: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..28, + value: Lambda( + ExprLambda { + range: 11..28, + parameters: Some( + Parameters { + range: 18..25, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 18..19, + parameter: Parameter { + range: 18..19, + name: Identifier { + id: "a", + range: 18..19, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 21..22, + parameter: Parameter { + range: 21..22, + name: Identifier { + id: "b", + range: 21..22, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 24..25, + parameter: Parameter { + range: 24..25, + name: Identifier { + id: "c", + range: 24..25, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 27..28, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..53, + value: Lambda( + ExprLambda { + range: 30..53, + parameters: Some( + Parameters { + range: 37..50, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 37..38, + parameter: Parameter { + range: 37..38, + name: Identifier { + id: "a", + range: 37..38, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 40..44, + parameter: Parameter { + range: 40..41, + name: Identifier { + id: "b", + range: 40..41, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 42..44, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 46..50, + parameter: Parameter { + range: 46..47, + name: Identifier { + id: "c", + range: 46..47, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 48..50, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 52..53, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 55..75, + value: Lambda( + ExprLambda { + range: 55..75, + parameters: Some( + Parameters { + range: 62..72, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 65..66, + parameter: Parameter { + range: 65..66, + name: Identifier { + id: "a", + range: 65..66, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 68..69, + parameter: Parameter { + range: 68..69, + name: Identifier { + id: "b", + range: 68..69, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 71..72, + parameter: Parameter { + range: 71..72, + name: Identifier { + id: "c", + range: 71..72, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 74..75, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 77..103, + value: Lambda( + ExprLambda { + range: 77..103, + parameters: Some( + Parameters { + range: 84..100, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 87..88, + parameter: Parameter { + range: 87..88, + name: Identifier { + id: "a", + range: 87..88, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 90..94, + parameter: Parameter { + range: 90..91, + name: Identifier { + id: "b", + range: 90..91, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 92..94, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 96..100, + parameter: Parameter { + range: 96..97, + name: Identifier { + id: "c", + range: 96..97, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 98..100, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 102..103, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 105..131, + value: Lambda( + ExprLambda { + range: 105..131, + parameters: Some( + Parameters { + range: 112..128, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 112..113, + parameter: Parameter { + range: 112..113, + name: Identifier { + id: "a", + range: 112..113, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 115..116, + parameter: Parameter { + range: 115..116, + name: Identifier { + id: "b", + range: 115..116, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 118..119, + parameter: Parameter { + range: 118..119, + name: Identifier { + id: "c", + range: 118..119, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 124..125, + parameter: Parameter { + range: 124..125, + name: Identifier { + id: "d", + range: 124..125, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 127..128, + parameter: Parameter { + range: 127..128, + name: Identifier { + id: "e", + range: 127..128, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 130..131, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list_comprehension.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list_comprehension.py.snap new file mode 100644 index 0000000000000..b9f8cc3f74468 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list_comprehension.py.snap @@ -0,0 +1,89 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/expressions/list_comprehension.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..27, + body: [ + Assign( + StmtAssign { + range: 0..26, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: ListComp( + ExprListComp { + range: 4..26, + elt: Name( + ExprName { + range: 5..6, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 7..25, + target: Name( + ExprName { + range: 11..12, + id: "y", + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 16..25, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 17..18, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__named_expression.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__named_expression.py.snap new file mode 100644 index 0000000000000..a6ca4b0b1c933 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__named_expression.py.snap @@ -0,0 +1,74 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/expressions/named_expression.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..30, + body: [ + If( + StmtIf { + range: 0..19, + test: NamedExpr( + ExprNamedExpr { + range: 3..9, + target: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 1, + ), + }, + ), + }, + ), + body: [ + Pass( + StmtPass { + range: 15..19, + }, + ), + ], + elif_else_clauses: [], + }, + ), + Expr( + StmtExpr { + range: 21..29, + value: NamedExpr( + ExprNamedExpr { + range: 22..28, + target: Name( + ExprName { + range: 22..23, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 27..28, + value: Int( + 5, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__annotated_assignment.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__annotated_assignment.py.snap new file mode 100644 index 0000000000000..21f0e91003fbe --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__annotated_assignment.py.snap @@ -0,0 +1,46 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/statement/annotated_assignment.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..11, + body: [ + AnnAssign( + StmtAnnAssign { + range: 0..10, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 3..6, + id: "int", + ctx: Load, + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 1, + ), + }, + ), + ), + simple: true, + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assignment.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assignment.py.snap new file mode 100644 index 0000000000000..0a20b416e28c7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assignment.py.snap @@ -0,0 +1,831 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/statement/assignment.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..692, + body: [ + Assign( + StmtAssign { + range: 0..13, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 4..13, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 15..33, + targets: [ + Tuple( + ExprTuple { + range: 15..21, + elts: [ + Name( + ExprName { + range: 16..17, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 19..20, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 24..33, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 31..32, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 35..53, + targets: [ + List( + ExprList { + range: 35..41, + elts: [ + Name( + ExprName { + range: 36..37, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 39..40, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 44..53, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 45..46, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 48..49, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 55..70, + targets: [ + Attribute( + ExprAttribute { + range: 55..58, + value: Name( + ExprName { + range: 55..56, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "y", + range: 57..58, + }, + ctx: Store, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 61..70, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 62..63, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 68..69, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 72..88, + targets: [ + Subscript( + ExprSubscript { + range: 72..76, + value: Name( + ExprName { + range: 72..73, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 74..75, + id: "y", + ctx: Load, + }, + ), + ctx: Store, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 79..88, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 80..81, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 83..84, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 86..87, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 90..109, + targets: [ + Tuple( + ExprTuple { + range: 90..97, + elts: [ + Name( + ExprName { + range: 91..92, + id: "x", + ctx: Store, + }, + ), + Starred( + ExprStarred { + range: 94..96, + value: Name( + ExprName { + range: 95..96, + id: "y", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 100..109, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 101..102, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 104..105, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 107..108, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 259..268, + targets: [ + Starred( + ExprStarred { + range: 259..263, + value: Name( + ExprName { + range: 260..263, + id: "foo", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 266..268, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 270..291, + targets: [ + List( + ExprList { + range: 270..279, + elts: [ + Name( + ExprName { + range: 271..272, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 274..275, + id: "y", + ctx: Store, + }, + ), + Name( + ExprName { + range: 277..278, + id: "z", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 282..291, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 283..284, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 286..287, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 289..290, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 293..314, + targets: [ + Tuple( + ExprTuple { + range: 293..302, + elts: [ + Name( + ExprName { + range: 294..295, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 297..298, + id: "y", + ctx: Store, + }, + ), + Name( + ExprName { + range: 300..301, + id: "z", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 305..314, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 306..307, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 309..310, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 312..313, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 315..324, + targets: [ + Subscript( + ExprSubscript { + range: 315..319, + value: Name( + ExprName { + range: 315..316, + id: "x", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 317..318, + value: Int( + 0, + ), + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 322..324, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 421..430, + targets: [ + Subscript( + ExprSubscript { + range: 421..425, + value: NumberLiteral( + ExprNumberLiteral { + range: 421..422, + value: Int( + 5, + ), + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 423..424, + value: Int( + 0, + ), + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 428..430, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 431..444, + targets: [ + Subscript( + ExprSubscript { + range: 431..437, + value: Name( + ExprName { + range: 431..432, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 433..436, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 433..434, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 435..436, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 440..444, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 441..443, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 540..553, + targets: [ + Subscript( + ExprSubscript { + range: 540..546, + value: NumberLiteral( + ExprNumberLiteral { + range: 540..541, + value: Int( + 5, + ), + }, + ), + slice: Slice( + ExprSlice { + range: 542..545, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 542..543, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 544..545, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 549..553, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 550..552, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 555..567, + targets: [ + Attribute( + ExprAttribute { + range: 555..562, + value: Name( + ExprName { + range: 555..558, + id: "foo", + ctx: Load, + }, + ), + attr: Identifier { + id: "bar", + range: 559..562, + }, + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 565..567, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 669..681, + targets: [ + Attribute( + ExprAttribute { + range: 669..676, + value: StringLiteral( + ExprStringLiteral { + range: 669..674, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 669..674, + value: "foo", + unicode: false, + }, + ), + }, + }, + ), + attr: Identifier { + id: "y", + range: 675..676, + }, + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 679..681, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 683..691, + targets: [ + Name( + ExprName { + range: 683..686, + id: "foo", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 689..691, + value: Int( + 42, + ), + }, + ), + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__augmented_assignment.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__augmented_assignment.py.snap new file mode 100644 index 0000000000000..8f54cc4dcd15e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__augmented_assignment.py.snap @@ -0,0 +1,152 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/statement/augmented_assignment.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..42, + body: [ + AugAssign( + StmtAugAssign { + range: 0..6, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 7..23, + target: Attribute( + ExprAttribute { + range: 7..10, + value: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "y", + range: 9..10, + }, + ctx: Store, + }, + ), + op: Add, + value: Tuple( + ExprTuple { + range: 14..23, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 21..22, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 24..41, + target: Subscript( + ExprSubscript { + range: 24..28, + value: Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 26..27, + id: "y", + ctx: Load, + }, + ), + ctx: Store, + }, + ), + op: Add, + value: Tuple( + ExprTuple { + range: 32..41, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 33..34, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 36..37, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 39..40, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__delete.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__delete.py.snap new file mode 100644 index 0000000000000..cdb4289531b9d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__delete.py.snap @@ -0,0 +1,81 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/statement/delete.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..23, + body: [ + Delete( + StmtDelete { + range: 0..5, + targets: [ + Name( + ExprName { + range: 4..5, + id: "x", + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 6..13, + targets: [ + Attribute( + ExprAttribute { + range: 10..13, + value: Name( + ExprName { + range: 10..11, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "y", + range: 12..13, + }, + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 14..22, + targets: [ + Subscript( + ExprSubscript { + range: 18..22, + value: Name( + ExprName { + range: 18..19, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 20..21, + id: "y", + ctx: Load, + }, + ), + ctx: Del, + }, + ), + ], + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__for.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__for.py.snap new file mode 100644 index 0000000000000..6cfa3f805570c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__for.py.snap @@ -0,0 +1,70 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/statement/for.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..29, + body: [ + For( + StmtFor { + range: 0..28, + is_async: false, + target: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 9..18, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 24..28, + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__function.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__function.py.snap new file mode 100644 index 0000000000000..eebd686df3050 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__function.py.snap @@ -0,0 +1,829 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/statement/function.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..683, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..29, + is_async: false, + decorator_list: [], + name: Identifier { + id: "no_parameters", + range: 4..17, + }, + type_params: None, + parameters: Parameters { + range: 17..19, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 25..29, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 32..76, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_parameters", + range: 36..57, + }, + type_params: None, + parameters: Parameters { + range: 57..66, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 58..59, + parameter: Parameter { + range: 58..59, + name: Identifier { + id: "a", + range: 58..59, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 61..62, + parameter: Parameter { + range: 61..62, + name: Identifier { + id: "b", + range: 61..62, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 64..65, + parameter: Parameter { + range: 64..65, + name: Identifier { + id: "c", + range: 64..65, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 72..76, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 79..149, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_parameters_with_default_values", + range: 83..124, + }, + type_params: None, + parameters: Parameters { + range: 124..139, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 125..126, + parameter: Parameter { + range: 125..126, + name: Identifier { + id: "a", + range: 125..126, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 128..132, + parameter: Parameter { + range: 128..129, + name: Identifier { + id: "b", + range: 128..129, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 130..132, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 134..138, + parameter: Parameter { + range: 134..135, + name: Identifier { + id: "c", + range: 134..135, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 136..138, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 145..149, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 152..201, + is_async: false, + decorator_list: [], + name: Identifier { + id: "keyword_only_parameters", + range: 156..179, + }, + type_params: None, + parameters: Parameters { + range: 179..191, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 183..184, + parameter: Parameter { + range: 183..184, + name: Identifier { + id: "a", + range: 183..184, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 186..187, + parameter: Parameter { + range: 186..187, + name: Identifier { + id: "b", + range: 186..187, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 189..190, + parameter: Parameter { + range: 189..190, + name: Identifier { + id: "c", + range: 189..190, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 197..201, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 204..273, + is_async: false, + decorator_list: [], + name: Identifier { + id: "keyword_only_parameters_with_defaults", + range: 208..245, + }, + type_params: None, + parameters: Parameters { + range: 245..263, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 249..250, + parameter: Parameter { + range: 249..250, + name: Identifier { + id: "a", + range: 249..250, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 252..256, + parameter: Parameter { + range: 252..253, + name: Identifier { + id: "b", + range: 252..253, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 254..256, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 258..262, + parameter: Parameter { + range: 258..259, + name: Identifier { + id: "c", + range: 258..259, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 260..262, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 269..273, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 276..344, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_and_keyword_parameters", + range: 280..313, + }, + type_params: None, + parameters: Parameters { + range: 313..334, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 314..315, + parameter: Parameter { + range: 314..315, + name: Identifier { + id: "a", + range: 314..315, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 317..318, + parameter: Parameter { + range: 317..318, + name: Identifier { + id: "b", + range: 317..318, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 320..321, + parameter: Parameter { + range: 320..321, + name: Identifier { + id: "c", + range: 320..321, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 326..327, + parameter: Parameter { + range: 326..327, + name: Identifier { + id: "d", + range: 326..327, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 329..330, + parameter: Parameter { + range: 329..330, + name: Identifier { + id: "e", + range: 329..330, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 332..333, + parameter: Parameter { + range: 332..333, + name: Identifier { + id: "f", + range: 332..333, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 340..344, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 347..435, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_and_keyword_parameters_with_defaults", + range: 351..398, + }, + type_params: None, + parameters: Parameters { + range: 398..425, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 399..400, + parameter: Parameter { + range: 399..400, + name: Identifier { + id: "a", + range: 399..400, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 402..403, + parameter: Parameter { + range: 402..403, + name: Identifier { + id: "b", + range: 402..403, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 405..406, + parameter: Parameter { + range: 405..406, + name: Identifier { + id: "c", + range: 405..406, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 411..412, + parameter: Parameter { + range: 411..412, + name: Identifier { + id: "d", + range: 411..412, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 414..418, + parameter: Parameter { + range: 414..415, + name: Identifier { + id: "e", + range: 414..415, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 416..418, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 420..424, + parameter: Parameter { + range: 420..421, + name: Identifier { + id: "f", + range: 420..421, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 422..424, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 431..435, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 438..548, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_and_keyword_parameters_with_defaults_and_varargs", + range: 442..501, + }, + type_params: None, + parameters: Parameters { + range: 501..538, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 507..508, + parameter: Parameter { + range: 507..508, + name: Identifier { + id: "a", + range: 507..508, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 510..511, + parameter: Parameter { + range: 510..511, + name: Identifier { + id: "b", + range: 510..511, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 513..514, + parameter: Parameter { + range: 513..514, + name: Identifier { + id: "c", + range: 513..514, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 517..521, + name: Identifier { + id: "args", + range: 517..521, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 523..524, + parameter: Parameter { + range: 523..524, + name: Identifier { + id: "d", + range: 523..524, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 526..530, + parameter: Parameter { + range: 526..527, + name: Identifier { + id: "e", + range: 526..527, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 528..530, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 532..536, + parameter: Parameter { + range: 532..533, + name: Identifier { + id: "f", + range: 532..533, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 534..536, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 544..548, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 551..682, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_and_keyword_parameters_with_defaults_and_varargs_and_kwargs", + range: 555..625, + }, + type_params: None, + parameters: Parameters { + range: 625..672, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 631..632, + parameter: Parameter { + range: 631..632, + name: Identifier { + id: "a", + range: 631..632, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 634..635, + parameter: Parameter { + range: 634..635, + name: Identifier { + id: "b", + range: 634..635, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 637..638, + parameter: Parameter { + range: 637..638, + name: Identifier { + id: "c", + range: 637..638, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 641..645, + name: Identifier { + id: "args", + range: 641..645, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 647..648, + parameter: Parameter { + range: 647..648, + name: Identifier { + id: "d", + range: 647..648, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 650..654, + parameter: Parameter { + range: 650..651, + name: Identifier { + id: "e", + range: 650..651, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 652..654, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 656..660, + parameter: Parameter { + range: 656..657, + name: Identifier { + id: "f", + range: 656..657, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 658..660, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 664..670, + name: Identifier { + id: "kwargs", + range: 664..670, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 678..682, + }, + ), + ], + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__with.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__with.py.snap new file mode 100644 index 0000000000000..e1cde9b732d30 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__with.py.snap @@ -0,0 +1,51 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/valid/statement/with.py +--- +## AST + + ``` +Module( + ModModule { + range: 0..22, + body: [ + With( + StmtWith { + range: 0..21, + is_async: false, + items: [ + WithItem { + range: 5..11, + context_expr: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 10..11, + id: "x", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Pass( + StmtPass { + range: 17..21, + }, + ), + ], + }, + ), + ], + }, +) +``` + diff --git a/crates/ruff_python_semantic/src/analyze/terminal.rs b/crates/ruff_python_semantic/src/analyze/terminal.rs index acb9483ba24ab..faa5dc070fcb6 100644 --- a/crates/ruff_python_semantic/src/analyze/terminal.rs +++ b/crates/ruff_python_semantic/src/analyze/terminal.rs @@ -339,6 +339,9 @@ fn is_wildcard(pattern: &ast::MatchCase) -> bool { | ast::Pattern::MatchMapping(_) | ast::Pattern::MatchClass(_) | ast::Pattern::MatchStar(_) => false, + + #[allow(deprecated)] + ast::Pattern::Invalid(_) => false, ast::Pattern::MatchAs(ast::PatternMatchAs { pattern, .. }) => pattern.is_none(), ast::Pattern::MatchOr(ast::PatternMatchOr { patterns, .. }) => { patterns.iter().all(is_wildcard_pattern) diff --git a/crates/ruff_python_semantic/src/analyze/type_inference.rs b/crates/ruff_python_semantic/src/analyze/type_inference.rs index 4a96689473379..39bf91096be56 100644 --- a/crates/ruff_python_semantic/src/analyze/type_inference.rs +++ b/crates/ruff_python_semantic/src/analyze/type_inference.rs @@ -329,6 +329,8 @@ impl From<&Expr> for ResolvedPythonType { | Expr::Name(_) | Expr::Slice(_) | Expr::IpyEscapeCommand(_) => ResolvedPythonType::Unknown, + #[allow(deprecated)] + Expr::Invalid(_) => ResolvedPythonType::Unknown, } } } diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 93dd398730034..acefe9898be6c 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -20,6 +20,7 @@ cargo-fuzz = true ruff_linter = { path = "../crates/ruff_linter" } ruff_python_ast = { path = "../crates/ruff_python_ast" } ruff_python_codegen = { path = "../crates/ruff_python_codegen" } +ruff_python_index = { path = "../crates/ruff_python_index" } ruff_python_parser = { path = "../crates/ruff_python_parser" } ruff_source_file = { path = "../crates/ruff_source_file" } ruff_python_formatter = { path = "../crates/ruff_python_formatter"} @@ -36,6 +37,10 @@ members = ["."] name = "ruff_parse_simple" path = "fuzz_targets/ruff_parse_simple.rs" +[[bin]] +name = "ruff_new_parser_equiv" +path = "fuzz_targets/ruff_new_parser_equiv.rs" + [[bin]] name = "ruff_fix_validity" path = "fuzz_targets/ruff_fix_validity.rs" diff --git a/fuzz/README.md b/fuzz/README.md index 1b91e57d05c1f..6ce2e9ed4e374 100644 --- a/fuzz/README.md +++ b/fuzz/README.md @@ -102,6 +102,14 @@ This fuzz harness checks that fixes applied by Ruff do not introduce new errors It currently is only configured to use default settings, but may be extended in future versions to test non-default linter settings. +### `ruff_new_parser_equiv` + +This fuzz harness was introduced to ensure that the handwritten parser ("new" parser) is +equivalent to the one which is automatically generated from a grammar. +This ensures that optimisations applied to the handwritten parser are indeed valid. +It is implemented by executing both the lalrpop-generated and the new parsers, then comparing +their output. + ### `ruff_formatter_idempotency` This fuzz harness ensures that the formatter is [idempotent](https://en.wikipedia.org/wiki/Idempotence) diff --git a/fuzz/corpus/ruff_new_parser_equiv b/fuzz/corpus/ruff_new_parser_equiv new file mode 120000 index 0000000000000..38dc5bc1ea310 --- /dev/null +++ b/fuzz/corpus/ruff_new_parser_equiv @@ -0,0 +1 @@ +ruff_fix_validity \ No newline at end of file diff --git a/fuzz/fuzz_targets/ruff_new_parser_equiv.rs b/fuzz/fuzz_targets/ruff_new_parser_equiv.rs new file mode 100644 index 0000000000000..b8d6ead67b901 --- /dev/null +++ b/fuzz/fuzz_targets/ruff_new_parser_equiv.rs @@ -0,0 +1,38 @@ +//! Fuzzer harness which ensures that the handwritten implementation of the parser ("new") is equivalent to the +//! auto-generated lalrpop version. + +#![no_main] + +use libfuzzer_sys::{fuzz_target, Corpus}; + +use ruff_python_ast::PySourceType; +use ruff_python_index::tokens_and_ranges; +use ruff_python_parser::{parse_tokens, set_new_parser, AsMode}; + +// modified from ruff_python_formatter::quick_test +fn do_fuzz(case: &[u8]) -> Corpus { + let Ok(source) = std::str::from_utf8(case) else { + return Corpus::Reject; + }; + + let source_type = PySourceType::Python; + let Ok((tokens, _comment_ranges)) = tokens_and_ranges(source, source_type) else { + return Corpus::Keep; // keep even rejected source code as this may allow us to explore later + }; + + set_new_parser(true); + let module_new = parse_tokens(tokens.clone(), source, source_type.as_mode()); + + set_new_parser(false); + let module_lalrpop = parse_tokens(tokens, source, source_type.as_mode()); + + assert_eq!(module_lalrpop.is_ok(), module_new.is_ok()); + + if let (Ok(module_lalrpop), Ok(module_new)) = (module_lalrpop, module_new) { + assert_eq!(module_lalrpop, module_new); + } + + Corpus::Keep +} + +fuzz_target!(|case: &[u8]| -> Corpus { do_fuzz(case) }); diff --git a/fuzz/fuzz_targets/ruff_parse_simple.rs b/fuzz/fuzz_targets/ruff_parse_simple.rs index 24a998336b110..657e8c1449ab6 100644 --- a/fuzz/fuzz_targets/ruff_parse_simple.rs +++ b/fuzz/fuzz_targets/ruff_parse_simple.rs @@ -17,8 +17,8 @@ fn do_fuzz(case: &[u8]) -> Corpus { let locator = Locator::new(code); let python_ast = match parse_suite(code) { Ok(stmts) => stmts, - Err(ParseError { offset, .. }) => { - let offset = offset.to_usize(); + Err(ParseError { location, .. }) => { + let offset = location.start().to_usize(); assert!( code.is_char_boundary(offset), "Invalid error location {} (not at char boundary)", @@ -47,7 +47,7 @@ fn do_fuzz(case: &[u8]) -> Corpus { ); } Err(err) => { - let offset = err.location().to_usize(); + let offset = err.location().start().to_usize(); assert!( code.is_char_boundary(offset), "Invalid error location {} (not at char boundary)",