mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-14 00:25:17 +00:00
Fix other compound statements
This commit is contained in:
parent
2d75aeb276
commit
c79cc34b37
2 changed files with 46 additions and 15 deletions
|
@ -357,16 +357,24 @@ CompoundStatement: ast::Stmt = {
|
||||||
};
|
};
|
||||||
|
|
||||||
IfStatement: ast::Stmt = {
|
IfStatement: ast::Stmt = {
|
||||||
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite @R)*> <s3:("else" ":" Suite)?> <end_location:@R> => {
|
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite)*> <s3:("else" ":" Suite)?> => {
|
||||||
// Determine last else:
|
// Determine last else:
|
||||||
let mut last = s3.map(|s| s.2).unwrap_or_default();
|
let mut last = s3.map(|s| s.2).unwrap_or_default();
|
||||||
|
let end_location = if let Some(last) = last.last() {
|
||||||
|
last.end_location
|
||||||
|
} else {
|
||||||
|
if let Some(last) = s2.last() {
|
||||||
|
last.4.last().unwrap().end_location
|
||||||
|
} else {
|
||||||
|
body.last().unwrap().end_location
|
||||||
|
}
|
||||||
|
};
|
||||||
// handle elif:
|
// handle elif:
|
||||||
for i in s2.into_iter().rev() {
|
for i in s2.into_iter().rev() {
|
||||||
let x = ast::Stmt {
|
let x = ast::Stmt {
|
||||||
custom: (),
|
custom: (),
|
||||||
location: i.0,
|
location: i.0,
|
||||||
end_location: Some(i.5),
|
end_location: i.4.last().unwrap().end_location,
|
||||||
node: ast::StmtKind::If { test: Box::new(i.2), body: i.4, orelse: last },
|
node: ast::StmtKind::If { test: Box::new(i.2), body: i.4, orelse: last },
|
||||||
};
|
};
|
||||||
last = vec![x];
|
last = vec![x];
|
||||||
|
@ -375,19 +383,24 @@ IfStatement: ast::Stmt = {
|
||||||
ast::Stmt {
|
ast::Stmt {
|
||||||
custom: (),
|
custom: (),
|
||||||
location,
|
location,
|
||||||
end_location: Some(end_location),
|
end_location,
|
||||||
node: ast::StmtKind::If { test: Box::new(test), body, orelse: last }
|
node: ast::StmtKind::If { test: Box::new(test), body, orelse: last }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
WhileStatement: ast::Stmt = {
|
WhileStatement: ast::Stmt = {
|
||||||
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> <end_location:@R> => {
|
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
|
||||||
|
let end_location = if let Some(ref s) = s2 {
|
||||||
|
s.2.last().unwrap().end_location
|
||||||
|
} else {
|
||||||
|
body.last().unwrap().end_location
|
||||||
|
};
|
||||||
let orelse = s2.map(|s| s.2).unwrap_or_default();
|
let orelse = s2.map(|s| s.2).unwrap_or_default();
|
||||||
ast::Stmt {
|
ast::Stmt {
|
||||||
custom: (),
|
custom: (),
|
||||||
location,
|
location,
|
||||||
end_location: Some(end_location),
|
end_location,
|
||||||
node: ast::StmtKind::While {
|
node: ast::StmtKind::While {
|
||||||
test: Box::new(test),
|
test: Box::new(test),
|
||||||
body,
|
body,
|
||||||
|
@ -398,7 +411,12 @@ WhileStatement: ast::Stmt = {
|
||||||
};
|
};
|
||||||
|
|
||||||
ForStatement: ast::Stmt = {
|
ForStatement: ast::Stmt = {
|
||||||
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2:("else" ":" Suite)?> <end_location:@R> => {
|
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
|
||||||
|
let end_location = if let Some(ref s) = s2 {
|
||||||
|
s.2.last().unwrap().end_location.unwrap()
|
||||||
|
} else {
|
||||||
|
body.last().unwrap().end_location.unwrap()
|
||||||
|
};
|
||||||
let orelse = s2.map(|s| s.2).unwrap_or_default();
|
let orelse = s2.map(|s| s.2).unwrap_or_default();
|
||||||
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
||||||
let iter = Box::new(iter);
|
let iter = Box::new(iter);
|
||||||
|
@ -416,10 +434,19 @@ TryStatement: ast::Stmt = {
|
||||||
<location:@L> "try" ":" <body:Suite> <handlers:ExceptClause+> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> <end_location:@R> => {
|
<location:@L> "try" ":" <body:Suite> <handlers:ExceptClause+> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> <end_location:@R> => {
|
||||||
let orelse = else_suite.map(|s| s.2).unwrap_or_default();
|
let orelse = else_suite.map(|s| s.2).unwrap_or_default();
|
||||||
let finalbody = finally.map(|s| s.2).unwrap_or_default();
|
let finalbody = finally.map(|s| s.2).unwrap_or_default();
|
||||||
|
let end_location = if let Some(last) = finalbody.last() {
|
||||||
|
last.end_location
|
||||||
|
} else {
|
||||||
|
if let Some(last) = orelse.last() {
|
||||||
|
last.end_location
|
||||||
|
} else {
|
||||||
|
handlers.last().unwrap().end_location
|
||||||
|
}
|
||||||
|
};
|
||||||
ast::Stmt {
|
ast::Stmt {
|
||||||
custom: (),
|
custom: (),
|
||||||
location,
|
location,
|
||||||
end_location: Some(end_location),
|
end_location,
|
||||||
node: ast::StmtKind::Try {
|
node: ast::StmtKind::Try {
|
||||||
body,
|
body,
|
||||||
handlers,
|
handlers,
|
||||||
|
@ -428,14 +455,15 @@ TryStatement: ast::Stmt = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
<location:@L> "try" ":" <body:Suite> <finally:("finally" ":" Suite)> <end_location:@R> => {
|
<location:@L> "try" ":" <body:Suite> <finally:("finally" ":" Suite)> => {
|
||||||
let handlers = vec![];
|
let handlers = vec![];
|
||||||
let orelse = vec![];
|
let orelse = vec![];
|
||||||
let finalbody = finally.2;
|
let finalbody = finally.2;
|
||||||
|
let end_location = finalbody.last().unwrap().end_location;
|
||||||
ast::Stmt {
|
ast::Stmt {
|
||||||
custom: (),
|
custom: (),
|
||||||
location,
|
location,
|
||||||
end_location: Some(end_location),
|
end_location,
|
||||||
node: ast::StmtKind::Try {
|
node: ast::StmtKind::Try {
|
||||||
body,
|
body,
|
||||||
handlers,
|
handlers,
|
||||||
|
@ -447,7 +475,8 @@ TryStatement: ast::Stmt = {
|
||||||
};
|
};
|
||||||
|
|
||||||
ExceptClause: ast::Excepthandler = {
|
ExceptClause: ast::Excepthandler = {
|
||||||
<location:@L> "except" <typ:Test?> ":" <body:Suite> <end_location:@R> => {
|
<location:@L> "except" <typ:Test?> ":" <body:Suite> => {
|
||||||
|
let end_location = body.last().unwrap().end_location.unwrap();
|
||||||
ast::Excepthandler::new(
|
ast::Excepthandler::new(
|
||||||
location,
|
location,
|
||||||
end_location,
|
end_location,
|
||||||
|
@ -458,7 +487,8 @@ ExceptClause: ast::Excepthandler = {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
<location:@L> "except" <x:(Test "as" Identifier)> ":" <body:Suite> <end_location:@R> => {
|
<location:@L> "except" <x:(Test "as" Identifier)> ":" <body:Suite> => {
|
||||||
|
let end_location = body.last().unwrap().end_location.unwrap();
|
||||||
ast::Excepthandler::new(
|
ast::Excepthandler::new(
|
||||||
location,
|
location,
|
||||||
end_location,
|
end_location,
|
||||||
|
@ -472,7 +502,8 @@ ExceptClause: ast::Excepthandler = {
|
||||||
};
|
};
|
||||||
|
|
||||||
WithStatement: ast::Stmt = {
|
WithStatement: ast::Stmt = {
|
||||||
<location:@L> <is_async:"async"?> "with" <items:OneOrMore<WithItem>> ":" <body:Suite> <end_location:@R> => {
|
<location:@L> <is_async:"async"?> "with" <items:OneOrMore<WithItem>> ":" <body:Suite> => {
|
||||||
|
let end_location = body.last().unwrap().end_location.unwrap();
|
||||||
let type_comment = None;
|
let type_comment = None;
|
||||||
let node = if is_async.is_some() {
|
let node = if is_async.is_some() {
|
||||||
ast::StmtKind::AsyncWith { items, body, type_comment }
|
ast::StmtKind::AsyncWith { items, body, type_comment }
|
||||||
|
|
|
@ -79,8 +79,8 @@ expression: parse_ast
|
||||||
},
|
},
|
||||||
end_location: Some(
|
end_location: Some(
|
||||||
Location {
|
Location {
|
||||||
row: 3,
|
row: 2,
|
||||||
column: 0,
|
column: 10,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
custom: (),
|
custom: (),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue