Mercurial > python-compiler.rs
comparison src/python_ast.rs @ 13:38b0d63697b1
Import the full AST grammar from CPython 3.5.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 02 Jun 2016 05:33:23 +0100 |
parents | fa7e285f88e7 |
children | 719a27f1c1c7 |
comparison
equal
deleted
inserted
replaced
12:0e96c5bc401d | 13:38b0d63697b1 |
---|---|
1 #[derive(Clone, Debug)] | 1 #[derive(Clone, Debug)] |
2 pub struct Module { | 2 pub struct Module { |
3 pub name: String, | 3 pub name: String, |
4 pub statements: Vec<Statement>, | 4 pub statements: Vec<stmt>, |
5 } | 5 } |
6 | 6 |
7 #[derive(Clone, Debug)] | 7 #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
8 pub enum Statement { | 8 pub enum stmt { |
9 ClassDef(String, Vec<Expr>, Vec<Statement>), | 9 // FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns) |
10 FunctionDef(String, Vec<String>, Vec<Statement>), | 10 FunctionDef(String, arguments, Vec<stmt>, Vec<expr>, Option<expr>), |
11 | |
12 // AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns) | |
13 //AsyncFunctionDef(String/*, arguments, Vec<expr>), | |
14 | |
15 // ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list) | |
16 ClassDef(String, Vec<expr>, Vec<keyword>, Vec<stmt>, Vec<expr>), | |
17 | |
18 // Return(expr? value) | |
19 Return(Option<expr>), | |
20 | |
21 // Delete(expr* targets) | |
22 //Delete(Vec<expr>), | |
23 | |
24 // Assign(expr* targets, expr value) | |
25 Assign(Vec<expr>, expr), | |
26 | |
27 //AugAssign(expr target, operator op, expr value) | |
28 AugAssign(expr, operator, expr), | |
29 | |
30 // For(expr target, expr iter, stmt* body, stmt* orelse) | |
31 For(expr, expr, Vec<stmt>, Vec<stmt>), | |
32 | |
33 // AsyncFor(expr target, expr iter, stmt* body, stmt* orelse) | |
34 //AsyncFor(expr, expr, Vec<stmt>, Vec<stmt>), | |
35 | |
36 // While(expr test, stmt* body, stmt* orelse) | |
37 While(expr, Vec<stmt>, Vec<stmt>), | |
38 | |
39 // If(expr test, stmt* body, stmt* orelse) | |
40 If(expr, Vec<stmt>, Vec<stmt>), | |
41 | |
42 // With(withitem* items, stmt* body) | |
43 //With(Vec<withitem>, Vec<stmt>) | |
44 | |
45 // AsyncWith(withitem* items, stmt* body) | |
46 //AsyncWith(Vec<withitem>, Vec<stmt>) | |
47 | |
48 // Raise(expr? exc, expr? cause) | |
49 //Raise(Option<expr>, Option<expr>) | |
50 | |
51 // Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody) | |
52 //Try(Vec<stmt>, Vec<excepthandler>, Vec<stmt>, Vec<stmt>) | |
53 | |
54 // Assert(expr test, expr? msg) | |
55 //Assert(expr, Option<expr>) | |
56 | |
57 // Import(alias* names) | |
58 //Import(Vec<alias>) | |
59 | |
60 // ImportFrom(identifier? module, alias* names, int? level) | |
61 ImportFrom(String, Vec<alias>, Option<i32>), // TODO: check the size of level. | |
62 | |
63 // Global(identifier* names) | |
11 Global(Vec<String>), | 64 Global(Vec<String>), |
12 If(Expr, Vec<Statement>, Vec<Statement>), | 65 |
13 For(Expr, Expr, Vec<Statement>, Vec<Statement>), | 66 // Nonlocal(identifier* names) |
14 While(Expr, Vec<Statement>, Vec<Statement>), | 67 //Nonlocal(Vec<String>), |
15 Assign(Vec<Expr>, Expr), | 68 |
16 AugAssign(Expr, BinOp, Expr), | 69 // expr(expr value) |
17 Return(Expr), | 70 Expr(expr), |
18 ImportFrom(String, Vec<Expr>), | 71 |
19 Expr(Expr), | 72 // Pass | Break | Continue |
73 //Pass, | |
20 Break, | 74 Break, |
21 Error | 75 //Continue |
22 } | 76 } |
23 | 77 |
24 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | 78 #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
25 pub enum Expr { | 79 pub enum expr { |
26 UnaryOp(UnaryOp, Box<Expr>), | 80 BoolOp(boolop, Vec<expr>), |
27 BinOp(Box<Expr>, BinOp, Box<Expr>), | 81 BinOp(Box<expr>, operator, Box<expr>), |
28 Compare(Box<Expr>, Vec<BinOp>, Vec<Expr>), | 82 UnaryOp(unaryop, Box<expr>), |
29 Call(Box<Expr>, Vec<Expr>), | 83 //Lambda(arguments, Box<expr>) |
30 Alias(String, Option<String>), | 84 //IfExp(Box<expr>, Box<expr>, Box<expr>) |
31 Attribute(Box<Expr>, String), | 85 //Dict(Vec<expr>, Vec<expr>) |
32 Name(String), | 86 //Set(Vec<expr>) |
87 //ListComp(Box<expr>, Vec<comprehension>), | |
88 //SetComp(Box<expr>, Vec<comprehension>) | |
89 //DictComp(Box<expr>, Box<expr>, Vec<comprehension>) | |
90 //GeneratorExp(Box<expr>, Vec<comprehension>) | |
91 | |
92 // the grammar constrains where yield expressions can occur | |
93 //Await(expr value) | |
94 //Yield(expr? value) | |
95 //YieldFrom(expr value) | |
96 | |
97 // need sequences for compare to distinguish between | |
98 // x < 4 < 3 and (x < 4) < 3 | |
99 Compare(Box<expr>, Vec<cmpop>, Vec<expr>), | |
100 | |
101 Call(Box<expr>, Vec<expr>, Vec<keyword>), | |
102 Num(String), | |
103 Str(String), | |
104 //Bytes(String), | |
33 NameConstant(String), | 105 NameConstant(String), |
34 Str(String), | 106 //Ellipsis, |
35 Num(String), | 107 |
36 List(Vec<Expr>), | 108 // the following expression can appear in assignment context |
37 Error | 109 Attribute(Box<expr>, String, expr_context), |
38 } | 110 //Subscript(Box<expr>, slice, expr_context), |
39 | 111 //Starred(Box<expr>, expr_context), |
40 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | 112 Name(String, expr_context), |
41 pub enum UnaryOp { | 113 List(Vec<expr>, expr_context), |
114 //Tuple(Vec<expr>, expr_context), | |
115 } | |
116 | |
117 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
118 pub enum expr_context { | |
119 Load, | |
120 Store, | |
121 Del, | |
122 AugLoad, | |
123 AugStore, | |
124 Param | |
125 } | |
126 | |
127 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
128 pub enum slice { | |
129 Slice(Option<expr>, Option<expr>, Option<expr>), | |
130 ExtSlice(Vec<slice>), | |
131 Index(expr) | |
132 } | |
133 | |
134 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
135 pub enum boolop { | |
136 And, | |
137 Or | |
138 } | |
139 | |
140 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
141 pub enum operator { | |
142 Add, | |
143 Sub, | |
144 Mult, | |
145 MatMult, | |
146 Div, | |
147 Mod, | |
148 Pow, | |
149 LShift, | |
150 RShift, | |
151 BitOr, | |
152 BitXor, | |
153 BitAnd, | |
154 FloorDiv | |
155 } | |
156 | |
157 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
158 pub enum unaryop { | |
159 Invert, | |
160 Not, | |
42 UAdd, | 161 UAdd, |
43 USub, | 162 USub |
44 Error | 163 } |
45 } | 164 |
46 | 165 #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
47 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | 166 pub enum cmpop { |
48 pub enum BinOp { | 167 Eq, |
49 BinAdd, | 168 NotEq, |
50 BinMult, | 169 Lt, |
51 BinEq, | 170 LtE, |
52 BinLt, | 171 Gt, |
53 BinGt, | 172 GtE, |
54 Sub, | 173 Is, |
55 Div, | 174 IsNot, |
56 Error | 175 In, |
57 } | 176 NotIn |
177 } | |
178 | |
179 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
180 pub struct comprehension { | |
181 pub target: expr, | |
182 pub iter: expr, | |
183 pub ifs: Vec<expr> | |
184 } | |
185 | |
186 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
187 pub enum excepthandler { | |
188 ExceptHandler(Option<expr>, Option<String>, Vec<stmt>) | |
189 } | |
190 | |
191 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
192 pub struct arguments { | |
193 pub args: Vec<arg>, | |
194 pub vararg: Option<arg>, | |
195 pub kwonlyargs: Vec<arg>, | |
196 pub kw_defaults: Vec<expr>, | |
197 pub kwarg: Option<arg>, | |
198 pub defaults: Vec<expr> | |
199 } | |
200 | |
201 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
202 pub struct arg { | |
203 pub arg: String, | |
204 pub annotation: Option<expr> | |
205 } | |
206 | |
207 // keyword arguments supplied to call (NULL identifier for **kwargs) | |
208 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
209 pub struct keyword { | |
210 pub arg: Option<String>, | |
211 pub value: expr | |
212 } | |
213 | |
214 // import name with optional 'as' alias. | |
215 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
216 pub struct alias { | |
217 pub name: String, | |
218 pub asname: Option<String> | |
219 } | |
220 | |
221 #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
222 pub struct withitem { | |
223 pub context_expr: expr, | |
224 pub optional_vars: Option<expr> | |
225 } |