diff --git a/CHANGES.md b/CHANGES.md index a1c8ccb0b7d..748dbca7019 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 8fe96672897..4a23d538b49 100644 --- a/src/blib2to3/pgen2/parse.py +++ b/src/blib2to3/pgen2/parse.py @@ -269,6 +269,10 @@ def addtoken(self, type: int, value: Text, context: Context) -> bool: 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 c1c0aeedb70..8e18ce2ada6 100644 --- a/tests/data/pattern_matching_style.py +++ b/tests/data/pattern_matching_style.py @@ -6,10 +6,52 @@ ): 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 @@ ): 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