Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Fix empty lambda args range (#35)
Browse files Browse the repository at this point in the history
Previously, empty lambda arguments (e.g. `lambda: 1`) would get the
range of the entire expression, which leads to incorrect comment
placement. Now empty lambda arguments get an empty range between the
`lambda` and the `:` tokens.
  • Loading branch information
konstin authored Jul 21, 2023
1 parent fb365c6 commit 4d03b9b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
7 changes: 7 additions & 0 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,13 @@ mod tests {
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_parse_lambda_no_args() {
let source = "lambda: 1";
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_parse_tuples() {
let source = "a, b = 4, 5";
Expand Down
4 changes: 2 additions & 2 deletions parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -1245,10 +1245,10 @@ NamedExpression: ast::Expr = {
};

LambdaDef: ast::Expr = {
<location:@L> "lambda" <p:ParameterList<UntypedParameter, StarUntypedParameter, StarUntypedParameter>?> ":" <body:Test<"all">> <end_location:@R> =>? {
<location:@L> "lambda" <location_args:@L> <p:ParameterList<UntypedParameter, StarUntypedParameter, StarUntypedParameter>?> <end_location_args:@R> ":" <body:Test<"all">> <end_location:@R> =>? {
p.as_ref().map(validate_arguments).transpose()?;
let p = p
.unwrap_or_else(|| ast::Arguments::empty((location..end_location).into()));
.unwrap_or_else(|| ast::Arguments::empty((location_args..end_location_args).into()));

Ok(ast::Expr::Lambda(
ast::ExprLambda {
Expand Down
36 changes: 28 additions & 8 deletions parser/src/python.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d03b9b

Please sign in to comment.