@@ -26,32 +26,56 @@ pub Top: ast::Mod = {
2626};
2727
2828Program: ast::Suite = {
29- <lines:FileLine*> => {
30- lines.into_iter().flatten().collect()
29+ => vec![],
30+ // Compound statements
31+ <mut statements:Program> <next:CompoundStatement> => {
32+ statements.push(next);
33+ statements
34+ },
35+
36+ // Small statements
37+ <mut statements:Program> <small:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
38+ statements.extend(small);
39+ statements.push(last);
40+ statements
3141 },
32- };
3342
34- // A file line either has a declaration, or an empty newline:
35- FileLine: ast::Suite = {
36- Statement,
37- "\n" => vec![],
43+ // Empty lines
44+ <s:Program> "\n" => s,
3845};
3946
4047Suite: ast::Suite = {
41- SimpleStatement,
42- "\n" Indent <s:Statement+> Dedent => s.into_iter().flatten().collect(),
48+ <mut statements:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
49+ statements.push(last);
50+ statements
51+ },
52+ "\n" Indent <s:Statements> Dedent => s,
4353};
4454
45- Statement: ast::Suite = {
46- SimpleStatement,
55+
56+ // One or more statements
57+ Statements: Vec<ast::Stmt> = {
58+ // First simple statement
59+ <mut head:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
60+ head.push(last);
61+ head
62+ },
63+
64+ // The first compound statement
4765 <s:CompoundStatement> => vec![s],
48- };
4966
50- SimpleStatement: ast::Suite = {
51- <mut statements:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
67+ // Any subsequent compound statements
68+ <mut statements:Statements> <next:CompoundStatement> => {
69+ statements.push(next);
70+ statements
71+ },
72+
73+ // Any subsequent small statements
74+ <mut statements:Statements> <small:(<SmallStatement> ";")*> <last:SmallStatement> ";"? "\n" => {
75+ statements.extend(small);
5276 statements.push(last);
5377 statements
54- }
78+ },
5579};
5680
5781SmallStatement: ast::Stmt = {
@@ -734,19 +758,19 @@ ClassPattern: ast::Pattern = {
734758}
735759
736760IfStatement: ast::Stmt = {
737- <location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite)*> <s3:("else" ":" Suite)?> => {
761+ <location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(<@L> "elif" < NamedExpressionTest> ":" < Suite> )*> <s3:("else" ":" < Suite> )?> => {
738762 // Determine last else:
739- let mut last = s3.map(|s| s.2). unwrap_or_default();
763+ let mut last = s3.unwrap_or_default();
740764 let end_location = last
741765 .last()
742- .or_else(|| s2.last().and_then(|last| last.4 .last()))
766+ .or_else(|| s2.last().and_then(|last| last.2 .last()))
743767 .or_else(|| body.last())
744768 .unwrap()
745769 .end();
746770 // handle elif:
747771 for i in s2.into_iter().rev() {
748772 let x = ast::Stmt::If(
749- ast::StmtIf { test: Box::new(i.2 ), body: i.4 , orelse: last, range: (i.0..end_location).into() }
773+ ast::StmtIf { test: Box::new(i.1 ), body: i.2 , orelse: last, range: (i.0..end_location).into() }
750774 );
751775 last = vec![x];
752776 }
@@ -758,8 +782,8 @@ IfStatement: ast::Stmt = {
758782};
759783
760784WhileStatement: ast::Stmt = {
761- <location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
762- let orelse = s2.map(|s| s.2). unwrap_or_default();
785+ <location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" < Suite> )?> => {
786+ let orelse = s2.unwrap_or_default();
763787 let end_location = orelse
764788 .last()
765789 .or_else(|| body.last())
@@ -777,8 +801,8 @@ WhileStatement: ast::Stmt = {
777801};
778802
779803ForStatement: ast::Stmt = {
780- <location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2 :("else" ":" Suite)?> => {
781- let orelse = s2.map(|s| s.2) .unwrap_or_default();
804+ <location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <orelse :("else" ":" < Suite> )?> => {
805+ let orelse = orelse .unwrap_or_default();
782806 let end_location = orelse
783807 .last()
784808 .or_else(|| body.last())
@@ -796,9 +820,9 @@ ForStatement: ast::Stmt = {
796820};
797821
798822TryStatement: ast::Stmt = {
799- <location:@L> "try" ":" <body:Suite> <handlers:ExceptClause+> <else_suite :("else" ":" Suite)?> <finally :("finally" ":" Suite)?> <end_location:@R> => {
800- let orelse = else_suite.map(|s| s.2) .unwrap_or_default();
801- let finalbody = finally.map(|s| s.2) .unwrap_or_default();
823+ <location:@L> "try" ":" <body:Suite> <handlers:ExceptClause+> <orelse :("else" ":" < Suite> )?> <finalbody :("finally" ":" < Suite> )?> <end_location:@R> => {
824+ let orelse = orelse .unwrap_or_default();
825+ let finalbody = finalbody .unwrap_or_default();
802826 let end_location = finalbody
803827 .last()
804828 .map(|last| last.end())
@@ -815,9 +839,9 @@ TryStatement: ast::Stmt = {
815839 },
816840 )
817841 },
818- <location:@L> "try" ":" <body:Suite> <handlers:ExceptStarClause+> <else_suite :("else" ":" Suite)?> <finally :("finally" ":" Suite)?> <end_location:@R> => {
819- let orelse = else_suite.map(|s| s.2) .unwrap_or_default();
820- let finalbody = finally.map(|s| s.2) .unwrap_or_default();
842+ <location:@L> "try" ":" <body:Suite> <handlers:ExceptStarClause+> <orelse :("else" ":" < Suite> )?> <finalbody :("finally" ":" < Suite> )?> <end_location:@R> => {
843+ let orelse = orelse .unwrap_or_default();
844+ let finalbody = finalbody .unwrap_or_default();
821845 let end_location = finalbody
822846 .last()
823847 .or_else(|| orelse.last())
@@ -834,10 +858,9 @@ TryStatement: ast::Stmt = {
834858 },
835859 )
836860 },
837- <location:@L> "try" ":" <body:Suite> <finally :("finally" ":" Suite)> => {
861+ <location:@L> "try" ":" <body:Suite> <finalbody :("finally" ":" < Suite> )> => {
838862 let handlers = vec![];
839863 let orelse = vec![];
840- let finalbody = finally.2;
841864 let end_location = finalbody.last().unwrap().end();
842865 ast::Stmt::Try(
843866 ast::StmtTry {
@@ -863,12 +886,12 @@ ExceptStarClause: ast::Excepthandler = {
863886 },
864887 )
865888 },
866- <location:@L> "except" "*" <x:(Test<"all"> "as" Identifier)> ":" <body:Suite> => {
889+ <location:@L> "except" "*" <x:(< Test<"all">> "as" < Identifier> )> ":" <body:Suite> => {
867890 let end_location = body.last().unwrap().end();
868891 ast::Excepthandler::ExceptHandler(
869892 ast::ExcepthandlerExceptHandler {
870893 type_: Some(Box::new(x.0)),
871- name: Some(x.2 ),
894+ name: Some(x.1 ),
872895 body,
873896 range: (location..end_location).into()
874897 },
@@ -889,12 +912,12 @@ ExceptClause: ast::Excepthandler = {
889912 },
890913 )
891914 },
892- <location:@L> "except" <x:(Test<"all"> "as" Identifier)> ":" <body:Suite> => {
915+ <location:@L> "except" <x:(< Test<"all">> "as" < Identifier> )> ":" <body:Suite> => {
893916 let end_location = body.last().unwrap().end();
894917 ast::Excepthandler::ExceptHandler(
895918 ast::ExcepthandlerExceptHandler {
896919 type_: Some(Box::new(x.0)),
897- name: Some(x.2 ),
920+ name: Some(x.1 ),
898921 body,
899922 range: (location..end_location).into()
900923 },
@@ -941,7 +964,7 @@ WithItem<Goal>: ast::Withitem = {
941964};
942965
943966FuncDef: ast::Stmt = {
944- <decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" <Test<"all">>)?> ":" <body:Suite> => {
967+ <decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" <Test<"all">>)?> ":" <body:Suite> => {
945968 let args = Box::new(args);
946969 let returns = r.map(|x| Box::new(x));
947970 let end_location = body.last().unwrap().end();
0 commit comments