From 0f26a0369efc7305a1a0120355f78d85b3030e56 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Mon, 10 Jan 2022 23:22:07 +0300 Subject: [PATCH 1/1] Fix handling of standalone match/case with newlines/comments (#2760) Resolves #2759 --- CHANGES.md | 2 + src/blib2to3/pgen2/parse.py | 4 ++ tests/data/pattern_matching_style.py | 68 +++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a1c8ccb..748dbca 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,8 @@ be formatted. (#2526) - Speed-up the new backtracking parser about 4X in general (enabled when `--target-version` is set to 3.10 and higher). (#2728) +- Fix handling of standalone `match()` or `case()` when there is a trailing newline or a + comment inside of the parentheses. (#2760) ### Packaging diff --git a/src/blib2to3/pgen2/parse.py b/src/blib2to3/pgen2/parse.py index 8fe9667..4a23d53 100644 --- a/src/blib2to3/pgen2/parse.py +++ b/src/blib2to3/pgen2/parse.py @@ -269,6 +269,10 @@ class Parser(object): break next_token_type, next_token_value, *_ = proxy.eat(counter) + if next_token_type in (tokenize.COMMENT, tokenize.NL): + counter += 1 + continue + if next_token_type == tokenize.OP: next_token_type = grammar.opmap[next_token_value] diff --git a/tests/data/pattern_matching_style.py b/tests/data/pattern_matching_style.py index c1c0aee..8e18ce2 100644 --- a/tests/data/pattern_matching_style.py +++ b/tests/data/pattern_matching_style.py @@ -6,10 +6,52 @@ match something: ): print(1) case c( very_complex=True, - perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 + perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1, ): print(2) case a: pass +match( + arg # comment +) + +match( +) + +match( + + +) + +case( + arg # comment +) + +case( +) + +case( + + +) + + +re.match( + something # fast +) +re.match( + + + +) +match match( + + +): + case case( + arg, # comment + ): + pass + # output match something: @@ -20,8 +62,30 @@ match something: ): print(1) case c( - very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 + very_complex=True, + perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1, ): print(2) case a: pass + +match(arg) # comment + +match() + +match() + +case(arg) # comment + +case() + +case() + + +re.match(something) # fast +re.match() +match match(): + case case( + arg, # comment + ): + pass -- 2.39.5