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:
@@ -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
40981
parser/src/python.rs
generated
File diff suppressed because it is too large
Load Diff
@@ -34,7 +34,7 @@ expression: parse_ast
|
||||
},
|
||||
),
|
||||
],
|
||||
orelse: [],
|
||||
elif_else_clauses: [],
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
@@ -627,7 +627,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||
},
|
||||
),
|
||||
],
|
||||
orelse: [],
|
||||
elif_else_clauses: [],
|
||||
},
|
||||
),
|
||||
Match(
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
),
|
||||
|
||||
@@ -627,7 +627,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||
},
|
||||
),
|
||||
],
|
||||
orelse: [],
|
||||
elif_else_clauses: [],
|
||||
},
|
||||
),
|
||||
Assign(
|
||||
|
||||
Reference in New Issue
Block a user