From 25cc1da319e66acf42340d498de3507e64d31732 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 17 May 2023 10:16:58 +0200 Subject: [PATCH 1/2] Include decorators in `Class` and `FunctionDef` range --- parser/src/python.lalrpop | 4 ++-- parser/src/python.rs | 46 +++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index 3de5197d..011e764b 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -964,7 +964,7 @@ WithItem: ast::Withitem = { }; FuncDef: ast::Stmt = { - "def" " >)?> ":" => { + "def" " >)?> ":" => { let args = Box::new(args); let returns = r.map(|x| Box::new(x)); let end_location = body.last().unwrap().end(); @@ -1135,7 +1135,7 @@ KwargParameter: Option> = { }; ClassDef: ast::Stmt = { - "class" ":" => { + "class" ":" => { let (bases, keywords) = match a { Some((_, arg, _)) => (arg.args, arg.keywords), None => (vec![], vec![]), diff --git a/parser/src/python.rs b/parser/src/python.rs index 6441474f..6ea0894d 100644 --- a/parser/src/python.rs +++ b/parser/src/python.rs @@ -1,5 +1,5 @@ // auto-generated: "lalrpop 0.20.0" -// sha3: a33e9abb4be2a3730161519ce0f298452edbc50335b9e0c812d5a1730f1d8816 +// sha3: f95a98ddc92e7f056dba3f921c7c9b36d7725b180106324fd00b6157a1c36fe7 use crate::{ ast::{self as ast, Ranged}, lexer::{LexicalError, LexicalErrorType}, @@ -30961,8 +30961,8 @@ fn __action156< #[allow(clippy::too_many_arguments)] fn __action157< >( - (_, decorator_list, _): (TextSize, alloc::vec::Vec, TextSize), (_, location, _): (TextSize, TextSize, TextSize), + (_, decorator_list, _): (TextSize, alloc::vec::Vec, TextSize), (_, is_async, _): (TextSize, core::option::Option, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, name, _): (TextSize, ast::Identifier, TextSize), @@ -31055,8 +31055,8 @@ fn __action161< #[allow(clippy::too_many_arguments)] fn __action162< >( - (_, decorator_list, _): (TextSize, alloc::vec::Vec, TextSize), (_, location, _): (TextSize, TextSize, TextSize), + (_, decorator_list, _): (TextSize, alloc::vec::Vec, TextSize), (_, _, _): (TextSize, token::Tok, TextSize), (_, name, _): (TextSize, ast::Identifier, TextSize), (_, a, _): (TextSize, core::option::Option<(token::Tok, ArgumentList, token::Tok)>, TextSize), @@ -37788,8 +37788,8 @@ fn __action636< #[allow(clippy::too_many_arguments)] fn __action637< >( - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, TextSize, TextSize), + __0: (TextSize, TextSize, TextSize), + __1: (TextSize, alloc::vec::Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, token::Tok, TextSize), __4: (TextSize, ast::Identifier, TextSize), @@ -37821,8 +37821,8 @@ fn __action637< #[allow(clippy::too_many_arguments)] fn __action638< >( - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, TextSize, TextSize), + __0: (TextSize, TextSize, TextSize), + __1: (TextSize, alloc::vec::Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Identifier, TextSize), __4: (TextSize, ast::Arguments, TextSize), @@ -37991,8 +37991,8 @@ fn __action643< #[allow(clippy::too_many_arguments)] fn __action644< >( - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, TextSize, TextSize), + __0: (TextSize, TextSize, TextSize), + __1: (TextSize, alloc::vec::Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Identifier, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -38024,8 +38024,8 @@ fn __action644< #[allow(clippy::too_many_arguments)] fn __action645< >( - __0: (TextSize, alloc::vec::Vec, TextSize), - __1: (TextSize, TextSize, TextSize), + __0: (TextSize, TextSize, TextSize), + __1: (TextSize, alloc::vec::Vec, TextSize), __2: (TextSize, token::Tok, TextSize), __3: (TextSize, ast::Identifier, TextSize), __4: (TextSize, token::Tok, TextSize), @@ -40293,16 +40293,16 @@ fn __action738< __7: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt { - let __start0 = __0.2; - let __end0 = __1.0; + let __start0 = __0.0; + let __end0 = __0.0; let __temp0 = __action371( &__start0, &__end0, ); let __temp0 = (__start0, __temp0, __end0); __action644( - __0, __temp0, + __0, __1, __2, __3, @@ -40323,16 +40323,16 @@ fn __action739< __4: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt { - let __start0 = __0.2; - let __end0 = __1.0; + let __start0 = __0.0; + let __end0 = __0.0; let __temp0 = __action371( &__start0, &__end0, ); let __temp0 = (__start0, __temp0, __end0); __action645( - __0, __temp0, + __0, __1, __2, __3, @@ -41316,16 +41316,16 @@ fn __action777< __7: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt { - let __start0 = __0.2; - let __end0 = __1.0; + let __start0 = __0.0; + let __end0 = __0.0; let __temp0 = __action371( &__start0, &__end0, ); let __temp0 = (__start0, __temp0, __end0); __action637( - __0, __temp0, + __0, __1, __2, __3, @@ -41348,16 +41348,16 @@ fn __action778< __6: (TextSize, ast::Suite, TextSize), ) -> ast::Stmt { - let __start0 = __0.2; - let __end0 = __1.0; + let __start0 = __0.0; + let __end0 = __0.0; let __temp0 = __action371( &__start0, &__end0, ); let __temp0 = (__start0, __temp0, __end0); __action638( - __0, __temp0, + __0, __1, __2, __3, From 41a0ef87402d8b2fb339b978199fe5149bb69508 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 19 May 2023 10:06:31 +0200 Subject: [PATCH 2/2] Add test --- parser/src/parser.rs | 20 ++++++ ...rser__parser__tests__decorator_ranges.snap | 72 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 77f836e4..8ecce8d6 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -853,4 +853,24 @@ def args_to_tuple(*args: *Ts) -> Tuple[*Ts]: ... .unwrap(); insta::assert_debug_snapshot!(parse_ast); } + + #[test] + #[cfg(not(feature = "all-nodes-with-ranges"))] + fn decorator_ranges() { + let parse_ast = parse_program( + r#" +@my_decorator +def test(): + pass + +@class_decorator +class Abcd: + pass +"# + .trim(), + "", + ) + .unwrap(); + insta::assert_debug_snapshot!(parse_ast); + } } diff --git a/parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap b/parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap new file mode 100644 index 00000000..41910455 --- /dev/null +++ b/parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap @@ -0,0 +1,72 @@ +--- +source: parser/src/parser.rs +expression: parse_ast +--- +[ + FunctionDef( + StmtFunctionDef { + range: 0..34, + name: Identifier( + "test", + ), + args: Arguments { + range: (), + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + Pass( + StmtPass { + range: 30..34, + }, + ), + ], + decorator_list: [ + Name( + ExprName { + range: 1..13, + id: Identifier( + "my_decorator", + ), + ctx: Load, + }, + ), + ], + returns: None, + type_comment: None, + }, + ), + ClassDef( + StmtClassDef { + range: 40..77, + name: Identifier( + "Abcd", + ), + bases: [], + keywords: [], + body: [ + Pass( + StmtPass { + range: 73..77, + }, + ), + ], + decorator_list: [ + Name( + ExprName { + range: 41..56, + id: Identifier( + "class_decorator", + ), + ctx: Load, + }, + ), + ], + }, + ), +]