From 1871a1632e310985414211222f5bf8069678892f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 4 Mar 2023 12:42:05 -0500 Subject: [PATCH] Treat match and case as soft keywords in lambda assignments (#4623) --- compiler/parser/src/parser.rs | 2 + ...r__parser__tests__match_as_identifier.snap | 236 ++++++++++++++++++ compiler/parser/src/soft_keywords.rs | 6 +- 3 files changed, 243 insertions(+), 1 deletion(-) diff --git a/compiler/parser/src/parser.rs b/compiler/parser/src/parser.rs index 04875c44e3..6376e0467f 100644 --- a/compiler/parser/src/parser.rs +++ b/compiler/parser/src/parser.rs @@ -604,6 +604,8 @@ match match: case 1: pass case 2: pass +match = lambda query: query == event +print(match(12)) "#, "", ) diff --git a/compiler/parser/src/snapshots/rustpython_parser__parser__tests__match_as_identifier.snap b/compiler/parser/src/snapshots/rustpython_parser__parser__tests__match_as_identifier.snap index 4a3c8af35f..2fb15a7f14 100644 --- a/compiler/parser/src/snapshots/rustpython_parser__parser__tests__match_as_identifier.snap +++ b/compiler/parser/src/snapshots/rustpython_parser__parser__tests__match_as_identifier.snap @@ -1671,4 +1671,240 @@ expression: parse_ast ], }, }, + Located { + location: Location { + row: 21, + column: 0, + }, + end_location: Some( + Location { + row: 21, + column: 36, + }, + ), + custom: (), + node: Assign { + targets: [ + Located { + location: Location { + row: 21, + column: 0, + }, + end_location: Some( + Location { + row: 21, + column: 5, + }, + ), + custom: (), + node: Name { + id: "match", + ctx: Store, + }, + }, + ], + value: Located { + location: Location { + row: 21, + column: 8, + }, + end_location: Some( + Location { + row: 21, + column: 36, + }, + ), + custom: (), + node: Lambda { + args: Arguments { + posonlyargs: [], + args: [ + Located { + location: Location { + row: 21, + column: 15, + }, + end_location: Some( + Location { + row: 21, + column: 20, + }, + ), + custom: (), + node: ArgData { + arg: "query", + annotation: None, + type_comment: None, + }, + }, + ], + vararg: None, + kwonlyargs: [], + kw_defaults: [], + kwarg: None, + defaults: [], + }, + body: Located { + location: Location { + row: 21, + column: 22, + }, + end_location: Some( + Location { + row: 21, + column: 36, + }, + ), + custom: (), + node: Compare { + left: Located { + location: Location { + row: 21, + column: 22, + }, + end_location: Some( + Location { + row: 21, + column: 27, + }, + ), + custom: (), + node: Name { + id: "query", + ctx: Load, + }, + }, + ops: [ + Eq, + ], + comparators: [ + Located { + location: Location { + row: 21, + column: 31, + }, + end_location: Some( + Location { + row: 21, + column: 36, + }, + ), + custom: (), + node: Name { + id: "event", + ctx: Load, + }, + }, + ], + }, + }, + }, + }, + type_comment: None, + }, + }, + Located { + location: Location { + row: 22, + column: 0, + }, + end_location: Some( + Location { + row: 22, + column: 16, + }, + ), + custom: (), + node: Expr { + value: Located { + location: Location { + row: 22, + column: 0, + }, + end_location: Some( + Location { + row: 22, + column: 16, + }, + ), + custom: (), + node: Call { + func: Located { + location: Location { + row: 22, + column: 0, + }, + end_location: Some( + Location { + row: 22, + column: 5, + }, + ), + custom: (), + node: Name { + id: "print", + ctx: Load, + }, + }, + args: [ + Located { + location: Location { + row: 22, + column: 6, + }, + end_location: Some( + Location { + row: 22, + column: 15, + }, + ), + custom: (), + node: Call { + func: Located { + location: Location { + row: 22, + column: 6, + }, + end_location: Some( + Location { + row: 22, + column: 11, + }, + ), + custom: (), + node: Name { + id: "match", + ctx: Load, + }, + }, + args: [ + Located { + location: Location { + row: 22, + column: 12, + }, + end_location: Some( + Location { + row: 22, + column: 14, + }, + ), + custom: (), + node: Constant { + value: Int( + 12, + ), + kind: None, + }, + }, + ], + keywords: [], + }, + }, + ], + keywords: [], + }, + }, + }, + }, ] diff --git a/compiler/parser/src/soft_keywords.rs b/compiler/parser/src/soft_keywords.rs index 2613914f21..b7b034f544 100644 --- a/compiler/parser/src/soft_keywords.rs +++ b/compiler/parser/src/soft_keywords.rs @@ -59,11 +59,15 @@ where let mut nesting = 0; let mut first = true; let mut seen_colon = false; + let mut seen_lambda = false; while let Some(Ok((_, tok, _))) = self.underlying.peek() { match tok { Tok::Newline => break, + Tok::Lambda if nesting == 0 => seen_lambda = true, Tok::Colon if nesting == 0 => { - if !first { + if seen_lambda { + seen_lambda = false; + } else if !first { seen_colon = true; } }