@@ -26,32 +26,56 @@ pub Top: ast::Mod = {
26
26
};
27
27
28
28
Program: 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
31
41
},
32
- };
33
42
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,
38
45
};
39
46
40
47
Suite: 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,
43
53
};
44
54
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
47
65
<s:CompoundStatement> => vec![s],
48
- };
49
66
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);
52
76
statements.push(last);
53
77
statements
54
- }
78
+ },
55
79
};
56
80
57
81
SmallStatement: ast::Stmt = {
@@ -734,19 +758,19 @@ ClassPattern: ast::Pattern = {
734
758
}
735
759
736
760
IfStatement: 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> )?> => {
738
762
// Determine last else:
739
- let mut last = s3.map(|s| s.2). unwrap_or_default();
763
+ let mut last = s3.unwrap_or_default();
740
764
let end_location = last
741
765
.last()
742
- .or_else(|| s2.last().and_then(|last| last.4 .last()))
766
+ .or_else(|| s2.last().and_then(|last| last.2 .last()))
743
767
.or_else(|| body.last())
744
768
.unwrap()
745
769
.end();
746
770
// handle elif:
747
771
for i in s2.into_iter().rev() {
748
772
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() }
750
774
);
751
775
last = vec![x];
752
776
}
@@ -758,8 +782,8 @@ IfStatement: ast::Stmt = {
758
782
};
759
783
760
784
WhileStatement: 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();
763
787
let end_location = orelse
764
788
.last()
765
789
.or_else(|| body.last())
@@ -777,8 +801,8 @@ WhileStatement: ast::Stmt = {
777
801
};
778
802
779
803
ForStatement: 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();
782
806
let end_location = orelse
783
807
.last()
784
808
.or_else(|| body.last())
@@ -796,9 +820,9 @@ ForStatement: ast::Stmt = {
796
820
};
797
821
798
822
TryStatement: 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();
802
826
let end_location = finalbody
803
827
.last()
804
828
.map(|last| last.end())
@@ -815,9 +839,9 @@ TryStatement: ast::Stmt = {
815
839
},
816
840
)
817
841
},
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();
821
845
let end_location = finalbody
822
846
.last()
823
847
.or_else(|| orelse.last())
@@ -834,10 +858,9 @@ TryStatement: ast::Stmt = {
834
858
},
835
859
)
836
860
},
837
- <location:@L> "try" ":" <body:Suite> <finally :("finally" ":" Suite)> => {
861
+ <location:@L> "try" ":" <body:Suite> <finalbody :("finally" ":" < Suite> )> => {
838
862
let handlers = vec![];
839
863
let orelse = vec![];
840
- let finalbody = finally.2;
841
864
let end_location = finalbody.last().unwrap().end();
842
865
ast::Stmt::Try(
843
866
ast::StmtTry {
@@ -863,12 +886,12 @@ ExceptStarClause: ast::Excepthandler = {
863
886
},
864
887
)
865
888
},
866
- <location:@L> "except" "*" <x:(Test<"all"> "as" Identifier)> ":" <body:Suite> => {
889
+ <location:@L> "except" "*" <x:(< Test<"all">> "as" < Identifier> )> ":" <body:Suite> => {
867
890
let end_location = body.last().unwrap().end();
868
891
ast::Excepthandler::ExceptHandler(
869
892
ast::ExcepthandlerExceptHandler {
870
893
type_: Some(Box::new(x.0)),
871
- name: Some(x.2 ),
894
+ name: Some(x.1 ),
872
895
body,
873
896
range: (location..end_location).into()
874
897
},
@@ -889,12 +912,12 @@ ExceptClause: ast::Excepthandler = {
889
912
},
890
913
)
891
914
},
892
- <location:@L> "except" <x:(Test<"all"> "as" Identifier)> ":" <body:Suite> => {
915
+ <location:@L> "except" <x:(< Test<"all">> "as" < Identifier> )> ":" <body:Suite> => {
893
916
let end_location = body.last().unwrap().end();
894
917
ast::Excepthandler::ExceptHandler(
895
918
ast::ExcepthandlerExceptHandler {
896
919
type_: Some(Box::new(x.0)),
897
- name: Some(x.2 ),
920
+ name: Some(x.1 ),
898
921
body,
899
922
range: (location..end_location).into()
900
923
},
@@ -941,7 +964,7 @@ WithItem<Goal>: ast::Withitem = {
941
964
};
942
965
943
966
FuncDef: 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> => {
945
968
let args = Box::new(args);
946
969
let returns = r.map(|x| Box::new(x));
947
970
let end_location = body.last().unwrap().end();
0 commit comments