Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Commit fbfb4ed

Browse files
committed
Refactor if statement, introduce nodes for elif and else
1 parent b62294d commit fbfb4ed

7 files changed

+10273
-10084
lines changed

ast/src/generic.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,12 @@ pub struct StmtIf {
606606
pub range: TextRange,
607607
pub test: Box<Expr>,
608608
pub body: Vec<Stmt>,
609-
pub orelse: Vec<Stmt>,
609+
pub elif_else_clauses: Vec<ElifElseClause>,
610610
}
611611

612612
impl Node for StmtIf {
613613
const NAME: &'static str = "If";
614-
const FIELD_NAMES: &'static [&'static str] = &["test", "body", "orelse"];
614+
const FIELD_NAMES: &'static [&'static str] = &["test", "body", "elif_else_clauses"];
615615
}
616616
impl From<StmtIf> for Stmt {
617617
fn from(payload: StmtIf) -> Self {
@@ -624,6 +624,18 @@ impl From<StmtIf> for Ast {
624624
}
625625
}
626626

627+
#[derive(Clone, Debug, PartialEq)]
628+
pub struct ElifElseClause {
629+
pub range: TextRange,
630+
pub test: Option<Expr>,
631+
pub body: Vec<Stmt>,
632+
}
633+
634+
impl Node for ElifElseClause {
635+
const NAME: &'static str = "ElifElse";
636+
const FIELD_NAMES: &'static [&'static str] = &["test", "body"];
637+
}
638+
627639
/// See also [With](https://docs.python.org/3/library/ast.html#ast.With)
628640
#[derive(Clone, Debug, PartialEq)]
629641
pub struct StmtWith {

ast/src/ranged.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ impl Ranged for crate::generic::StmtIf {
122122
self.range
123123
}
124124
}
125+
impl Ranged for crate::generic::ElifElseClause {
126+
fn range(&self) -> TextRange {
127+
self.range
128+
}
129+
}
125130
impl Ranged for crate::generic::StmtWith {
126131
fn range(&self) -> TextRange {
127132
self.range

parser/src/python.lalrpop

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -759,25 +759,24 @@ ClassPattern: ast::Pattern = {
759759
}
760760

761761
IfStatement: ast::Stmt = {
762-
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(<@L> "elif" <NamedExpressionTest> ":" <Suite>)*> <s3:("else" ":" <Suite>)?> => {
763-
// Determine last else:
764-
let mut last = s3.unwrap_or_default();
765-
let end_location = last
762+
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(<@L> "elif" <NamedExpressionTest> ":" <Suite>)*> <s3:(<@L> "else" ":" <Suite>)?> => {
763+
let elif_else_clauses: Vec<_> = s2.into_iter().map(|(start, test, body)| ast::ElifElseClause {
764+
range: (start..body.last().unwrap().end()).into(),
765+
test: Some(test),
766+
body,
767+
}).chain(s3.into_iter().map(|(start, body)| ast::ElifElseClause {
768+
range: (start..body.last().unwrap().end()).into(),
769+
test: None,
770+
body,
771+
})).collect();
772+
773+
let end_location = elif_else_clauses
766774
.last()
767-
.or_else(|| s2.last().and_then(|last| last.2.last()))
768-
.or_else(|| body.last())
769-
.unwrap()
770-
.end();
771-
// handle elif:
772-
for i in s2.into_iter().rev() {
773-
let x = ast::Stmt::If(
774-
ast::StmtIf { test: Box::new(i.1), body: i.2, orelse: last, range: (i.0..end_location).into() }
775-
);
776-
last = vec![x];
777-
}
775+
.map(|last| last.end())
776+
.unwrap_or(body.last().unwrap().end());
778777

779778
ast::Stmt::If(
780-
ast::StmtIf { test: Box::new(test), body, orelse: last, range: (location..end_location).into() }
779+
ast::StmtIf { test: Box::new(test), body, elif_else_clauses, range: (location..end_location).into() }
781780
)
782781
},
783782
};

parser/src/python.rs

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

parser/src/snapshots/rustpython_parser__context__tests__assign_named_expr.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/src/snapshots/rustpython_parser__parser__tests__match_as_identifier.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/src/snapshots/rustpython_parser__parser__tests__parse_if_elif_else.snap

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

0 commit comments

Comments
 (0)