Skip to content

Commit f43e5b7

Browse files
authored
Merge pull request #4552 from charliermarsh/charlie/loc
Limit match range to end of last statement
2 parents a19f294 + 7ebef61 commit f43e5b7

4 files changed

+109
-85
lines changed

parser/python.lalrpop

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,15 @@ CompoundStatement: ast::Stmt = {
358358
};
359359

360360
MatchStatement: ast::Stmt = {
361-
<location:@L> "match" <subject:TestOrStarNamedExpr> ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
361+
<location:@L> "match" <subject:TestOrStarNamedExpr> ":" "\n" Indent <cases:MatchCase+> Dedent => {
362+
let end_location = cases
363+
.last()
364+
.unwrap()
365+
.body
366+
.last()
367+
.unwrap()
368+
.end_location
369+
.unwrap();
362370
ast::Stmt {
363371
location,
364372
end_location: Some(end_location),
@@ -369,7 +377,15 @@ MatchStatement: ast::Stmt = {
369377
}
370378
}
371379
},
372-
<location:@L> "match" <subject:TestOrStarNamedExpr> "," ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
380+
<location:@L> "match" <subject:TestOrStarNamedExpr> "," ":" "\n" Indent <cases:MatchCase+> Dedent => {
381+
let end_location = cases
382+
.last()
383+
.unwrap()
384+
.body
385+
.last()
386+
.unwrap()
387+
.end_location
388+
.unwrap();
373389
ast::Stmt {
374390
location,
375391
end_location: Some(end_location),
@@ -380,7 +396,15 @@ MatchStatement: ast::Stmt = {
380396
}
381397
}
382398
},
383-
<location:@L> "match" <subject:TestOrStarNamedExpr> "," <subjects:OneOrMore<TestOrStarNamedExpr>> ","? ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
399+
<location:@L> "match" <subject:TestOrStarNamedExpr> "," <subjects:OneOrMore<TestOrStarNamedExpr>> ","? ":" "\n" Indent <cases:MatchCase+> Dedent => {
400+
let end_location = cases
401+
.last()
402+
.unwrap()
403+
.body
404+
.last()
405+
.unwrap()
406+
.end_location
407+
.unwrap();
384408
let mut subjects = subjects;
385409
subjects.insert(0, subject);
386410
ast::Stmt {

parser/src/snapshots/rustpython_parser__parser__tests__match.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ expression: parse_ast
1010
},
1111
end_location: Some(
1212
Location {
13-
row: 7,
14-
column: 0,
13+
row: 6,
14+
column: 19,
1515
},
1616
),
1717
custom: (),
@@ -178,8 +178,8 @@ expression: parse_ast
178178
},
179179
end_location: Some(
180180
Location {
181-
row: 12,
182-
column: 0,
181+
row: 11,
182+
column: 20,
183183
},
184184
),
185185
custom: (),
@@ -453,8 +453,8 @@ expression: parse_ast
453453
},
454454
end_location: Some(
455455
Location {
456-
row: 15,
457-
column: 0,
456+
row: 14,
457+
column: 13,
458458
},
459459
),
460460
custom: (),
@@ -631,8 +631,8 @@ expression: parse_ast
631631
},
632632
end_location: Some(
633633
Location {
634-
row: 18,
635-
column: 0,
634+
row: 17,
635+
column: 13,
636636
},
637637
),
638638
custom: (),
@@ -809,8 +809,8 @@ expression: parse_ast
809809
},
810810
end_location: Some(
811811
Location {
812-
row: 21,
813-
column: 0,
812+
row: 20,
813+
column: 13,
814814
},
815815
),
816816
custom: (),

parser/src/snapshots/rustpython_parser__parser__tests__match_as_identifier.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,8 +1538,8 @@ expression: parse_ast
15381538
},
15391539
end_location: Some(
15401540
Location {
1541-
row: 21,
1542-
column: 0,
1541+
row: 20,
1542+
column: 12,
15431543
},
15441544
),
15451545
custom: (),

0 commit comments

Comments
 (0)