From 8a415fa61e912f27c3d7be61621427796e32a5ec Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 7 Jun 2023 22:26:20 +0200 Subject: [PATCH] Include argument parentheses in range (#5) --- parser/src/function.rs | 7 ++- parser/src/parser.rs | 2 +- parser/src/python.lalrpop | 8 ++- parser/src/python.rs | 10 ++- ...__tests__function_no_args_with_ranges.snap | 36 +++++++++++ ..._tests__function_pos_args_with_ranges.snap | 61 +++++++++++++++++++ ...rser__parser__tests__decorator_ranges.snap | 6 +- 7 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 parser/src/snapshots/rustpython_parser__function__tests__function_no_args_with_ranges.snap create mode 100644 parser/src/snapshots/rustpython_parser__function__tests__function_pos_args_with_ranges.snap diff --git a/parser/src/function.rs b/parser/src/function.rs index ec6d53bf72..2fe262bbb9 100644 --- a/parser/src/function.rs +++ b/parser/src/function.rs @@ -136,7 +136,6 @@ mod tests { use super::*; use crate::{ast, parser::ParseErrorType, Parse}; - #[cfg(feature = "all-nodes-with-ranges")] macro_rules! function_and_lambda { ($($name:ident: $code:expr,)*) => { $( @@ -149,6 +148,12 @@ mod tests { } } + #[cfg(feature = "all-nodes-with-ranges")] + function_and_lambda! { + test_function_no_args_with_ranges: "def f(): pass", + test_function_pos_args_with_ranges: "def f(a, b, c): pass", + } + #[cfg(feature = "all-nodes-with-ranges")] function_and_lambda! { test_function_no_args: "def f(): pass", diff --git a/parser/src/parser.rs b/parser/src/parser.rs index fdbeab70b5..7cfbae35d7 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -1109,7 +1109,7 @@ def args_to_tuple(*args: *Ts) -> Tuple[*Ts]: ... @my_decorator def test(): pass - + @class_decorator class Abcd: pass diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index 0d170f6727..63191a5858 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -981,8 +981,14 @@ FuncDef: ast::Stmt = { Parameters: ast::Arguments = { "(" )?> ")" =>? { a.as_ref().map(validate_arguments).transpose()?; + + let range = optional_range(location, end_location); let args = a - .unwrap_or_else(|| ast::Arguments::empty(optional_range(location, end_location))); + .map(|mut arguments| { + arguments.range = range; + arguments + }) + .unwrap_or_else(|| ast::Arguments::empty(range)); Ok(args) } diff --git a/parser/src/python.rs b/parser/src/python.rs index ae086e45b2..256ca7aef4 100644 --- a/parser/src/python.rs +++ b/parser/src/python.rs @@ -1,5 +1,5 @@ // auto-generated: "lalrpop 0.19.8" -// sha3: 82bdc299d8185d1aa4a2c969643cfcf7097ba66bb460cb6ae85023c1f358e750 +// sha3: 78e4fe2d25728ae4a1a411a48ffdc7845d05f16bacd09d23349a56b99a3eeaf4 use crate::{ ast::{self as ast, Ranged, bigint::BigInt}, lexer::{LexicalError, LexicalErrorType}, @@ -25564,8 +25564,14 @@ fn __action158< { { a.as_ref().map(validate_arguments).transpose()?; + + let range = optional_range(location, end_location); let args = a - .unwrap_or_else(|| ast::Arguments::empty(optional_range(location, end_location))); + .map(|mut arguments| { + arguments.range = range; + arguments + }) + .unwrap_or_else(|| ast::Arguments::empty(range)); Ok(args) } diff --git a/parser/src/snapshots/rustpython_parser__function__tests__function_no_args_with_ranges.snap b/parser/src/snapshots/rustpython_parser__function__tests__function_no_args_with_ranges.snap new file mode 100644 index 0000000000..d1bab55ee3 --- /dev/null +++ b/parser/src/snapshots/rustpython_parser__function__tests__function_no_args_with_ranges.snap @@ -0,0 +1,36 @@ +--- +source: parser/src/function.rs +expression: parse_ast +--- +Ok( + [ + FunctionDef( + StmtFunctionDef { + range: 0..13, + name: Identifier( + "f", + ), + args: Arguments { + range: 5..7, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + ], +) diff --git a/parser/src/snapshots/rustpython_parser__function__tests__function_pos_args_with_ranges.snap b/parser/src/snapshots/rustpython_parser__function__tests__function_pos_args_with_ranges.snap new file mode 100644 index 0000000000..f1de4fda5f --- /dev/null +++ b/parser/src/snapshots/rustpython_parser__function__tests__function_pos_args_with_ranges.snap @@ -0,0 +1,61 @@ +--- +source: parser/src/function.rs +expression: parse_ast +--- +Ok( + [ + FunctionDef( + StmtFunctionDef { + range: 0..20, + name: Identifier( + "f", + ), + args: Arguments { + range: 5..14, + posonlyargs: [], + args: [ + Arg { + range: 6..7, + arg: Identifier( + "a", + ), + annotation: None, + type_comment: None, + }, + Arg { + range: 9..10, + arg: Identifier( + "b", + ), + annotation: None, + type_comment: None, + }, + Arg { + range: 12..13, + arg: Identifier( + "c", + ), + annotation: None, + type_comment: None, + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: [ + Pass( + StmtPass { + range: 16..20, + }, + ), + ], + decorator_list: [], + returns: None, + type_comment: None, + }, + ), + ], +) diff --git a/parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap b/parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap index ed34573933..b7484bdf16 100644 --- a/parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap +++ b/parser/src/snapshots/rustpython_parser__parser__tests__decorator_ranges.snap @@ -41,7 +41,7 @@ expression: parse_ast ), ClassDef( StmtClassDef { - range: 40..77, + range: 36..73, name: Identifier( "Abcd", ), @@ -50,14 +50,14 @@ expression: parse_ast body: [ Pass( StmtPass { - range: 73..77, + range: 69..73, }, ), ], decorator_list: [ Name( ExprName { - range: 41..56, + range: 37..52, id: Identifier( "class_decorator", ),