Skip to content

Commit

Permalink
Fix range of unparenthesized tuple subject in match statement
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 21, 2023
1 parent f6d6200 commit f83e6f9
Show file tree
Hide file tree
Showing 8 changed files with 1,747 additions and 1,352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,17 @@ def foo():
# comment
a, b,):
pass

# Tuple subject.
match n % 3, n % 5:
case 0, 0:
# n is divisible by both 3 and 5
print("FizzBuzz")
case 0, _:
# n is divisible by 3, but not 5
print("Fizz")
case _, 0:
# n is divisible by 5, but not 3
print("Buzz")
case _:
print(n)
1 change: 1 addition & 0 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ fn handle_enclosed_comment<'a>(
}
AnyNodeRef::WithItem(_) => handle_with_item_comment(comment, locator),
AnyNodeRef::PatternMatchSequence(pattern_match_sequence) => {
println!("pattern_match_sequence");
if SequenceType::from_pattern(pattern_match_sequence, locator.contents())
.is_parenthesized()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,20 @@ match pattern:
# comment
a, b,):
pass
# Tuple subject.
match n % 3, n % 5:
case 0, 0:
# n is divisible by both 3 and 5
print("FizzBuzz")
case 0, _:
# n is divisible by 3, but not 5
print("Fizz")
case _, 0:
# n is divisible by 5, but not 3
print("Buzz")
case _:
print(n)
```

## Output
Expand Down Expand Up @@ -1182,6 +1196,20 @@ match pattern:
b,
):
pass
# Tuple subject.
match n % 3, n % 5:
case 0, 0:
# n is divisible by both 3 and 5
print("FizzBuzz")
case 0, _:
# n is divisible by 3, but not 5
print("Fizz")
case _, 0:
# n is divisible by 5, but not 3
print("Buzz")
case _:
print(n)
```


Expand Down
9 changes: 9 additions & 0 deletions crates/ruff_python_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,15 @@ match x:
match x:
case (0,):
y = 0
match x,:
case z:
pass
match x, y:
case z:
pass
match x, y,:
case z:
pass
"#,
"<test>",
)
Expand Down
14 changes: 10 additions & 4 deletions crates/ruff_python_parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ MatchStatement: ast::Stmt = {
}
)
},
<location:@L> "match" <subject:TestOrStarNamedExpr> "," ":" "\n" Indent <cases:MatchCase+> Dedent => {
<location:@L> "match" <tuple_location:@L> <subject:TestOrStarNamedExpr> "," <tuple_end_location:@R> ":" "\n" Indent <cases:MatchCase+> Dedent => {
let end_location = cases
.last()
.unwrap()
Expand All @@ -476,13 +476,19 @@ MatchStatement: ast::Stmt = {
.end();
ast::Stmt::Match(
ast::StmtMatch {
subject: Box::new(subject.into()),
subject: Box::new(ast::Expr::Tuple(
ast::ExprTuple {
elts: vec![subject.into()],
ctx: ast::ExprContext::Load,
range: (tuple_location..tuple_end_location).into()
},
)),
cases,
range: (location..end_location).into()
}
)
},
<location:@L> "match" <elts:TwoOrMore<TestOrStarNamedExpr, ",">> ","? ":" "\n" Indent <cases:MatchCase+> Dedent => {
<location:@L> "match" <tuple_location:@L> <elts:TwoOrMore<TestOrStarNamedExpr, ",">> ","? <tuple_end_location:@R> ":" "\n" Indent <cases:MatchCase+> Dedent => {
let end_location = cases
.last()
.unwrap()
Expand All @@ -497,7 +503,7 @@ MatchStatement: ast::Stmt = {
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into()
range: (tuple_location..tuple_end_location).into()
},
)),
cases,
Expand Down
2,824 changes: 1,498 additions & 1,326 deletions crates/ruff_python_parser/src/python.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,153 @@ expression: parse_ast
],
},
),
Match(
StmtMatch {
range: 298..332,
subject: Tuple(
ExprTuple {
range: 304..306,
elts: [
Name(
ExprName {
range: 304..305,
id: "x",
ctx: Load,
},
),
],
ctx: Load,
},
),
cases: [
MatchCase {
range: 312..332,
pattern: MatchAs(
PatternMatchAs {
range: 317..318,
pattern: None,
name: Some(
Identifier {
id: "z",
range: 317..318,
},
),
},
),
guard: None,
body: [
Pass(
StmtPass {
range: 328..332,
},
),
],
},
],
},
),
Match(
StmtMatch {
range: 333..369,
subject: Tuple(
ExprTuple {
range: 339..343,
elts: [
Name(
ExprName {
range: 339..340,
id: "x",
ctx: Load,
},
),
Name(
ExprName {
range: 342..343,
id: "y",
ctx: Load,
},
),
],
ctx: Load,
},
),
cases: [
MatchCase {
range: 349..369,
pattern: MatchAs(
PatternMatchAs {
range: 354..355,
pattern: None,
name: Some(
Identifier {
id: "z",
range: 354..355,
},
),
},
),
guard: None,
body: [
Pass(
StmtPass {
range: 365..369,
},
),
],
},
],
},
),
Match(
StmtMatch {
range: 370..407,
subject: Tuple(
ExprTuple {
range: 376..381,
elts: [
Name(
ExprName {
range: 376..377,
id: "x",
ctx: Load,
},
),
Name(
ExprName {
range: 379..380,
id: "y",
ctx: Load,
},
),
],
ctx: Load,
},
),
cases: [
MatchCase {
range: 387..407,
pattern: MatchAs(
PatternMatchAs {
range: 392..393,
pattern: None,
name: Some(
Identifier {
id: "z",
range: 392..393,
},
),
},
),
guard: None,
body: [
Pass(
StmtPass {
range: 403..407,
},
),
],
},
],
},
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -3449,10 +3449,18 @@ expression: parse_ast
Match(
StmtMatch {
range: 2661..2697,
subject: Name(
ExprName {
range: 2667..2668,
id: "x",
subject: Tuple(
ExprTuple {
range: 2667..2669,
elts: [
Name(
ExprName {
range: 2667..2668,
id: "x",
ctx: Load,
},
),
],
ctx: Load,
},
),
Expand Down Expand Up @@ -3512,7 +3520,7 @@ expression: parse_ast
range: 2720..2760,
subject: Tuple(
ExprTuple {
range: 2720..2760,
range: 2726..2730,
elts: [
Name(
ExprName {
Expand Down Expand Up @@ -3598,23 +3606,31 @@ expression: parse_ast
Match(
StmtMatch {
range: 2783..2829,
subject: NamedExpr(
ExprNamedExpr {
range: 2789..2795,
target: Name(
ExprName {
range: 2789..2790,
id: "w",
ctx: Store,
},
),
value: Name(
ExprName {
range: 2794..2795,
id: "x",
ctx: Load,
},
),
subject: Tuple(
ExprTuple {
range: 2789..2796,
elts: [
NamedExpr(
ExprNamedExpr {
range: 2789..2795,
target: Name(
ExprName {
range: 2789..2790,
id: "w",
ctx: Store,
},
),
value: Name(
ExprName {
range: 2794..2795,
id: "x",
ctx: Load,
},
),
},
),
],
ctx: Load,
},
),
cases: [
Expand Down

0 comments on commit f83e6f9

Please sign in to comment.