Skip to content

Commit 91f1fc7

Browse files
committed
Fix other compound statements
1 parent 9f9ba02 commit 91f1fc7

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

compiler/parser/python.lalrpop

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,24 @@ CompoundStatement: ast::Stmt = {
357357
};
358358

359359
IfStatement: ast::Stmt = {
360-
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite @R)*> <s3:("else" ":" Suite)?> <end_location:@R> => {
360+
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite)*> <s3:("else" ":" Suite)?> => {
361361
// Determine last else:
362362
let mut last = s3.map(|s| s.2).unwrap_or_default();
363-
363+
let end_location = if let Some(last) = last.last() {
364+
last.end_location
365+
} else {
366+
if let Some(last) = s2.last() {
367+
last.4.last().unwrap().end_location
368+
} else {
369+
body.last().unwrap().end_location
370+
}
371+
};
364372
// handle elif:
365373
for i in s2.into_iter().rev() {
366374
let x = ast::Stmt {
367375
custom: (),
368376
location: i.0,
369-
end_location: Some(i.5),
377+
end_location: i.4.last().unwrap().end_location,
370378
node: ast::StmtKind::If { test: Box::new(i.2), body: i.4, orelse: last },
371379
};
372380
last = vec![x];
@@ -375,19 +383,24 @@ IfStatement: ast::Stmt = {
375383
ast::Stmt {
376384
custom: (),
377385
location,
378-
end_location: Some(end_location),
386+
end_location,
379387
node: ast::StmtKind::If { test: Box::new(test), body, orelse: last }
380388
}
381389
},
382390
};
383391

384392
WhileStatement: ast::Stmt = {
385-
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> <end_location:@R> => {
393+
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
394+
let end_location = if let Some(ref s) = s2 {
395+
s.2.last().unwrap().end_location
396+
} else {
397+
body.last().unwrap().end_location
398+
};
386399
let orelse = s2.map(|s| s.2).unwrap_or_default();
387400
ast::Stmt {
388401
custom: (),
389402
location,
390-
end_location: Some(end_location),
403+
end_location,
391404
node: ast::StmtKind::While {
392405
test: Box::new(test),
393406
body,
@@ -398,7 +411,12 @@ WhileStatement: ast::Stmt = {
398411
};
399412

400413
ForStatement: ast::Stmt = {
401-
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2:("else" ":" Suite)?> <end_location:@R> => {
414+
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
415+
let end_location = if let Some(ref s) = s2 {
416+
s.2.last().unwrap().end_location.unwrap()
417+
} else {
418+
body.last().unwrap().end_location.unwrap()
419+
};
402420
let orelse = s2.map(|s| s.2).unwrap_or_default();
403421
let target = Box::new(set_context(target, ast::ExprContext::Store));
404422
let iter = Box::new(iter);
@@ -416,10 +434,19 @@ TryStatement: ast::Stmt = {
416434
<location:@L> "try" ":" <body:Suite> <handlers:ExceptClause+> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> <end_location:@R> => {
417435
let orelse = else_suite.map(|s| s.2).unwrap_or_default();
418436
let finalbody = finally.map(|s| s.2).unwrap_or_default();
437+
let end_location = if let Some(last) = finalbody.last() {
438+
last.end_location
439+
} else {
440+
if let Some(last) = orelse.last() {
441+
last.end_location
442+
} else {
443+
handlers.last().unwrap().end_location
444+
}
445+
};
419446
ast::Stmt {
420447
custom: (),
421448
location,
422-
end_location: Some(end_location),
449+
end_location,
423450
node: ast::StmtKind::Try {
424451
body,
425452
handlers,
@@ -428,14 +455,15 @@ TryStatement: ast::Stmt = {
428455
},
429456
}
430457
},
431-
<location:@L> "try" ":" <body:Suite> <finally:("finally" ":" Suite)> <end_location:@R> => {
458+
<location:@L> "try" ":" <body:Suite> <finally:("finally" ":" Suite)> => {
432459
let handlers = vec![];
433460
let orelse = vec![];
434461
let finalbody = finally.2;
462+
let end_location = finalbody.last().unwrap().end_location;
435463
ast::Stmt {
436464
custom: (),
437465
location,
438-
end_location: Some(end_location),
466+
end_location,
439467
node: ast::StmtKind::Try {
440468
body,
441469
handlers,
@@ -447,7 +475,8 @@ TryStatement: ast::Stmt = {
447475
};
448476

449477
ExceptClause: ast::Excepthandler = {
450-
<location:@L> "except" <typ:Test?> ":" <body:Suite> <end_location:@R> => {
478+
<location:@L> "except" <typ:Test?> ":" <body:Suite> => {
479+
let end_location = body.last().unwrap().end_location.unwrap();
451480
ast::Excepthandler::new(
452481
location,
453482
end_location,
@@ -458,7 +487,8 @@ ExceptClause: ast::Excepthandler = {
458487
},
459488
)
460489
},
461-
<location:@L> "except" <x:(Test "as" Identifier)> ":" <body:Suite> <end_location:@R> => {
490+
<location:@L> "except" <x:(Test "as" Identifier)> ":" <body:Suite> => {
491+
let end_location = body.last().unwrap().end_location.unwrap();
462492
ast::Excepthandler::new(
463493
location,
464494
end_location,
@@ -472,7 +502,8 @@ ExceptClause: ast::Excepthandler = {
472502
};
473503

474504
WithStatement: ast::Stmt = {
475-
<location:@L> <is_async:"async"?> "with" <items:OneOrMore<WithItem>> ":" <body:Suite> <end_location:@R> => {
505+
<location:@L> <is_async:"async"?> "with" <items:OneOrMore<WithItem>> ":" <body:Suite> => {
506+
let end_location = body.last().unwrap().end_location.unwrap();
476507
let type_comment = None;
477508
let node = if is_async.is_some() {
478509
ast::StmtKind::AsyncWith { items, body, type_comment }

compiler/parser/src/snapshots/rustpython_parser__parser__tests__parse_if_elif_else.snap

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)