Merge pull request #60 from astral-sh/reduce-copy

This commit is contained in:
Micha Reiser 2023-05-19 08:16:28 +02:00 committed by GitHub
commit 33a3c407a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20387 additions and 19767 deletions

View file

@ -26,32 +26,56 @@ pub Top: ast::Mod = {
};
Program: ast::Suite = {
<lines:FileLine*> => {
lines.into_iter().flatten().collect()
=> vec![],
// Compound statements
<mut statements:Program> <next:CompoundStatement> => {
statements.push(next);
statements
},
};
// A file line either has a declaration, or an empty newline:
FileLine: ast::Suite = {
Statement,
"\n" => vec![],
// Small statements
<mut statements:Program> <small:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
statements.extend(small);
statements.push(last);
statements
},
// Empty lines
<s:Program> "\n" => s,
};
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" => {
statements.push(last);
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 = {
@ -734,19 +758,19 @@ ClassPattern: ast::Pattern = {
}
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:
let mut last = s3.map(|s| s.2).unwrap_or_default();
let mut last = s3.unwrap_or_default();
let end_location = 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())
.unwrap()
.end();
// handle elif:
for i in s2.into_iter().rev() {
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];
}
@ -758,8 +782,8 @@ IfStatement: ast::Stmt = {
};
WhileStatement: ast::Stmt = {
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
let orelse = s2.map(|s| s.2).unwrap_or_default();
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" <Suite>)?> => {
let orelse = s2.unwrap_or_default();
let end_location = orelse
.last()
.or_else(|| body.last())
@ -777,8 +801,8 @@ WhileStatement: ast::Stmt = {
};
ForStatement: ast::Stmt = {
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
let orelse = s2.map(|s| s.2).unwrap_or_default();
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <orelse:("else" ":" <Suite>)?> => {
let orelse = orelse.unwrap_or_default();
let end_location = orelse
.last()
.or_else(|| body.last())
@ -796,9 +820,9 @@ ForStatement: ast::Stmt = {
};
TryStatement: ast::Stmt = {
<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 finalbody = finally.map(|s| s.2).unwrap_or_default();
<location:@L> "try" ":" <body:Suite> <handlers:ExceptClause+> <orelse:("else" ":" <Suite>)?> <finalbody:("finally" ":" <Suite>)?> <end_location:@R> => {
let orelse = orelse.unwrap_or_default();
let finalbody = finalbody.unwrap_or_default();
let end_location = finalbody
.last()
.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> => {
let orelse = else_suite.map(|s| s.2).unwrap_or_default();
let finalbody = finally.map(|s| s.2).unwrap_or_default();
<location:@L> "try" ":" <body:Suite> <handlers:ExceptStarClause+> <orelse:("else" ":" <Suite>)?> <finalbody:("finally" ":" <Suite>)?> <end_location:@R> => {
let orelse = orelse.unwrap_or_default();
let finalbody = finalbody.unwrap_or_default();
let end_location = finalbody
.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 orelse = vec![];
let finalbody = finally.2;
let end_location = finalbody.last().unwrap().end();
ast::Stmt::Try(
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();
ast::Excepthandler::ExceptHandler(
ast::ExcepthandlerExceptHandler {
type_: Some(Box::new(x.0)),
name: Some(x.2),
name: Some(x.1),
body,
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();
ast::Excepthandler::ExceptHandler(
ast::ExcepthandlerExceptHandler {
type_: Some(Box::new(x.0)),
name: Some(x.2),
name: Some(x.1),
body,
range: (location..end_location).into()
},

40055
parser/src/python.rs generated

File diff suppressed because it is too large Load diff