Avoid allocating vecs for each statement

This commit is contained in:
Micha Reiser 2023-05-18 21:37:36 +02:00
parent 851f23668f
commit 726884b287
No known key found for this signature in database
2 changed files with 19905 additions and 19272 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 = {

39117
parser/src/python.rs generated

File diff suppressed because it is too large Load diff