Repair try-except-else syntax. Fixes #1300.

This commit is contained in:
Windel Bouwman committed 2019-08-19 08:37:07 +02:00
1 parent 14bc2a260b
commit 3ed71308bf
3 files changed
+37 -20

No files matched your search

+15 -14
View File
@@ -24,7 +24,7 @@ pub enum Top {
#[derive(Debug, PartialEq)]
pub struct Program {
pub statements: Vec<Statement>,
pub statements: Suite,
}
#[derive(Debug, PartialEq)]
@@ -40,6 +40,7 @@ pub struct Located<T> {
}
pub type Statement = Located<StatementType>;
pub type Suite = Vec<Statement>;
/// Abstract syntax tree nodes for python statements.
#[derive(Debug, PartialEq)]
@@ -90,39 +91,39 @@ pub enum StatementType {
},
If {
test: Expression,
body: Vec<Statement>,
orelse: Option<Vec<Statement>>,
body: Suite,
orelse: Option<Suite>,
},
While {
test: Expression,
body: Vec<Statement>,
orelse: Option<Vec<Statement>>,
body: Suite,
orelse: Option<Suite>,
},
With {
is_async: bool,
items: Vec<WithItem>,
body: Vec<Statement>,
body: Suite,
},
For {
is_async: bool,
target: Box<Expression>,
iter: Box<Expression>,
body: Vec<Statement>,
orelse: Option<Vec<Statement>>,
body: Suite,
orelse: Option<Suite>,
},
Raise {
exception: Option<Expression>,
cause: Option<Expression>,
},
Try {
body: Vec<Statement>,
body: Suite,
handlers: Vec<ExceptHandler>,
orelse: Option<Vec<Statement>>,
finalbody: Option<Vec<Statement>>,
orelse: Option<Suite>,
finalbody: Option<Suite>,
},
ClassDef {
name: String,
body: Vec<Statement>,
body: Suite,
bases: Vec<Expression>,
keywords: Vec<Keyword>,
decorator_list: Vec<Expression>,
@@ -131,7 +132,7 @@ pub enum StatementType {
is_async: bool,
name: String,
args: Box<Parameters>,
body: Vec<Statement>,
body: Suite,
decorator_list: Vec<Expression>,
returns: Option<Expression>,
},
@@ -331,7 +332,7 @@ pub struct ExceptHandler {
pub location: Location,
pub typ: Option<Expression>,
pub name: Option<String>,
pub body: Vec<Statement>,
pub body: Suite,
}
#[derive(Debug, PartialEq)]
+13 -6
View File
@@ -31,22 +31,22 @@ Program: ast::Program = {
};
// A file line either has a declaration, or an empty newline:
FileLine: Vec<ast::Statement> = {
FileLine: ast::Suite = {
Statement,
"\n" => vec![],
};
Suite: Vec<ast::Statement> = {
Suite: ast::Suite = {
SimpleStatement,
"\n" indent <s:Statement+> dedent => s.into_iter().flatten().collect(),
};
Statement: Vec<ast::Statement> = {
Statement: ast::Suite = {
SimpleStatement,
<s:CompoundStatement> => vec![s],
};
SimpleStatement: Vec<ast::Statement> = {
SimpleStatement: ast::Suite = {
<s1:SmallStatement> <s2:(";" SmallStatement)*> ";"? "\n" => {
let mut statements = vec![s1];
statements.extend(s2.into_iter().map(|e| e.1));
@@ -365,8 +365,8 @@ ForStatement: ast::Statement = {
};
TryStatement: ast::Statement = {
<location:@L> "try" ":" <body:Suite> <handlers:ExceptClause*> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> => {
let orelse = else_suite.map(|s| s.2);
<location:@L> "try" ":" <body:Suite> <handlers:ExceptionHandlers?> <finally:("finally" ":" Suite)?> => {
let (handlers, orelse) = handlers.unwrap_or_default();
let finalbody = finally.map(|s| s.2);
ast::Statement {
location,
@@ -380,6 +380,13 @@ TryStatement: ast::Statement = {
},
};
ExceptionHandlers: (Vec<ast::ExceptHandler>, Option<ast::Suite>) = {
<handlers:ExceptClause+> <else_suite:("else" ":" Suite)?> => {
let orelse = else_suite.map(|s| s.2);
(handlers, orelse)
},
};
ExceptClause: ast::ExceptHandler = {
<location:@L> "except" <typ:Test?> ":" <body:Suite> => {
ast::ExceptHandler {
+9
View File
@@ -238,3 +238,12 @@ try:
raise NameError from ex
except NameError as ex2:
pass
with assertRaises(SyntaxError):
exec("""if True:
try:
pass
else:
pass
""")