Skip to content

Commit

Permalink
Fix handling of standalone match/case with newlines/comments (#2760)
Browse files Browse the repository at this point in the history
Resolves #2759
  • Loading branch information
isidentical authored Jan 10, 2022
1 parent 3e73152 commit 0f26a03
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/blib2to3/pgen2/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
68 changes: 66 additions & 2 deletions tests/data/pattern_matching_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

0 comments on commit 0f26a03

Please sign in to comment.