7
7
use super::ast;
8
8
use super::lexer;
9
9
use std::iter::FromIterator;
10
- use std::str::FromStr;
11
10
12
11
grammar;
13
12
@@ -53,6 +52,8 @@ SmallStatement: ast::LocatedStatement = {
53
52
DelStatement,
54
53
FlowStatement,
55
54
ImportStatement,
55
+ GlobalStatement,
56
+ NonlocalStatement,
56
57
AssertStatement,
57
58
};
58
59
@@ -282,6 +283,24 @@ DottedName: String = {
282
283
},
283
284
};
284
285
286
+ GlobalStatement: ast::LocatedStatement = {
287
+ <loc:@L> "global" <names:OneOrMore<Identifier>> => {
288
+ ast::LocatedStatement {
289
+ location: loc,
290
+ node: ast::Statement::Global { names }
291
+ }
292
+ },
293
+ };
294
+
295
+ NonlocalStatement: ast::LocatedStatement = {
296
+ <loc:@L> "nonlocal" <names:OneOrMore<Identifier>> => {
297
+ ast::LocatedStatement {
298
+ location: loc,
299
+ node: ast::Statement::Nonlocal { names }
300
+ }
301
+ },
302
+ };
303
+
285
304
AssertStatement: ast::LocatedStatement = {
286
305
<loc:@L> "assert" <t:Test> <m: ("," Test)?> => {
287
306
ast::LocatedStatement {
@@ -400,11 +419,7 @@ ExceptClause: ast::ExceptHandler = {
400
419
};
401
420
402
421
WithStatement: ast::LocatedStatement = {
403
- <loc:@L> "with" <i1:WithItem> <i2:("," WithItem)*> ":" <s:Suite> => {
404
- let mut items = vec![i1];
405
- for item in i2 {
406
- items.push(item.1);
407
- }
422
+ <loc:@L> "with" <items:OneOrMore<WithItem>> ":" <s:Suite> => {
408
423
ast::LocatedStatement {
409
424
location: loc,
410
425
node: ast::Statement::With { items: items, body: s },
@@ -808,10 +823,8 @@ Atom: ast::Expression = {
808
823
};
809
824
810
825
TestListComp: Vec<ast::Expression> = {
811
- <e:TestOrStarExpr> <e2:("," TestOrStarExpr)*> <_trailing_comma:","?> => {
812
- let mut res = vec![e];
813
- res.extend(e2.into_iter().map(|x| x.1));
814
- res
826
+ <e:OneOrMore<TestOrStarExpr>> <_trailing_comma:","?> => {
827
+ e
815
828
},
816
829
};
817
830
@@ -825,10 +838,8 @@ TestListComp2: ast::Expression = {
825
838
};
826
839
827
840
TestDict: Vec<(ast::Expression, ast::Expression)> = {
828
- <e1:DictEntry> <e2:("," DictEntry)*> <_trailing_comma:","?> => {
829
- let mut d = vec![e1];
830
- d.extend(e2.into_iter().map(|x| x.1));
831
- d
841
+ <e1:OneOrMore<DictEntry>> <_trailing_comma:","?> => {
842
+ e1
832
843
}
833
844
};
834
845
@@ -846,10 +857,8 @@ DictEntry: (ast::Expression, ast::Expression) = {
846
857
};
847
858
848
859
TestSet: Vec<ast::Expression> = {
849
- <e1:Test> <e2:("," Test)*> ","? => {
850
- let mut e = vec![e1];
851
- e.extend(e2.into_iter().map(|x| x.1));
852
- e
860
+ <e1:OneOrMore<Test>> ","? => {
861
+ e1
853
862
}
854
863
};
855
864
@@ -962,14 +971,22 @@ Comma<T>: Vec<T> = {
962
971
}
963
972
};
964
973
965
- Number: ast::Number = {
966
- <s:number > => {
967
- if s.contains(".") {
968
- ast::Number::Float { value: f64::from_str(&s).unwrap() }
969
- } else {
970
- ast::Number::Integer { value: i32::from_str(&s).unwrap() }
974
+ #[inline]
975
+ OneOrMore<T>: Vec<T > = {
976
+ <i1: T> <i2:("," T)*> => {
977
+ let mut items = vec![i1];
978
+ items.extend(i2.into_iter().map(|e| e.1));
979
+ items
971
980
}
972
- }
981
+ };
982
+
983
+ Number: ast::Number = {
984
+ <s:int> => {
985
+ ast::Number::Integer { value: s }
986
+ },
987
+ <s:float> => {
988
+ ast::Number::Float { value: s }
989
+ },
973
990
};
974
991
975
992
StringConstant: ast::Expression = {
@@ -1052,12 +1069,14 @@ extern {
1052
1069
"finally" => lexer::Tok::Finally,
1053
1070
"for" => lexer::Tok::For,
1054
1071
"from" => lexer::Tok::From,
1072
+ "global" => lexer::Tok::Global,
1055
1073
"if" => lexer::Tok::If,
1056
1074
"in" => lexer::Tok::In,
1057
1075
"is" => lexer::Tok::Is,
1058
1076
"import" => lexer::Tok::Import,
1059
1077
"from" => lexer::Tok::From,
1060
1078
"lambda" => lexer::Tok::Lambda,
1079
+ "nonlocal" => lexer::Tok::Nonlocal,
1061
1080
"not" => lexer::Tok::Not,
1062
1081
"or" => lexer::Tok::Or,
1063
1082
"pass" => lexer::Tok::Pass,
@@ -1070,7 +1089,8 @@ extern {
1070
1089
"True" => lexer::Tok::True,
1071
1090
"False" => lexer::Tok::False,
1072
1091
"None" => lexer::Tok::None,
1073
- number => lexer::Tok::Number { value: <String> },
1092
+ int => lexer::Tok::Int { value: <i32> },
1093
+ float => lexer::Tok::Float { value: <f64> },
1074
1094
string => lexer::Tok::String { value: <String> },
1075
1095
bytes => lexer::Tok::Bytes { value: <Vec<u8>> },
1076
1096
name => lexer::Tok::Name { name: <String> },
0 commit comments