mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-26 05:14:40 +00:00
Merge pull request #60 from astral-sh/reduce-copy
This commit is contained in:
commit
33a3c407a9
2 changed files with 20387 additions and 19767 deletions
|
@ -26,32 +26,56 @@ pub Top: ast::Mod = {
|
||||||
};
|
};
|
||||||
|
|
||||||
Program: ast::Suite = {
|
Program: ast::Suite = {
|
||||||
<lines:FileLine*> => {
|
=> vec![],
|
||||||
lines.into_iter().flatten().collect()
|
// Compound statements
|
||||||
|
<mut statements:Program> <next:CompoundStatement> => {
|
||||||
|
statements.push(next);
|
||||||
|
statements
|
||||||
},
|
},
|
||||||
};
|
|
||||||
|
|
||||||
// A file line either has a declaration, or an empty newline:
|
// Small statements
|
||||||
FileLine: ast::Suite = {
|
<mut statements:Program> <small:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
|
||||||
Statement,
|
statements.extend(small);
|
||||||
"\n" => vec![],
|
statements.push(last);
|
||||||
|
statements
|
||||||
|
},
|
||||||
|
|
||||||
|
// Empty lines
|
||||||
|
<s:Program> "\n" => s,
|
||||||
};
|
};
|
||||||
|
|
||||||
Suite: ast::Suite = {
|
Suite: ast::Suite = {
|
||||||
SimpleStatement,
|
|
||||||
"\n" Indent <s:Statement+> Dedent => s.into_iter().flatten().collect(),
|
|
||||||
};
|
|
||||||
|
|
||||||
Statement: ast::Suite = {
|
|
||||||
SimpleStatement,
|
|
||||||
<s:CompoundStatement> => vec![s],
|
|
||||||
};
|
|
||||||
|
|
||||||
SimpleStatement: ast::Suite = {
|
|
||||||
<mut statements:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
|
<mut statements:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
|
||||||
statements.push(last);
|
statements.push(last);
|
||||||
statements
|
statements
|
||||||
}
|
},
|
||||||
|
"\n" Indent <s:Statements> Dedent => s,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// One or more statements
|
||||||
|
Statements: Vec<ast::Stmt> = {
|
||||||
|
// First simple statement
|
||||||
|
<mut head:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
|
||||||
|
head.push(last);
|
||||||
|
head
|
||||||
|
},
|
||||||
|
|
||||||
|
// The first compound statement
|
||||||
|
<s:CompoundStatement> => vec![s],
|
||||||
|
|
||||||
|
// Any subsequent compound statements
|
||||||
|
<mut statements:Statements> <next:CompoundStatement> => {
|
||||||
|
statements.push(next);
|
||||||
|
statements
|
||||||
|
},
|
||||||
|
|
||||||
|
// Any subsequent small statements
|
||||||
|
<mut statements:Statements> <small:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
|
||||||
|
statements.extend(small);
|
||||||
|
statements.push(last);
|
||||||
|
statements
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
SmallStatement: ast::Stmt = {
|
SmallStatement: ast::Stmt = {
|
||||||
|
@ -734,19 +758,19 @@ ClassPattern: ast::Pattern = {
|
||||||
}
|
}
|
||||||
|
|
||||||
IfStatement: ast::Stmt = {
|
IfStatement: ast::Stmt = {
|
||||||
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite)*> <s3:("else" ":" Suite)?> => {
|
<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.unwrap_or_default();
|
||||||
let end_location = last
|
let end_location = last
|
||||||
.last()
|
.last()
|
||||||
.or_else(|| s2.last().and_then(|last| last.4.last()))
|
.or_else(|| s2.last().and_then(|last| last.2.last()))
|
||||||
.or_else(|| body.last())
|
.or_else(|| body.last())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.end();
|
.end();
|
||||||
// handle elif:
|
// handle elif:
|
||||||
for i in s2.into_iter().rev() {
|
for i in s2.into_iter().rev() {
|
||||||
let x = ast::Stmt::If(
|
let x = ast::Stmt::If(
|
||||||
ast::StmtIf { test: Box::new(i.2), body: i.4, orelse: last, range: (i.0..end_location).into() }
|
ast::StmtIf { test: Box::new(i.1), body: i.2, orelse: last, range: (i.0..end_location).into() }
|
||||||
);
|
);
|
||||||
last = vec![x];
|
last = vec![x];
|
||||||
}
|
}
|
||||||
|
@ -758,8 +782,8 @@ IfStatement: ast::Stmt = {
|
||||||
};
|
};
|
||||||
|
|
||||||
WhileStatement: ast::Stmt = {
|
WhileStatement: ast::Stmt = {
|
||||||
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
|
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" <Suite>)?> => {
|
||||||
let orelse = s2.map(|s| s.2).unwrap_or_default();
|
let orelse = s2.unwrap_or_default();
|
||||||
let end_location = orelse
|
let end_location = orelse
|
||||||
.last()
|
.last()
|
||||||
.or_else(|| body.last())
|
.or_else(|| body.last())
|
||||||
|
@ -777,8 +801,8 @@ 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)?> => {
|
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <orelse:("else" ":" <Suite>)?> => {
|
||||||
let orelse = s2.map(|s| s.2).unwrap_or_default();
|
let orelse = orelse.unwrap_or_default();
|
||||||
let end_location = orelse
|
let end_location = orelse
|
||||||
.last()
|
.last()
|
||||||
.or_else(|| body.last())
|
.or_else(|| body.last())
|
||||||
|
@ -796,9 +820,9 @@ ForStatement: ast::Stmt = {
|
||||||
};
|
};
|
||||||
|
|
||||||
TryStatement: ast::Stmt = {
|
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+> <orelse:("else" ":" <Suite>)?> <finalbody:("finally" ":" <Suite>)?> <end_location:@R> => {
|
||||||
let orelse = else_suite.map(|s| s.2).unwrap_or_default();
|
let orelse = orelse.unwrap_or_default();
|
||||||
let finalbody = finally.map(|s| s.2).unwrap_or_default();
|
let finalbody = finalbody.unwrap_or_default();
|
||||||
let end_location = finalbody
|
let end_location = finalbody
|
||||||
.last()
|
.last()
|
||||||
.map(|last| last.end())
|
.map(|last| last.end())
|
||||||
|
@ -815,9 +839,9 @@ TryStatement: ast::Stmt = {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
<location:@L> "try" ":" <body:Suite> <handlers:ExceptStarClause+> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> <end_location:@R> => {
|
<location:@L> "try" ":" <body:Suite> <handlers:ExceptStarClause+> <orelse:("else" ":" <Suite>)?> <finalbody:("finally" ":" <Suite>)?> <end_location:@R> => {
|
||||||
let orelse = else_suite.map(|s| s.2).unwrap_or_default();
|
let orelse = orelse.unwrap_or_default();
|
||||||
let finalbody = finally.map(|s| s.2).unwrap_or_default();
|
let finalbody = finalbody.unwrap_or_default();
|
||||||
let end_location = finalbody
|
let end_location = finalbody
|
||||||
.last()
|
.last()
|
||||||
.or_else(|| orelse.last())
|
.or_else(|| orelse.last())
|
||||||
|
@ -834,10 +858,9 @@ TryStatement: ast::Stmt = {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
<location:@L> "try" ":" <body:Suite> <finally:("finally" ":" Suite)> => {
|
<location:@L> "try" ":" <body:Suite> <finalbody:("finally" ":" <Suite>)> => {
|
||||||
let handlers = vec![];
|
let handlers = vec![];
|
||||||
let orelse = vec![];
|
let orelse = vec![];
|
||||||
let finalbody = finally.2;
|
|
||||||
let end_location = finalbody.last().unwrap().end();
|
let end_location = finalbody.last().unwrap().end();
|
||||||
ast::Stmt::Try(
|
ast::Stmt::Try(
|
||||||
ast::StmtTry {
|
ast::StmtTry {
|
||||||
|
@ -863,12 +886,12 @@ ExceptStarClause: ast::Excepthandler = {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
<location:@L> "except" "*" <x:(Test<"all"> "as" Identifier)> ":" <body:Suite> => {
|
<location:@L> "except" "*" <x:(<Test<"all">> "as" <Identifier>)> ":" <body:Suite> => {
|
||||||
let end_location = body.last().unwrap().end();
|
let end_location = body.last().unwrap().end();
|
||||||
ast::Excepthandler::ExceptHandler(
|
ast::Excepthandler::ExceptHandler(
|
||||||
ast::ExcepthandlerExceptHandler {
|
ast::ExcepthandlerExceptHandler {
|
||||||
type_: Some(Box::new(x.0)),
|
type_: Some(Box::new(x.0)),
|
||||||
name: Some(x.2),
|
name: Some(x.1),
|
||||||
body,
|
body,
|
||||||
range: (location..end_location).into()
|
range: (location..end_location).into()
|
||||||
},
|
},
|
||||||
|
@ -889,12 +912,12 @@ ExceptClause: ast::Excepthandler = {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
<location:@L> "except" <x:(Test<"all"> "as" Identifier)> ":" <body:Suite> => {
|
<location:@L> "except" <x:(<Test<"all">> "as" <Identifier>)> ":" <body:Suite> => {
|
||||||
let end_location = body.last().unwrap().end();
|
let end_location = body.last().unwrap().end();
|
||||||
ast::Excepthandler::ExceptHandler(
|
ast::Excepthandler::ExceptHandler(
|
||||||
ast::ExcepthandlerExceptHandler {
|
ast::ExcepthandlerExceptHandler {
|
||||||
type_: Some(Box::new(x.0)),
|
type_: Some(Box::new(x.0)),
|
||||||
name: Some(x.2),
|
name: Some(x.1),
|
||||||
body,
|
body,
|
||||||
range: (location..end_location).into()
|
range: (location..end_location).into()
|
||||||
},
|
},
|
||||||
|
|
40055
parser/src/python.rs
generated
40055
parser/src/python.rs
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue