Refactor if statement, introduce nodes for elif and else (#22)

The old if layout couldn't differentiate between an else block with a
single if statement and an elif statement. Additionally we getting rid
of the recursion in favor of a single if struct with a vec of elif/else
children. This is accompanied by a big refactoring in ruff which removes
a bunch of TODOs and false negatives.
This commit is contained in:
konsti 2023-07-18 13:14:40 +02:00 committed by GitHub
parent ff3e8ead36
commit db04fd4157
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 20655 additions and 20466 deletions

View File

@ -650,12 +650,12 @@ pub struct StmtIf {
pub range: TextRange,
pub test: Box<Expr>,
pub body: Vec<Stmt>,
pub orelse: Vec<Stmt>,
pub elif_else_clauses: Vec<ElifElseClause>,
}
impl Node for StmtIf {
const NAME: &'static str = "If";
const FIELD_NAMES: &'static [&'static str] = &["test", "body", "orelse"];
const FIELD_NAMES: &'static [&'static str] = &["test", "body", "elif_else_clauses"];
}
impl From<StmtIf> for Stmt {
fn from(payload: StmtIf) -> Self {
@ -668,6 +668,18 @@ impl From<StmtIf> for Ast {
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ElifElseClause {
pub range: TextRange,
pub test: Option<Expr>,
pub body: Vec<Stmt>,
}
impl Node for ElifElseClause {
const NAME: &'static str = "ElifElse";
const FIELD_NAMES: &'static [&'static str] = &["test", "body"];
}
/// See also [With](https://docs.python.org/3/library/ast.html#ast.With)
#[derive(Clone, Debug, PartialEq)]
pub struct StmtWith {

View File

@ -127,6 +127,11 @@ impl Ranged for crate::generic::StmtIf {
self.range
}
}
impl Ranged for crate::generic::ElifElseClause {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::generic::StmtWith {
fn range(&self) -> TextRange {
self.range

View File

@ -760,25 +760,24 @@ ClassPattern: ast::Pattern = {
}
IfStatement: ast::Stmt = {
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(<@L> "elif" <NamedExpressionTest> ":" <Suite>)*> <s3:("else" ":" <Suite>)?> => {
// Determine last else:
let mut last = s3.unwrap_or_default();
let end_location = last
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(<@L> "elif" <NamedExpressionTest> ":" <Suite>)*> <s3:(<@L> "else" ":" <Suite>)?> => {
let elif_else_clauses: Vec<_> = s2.into_iter().map(|(start, test, body)| ast::ElifElseClause {
range: (start..body.last().unwrap().end()).into(),
test: Some(test),
body,
}).chain(s3.into_iter().map(|(start, body)| ast::ElifElseClause {
range: (start..body.last().unwrap().end()).into(),
test: None,
body,
})).collect();
let end_location = elif_else_clauses
.last()
.or_else(|| s2.last().and_then(|last| last.2.last()))
.or_else(|| body.last())
.unwrap()
.end();
// handle elif:
for i in s2.into_iter().rev() {
let x = ast::Stmt::If(
ast::StmtIf { test: Box::new(i.1), body: i.2, orelse: last, range: (i.0..end_location).into() }
);
last = vec![x];
}
.map(|last| last.end())
.unwrap_or_else(|| body.last().unwrap().end());
ast::Stmt::If(
ast::StmtIf { test: Box::new(test), body, orelse: last, range: (location..end_location).into() }
ast::StmtIf { test: Box::new(test), body, elif_else_clauses, range: (location..end_location).into() }
)
},
};

40981
parser/src/python.rs generated

File diff suppressed because it is too large Load Diff

View File

@ -34,7 +34,7 @@ expression: parse_ast
},
),
],
orelse: [],
elif_else_clauses: [],
},
),
]

View File

@ -627,7 +627,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
},
),
],
orelse: [],
elif_else_clauses: [],
},
),
Match(

View File

@ -31,11 +31,11 @@ expression: parse_ast
},
),
],
orelse: [
If(
StmtIf {
range: 9..28,
test: Constant(
elif_else_clauses: [
ElifElseClause {
range: 9..19,
test: Some(
Constant(
ExprConstant {
range: 14..15,
value: Int(
@ -44,40 +44,44 @@ expression: parse_ast
kind: None,
},
),
body: [
Expr(
StmtExpr {
range: 17..19,
value: Constant(
ExprConstant {
range: 17..19,
value: Int(
20,
),
kind: None,
},
),
},
),
],
orelse: [
Expr(
StmtExpr {
range: 26..28,
value: Constant(
ExprConstant {
range: 26..28,
value: Int(
30,
),
kind: None,
},
),
},
),
],
},
),
),
body: [
Expr(
StmtExpr {
range: 17..19,
value: Constant(
ExprConstant {
range: 17..19,
value: Int(
20,
),
kind: None,
},
),
},
),
],
},
ElifElseClause {
range: 20..28,
test: None,
body: [
Expr(
StmtExpr {
range: 26..28,
value: Constant(
ExprConstant {
range: 26..28,
value: Int(
30,
),
kind: None,
},
),
},
),
],
},
],
},
),

View File

@ -627,7 +627,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
},
),
],
orelse: [],
elif_else_clauses: [],
},
),
Assign(