1 | n/a | /* |
---|
2 | n/a | * This file includes functions to transform a concrete syntax tree (CST) to |
---|
3 | n/a | * an abstract syntax tree (AST). The main function is PyAST_FromNode(). |
---|
4 | n/a | * |
---|
5 | n/a | */ |
---|
6 | n/a | #include "Python.h" |
---|
7 | n/a | #include "Python-ast.h" |
---|
8 | n/a | #include "node.h" |
---|
9 | n/a | #include "ast.h" |
---|
10 | n/a | #include "token.h" |
---|
11 | n/a | |
---|
12 | n/a | #include <assert.h> |
---|
13 | n/a | |
---|
14 | n/a | static int validate_stmts(asdl_seq *); |
---|
15 | n/a | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
---|
16 | n/a | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
---|
17 | n/a | static int validate_stmt(stmt_ty); |
---|
18 | n/a | static int validate_expr(expr_ty, expr_context_ty); |
---|
19 | n/a | |
---|
20 | n/a | static int |
---|
21 | n/a | validate_comprehension(asdl_seq *gens) |
---|
22 | n/a | { |
---|
23 | n/a | int i; |
---|
24 | n/a | if (!asdl_seq_LEN(gens)) { |
---|
25 | n/a | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
---|
26 | n/a | return 0; |
---|
27 | n/a | } |
---|
28 | n/a | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
---|
29 | n/a | comprehension_ty comp = asdl_seq_GET(gens, i); |
---|
30 | n/a | if (!validate_expr(comp->target, Store) || |
---|
31 | n/a | !validate_expr(comp->iter, Load) || |
---|
32 | n/a | !validate_exprs(comp->ifs, Load, 0)) |
---|
33 | n/a | return 0; |
---|
34 | n/a | } |
---|
35 | n/a | return 1; |
---|
36 | n/a | } |
---|
37 | n/a | |
---|
38 | n/a | static int |
---|
39 | n/a | validate_slice(slice_ty slice) |
---|
40 | n/a | { |
---|
41 | n/a | switch (slice->kind) { |
---|
42 | n/a | case Slice_kind: |
---|
43 | n/a | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
---|
44 | n/a | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
---|
45 | n/a | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
---|
46 | n/a | case ExtSlice_kind: { |
---|
47 | n/a | int i; |
---|
48 | n/a | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
---|
49 | n/a | return 0; |
---|
50 | n/a | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
---|
51 | n/a | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
---|
52 | n/a | return 0; |
---|
53 | n/a | return 1; |
---|
54 | n/a | } |
---|
55 | n/a | case Index_kind: |
---|
56 | n/a | return validate_expr(slice->v.Index.value, Load); |
---|
57 | n/a | default: |
---|
58 | n/a | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
---|
59 | n/a | return 0; |
---|
60 | n/a | } |
---|
61 | n/a | } |
---|
62 | n/a | |
---|
63 | n/a | static int |
---|
64 | n/a | validate_keywords(asdl_seq *keywords) |
---|
65 | n/a | { |
---|
66 | n/a | int i; |
---|
67 | n/a | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
---|
68 | n/a | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
---|
69 | n/a | return 0; |
---|
70 | n/a | return 1; |
---|
71 | n/a | } |
---|
72 | n/a | |
---|
73 | n/a | static int |
---|
74 | n/a | validate_args(asdl_seq *args) |
---|
75 | n/a | { |
---|
76 | n/a | int i; |
---|
77 | n/a | for (i = 0; i < asdl_seq_LEN(args); i++) { |
---|
78 | n/a | arg_ty arg = asdl_seq_GET(args, i); |
---|
79 | n/a | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
---|
80 | n/a | return 0; |
---|
81 | n/a | } |
---|
82 | n/a | return 1; |
---|
83 | n/a | } |
---|
84 | n/a | |
---|
85 | n/a | static const char * |
---|
86 | n/a | expr_context_name(expr_context_ty ctx) |
---|
87 | n/a | { |
---|
88 | n/a | switch (ctx) { |
---|
89 | n/a | case Load: |
---|
90 | n/a | return "Load"; |
---|
91 | n/a | case Store: |
---|
92 | n/a | return "Store"; |
---|
93 | n/a | case Del: |
---|
94 | n/a | return "Del"; |
---|
95 | n/a | case AugLoad: |
---|
96 | n/a | return "AugLoad"; |
---|
97 | n/a | case AugStore: |
---|
98 | n/a | return "AugStore"; |
---|
99 | n/a | case Param: |
---|
100 | n/a | return "Param"; |
---|
101 | n/a | default: |
---|
102 | n/a | assert(0); |
---|
103 | n/a | return "(unknown)"; |
---|
104 | n/a | } |
---|
105 | n/a | } |
---|
106 | n/a | |
---|
107 | n/a | static int |
---|
108 | n/a | validate_arguments(arguments_ty args) |
---|
109 | n/a | { |
---|
110 | n/a | if (!validate_args(args->args)) |
---|
111 | n/a | return 0; |
---|
112 | n/a | if (args->vararg && args->vararg->annotation |
---|
113 | n/a | && !validate_expr(args->vararg->annotation, Load)) { |
---|
114 | n/a | return 0; |
---|
115 | n/a | } |
---|
116 | n/a | if (!validate_args(args->kwonlyargs)) |
---|
117 | n/a | return 0; |
---|
118 | n/a | if (args->kwarg && args->kwarg->annotation |
---|
119 | n/a | && !validate_expr(args->kwarg->annotation, Load)) { |
---|
120 | n/a | return 0; |
---|
121 | n/a | } |
---|
122 | n/a | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
---|
123 | n/a | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
---|
124 | n/a | return 0; |
---|
125 | n/a | } |
---|
126 | n/a | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
---|
127 | n/a | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
---|
128 | n/a | "kw_defaults on arguments"); |
---|
129 | n/a | return 0; |
---|
130 | n/a | } |
---|
131 | n/a | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
---|
132 | n/a | } |
---|
133 | n/a | |
---|
134 | n/a | static int |
---|
135 | n/a | validate_constant(PyObject *value) |
---|
136 | n/a | { |
---|
137 | n/a | if (value == Py_None || value == Py_Ellipsis) |
---|
138 | n/a | return 1; |
---|
139 | n/a | |
---|
140 | n/a | if (PyLong_CheckExact(value) |
---|
141 | n/a | || PyFloat_CheckExact(value) |
---|
142 | n/a | || PyComplex_CheckExact(value) |
---|
143 | n/a | || PyBool_Check(value) |
---|
144 | n/a | || PyUnicode_CheckExact(value) |
---|
145 | n/a | || PyBytes_CheckExact(value)) |
---|
146 | n/a | return 1; |
---|
147 | n/a | |
---|
148 | n/a | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
---|
149 | n/a | PyObject *it; |
---|
150 | n/a | |
---|
151 | n/a | it = PyObject_GetIter(value); |
---|
152 | n/a | if (it == NULL) |
---|
153 | n/a | return 0; |
---|
154 | n/a | |
---|
155 | n/a | while (1) { |
---|
156 | n/a | PyObject *item = PyIter_Next(it); |
---|
157 | n/a | if (item == NULL) { |
---|
158 | n/a | if (PyErr_Occurred()) { |
---|
159 | n/a | Py_DECREF(it); |
---|
160 | n/a | return 0; |
---|
161 | n/a | } |
---|
162 | n/a | break; |
---|
163 | n/a | } |
---|
164 | n/a | |
---|
165 | n/a | if (!validate_constant(item)) { |
---|
166 | n/a | Py_DECREF(it); |
---|
167 | n/a | Py_DECREF(item); |
---|
168 | n/a | return 0; |
---|
169 | n/a | } |
---|
170 | n/a | Py_DECREF(item); |
---|
171 | n/a | } |
---|
172 | n/a | |
---|
173 | n/a | Py_DECREF(it); |
---|
174 | n/a | return 1; |
---|
175 | n/a | } |
---|
176 | n/a | |
---|
177 | n/a | return 0; |
---|
178 | n/a | } |
---|
179 | n/a | |
---|
180 | n/a | static int |
---|
181 | n/a | validate_expr(expr_ty exp, expr_context_ty ctx) |
---|
182 | n/a | { |
---|
183 | n/a | int check_ctx = 1; |
---|
184 | n/a | expr_context_ty actual_ctx; |
---|
185 | n/a | |
---|
186 | n/a | /* First check expression context. */ |
---|
187 | n/a | switch (exp->kind) { |
---|
188 | n/a | case Attribute_kind: |
---|
189 | n/a | actual_ctx = exp->v.Attribute.ctx; |
---|
190 | n/a | break; |
---|
191 | n/a | case Subscript_kind: |
---|
192 | n/a | actual_ctx = exp->v.Subscript.ctx; |
---|
193 | n/a | break; |
---|
194 | n/a | case Starred_kind: |
---|
195 | n/a | actual_ctx = exp->v.Starred.ctx; |
---|
196 | n/a | break; |
---|
197 | n/a | case Name_kind: |
---|
198 | n/a | actual_ctx = exp->v.Name.ctx; |
---|
199 | n/a | break; |
---|
200 | n/a | case List_kind: |
---|
201 | n/a | actual_ctx = exp->v.List.ctx; |
---|
202 | n/a | break; |
---|
203 | n/a | case Tuple_kind: |
---|
204 | n/a | actual_ctx = exp->v.Tuple.ctx; |
---|
205 | n/a | break; |
---|
206 | n/a | default: |
---|
207 | n/a | if (ctx != Load) { |
---|
208 | n/a | PyErr_Format(PyExc_ValueError, "expression which can't be " |
---|
209 | n/a | "assigned to in %s context", expr_context_name(ctx)); |
---|
210 | n/a | return 0; |
---|
211 | n/a | } |
---|
212 | n/a | check_ctx = 0; |
---|
213 | n/a | /* set actual_ctx to prevent gcc warning */ |
---|
214 | n/a | actual_ctx = 0; |
---|
215 | n/a | } |
---|
216 | n/a | if (check_ctx && actual_ctx != ctx) { |
---|
217 | n/a | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
---|
218 | n/a | expr_context_name(ctx), expr_context_name(actual_ctx)); |
---|
219 | n/a | return 0; |
---|
220 | n/a | } |
---|
221 | n/a | |
---|
222 | n/a | /* Now validate expression. */ |
---|
223 | n/a | switch (exp->kind) { |
---|
224 | n/a | case BoolOp_kind: |
---|
225 | n/a | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
---|
226 | n/a | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
---|
227 | n/a | return 0; |
---|
228 | n/a | } |
---|
229 | n/a | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
---|
230 | n/a | case BinOp_kind: |
---|
231 | n/a | return validate_expr(exp->v.BinOp.left, Load) && |
---|
232 | n/a | validate_expr(exp->v.BinOp.right, Load); |
---|
233 | n/a | case UnaryOp_kind: |
---|
234 | n/a | return validate_expr(exp->v.UnaryOp.operand, Load); |
---|
235 | n/a | case Lambda_kind: |
---|
236 | n/a | return validate_arguments(exp->v.Lambda.args) && |
---|
237 | n/a | validate_expr(exp->v.Lambda.body, Load); |
---|
238 | n/a | case IfExp_kind: |
---|
239 | n/a | return validate_expr(exp->v.IfExp.test, Load) && |
---|
240 | n/a | validate_expr(exp->v.IfExp.body, Load) && |
---|
241 | n/a | validate_expr(exp->v.IfExp.orelse, Load); |
---|
242 | n/a | case Dict_kind: |
---|
243 | n/a | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
---|
244 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
245 | n/a | "Dict doesn't have the same number of keys as values"); |
---|
246 | n/a | return 0; |
---|
247 | n/a | } |
---|
248 | n/a | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
---|
249 | n/a | dict literals, i.e. ``{**{a:b}}`` */ |
---|
250 | n/a | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
---|
251 | n/a | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
---|
252 | n/a | case Set_kind: |
---|
253 | n/a | return validate_exprs(exp->v.Set.elts, Load, 0); |
---|
254 | n/a | #define COMP(NAME) \ |
---|
255 | n/a | case NAME ## _kind: \ |
---|
256 | n/a | return validate_comprehension(exp->v.NAME.generators) && \ |
---|
257 | n/a | validate_expr(exp->v.NAME.elt, Load); |
---|
258 | n/a | COMP(ListComp) |
---|
259 | n/a | COMP(SetComp) |
---|
260 | n/a | COMP(GeneratorExp) |
---|
261 | n/a | #undef COMP |
---|
262 | n/a | case DictComp_kind: |
---|
263 | n/a | return validate_comprehension(exp->v.DictComp.generators) && |
---|
264 | n/a | validate_expr(exp->v.DictComp.key, Load) && |
---|
265 | n/a | validate_expr(exp->v.DictComp.value, Load); |
---|
266 | n/a | case Yield_kind: |
---|
267 | n/a | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
---|
268 | n/a | case YieldFrom_kind: |
---|
269 | n/a | return validate_expr(exp->v.YieldFrom.value, Load); |
---|
270 | n/a | case Await_kind: |
---|
271 | n/a | return validate_expr(exp->v.Await.value, Load); |
---|
272 | n/a | case Compare_kind: |
---|
273 | n/a | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
---|
274 | n/a | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
---|
275 | n/a | return 0; |
---|
276 | n/a | } |
---|
277 | n/a | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
---|
278 | n/a | asdl_seq_LEN(exp->v.Compare.ops)) { |
---|
279 | n/a | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
---|
280 | n/a | "of comparators and operands"); |
---|
281 | n/a | return 0; |
---|
282 | n/a | } |
---|
283 | n/a | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
---|
284 | n/a | validate_expr(exp->v.Compare.left, Load); |
---|
285 | n/a | case Call_kind: |
---|
286 | n/a | return validate_expr(exp->v.Call.func, Load) && |
---|
287 | n/a | validate_exprs(exp->v.Call.args, Load, 0) && |
---|
288 | n/a | validate_keywords(exp->v.Call.keywords); |
---|
289 | n/a | case Constant_kind: |
---|
290 | n/a | if (!validate_constant(exp->v.Constant.value)) { |
---|
291 | n/a | PyErr_Format(PyExc_TypeError, |
---|
292 | n/a | "got an invalid type in Constant: %s", |
---|
293 | n/a | Py_TYPE(exp->v.Constant.value)->tp_name); |
---|
294 | n/a | return 0; |
---|
295 | n/a | } |
---|
296 | n/a | return 1; |
---|
297 | n/a | case Num_kind: { |
---|
298 | n/a | PyObject *n = exp->v.Num.n; |
---|
299 | n/a | if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) && |
---|
300 | n/a | !PyComplex_CheckExact(n)) { |
---|
301 | n/a | PyErr_SetString(PyExc_TypeError, "non-numeric type in Num"); |
---|
302 | n/a | return 0; |
---|
303 | n/a | } |
---|
304 | n/a | return 1; |
---|
305 | n/a | } |
---|
306 | n/a | case Str_kind: { |
---|
307 | n/a | PyObject *s = exp->v.Str.s; |
---|
308 | n/a | if (!PyUnicode_CheckExact(s)) { |
---|
309 | n/a | PyErr_SetString(PyExc_TypeError, "non-string type in Str"); |
---|
310 | n/a | return 0; |
---|
311 | n/a | } |
---|
312 | n/a | return 1; |
---|
313 | n/a | } |
---|
314 | n/a | case JoinedStr_kind: |
---|
315 | n/a | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
---|
316 | n/a | case FormattedValue_kind: |
---|
317 | n/a | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
---|
318 | n/a | return 0; |
---|
319 | n/a | if (exp->v.FormattedValue.format_spec) |
---|
320 | n/a | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
---|
321 | n/a | return 1; |
---|
322 | n/a | case Bytes_kind: { |
---|
323 | n/a | PyObject *b = exp->v.Bytes.s; |
---|
324 | n/a | if (!PyBytes_CheckExact(b)) { |
---|
325 | n/a | PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes"); |
---|
326 | n/a | return 0; |
---|
327 | n/a | } |
---|
328 | n/a | return 1; |
---|
329 | n/a | } |
---|
330 | n/a | case Attribute_kind: |
---|
331 | n/a | return validate_expr(exp->v.Attribute.value, Load); |
---|
332 | n/a | case Subscript_kind: |
---|
333 | n/a | return validate_slice(exp->v.Subscript.slice) && |
---|
334 | n/a | validate_expr(exp->v.Subscript.value, Load); |
---|
335 | n/a | case Starred_kind: |
---|
336 | n/a | return validate_expr(exp->v.Starred.value, ctx); |
---|
337 | n/a | case List_kind: |
---|
338 | n/a | return validate_exprs(exp->v.List.elts, ctx, 0); |
---|
339 | n/a | case Tuple_kind: |
---|
340 | n/a | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
---|
341 | n/a | /* These last cases don't have any checking. */ |
---|
342 | n/a | case Name_kind: |
---|
343 | n/a | case NameConstant_kind: |
---|
344 | n/a | case Ellipsis_kind: |
---|
345 | n/a | return 1; |
---|
346 | n/a | default: |
---|
347 | n/a | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
---|
348 | n/a | return 0; |
---|
349 | n/a | } |
---|
350 | n/a | } |
---|
351 | n/a | |
---|
352 | n/a | static int |
---|
353 | n/a | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
---|
354 | n/a | { |
---|
355 | n/a | if (asdl_seq_LEN(seq)) |
---|
356 | n/a | return 1; |
---|
357 | n/a | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
---|
358 | n/a | return 0; |
---|
359 | n/a | } |
---|
360 | n/a | |
---|
361 | n/a | static int |
---|
362 | n/a | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
---|
363 | n/a | { |
---|
364 | n/a | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
---|
365 | n/a | validate_exprs(targets, ctx, 0); |
---|
366 | n/a | } |
---|
367 | n/a | |
---|
368 | n/a | static int |
---|
369 | n/a | validate_body(asdl_seq *body, const char *owner) |
---|
370 | n/a | { |
---|
371 | n/a | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
---|
372 | n/a | } |
---|
373 | n/a | |
---|
374 | n/a | static int |
---|
375 | n/a | validate_stmt(stmt_ty stmt) |
---|
376 | n/a | { |
---|
377 | n/a | int i; |
---|
378 | n/a | switch (stmt->kind) { |
---|
379 | n/a | case FunctionDef_kind: |
---|
380 | n/a | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
---|
381 | n/a | validate_arguments(stmt->v.FunctionDef.args) && |
---|
382 | n/a | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
---|
383 | n/a | (!stmt->v.FunctionDef.returns || |
---|
384 | n/a | validate_expr(stmt->v.FunctionDef.returns, Load)); |
---|
385 | n/a | case ClassDef_kind: |
---|
386 | n/a | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
---|
387 | n/a | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
---|
388 | n/a | validate_keywords(stmt->v.ClassDef.keywords) && |
---|
389 | n/a | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
---|
390 | n/a | case Return_kind: |
---|
391 | n/a | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
---|
392 | n/a | case Delete_kind: |
---|
393 | n/a | return validate_assignlist(stmt->v.Delete.targets, Del); |
---|
394 | n/a | case Assign_kind: |
---|
395 | n/a | return validate_assignlist(stmt->v.Assign.targets, Store) && |
---|
396 | n/a | validate_expr(stmt->v.Assign.value, Load); |
---|
397 | n/a | case AugAssign_kind: |
---|
398 | n/a | return validate_expr(stmt->v.AugAssign.target, Store) && |
---|
399 | n/a | validate_expr(stmt->v.AugAssign.value, Load); |
---|
400 | n/a | case AnnAssign_kind: |
---|
401 | n/a | if (stmt->v.AnnAssign.target->kind != Name_kind && |
---|
402 | n/a | stmt->v.AnnAssign.simple) { |
---|
403 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
404 | n/a | "AnnAssign with simple non-Name target"); |
---|
405 | n/a | return 0; |
---|
406 | n/a | } |
---|
407 | n/a | return validate_expr(stmt->v.AnnAssign.target, Store) && |
---|
408 | n/a | (!stmt->v.AnnAssign.value || |
---|
409 | n/a | validate_expr(stmt->v.AnnAssign.value, Load)) && |
---|
410 | n/a | validate_expr(stmt->v.AnnAssign.annotation, Load); |
---|
411 | n/a | case For_kind: |
---|
412 | n/a | return validate_expr(stmt->v.For.target, Store) && |
---|
413 | n/a | validate_expr(stmt->v.For.iter, Load) && |
---|
414 | n/a | validate_body(stmt->v.For.body, "For") && |
---|
415 | n/a | validate_stmts(stmt->v.For.orelse); |
---|
416 | n/a | case AsyncFor_kind: |
---|
417 | n/a | return validate_expr(stmt->v.AsyncFor.target, Store) && |
---|
418 | n/a | validate_expr(stmt->v.AsyncFor.iter, Load) && |
---|
419 | n/a | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
---|
420 | n/a | validate_stmts(stmt->v.AsyncFor.orelse); |
---|
421 | n/a | case While_kind: |
---|
422 | n/a | return validate_expr(stmt->v.While.test, Load) && |
---|
423 | n/a | validate_body(stmt->v.While.body, "While") && |
---|
424 | n/a | validate_stmts(stmt->v.While.orelse); |
---|
425 | n/a | case If_kind: |
---|
426 | n/a | return validate_expr(stmt->v.If.test, Load) && |
---|
427 | n/a | validate_body(stmt->v.If.body, "If") && |
---|
428 | n/a | validate_stmts(stmt->v.If.orelse); |
---|
429 | n/a | case With_kind: |
---|
430 | n/a | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
---|
431 | n/a | return 0; |
---|
432 | n/a | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
---|
433 | n/a | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
---|
434 | n/a | if (!validate_expr(item->context_expr, Load) || |
---|
435 | n/a | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
---|
436 | n/a | return 0; |
---|
437 | n/a | } |
---|
438 | n/a | return validate_body(stmt->v.With.body, "With"); |
---|
439 | n/a | case AsyncWith_kind: |
---|
440 | n/a | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
---|
441 | n/a | return 0; |
---|
442 | n/a | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
---|
443 | n/a | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
---|
444 | n/a | if (!validate_expr(item->context_expr, Load) || |
---|
445 | n/a | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
---|
446 | n/a | return 0; |
---|
447 | n/a | } |
---|
448 | n/a | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
---|
449 | n/a | case Raise_kind: |
---|
450 | n/a | if (stmt->v.Raise.exc) { |
---|
451 | n/a | return validate_expr(stmt->v.Raise.exc, Load) && |
---|
452 | n/a | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
---|
453 | n/a | } |
---|
454 | n/a | if (stmt->v.Raise.cause) { |
---|
455 | n/a | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
---|
456 | n/a | return 0; |
---|
457 | n/a | } |
---|
458 | n/a | return 1; |
---|
459 | n/a | case Try_kind: |
---|
460 | n/a | if (!validate_body(stmt->v.Try.body, "Try")) |
---|
461 | n/a | return 0; |
---|
462 | n/a | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
---|
463 | n/a | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
---|
464 | n/a | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
---|
465 | n/a | return 0; |
---|
466 | n/a | } |
---|
467 | n/a | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
---|
468 | n/a | asdl_seq_LEN(stmt->v.Try.orelse)) { |
---|
469 | n/a | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
---|
470 | n/a | return 0; |
---|
471 | n/a | } |
---|
472 | n/a | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
---|
473 | n/a | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
---|
474 | n/a | if ((handler->v.ExceptHandler.type && |
---|
475 | n/a | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
---|
476 | n/a | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
---|
477 | n/a | return 0; |
---|
478 | n/a | } |
---|
479 | n/a | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
---|
480 | n/a | validate_stmts(stmt->v.Try.finalbody)) && |
---|
481 | n/a | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
---|
482 | n/a | validate_stmts(stmt->v.Try.orelse)); |
---|
483 | n/a | case Assert_kind: |
---|
484 | n/a | return validate_expr(stmt->v.Assert.test, Load) && |
---|
485 | n/a | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
---|
486 | n/a | case Import_kind: |
---|
487 | n/a | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
---|
488 | n/a | case ImportFrom_kind: |
---|
489 | n/a | if (stmt->v.ImportFrom.level < 0) { |
---|
490 | n/a | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
---|
491 | n/a | return 0; |
---|
492 | n/a | } |
---|
493 | n/a | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
---|
494 | n/a | case Global_kind: |
---|
495 | n/a | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
---|
496 | n/a | case Nonlocal_kind: |
---|
497 | n/a | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
---|
498 | n/a | case Expr_kind: |
---|
499 | n/a | return validate_expr(stmt->v.Expr.value, Load); |
---|
500 | n/a | case AsyncFunctionDef_kind: |
---|
501 | n/a | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
---|
502 | n/a | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
---|
503 | n/a | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
---|
504 | n/a | (!stmt->v.AsyncFunctionDef.returns || |
---|
505 | n/a | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
---|
506 | n/a | case Pass_kind: |
---|
507 | n/a | case Break_kind: |
---|
508 | n/a | case Continue_kind: |
---|
509 | n/a | return 1; |
---|
510 | n/a | default: |
---|
511 | n/a | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
---|
512 | n/a | return 0; |
---|
513 | n/a | } |
---|
514 | n/a | } |
---|
515 | n/a | |
---|
516 | n/a | static int |
---|
517 | n/a | validate_stmts(asdl_seq *seq) |
---|
518 | n/a | { |
---|
519 | n/a | int i; |
---|
520 | n/a | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
---|
521 | n/a | stmt_ty stmt = asdl_seq_GET(seq, i); |
---|
522 | n/a | if (stmt) { |
---|
523 | n/a | if (!validate_stmt(stmt)) |
---|
524 | n/a | return 0; |
---|
525 | n/a | } |
---|
526 | n/a | else { |
---|
527 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
528 | n/a | "None disallowed in statement list"); |
---|
529 | n/a | return 0; |
---|
530 | n/a | } |
---|
531 | n/a | } |
---|
532 | n/a | return 1; |
---|
533 | n/a | } |
---|
534 | n/a | |
---|
535 | n/a | static int |
---|
536 | n/a | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
---|
537 | n/a | { |
---|
538 | n/a | int i; |
---|
539 | n/a | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
---|
540 | n/a | expr_ty expr = asdl_seq_GET(exprs, i); |
---|
541 | n/a | if (expr) { |
---|
542 | n/a | if (!validate_expr(expr, ctx)) |
---|
543 | n/a | return 0; |
---|
544 | n/a | } |
---|
545 | n/a | else if (!null_ok) { |
---|
546 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
547 | n/a | "None disallowed in expression list"); |
---|
548 | n/a | return 0; |
---|
549 | n/a | } |
---|
550 | n/a | |
---|
551 | n/a | } |
---|
552 | n/a | return 1; |
---|
553 | n/a | } |
---|
554 | n/a | |
---|
555 | n/a | int |
---|
556 | n/a | PyAST_Validate(mod_ty mod) |
---|
557 | n/a | { |
---|
558 | n/a | int res = 0; |
---|
559 | n/a | |
---|
560 | n/a | switch (mod->kind) { |
---|
561 | n/a | case Module_kind: |
---|
562 | n/a | res = validate_stmts(mod->v.Module.body); |
---|
563 | n/a | break; |
---|
564 | n/a | case Interactive_kind: |
---|
565 | n/a | res = validate_stmts(mod->v.Interactive.body); |
---|
566 | n/a | break; |
---|
567 | n/a | case Expression_kind: |
---|
568 | n/a | res = validate_expr(mod->v.Expression.body, Load); |
---|
569 | n/a | break; |
---|
570 | n/a | case Suite_kind: |
---|
571 | n/a | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
---|
572 | n/a | break; |
---|
573 | n/a | default: |
---|
574 | n/a | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
---|
575 | n/a | res = 0; |
---|
576 | n/a | break; |
---|
577 | n/a | } |
---|
578 | n/a | return res; |
---|
579 | n/a | } |
---|
580 | n/a | |
---|
581 | n/a | /* This is done here, so defines like "test" don't interfere with AST use above. */ |
---|
582 | n/a | #include "grammar.h" |
---|
583 | n/a | #include "parsetok.h" |
---|
584 | n/a | #include "graminit.h" |
---|
585 | n/a | |
---|
586 | n/a | /* Data structure used internally */ |
---|
587 | n/a | struct compiling { |
---|
588 | n/a | PyArena *c_arena; /* Arena for allocating memory. */ |
---|
589 | n/a | PyObject *c_filename; /* filename */ |
---|
590 | n/a | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
---|
591 | n/a | PyObject *c_normalize_args; /* Normalization argument tuple. */ |
---|
592 | n/a | }; |
---|
593 | n/a | |
---|
594 | n/a | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
---|
595 | n/a | static expr_ty ast_for_expr(struct compiling *, const node *); |
---|
596 | n/a | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
---|
597 | n/a | static asdl_seq *ast_for_suite(struct compiling *, const node *); |
---|
598 | n/a | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
---|
599 | n/a | expr_context_ty); |
---|
600 | n/a | static expr_ty ast_for_testlist(struct compiling *, const node *); |
---|
601 | n/a | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
---|
602 | n/a | |
---|
603 | n/a | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int); |
---|
604 | n/a | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int); |
---|
605 | n/a | |
---|
606 | n/a | /* Note different signature for ast_for_call */ |
---|
607 | n/a | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); |
---|
608 | n/a | |
---|
609 | n/a | static PyObject *parsenumber(struct compiling *, const char *); |
---|
610 | n/a | static expr_ty parsestrplus(struct compiling *, const node *n); |
---|
611 | n/a | |
---|
612 | n/a | #define COMP_GENEXP 0 |
---|
613 | n/a | #define COMP_LISTCOMP 1 |
---|
614 | n/a | #define COMP_SETCOMP 2 |
---|
615 | n/a | |
---|
616 | n/a | static int |
---|
617 | n/a | init_normalization(struct compiling *c) |
---|
618 | n/a | { |
---|
619 | n/a | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
---|
620 | n/a | if (!m) |
---|
621 | n/a | return 0; |
---|
622 | n/a | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
---|
623 | n/a | Py_DECREF(m); |
---|
624 | n/a | if (!c->c_normalize) |
---|
625 | n/a | return 0; |
---|
626 | n/a | c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None); |
---|
627 | n/a | if (!c->c_normalize_args) { |
---|
628 | n/a | Py_CLEAR(c->c_normalize); |
---|
629 | n/a | return 0; |
---|
630 | n/a | } |
---|
631 | n/a | PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL); |
---|
632 | n/a | return 1; |
---|
633 | n/a | } |
---|
634 | n/a | |
---|
635 | n/a | static identifier |
---|
636 | n/a | new_identifier(const char *n, struct compiling *c) |
---|
637 | n/a | { |
---|
638 | n/a | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
---|
639 | n/a | if (!id) |
---|
640 | n/a | return NULL; |
---|
641 | n/a | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
---|
642 | n/a | assert(PyUnicode_IS_READY(id)); |
---|
643 | n/a | /* Check whether there are non-ASCII characters in the |
---|
644 | n/a | identifier; if so, normalize to NFKC. */ |
---|
645 | n/a | if (!PyUnicode_IS_ASCII(id)) { |
---|
646 | n/a | PyObject *id2; |
---|
647 | n/a | if (!c->c_normalize && !init_normalization(c)) { |
---|
648 | n/a | Py_DECREF(id); |
---|
649 | n/a | return NULL; |
---|
650 | n/a | } |
---|
651 | n/a | PyTuple_SET_ITEM(c->c_normalize_args, 1, id); |
---|
652 | n/a | id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL); |
---|
653 | n/a | Py_DECREF(id); |
---|
654 | n/a | if (!id2) |
---|
655 | n/a | return NULL; |
---|
656 | n/a | id = id2; |
---|
657 | n/a | } |
---|
658 | n/a | PyUnicode_InternInPlace(&id); |
---|
659 | n/a | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
---|
660 | n/a | Py_DECREF(id); |
---|
661 | n/a | return NULL; |
---|
662 | n/a | } |
---|
663 | n/a | return id; |
---|
664 | n/a | } |
---|
665 | n/a | |
---|
666 | n/a | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
---|
667 | n/a | |
---|
668 | n/a | static int |
---|
669 | n/a | ast_error(struct compiling *c, const node *n, const char *errmsg) |
---|
670 | n/a | { |
---|
671 | n/a | PyObject *value, *errstr, *loc, *tmp; |
---|
672 | n/a | |
---|
673 | n/a | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
---|
674 | n/a | if (!loc) { |
---|
675 | n/a | Py_INCREF(Py_None); |
---|
676 | n/a | loc = Py_None; |
---|
677 | n/a | } |
---|
678 | n/a | tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc); |
---|
679 | n/a | if (!tmp) |
---|
680 | n/a | return 0; |
---|
681 | n/a | errstr = PyUnicode_FromString(errmsg); |
---|
682 | n/a | if (!errstr) { |
---|
683 | n/a | Py_DECREF(tmp); |
---|
684 | n/a | return 0; |
---|
685 | n/a | } |
---|
686 | n/a | value = PyTuple_Pack(2, errstr, tmp); |
---|
687 | n/a | Py_DECREF(errstr); |
---|
688 | n/a | Py_DECREF(tmp); |
---|
689 | n/a | if (value) { |
---|
690 | n/a | PyErr_SetObject(PyExc_SyntaxError, value); |
---|
691 | n/a | Py_DECREF(value); |
---|
692 | n/a | } |
---|
693 | n/a | return 0; |
---|
694 | n/a | } |
---|
695 | n/a | |
---|
696 | n/a | /* num_stmts() returns number of contained statements. |
---|
697 | n/a | |
---|
698 | n/a | Use this routine to determine how big a sequence is needed for |
---|
699 | n/a | the statements in a parse tree. Its raison d'etre is this bit of |
---|
700 | n/a | grammar: |
---|
701 | n/a | |
---|
702 | n/a | stmt: simple_stmt | compound_stmt |
---|
703 | n/a | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
---|
704 | n/a | |
---|
705 | n/a | A simple_stmt can contain multiple small_stmt elements joined |
---|
706 | n/a | by semicolons. If the arg is a simple_stmt, the number of |
---|
707 | n/a | small_stmt elements is returned. |
---|
708 | n/a | */ |
---|
709 | n/a | |
---|
710 | n/a | static int |
---|
711 | n/a | num_stmts(const node *n) |
---|
712 | n/a | { |
---|
713 | n/a | int i, l; |
---|
714 | n/a | node *ch; |
---|
715 | n/a | |
---|
716 | n/a | switch (TYPE(n)) { |
---|
717 | n/a | case single_input: |
---|
718 | n/a | if (TYPE(CHILD(n, 0)) == NEWLINE) |
---|
719 | n/a | return 0; |
---|
720 | n/a | else |
---|
721 | n/a | return num_stmts(CHILD(n, 0)); |
---|
722 | n/a | case file_input: |
---|
723 | n/a | l = 0; |
---|
724 | n/a | for (i = 0; i < NCH(n); i++) { |
---|
725 | n/a | ch = CHILD(n, i); |
---|
726 | n/a | if (TYPE(ch) == stmt) |
---|
727 | n/a | l += num_stmts(ch); |
---|
728 | n/a | } |
---|
729 | n/a | return l; |
---|
730 | n/a | case stmt: |
---|
731 | n/a | return num_stmts(CHILD(n, 0)); |
---|
732 | n/a | case compound_stmt: |
---|
733 | n/a | return 1; |
---|
734 | n/a | case simple_stmt: |
---|
735 | n/a | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
---|
736 | n/a | case suite: |
---|
737 | n/a | if (NCH(n) == 1) |
---|
738 | n/a | return num_stmts(CHILD(n, 0)); |
---|
739 | n/a | else { |
---|
740 | n/a | l = 0; |
---|
741 | n/a | for (i = 2; i < (NCH(n) - 1); i++) |
---|
742 | n/a | l += num_stmts(CHILD(n, i)); |
---|
743 | n/a | return l; |
---|
744 | n/a | } |
---|
745 | n/a | default: { |
---|
746 | n/a | char buf[128]; |
---|
747 | n/a | |
---|
748 | n/a | sprintf(buf, "Non-statement found: %d %d", |
---|
749 | n/a | TYPE(n), NCH(n)); |
---|
750 | n/a | Py_FatalError(buf); |
---|
751 | n/a | } |
---|
752 | n/a | } |
---|
753 | n/a | assert(0); |
---|
754 | n/a | return 0; |
---|
755 | n/a | } |
---|
756 | n/a | |
---|
757 | n/a | /* Transform the CST rooted at node * to the appropriate AST |
---|
758 | n/a | */ |
---|
759 | n/a | |
---|
760 | n/a | mod_ty |
---|
761 | n/a | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
---|
762 | n/a | PyObject *filename, PyArena *arena) |
---|
763 | n/a | { |
---|
764 | n/a | int i, j, k, num; |
---|
765 | n/a | asdl_seq *stmts = NULL; |
---|
766 | n/a | stmt_ty s; |
---|
767 | n/a | node *ch; |
---|
768 | n/a | struct compiling c; |
---|
769 | n/a | mod_ty res = NULL; |
---|
770 | n/a | |
---|
771 | n/a | c.c_arena = arena; |
---|
772 | n/a | /* borrowed reference */ |
---|
773 | n/a | c.c_filename = filename; |
---|
774 | n/a | c.c_normalize = NULL; |
---|
775 | n/a | c.c_normalize_args = NULL; |
---|
776 | n/a | |
---|
777 | n/a | if (TYPE(n) == encoding_decl) |
---|
778 | n/a | n = CHILD(n, 0); |
---|
779 | n/a | |
---|
780 | n/a | k = 0; |
---|
781 | n/a | switch (TYPE(n)) { |
---|
782 | n/a | case file_input: |
---|
783 | n/a | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
---|
784 | n/a | if (!stmts) |
---|
785 | n/a | goto out; |
---|
786 | n/a | for (i = 0; i < NCH(n) - 1; i++) { |
---|
787 | n/a | ch = CHILD(n, i); |
---|
788 | n/a | if (TYPE(ch) == NEWLINE) |
---|
789 | n/a | continue; |
---|
790 | n/a | REQ(ch, stmt); |
---|
791 | n/a | num = num_stmts(ch); |
---|
792 | n/a | if (num == 1) { |
---|
793 | n/a | s = ast_for_stmt(&c, ch); |
---|
794 | n/a | if (!s) |
---|
795 | n/a | goto out; |
---|
796 | n/a | asdl_seq_SET(stmts, k++, s); |
---|
797 | n/a | } |
---|
798 | n/a | else { |
---|
799 | n/a | ch = CHILD(ch, 0); |
---|
800 | n/a | REQ(ch, simple_stmt); |
---|
801 | n/a | for (j = 0; j < num; j++) { |
---|
802 | n/a | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
---|
803 | n/a | if (!s) |
---|
804 | n/a | goto out; |
---|
805 | n/a | asdl_seq_SET(stmts, k++, s); |
---|
806 | n/a | } |
---|
807 | n/a | } |
---|
808 | n/a | } |
---|
809 | n/a | res = Module(stmts, arena); |
---|
810 | n/a | break; |
---|
811 | n/a | case eval_input: { |
---|
812 | n/a | expr_ty testlist_ast; |
---|
813 | n/a | |
---|
814 | n/a | /* XXX Why not comp_for here? */ |
---|
815 | n/a | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
---|
816 | n/a | if (!testlist_ast) |
---|
817 | n/a | goto out; |
---|
818 | n/a | res = Expression(testlist_ast, arena); |
---|
819 | n/a | break; |
---|
820 | n/a | } |
---|
821 | n/a | case single_input: |
---|
822 | n/a | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
---|
823 | n/a | stmts = _Py_asdl_seq_new(1, arena); |
---|
824 | n/a | if (!stmts) |
---|
825 | n/a | goto out; |
---|
826 | n/a | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
---|
827 | n/a | arena)); |
---|
828 | n/a | if (!asdl_seq_GET(stmts, 0)) |
---|
829 | n/a | goto out; |
---|
830 | n/a | res = Interactive(stmts, arena); |
---|
831 | n/a | } |
---|
832 | n/a | else { |
---|
833 | n/a | n = CHILD(n, 0); |
---|
834 | n/a | num = num_stmts(n); |
---|
835 | n/a | stmts = _Py_asdl_seq_new(num, arena); |
---|
836 | n/a | if (!stmts) |
---|
837 | n/a | goto out; |
---|
838 | n/a | if (num == 1) { |
---|
839 | n/a | s = ast_for_stmt(&c, n); |
---|
840 | n/a | if (!s) |
---|
841 | n/a | goto out; |
---|
842 | n/a | asdl_seq_SET(stmts, 0, s); |
---|
843 | n/a | } |
---|
844 | n/a | else { |
---|
845 | n/a | /* Only a simple_stmt can contain multiple statements. */ |
---|
846 | n/a | REQ(n, simple_stmt); |
---|
847 | n/a | for (i = 0; i < NCH(n); i += 2) { |
---|
848 | n/a | if (TYPE(CHILD(n, i)) == NEWLINE) |
---|
849 | n/a | break; |
---|
850 | n/a | s = ast_for_stmt(&c, CHILD(n, i)); |
---|
851 | n/a | if (!s) |
---|
852 | n/a | goto out; |
---|
853 | n/a | asdl_seq_SET(stmts, i / 2, s); |
---|
854 | n/a | } |
---|
855 | n/a | } |
---|
856 | n/a | |
---|
857 | n/a | res = Interactive(stmts, arena); |
---|
858 | n/a | } |
---|
859 | n/a | break; |
---|
860 | n/a | default: |
---|
861 | n/a | PyErr_Format(PyExc_SystemError, |
---|
862 | n/a | "invalid node %d for PyAST_FromNode", TYPE(n)); |
---|
863 | n/a | goto out; |
---|
864 | n/a | } |
---|
865 | n/a | out: |
---|
866 | n/a | if (c.c_normalize) { |
---|
867 | n/a | Py_DECREF(c.c_normalize); |
---|
868 | n/a | PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL); |
---|
869 | n/a | Py_DECREF(c.c_normalize_args); |
---|
870 | n/a | } |
---|
871 | n/a | return res; |
---|
872 | n/a | } |
---|
873 | n/a | |
---|
874 | n/a | mod_ty |
---|
875 | n/a | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
---|
876 | n/a | PyArena *arena) |
---|
877 | n/a | { |
---|
878 | n/a | mod_ty mod; |
---|
879 | n/a | PyObject *filename; |
---|
880 | n/a | filename = PyUnicode_DecodeFSDefault(filename_str); |
---|
881 | n/a | if (filename == NULL) |
---|
882 | n/a | return NULL; |
---|
883 | n/a | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
---|
884 | n/a | Py_DECREF(filename); |
---|
885 | n/a | return mod; |
---|
886 | n/a | |
---|
887 | n/a | } |
---|
888 | n/a | |
---|
889 | n/a | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
---|
890 | n/a | */ |
---|
891 | n/a | |
---|
892 | n/a | static operator_ty |
---|
893 | n/a | get_operator(const node *n) |
---|
894 | n/a | { |
---|
895 | n/a | switch (TYPE(n)) { |
---|
896 | n/a | case VBAR: |
---|
897 | n/a | return BitOr; |
---|
898 | n/a | case CIRCUMFLEX: |
---|
899 | n/a | return BitXor; |
---|
900 | n/a | case AMPER: |
---|
901 | n/a | return BitAnd; |
---|
902 | n/a | case LEFTSHIFT: |
---|
903 | n/a | return LShift; |
---|
904 | n/a | case RIGHTSHIFT: |
---|
905 | n/a | return RShift; |
---|
906 | n/a | case PLUS: |
---|
907 | n/a | return Add; |
---|
908 | n/a | case MINUS: |
---|
909 | n/a | return Sub; |
---|
910 | n/a | case STAR: |
---|
911 | n/a | return Mult; |
---|
912 | n/a | case AT: |
---|
913 | n/a | return MatMult; |
---|
914 | n/a | case SLASH: |
---|
915 | n/a | return Div; |
---|
916 | n/a | case DOUBLESLASH: |
---|
917 | n/a | return FloorDiv; |
---|
918 | n/a | case PERCENT: |
---|
919 | n/a | return Mod; |
---|
920 | n/a | default: |
---|
921 | n/a | return (operator_ty)0; |
---|
922 | n/a | } |
---|
923 | n/a | } |
---|
924 | n/a | |
---|
925 | n/a | static const char * const FORBIDDEN[] = { |
---|
926 | n/a | "None", |
---|
927 | n/a | "True", |
---|
928 | n/a | "False", |
---|
929 | n/a | NULL, |
---|
930 | n/a | }; |
---|
931 | n/a | |
---|
932 | n/a | static int |
---|
933 | n/a | forbidden_name(struct compiling *c, identifier name, const node *n, |
---|
934 | n/a | int full_checks) |
---|
935 | n/a | { |
---|
936 | n/a | assert(PyUnicode_Check(name)); |
---|
937 | n/a | if (_PyUnicode_EqualToASCIIString(name, "__debug__")) { |
---|
938 | n/a | ast_error(c, n, "assignment to keyword"); |
---|
939 | n/a | return 1; |
---|
940 | n/a | } |
---|
941 | n/a | if (_PyUnicode_EqualToASCIIString(name, "async") || |
---|
942 | n/a | _PyUnicode_EqualToASCIIString(name, "await")) |
---|
943 | n/a | { |
---|
944 | n/a | PyObject *message = PyUnicode_FromString( |
---|
945 | n/a | "'async' and 'await' will become reserved keywords" |
---|
946 | n/a | " in Python 3.7"); |
---|
947 | n/a | int ret; |
---|
948 | n/a | if (message == NULL) { |
---|
949 | n/a | return 1; |
---|
950 | n/a | } |
---|
951 | n/a | ret = PyErr_WarnExplicitObject( |
---|
952 | n/a | PyExc_DeprecationWarning, |
---|
953 | n/a | message, |
---|
954 | n/a | c->c_filename, |
---|
955 | n/a | LINENO(n), |
---|
956 | n/a | NULL, |
---|
957 | n/a | NULL); |
---|
958 | n/a | Py_DECREF(message); |
---|
959 | n/a | if (ret < 0) { |
---|
960 | n/a | return 1; |
---|
961 | n/a | } |
---|
962 | n/a | } |
---|
963 | n/a | if (full_checks) { |
---|
964 | n/a | const char * const *p; |
---|
965 | n/a | for (p = FORBIDDEN; *p; p++) { |
---|
966 | n/a | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
---|
967 | n/a | ast_error(c, n, "assignment to keyword"); |
---|
968 | n/a | return 1; |
---|
969 | n/a | } |
---|
970 | n/a | } |
---|
971 | n/a | } |
---|
972 | n/a | return 0; |
---|
973 | n/a | } |
---|
974 | n/a | |
---|
975 | n/a | /* Set the context ctx for expr_ty e, recursively traversing e. |
---|
976 | n/a | |
---|
977 | n/a | Only sets context for expr kinds that "can appear in assignment context" |
---|
978 | n/a | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
---|
979 | n/a | an appropriate syntax error and returns false. |
---|
980 | n/a | */ |
---|
981 | n/a | |
---|
982 | n/a | static int |
---|
983 | n/a | set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) |
---|
984 | n/a | { |
---|
985 | n/a | asdl_seq *s = NULL; |
---|
986 | n/a | /* If a particular expression type can't be used for assign / delete, |
---|
987 | n/a | set expr_name to its name and an error message will be generated. |
---|
988 | n/a | */ |
---|
989 | n/a | const char* expr_name = NULL; |
---|
990 | n/a | |
---|
991 | n/a | /* The ast defines augmented store and load contexts, but the |
---|
992 | n/a | implementation here doesn't actually use them. The code may be |
---|
993 | n/a | a little more complex than necessary as a result. It also means |
---|
994 | n/a | that expressions in an augmented assignment have a Store context. |
---|
995 | n/a | Consider restructuring so that augmented assignment uses |
---|
996 | n/a | set_context(), too. |
---|
997 | n/a | */ |
---|
998 | n/a | assert(ctx != AugStore && ctx != AugLoad); |
---|
999 | n/a | |
---|
1000 | n/a | switch (e->kind) { |
---|
1001 | n/a | case Attribute_kind: |
---|
1002 | n/a | e->v.Attribute.ctx = ctx; |
---|
1003 | n/a | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
---|
1004 | n/a | return 0; |
---|
1005 | n/a | break; |
---|
1006 | n/a | case Subscript_kind: |
---|
1007 | n/a | e->v.Subscript.ctx = ctx; |
---|
1008 | n/a | break; |
---|
1009 | n/a | case Starred_kind: |
---|
1010 | n/a | e->v.Starred.ctx = ctx; |
---|
1011 | n/a | if (!set_context(c, e->v.Starred.value, ctx, n)) |
---|
1012 | n/a | return 0; |
---|
1013 | n/a | break; |
---|
1014 | n/a | case Name_kind: |
---|
1015 | n/a | if (ctx == Store) { |
---|
1016 | n/a | if (forbidden_name(c, e->v.Name.id, n, 0)) |
---|
1017 | n/a | return 0; /* forbidden_name() calls ast_error() */ |
---|
1018 | n/a | } |
---|
1019 | n/a | e->v.Name.ctx = ctx; |
---|
1020 | n/a | break; |
---|
1021 | n/a | case List_kind: |
---|
1022 | n/a | e->v.List.ctx = ctx; |
---|
1023 | n/a | s = e->v.List.elts; |
---|
1024 | n/a | break; |
---|
1025 | n/a | case Tuple_kind: |
---|
1026 | n/a | e->v.Tuple.ctx = ctx; |
---|
1027 | n/a | s = e->v.Tuple.elts; |
---|
1028 | n/a | break; |
---|
1029 | n/a | case Lambda_kind: |
---|
1030 | n/a | expr_name = "lambda"; |
---|
1031 | n/a | break; |
---|
1032 | n/a | case Call_kind: |
---|
1033 | n/a | expr_name = "function call"; |
---|
1034 | n/a | break; |
---|
1035 | n/a | case BoolOp_kind: |
---|
1036 | n/a | case BinOp_kind: |
---|
1037 | n/a | case UnaryOp_kind: |
---|
1038 | n/a | expr_name = "operator"; |
---|
1039 | n/a | break; |
---|
1040 | n/a | case GeneratorExp_kind: |
---|
1041 | n/a | expr_name = "generator expression"; |
---|
1042 | n/a | break; |
---|
1043 | n/a | case Yield_kind: |
---|
1044 | n/a | case YieldFrom_kind: |
---|
1045 | n/a | expr_name = "yield expression"; |
---|
1046 | n/a | break; |
---|
1047 | n/a | case Await_kind: |
---|
1048 | n/a | expr_name = "await expression"; |
---|
1049 | n/a | break; |
---|
1050 | n/a | case ListComp_kind: |
---|
1051 | n/a | expr_name = "list comprehension"; |
---|
1052 | n/a | break; |
---|
1053 | n/a | case SetComp_kind: |
---|
1054 | n/a | expr_name = "set comprehension"; |
---|
1055 | n/a | break; |
---|
1056 | n/a | case DictComp_kind: |
---|
1057 | n/a | expr_name = "dict comprehension"; |
---|
1058 | n/a | break; |
---|
1059 | n/a | case Dict_kind: |
---|
1060 | n/a | case Set_kind: |
---|
1061 | n/a | case Num_kind: |
---|
1062 | n/a | case Str_kind: |
---|
1063 | n/a | case Bytes_kind: |
---|
1064 | n/a | case JoinedStr_kind: |
---|
1065 | n/a | case FormattedValue_kind: |
---|
1066 | n/a | expr_name = "literal"; |
---|
1067 | n/a | break; |
---|
1068 | n/a | case NameConstant_kind: |
---|
1069 | n/a | expr_name = "keyword"; |
---|
1070 | n/a | break; |
---|
1071 | n/a | case Ellipsis_kind: |
---|
1072 | n/a | expr_name = "Ellipsis"; |
---|
1073 | n/a | break; |
---|
1074 | n/a | case Compare_kind: |
---|
1075 | n/a | expr_name = "comparison"; |
---|
1076 | n/a | break; |
---|
1077 | n/a | case IfExp_kind: |
---|
1078 | n/a | expr_name = "conditional expression"; |
---|
1079 | n/a | break; |
---|
1080 | n/a | default: |
---|
1081 | n/a | PyErr_Format(PyExc_SystemError, |
---|
1082 | n/a | "unexpected expression in assignment %d (line %d)", |
---|
1083 | n/a | e->kind, e->lineno); |
---|
1084 | n/a | return 0; |
---|
1085 | n/a | } |
---|
1086 | n/a | /* Check for error string set by switch */ |
---|
1087 | n/a | if (expr_name) { |
---|
1088 | n/a | char buf[300]; |
---|
1089 | n/a | PyOS_snprintf(buf, sizeof(buf), |
---|
1090 | n/a | "can't %s %s", |
---|
1091 | n/a | ctx == Store ? "assign to" : "delete", |
---|
1092 | n/a | expr_name); |
---|
1093 | n/a | return ast_error(c, n, buf); |
---|
1094 | n/a | } |
---|
1095 | n/a | |
---|
1096 | n/a | /* If the LHS is a list or tuple, we need to set the assignment |
---|
1097 | n/a | context for all the contained elements. |
---|
1098 | n/a | */ |
---|
1099 | n/a | if (s) { |
---|
1100 | n/a | int i; |
---|
1101 | n/a | |
---|
1102 | n/a | for (i = 0; i < asdl_seq_LEN(s); i++) { |
---|
1103 | n/a | if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n)) |
---|
1104 | n/a | return 0; |
---|
1105 | n/a | } |
---|
1106 | n/a | } |
---|
1107 | n/a | return 1; |
---|
1108 | n/a | } |
---|
1109 | n/a | |
---|
1110 | n/a | static operator_ty |
---|
1111 | n/a | ast_for_augassign(struct compiling *c, const node *n) |
---|
1112 | n/a | { |
---|
1113 | n/a | REQ(n, augassign); |
---|
1114 | n/a | n = CHILD(n, 0); |
---|
1115 | n/a | switch (STR(n)[0]) { |
---|
1116 | n/a | case '+': |
---|
1117 | n/a | return Add; |
---|
1118 | n/a | case '-': |
---|
1119 | n/a | return Sub; |
---|
1120 | n/a | case '/': |
---|
1121 | n/a | if (STR(n)[1] == '/') |
---|
1122 | n/a | return FloorDiv; |
---|
1123 | n/a | else |
---|
1124 | n/a | return Div; |
---|
1125 | n/a | case '%': |
---|
1126 | n/a | return Mod; |
---|
1127 | n/a | case '<': |
---|
1128 | n/a | return LShift; |
---|
1129 | n/a | case '>': |
---|
1130 | n/a | return RShift; |
---|
1131 | n/a | case '&': |
---|
1132 | n/a | return BitAnd; |
---|
1133 | n/a | case '^': |
---|
1134 | n/a | return BitXor; |
---|
1135 | n/a | case '|': |
---|
1136 | n/a | return BitOr; |
---|
1137 | n/a | case '*': |
---|
1138 | n/a | if (STR(n)[1] == '*') |
---|
1139 | n/a | return Pow; |
---|
1140 | n/a | else |
---|
1141 | n/a | return Mult; |
---|
1142 | n/a | case '@': |
---|
1143 | n/a | return MatMult; |
---|
1144 | n/a | default: |
---|
1145 | n/a | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
---|
1146 | n/a | return (operator_ty)0; |
---|
1147 | n/a | } |
---|
1148 | n/a | } |
---|
1149 | n/a | |
---|
1150 | n/a | static cmpop_ty |
---|
1151 | n/a | ast_for_comp_op(struct compiling *c, const node *n) |
---|
1152 | n/a | { |
---|
1153 | n/a | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
---|
1154 | n/a | |'is' 'not' |
---|
1155 | n/a | */ |
---|
1156 | n/a | REQ(n, comp_op); |
---|
1157 | n/a | if (NCH(n) == 1) { |
---|
1158 | n/a | n = CHILD(n, 0); |
---|
1159 | n/a | switch (TYPE(n)) { |
---|
1160 | n/a | case LESS: |
---|
1161 | n/a | return Lt; |
---|
1162 | n/a | case GREATER: |
---|
1163 | n/a | return Gt; |
---|
1164 | n/a | case EQEQUAL: /* == */ |
---|
1165 | n/a | return Eq; |
---|
1166 | n/a | case LESSEQUAL: |
---|
1167 | n/a | return LtE; |
---|
1168 | n/a | case GREATEREQUAL: |
---|
1169 | n/a | return GtE; |
---|
1170 | n/a | case NOTEQUAL: |
---|
1171 | n/a | return NotEq; |
---|
1172 | n/a | case NAME: |
---|
1173 | n/a | if (strcmp(STR(n), "in") == 0) |
---|
1174 | n/a | return In; |
---|
1175 | n/a | if (strcmp(STR(n), "is") == 0) |
---|
1176 | n/a | return Is; |
---|
1177 | n/a | default: |
---|
1178 | n/a | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
---|
1179 | n/a | STR(n)); |
---|
1180 | n/a | return (cmpop_ty)0; |
---|
1181 | n/a | } |
---|
1182 | n/a | } |
---|
1183 | n/a | else if (NCH(n) == 2) { |
---|
1184 | n/a | /* handle "not in" and "is not" */ |
---|
1185 | n/a | switch (TYPE(CHILD(n, 0))) { |
---|
1186 | n/a | case NAME: |
---|
1187 | n/a | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
---|
1188 | n/a | return NotIn; |
---|
1189 | n/a | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
---|
1190 | n/a | return IsNot; |
---|
1191 | n/a | default: |
---|
1192 | n/a | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
---|
1193 | n/a | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
---|
1194 | n/a | return (cmpop_ty)0; |
---|
1195 | n/a | } |
---|
1196 | n/a | } |
---|
1197 | n/a | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
---|
1198 | n/a | NCH(n)); |
---|
1199 | n/a | return (cmpop_ty)0; |
---|
1200 | n/a | } |
---|
1201 | n/a | |
---|
1202 | n/a | static asdl_seq * |
---|
1203 | n/a | seq_for_testlist(struct compiling *c, const node *n) |
---|
1204 | n/a | { |
---|
1205 | n/a | /* testlist: test (',' test)* [','] |
---|
1206 | n/a | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
---|
1207 | n/a | */ |
---|
1208 | n/a | asdl_seq *seq; |
---|
1209 | n/a | expr_ty expression; |
---|
1210 | n/a | int i; |
---|
1211 | n/a | assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp); |
---|
1212 | n/a | |
---|
1213 | n/a | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
---|
1214 | n/a | if (!seq) |
---|
1215 | n/a | return NULL; |
---|
1216 | n/a | |
---|
1217 | n/a | for (i = 0; i < NCH(n); i += 2) { |
---|
1218 | n/a | const node *ch = CHILD(n, i); |
---|
1219 | n/a | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
---|
1220 | n/a | |
---|
1221 | n/a | expression = ast_for_expr(c, ch); |
---|
1222 | n/a | if (!expression) |
---|
1223 | n/a | return NULL; |
---|
1224 | n/a | |
---|
1225 | n/a | assert(i / 2 < seq->size); |
---|
1226 | n/a | asdl_seq_SET(seq, i / 2, expression); |
---|
1227 | n/a | } |
---|
1228 | n/a | return seq; |
---|
1229 | n/a | } |
---|
1230 | n/a | |
---|
1231 | n/a | static arg_ty |
---|
1232 | n/a | ast_for_arg(struct compiling *c, const node *n) |
---|
1233 | n/a | { |
---|
1234 | n/a | identifier name; |
---|
1235 | n/a | expr_ty annotation = NULL; |
---|
1236 | n/a | node *ch; |
---|
1237 | n/a | arg_ty ret; |
---|
1238 | n/a | |
---|
1239 | n/a | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
---|
1240 | n/a | ch = CHILD(n, 0); |
---|
1241 | n/a | name = NEW_IDENTIFIER(ch); |
---|
1242 | n/a | if (!name) |
---|
1243 | n/a | return NULL; |
---|
1244 | n/a | if (forbidden_name(c, name, ch, 0)) |
---|
1245 | n/a | return NULL; |
---|
1246 | n/a | |
---|
1247 | n/a | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
---|
1248 | n/a | annotation = ast_for_expr(c, CHILD(n, 2)); |
---|
1249 | n/a | if (!annotation) |
---|
1250 | n/a | return NULL; |
---|
1251 | n/a | } |
---|
1252 | n/a | |
---|
1253 | n/a | ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena); |
---|
1254 | n/a | if (!ret) |
---|
1255 | n/a | return NULL; |
---|
1256 | n/a | return ret; |
---|
1257 | n/a | } |
---|
1258 | n/a | |
---|
1259 | n/a | /* returns -1 if failed to handle keyword only arguments |
---|
1260 | n/a | returns new position to keep processing if successful |
---|
1261 | n/a | (',' tfpdef ['=' test])* |
---|
1262 | n/a | ^^^ |
---|
1263 | n/a | start pointing here |
---|
1264 | n/a | */ |
---|
1265 | n/a | static int |
---|
1266 | n/a | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
---|
1267 | n/a | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
---|
1268 | n/a | { |
---|
1269 | n/a | PyObject *argname; |
---|
1270 | n/a | node *ch; |
---|
1271 | n/a | expr_ty expression, annotation; |
---|
1272 | n/a | arg_ty arg; |
---|
1273 | n/a | int i = start; |
---|
1274 | n/a | int j = 0; /* index for kwdefaults and kwonlyargs */ |
---|
1275 | n/a | |
---|
1276 | n/a | if (kwonlyargs == NULL) { |
---|
1277 | n/a | ast_error(c, CHILD(n, start), "named arguments must follow bare *"); |
---|
1278 | n/a | return -1; |
---|
1279 | n/a | } |
---|
1280 | n/a | assert(kwdefaults != NULL); |
---|
1281 | n/a | while (i < NCH(n)) { |
---|
1282 | n/a | ch = CHILD(n, i); |
---|
1283 | n/a | switch (TYPE(ch)) { |
---|
1284 | n/a | case vfpdef: |
---|
1285 | n/a | case tfpdef: |
---|
1286 | n/a | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
---|
1287 | n/a | expression = ast_for_expr(c, CHILD(n, i + 2)); |
---|
1288 | n/a | if (!expression) |
---|
1289 | n/a | goto error; |
---|
1290 | n/a | asdl_seq_SET(kwdefaults, j, expression); |
---|
1291 | n/a | i += 2; /* '=' and test */ |
---|
1292 | n/a | } |
---|
1293 | n/a | else { /* setting NULL if no default value exists */ |
---|
1294 | n/a | asdl_seq_SET(kwdefaults, j, NULL); |
---|
1295 | n/a | } |
---|
1296 | n/a | if (NCH(ch) == 3) { |
---|
1297 | n/a | /* ch is NAME ':' test */ |
---|
1298 | n/a | annotation = ast_for_expr(c, CHILD(ch, 2)); |
---|
1299 | n/a | if (!annotation) |
---|
1300 | n/a | goto error; |
---|
1301 | n/a | } |
---|
1302 | n/a | else { |
---|
1303 | n/a | annotation = NULL; |
---|
1304 | n/a | } |
---|
1305 | n/a | ch = CHILD(ch, 0); |
---|
1306 | n/a | argname = NEW_IDENTIFIER(ch); |
---|
1307 | n/a | if (!argname) |
---|
1308 | n/a | goto error; |
---|
1309 | n/a | if (forbidden_name(c, argname, ch, 0)) |
---|
1310 | n/a | goto error; |
---|
1311 | n/a | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
---|
1312 | n/a | c->c_arena); |
---|
1313 | n/a | if (!arg) |
---|
1314 | n/a | goto error; |
---|
1315 | n/a | asdl_seq_SET(kwonlyargs, j++, arg); |
---|
1316 | n/a | i += 2; /* the name and the comma */ |
---|
1317 | n/a | break; |
---|
1318 | n/a | case DOUBLESTAR: |
---|
1319 | n/a | return i; |
---|
1320 | n/a | default: |
---|
1321 | n/a | ast_error(c, ch, "unexpected node"); |
---|
1322 | n/a | goto error; |
---|
1323 | n/a | } |
---|
1324 | n/a | } |
---|
1325 | n/a | return i; |
---|
1326 | n/a | error: |
---|
1327 | n/a | return -1; |
---|
1328 | n/a | } |
---|
1329 | n/a | |
---|
1330 | n/a | /* Create AST for argument list. */ |
---|
1331 | n/a | |
---|
1332 | n/a | static arguments_ty |
---|
1333 | n/a | ast_for_arguments(struct compiling *c, const node *n) |
---|
1334 | n/a | { |
---|
1335 | n/a | /* This function handles both typedargslist (function definition) |
---|
1336 | n/a | and varargslist (lambda definition). |
---|
1337 | n/a | |
---|
1338 | n/a | parameters: '(' [typedargslist] ')' |
---|
1339 | n/a | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
---|
1340 | n/a | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
---|
1341 | n/a | | '**' tfpdef [',']]] |
---|
1342 | n/a | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
---|
1343 | n/a | | '**' tfpdef [',']) |
---|
1344 | n/a | tfpdef: NAME [':' test] |
---|
1345 | n/a | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
---|
1346 | n/a | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
---|
1347 | n/a | | '**' vfpdef [',']]] |
---|
1348 | n/a | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
---|
1349 | n/a | | '**' vfpdef [','] |
---|
1350 | n/a | ) |
---|
1351 | n/a | vfpdef: NAME |
---|
1352 | n/a | |
---|
1353 | n/a | */ |
---|
1354 | n/a | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
---|
1355 | n/a | int nposdefaults = 0, found_default = 0; |
---|
1356 | n/a | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
---|
1357 | n/a | arg_ty vararg = NULL, kwarg = NULL; |
---|
1358 | n/a | arg_ty arg; |
---|
1359 | n/a | node *ch; |
---|
1360 | n/a | |
---|
1361 | n/a | if (TYPE(n) == parameters) { |
---|
1362 | n/a | if (NCH(n) == 2) /* () as argument list */ |
---|
1363 | n/a | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
---|
1364 | n/a | n = CHILD(n, 1); |
---|
1365 | n/a | } |
---|
1366 | n/a | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
---|
1367 | n/a | |
---|
1368 | n/a | /* First count the number of positional args & defaults. The |
---|
1369 | n/a | variable i is the loop index for this for loop and the next. |
---|
1370 | n/a | The next loop picks up where the first leaves off. |
---|
1371 | n/a | */ |
---|
1372 | n/a | for (i = 0; i < NCH(n); i++) { |
---|
1373 | n/a | ch = CHILD(n, i); |
---|
1374 | n/a | if (TYPE(ch) == STAR) { |
---|
1375 | n/a | /* skip star */ |
---|
1376 | n/a | i++; |
---|
1377 | n/a | if (i < NCH(n) && /* skip argument following star */ |
---|
1378 | n/a | (TYPE(CHILD(n, i)) == tfpdef || |
---|
1379 | n/a | TYPE(CHILD(n, i)) == vfpdef)) { |
---|
1380 | n/a | i++; |
---|
1381 | n/a | } |
---|
1382 | n/a | break; |
---|
1383 | n/a | } |
---|
1384 | n/a | if (TYPE(ch) == DOUBLESTAR) break; |
---|
1385 | n/a | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
---|
1386 | n/a | if (TYPE(ch) == EQUAL) nposdefaults++; |
---|
1387 | n/a | } |
---|
1388 | n/a | /* count the number of keyword only args & |
---|
1389 | n/a | defaults for keyword only args */ |
---|
1390 | n/a | for ( ; i < NCH(n); ++i) { |
---|
1391 | n/a | ch = CHILD(n, i); |
---|
1392 | n/a | if (TYPE(ch) == DOUBLESTAR) break; |
---|
1393 | n/a | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
---|
1394 | n/a | } |
---|
1395 | n/a | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
---|
1396 | n/a | if (!posargs && nposargs) |
---|
1397 | n/a | return NULL; |
---|
1398 | n/a | kwonlyargs = (nkwonlyargs ? |
---|
1399 | n/a | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
---|
1400 | n/a | if (!kwonlyargs && nkwonlyargs) |
---|
1401 | n/a | return NULL; |
---|
1402 | n/a | posdefaults = (nposdefaults ? |
---|
1403 | n/a | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
---|
1404 | n/a | if (!posdefaults && nposdefaults) |
---|
1405 | n/a | return NULL; |
---|
1406 | n/a | /* The length of kwonlyargs and kwdefaults are same |
---|
1407 | n/a | since we set NULL as default for keyword only argument w/o default |
---|
1408 | n/a | - we have sequence data structure, but no dictionary */ |
---|
1409 | n/a | kwdefaults = (nkwonlyargs ? |
---|
1410 | n/a | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
---|
1411 | n/a | if (!kwdefaults && nkwonlyargs) |
---|
1412 | n/a | return NULL; |
---|
1413 | n/a | |
---|
1414 | n/a | /* tfpdef: NAME [':' test] |
---|
1415 | n/a | vfpdef: NAME |
---|
1416 | n/a | */ |
---|
1417 | n/a | i = 0; |
---|
1418 | n/a | j = 0; /* index for defaults */ |
---|
1419 | n/a | k = 0; /* index for args */ |
---|
1420 | n/a | while (i < NCH(n)) { |
---|
1421 | n/a | ch = CHILD(n, i); |
---|
1422 | n/a | switch (TYPE(ch)) { |
---|
1423 | n/a | case tfpdef: |
---|
1424 | n/a | case vfpdef: |
---|
1425 | n/a | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
---|
1426 | n/a | anything other than EQUAL or a comma? */ |
---|
1427 | n/a | /* XXX Should NCH(n) check be made a separate check? */ |
---|
1428 | n/a | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
---|
1429 | n/a | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
---|
1430 | n/a | if (!expression) |
---|
1431 | n/a | return NULL; |
---|
1432 | n/a | assert(posdefaults != NULL); |
---|
1433 | n/a | asdl_seq_SET(posdefaults, j++, expression); |
---|
1434 | n/a | i += 2; |
---|
1435 | n/a | found_default = 1; |
---|
1436 | n/a | } |
---|
1437 | n/a | else if (found_default) { |
---|
1438 | n/a | ast_error(c, n, |
---|
1439 | n/a | "non-default argument follows default argument"); |
---|
1440 | n/a | return NULL; |
---|
1441 | n/a | } |
---|
1442 | n/a | arg = ast_for_arg(c, ch); |
---|
1443 | n/a | if (!arg) |
---|
1444 | n/a | return NULL; |
---|
1445 | n/a | asdl_seq_SET(posargs, k++, arg); |
---|
1446 | n/a | i += 2; /* the name and the comma */ |
---|
1447 | n/a | break; |
---|
1448 | n/a | case STAR: |
---|
1449 | n/a | if (i+1 >= NCH(n) || |
---|
1450 | n/a | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
---|
1451 | n/a | ast_error(c, CHILD(n, i), |
---|
1452 | n/a | "named arguments must follow bare *"); |
---|
1453 | n/a | return NULL; |
---|
1454 | n/a | } |
---|
1455 | n/a | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
---|
1456 | n/a | if (TYPE(ch) == COMMA) { |
---|
1457 | n/a | int res = 0; |
---|
1458 | n/a | i += 2; /* now follows keyword only arguments */ |
---|
1459 | n/a | res = handle_keywordonly_args(c, n, i, |
---|
1460 | n/a | kwonlyargs, kwdefaults); |
---|
1461 | n/a | if (res == -1) return NULL; |
---|
1462 | n/a | i = res; /* res has new position to process */ |
---|
1463 | n/a | } |
---|
1464 | n/a | else { |
---|
1465 | n/a | vararg = ast_for_arg(c, ch); |
---|
1466 | n/a | if (!vararg) |
---|
1467 | n/a | return NULL; |
---|
1468 | n/a | |
---|
1469 | n/a | i += 3; |
---|
1470 | n/a | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
---|
1471 | n/a | || TYPE(CHILD(n, i)) == vfpdef)) { |
---|
1472 | n/a | int res = 0; |
---|
1473 | n/a | res = handle_keywordonly_args(c, n, i, |
---|
1474 | n/a | kwonlyargs, kwdefaults); |
---|
1475 | n/a | if (res == -1) return NULL; |
---|
1476 | n/a | i = res; /* res has new position to process */ |
---|
1477 | n/a | } |
---|
1478 | n/a | } |
---|
1479 | n/a | break; |
---|
1480 | n/a | case DOUBLESTAR: |
---|
1481 | n/a | ch = CHILD(n, i+1); /* tfpdef */ |
---|
1482 | n/a | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
---|
1483 | n/a | kwarg = ast_for_arg(c, ch); |
---|
1484 | n/a | if (!kwarg) |
---|
1485 | n/a | return NULL; |
---|
1486 | n/a | i += 3; |
---|
1487 | n/a | break; |
---|
1488 | n/a | default: |
---|
1489 | n/a | PyErr_Format(PyExc_SystemError, |
---|
1490 | n/a | "unexpected node in varargslist: %d @ %d", |
---|
1491 | n/a | TYPE(ch), i); |
---|
1492 | n/a | return NULL; |
---|
1493 | n/a | } |
---|
1494 | n/a | } |
---|
1495 | n/a | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
---|
1496 | n/a | } |
---|
1497 | n/a | |
---|
1498 | n/a | static expr_ty |
---|
1499 | n/a | ast_for_dotted_name(struct compiling *c, const node *n) |
---|
1500 | n/a | { |
---|
1501 | n/a | expr_ty e; |
---|
1502 | n/a | identifier id; |
---|
1503 | n/a | int lineno, col_offset; |
---|
1504 | n/a | int i; |
---|
1505 | n/a | |
---|
1506 | n/a | REQ(n, dotted_name); |
---|
1507 | n/a | |
---|
1508 | n/a | lineno = LINENO(n); |
---|
1509 | n/a | col_offset = n->n_col_offset; |
---|
1510 | n/a | |
---|
1511 | n/a | id = NEW_IDENTIFIER(CHILD(n, 0)); |
---|
1512 | n/a | if (!id) |
---|
1513 | n/a | return NULL; |
---|
1514 | n/a | e = Name(id, Load, lineno, col_offset, c->c_arena); |
---|
1515 | n/a | if (!e) |
---|
1516 | n/a | return NULL; |
---|
1517 | n/a | |
---|
1518 | n/a | for (i = 2; i < NCH(n); i+=2) { |
---|
1519 | n/a | id = NEW_IDENTIFIER(CHILD(n, i)); |
---|
1520 | n/a | if (!id) |
---|
1521 | n/a | return NULL; |
---|
1522 | n/a | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
---|
1523 | n/a | if (!e) |
---|
1524 | n/a | return NULL; |
---|
1525 | n/a | } |
---|
1526 | n/a | |
---|
1527 | n/a | return e; |
---|
1528 | n/a | } |
---|
1529 | n/a | |
---|
1530 | n/a | static expr_ty |
---|
1531 | n/a | ast_for_decorator(struct compiling *c, const node *n) |
---|
1532 | n/a | { |
---|
1533 | n/a | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
---|
1534 | n/a | expr_ty d = NULL; |
---|
1535 | n/a | expr_ty name_expr; |
---|
1536 | n/a | |
---|
1537 | n/a | REQ(n, decorator); |
---|
1538 | n/a | REQ(CHILD(n, 0), AT); |
---|
1539 | n/a | REQ(RCHILD(n, -1), NEWLINE); |
---|
1540 | n/a | |
---|
1541 | n/a | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
---|
1542 | n/a | if (!name_expr) |
---|
1543 | n/a | return NULL; |
---|
1544 | n/a | |
---|
1545 | n/a | if (NCH(n) == 3) { /* No arguments */ |
---|
1546 | n/a | d = name_expr; |
---|
1547 | n/a | name_expr = NULL; |
---|
1548 | n/a | } |
---|
1549 | n/a | else if (NCH(n) == 5) { /* Call with no arguments */ |
---|
1550 | n/a | d = Call(name_expr, NULL, NULL, LINENO(n), |
---|
1551 | n/a | n->n_col_offset, c->c_arena); |
---|
1552 | n/a | if (!d) |
---|
1553 | n/a | return NULL; |
---|
1554 | n/a | name_expr = NULL; |
---|
1555 | n/a | } |
---|
1556 | n/a | else { |
---|
1557 | n/a | d = ast_for_call(c, CHILD(n, 3), name_expr); |
---|
1558 | n/a | if (!d) |
---|
1559 | n/a | return NULL; |
---|
1560 | n/a | name_expr = NULL; |
---|
1561 | n/a | } |
---|
1562 | n/a | |
---|
1563 | n/a | return d; |
---|
1564 | n/a | } |
---|
1565 | n/a | |
---|
1566 | n/a | static asdl_seq* |
---|
1567 | n/a | ast_for_decorators(struct compiling *c, const node *n) |
---|
1568 | n/a | { |
---|
1569 | n/a | asdl_seq* decorator_seq; |
---|
1570 | n/a | expr_ty d; |
---|
1571 | n/a | int i; |
---|
1572 | n/a | |
---|
1573 | n/a | REQ(n, decorators); |
---|
1574 | n/a | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
---|
1575 | n/a | if (!decorator_seq) |
---|
1576 | n/a | return NULL; |
---|
1577 | n/a | |
---|
1578 | n/a | for (i = 0; i < NCH(n); i++) { |
---|
1579 | n/a | d = ast_for_decorator(c, CHILD(n, i)); |
---|
1580 | n/a | if (!d) |
---|
1581 | n/a | return NULL; |
---|
1582 | n/a | asdl_seq_SET(decorator_seq, i, d); |
---|
1583 | n/a | } |
---|
1584 | n/a | return decorator_seq; |
---|
1585 | n/a | } |
---|
1586 | n/a | |
---|
1587 | n/a | static stmt_ty |
---|
1588 | n/a | ast_for_funcdef_impl(struct compiling *c, const node *n, |
---|
1589 | n/a | asdl_seq *decorator_seq, int is_async) |
---|
1590 | n/a | { |
---|
1591 | n/a | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
---|
1592 | n/a | identifier name; |
---|
1593 | n/a | arguments_ty args; |
---|
1594 | n/a | asdl_seq *body; |
---|
1595 | n/a | expr_ty returns = NULL; |
---|
1596 | n/a | int name_i = 1; |
---|
1597 | n/a | |
---|
1598 | n/a | REQ(n, funcdef); |
---|
1599 | n/a | |
---|
1600 | n/a | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
---|
1601 | n/a | if (!name) |
---|
1602 | n/a | return NULL; |
---|
1603 | n/a | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
---|
1604 | n/a | return NULL; |
---|
1605 | n/a | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
---|
1606 | n/a | if (!args) |
---|
1607 | n/a | return NULL; |
---|
1608 | n/a | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
---|
1609 | n/a | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
---|
1610 | n/a | if (!returns) |
---|
1611 | n/a | return NULL; |
---|
1612 | n/a | name_i += 2; |
---|
1613 | n/a | } |
---|
1614 | n/a | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
---|
1615 | n/a | if (!body) |
---|
1616 | n/a | return NULL; |
---|
1617 | n/a | |
---|
1618 | n/a | if (is_async) |
---|
1619 | n/a | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
---|
1620 | n/a | LINENO(n), |
---|
1621 | n/a | n->n_col_offset, c->c_arena); |
---|
1622 | n/a | else |
---|
1623 | n/a | return FunctionDef(name, args, body, decorator_seq, returns, |
---|
1624 | n/a | LINENO(n), |
---|
1625 | n/a | n->n_col_offset, c->c_arena); |
---|
1626 | n/a | } |
---|
1627 | n/a | |
---|
1628 | n/a | static stmt_ty |
---|
1629 | n/a | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
---|
1630 | n/a | { |
---|
1631 | n/a | /* async_funcdef: ASYNC funcdef */ |
---|
1632 | n/a | REQ(n, async_funcdef); |
---|
1633 | n/a | REQ(CHILD(n, 0), ASYNC); |
---|
1634 | n/a | REQ(CHILD(n, 1), funcdef); |
---|
1635 | n/a | |
---|
1636 | n/a | return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, |
---|
1637 | n/a | 1 /* is_async */); |
---|
1638 | n/a | } |
---|
1639 | n/a | |
---|
1640 | n/a | static stmt_ty |
---|
1641 | n/a | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
---|
1642 | n/a | { |
---|
1643 | n/a | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
---|
1644 | n/a | return ast_for_funcdef_impl(c, n, decorator_seq, |
---|
1645 | n/a | 0 /* is_async */); |
---|
1646 | n/a | } |
---|
1647 | n/a | |
---|
1648 | n/a | |
---|
1649 | n/a | static stmt_ty |
---|
1650 | n/a | ast_for_async_stmt(struct compiling *c, const node *n) |
---|
1651 | n/a | { |
---|
1652 | n/a | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
---|
1653 | n/a | REQ(n, async_stmt); |
---|
1654 | n/a | REQ(CHILD(n, 0), ASYNC); |
---|
1655 | n/a | |
---|
1656 | n/a | switch (TYPE(CHILD(n, 1))) { |
---|
1657 | n/a | case funcdef: |
---|
1658 | n/a | return ast_for_funcdef_impl(c, CHILD(n, 1), NULL, |
---|
1659 | n/a | 1 /* is_async */); |
---|
1660 | n/a | case with_stmt: |
---|
1661 | n/a | return ast_for_with_stmt(c, CHILD(n, 1), |
---|
1662 | n/a | 1 /* is_async */); |
---|
1663 | n/a | |
---|
1664 | n/a | case for_stmt: |
---|
1665 | n/a | return ast_for_for_stmt(c, CHILD(n, 1), |
---|
1666 | n/a | 1 /* is_async */); |
---|
1667 | n/a | |
---|
1668 | n/a | default: |
---|
1669 | n/a | PyErr_Format(PyExc_SystemError, |
---|
1670 | n/a | "invalid async stament: %s", |
---|
1671 | n/a | STR(CHILD(n, 1))); |
---|
1672 | n/a | return NULL; |
---|
1673 | n/a | } |
---|
1674 | n/a | } |
---|
1675 | n/a | |
---|
1676 | n/a | static stmt_ty |
---|
1677 | n/a | ast_for_decorated(struct compiling *c, const node *n) |
---|
1678 | n/a | { |
---|
1679 | n/a | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
---|
1680 | n/a | stmt_ty thing = NULL; |
---|
1681 | n/a | asdl_seq *decorator_seq = NULL; |
---|
1682 | n/a | |
---|
1683 | n/a | REQ(n, decorated); |
---|
1684 | n/a | |
---|
1685 | n/a | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
---|
1686 | n/a | if (!decorator_seq) |
---|
1687 | n/a | return NULL; |
---|
1688 | n/a | |
---|
1689 | n/a | assert(TYPE(CHILD(n, 1)) == funcdef || |
---|
1690 | n/a | TYPE(CHILD(n, 1)) == async_funcdef || |
---|
1691 | n/a | TYPE(CHILD(n, 1)) == classdef); |
---|
1692 | n/a | |
---|
1693 | n/a | if (TYPE(CHILD(n, 1)) == funcdef) { |
---|
1694 | n/a | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
---|
1695 | n/a | } else if (TYPE(CHILD(n, 1)) == classdef) { |
---|
1696 | n/a | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
---|
1697 | n/a | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
---|
1698 | n/a | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
---|
1699 | n/a | } |
---|
1700 | n/a | /* we count the decorators in when talking about the class' or |
---|
1701 | n/a | * function's line number */ |
---|
1702 | n/a | if (thing) { |
---|
1703 | n/a | thing->lineno = LINENO(n); |
---|
1704 | n/a | thing->col_offset = n->n_col_offset; |
---|
1705 | n/a | } |
---|
1706 | n/a | return thing; |
---|
1707 | n/a | } |
---|
1708 | n/a | |
---|
1709 | n/a | static expr_ty |
---|
1710 | n/a | ast_for_lambdef(struct compiling *c, const node *n) |
---|
1711 | n/a | { |
---|
1712 | n/a | /* lambdef: 'lambda' [varargslist] ':' test |
---|
1713 | n/a | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
---|
1714 | n/a | arguments_ty args; |
---|
1715 | n/a | expr_ty expression; |
---|
1716 | n/a | |
---|
1717 | n/a | if (NCH(n) == 3) { |
---|
1718 | n/a | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
---|
1719 | n/a | if (!args) |
---|
1720 | n/a | return NULL; |
---|
1721 | n/a | expression = ast_for_expr(c, CHILD(n, 2)); |
---|
1722 | n/a | if (!expression) |
---|
1723 | n/a | return NULL; |
---|
1724 | n/a | } |
---|
1725 | n/a | else { |
---|
1726 | n/a | args = ast_for_arguments(c, CHILD(n, 1)); |
---|
1727 | n/a | if (!args) |
---|
1728 | n/a | return NULL; |
---|
1729 | n/a | expression = ast_for_expr(c, CHILD(n, 3)); |
---|
1730 | n/a | if (!expression) |
---|
1731 | n/a | return NULL; |
---|
1732 | n/a | } |
---|
1733 | n/a | |
---|
1734 | n/a | return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena); |
---|
1735 | n/a | } |
---|
1736 | n/a | |
---|
1737 | n/a | static expr_ty |
---|
1738 | n/a | ast_for_ifexpr(struct compiling *c, const node *n) |
---|
1739 | n/a | { |
---|
1740 | n/a | /* test: or_test 'if' or_test 'else' test */ |
---|
1741 | n/a | expr_ty expression, body, orelse; |
---|
1742 | n/a | |
---|
1743 | n/a | assert(NCH(n) == 5); |
---|
1744 | n/a | body = ast_for_expr(c, CHILD(n, 0)); |
---|
1745 | n/a | if (!body) |
---|
1746 | n/a | return NULL; |
---|
1747 | n/a | expression = ast_for_expr(c, CHILD(n, 2)); |
---|
1748 | n/a | if (!expression) |
---|
1749 | n/a | return NULL; |
---|
1750 | n/a | orelse = ast_for_expr(c, CHILD(n, 4)); |
---|
1751 | n/a | if (!orelse) |
---|
1752 | n/a | return NULL; |
---|
1753 | n/a | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
---|
1754 | n/a | c->c_arena); |
---|
1755 | n/a | } |
---|
1756 | n/a | |
---|
1757 | n/a | /* |
---|
1758 | n/a | Count the number of 'for' loops in a comprehension. |
---|
1759 | n/a | |
---|
1760 | n/a | Helper for ast_for_comprehension(). |
---|
1761 | n/a | */ |
---|
1762 | n/a | |
---|
1763 | n/a | static int |
---|
1764 | n/a | count_comp_fors(struct compiling *c, const node *n) |
---|
1765 | n/a | { |
---|
1766 | n/a | int n_fors = 0; |
---|
1767 | n/a | int is_async; |
---|
1768 | n/a | |
---|
1769 | n/a | count_comp_for: |
---|
1770 | n/a | is_async = 0; |
---|
1771 | n/a | n_fors++; |
---|
1772 | n/a | REQ(n, comp_for); |
---|
1773 | n/a | if (TYPE(CHILD(n, 0)) == ASYNC) { |
---|
1774 | n/a | is_async = 1; |
---|
1775 | n/a | } |
---|
1776 | n/a | if (NCH(n) == (5 + is_async)) { |
---|
1777 | n/a | n = CHILD(n, 4 + is_async); |
---|
1778 | n/a | } |
---|
1779 | n/a | else { |
---|
1780 | n/a | return n_fors; |
---|
1781 | n/a | } |
---|
1782 | n/a | count_comp_iter: |
---|
1783 | n/a | REQ(n, comp_iter); |
---|
1784 | n/a | n = CHILD(n, 0); |
---|
1785 | n/a | if (TYPE(n) == comp_for) |
---|
1786 | n/a | goto count_comp_for; |
---|
1787 | n/a | else if (TYPE(n) == comp_if) { |
---|
1788 | n/a | if (NCH(n) == 3) { |
---|
1789 | n/a | n = CHILD(n, 2); |
---|
1790 | n/a | goto count_comp_iter; |
---|
1791 | n/a | } |
---|
1792 | n/a | else |
---|
1793 | n/a | return n_fors; |
---|
1794 | n/a | } |
---|
1795 | n/a | |
---|
1796 | n/a | /* Should never be reached */ |
---|
1797 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1798 | n/a | "logic error in count_comp_fors"); |
---|
1799 | n/a | return -1; |
---|
1800 | n/a | } |
---|
1801 | n/a | |
---|
1802 | n/a | /* Count the number of 'if' statements in a comprehension. |
---|
1803 | n/a | |
---|
1804 | n/a | Helper for ast_for_comprehension(). |
---|
1805 | n/a | */ |
---|
1806 | n/a | |
---|
1807 | n/a | static int |
---|
1808 | n/a | count_comp_ifs(struct compiling *c, const node *n) |
---|
1809 | n/a | { |
---|
1810 | n/a | int n_ifs = 0; |
---|
1811 | n/a | |
---|
1812 | n/a | while (1) { |
---|
1813 | n/a | REQ(n, comp_iter); |
---|
1814 | n/a | if (TYPE(CHILD(n, 0)) == comp_for) |
---|
1815 | n/a | return n_ifs; |
---|
1816 | n/a | n = CHILD(n, 0); |
---|
1817 | n/a | REQ(n, comp_if); |
---|
1818 | n/a | n_ifs++; |
---|
1819 | n/a | if (NCH(n) == 2) |
---|
1820 | n/a | return n_ifs; |
---|
1821 | n/a | n = CHILD(n, 2); |
---|
1822 | n/a | } |
---|
1823 | n/a | } |
---|
1824 | n/a | |
---|
1825 | n/a | static asdl_seq * |
---|
1826 | n/a | ast_for_comprehension(struct compiling *c, const node *n) |
---|
1827 | n/a | { |
---|
1828 | n/a | int i, n_fors; |
---|
1829 | n/a | asdl_seq *comps; |
---|
1830 | n/a | |
---|
1831 | n/a | n_fors = count_comp_fors(c, n); |
---|
1832 | n/a | if (n_fors == -1) |
---|
1833 | n/a | return NULL; |
---|
1834 | n/a | |
---|
1835 | n/a | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
---|
1836 | n/a | if (!comps) |
---|
1837 | n/a | return NULL; |
---|
1838 | n/a | |
---|
1839 | n/a | for (i = 0; i < n_fors; i++) { |
---|
1840 | n/a | comprehension_ty comp; |
---|
1841 | n/a | asdl_seq *t; |
---|
1842 | n/a | expr_ty expression, first; |
---|
1843 | n/a | node *for_ch; |
---|
1844 | n/a | int is_async = 0; |
---|
1845 | n/a | |
---|
1846 | n/a | REQ(n, comp_for); |
---|
1847 | n/a | |
---|
1848 | n/a | if (TYPE(CHILD(n, 0)) == ASYNC) { |
---|
1849 | n/a | is_async = 1; |
---|
1850 | n/a | } |
---|
1851 | n/a | |
---|
1852 | n/a | for_ch = CHILD(n, 1 + is_async); |
---|
1853 | n/a | t = ast_for_exprlist(c, for_ch, Store); |
---|
1854 | n/a | if (!t) |
---|
1855 | n/a | return NULL; |
---|
1856 | n/a | expression = ast_for_expr(c, CHILD(n, 3 + is_async)); |
---|
1857 | n/a | if (!expression) |
---|
1858 | n/a | return NULL; |
---|
1859 | n/a | |
---|
1860 | n/a | /* Check the # of children rather than the length of t, since |
---|
1861 | n/a | (x for x, in ...) has 1 element in t, but still requires a Tuple. */ |
---|
1862 | n/a | first = (expr_ty)asdl_seq_GET(t, 0); |
---|
1863 | n/a | if (NCH(for_ch) == 1) |
---|
1864 | n/a | comp = comprehension(first, expression, NULL, |
---|
1865 | n/a | is_async, c->c_arena); |
---|
1866 | n/a | else |
---|
1867 | n/a | comp = comprehension(Tuple(t, Store, first->lineno, |
---|
1868 | n/a | first->col_offset, c->c_arena), |
---|
1869 | n/a | expression, NULL, is_async, c->c_arena); |
---|
1870 | n/a | if (!comp) |
---|
1871 | n/a | return NULL; |
---|
1872 | n/a | |
---|
1873 | n/a | if (NCH(n) == (5 + is_async)) { |
---|
1874 | n/a | int j, n_ifs; |
---|
1875 | n/a | asdl_seq *ifs; |
---|
1876 | n/a | |
---|
1877 | n/a | n = CHILD(n, 4 + is_async); |
---|
1878 | n/a | n_ifs = count_comp_ifs(c, n); |
---|
1879 | n/a | if (n_ifs == -1) |
---|
1880 | n/a | return NULL; |
---|
1881 | n/a | |
---|
1882 | n/a | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
---|
1883 | n/a | if (!ifs) |
---|
1884 | n/a | return NULL; |
---|
1885 | n/a | |
---|
1886 | n/a | for (j = 0; j < n_ifs; j++) { |
---|
1887 | n/a | REQ(n, comp_iter); |
---|
1888 | n/a | n = CHILD(n, 0); |
---|
1889 | n/a | REQ(n, comp_if); |
---|
1890 | n/a | |
---|
1891 | n/a | expression = ast_for_expr(c, CHILD(n, 1)); |
---|
1892 | n/a | if (!expression) |
---|
1893 | n/a | return NULL; |
---|
1894 | n/a | asdl_seq_SET(ifs, j, expression); |
---|
1895 | n/a | if (NCH(n) == 3) |
---|
1896 | n/a | n = CHILD(n, 2); |
---|
1897 | n/a | } |
---|
1898 | n/a | /* on exit, must guarantee that n is a comp_for */ |
---|
1899 | n/a | if (TYPE(n) == comp_iter) |
---|
1900 | n/a | n = CHILD(n, 0); |
---|
1901 | n/a | comp->ifs = ifs; |
---|
1902 | n/a | } |
---|
1903 | n/a | asdl_seq_SET(comps, i, comp); |
---|
1904 | n/a | } |
---|
1905 | n/a | return comps; |
---|
1906 | n/a | } |
---|
1907 | n/a | |
---|
1908 | n/a | static expr_ty |
---|
1909 | n/a | ast_for_itercomp(struct compiling *c, const node *n, int type) |
---|
1910 | n/a | { |
---|
1911 | n/a | /* testlist_comp: (test|star_expr) |
---|
1912 | n/a | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
---|
1913 | n/a | expr_ty elt; |
---|
1914 | n/a | asdl_seq *comps; |
---|
1915 | n/a | node *ch; |
---|
1916 | n/a | |
---|
1917 | n/a | assert(NCH(n) > 1); |
---|
1918 | n/a | |
---|
1919 | n/a | ch = CHILD(n, 0); |
---|
1920 | n/a | elt = ast_for_expr(c, ch); |
---|
1921 | n/a | if (!elt) |
---|
1922 | n/a | return NULL; |
---|
1923 | n/a | if (elt->kind == Starred_kind) { |
---|
1924 | n/a | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
---|
1925 | n/a | return NULL; |
---|
1926 | n/a | } |
---|
1927 | n/a | |
---|
1928 | n/a | comps = ast_for_comprehension(c, CHILD(n, 1)); |
---|
1929 | n/a | if (!comps) |
---|
1930 | n/a | return NULL; |
---|
1931 | n/a | |
---|
1932 | n/a | if (type == COMP_GENEXP) |
---|
1933 | n/a | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
---|
1934 | n/a | else if (type == COMP_LISTCOMP) |
---|
1935 | n/a | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
---|
1936 | n/a | else if (type == COMP_SETCOMP) |
---|
1937 | n/a | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
---|
1938 | n/a | else |
---|
1939 | n/a | /* Should never happen */ |
---|
1940 | n/a | return NULL; |
---|
1941 | n/a | } |
---|
1942 | n/a | |
---|
1943 | n/a | /* Fills in the key, value pair corresponding to the dict element. In case |
---|
1944 | n/a | * of an unpacking, key is NULL. *i is advanced by the number of ast |
---|
1945 | n/a | * elements. Iff successful, nonzero is returned. |
---|
1946 | n/a | */ |
---|
1947 | n/a | static int |
---|
1948 | n/a | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
---|
1949 | n/a | expr_ty *key, expr_ty *value) |
---|
1950 | n/a | { |
---|
1951 | n/a | expr_ty expression; |
---|
1952 | n/a | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
---|
1953 | n/a | assert(NCH(n) - *i >= 2); |
---|
1954 | n/a | |
---|
1955 | n/a | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
---|
1956 | n/a | if (!expression) |
---|
1957 | n/a | return 0; |
---|
1958 | n/a | *key = NULL; |
---|
1959 | n/a | *value = expression; |
---|
1960 | n/a | |
---|
1961 | n/a | *i += 2; |
---|
1962 | n/a | } |
---|
1963 | n/a | else { |
---|
1964 | n/a | assert(NCH(n) - *i >= 3); |
---|
1965 | n/a | |
---|
1966 | n/a | expression = ast_for_expr(c, CHILD(n, *i)); |
---|
1967 | n/a | if (!expression) |
---|
1968 | n/a | return 0; |
---|
1969 | n/a | *key = expression; |
---|
1970 | n/a | |
---|
1971 | n/a | REQ(CHILD(n, *i + 1), COLON); |
---|
1972 | n/a | |
---|
1973 | n/a | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
---|
1974 | n/a | if (!expression) |
---|
1975 | n/a | return 0; |
---|
1976 | n/a | *value = expression; |
---|
1977 | n/a | |
---|
1978 | n/a | *i += 3; |
---|
1979 | n/a | } |
---|
1980 | n/a | return 1; |
---|
1981 | n/a | } |
---|
1982 | n/a | |
---|
1983 | n/a | static expr_ty |
---|
1984 | n/a | ast_for_dictcomp(struct compiling *c, const node *n) |
---|
1985 | n/a | { |
---|
1986 | n/a | expr_ty key, value; |
---|
1987 | n/a | asdl_seq *comps; |
---|
1988 | n/a | int i = 0; |
---|
1989 | n/a | |
---|
1990 | n/a | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
---|
1991 | n/a | return NULL; |
---|
1992 | n/a | assert(key); |
---|
1993 | n/a | assert(NCH(n) - i >= 1); |
---|
1994 | n/a | |
---|
1995 | n/a | comps = ast_for_comprehension(c, CHILD(n, i)); |
---|
1996 | n/a | if (!comps) |
---|
1997 | n/a | return NULL; |
---|
1998 | n/a | |
---|
1999 | n/a | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2000 | n/a | } |
---|
2001 | n/a | |
---|
2002 | n/a | static expr_ty |
---|
2003 | n/a | ast_for_dictdisplay(struct compiling *c, const node *n) |
---|
2004 | n/a | { |
---|
2005 | n/a | int i; |
---|
2006 | n/a | int j; |
---|
2007 | n/a | int size; |
---|
2008 | n/a | asdl_seq *keys, *values; |
---|
2009 | n/a | |
---|
2010 | n/a | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
---|
2011 | n/a | keys = _Py_asdl_seq_new(size, c->c_arena); |
---|
2012 | n/a | if (!keys) |
---|
2013 | n/a | return NULL; |
---|
2014 | n/a | |
---|
2015 | n/a | values = _Py_asdl_seq_new(size, c->c_arena); |
---|
2016 | n/a | if (!values) |
---|
2017 | n/a | return NULL; |
---|
2018 | n/a | |
---|
2019 | n/a | j = 0; |
---|
2020 | n/a | for (i = 0; i < NCH(n); i++) { |
---|
2021 | n/a | expr_ty key, value; |
---|
2022 | n/a | |
---|
2023 | n/a | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
---|
2024 | n/a | return NULL; |
---|
2025 | n/a | asdl_seq_SET(keys, j, key); |
---|
2026 | n/a | asdl_seq_SET(values, j, value); |
---|
2027 | n/a | |
---|
2028 | n/a | j++; |
---|
2029 | n/a | } |
---|
2030 | n/a | keys->size = j; |
---|
2031 | n/a | values->size = j; |
---|
2032 | n/a | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2033 | n/a | } |
---|
2034 | n/a | |
---|
2035 | n/a | static expr_ty |
---|
2036 | n/a | ast_for_genexp(struct compiling *c, const node *n) |
---|
2037 | n/a | { |
---|
2038 | n/a | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
---|
2039 | n/a | return ast_for_itercomp(c, n, COMP_GENEXP); |
---|
2040 | n/a | } |
---|
2041 | n/a | |
---|
2042 | n/a | static expr_ty |
---|
2043 | n/a | ast_for_listcomp(struct compiling *c, const node *n) |
---|
2044 | n/a | { |
---|
2045 | n/a | assert(TYPE(n) == (testlist_comp)); |
---|
2046 | n/a | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
---|
2047 | n/a | } |
---|
2048 | n/a | |
---|
2049 | n/a | static expr_ty |
---|
2050 | n/a | ast_for_setcomp(struct compiling *c, const node *n) |
---|
2051 | n/a | { |
---|
2052 | n/a | assert(TYPE(n) == (dictorsetmaker)); |
---|
2053 | n/a | return ast_for_itercomp(c, n, COMP_SETCOMP); |
---|
2054 | n/a | } |
---|
2055 | n/a | |
---|
2056 | n/a | static expr_ty |
---|
2057 | n/a | ast_for_setdisplay(struct compiling *c, const node *n) |
---|
2058 | n/a | { |
---|
2059 | n/a | int i; |
---|
2060 | n/a | int size; |
---|
2061 | n/a | asdl_seq *elts; |
---|
2062 | n/a | |
---|
2063 | n/a | assert(TYPE(n) == (dictorsetmaker)); |
---|
2064 | n/a | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
---|
2065 | n/a | elts = _Py_asdl_seq_new(size, c->c_arena); |
---|
2066 | n/a | if (!elts) |
---|
2067 | n/a | return NULL; |
---|
2068 | n/a | for (i = 0; i < NCH(n); i += 2) { |
---|
2069 | n/a | expr_ty expression; |
---|
2070 | n/a | expression = ast_for_expr(c, CHILD(n, i)); |
---|
2071 | n/a | if (!expression) |
---|
2072 | n/a | return NULL; |
---|
2073 | n/a | asdl_seq_SET(elts, i / 2, expression); |
---|
2074 | n/a | } |
---|
2075 | n/a | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2076 | n/a | } |
---|
2077 | n/a | |
---|
2078 | n/a | static expr_ty |
---|
2079 | n/a | ast_for_atom(struct compiling *c, const node *n) |
---|
2080 | n/a | { |
---|
2081 | n/a | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
---|
2082 | n/a | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
---|
2083 | n/a | | '...' | 'None' | 'True' | 'False' |
---|
2084 | n/a | */ |
---|
2085 | n/a | node *ch = CHILD(n, 0); |
---|
2086 | n/a | |
---|
2087 | n/a | switch (TYPE(ch)) { |
---|
2088 | n/a | case NAME: { |
---|
2089 | n/a | PyObject *name; |
---|
2090 | n/a | const char *s = STR(ch); |
---|
2091 | n/a | size_t len = strlen(s); |
---|
2092 | n/a | if (len >= 4 && len <= 5) { |
---|
2093 | n/a | if (!strcmp(s, "None")) |
---|
2094 | n/a | return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2095 | n/a | if (!strcmp(s, "True")) |
---|
2096 | n/a | return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2097 | n/a | if (!strcmp(s, "False")) |
---|
2098 | n/a | return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2099 | n/a | } |
---|
2100 | n/a | name = new_identifier(s, c); |
---|
2101 | n/a | if (!name) |
---|
2102 | n/a | return NULL; |
---|
2103 | n/a | /* All names start in Load context, but may later be changed. */ |
---|
2104 | n/a | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2105 | n/a | } |
---|
2106 | n/a | case STRING: { |
---|
2107 | n/a | expr_ty str = parsestrplus(c, n); |
---|
2108 | n/a | if (!str) { |
---|
2109 | n/a | const char *errtype = NULL; |
---|
2110 | n/a | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
---|
2111 | n/a | errtype = "unicode error"; |
---|
2112 | n/a | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
---|
2113 | n/a | errtype = "value error"; |
---|
2114 | n/a | if (errtype) { |
---|
2115 | n/a | char buf[128]; |
---|
2116 | n/a | const char *s = NULL; |
---|
2117 | n/a | PyObject *type, *value, *tback, *errstr; |
---|
2118 | n/a | PyErr_Fetch(&type, &value, &tback); |
---|
2119 | n/a | errstr = PyObject_Str(value); |
---|
2120 | n/a | if (errstr) |
---|
2121 | n/a | s = PyUnicode_AsUTF8(errstr); |
---|
2122 | n/a | if (s) { |
---|
2123 | n/a | PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s); |
---|
2124 | n/a | } else { |
---|
2125 | n/a | PyErr_Clear(); |
---|
2126 | n/a | PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype); |
---|
2127 | n/a | } |
---|
2128 | n/a | Py_XDECREF(errstr); |
---|
2129 | n/a | ast_error(c, n, buf); |
---|
2130 | n/a | Py_DECREF(type); |
---|
2131 | n/a | Py_XDECREF(value); |
---|
2132 | n/a | Py_XDECREF(tback); |
---|
2133 | n/a | } |
---|
2134 | n/a | return NULL; |
---|
2135 | n/a | } |
---|
2136 | n/a | return str; |
---|
2137 | n/a | } |
---|
2138 | n/a | case NUMBER: { |
---|
2139 | n/a | PyObject *pynum = parsenumber(c, STR(ch)); |
---|
2140 | n/a | if (!pynum) |
---|
2141 | n/a | return NULL; |
---|
2142 | n/a | |
---|
2143 | n/a | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
---|
2144 | n/a | Py_DECREF(pynum); |
---|
2145 | n/a | return NULL; |
---|
2146 | n/a | } |
---|
2147 | n/a | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2148 | n/a | } |
---|
2149 | n/a | case ELLIPSIS: /* Ellipsis */ |
---|
2150 | n/a | return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
---|
2151 | n/a | case LPAR: /* some parenthesized expressions */ |
---|
2152 | n/a | ch = CHILD(n, 1); |
---|
2153 | n/a | |
---|
2154 | n/a | if (TYPE(ch) == RPAR) |
---|
2155 | n/a | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2156 | n/a | |
---|
2157 | n/a | if (TYPE(ch) == yield_expr) |
---|
2158 | n/a | return ast_for_expr(c, ch); |
---|
2159 | n/a | |
---|
2160 | n/a | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
---|
2161 | n/a | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
---|
2162 | n/a | return ast_for_genexp(c, ch); |
---|
2163 | n/a | |
---|
2164 | n/a | return ast_for_testlist(c, ch); |
---|
2165 | n/a | case LSQB: /* list (or list comprehension) */ |
---|
2166 | n/a | ch = CHILD(n, 1); |
---|
2167 | n/a | |
---|
2168 | n/a | if (TYPE(ch) == RSQB) |
---|
2169 | n/a | return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2170 | n/a | |
---|
2171 | n/a | REQ(ch, testlist_comp); |
---|
2172 | n/a | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
---|
2173 | n/a | asdl_seq *elts = seq_for_testlist(c, ch); |
---|
2174 | n/a | if (!elts) |
---|
2175 | n/a | return NULL; |
---|
2176 | n/a | |
---|
2177 | n/a | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2178 | n/a | } |
---|
2179 | n/a | else |
---|
2180 | n/a | return ast_for_listcomp(c, ch); |
---|
2181 | n/a | case LBRACE: { |
---|
2182 | n/a | /* dictorsetmaker: ( ((test ':' test | '**' test) |
---|
2183 | n/a | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
---|
2184 | n/a | * ((test | '*' test) |
---|
2185 | n/a | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
---|
2186 | n/a | expr_ty res; |
---|
2187 | n/a | ch = CHILD(n, 1); |
---|
2188 | n/a | if (TYPE(ch) == RBRACE) { |
---|
2189 | n/a | /* It's an empty dict. */ |
---|
2190 | n/a | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2191 | n/a | } |
---|
2192 | n/a | else { |
---|
2193 | n/a | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
---|
2194 | n/a | if (NCH(ch) == 1 || |
---|
2195 | n/a | (NCH(ch) > 1 && |
---|
2196 | n/a | TYPE(CHILD(ch, 1)) == COMMA)) { |
---|
2197 | n/a | /* It's a set display. */ |
---|
2198 | n/a | res = ast_for_setdisplay(c, ch); |
---|
2199 | n/a | } |
---|
2200 | n/a | else if (NCH(ch) > 1 && |
---|
2201 | n/a | TYPE(CHILD(ch, 1)) == comp_for) { |
---|
2202 | n/a | /* It's a set comprehension. */ |
---|
2203 | n/a | res = ast_for_setcomp(c, ch); |
---|
2204 | n/a | } |
---|
2205 | n/a | else if (NCH(ch) > 3 - is_dict && |
---|
2206 | n/a | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
---|
2207 | n/a | /* It's a dictionary comprehension. */ |
---|
2208 | n/a | if (is_dict) { |
---|
2209 | n/a | ast_error(c, n, "dict unpacking cannot be used in " |
---|
2210 | n/a | "dict comprehension"); |
---|
2211 | n/a | return NULL; |
---|
2212 | n/a | } |
---|
2213 | n/a | res = ast_for_dictcomp(c, ch); |
---|
2214 | n/a | } |
---|
2215 | n/a | else { |
---|
2216 | n/a | /* It's a dictionary display. */ |
---|
2217 | n/a | res = ast_for_dictdisplay(c, ch); |
---|
2218 | n/a | } |
---|
2219 | n/a | if (res) { |
---|
2220 | n/a | res->lineno = LINENO(n); |
---|
2221 | n/a | res->col_offset = n->n_col_offset; |
---|
2222 | n/a | } |
---|
2223 | n/a | return res; |
---|
2224 | n/a | } |
---|
2225 | n/a | } |
---|
2226 | n/a | default: |
---|
2227 | n/a | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
---|
2228 | n/a | return NULL; |
---|
2229 | n/a | } |
---|
2230 | n/a | } |
---|
2231 | n/a | |
---|
2232 | n/a | static slice_ty |
---|
2233 | n/a | ast_for_slice(struct compiling *c, const node *n) |
---|
2234 | n/a | { |
---|
2235 | n/a | node *ch; |
---|
2236 | n/a | expr_ty lower = NULL, upper = NULL, step = NULL; |
---|
2237 | n/a | |
---|
2238 | n/a | REQ(n, subscript); |
---|
2239 | n/a | |
---|
2240 | n/a | /* |
---|
2241 | n/a | subscript: test | [test] ':' [test] [sliceop] |
---|
2242 | n/a | sliceop: ':' [test] |
---|
2243 | n/a | */ |
---|
2244 | n/a | ch = CHILD(n, 0); |
---|
2245 | n/a | if (NCH(n) == 1 && TYPE(ch) == test) { |
---|
2246 | n/a | /* 'step' variable hold no significance in terms of being used over |
---|
2247 | n/a | other vars */ |
---|
2248 | n/a | step = ast_for_expr(c, ch); |
---|
2249 | n/a | if (!step) |
---|
2250 | n/a | return NULL; |
---|
2251 | n/a | |
---|
2252 | n/a | return Index(step, c->c_arena); |
---|
2253 | n/a | } |
---|
2254 | n/a | |
---|
2255 | n/a | if (TYPE(ch) == test) { |
---|
2256 | n/a | lower = ast_for_expr(c, ch); |
---|
2257 | n/a | if (!lower) |
---|
2258 | n/a | return NULL; |
---|
2259 | n/a | } |
---|
2260 | n/a | |
---|
2261 | n/a | /* If there's an upper bound it's in the second or third position. */ |
---|
2262 | n/a | if (TYPE(ch) == COLON) { |
---|
2263 | n/a | if (NCH(n) > 1) { |
---|
2264 | n/a | node *n2 = CHILD(n, 1); |
---|
2265 | n/a | |
---|
2266 | n/a | if (TYPE(n2) == test) { |
---|
2267 | n/a | upper = ast_for_expr(c, n2); |
---|
2268 | n/a | if (!upper) |
---|
2269 | n/a | return NULL; |
---|
2270 | n/a | } |
---|
2271 | n/a | } |
---|
2272 | n/a | } else if (NCH(n) > 2) { |
---|
2273 | n/a | node *n2 = CHILD(n, 2); |
---|
2274 | n/a | |
---|
2275 | n/a | if (TYPE(n2) == test) { |
---|
2276 | n/a | upper = ast_for_expr(c, n2); |
---|
2277 | n/a | if (!upper) |
---|
2278 | n/a | return NULL; |
---|
2279 | n/a | } |
---|
2280 | n/a | } |
---|
2281 | n/a | |
---|
2282 | n/a | ch = CHILD(n, NCH(n) - 1); |
---|
2283 | n/a | if (TYPE(ch) == sliceop) { |
---|
2284 | n/a | if (NCH(ch) != 1) { |
---|
2285 | n/a | ch = CHILD(ch, 1); |
---|
2286 | n/a | if (TYPE(ch) == test) { |
---|
2287 | n/a | step = ast_for_expr(c, ch); |
---|
2288 | n/a | if (!step) |
---|
2289 | n/a | return NULL; |
---|
2290 | n/a | } |
---|
2291 | n/a | } |
---|
2292 | n/a | } |
---|
2293 | n/a | |
---|
2294 | n/a | return Slice(lower, upper, step, c->c_arena); |
---|
2295 | n/a | } |
---|
2296 | n/a | |
---|
2297 | n/a | static expr_ty |
---|
2298 | n/a | ast_for_binop(struct compiling *c, const node *n) |
---|
2299 | n/a | { |
---|
2300 | n/a | /* Must account for a sequence of expressions. |
---|
2301 | n/a | How should A op B op C by represented? |
---|
2302 | n/a | BinOp(BinOp(A, op, B), op, C). |
---|
2303 | n/a | */ |
---|
2304 | n/a | |
---|
2305 | n/a | int i, nops; |
---|
2306 | n/a | expr_ty expr1, expr2, result; |
---|
2307 | n/a | operator_ty newoperator; |
---|
2308 | n/a | |
---|
2309 | n/a | expr1 = ast_for_expr(c, CHILD(n, 0)); |
---|
2310 | n/a | if (!expr1) |
---|
2311 | n/a | return NULL; |
---|
2312 | n/a | |
---|
2313 | n/a | expr2 = ast_for_expr(c, CHILD(n, 2)); |
---|
2314 | n/a | if (!expr2) |
---|
2315 | n/a | return NULL; |
---|
2316 | n/a | |
---|
2317 | n/a | newoperator = get_operator(CHILD(n, 1)); |
---|
2318 | n/a | if (!newoperator) |
---|
2319 | n/a | return NULL; |
---|
2320 | n/a | |
---|
2321 | n/a | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
---|
2322 | n/a | c->c_arena); |
---|
2323 | n/a | if (!result) |
---|
2324 | n/a | return NULL; |
---|
2325 | n/a | |
---|
2326 | n/a | nops = (NCH(n) - 1) / 2; |
---|
2327 | n/a | for (i = 1; i < nops; i++) { |
---|
2328 | n/a | expr_ty tmp_result, tmp; |
---|
2329 | n/a | const node* next_oper = CHILD(n, i * 2 + 1); |
---|
2330 | n/a | |
---|
2331 | n/a | newoperator = get_operator(next_oper); |
---|
2332 | n/a | if (!newoperator) |
---|
2333 | n/a | return NULL; |
---|
2334 | n/a | |
---|
2335 | n/a | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
---|
2336 | n/a | if (!tmp) |
---|
2337 | n/a | return NULL; |
---|
2338 | n/a | |
---|
2339 | n/a | tmp_result = BinOp(result, newoperator, tmp, |
---|
2340 | n/a | LINENO(next_oper), next_oper->n_col_offset, |
---|
2341 | n/a | c->c_arena); |
---|
2342 | n/a | if (!tmp_result) |
---|
2343 | n/a | return NULL; |
---|
2344 | n/a | result = tmp_result; |
---|
2345 | n/a | } |
---|
2346 | n/a | return result; |
---|
2347 | n/a | } |
---|
2348 | n/a | |
---|
2349 | n/a | static expr_ty |
---|
2350 | n/a | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
---|
2351 | n/a | { |
---|
2352 | n/a | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
---|
2353 | n/a | subscriptlist: subscript (',' subscript)* [','] |
---|
2354 | n/a | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
---|
2355 | n/a | */ |
---|
2356 | n/a | REQ(n, trailer); |
---|
2357 | n/a | if (TYPE(CHILD(n, 0)) == LPAR) { |
---|
2358 | n/a | if (NCH(n) == 2) |
---|
2359 | n/a | return Call(left_expr, NULL, NULL, LINENO(n), |
---|
2360 | n/a | n->n_col_offset, c->c_arena); |
---|
2361 | n/a | else |
---|
2362 | n/a | return ast_for_call(c, CHILD(n, 1), left_expr); |
---|
2363 | n/a | } |
---|
2364 | n/a | else if (TYPE(CHILD(n, 0)) == DOT) { |
---|
2365 | n/a | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
---|
2366 | n/a | if (!attr_id) |
---|
2367 | n/a | return NULL; |
---|
2368 | n/a | return Attribute(left_expr, attr_id, Load, |
---|
2369 | n/a | LINENO(n), n->n_col_offset, c->c_arena); |
---|
2370 | n/a | } |
---|
2371 | n/a | else { |
---|
2372 | n/a | REQ(CHILD(n, 0), LSQB); |
---|
2373 | n/a | REQ(CHILD(n, 2), RSQB); |
---|
2374 | n/a | n = CHILD(n, 1); |
---|
2375 | n/a | if (NCH(n) == 1) { |
---|
2376 | n/a | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
---|
2377 | n/a | if (!slc) |
---|
2378 | n/a | return NULL; |
---|
2379 | n/a | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
---|
2380 | n/a | c->c_arena); |
---|
2381 | n/a | } |
---|
2382 | n/a | else { |
---|
2383 | n/a | /* The grammar is ambiguous here. The ambiguity is resolved |
---|
2384 | n/a | by treating the sequence as a tuple literal if there are |
---|
2385 | n/a | no slice features. |
---|
2386 | n/a | */ |
---|
2387 | n/a | int j; |
---|
2388 | n/a | slice_ty slc; |
---|
2389 | n/a | expr_ty e; |
---|
2390 | n/a | int simple = 1; |
---|
2391 | n/a | asdl_seq *slices, *elts; |
---|
2392 | n/a | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
---|
2393 | n/a | if (!slices) |
---|
2394 | n/a | return NULL; |
---|
2395 | n/a | for (j = 0; j < NCH(n); j += 2) { |
---|
2396 | n/a | slc = ast_for_slice(c, CHILD(n, j)); |
---|
2397 | n/a | if (!slc) |
---|
2398 | n/a | return NULL; |
---|
2399 | n/a | if (slc->kind != Index_kind) |
---|
2400 | n/a | simple = 0; |
---|
2401 | n/a | asdl_seq_SET(slices, j / 2, slc); |
---|
2402 | n/a | } |
---|
2403 | n/a | if (!simple) { |
---|
2404 | n/a | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
---|
2405 | n/a | Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2406 | n/a | } |
---|
2407 | n/a | /* extract Index values and put them in a Tuple */ |
---|
2408 | n/a | elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena); |
---|
2409 | n/a | if (!elts) |
---|
2410 | n/a | return NULL; |
---|
2411 | n/a | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
---|
2412 | n/a | slc = (slice_ty)asdl_seq_GET(slices, j); |
---|
2413 | n/a | assert(slc->kind == Index_kind && slc->v.Index.value); |
---|
2414 | n/a | asdl_seq_SET(elts, j, slc->v.Index.value); |
---|
2415 | n/a | } |
---|
2416 | n/a | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2417 | n/a | if (!e) |
---|
2418 | n/a | return NULL; |
---|
2419 | n/a | return Subscript(left_expr, Index(e, c->c_arena), |
---|
2420 | n/a | Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2421 | n/a | } |
---|
2422 | n/a | } |
---|
2423 | n/a | } |
---|
2424 | n/a | |
---|
2425 | n/a | static expr_ty |
---|
2426 | n/a | ast_for_factor(struct compiling *c, const node *n) |
---|
2427 | n/a | { |
---|
2428 | n/a | expr_ty expression; |
---|
2429 | n/a | |
---|
2430 | n/a | expression = ast_for_expr(c, CHILD(n, 1)); |
---|
2431 | n/a | if (!expression) |
---|
2432 | n/a | return NULL; |
---|
2433 | n/a | |
---|
2434 | n/a | switch (TYPE(CHILD(n, 0))) { |
---|
2435 | n/a | case PLUS: |
---|
2436 | n/a | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
---|
2437 | n/a | c->c_arena); |
---|
2438 | n/a | case MINUS: |
---|
2439 | n/a | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
---|
2440 | n/a | c->c_arena); |
---|
2441 | n/a | case TILDE: |
---|
2442 | n/a | return UnaryOp(Invert, expression, LINENO(n), |
---|
2443 | n/a | n->n_col_offset, c->c_arena); |
---|
2444 | n/a | } |
---|
2445 | n/a | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
---|
2446 | n/a | TYPE(CHILD(n, 0))); |
---|
2447 | n/a | return NULL; |
---|
2448 | n/a | } |
---|
2449 | n/a | |
---|
2450 | n/a | static expr_ty |
---|
2451 | n/a | ast_for_atom_expr(struct compiling *c, const node *n) |
---|
2452 | n/a | { |
---|
2453 | n/a | int i, nch, start = 0; |
---|
2454 | n/a | expr_ty e, tmp; |
---|
2455 | n/a | |
---|
2456 | n/a | REQ(n, atom_expr); |
---|
2457 | n/a | nch = NCH(n); |
---|
2458 | n/a | |
---|
2459 | n/a | if (TYPE(CHILD(n, 0)) == AWAIT) { |
---|
2460 | n/a | start = 1; |
---|
2461 | n/a | assert(nch > 1); |
---|
2462 | n/a | } |
---|
2463 | n/a | |
---|
2464 | n/a | e = ast_for_atom(c, CHILD(n, start)); |
---|
2465 | n/a | if (!e) |
---|
2466 | n/a | return NULL; |
---|
2467 | n/a | if (nch == 1) |
---|
2468 | n/a | return e; |
---|
2469 | n/a | if (start && nch == 2) { |
---|
2470 | n/a | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2471 | n/a | } |
---|
2472 | n/a | |
---|
2473 | n/a | for (i = start + 1; i < nch; i++) { |
---|
2474 | n/a | node *ch = CHILD(n, i); |
---|
2475 | n/a | if (TYPE(ch) != trailer) |
---|
2476 | n/a | break; |
---|
2477 | n/a | tmp = ast_for_trailer(c, ch, e); |
---|
2478 | n/a | if (!tmp) |
---|
2479 | n/a | return NULL; |
---|
2480 | n/a | tmp->lineno = e->lineno; |
---|
2481 | n/a | tmp->col_offset = e->col_offset; |
---|
2482 | n/a | e = tmp; |
---|
2483 | n/a | } |
---|
2484 | n/a | |
---|
2485 | n/a | if (start) { |
---|
2486 | n/a | /* there was an AWAIT */ |
---|
2487 | n/a | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2488 | n/a | } |
---|
2489 | n/a | else { |
---|
2490 | n/a | return e; |
---|
2491 | n/a | } |
---|
2492 | n/a | } |
---|
2493 | n/a | |
---|
2494 | n/a | static expr_ty |
---|
2495 | n/a | ast_for_power(struct compiling *c, const node *n) |
---|
2496 | n/a | { |
---|
2497 | n/a | /* power: atom trailer* ('**' factor)* |
---|
2498 | n/a | */ |
---|
2499 | n/a | expr_ty e; |
---|
2500 | n/a | REQ(n, power); |
---|
2501 | n/a | e = ast_for_atom_expr(c, CHILD(n, 0)); |
---|
2502 | n/a | if (!e) |
---|
2503 | n/a | return NULL; |
---|
2504 | n/a | if (NCH(n) == 1) |
---|
2505 | n/a | return e; |
---|
2506 | n/a | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
---|
2507 | n/a | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
---|
2508 | n/a | if (!f) |
---|
2509 | n/a | return NULL; |
---|
2510 | n/a | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2511 | n/a | } |
---|
2512 | n/a | return e; |
---|
2513 | n/a | } |
---|
2514 | n/a | |
---|
2515 | n/a | static expr_ty |
---|
2516 | n/a | ast_for_starred(struct compiling *c, const node *n) |
---|
2517 | n/a | { |
---|
2518 | n/a | expr_ty tmp; |
---|
2519 | n/a | REQ(n, star_expr); |
---|
2520 | n/a | |
---|
2521 | n/a | tmp = ast_for_expr(c, CHILD(n, 1)); |
---|
2522 | n/a | if (!tmp) |
---|
2523 | n/a | return NULL; |
---|
2524 | n/a | |
---|
2525 | n/a | /* The Load context is changed later. */ |
---|
2526 | n/a | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2527 | n/a | } |
---|
2528 | n/a | |
---|
2529 | n/a | |
---|
2530 | n/a | /* Do not name a variable 'expr'! Will cause a compile error. |
---|
2531 | n/a | */ |
---|
2532 | n/a | |
---|
2533 | n/a | static expr_ty |
---|
2534 | n/a | ast_for_expr(struct compiling *c, const node *n) |
---|
2535 | n/a | { |
---|
2536 | n/a | /* handle the full range of simple expressions |
---|
2537 | n/a | test: or_test ['if' or_test 'else' test] | lambdef |
---|
2538 | n/a | test_nocond: or_test | lambdef_nocond |
---|
2539 | n/a | or_test: and_test ('or' and_test)* |
---|
2540 | n/a | and_test: not_test ('and' not_test)* |
---|
2541 | n/a | not_test: 'not' not_test | comparison |
---|
2542 | n/a | comparison: expr (comp_op expr)* |
---|
2543 | n/a | expr: xor_expr ('|' xor_expr)* |
---|
2544 | n/a | xor_expr: and_expr ('^' and_expr)* |
---|
2545 | n/a | and_expr: shift_expr ('&' shift_expr)* |
---|
2546 | n/a | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
---|
2547 | n/a | arith_expr: term (('+'|'-') term)* |
---|
2548 | n/a | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
---|
2549 | n/a | factor: ('+'|'-'|'~') factor | power |
---|
2550 | n/a | power: atom_expr ['**' factor] |
---|
2551 | n/a | atom_expr: [AWAIT] atom trailer* |
---|
2552 | n/a | yield_expr: 'yield' [yield_arg] |
---|
2553 | n/a | */ |
---|
2554 | n/a | |
---|
2555 | n/a | asdl_seq *seq; |
---|
2556 | n/a | int i; |
---|
2557 | n/a | |
---|
2558 | n/a | loop: |
---|
2559 | n/a | switch (TYPE(n)) { |
---|
2560 | n/a | case test: |
---|
2561 | n/a | case test_nocond: |
---|
2562 | n/a | if (TYPE(CHILD(n, 0)) == lambdef || |
---|
2563 | n/a | TYPE(CHILD(n, 0)) == lambdef_nocond) |
---|
2564 | n/a | return ast_for_lambdef(c, CHILD(n, 0)); |
---|
2565 | n/a | else if (NCH(n) > 1) |
---|
2566 | n/a | return ast_for_ifexpr(c, n); |
---|
2567 | n/a | /* Fallthrough */ |
---|
2568 | n/a | case or_test: |
---|
2569 | n/a | case and_test: |
---|
2570 | n/a | if (NCH(n) == 1) { |
---|
2571 | n/a | n = CHILD(n, 0); |
---|
2572 | n/a | goto loop; |
---|
2573 | n/a | } |
---|
2574 | n/a | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
---|
2575 | n/a | if (!seq) |
---|
2576 | n/a | return NULL; |
---|
2577 | n/a | for (i = 0; i < NCH(n); i += 2) { |
---|
2578 | n/a | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
---|
2579 | n/a | if (!e) |
---|
2580 | n/a | return NULL; |
---|
2581 | n/a | asdl_seq_SET(seq, i / 2, e); |
---|
2582 | n/a | } |
---|
2583 | n/a | if (!strcmp(STR(CHILD(n, 1)), "and")) |
---|
2584 | n/a | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
---|
2585 | n/a | c->c_arena); |
---|
2586 | n/a | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
---|
2587 | n/a | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2588 | n/a | case not_test: |
---|
2589 | n/a | if (NCH(n) == 1) { |
---|
2590 | n/a | n = CHILD(n, 0); |
---|
2591 | n/a | goto loop; |
---|
2592 | n/a | } |
---|
2593 | n/a | else { |
---|
2594 | n/a | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
---|
2595 | n/a | if (!expression) |
---|
2596 | n/a | return NULL; |
---|
2597 | n/a | |
---|
2598 | n/a | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
---|
2599 | n/a | c->c_arena); |
---|
2600 | n/a | } |
---|
2601 | n/a | case comparison: |
---|
2602 | n/a | if (NCH(n) == 1) { |
---|
2603 | n/a | n = CHILD(n, 0); |
---|
2604 | n/a | goto loop; |
---|
2605 | n/a | } |
---|
2606 | n/a | else { |
---|
2607 | n/a | expr_ty expression; |
---|
2608 | n/a | asdl_int_seq *ops; |
---|
2609 | n/a | asdl_seq *cmps; |
---|
2610 | n/a | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
---|
2611 | n/a | if (!ops) |
---|
2612 | n/a | return NULL; |
---|
2613 | n/a | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
---|
2614 | n/a | if (!cmps) { |
---|
2615 | n/a | return NULL; |
---|
2616 | n/a | } |
---|
2617 | n/a | for (i = 1; i < NCH(n); i += 2) { |
---|
2618 | n/a | cmpop_ty newoperator; |
---|
2619 | n/a | |
---|
2620 | n/a | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
---|
2621 | n/a | if (!newoperator) { |
---|
2622 | n/a | return NULL; |
---|
2623 | n/a | } |
---|
2624 | n/a | |
---|
2625 | n/a | expression = ast_for_expr(c, CHILD(n, i + 1)); |
---|
2626 | n/a | if (!expression) { |
---|
2627 | n/a | return NULL; |
---|
2628 | n/a | } |
---|
2629 | n/a | |
---|
2630 | n/a | asdl_seq_SET(ops, i / 2, newoperator); |
---|
2631 | n/a | asdl_seq_SET(cmps, i / 2, expression); |
---|
2632 | n/a | } |
---|
2633 | n/a | expression = ast_for_expr(c, CHILD(n, 0)); |
---|
2634 | n/a | if (!expression) { |
---|
2635 | n/a | return NULL; |
---|
2636 | n/a | } |
---|
2637 | n/a | |
---|
2638 | n/a | return Compare(expression, ops, cmps, LINENO(n), |
---|
2639 | n/a | n->n_col_offset, c->c_arena); |
---|
2640 | n/a | } |
---|
2641 | n/a | break; |
---|
2642 | n/a | |
---|
2643 | n/a | case star_expr: |
---|
2644 | n/a | return ast_for_starred(c, n); |
---|
2645 | n/a | /* The next five cases all handle BinOps. The main body of code |
---|
2646 | n/a | is the same in each case, but the switch turned inside out to |
---|
2647 | n/a | reuse the code for each type of operator. |
---|
2648 | n/a | */ |
---|
2649 | n/a | case expr: |
---|
2650 | n/a | case xor_expr: |
---|
2651 | n/a | case and_expr: |
---|
2652 | n/a | case shift_expr: |
---|
2653 | n/a | case arith_expr: |
---|
2654 | n/a | case term: |
---|
2655 | n/a | if (NCH(n) == 1) { |
---|
2656 | n/a | n = CHILD(n, 0); |
---|
2657 | n/a | goto loop; |
---|
2658 | n/a | } |
---|
2659 | n/a | return ast_for_binop(c, n); |
---|
2660 | n/a | case yield_expr: { |
---|
2661 | n/a | node *an = NULL; |
---|
2662 | n/a | node *en = NULL; |
---|
2663 | n/a | int is_from = 0; |
---|
2664 | n/a | expr_ty exp = NULL; |
---|
2665 | n/a | if (NCH(n) > 1) |
---|
2666 | n/a | an = CHILD(n, 1); /* yield_arg */ |
---|
2667 | n/a | if (an) { |
---|
2668 | n/a | en = CHILD(an, NCH(an) - 1); |
---|
2669 | n/a | if (NCH(an) == 2) { |
---|
2670 | n/a | is_from = 1; |
---|
2671 | n/a | exp = ast_for_expr(c, en); |
---|
2672 | n/a | } |
---|
2673 | n/a | else |
---|
2674 | n/a | exp = ast_for_testlist(c, en); |
---|
2675 | n/a | if (!exp) |
---|
2676 | n/a | return NULL; |
---|
2677 | n/a | } |
---|
2678 | n/a | if (is_from) |
---|
2679 | n/a | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2680 | n/a | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2681 | n/a | } |
---|
2682 | n/a | case factor: |
---|
2683 | n/a | if (NCH(n) == 1) { |
---|
2684 | n/a | n = CHILD(n, 0); |
---|
2685 | n/a | goto loop; |
---|
2686 | n/a | } |
---|
2687 | n/a | return ast_for_factor(c, n); |
---|
2688 | n/a | case power: |
---|
2689 | n/a | return ast_for_power(c, n); |
---|
2690 | n/a | default: |
---|
2691 | n/a | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
---|
2692 | n/a | return NULL; |
---|
2693 | n/a | } |
---|
2694 | n/a | /* should never get here unless if error is set */ |
---|
2695 | n/a | return NULL; |
---|
2696 | n/a | } |
---|
2697 | n/a | |
---|
2698 | n/a | static expr_ty |
---|
2699 | n/a | ast_for_call(struct compiling *c, const node *n, expr_ty func) |
---|
2700 | n/a | { |
---|
2701 | n/a | /* |
---|
2702 | n/a | arglist: argument (',' argument)* [','] |
---|
2703 | n/a | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
---|
2704 | n/a | */ |
---|
2705 | n/a | |
---|
2706 | n/a | int i, nargs, nkeywords, ngens; |
---|
2707 | n/a | int ndoublestars; |
---|
2708 | n/a | asdl_seq *args; |
---|
2709 | n/a | asdl_seq *keywords; |
---|
2710 | n/a | |
---|
2711 | n/a | REQ(n, arglist); |
---|
2712 | n/a | |
---|
2713 | n/a | nargs = 0; |
---|
2714 | n/a | nkeywords = 0; |
---|
2715 | n/a | ngens = 0; |
---|
2716 | n/a | for (i = 0; i < NCH(n); i++) { |
---|
2717 | n/a | node *ch = CHILD(n, i); |
---|
2718 | n/a | if (TYPE(ch) == argument) { |
---|
2719 | n/a | if (NCH(ch) == 1) |
---|
2720 | n/a | nargs++; |
---|
2721 | n/a | else if (TYPE(CHILD(ch, 1)) == comp_for) |
---|
2722 | n/a | ngens++; |
---|
2723 | n/a | else if (TYPE(CHILD(ch, 0)) == STAR) |
---|
2724 | n/a | nargs++; |
---|
2725 | n/a | else |
---|
2726 | n/a | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
---|
2727 | n/a | nkeywords++; |
---|
2728 | n/a | } |
---|
2729 | n/a | } |
---|
2730 | n/a | if (ngens > 1 || (ngens && (nargs || nkeywords))) { |
---|
2731 | n/a | ast_error(c, n, "Generator expression must be parenthesized " |
---|
2732 | n/a | "if not sole argument"); |
---|
2733 | n/a | return NULL; |
---|
2734 | n/a | } |
---|
2735 | n/a | |
---|
2736 | n/a | args = _Py_asdl_seq_new(nargs + ngens, c->c_arena); |
---|
2737 | n/a | if (!args) |
---|
2738 | n/a | return NULL; |
---|
2739 | n/a | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
---|
2740 | n/a | if (!keywords) |
---|
2741 | n/a | return NULL; |
---|
2742 | n/a | |
---|
2743 | n/a | nargs = 0; /* positional arguments + iterable argument unpackings */ |
---|
2744 | n/a | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
---|
2745 | n/a | ndoublestars = 0; /* just keyword argument unpackings */ |
---|
2746 | n/a | for (i = 0; i < NCH(n); i++) { |
---|
2747 | n/a | node *ch = CHILD(n, i); |
---|
2748 | n/a | if (TYPE(ch) == argument) { |
---|
2749 | n/a | expr_ty e; |
---|
2750 | n/a | node *chch = CHILD(ch, 0); |
---|
2751 | n/a | if (NCH(ch) == 1) { |
---|
2752 | n/a | /* a positional argument */ |
---|
2753 | n/a | if (nkeywords) { |
---|
2754 | n/a | if (ndoublestars) { |
---|
2755 | n/a | ast_error(c, chch, |
---|
2756 | n/a | "positional argument follows " |
---|
2757 | n/a | "keyword argument unpacking"); |
---|
2758 | n/a | } |
---|
2759 | n/a | else { |
---|
2760 | n/a | ast_error(c, chch, |
---|
2761 | n/a | "positional argument follows " |
---|
2762 | n/a | "keyword argument"); |
---|
2763 | n/a | } |
---|
2764 | n/a | return NULL; |
---|
2765 | n/a | } |
---|
2766 | n/a | e = ast_for_expr(c, chch); |
---|
2767 | n/a | if (!e) |
---|
2768 | n/a | return NULL; |
---|
2769 | n/a | asdl_seq_SET(args, nargs++, e); |
---|
2770 | n/a | } |
---|
2771 | n/a | else if (TYPE(chch) == STAR) { |
---|
2772 | n/a | /* an iterable argument unpacking */ |
---|
2773 | n/a | expr_ty starred; |
---|
2774 | n/a | if (ndoublestars) { |
---|
2775 | n/a | ast_error(c, chch, |
---|
2776 | n/a | "iterable argument unpacking follows " |
---|
2777 | n/a | "keyword argument unpacking"); |
---|
2778 | n/a | return NULL; |
---|
2779 | n/a | } |
---|
2780 | n/a | e = ast_for_expr(c, CHILD(ch, 1)); |
---|
2781 | n/a | if (!e) |
---|
2782 | n/a | return NULL; |
---|
2783 | n/a | starred = Starred(e, Load, LINENO(chch), |
---|
2784 | n/a | chch->n_col_offset, |
---|
2785 | n/a | c->c_arena); |
---|
2786 | n/a | if (!starred) |
---|
2787 | n/a | return NULL; |
---|
2788 | n/a | asdl_seq_SET(args, nargs++, starred); |
---|
2789 | n/a | |
---|
2790 | n/a | } |
---|
2791 | n/a | else if (TYPE(chch) == DOUBLESTAR) { |
---|
2792 | n/a | /* a keyword argument unpacking */ |
---|
2793 | n/a | keyword_ty kw; |
---|
2794 | n/a | i++; |
---|
2795 | n/a | e = ast_for_expr(c, CHILD(ch, 1)); |
---|
2796 | n/a | if (!e) |
---|
2797 | n/a | return NULL; |
---|
2798 | n/a | kw = keyword(NULL, e, c->c_arena); |
---|
2799 | n/a | asdl_seq_SET(keywords, nkeywords++, kw); |
---|
2800 | n/a | ndoublestars++; |
---|
2801 | n/a | } |
---|
2802 | n/a | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
---|
2803 | n/a | /* the lone generator expression */ |
---|
2804 | n/a | e = ast_for_genexp(c, ch); |
---|
2805 | n/a | if (!e) |
---|
2806 | n/a | return NULL; |
---|
2807 | n/a | asdl_seq_SET(args, nargs++, e); |
---|
2808 | n/a | } |
---|
2809 | n/a | else { |
---|
2810 | n/a | /* a keyword argument */ |
---|
2811 | n/a | keyword_ty kw; |
---|
2812 | n/a | identifier key, tmp; |
---|
2813 | n/a | int k; |
---|
2814 | n/a | |
---|
2815 | n/a | /* chch is test, but must be an identifier? */ |
---|
2816 | n/a | e = ast_for_expr(c, chch); |
---|
2817 | n/a | if (!e) |
---|
2818 | n/a | return NULL; |
---|
2819 | n/a | /* f(lambda x: x[0] = 3) ends up getting parsed with |
---|
2820 | n/a | * LHS test = lambda x: x[0], and RHS test = 3. |
---|
2821 | n/a | * SF bug 132313 points out that complaining about a keyword |
---|
2822 | n/a | * then is very confusing. |
---|
2823 | n/a | */ |
---|
2824 | n/a | if (e->kind == Lambda_kind) { |
---|
2825 | n/a | ast_error(c, chch, |
---|
2826 | n/a | "lambda cannot contain assignment"); |
---|
2827 | n/a | return NULL; |
---|
2828 | n/a | } |
---|
2829 | n/a | else if (e->kind != Name_kind) { |
---|
2830 | n/a | ast_error(c, chch, |
---|
2831 | n/a | "keyword can't be an expression"); |
---|
2832 | n/a | return NULL; |
---|
2833 | n/a | } |
---|
2834 | n/a | else if (forbidden_name(c, e->v.Name.id, ch, 1)) { |
---|
2835 | n/a | return NULL; |
---|
2836 | n/a | } |
---|
2837 | n/a | key = e->v.Name.id; |
---|
2838 | n/a | for (k = 0; k < nkeywords; k++) { |
---|
2839 | n/a | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
---|
2840 | n/a | if (tmp && !PyUnicode_Compare(tmp, key)) { |
---|
2841 | n/a | ast_error(c, chch, |
---|
2842 | n/a | "keyword argument repeated"); |
---|
2843 | n/a | return NULL; |
---|
2844 | n/a | } |
---|
2845 | n/a | } |
---|
2846 | n/a | e = ast_for_expr(c, CHILD(ch, 2)); |
---|
2847 | n/a | if (!e) |
---|
2848 | n/a | return NULL; |
---|
2849 | n/a | kw = keyword(key, e, c->c_arena); |
---|
2850 | n/a | if (!kw) |
---|
2851 | n/a | return NULL; |
---|
2852 | n/a | asdl_seq_SET(keywords, nkeywords++, kw); |
---|
2853 | n/a | } |
---|
2854 | n/a | } |
---|
2855 | n/a | } |
---|
2856 | n/a | |
---|
2857 | n/a | return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena); |
---|
2858 | n/a | } |
---|
2859 | n/a | |
---|
2860 | n/a | static expr_ty |
---|
2861 | n/a | ast_for_testlist(struct compiling *c, const node* n) |
---|
2862 | n/a | { |
---|
2863 | n/a | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
---|
2864 | n/a | /* testlist: test (',' test)* [','] */ |
---|
2865 | n/a | assert(NCH(n) > 0); |
---|
2866 | n/a | if (TYPE(n) == testlist_comp) { |
---|
2867 | n/a | if (NCH(n) > 1) |
---|
2868 | n/a | assert(TYPE(CHILD(n, 1)) != comp_for); |
---|
2869 | n/a | } |
---|
2870 | n/a | else { |
---|
2871 | n/a | assert(TYPE(n) == testlist || |
---|
2872 | n/a | TYPE(n) == testlist_star_expr); |
---|
2873 | n/a | } |
---|
2874 | n/a | if (NCH(n) == 1) |
---|
2875 | n/a | return ast_for_expr(c, CHILD(n, 0)); |
---|
2876 | n/a | else { |
---|
2877 | n/a | asdl_seq *tmp = seq_for_testlist(c, n); |
---|
2878 | n/a | if (!tmp) |
---|
2879 | n/a | return NULL; |
---|
2880 | n/a | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2881 | n/a | } |
---|
2882 | n/a | } |
---|
2883 | n/a | |
---|
2884 | n/a | static stmt_ty |
---|
2885 | n/a | ast_for_expr_stmt(struct compiling *c, const node *n) |
---|
2886 | n/a | { |
---|
2887 | n/a | REQ(n, expr_stmt); |
---|
2888 | n/a | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
---|
2889 | n/a | ('=' (yield_expr|testlist_star_expr))*) |
---|
2890 | n/a | annassign: ':' test ['=' test] |
---|
2891 | n/a | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
---|
2892 | n/a | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
---|
2893 | n/a | | '<<=' | '>>=' | '**=' | '//=' |
---|
2894 | n/a | test: ... here starts the operator precedence dance |
---|
2895 | n/a | */ |
---|
2896 | n/a | |
---|
2897 | n/a | if (NCH(n) == 1) { |
---|
2898 | n/a | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
---|
2899 | n/a | if (!e) |
---|
2900 | n/a | return NULL; |
---|
2901 | n/a | |
---|
2902 | n/a | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2903 | n/a | } |
---|
2904 | n/a | else if (TYPE(CHILD(n, 1)) == augassign) { |
---|
2905 | n/a | expr_ty expr1, expr2; |
---|
2906 | n/a | operator_ty newoperator; |
---|
2907 | n/a | node *ch = CHILD(n, 0); |
---|
2908 | n/a | |
---|
2909 | n/a | expr1 = ast_for_testlist(c, ch); |
---|
2910 | n/a | if (!expr1) |
---|
2911 | n/a | return NULL; |
---|
2912 | n/a | if(!set_context(c, expr1, Store, ch)) |
---|
2913 | n/a | return NULL; |
---|
2914 | n/a | /* set_context checks that most expressions are not the left side. |
---|
2915 | n/a | Augmented assignments can only have a name, a subscript, or an |
---|
2916 | n/a | attribute on the left, though, so we have to explicitly check for |
---|
2917 | n/a | those. */ |
---|
2918 | n/a | switch (expr1->kind) { |
---|
2919 | n/a | case Name_kind: |
---|
2920 | n/a | case Attribute_kind: |
---|
2921 | n/a | case Subscript_kind: |
---|
2922 | n/a | break; |
---|
2923 | n/a | default: |
---|
2924 | n/a | ast_error(c, ch, "illegal expression for augmented assignment"); |
---|
2925 | n/a | return NULL; |
---|
2926 | n/a | } |
---|
2927 | n/a | |
---|
2928 | n/a | ch = CHILD(n, 2); |
---|
2929 | n/a | if (TYPE(ch) == testlist) |
---|
2930 | n/a | expr2 = ast_for_testlist(c, ch); |
---|
2931 | n/a | else |
---|
2932 | n/a | expr2 = ast_for_expr(c, ch); |
---|
2933 | n/a | if (!expr2) |
---|
2934 | n/a | return NULL; |
---|
2935 | n/a | |
---|
2936 | n/a | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
---|
2937 | n/a | if (!newoperator) |
---|
2938 | n/a | return NULL; |
---|
2939 | n/a | |
---|
2940 | n/a | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
---|
2941 | n/a | } |
---|
2942 | n/a | else if (TYPE(CHILD(n, 1)) == annassign) { |
---|
2943 | n/a | expr_ty expr1, expr2, expr3; |
---|
2944 | n/a | node *ch = CHILD(n, 0); |
---|
2945 | n/a | node *deep, *ann = CHILD(n, 1); |
---|
2946 | n/a | int simple = 1; |
---|
2947 | n/a | |
---|
2948 | n/a | /* we keep track of parens to qualify (x) as expression not name */ |
---|
2949 | n/a | deep = ch; |
---|
2950 | n/a | while (NCH(deep) == 1) { |
---|
2951 | n/a | deep = CHILD(deep, 0); |
---|
2952 | n/a | } |
---|
2953 | n/a | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
---|
2954 | n/a | simple = 0; |
---|
2955 | n/a | } |
---|
2956 | n/a | expr1 = ast_for_testlist(c, ch); |
---|
2957 | n/a | if (!expr1) { |
---|
2958 | n/a | return NULL; |
---|
2959 | n/a | } |
---|
2960 | n/a | switch (expr1->kind) { |
---|
2961 | n/a | case Name_kind: |
---|
2962 | n/a | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
---|
2963 | n/a | return NULL; |
---|
2964 | n/a | } |
---|
2965 | n/a | expr1->v.Name.ctx = Store; |
---|
2966 | n/a | break; |
---|
2967 | n/a | case Attribute_kind: |
---|
2968 | n/a | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
---|
2969 | n/a | return NULL; |
---|
2970 | n/a | } |
---|
2971 | n/a | expr1->v.Attribute.ctx = Store; |
---|
2972 | n/a | break; |
---|
2973 | n/a | case Subscript_kind: |
---|
2974 | n/a | expr1->v.Subscript.ctx = Store; |
---|
2975 | n/a | break; |
---|
2976 | n/a | case List_kind: |
---|
2977 | n/a | ast_error(c, ch, |
---|
2978 | n/a | "only single target (not list) can be annotated"); |
---|
2979 | n/a | return NULL; |
---|
2980 | n/a | case Tuple_kind: |
---|
2981 | n/a | ast_error(c, ch, |
---|
2982 | n/a | "only single target (not tuple) can be annotated"); |
---|
2983 | n/a | return NULL; |
---|
2984 | n/a | default: |
---|
2985 | n/a | ast_error(c, ch, |
---|
2986 | n/a | "illegal target for annotation"); |
---|
2987 | n/a | return NULL; |
---|
2988 | n/a | } |
---|
2989 | n/a | |
---|
2990 | n/a | if (expr1->kind != Name_kind) { |
---|
2991 | n/a | simple = 0; |
---|
2992 | n/a | } |
---|
2993 | n/a | ch = CHILD(ann, 1); |
---|
2994 | n/a | expr2 = ast_for_expr(c, ch); |
---|
2995 | n/a | if (!expr2) { |
---|
2996 | n/a | return NULL; |
---|
2997 | n/a | } |
---|
2998 | n/a | if (NCH(ann) == 2) { |
---|
2999 | n/a | return AnnAssign(expr1, expr2, NULL, simple, |
---|
3000 | n/a | LINENO(n), n->n_col_offset, c->c_arena); |
---|
3001 | n/a | } |
---|
3002 | n/a | else { |
---|
3003 | n/a | ch = CHILD(ann, 3); |
---|
3004 | n/a | expr3 = ast_for_expr(c, ch); |
---|
3005 | n/a | if (!expr3) { |
---|
3006 | n/a | return NULL; |
---|
3007 | n/a | } |
---|
3008 | n/a | return AnnAssign(expr1, expr2, expr3, simple, |
---|
3009 | n/a | LINENO(n), n->n_col_offset, c->c_arena); |
---|
3010 | n/a | } |
---|
3011 | n/a | } |
---|
3012 | n/a | else { |
---|
3013 | n/a | int i; |
---|
3014 | n/a | asdl_seq *targets; |
---|
3015 | n/a | node *value; |
---|
3016 | n/a | expr_ty expression; |
---|
3017 | n/a | |
---|
3018 | n/a | /* a normal assignment */ |
---|
3019 | n/a | REQ(CHILD(n, 1), EQUAL); |
---|
3020 | n/a | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
---|
3021 | n/a | if (!targets) |
---|
3022 | n/a | return NULL; |
---|
3023 | n/a | for (i = 0; i < NCH(n) - 2; i += 2) { |
---|
3024 | n/a | expr_ty e; |
---|
3025 | n/a | node *ch = CHILD(n, i); |
---|
3026 | n/a | if (TYPE(ch) == yield_expr) { |
---|
3027 | n/a | ast_error(c, ch, "assignment to yield expression not possible"); |
---|
3028 | n/a | return NULL; |
---|
3029 | n/a | } |
---|
3030 | n/a | e = ast_for_testlist(c, ch); |
---|
3031 | n/a | if (!e) |
---|
3032 | n/a | return NULL; |
---|
3033 | n/a | |
---|
3034 | n/a | /* set context to assign */ |
---|
3035 | n/a | if (!set_context(c, e, Store, CHILD(n, i))) |
---|
3036 | n/a | return NULL; |
---|
3037 | n/a | |
---|
3038 | n/a | asdl_seq_SET(targets, i / 2, e); |
---|
3039 | n/a | } |
---|
3040 | n/a | value = CHILD(n, NCH(n) - 1); |
---|
3041 | n/a | if (TYPE(value) == testlist_star_expr) |
---|
3042 | n/a | expression = ast_for_testlist(c, value); |
---|
3043 | n/a | else |
---|
3044 | n/a | expression = ast_for_expr(c, value); |
---|
3045 | n/a | if (!expression) |
---|
3046 | n/a | return NULL; |
---|
3047 | n/a | return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3048 | n/a | } |
---|
3049 | n/a | } |
---|
3050 | n/a | |
---|
3051 | n/a | |
---|
3052 | n/a | static asdl_seq * |
---|
3053 | n/a | ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context) |
---|
3054 | n/a | { |
---|
3055 | n/a | asdl_seq *seq; |
---|
3056 | n/a | int i; |
---|
3057 | n/a | expr_ty e; |
---|
3058 | n/a | |
---|
3059 | n/a | REQ(n, exprlist); |
---|
3060 | n/a | |
---|
3061 | n/a | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
---|
3062 | n/a | if (!seq) |
---|
3063 | n/a | return NULL; |
---|
3064 | n/a | for (i = 0; i < NCH(n); i += 2) { |
---|
3065 | n/a | e = ast_for_expr(c, CHILD(n, i)); |
---|
3066 | n/a | if (!e) |
---|
3067 | n/a | return NULL; |
---|
3068 | n/a | asdl_seq_SET(seq, i / 2, e); |
---|
3069 | n/a | if (context && !set_context(c, e, context, CHILD(n, i))) |
---|
3070 | n/a | return NULL; |
---|
3071 | n/a | } |
---|
3072 | n/a | return seq; |
---|
3073 | n/a | } |
---|
3074 | n/a | |
---|
3075 | n/a | static stmt_ty |
---|
3076 | n/a | ast_for_del_stmt(struct compiling *c, const node *n) |
---|
3077 | n/a | { |
---|
3078 | n/a | asdl_seq *expr_list; |
---|
3079 | n/a | |
---|
3080 | n/a | /* del_stmt: 'del' exprlist */ |
---|
3081 | n/a | REQ(n, del_stmt); |
---|
3082 | n/a | |
---|
3083 | n/a | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
---|
3084 | n/a | if (!expr_list) |
---|
3085 | n/a | return NULL; |
---|
3086 | n/a | return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3087 | n/a | } |
---|
3088 | n/a | |
---|
3089 | n/a | static stmt_ty |
---|
3090 | n/a | ast_for_flow_stmt(struct compiling *c, const node *n) |
---|
3091 | n/a | { |
---|
3092 | n/a | /* |
---|
3093 | n/a | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
---|
3094 | n/a | | yield_stmt |
---|
3095 | n/a | break_stmt: 'break' |
---|
3096 | n/a | continue_stmt: 'continue' |
---|
3097 | n/a | return_stmt: 'return' [testlist] |
---|
3098 | n/a | yield_stmt: yield_expr |
---|
3099 | n/a | yield_expr: 'yield' testlist | 'yield' 'from' test |
---|
3100 | n/a | raise_stmt: 'raise' [test [',' test [',' test]]] |
---|
3101 | n/a | */ |
---|
3102 | n/a | node *ch; |
---|
3103 | n/a | |
---|
3104 | n/a | REQ(n, flow_stmt); |
---|
3105 | n/a | ch = CHILD(n, 0); |
---|
3106 | n/a | switch (TYPE(ch)) { |
---|
3107 | n/a | case break_stmt: |
---|
3108 | n/a | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
---|
3109 | n/a | case continue_stmt: |
---|
3110 | n/a | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
---|
3111 | n/a | case yield_stmt: { /* will reduce to yield_expr */ |
---|
3112 | n/a | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
---|
3113 | n/a | if (!exp) |
---|
3114 | n/a | return NULL; |
---|
3115 | n/a | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3116 | n/a | } |
---|
3117 | n/a | case return_stmt: |
---|
3118 | n/a | if (NCH(ch) == 1) |
---|
3119 | n/a | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3120 | n/a | else { |
---|
3121 | n/a | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
---|
3122 | n/a | if (!expression) |
---|
3123 | n/a | return NULL; |
---|
3124 | n/a | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3125 | n/a | } |
---|
3126 | n/a | case raise_stmt: |
---|
3127 | n/a | if (NCH(ch) == 1) |
---|
3128 | n/a | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3129 | n/a | else if (NCH(ch) >= 2) { |
---|
3130 | n/a | expr_ty cause = NULL; |
---|
3131 | n/a | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
---|
3132 | n/a | if (!expression) |
---|
3133 | n/a | return NULL; |
---|
3134 | n/a | if (NCH(ch) == 4) { |
---|
3135 | n/a | cause = ast_for_expr(c, CHILD(ch, 3)); |
---|
3136 | n/a | if (!cause) |
---|
3137 | n/a | return NULL; |
---|
3138 | n/a | } |
---|
3139 | n/a | return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3140 | n/a | } |
---|
3141 | n/a | default: |
---|
3142 | n/a | PyErr_Format(PyExc_SystemError, |
---|
3143 | n/a | "unexpected flow_stmt: %d", TYPE(ch)); |
---|
3144 | n/a | return NULL; |
---|
3145 | n/a | } |
---|
3146 | n/a | } |
---|
3147 | n/a | |
---|
3148 | n/a | static alias_ty |
---|
3149 | n/a | alias_for_import_name(struct compiling *c, const node *n, int store) |
---|
3150 | n/a | { |
---|
3151 | n/a | /* |
---|
3152 | n/a | import_as_name: NAME ['as' NAME] |
---|
3153 | n/a | dotted_as_name: dotted_name ['as' NAME] |
---|
3154 | n/a | dotted_name: NAME ('.' NAME)* |
---|
3155 | n/a | */ |
---|
3156 | n/a | identifier str, name; |
---|
3157 | n/a | |
---|
3158 | n/a | loop: |
---|
3159 | n/a | switch (TYPE(n)) { |
---|
3160 | n/a | case import_as_name: { |
---|
3161 | n/a | node *name_node = CHILD(n, 0); |
---|
3162 | n/a | str = NULL; |
---|
3163 | n/a | name = NEW_IDENTIFIER(name_node); |
---|
3164 | n/a | if (!name) |
---|
3165 | n/a | return NULL; |
---|
3166 | n/a | if (NCH(n) == 3) { |
---|
3167 | n/a | node *str_node = CHILD(n, 2); |
---|
3168 | n/a | str = NEW_IDENTIFIER(str_node); |
---|
3169 | n/a | if (!str) |
---|
3170 | n/a | return NULL; |
---|
3171 | n/a | if (store && forbidden_name(c, str, str_node, 0)) |
---|
3172 | n/a | return NULL; |
---|
3173 | n/a | } |
---|
3174 | n/a | else { |
---|
3175 | n/a | if (forbidden_name(c, name, name_node, 0)) |
---|
3176 | n/a | return NULL; |
---|
3177 | n/a | } |
---|
3178 | n/a | return alias(name, str, c->c_arena); |
---|
3179 | n/a | } |
---|
3180 | n/a | case dotted_as_name: |
---|
3181 | n/a | if (NCH(n) == 1) { |
---|
3182 | n/a | n = CHILD(n, 0); |
---|
3183 | n/a | goto loop; |
---|
3184 | n/a | } |
---|
3185 | n/a | else { |
---|
3186 | n/a | node *asname_node = CHILD(n, 2); |
---|
3187 | n/a | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
---|
3188 | n/a | if (!a) |
---|
3189 | n/a | return NULL; |
---|
3190 | n/a | assert(!a->asname); |
---|
3191 | n/a | a->asname = NEW_IDENTIFIER(asname_node); |
---|
3192 | n/a | if (!a->asname) |
---|
3193 | n/a | return NULL; |
---|
3194 | n/a | if (forbidden_name(c, a->asname, asname_node, 0)) |
---|
3195 | n/a | return NULL; |
---|
3196 | n/a | return a; |
---|
3197 | n/a | } |
---|
3198 | n/a | break; |
---|
3199 | n/a | case dotted_name: |
---|
3200 | n/a | if (NCH(n) == 1) { |
---|
3201 | n/a | node *name_node = CHILD(n, 0); |
---|
3202 | n/a | name = NEW_IDENTIFIER(name_node); |
---|
3203 | n/a | if (!name) |
---|
3204 | n/a | return NULL; |
---|
3205 | n/a | if (store && forbidden_name(c, name, name_node, 0)) |
---|
3206 | n/a | return NULL; |
---|
3207 | n/a | return alias(name, NULL, c->c_arena); |
---|
3208 | n/a | } |
---|
3209 | n/a | else { |
---|
3210 | n/a | /* Create a string of the form "a.b.c" */ |
---|
3211 | n/a | int i; |
---|
3212 | n/a | size_t len; |
---|
3213 | n/a | char *s; |
---|
3214 | n/a | PyObject *uni; |
---|
3215 | n/a | |
---|
3216 | n/a | len = 0; |
---|
3217 | n/a | for (i = 0; i < NCH(n); i += 2) |
---|
3218 | n/a | /* length of string plus one for the dot */ |
---|
3219 | n/a | len += strlen(STR(CHILD(n, i))) + 1; |
---|
3220 | n/a | len--; /* the last name doesn't have a dot */ |
---|
3221 | n/a | str = PyBytes_FromStringAndSize(NULL, len); |
---|
3222 | n/a | if (!str) |
---|
3223 | n/a | return NULL; |
---|
3224 | n/a | s = PyBytes_AS_STRING(str); |
---|
3225 | n/a | if (!s) |
---|
3226 | n/a | return NULL; |
---|
3227 | n/a | for (i = 0; i < NCH(n); i += 2) { |
---|
3228 | n/a | char *sch = STR(CHILD(n, i)); |
---|
3229 | n/a | strcpy(s, STR(CHILD(n, i))); |
---|
3230 | n/a | s += strlen(sch); |
---|
3231 | n/a | *s++ = '.'; |
---|
3232 | n/a | } |
---|
3233 | n/a | --s; |
---|
3234 | n/a | *s = '\0'; |
---|
3235 | n/a | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
---|
3236 | n/a | PyBytes_GET_SIZE(str), |
---|
3237 | n/a | NULL); |
---|
3238 | n/a | Py_DECREF(str); |
---|
3239 | n/a | if (!uni) |
---|
3240 | n/a | return NULL; |
---|
3241 | n/a | str = uni; |
---|
3242 | n/a | PyUnicode_InternInPlace(&str); |
---|
3243 | n/a | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
---|
3244 | n/a | Py_DECREF(str); |
---|
3245 | n/a | return NULL; |
---|
3246 | n/a | } |
---|
3247 | n/a | return alias(str, NULL, c->c_arena); |
---|
3248 | n/a | } |
---|
3249 | n/a | break; |
---|
3250 | n/a | case STAR: |
---|
3251 | n/a | str = PyUnicode_InternFromString("*"); |
---|
3252 | n/a | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
---|
3253 | n/a | Py_DECREF(str); |
---|
3254 | n/a | return NULL; |
---|
3255 | n/a | } |
---|
3256 | n/a | return alias(str, NULL, c->c_arena); |
---|
3257 | n/a | default: |
---|
3258 | n/a | PyErr_Format(PyExc_SystemError, |
---|
3259 | n/a | "unexpected import name: %d", TYPE(n)); |
---|
3260 | n/a | return NULL; |
---|
3261 | n/a | } |
---|
3262 | n/a | |
---|
3263 | n/a | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
---|
3264 | n/a | return NULL; |
---|
3265 | n/a | } |
---|
3266 | n/a | |
---|
3267 | n/a | static stmt_ty |
---|
3268 | n/a | ast_for_import_stmt(struct compiling *c, const node *n) |
---|
3269 | n/a | { |
---|
3270 | n/a | /* |
---|
3271 | n/a | import_stmt: import_name | import_from |
---|
3272 | n/a | import_name: 'import' dotted_as_names |
---|
3273 | n/a | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
---|
3274 | n/a | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
---|
3275 | n/a | */ |
---|
3276 | n/a | int lineno; |
---|
3277 | n/a | int col_offset; |
---|
3278 | n/a | int i; |
---|
3279 | n/a | asdl_seq *aliases; |
---|
3280 | n/a | |
---|
3281 | n/a | REQ(n, import_stmt); |
---|
3282 | n/a | lineno = LINENO(n); |
---|
3283 | n/a | col_offset = n->n_col_offset; |
---|
3284 | n/a | n = CHILD(n, 0); |
---|
3285 | n/a | if (TYPE(n) == import_name) { |
---|
3286 | n/a | n = CHILD(n, 1); |
---|
3287 | n/a | REQ(n, dotted_as_names); |
---|
3288 | n/a | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
---|
3289 | n/a | if (!aliases) |
---|
3290 | n/a | return NULL; |
---|
3291 | n/a | for (i = 0; i < NCH(n); i += 2) { |
---|
3292 | n/a | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1); |
---|
3293 | n/a | if (!import_alias) |
---|
3294 | n/a | return NULL; |
---|
3295 | n/a | asdl_seq_SET(aliases, i / 2, import_alias); |
---|
3296 | n/a | } |
---|
3297 | n/a | return Import(aliases, lineno, col_offset, c->c_arena); |
---|
3298 | n/a | } |
---|
3299 | n/a | else if (TYPE(n) == import_from) { |
---|
3300 | n/a | int n_children; |
---|
3301 | n/a | int idx, ndots = 0; |
---|
3302 | n/a | alias_ty mod = NULL; |
---|
3303 | n/a | identifier modname = NULL; |
---|
3304 | n/a | |
---|
3305 | n/a | /* Count the number of dots (for relative imports) and check for the |
---|
3306 | n/a | optional module name */ |
---|
3307 | n/a | for (idx = 1; idx < NCH(n); idx++) { |
---|
3308 | n/a | if (TYPE(CHILD(n, idx)) == dotted_name) { |
---|
3309 | n/a | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
---|
3310 | n/a | if (!mod) |
---|
3311 | n/a | return NULL; |
---|
3312 | n/a | idx++; |
---|
3313 | n/a | break; |
---|
3314 | n/a | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
---|
3315 | n/a | /* three consecutive dots are tokenized as one ELLIPSIS */ |
---|
3316 | n/a | ndots += 3; |
---|
3317 | n/a | continue; |
---|
3318 | n/a | } else if (TYPE(CHILD(n, idx)) != DOT) { |
---|
3319 | n/a | break; |
---|
3320 | n/a | } |
---|
3321 | n/a | ndots++; |
---|
3322 | n/a | } |
---|
3323 | n/a | idx++; /* skip over the 'import' keyword */ |
---|
3324 | n/a | switch (TYPE(CHILD(n, idx))) { |
---|
3325 | n/a | case STAR: |
---|
3326 | n/a | /* from ... import * */ |
---|
3327 | n/a | n = CHILD(n, idx); |
---|
3328 | n/a | n_children = 1; |
---|
3329 | n/a | break; |
---|
3330 | n/a | case LPAR: |
---|
3331 | n/a | /* from ... import (x, y, z) */ |
---|
3332 | n/a | n = CHILD(n, idx + 1); |
---|
3333 | n/a | n_children = NCH(n); |
---|
3334 | n/a | break; |
---|
3335 | n/a | case import_as_names: |
---|
3336 | n/a | /* from ... import x, y, z */ |
---|
3337 | n/a | n = CHILD(n, idx); |
---|
3338 | n/a | n_children = NCH(n); |
---|
3339 | n/a | if (n_children % 2 == 0) { |
---|
3340 | n/a | ast_error(c, n, "trailing comma not allowed without" |
---|
3341 | n/a | " surrounding parentheses"); |
---|
3342 | n/a | return NULL; |
---|
3343 | n/a | } |
---|
3344 | n/a | break; |
---|
3345 | n/a | default: |
---|
3346 | n/a | ast_error(c, n, "Unexpected node-type in from-import"); |
---|
3347 | n/a | return NULL; |
---|
3348 | n/a | } |
---|
3349 | n/a | |
---|
3350 | n/a | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
---|
3351 | n/a | if (!aliases) |
---|
3352 | n/a | return NULL; |
---|
3353 | n/a | |
---|
3354 | n/a | /* handle "from ... import *" special b/c there's no children */ |
---|
3355 | n/a | if (TYPE(n) == STAR) { |
---|
3356 | n/a | alias_ty import_alias = alias_for_import_name(c, n, 1); |
---|
3357 | n/a | if (!import_alias) |
---|
3358 | n/a | return NULL; |
---|
3359 | n/a | asdl_seq_SET(aliases, 0, import_alias); |
---|
3360 | n/a | } |
---|
3361 | n/a | else { |
---|
3362 | n/a | for (i = 0; i < NCH(n); i += 2) { |
---|
3363 | n/a | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1); |
---|
3364 | n/a | if (!import_alias) |
---|
3365 | n/a | return NULL; |
---|
3366 | n/a | asdl_seq_SET(aliases, i / 2, import_alias); |
---|
3367 | n/a | } |
---|
3368 | n/a | } |
---|
3369 | n/a | if (mod != NULL) |
---|
3370 | n/a | modname = mod->name; |
---|
3371 | n/a | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
---|
3372 | n/a | c->c_arena); |
---|
3373 | n/a | } |
---|
3374 | n/a | PyErr_Format(PyExc_SystemError, |
---|
3375 | n/a | "unknown import statement: starts with command '%s'", |
---|
3376 | n/a | STR(CHILD(n, 0))); |
---|
3377 | n/a | return NULL; |
---|
3378 | n/a | } |
---|
3379 | n/a | |
---|
3380 | n/a | static stmt_ty |
---|
3381 | n/a | ast_for_global_stmt(struct compiling *c, const node *n) |
---|
3382 | n/a | { |
---|
3383 | n/a | /* global_stmt: 'global' NAME (',' NAME)* */ |
---|
3384 | n/a | identifier name; |
---|
3385 | n/a | asdl_seq *s; |
---|
3386 | n/a | int i; |
---|
3387 | n/a | |
---|
3388 | n/a | REQ(n, global_stmt); |
---|
3389 | n/a | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
---|
3390 | n/a | if (!s) |
---|
3391 | n/a | return NULL; |
---|
3392 | n/a | for (i = 1; i < NCH(n); i += 2) { |
---|
3393 | n/a | name = NEW_IDENTIFIER(CHILD(n, i)); |
---|
3394 | n/a | if (!name) |
---|
3395 | n/a | return NULL; |
---|
3396 | n/a | asdl_seq_SET(s, i / 2, name); |
---|
3397 | n/a | } |
---|
3398 | n/a | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3399 | n/a | } |
---|
3400 | n/a | |
---|
3401 | n/a | static stmt_ty |
---|
3402 | n/a | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
---|
3403 | n/a | { |
---|
3404 | n/a | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
---|
3405 | n/a | identifier name; |
---|
3406 | n/a | asdl_seq *s; |
---|
3407 | n/a | int i; |
---|
3408 | n/a | |
---|
3409 | n/a | REQ(n, nonlocal_stmt); |
---|
3410 | n/a | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
---|
3411 | n/a | if (!s) |
---|
3412 | n/a | return NULL; |
---|
3413 | n/a | for (i = 1; i < NCH(n); i += 2) { |
---|
3414 | n/a | name = NEW_IDENTIFIER(CHILD(n, i)); |
---|
3415 | n/a | if (!name) |
---|
3416 | n/a | return NULL; |
---|
3417 | n/a | asdl_seq_SET(s, i / 2, name); |
---|
3418 | n/a | } |
---|
3419 | n/a | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3420 | n/a | } |
---|
3421 | n/a | |
---|
3422 | n/a | static stmt_ty |
---|
3423 | n/a | ast_for_assert_stmt(struct compiling *c, const node *n) |
---|
3424 | n/a | { |
---|
3425 | n/a | /* assert_stmt: 'assert' test [',' test] */ |
---|
3426 | n/a | REQ(n, assert_stmt); |
---|
3427 | n/a | if (NCH(n) == 2) { |
---|
3428 | n/a | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
---|
3429 | n/a | if (!expression) |
---|
3430 | n/a | return NULL; |
---|
3431 | n/a | return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3432 | n/a | } |
---|
3433 | n/a | else if (NCH(n) == 4) { |
---|
3434 | n/a | expr_ty expr1, expr2; |
---|
3435 | n/a | |
---|
3436 | n/a | expr1 = ast_for_expr(c, CHILD(n, 1)); |
---|
3437 | n/a | if (!expr1) |
---|
3438 | n/a | return NULL; |
---|
3439 | n/a | expr2 = ast_for_expr(c, CHILD(n, 3)); |
---|
3440 | n/a | if (!expr2) |
---|
3441 | n/a | return NULL; |
---|
3442 | n/a | |
---|
3443 | n/a | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3444 | n/a | } |
---|
3445 | n/a | PyErr_Format(PyExc_SystemError, |
---|
3446 | n/a | "improper number of parts to 'assert' statement: %d", |
---|
3447 | n/a | NCH(n)); |
---|
3448 | n/a | return NULL; |
---|
3449 | n/a | } |
---|
3450 | n/a | |
---|
3451 | n/a | static asdl_seq * |
---|
3452 | n/a | ast_for_suite(struct compiling *c, const node *n) |
---|
3453 | n/a | { |
---|
3454 | n/a | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
---|
3455 | n/a | asdl_seq *seq; |
---|
3456 | n/a | stmt_ty s; |
---|
3457 | n/a | int i, total, num, end, pos = 0; |
---|
3458 | n/a | node *ch; |
---|
3459 | n/a | |
---|
3460 | n/a | REQ(n, suite); |
---|
3461 | n/a | |
---|
3462 | n/a | total = num_stmts(n); |
---|
3463 | n/a | seq = _Py_asdl_seq_new(total, c->c_arena); |
---|
3464 | n/a | if (!seq) |
---|
3465 | n/a | return NULL; |
---|
3466 | n/a | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
---|
3467 | n/a | n = CHILD(n, 0); |
---|
3468 | n/a | /* simple_stmt always ends with a NEWLINE, |
---|
3469 | n/a | and may have a trailing SEMI |
---|
3470 | n/a | */ |
---|
3471 | n/a | end = NCH(n) - 1; |
---|
3472 | n/a | if (TYPE(CHILD(n, end - 1)) == SEMI) |
---|
3473 | n/a | end--; |
---|
3474 | n/a | /* loop by 2 to skip semi-colons */ |
---|
3475 | n/a | for (i = 0; i < end; i += 2) { |
---|
3476 | n/a | ch = CHILD(n, i); |
---|
3477 | n/a | s = ast_for_stmt(c, ch); |
---|
3478 | n/a | if (!s) |
---|
3479 | n/a | return NULL; |
---|
3480 | n/a | asdl_seq_SET(seq, pos++, s); |
---|
3481 | n/a | } |
---|
3482 | n/a | } |
---|
3483 | n/a | else { |
---|
3484 | n/a | for (i = 2; i < (NCH(n) - 1); i++) { |
---|
3485 | n/a | ch = CHILD(n, i); |
---|
3486 | n/a | REQ(ch, stmt); |
---|
3487 | n/a | num = num_stmts(ch); |
---|
3488 | n/a | if (num == 1) { |
---|
3489 | n/a | /* small_stmt or compound_stmt with only one child */ |
---|
3490 | n/a | s = ast_for_stmt(c, ch); |
---|
3491 | n/a | if (!s) |
---|
3492 | n/a | return NULL; |
---|
3493 | n/a | asdl_seq_SET(seq, pos++, s); |
---|
3494 | n/a | } |
---|
3495 | n/a | else { |
---|
3496 | n/a | int j; |
---|
3497 | n/a | ch = CHILD(ch, 0); |
---|
3498 | n/a | REQ(ch, simple_stmt); |
---|
3499 | n/a | for (j = 0; j < NCH(ch); j += 2) { |
---|
3500 | n/a | /* statement terminates with a semi-colon ';' */ |
---|
3501 | n/a | if (NCH(CHILD(ch, j)) == 0) { |
---|
3502 | n/a | assert((j + 1) == NCH(ch)); |
---|
3503 | n/a | break; |
---|
3504 | n/a | } |
---|
3505 | n/a | s = ast_for_stmt(c, CHILD(ch, j)); |
---|
3506 | n/a | if (!s) |
---|
3507 | n/a | return NULL; |
---|
3508 | n/a | asdl_seq_SET(seq, pos++, s); |
---|
3509 | n/a | } |
---|
3510 | n/a | } |
---|
3511 | n/a | } |
---|
3512 | n/a | } |
---|
3513 | n/a | assert(pos == seq->size); |
---|
3514 | n/a | return seq; |
---|
3515 | n/a | } |
---|
3516 | n/a | |
---|
3517 | n/a | static stmt_ty |
---|
3518 | n/a | ast_for_if_stmt(struct compiling *c, const node *n) |
---|
3519 | n/a | { |
---|
3520 | n/a | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
---|
3521 | n/a | ['else' ':' suite] |
---|
3522 | n/a | */ |
---|
3523 | n/a | char *s; |
---|
3524 | n/a | |
---|
3525 | n/a | REQ(n, if_stmt); |
---|
3526 | n/a | |
---|
3527 | n/a | if (NCH(n) == 4) { |
---|
3528 | n/a | expr_ty expression; |
---|
3529 | n/a | asdl_seq *suite_seq; |
---|
3530 | n/a | |
---|
3531 | n/a | expression = ast_for_expr(c, CHILD(n, 1)); |
---|
3532 | n/a | if (!expression) |
---|
3533 | n/a | return NULL; |
---|
3534 | n/a | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
---|
3535 | n/a | if (!suite_seq) |
---|
3536 | n/a | return NULL; |
---|
3537 | n/a | |
---|
3538 | n/a | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
---|
3539 | n/a | c->c_arena); |
---|
3540 | n/a | } |
---|
3541 | n/a | |
---|
3542 | n/a | s = STR(CHILD(n, 4)); |
---|
3543 | n/a | /* s[2], the third character in the string, will be |
---|
3544 | n/a | 's' for el_s_e, or |
---|
3545 | n/a | 'i' for el_i_f |
---|
3546 | n/a | */ |
---|
3547 | n/a | if (s[2] == 's') { |
---|
3548 | n/a | expr_ty expression; |
---|
3549 | n/a | asdl_seq *seq1, *seq2; |
---|
3550 | n/a | |
---|
3551 | n/a | expression = ast_for_expr(c, CHILD(n, 1)); |
---|
3552 | n/a | if (!expression) |
---|
3553 | n/a | return NULL; |
---|
3554 | n/a | seq1 = ast_for_suite(c, CHILD(n, 3)); |
---|
3555 | n/a | if (!seq1) |
---|
3556 | n/a | return NULL; |
---|
3557 | n/a | seq2 = ast_for_suite(c, CHILD(n, 6)); |
---|
3558 | n/a | if (!seq2) |
---|
3559 | n/a | return NULL; |
---|
3560 | n/a | |
---|
3561 | n/a | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
---|
3562 | n/a | c->c_arena); |
---|
3563 | n/a | } |
---|
3564 | n/a | else if (s[2] == 'i') { |
---|
3565 | n/a | int i, n_elif, has_else = 0; |
---|
3566 | n/a | expr_ty expression; |
---|
3567 | n/a | asdl_seq *suite_seq; |
---|
3568 | n/a | asdl_seq *orelse = NULL; |
---|
3569 | n/a | n_elif = NCH(n) - 4; |
---|
3570 | n/a | /* must reference the child n_elif+1 since 'else' token is third, |
---|
3571 | n/a | not fourth, child from the end. */ |
---|
3572 | n/a | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
---|
3573 | n/a | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
---|
3574 | n/a | has_else = 1; |
---|
3575 | n/a | n_elif -= 3; |
---|
3576 | n/a | } |
---|
3577 | n/a | n_elif /= 4; |
---|
3578 | n/a | |
---|
3579 | n/a | if (has_else) { |
---|
3580 | n/a | asdl_seq *suite_seq2; |
---|
3581 | n/a | |
---|
3582 | n/a | orelse = _Py_asdl_seq_new(1, c->c_arena); |
---|
3583 | n/a | if (!orelse) |
---|
3584 | n/a | return NULL; |
---|
3585 | n/a | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
---|
3586 | n/a | if (!expression) |
---|
3587 | n/a | return NULL; |
---|
3588 | n/a | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
---|
3589 | n/a | if (!suite_seq) |
---|
3590 | n/a | return NULL; |
---|
3591 | n/a | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
---|
3592 | n/a | if (!suite_seq2) |
---|
3593 | n/a | return NULL; |
---|
3594 | n/a | |
---|
3595 | n/a | asdl_seq_SET(orelse, 0, |
---|
3596 | n/a | If(expression, suite_seq, suite_seq2, |
---|
3597 | n/a | LINENO(CHILD(n, NCH(n) - 6)), |
---|
3598 | n/a | CHILD(n, NCH(n) - 6)->n_col_offset, |
---|
3599 | n/a | c->c_arena)); |
---|
3600 | n/a | /* the just-created orelse handled the last elif */ |
---|
3601 | n/a | n_elif--; |
---|
3602 | n/a | } |
---|
3603 | n/a | |
---|
3604 | n/a | for (i = 0; i < n_elif; i++) { |
---|
3605 | n/a | int off = 5 + (n_elif - i - 1) * 4; |
---|
3606 | n/a | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
---|
3607 | n/a | if (!newobj) |
---|
3608 | n/a | return NULL; |
---|
3609 | n/a | expression = ast_for_expr(c, CHILD(n, off)); |
---|
3610 | n/a | if (!expression) |
---|
3611 | n/a | return NULL; |
---|
3612 | n/a | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
---|
3613 | n/a | if (!suite_seq) |
---|
3614 | n/a | return NULL; |
---|
3615 | n/a | |
---|
3616 | n/a | asdl_seq_SET(newobj, 0, |
---|
3617 | n/a | If(expression, suite_seq, orelse, |
---|
3618 | n/a | LINENO(CHILD(n, off)), |
---|
3619 | n/a | CHILD(n, off)->n_col_offset, c->c_arena)); |
---|
3620 | n/a | orelse = newobj; |
---|
3621 | n/a | } |
---|
3622 | n/a | expression = ast_for_expr(c, CHILD(n, 1)); |
---|
3623 | n/a | if (!expression) |
---|
3624 | n/a | return NULL; |
---|
3625 | n/a | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
---|
3626 | n/a | if (!suite_seq) |
---|
3627 | n/a | return NULL; |
---|
3628 | n/a | return If(expression, suite_seq, orelse, |
---|
3629 | n/a | LINENO(n), n->n_col_offset, c->c_arena); |
---|
3630 | n/a | } |
---|
3631 | n/a | |
---|
3632 | n/a | PyErr_Format(PyExc_SystemError, |
---|
3633 | n/a | "unexpected token in 'if' statement: %s", s); |
---|
3634 | n/a | return NULL; |
---|
3635 | n/a | } |
---|
3636 | n/a | |
---|
3637 | n/a | static stmt_ty |
---|
3638 | n/a | ast_for_while_stmt(struct compiling *c, const node *n) |
---|
3639 | n/a | { |
---|
3640 | n/a | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
---|
3641 | n/a | REQ(n, while_stmt); |
---|
3642 | n/a | |
---|
3643 | n/a | if (NCH(n) == 4) { |
---|
3644 | n/a | expr_ty expression; |
---|
3645 | n/a | asdl_seq *suite_seq; |
---|
3646 | n/a | |
---|
3647 | n/a | expression = ast_for_expr(c, CHILD(n, 1)); |
---|
3648 | n/a | if (!expression) |
---|
3649 | n/a | return NULL; |
---|
3650 | n/a | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
---|
3651 | n/a | if (!suite_seq) |
---|
3652 | n/a | return NULL; |
---|
3653 | n/a | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3654 | n/a | } |
---|
3655 | n/a | else if (NCH(n) == 7) { |
---|
3656 | n/a | expr_ty expression; |
---|
3657 | n/a | asdl_seq *seq1, *seq2; |
---|
3658 | n/a | |
---|
3659 | n/a | expression = ast_for_expr(c, CHILD(n, 1)); |
---|
3660 | n/a | if (!expression) |
---|
3661 | n/a | return NULL; |
---|
3662 | n/a | seq1 = ast_for_suite(c, CHILD(n, 3)); |
---|
3663 | n/a | if (!seq1) |
---|
3664 | n/a | return NULL; |
---|
3665 | n/a | seq2 = ast_for_suite(c, CHILD(n, 6)); |
---|
3666 | n/a | if (!seq2) |
---|
3667 | n/a | return NULL; |
---|
3668 | n/a | |
---|
3669 | n/a | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3670 | n/a | } |
---|
3671 | n/a | |
---|
3672 | n/a | PyErr_Format(PyExc_SystemError, |
---|
3673 | n/a | "wrong number of tokens for 'while' statement: %d", |
---|
3674 | n/a | NCH(n)); |
---|
3675 | n/a | return NULL; |
---|
3676 | n/a | } |
---|
3677 | n/a | |
---|
3678 | n/a | static stmt_ty |
---|
3679 | n/a | ast_for_for_stmt(struct compiling *c, const node *n, int is_async) |
---|
3680 | n/a | { |
---|
3681 | n/a | asdl_seq *_target, *seq = NULL, *suite_seq; |
---|
3682 | n/a | expr_ty expression; |
---|
3683 | n/a | expr_ty target, first; |
---|
3684 | n/a | const node *node_target; |
---|
3685 | n/a | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
---|
3686 | n/a | REQ(n, for_stmt); |
---|
3687 | n/a | |
---|
3688 | n/a | if (NCH(n) == 9) { |
---|
3689 | n/a | seq = ast_for_suite(c, CHILD(n, 8)); |
---|
3690 | n/a | if (!seq) |
---|
3691 | n/a | return NULL; |
---|
3692 | n/a | } |
---|
3693 | n/a | |
---|
3694 | n/a | node_target = CHILD(n, 1); |
---|
3695 | n/a | _target = ast_for_exprlist(c, node_target, Store); |
---|
3696 | n/a | if (!_target) |
---|
3697 | n/a | return NULL; |
---|
3698 | n/a | /* Check the # of children rather than the length of _target, since |
---|
3699 | n/a | for x, in ... has 1 element in _target, but still requires a Tuple. */ |
---|
3700 | n/a | first = (expr_ty)asdl_seq_GET(_target, 0); |
---|
3701 | n/a | if (NCH(node_target) == 1) |
---|
3702 | n/a | target = first; |
---|
3703 | n/a | else |
---|
3704 | n/a | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
---|
3705 | n/a | |
---|
3706 | n/a | expression = ast_for_testlist(c, CHILD(n, 3)); |
---|
3707 | n/a | if (!expression) |
---|
3708 | n/a | return NULL; |
---|
3709 | n/a | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
---|
3710 | n/a | if (!suite_seq) |
---|
3711 | n/a | return NULL; |
---|
3712 | n/a | |
---|
3713 | n/a | if (is_async) |
---|
3714 | n/a | return AsyncFor(target, expression, suite_seq, seq, |
---|
3715 | n/a | LINENO(n), n->n_col_offset, |
---|
3716 | n/a | c->c_arena); |
---|
3717 | n/a | else |
---|
3718 | n/a | return For(target, expression, suite_seq, seq, |
---|
3719 | n/a | LINENO(n), n->n_col_offset, |
---|
3720 | n/a | c->c_arena); |
---|
3721 | n/a | } |
---|
3722 | n/a | |
---|
3723 | n/a | static excepthandler_ty |
---|
3724 | n/a | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
---|
3725 | n/a | { |
---|
3726 | n/a | /* except_clause: 'except' [test ['as' test]] */ |
---|
3727 | n/a | REQ(exc, except_clause); |
---|
3728 | n/a | REQ(body, suite); |
---|
3729 | n/a | |
---|
3730 | n/a | if (NCH(exc) == 1) { |
---|
3731 | n/a | asdl_seq *suite_seq = ast_for_suite(c, body); |
---|
3732 | n/a | if (!suite_seq) |
---|
3733 | n/a | return NULL; |
---|
3734 | n/a | |
---|
3735 | n/a | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
---|
3736 | n/a | exc->n_col_offset, c->c_arena); |
---|
3737 | n/a | } |
---|
3738 | n/a | else if (NCH(exc) == 2) { |
---|
3739 | n/a | expr_ty expression; |
---|
3740 | n/a | asdl_seq *suite_seq; |
---|
3741 | n/a | |
---|
3742 | n/a | expression = ast_for_expr(c, CHILD(exc, 1)); |
---|
3743 | n/a | if (!expression) |
---|
3744 | n/a | return NULL; |
---|
3745 | n/a | suite_seq = ast_for_suite(c, body); |
---|
3746 | n/a | if (!suite_seq) |
---|
3747 | n/a | return NULL; |
---|
3748 | n/a | |
---|
3749 | n/a | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
---|
3750 | n/a | exc->n_col_offset, c->c_arena); |
---|
3751 | n/a | } |
---|
3752 | n/a | else if (NCH(exc) == 4) { |
---|
3753 | n/a | asdl_seq *suite_seq; |
---|
3754 | n/a | expr_ty expression; |
---|
3755 | n/a | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
---|
3756 | n/a | if (!e) |
---|
3757 | n/a | return NULL; |
---|
3758 | n/a | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
---|
3759 | n/a | return NULL; |
---|
3760 | n/a | expression = ast_for_expr(c, CHILD(exc, 1)); |
---|
3761 | n/a | if (!expression) |
---|
3762 | n/a | return NULL; |
---|
3763 | n/a | suite_seq = ast_for_suite(c, body); |
---|
3764 | n/a | if (!suite_seq) |
---|
3765 | n/a | return NULL; |
---|
3766 | n/a | |
---|
3767 | n/a | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
---|
3768 | n/a | exc->n_col_offset, c->c_arena); |
---|
3769 | n/a | } |
---|
3770 | n/a | |
---|
3771 | n/a | PyErr_Format(PyExc_SystemError, |
---|
3772 | n/a | "wrong number of children for 'except' clause: %d", |
---|
3773 | n/a | NCH(exc)); |
---|
3774 | n/a | return NULL; |
---|
3775 | n/a | } |
---|
3776 | n/a | |
---|
3777 | n/a | static stmt_ty |
---|
3778 | n/a | ast_for_try_stmt(struct compiling *c, const node *n) |
---|
3779 | n/a | { |
---|
3780 | n/a | const int nch = NCH(n); |
---|
3781 | n/a | int n_except = (nch - 3)/3; |
---|
3782 | n/a | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
---|
3783 | n/a | |
---|
3784 | n/a | REQ(n, try_stmt); |
---|
3785 | n/a | |
---|
3786 | n/a | body = ast_for_suite(c, CHILD(n, 2)); |
---|
3787 | n/a | if (body == NULL) |
---|
3788 | n/a | return NULL; |
---|
3789 | n/a | |
---|
3790 | n/a | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
---|
3791 | n/a | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
---|
3792 | n/a | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
---|
3793 | n/a | /* we can assume it's an "else", |
---|
3794 | n/a | because nch >= 9 for try-else-finally and |
---|
3795 | n/a | it would otherwise have a type of except_clause */ |
---|
3796 | n/a | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
---|
3797 | n/a | if (orelse == NULL) |
---|
3798 | n/a | return NULL; |
---|
3799 | n/a | n_except--; |
---|
3800 | n/a | } |
---|
3801 | n/a | |
---|
3802 | n/a | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
---|
3803 | n/a | if (finally == NULL) |
---|
3804 | n/a | return NULL; |
---|
3805 | n/a | n_except--; |
---|
3806 | n/a | } |
---|
3807 | n/a | else { |
---|
3808 | n/a | /* we can assume it's an "else", |
---|
3809 | n/a | otherwise it would have a type of except_clause */ |
---|
3810 | n/a | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
---|
3811 | n/a | if (orelse == NULL) |
---|
3812 | n/a | return NULL; |
---|
3813 | n/a | n_except--; |
---|
3814 | n/a | } |
---|
3815 | n/a | } |
---|
3816 | n/a | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
---|
3817 | n/a | ast_error(c, n, "malformed 'try' statement"); |
---|
3818 | n/a | return NULL; |
---|
3819 | n/a | } |
---|
3820 | n/a | |
---|
3821 | n/a | if (n_except > 0) { |
---|
3822 | n/a | int i; |
---|
3823 | n/a | /* process except statements to create a try ... except */ |
---|
3824 | n/a | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
---|
3825 | n/a | if (handlers == NULL) |
---|
3826 | n/a | return NULL; |
---|
3827 | n/a | |
---|
3828 | n/a | for (i = 0; i < n_except; i++) { |
---|
3829 | n/a | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
---|
3830 | n/a | CHILD(n, 5 + i * 3)); |
---|
3831 | n/a | if (!e) |
---|
3832 | n/a | return NULL; |
---|
3833 | n/a | asdl_seq_SET(handlers, i, e); |
---|
3834 | n/a | } |
---|
3835 | n/a | } |
---|
3836 | n/a | |
---|
3837 | n/a | assert(finally != NULL || asdl_seq_LEN(handlers)); |
---|
3838 | n/a | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3839 | n/a | } |
---|
3840 | n/a | |
---|
3841 | n/a | /* with_item: test ['as' expr] */ |
---|
3842 | n/a | static withitem_ty |
---|
3843 | n/a | ast_for_with_item(struct compiling *c, const node *n) |
---|
3844 | n/a | { |
---|
3845 | n/a | expr_ty context_expr, optional_vars = NULL; |
---|
3846 | n/a | |
---|
3847 | n/a | REQ(n, with_item); |
---|
3848 | n/a | context_expr = ast_for_expr(c, CHILD(n, 0)); |
---|
3849 | n/a | if (!context_expr) |
---|
3850 | n/a | return NULL; |
---|
3851 | n/a | if (NCH(n) == 3) { |
---|
3852 | n/a | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
---|
3853 | n/a | |
---|
3854 | n/a | if (!optional_vars) { |
---|
3855 | n/a | return NULL; |
---|
3856 | n/a | } |
---|
3857 | n/a | if (!set_context(c, optional_vars, Store, n)) { |
---|
3858 | n/a | return NULL; |
---|
3859 | n/a | } |
---|
3860 | n/a | } |
---|
3861 | n/a | |
---|
3862 | n/a | return withitem(context_expr, optional_vars, c->c_arena); |
---|
3863 | n/a | } |
---|
3864 | n/a | |
---|
3865 | n/a | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
---|
3866 | n/a | static stmt_ty |
---|
3867 | n/a | ast_for_with_stmt(struct compiling *c, const node *n, int is_async) |
---|
3868 | n/a | { |
---|
3869 | n/a | int i, n_items; |
---|
3870 | n/a | asdl_seq *items, *body; |
---|
3871 | n/a | |
---|
3872 | n/a | REQ(n, with_stmt); |
---|
3873 | n/a | |
---|
3874 | n/a | n_items = (NCH(n) - 2) / 2; |
---|
3875 | n/a | items = _Py_asdl_seq_new(n_items, c->c_arena); |
---|
3876 | n/a | if (!items) |
---|
3877 | n/a | return NULL; |
---|
3878 | n/a | for (i = 1; i < NCH(n) - 2; i += 2) { |
---|
3879 | n/a | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
---|
3880 | n/a | if (!item) |
---|
3881 | n/a | return NULL; |
---|
3882 | n/a | asdl_seq_SET(items, (i - 1) / 2, item); |
---|
3883 | n/a | } |
---|
3884 | n/a | |
---|
3885 | n/a | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
---|
3886 | n/a | if (!body) |
---|
3887 | n/a | return NULL; |
---|
3888 | n/a | |
---|
3889 | n/a | if (is_async) |
---|
3890 | n/a | return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3891 | n/a | else |
---|
3892 | n/a | return With(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3893 | n/a | } |
---|
3894 | n/a | |
---|
3895 | n/a | static stmt_ty |
---|
3896 | n/a | ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
---|
3897 | n/a | { |
---|
3898 | n/a | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
---|
3899 | n/a | PyObject *classname; |
---|
3900 | n/a | asdl_seq *s; |
---|
3901 | n/a | expr_ty call; |
---|
3902 | n/a | |
---|
3903 | n/a | REQ(n, classdef); |
---|
3904 | n/a | |
---|
3905 | n/a | if (NCH(n) == 4) { /* class NAME ':' suite */ |
---|
3906 | n/a | s = ast_for_suite(c, CHILD(n, 3)); |
---|
3907 | n/a | if (!s) |
---|
3908 | n/a | return NULL; |
---|
3909 | n/a | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
---|
3910 | n/a | if (!classname) |
---|
3911 | n/a | return NULL; |
---|
3912 | n/a | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
---|
3913 | n/a | return NULL; |
---|
3914 | n/a | return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n), |
---|
3915 | n/a | n->n_col_offset, c->c_arena); |
---|
3916 | n/a | } |
---|
3917 | n/a | |
---|
3918 | n/a | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
---|
3919 | n/a | s = ast_for_suite(c, CHILD(n,5)); |
---|
3920 | n/a | if (!s) |
---|
3921 | n/a | return NULL; |
---|
3922 | n/a | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
---|
3923 | n/a | if (!classname) |
---|
3924 | n/a | return NULL; |
---|
3925 | n/a | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
---|
3926 | n/a | return NULL; |
---|
3927 | n/a | return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n), |
---|
3928 | n/a | n->n_col_offset, c->c_arena); |
---|
3929 | n/a | } |
---|
3930 | n/a | |
---|
3931 | n/a | /* class NAME '(' arglist ')' ':' suite */ |
---|
3932 | n/a | /* build up a fake Call node so we can extract its pieces */ |
---|
3933 | n/a | { |
---|
3934 | n/a | PyObject *dummy_name; |
---|
3935 | n/a | expr_ty dummy; |
---|
3936 | n/a | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
---|
3937 | n/a | if (!dummy_name) |
---|
3938 | n/a | return NULL; |
---|
3939 | n/a | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3940 | n/a | call = ast_for_call(c, CHILD(n, 3), dummy); |
---|
3941 | n/a | if (!call) |
---|
3942 | n/a | return NULL; |
---|
3943 | n/a | } |
---|
3944 | n/a | s = ast_for_suite(c, CHILD(n, 6)); |
---|
3945 | n/a | if (!s) |
---|
3946 | n/a | return NULL; |
---|
3947 | n/a | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
---|
3948 | n/a | if (!classname) |
---|
3949 | n/a | return NULL; |
---|
3950 | n/a | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
---|
3951 | n/a | return NULL; |
---|
3952 | n/a | |
---|
3953 | n/a | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
---|
3954 | n/a | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
---|
3955 | n/a | } |
---|
3956 | n/a | |
---|
3957 | n/a | static stmt_ty |
---|
3958 | n/a | ast_for_stmt(struct compiling *c, const node *n) |
---|
3959 | n/a | { |
---|
3960 | n/a | if (TYPE(n) == stmt) { |
---|
3961 | n/a | assert(NCH(n) == 1); |
---|
3962 | n/a | n = CHILD(n, 0); |
---|
3963 | n/a | } |
---|
3964 | n/a | if (TYPE(n) == simple_stmt) { |
---|
3965 | n/a | assert(num_stmts(n) == 1); |
---|
3966 | n/a | n = CHILD(n, 0); |
---|
3967 | n/a | } |
---|
3968 | n/a | if (TYPE(n) == small_stmt) { |
---|
3969 | n/a | n = CHILD(n, 0); |
---|
3970 | n/a | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
---|
3971 | n/a | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
---|
3972 | n/a | */ |
---|
3973 | n/a | switch (TYPE(n)) { |
---|
3974 | n/a | case expr_stmt: |
---|
3975 | n/a | return ast_for_expr_stmt(c, n); |
---|
3976 | n/a | case del_stmt: |
---|
3977 | n/a | return ast_for_del_stmt(c, n); |
---|
3978 | n/a | case pass_stmt: |
---|
3979 | n/a | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
---|
3980 | n/a | case flow_stmt: |
---|
3981 | n/a | return ast_for_flow_stmt(c, n); |
---|
3982 | n/a | case import_stmt: |
---|
3983 | n/a | return ast_for_import_stmt(c, n); |
---|
3984 | n/a | case global_stmt: |
---|
3985 | n/a | return ast_for_global_stmt(c, n); |
---|
3986 | n/a | case nonlocal_stmt: |
---|
3987 | n/a | return ast_for_nonlocal_stmt(c, n); |
---|
3988 | n/a | case assert_stmt: |
---|
3989 | n/a | return ast_for_assert_stmt(c, n); |
---|
3990 | n/a | default: |
---|
3991 | n/a | PyErr_Format(PyExc_SystemError, |
---|
3992 | n/a | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
---|
3993 | n/a | TYPE(n), NCH(n)); |
---|
3994 | n/a | return NULL; |
---|
3995 | n/a | } |
---|
3996 | n/a | } |
---|
3997 | n/a | else { |
---|
3998 | n/a | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
---|
3999 | n/a | | funcdef | classdef | decorated | async_stmt |
---|
4000 | n/a | */ |
---|
4001 | n/a | node *ch = CHILD(n, 0); |
---|
4002 | n/a | REQ(n, compound_stmt); |
---|
4003 | n/a | switch (TYPE(ch)) { |
---|
4004 | n/a | case if_stmt: |
---|
4005 | n/a | return ast_for_if_stmt(c, ch); |
---|
4006 | n/a | case while_stmt: |
---|
4007 | n/a | return ast_for_while_stmt(c, ch); |
---|
4008 | n/a | case for_stmt: |
---|
4009 | n/a | return ast_for_for_stmt(c, ch, 0); |
---|
4010 | n/a | case try_stmt: |
---|
4011 | n/a | return ast_for_try_stmt(c, ch); |
---|
4012 | n/a | case with_stmt: |
---|
4013 | n/a | return ast_for_with_stmt(c, ch, 0); |
---|
4014 | n/a | case funcdef: |
---|
4015 | n/a | return ast_for_funcdef(c, ch, NULL); |
---|
4016 | n/a | case classdef: |
---|
4017 | n/a | return ast_for_classdef(c, ch, NULL); |
---|
4018 | n/a | case decorated: |
---|
4019 | n/a | return ast_for_decorated(c, ch); |
---|
4020 | n/a | case async_stmt: |
---|
4021 | n/a | return ast_for_async_stmt(c, ch); |
---|
4022 | n/a | default: |
---|
4023 | n/a | PyErr_Format(PyExc_SystemError, |
---|
4024 | n/a | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
---|
4025 | n/a | TYPE(n), NCH(n)); |
---|
4026 | n/a | return NULL; |
---|
4027 | n/a | } |
---|
4028 | n/a | } |
---|
4029 | n/a | } |
---|
4030 | n/a | |
---|
4031 | n/a | static PyObject * |
---|
4032 | n/a | parsenumber_raw(struct compiling *c, const char *s) |
---|
4033 | n/a | { |
---|
4034 | n/a | const char *end; |
---|
4035 | n/a | long x; |
---|
4036 | n/a | double dx; |
---|
4037 | n/a | Py_complex compl; |
---|
4038 | n/a | int imflag; |
---|
4039 | n/a | |
---|
4040 | n/a | assert(s != NULL); |
---|
4041 | n/a | errno = 0; |
---|
4042 | n/a | end = s + strlen(s) - 1; |
---|
4043 | n/a | imflag = *end == 'j' || *end == 'J'; |
---|
4044 | n/a | if (s[0] == '0') { |
---|
4045 | n/a | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
---|
4046 | n/a | if (x < 0 && errno == 0) { |
---|
4047 | n/a | return PyLong_FromString(s, (char **)0, 0); |
---|
4048 | n/a | } |
---|
4049 | n/a | } |
---|
4050 | n/a | else |
---|
4051 | n/a | x = PyOS_strtol(s, (char **)&end, 0); |
---|
4052 | n/a | if (*end == '\0') { |
---|
4053 | n/a | if (errno != 0) |
---|
4054 | n/a | return PyLong_FromString(s, (char **)0, 0); |
---|
4055 | n/a | return PyLong_FromLong(x); |
---|
4056 | n/a | } |
---|
4057 | n/a | /* XXX Huge floats may silently fail */ |
---|
4058 | n/a | if (imflag) { |
---|
4059 | n/a | compl.real = 0.; |
---|
4060 | n/a | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
---|
4061 | n/a | if (compl.imag == -1.0 && PyErr_Occurred()) |
---|
4062 | n/a | return NULL; |
---|
4063 | n/a | return PyComplex_FromCComplex(compl); |
---|
4064 | n/a | } |
---|
4065 | n/a | else |
---|
4066 | n/a | { |
---|
4067 | n/a | dx = PyOS_string_to_double(s, NULL, NULL); |
---|
4068 | n/a | if (dx == -1.0 && PyErr_Occurred()) |
---|
4069 | n/a | return NULL; |
---|
4070 | n/a | return PyFloat_FromDouble(dx); |
---|
4071 | n/a | } |
---|
4072 | n/a | } |
---|
4073 | n/a | |
---|
4074 | n/a | static PyObject * |
---|
4075 | n/a | parsenumber(struct compiling *c, const char *s) |
---|
4076 | n/a | { |
---|
4077 | n/a | char *dup, *end; |
---|
4078 | n/a | PyObject *res = NULL; |
---|
4079 | n/a | |
---|
4080 | n/a | assert(s != NULL); |
---|
4081 | n/a | |
---|
4082 | n/a | if (strchr(s, '_') == NULL) { |
---|
4083 | n/a | return parsenumber_raw(c, s); |
---|
4084 | n/a | } |
---|
4085 | n/a | /* Create a duplicate without underscores. */ |
---|
4086 | n/a | dup = PyMem_Malloc(strlen(s) + 1); |
---|
4087 | n/a | end = dup; |
---|
4088 | n/a | for (; *s; s++) { |
---|
4089 | n/a | if (*s != '_') { |
---|
4090 | n/a | *end++ = *s; |
---|
4091 | n/a | } |
---|
4092 | n/a | } |
---|
4093 | n/a | *end = '\0'; |
---|
4094 | n/a | res = parsenumber_raw(c, dup); |
---|
4095 | n/a | PyMem_Free(dup); |
---|
4096 | n/a | return res; |
---|
4097 | n/a | } |
---|
4098 | n/a | |
---|
4099 | n/a | static PyObject * |
---|
4100 | n/a | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
---|
4101 | n/a | { |
---|
4102 | n/a | const char *s, *t; |
---|
4103 | n/a | t = s = *sPtr; |
---|
4104 | n/a | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
---|
4105 | n/a | while (s < end && (*s & 0x80)) s++; |
---|
4106 | n/a | *sPtr = s; |
---|
4107 | n/a | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
---|
4108 | n/a | } |
---|
4109 | n/a | |
---|
4110 | n/a | static int |
---|
4111 | n/a | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
---|
4112 | n/a | char first_invalid_escape_char) |
---|
4113 | n/a | { |
---|
4114 | n/a | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
---|
4115 | n/a | first_invalid_escape_char); |
---|
4116 | n/a | if (msg == NULL) { |
---|
4117 | n/a | return -1; |
---|
4118 | n/a | } |
---|
4119 | n/a | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
---|
4120 | n/a | c->c_filename, LINENO(n), |
---|
4121 | n/a | NULL, NULL) < 0 && |
---|
4122 | n/a | PyErr_ExceptionMatches(PyExc_DeprecationWarning)) |
---|
4123 | n/a | { |
---|
4124 | n/a | const char *s; |
---|
4125 | n/a | |
---|
4126 | n/a | /* Replace the DeprecationWarning exception with a SyntaxError |
---|
4127 | n/a | to get a more accurate error report */ |
---|
4128 | n/a | PyErr_Clear(); |
---|
4129 | n/a | |
---|
4130 | n/a | s = PyUnicode_AsUTF8(msg); |
---|
4131 | n/a | if (s != NULL) { |
---|
4132 | n/a | ast_error(c, n, s); |
---|
4133 | n/a | } |
---|
4134 | n/a | Py_DECREF(msg); |
---|
4135 | n/a | return -1; |
---|
4136 | n/a | } |
---|
4137 | n/a | Py_DECREF(msg); |
---|
4138 | n/a | return 0; |
---|
4139 | n/a | } |
---|
4140 | n/a | |
---|
4141 | n/a | static PyObject * |
---|
4142 | n/a | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
---|
4143 | n/a | size_t len) |
---|
4144 | n/a | { |
---|
4145 | n/a | PyObject *v, *u; |
---|
4146 | n/a | char *buf; |
---|
4147 | n/a | char *p; |
---|
4148 | n/a | const char *end; |
---|
4149 | n/a | |
---|
4150 | n/a | /* check for integer overflow */ |
---|
4151 | n/a | if (len > SIZE_MAX / 6) |
---|
4152 | n/a | return NULL; |
---|
4153 | n/a | /* "รยค" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
---|
4154 | n/a | "\รยค" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
---|
4155 | n/a | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
---|
4156 | n/a | if (u == NULL) |
---|
4157 | n/a | return NULL; |
---|
4158 | n/a | p = buf = PyBytes_AsString(u); |
---|
4159 | n/a | end = s + len; |
---|
4160 | n/a | while (s < end) { |
---|
4161 | n/a | if (*s == '\\') { |
---|
4162 | n/a | *p++ = *s++; |
---|
4163 | n/a | if (*s & 0x80) { |
---|
4164 | n/a | strcpy(p, "u005c"); |
---|
4165 | n/a | p += 5; |
---|
4166 | n/a | } |
---|
4167 | n/a | } |
---|
4168 | n/a | if (*s & 0x80) { /* XXX inefficient */ |
---|
4169 | n/a | PyObject *w; |
---|
4170 | n/a | int kind; |
---|
4171 | n/a | void *data; |
---|
4172 | n/a | Py_ssize_t len, i; |
---|
4173 | n/a | w = decode_utf8(c, &s, end); |
---|
4174 | n/a | if (w == NULL) { |
---|
4175 | n/a | Py_DECREF(u); |
---|
4176 | n/a | return NULL; |
---|
4177 | n/a | } |
---|
4178 | n/a | kind = PyUnicode_KIND(w); |
---|
4179 | n/a | data = PyUnicode_DATA(w); |
---|
4180 | n/a | len = PyUnicode_GET_LENGTH(w); |
---|
4181 | n/a | for (i = 0; i < len; i++) { |
---|
4182 | n/a | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
---|
4183 | n/a | sprintf(p, "\\U%08x", chr); |
---|
4184 | n/a | p += 10; |
---|
4185 | n/a | } |
---|
4186 | n/a | /* Should be impossible to overflow */ |
---|
4187 | n/a | assert(p - buf <= Py_SIZE(u)); |
---|
4188 | n/a | Py_DECREF(w); |
---|
4189 | n/a | } else { |
---|
4190 | n/a | *p++ = *s++; |
---|
4191 | n/a | } |
---|
4192 | n/a | } |
---|
4193 | n/a | len = p - buf; |
---|
4194 | n/a | s = buf; |
---|
4195 | n/a | |
---|
4196 | n/a | const char *first_invalid_escape; |
---|
4197 | n/a | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
---|
4198 | n/a | |
---|
4199 | n/a | if (v != NULL && first_invalid_escape != NULL) { |
---|
4200 | n/a | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
---|
4201 | n/a | /* We have not decref u before because first_invalid_escape points |
---|
4202 | n/a | inside u. */ |
---|
4203 | n/a | Py_XDECREF(u); |
---|
4204 | n/a | Py_DECREF(v); |
---|
4205 | n/a | return NULL; |
---|
4206 | n/a | } |
---|
4207 | n/a | } |
---|
4208 | n/a | Py_XDECREF(u); |
---|
4209 | n/a | return v; |
---|
4210 | n/a | } |
---|
4211 | n/a | |
---|
4212 | n/a | static PyObject * |
---|
4213 | n/a | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
---|
4214 | n/a | size_t len) |
---|
4215 | n/a | { |
---|
4216 | n/a | const char *first_invalid_escape; |
---|
4217 | n/a | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
---|
4218 | n/a | &first_invalid_escape); |
---|
4219 | n/a | if (result == NULL) |
---|
4220 | n/a | return NULL; |
---|
4221 | n/a | |
---|
4222 | n/a | if (first_invalid_escape != NULL) { |
---|
4223 | n/a | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
---|
4224 | n/a | Py_DECREF(result); |
---|
4225 | n/a | return NULL; |
---|
4226 | n/a | } |
---|
4227 | n/a | } |
---|
4228 | n/a | return result; |
---|
4229 | n/a | } |
---|
4230 | n/a | |
---|
4231 | n/a | /* Compile this expression in to an expr_ty. Add parens around the |
---|
4232 | n/a | expression, in order to allow leading spaces in the expression. */ |
---|
4233 | n/a | static expr_ty |
---|
4234 | n/a | fstring_compile_expr(const char *expr_start, const char *expr_end, |
---|
4235 | n/a | struct compiling *c, const node *n) |
---|
4236 | n/a | |
---|
4237 | n/a | { |
---|
4238 | n/a | int all_whitespace = 1; |
---|
4239 | n/a | int kind; |
---|
4240 | n/a | void *data; |
---|
4241 | n/a | PyCompilerFlags cf; |
---|
4242 | n/a | mod_ty mod; |
---|
4243 | n/a | char *str; |
---|
4244 | n/a | PyObject *o; |
---|
4245 | n/a | Py_ssize_t len; |
---|
4246 | n/a | Py_ssize_t i; |
---|
4247 | n/a | |
---|
4248 | n/a | assert(expr_end >= expr_start); |
---|
4249 | n/a | assert(*(expr_start-1) == '{'); |
---|
4250 | n/a | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
---|
4251 | n/a | |
---|
4252 | n/a | /* We know there are no escapes here, because backslashes are not allowed, |
---|
4253 | n/a | and we know it's utf-8 encoded (per PEP 263). But, in order to check |
---|
4254 | n/a | that each char is not whitespace, we need to decode it to unicode. |
---|
4255 | n/a | Which is unfortunate, but such is life. */ |
---|
4256 | n/a | |
---|
4257 | n/a | /* If the substring is all whitespace, it's an error. We need to catch |
---|
4258 | n/a | this here, and not when we call PyParser_ASTFromString, because turning |
---|
4259 | n/a | the expression '' in to '()' would go from being invalid to valid. */ |
---|
4260 | n/a | /* Note that this code says an empty string is all whitespace. That's |
---|
4261 | n/a | important. There's a test for it: f'{}'. */ |
---|
4262 | n/a | o = PyUnicode_DecodeUTF8(expr_start, expr_end-expr_start, NULL); |
---|
4263 | n/a | if (o == NULL) |
---|
4264 | n/a | return NULL; |
---|
4265 | n/a | len = PyUnicode_GET_LENGTH(o); |
---|
4266 | n/a | kind = PyUnicode_KIND(o); |
---|
4267 | n/a | data = PyUnicode_DATA(o); |
---|
4268 | n/a | for (i = 0; i < len; i++) { |
---|
4269 | n/a | if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
---|
4270 | n/a | all_whitespace = 0; |
---|
4271 | n/a | break; |
---|
4272 | n/a | } |
---|
4273 | n/a | } |
---|
4274 | n/a | Py_DECREF(o); |
---|
4275 | n/a | if (all_whitespace) { |
---|
4276 | n/a | ast_error(c, n, "f-string: empty expression not allowed"); |
---|
4277 | n/a | return NULL; |
---|
4278 | n/a | } |
---|
4279 | n/a | |
---|
4280 | n/a | /* Reuse len to be the length of the utf-8 input string. */ |
---|
4281 | n/a | len = expr_end - expr_start; |
---|
4282 | n/a | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
---|
4283 | n/a | str = PyMem_RawMalloc(len + 3); |
---|
4284 | n/a | if (str == NULL) |
---|
4285 | n/a | return NULL; |
---|
4286 | n/a | |
---|
4287 | n/a | str[0] = '('; |
---|
4288 | n/a | memcpy(str+1, expr_start, len); |
---|
4289 | n/a | str[len+1] = ')'; |
---|
4290 | n/a | str[len+2] = 0; |
---|
4291 | n/a | |
---|
4292 | n/a | cf.cf_flags = PyCF_ONLY_AST; |
---|
4293 | n/a | mod = PyParser_ASTFromString(str, "<fstring>", |
---|
4294 | n/a | Py_eval_input, &cf, c->c_arena); |
---|
4295 | n/a | PyMem_RawFree(str); |
---|
4296 | n/a | if (!mod) |
---|
4297 | n/a | return NULL; |
---|
4298 | n/a | return mod->v.Expression.body; |
---|
4299 | n/a | } |
---|
4300 | n/a | |
---|
4301 | n/a | /* Return -1 on error. |
---|
4302 | n/a | |
---|
4303 | n/a | Return 0 if we reached the end of the literal. |
---|
4304 | n/a | |
---|
4305 | n/a | Return 1 if we haven't reached the end of the literal, but we want |
---|
4306 | n/a | the caller to process the literal up to this point. Used for |
---|
4307 | n/a | doubled braces. |
---|
4308 | n/a | */ |
---|
4309 | n/a | static int |
---|
4310 | n/a | fstring_find_literal(const char **str, const char *end, int raw, |
---|
4311 | n/a | PyObject **literal, int recurse_lvl, |
---|
4312 | n/a | struct compiling *c, const node *n) |
---|
4313 | n/a | { |
---|
4314 | n/a | /* Get any literal string. It ends when we hit an un-doubled left |
---|
4315 | n/a | brace (which isn't part of a unicode name escape such as |
---|
4316 | n/a | "\N{EULER CONSTANT}"), or the end of the string. */ |
---|
4317 | n/a | |
---|
4318 | n/a | const char *literal_start = *str; |
---|
4319 | n/a | const char *literal_end; |
---|
4320 | n/a | int in_named_escape = 0; |
---|
4321 | n/a | int result = 0; |
---|
4322 | n/a | |
---|
4323 | n/a | assert(*literal == NULL); |
---|
4324 | n/a | for (; *str < end; (*str)++) { |
---|
4325 | n/a | char ch = **str; |
---|
4326 | n/a | if (!in_named_escape && ch == '{' && (*str)-literal_start >= 2 && |
---|
4327 | n/a | *(*str-2) == '\\' && *(*str-1) == 'N') { |
---|
4328 | n/a | in_named_escape = 1; |
---|
4329 | n/a | } else if (in_named_escape && ch == '}') { |
---|
4330 | n/a | in_named_escape = 0; |
---|
4331 | n/a | } else if (ch == '{' || ch == '}') { |
---|
4332 | n/a | /* Check for doubled braces, but only at the top level. If |
---|
4333 | n/a | we checked at every level, then f'{0:{3}}' would fail |
---|
4334 | n/a | with the two closing braces. */ |
---|
4335 | n/a | if (recurse_lvl == 0) { |
---|
4336 | n/a | if (*str+1 < end && *(*str+1) == ch) { |
---|
4337 | n/a | /* We're going to tell the caller that the literal ends |
---|
4338 | n/a | here, but that they should continue scanning. But also |
---|
4339 | n/a | skip over the second brace when we resume scanning. */ |
---|
4340 | n/a | literal_end = *str+1; |
---|
4341 | n/a | *str += 2; |
---|
4342 | n/a | result = 1; |
---|
4343 | n/a | goto done; |
---|
4344 | n/a | } |
---|
4345 | n/a | |
---|
4346 | n/a | /* Where a single '{' is the start of a new expression, a |
---|
4347 | n/a | single '}' is not allowed. */ |
---|
4348 | n/a | if (ch == '}') { |
---|
4349 | n/a | ast_error(c, n, "f-string: single '}' is not allowed"); |
---|
4350 | n/a | return -1; |
---|
4351 | n/a | } |
---|
4352 | n/a | } |
---|
4353 | n/a | /* We're either at a '{', which means we're starting another |
---|
4354 | n/a | expression; or a '}', which means we're at the end of this |
---|
4355 | n/a | f-string (for a nested format_spec). */ |
---|
4356 | n/a | break; |
---|
4357 | n/a | } |
---|
4358 | n/a | } |
---|
4359 | n/a | literal_end = *str; |
---|
4360 | n/a | assert(*str <= end); |
---|
4361 | n/a | assert(*str == end || **str == '{' || **str == '}'); |
---|
4362 | n/a | done: |
---|
4363 | n/a | if (literal_start != literal_end) { |
---|
4364 | n/a | if (raw) |
---|
4365 | n/a | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
---|
4366 | n/a | literal_end-literal_start, |
---|
4367 | n/a | NULL, NULL); |
---|
4368 | n/a | else |
---|
4369 | n/a | *literal = decode_unicode_with_escapes(c, n, literal_start, |
---|
4370 | n/a | literal_end-literal_start); |
---|
4371 | n/a | if (!*literal) |
---|
4372 | n/a | return -1; |
---|
4373 | n/a | } |
---|
4374 | n/a | return result; |
---|
4375 | n/a | } |
---|
4376 | n/a | |
---|
4377 | n/a | /* Forward declaration because parsing is recursive. */ |
---|
4378 | n/a | static expr_ty |
---|
4379 | n/a | fstring_parse(const char **str, const char *end, int raw, int recurse_lvl, |
---|
4380 | n/a | struct compiling *c, const node *n); |
---|
4381 | n/a | |
---|
4382 | n/a | /* Parse the f-string at *str, ending at end. We know *str starts an |
---|
4383 | n/a | expression (so it must be a '{'). Returns the FormattedValue node, |
---|
4384 | n/a | which includes the expression, conversion character, and |
---|
4385 | n/a | format_spec expression. |
---|
4386 | n/a | |
---|
4387 | n/a | Note that I don't do a perfect job here: I don't make sure that a |
---|
4388 | n/a | closing brace doesn't match an opening paren, for example. It |
---|
4389 | n/a | doesn't need to error on all invalid expressions, just correctly |
---|
4390 | n/a | find the end of all valid ones. Any errors inside the expression |
---|
4391 | n/a | will be caught when we parse it later. */ |
---|
4392 | n/a | static int |
---|
4393 | n/a | fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, |
---|
4394 | n/a | expr_ty *expression, struct compiling *c, const node *n) |
---|
4395 | n/a | { |
---|
4396 | n/a | /* Return -1 on error, else 0. */ |
---|
4397 | n/a | |
---|
4398 | n/a | const char *expr_start; |
---|
4399 | n/a | const char *expr_end; |
---|
4400 | n/a | expr_ty simple_expression; |
---|
4401 | n/a | expr_ty format_spec = NULL; /* Optional format specifier. */ |
---|
4402 | n/a | int conversion = -1; /* The conversion char. -1 if not specified. */ |
---|
4403 | n/a | |
---|
4404 | n/a | /* 0 if we're not in a string, else the quote char we're trying to |
---|
4405 | n/a | match (single or double quote). */ |
---|
4406 | n/a | char quote_char = 0; |
---|
4407 | n/a | |
---|
4408 | n/a | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
---|
4409 | n/a | int string_type = 0; |
---|
4410 | n/a | |
---|
4411 | n/a | /* Keep track of nesting level for braces/parens/brackets in |
---|
4412 | n/a | expressions. */ |
---|
4413 | n/a | Py_ssize_t nested_depth = 0; |
---|
4414 | n/a | |
---|
4415 | n/a | /* Can only nest one level deep. */ |
---|
4416 | n/a | if (recurse_lvl >= 2) { |
---|
4417 | n/a | ast_error(c, n, "f-string: expressions nested too deeply"); |
---|
4418 | n/a | return -1; |
---|
4419 | n/a | } |
---|
4420 | n/a | |
---|
4421 | n/a | /* The first char must be a left brace, or we wouldn't have gotten |
---|
4422 | n/a | here. Skip over it. */ |
---|
4423 | n/a | assert(**str == '{'); |
---|
4424 | n/a | *str += 1; |
---|
4425 | n/a | |
---|
4426 | n/a | expr_start = *str; |
---|
4427 | n/a | for (; *str < end; (*str)++) { |
---|
4428 | n/a | char ch; |
---|
4429 | n/a | |
---|
4430 | n/a | /* Loop invariants. */ |
---|
4431 | n/a | assert(nested_depth >= 0); |
---|
4432 | n/a | assert(*str >= expr_start && *str < end); |
---|
4433 | n/a | if (quote_char) |
---|
4434 | n/a | assert(string_type == 1 || string_type == 3); |
---|
4435 | n/a | else |
---|
4436 | n/a | assert(string_type == 0); |
---|
4437 | n/a | |
---|
4438 | n/a | ch = **str; |
---|
4439 | n/a | /* Nowhere inside an expression is a backslash allowed. */ |
---|
4440 | n/a | if (ch == '\\') { |
---|
4441 | n/a | /* Error: can't include a backslash character, inside |
---|
4442 | n/a | parens or strings or not. */ |
---|
4443 | n/a | ast_error(c, n, "f-string expression part " |
---|
4444 | n/a | "cannot include a backslash"); |
---|
4445 | n/a | return -1; |
---|
4446 | n/a | } |
---|
4447 | n/a | if (quote_char) { |
---|
4448 | n/a | /* We're inside a string. See if we're at the end. */ |
---|
4449 | n/a | /* This code needs to implement the same non-error logic |
---|
4450 | n/a | as tok_get from tokenizer.c, at the letter_quote |
---|
4451 | n/a | label. To actually share that code would be a |
---|
4452 | n/a | nightmare. But, it's unlikely to change and is small, |
---|
4453 | n/a | so duplicate it here. Note we don't need to catch all |
---|
4454 | n/a | of the errors, since they'll be caught when parsing the |
---|
4455 | n/a | expression. We just need to match the non-error |
---|
4456 | n/a | cases. Thus we can ignore \n in single-quoted strings, |
---|
4457 | n/a | for example. Or non-terminated strings. */ |
---|
4458 | n/a | if (ch == quote_char) { |
---|
4459 | n/a | /* Does this match the string_type (single or triple |
---|
4460 | n/a | quoted)? */ |
---|
4461 | n/a | if (string_type == 3) { |
---|
4462 | n/a | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
---|
4463 | n/a | /* We're at the end of a triple quoted string. */ |
---|
4464 | n/a | *str += 2; |
---|
4465 | n/a | string_type = 0; |
---|
4466 | n/a | quote_char = 0; |
---|
4467 | n/a | continue; |
---|
4468 | n/a | } |
---|
4469 | n/a | } else { |
---|
4470 | n/a | /* We're at the end of a normal string. */ |
---|
4471 | n/a | quote_char = 0; |
---|
4472 | n/a | string_type = 0; |
---|
4473 | n/a | continue; |
---|
4474 | n/a | } |
---|
4475 | n/a | } |
---|
4476 | n/a | } else if (ch == '\'' || ch == '"') { |
---|
4477 | n/a | /* Is this a triple quoted string? */ |
---|
4478 | n/a | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
---|
4479 | n/a | string_type = 3; |
---|
4480 | n/a | *str += 2; |
---|
4481 | n/a | } else { |
---|
4482 | n/a | /* Start of a normal string. */ |
---|
4483 | n/a | string_type = 1; |
---|
4484 | n/a | } |
---|
4485 | n/a | /* Start looking for the end of the string. */ |
---|
4486 | n/a | quote_char = ch; |
---|
4487 | n/a | } else if (ch == '[' || ch == '{' || ch == '(') { |
---|
4488 | n/a | nested_depth++; |
---|
4489 | n/a | } else if (nested_depth != 0 && |
---|
4490 | n/a | (ch == ']' || ch == '}' || ch == ')')) { |
---|
4491 | n/a | nested_depth--; |
---|
4492 | n/a | } else if (ch == '#') { |
---|
4493 | n/a | /* Error: can't include a comment character, inside parens |
---|
4494 | n/a | or not. */ |
---|
4495 | n/a | ast_error(c, n, "f-string expression part cannot include '#'"); |
---|
4496 | n/a | return -1; |
---|
4497 | n/a | } else if (nested_depth == 0 && |
---|
4498 | n/a | (ch == '!' || ch == ':' || ch == '}')) { |
---|
4499 | n/a | /* First, test for the special case of "!=". Since '=' is |
---|
4500 | n/a | not an allowed conversion character, nothing is lost in |
---|
4501 | n/a | this test. */ |
---|
4502 | n/a | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
---|
4503 | n/a | /* This isn't a conversion character, just continue. */ |
---|
4504 | n/a | continue; |
---|
4505 | n/a | } |
---|
4506 | n/a | /* Normal way out of this loop. */ |
---|
4507 | n/a | break; |
---|
4508 | n/a | } else { |
---|
4509 | n/a | /* Just consume this char and loop around. */ |
---|
4510 | n/a | } |
---|
4511 | n/a | } |
---|
4512 | n/a | expr_end = *str; |
---|
4513 | n/a | /* If we leave this loop in a string or with mismatched parens, we |
---|
4514 | n/a | don't care. We'll get a syntax error when compiling the |
---|
4515 | n/a | expression. But, we can produce a better error message, so |
---|
4516 | n/a | let's just do that.*/ |
---|
4517 | n/a | if (quote_char) { |
---|
4518 | n/a | ast_error(c, n, "f-string: unterminated string"); |
---|
4519 | n/a | return -1; |
---|
4520 | n/a | } |
---|
4521 | n/a | if (nested_depth) { |
---|
4522 | n/a | ast_error(c, n, "f-string: mismatched '(', '{', or '['"); |
---|
4523 | n/a | return -1; |
---|
4524 | n/a | } |
---|
4525 | n/a | |
---|
4526 | n/a | if (*str >= end) |
---|
4527 | n/a | goto unexpected_end_of_string; |
---|
4528 | n/a | |
---|
4529 | n/a | /* Compile the expression as soon as possible, so we show errors |
---|
4530 | n/a | related to the expression before errors related to the |
---|
4531 | n/a | conversion or format_spec. */ |
---|
4532 | n/a | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
---|
4533 | n/a | if (!simple_expression) |
---|
4534 | n/a | return -1; |
---|
4535 | n/a | |
---|
4536 | n/a | /* Check for a conversion char, if present. */ |
---|
4537 | n/a | if (**str == '!') { |
---|
4538 | n/a | *str += 1; |
---|
4539 | n/a | if (*str >= end) |
---|
4540 | n/a | goto unexpected_end_of_string; |
---|
4541 | n/a | |
---|
4542 | n/a | conversion = **str; |
---|
4543 | n/a | *str += 1; |
---|
4544 | n/a | |
---|
4545 | n/a | /* Validate the conversion. */ |
---|
4546 | n/a | if (!(conversion == 's' || conversion == 'r' |
---|
4547 | n/a | || conversion == 'a')) { |
---|
4548 | n/a | ast_error(c, n, "f-string: invalid conversion character: " |
---|
4549 | n/a | "expected 's', 'r', or 'a'"); |
---|
4550 | n/a | return -1; |
---|
4551 | n/a | } |
---|
4552 | n/a | } |
---|
4553 | n/a | |
---|
4554 | n/a | /* Check for the format spec, if present. */ |
---|
4555 | n/a | if (*str >= end) |
---|
4556 | n/a | goto unexpected_end_of_string; |
---|
4557 | n/a | if (**str == ':') { |
---|
4558 | n/a | *str += 1; |
---|
4559 | n/a | if (*str >= end) |
---|
4560 | n/a | goto unexpected_end_of_string; |
---|
4561 | n/a | |
---|
4562 | n/a | /* Parse the format spec. */ |
---|
4563 | n/a | format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n); |
---|
4564 | n/a | if (!format_spec) |
---|
4565 | n/a | return -1; |
---|
4566 | n/a | } |
---|
4567 | n/a | |
---|
4568 | n/a | if (*str >= end || **str != '}') |
---|
4569 | n/a | goto unexpected_end_of_string; |
---|
4570 | n/a | |
---|
4571 | n/a | /* We're at a right brace. Consume it. */ |
---|
4572 | n/a | assert(*str < end); |
---|
4573 | n/a | assert(**str == '}'); |
---|
4574 | n/a | *str += 1; |
---|
4575 | n/a | |
---|
4576 | n/a | /* And now create the FormattedValue node that represents this |
---|
4577 | n/a | entire expression with the conversion and format spec. */ |
---|
4578 | n/a | *expression = FormattedValue(simple_expression, conversion, |
---|
4579 | n/a | format_spec, LINENO(n), n->n_col_offset, |
---|
4580 | n/a | c->c_arena); |
---|
4581 | n/a | if (!*expression) |
---|
4582 | n/a | return -1; |
---|
4583 | n/a | |
---|
4584 | n/a | return 0; |
---|
4585 | n/a | |
---|
4586 | n/a | unexpected_end_of_string: |
---|
4587 | n/a | ast_error(c, n, "f-string: expecting '}'"); |
---|
4588 | n/a | return -1; |
---|
4589 | n/a | } |
---|
4590 | n/a | |
---|
4591 | n/a | /* Return -1 on error. |
---|
4592 | n/a | |
---|
4593 | n/a | Return 0 if we have a literal (possible zero length) and an |
---|
4594 | n/a | expression (zero length if at the end of the string. |
---|
4595 | n/a | |
---|
4596 | n/a | Return 1 if we have a literal, but no expression, and we want the |
---|
4597 | n/a | caller to call us again. This is used to deal with doubled |
---|
4598 | n/a | braces. |
---|
4599 | n/a | |
---|
4600 | n/a | When called multiple times on the string 'a{{b{0}c', this function |
---|
4601 | n/a | will return: |
---|
4602 | n/a | |
---|
4603 | n/a | 1. the literal 'a{' with no expression, and a return value |
---|
4604 | n/a | of 1. Despite the fact that there's no expression, the return |
---|
4605 | n/a | value of 1 means we're not finished yet. |
---|
4606 | n/a | |
---|
4607 | n/a | 2. the literal 'b' and the expression '0', with a return value of |
---|
4608 | n/a | 0. The fact that there's an expression means we're not finished. |
---|
4609 | n/a | |
---|
4610 | n/a | 3. literal 'c' with no expression and a return value of 0. The |
---|
4611 | n/a | combination of the return value of 0 with no expression means |
---|
4612 | n/a | we're finished. |
---|
4613 | n/a | */ |
---|
4614 | n/a | static int |
---|
4615 | n/a | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
---|
4616 | n/a | int recurse_lvl, PyObject **literal, |
---|
4617 | n/a | expr_ty *expression, |
---|
4618 | n/a | struct compiling *c, const node *n) |
---|
4619 | n/a | { |
---|
4620 | n/a | int result; |
---|
4621 | n/a | |
---|
4622 | n/a | assert(*literal == NULL && *expression == NULL); |
---|
4623 | n/a | |
---|
4624 | n/a | /* Get any literal string. */ |
---|
4625 | n/a | result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n); |
---|
4626 | n/a | if (result < 0) |
---|
4627 | n/a | goto error; |
---|
4628 | n/a | |
---|
4629 | n/a | assert(result == 0 || result == 1); |
---|
4630 | n/a | |
---|
4631 | n/a | if (result == 1) |
---|
4632 | n/a | /* We have a literal, but don't look at the expression. */ |
---|
4633 | n/a | return 1; |
---|
4634 | n/a | |
---|
4635 | n/a | if (*str >= end || **str == '}') |
---|
4636 | n/a | /* We're at the end of the string or the end of a nested |
---|
4637 | n/a | f-string: no expression. The top-level error case where we |
---|
4638 | n/a | expect to be at the end of the string but we're at a '}' is |
---|
4639 | n/a | handled later. */ |
---|
4640 | n/a | return 0; |
---|
4641 | n/a | |
---|
4642 | n/a | /* We must now be the start of an expression, on a '{'. */ |
---|
4643 | n/a | assert(**str == '{'); |
---|
4644 | n/a | |
---|
4645 | n/a | if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0) |
---|
4646 | n/a | goto error; |
---|
4647 | n/a | |
---|
4648 | n/a | return 0; |
---|
4649 | n/a | |
---|
4650 | n/a | error: |
---|
4651 | n/a | Py_CLEAR(*literal); |
---|
4652 | n/a | return -1; |
---|
4653 | n/a | } |
---|
4654 | n/a | |
---|
4655 | n/a | #define EXPRLIST_N_CACHED 64 |
---|
4656 | n/a | |
---|
4657 | n/a | typedef struct { |
---|
4658 | n/a | /* Incrementally build an array of expr_ty, so be used in an |
---|
4659 | n/a | asdl_seq. Cache some small but reasonably sized number of |
---|
4660 | n/a | expr_ty's, and then after that start dynamically allocating, |
---|
4661 | n/a | doubling the number allocated each time. Note that the f-string |
---|
4662 | n/a | f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one |
---|
4663 | n/a | Str for the literal 'a'. So you add expr_ty's about twice as |
---|
4664 | n/a | fast as you add exressions in an f-string. */ |
---|
4665 | n/a | |
---|
4666 | n/a | Py_ssize_t allocated; /* Number we've allocated. */ |
---|
4667 | n/a | Py_ssize_t size; /* Number we've used. */ |
---|
4668 | n/a | expr_ty *p; /* Pointer to the memory we're actually |
---|
4669 | n/a | using. Will point to 'data' until we |
---|
4670 | n/a | start dynamically allocating. */ |
---|
4671 | n/a | expr_ty data[EXPRLIST_N_CACHED]; |
---|
4672 | n/a | } ExprList; |
---|
4673 | n/a | |
---|
4674 | n/a | #ifdef NDEBUG |
---|
4675 | n/a | #define ExprList_check_invariants(l) |
---|
4676 | n/a | #else |
---|
4677 | n/a | static void |
---|
4678 | n/a | ExprList_check_invariants(ExprList *l) |
---|
4679 | n/a | { |
---|
4680 | n/a | /* Check our invariants. Make sure this object is "live", and |
---|
4681 | n/a | hasn't been deallocated. */ |
---|
4682 | n/a | assert(l->size >= 0); |
---|
4683 | n/a | assert(l->p != NULL); |
---|
4684 | n/a | if (l->size <= EXPRLIST_N_CACHED) |
---|
4685 | n/a | assert(l->data == l->p); |
---|
4686 | n/a | } |
---|
4687 | n/a | #endif |
---|
4688 | n/a | |
---|
4689 | n/a | static void |
---|
4690 | n/a | ExprList_Init(ExprList *l) |
---|
4691 | n/a | { |
---|
4692 | n/a | l->allocated = EXPRLIST_N_CACHED; |
---|
4693 | n/a | l->size = 0; |
---|
4694 | n/a | |
---|
4695 | n/a | /* Until we start allocating dynamically, p points to data. */ |
---|
4696 | n/a | l->p = l->data; |
---|
4697 | n/a | |
---|
4698 | n/a | ExprList_check_invariants(l); |
---|
4699 | n/a | } |
---|
4700 | n/a | |
---|
4701 | n/a | static int |
---|
4702 | n/a | ExprList_Append(ExprList *l, expr_ty exp) |
---|
4703 | n/a | { |
---|
4704 | n/a | ExprList_check_invariants(l); |
---|
4705 | n/a | if (l->size >= l->allocated) { |
---|
4706 | n/a | /* We need to alloc (or realloc) the memory. */ |
---|
4707 | n/a | Py_ssize_t new_size = l->allocated * 2; |
---|
4708 | n/a | |
---|
4709 | n/a | /* See if we've ever allocated anything dynamically. */ |
---|
4710 | n/a | if (l->p == l->data) { |
---|
4711 | n/a | Py_ssize_t i; |
---|
4712 | n/a | /* We're still using the cached data. Switch to |
---|
4713 | n/a | alloc-ing. */ |
---|
4714 | n/a | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
---|
4715 | n/a | if (!l->p) |
---|
4716 | n/a | return -1; |
---|
4717 | n/a | /* Copy the cached data into the new buffer. */ |
---|
4718 | n/a | for (i = 0; i < l->size; i++) |
---|
4719 | n/a | l->p[i] = l->data[i]; |
---|
4720 | n/a | } else { |
---|
4721 | n/a | /* Just realloc. */ |
---|
4722 | n/a | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
---|
4723 | n/a | if (!tmp) { |
---|
4724 | n/a | PyMem_RawFree(l->p); |
---|
4725 | n/a | l->p = NULL; |
---|
4726 | n/a | return -1; |
---|
4727 | n/a | } |
---|
4728 | n/a | l->p = tmp; |
---|
4729 | n/a | } |
---|
4730 | n/a | |
---|
4731 | n/a | l->allocated = new_size; |
---|
4732 | n/a | assert(l->allocated == 2 * l->size); |
---|
4733 | n/a | } |
---|
4734 | n/a | |
---|
4735 | n/a | l->p[l->size++] = exp; |
---|
4736 | n/a | |
---|
4737 | n/a | ExprList_check_invariants(l); |
---|
4738 | n/a | return 0; |
---|
4739 | n/a | } |
---|
4740 | n/a | |
---|
4741 | n/a | static void |
---|
4742 | n/a | ExprList_Dealloc(ExprList *l) |
---|
4743 | n/a | { |
---|
4744 | n/a | ExprList_check_invariants(l); |
---|
4745 | n/a | |
---|
4746 | n/a | /* If there's been an error, or we've never dynamically allocated, |
---|
4747 | n/a | do nothing. */ |
---|
4748 | n/a | if (!l->p || l->p == l->data) { |
---|
4749 | n/a | /* Do nothing. */ |
---|
4750 | n/a | } else { |
---|
4751 | n/a | /* We have dynamically allocated. Free the memory. */ |
---|
4752 | n/a | PyMem_RawFree(l->p); |
---|
4753 | n/a | } |
---|
4754 | n/a | l->p = NULL; |
---|
4755 | n/a | l->size = -1; |
---|
4756 | n/a | } |
---|
4757 | n/a | |
---|
4758 | n/a | static asdl_seq * |
---|
4759 | n/a | ExprList_Finish(ExprList *l, PyArena *arena) |
---|
4760 | n/a | { |
---|
4761 | n/a | asdl_seq *seq; |
---|
4762 | n/a | |
---|
4763 | n/a | ExprList_check_invariants(l); |
---|
4764 | n/a | |
---|
4765 | n/a | /* Allocate the asdl_seq and copy the expressions in to it. */ |
---|
4766 | n/a | seq = _Py_asdl_seq_new(l->size, arena); |
---|
4767 | n/a | if (seq) { |
---|
4768 | n/a | Py_ssize_t i; |
---|
4769 | n/a | for (i = 0; i < l->size; i++) |
---|
4770 | n/a | asdl_seq_SET(seq, i, l->p[i]); |
---|
4771 | n/a | } |
---|
4772 | n/a | ExprList_Dealloc(l); |
---|
4773 | n/a | return seq; |
---|
4774 | n/a | } |
---|
4775 | n/a | |
---|
4776 | n/a | /* The FstringParser is designed to add a mix of strings and |
---|
4777 | n/a | f-strings, and concat them together as needed. Ultimately, it |
---|
4778 | n/a | generates an expr_ty. */ |
---|
4779 | n/a | typedef struct { |
---|
4780 | n/a | PyObject *last_str; |
---|
4781 | n/a | ExprList expr_list; |
---|
4782 | n/a | int fmode; |
---|
4783 | n/a | } FstringParser; |
---|
4784 | n/a | |
---|
4785 | n/a | #ifdef NDEBUG |
---|
4786 | n/a | #define FstringParser_check_invariants(state) |
---|
4787 | n/a | #else |
---|
4788 | n/a | static void |
---|
4789 | n/a | FstringParser_check_invariants(FstringParser *state) |
---|
4790 | n/a | { |
---|
4791 | n/a | if (state->last_str) |
---|
4792 | n/a | assert(PyUnicode_CheckExact(state->last_str)); |
---|
4793 | n/a | ExprList_check_invariants(&state->expr_list); |
---|
4794 | n/a | } |
---|
4795 | n/a | #endif |
---|
4796 | n/a | |
---|
4797 | n/a | static void |
---|
4798 | n/a | FstringParser_Init(FstringParser *state) |
---|
4799 | n/a | { |
---|
4800 | n/a | state->last_str = NULL; |
---|
4801 | n/a | state->fmode = 0; |
---|
4802 | n/a | ExprList_Init(&state->expr_list); |
---|
4803 | n/a | FstringParser_check_invariants(state); |
---|
4804 | n/a | } |
---|
4805 | n/a | |
---|
4806 | n/a | static void |
---|
4807 | n/a | FstringParser_Dealloc(FstringParser *state) |
---|
4808 | n/a | { |
---|
4809 | n/a | FstringParser_check_invariants(state); |
---|
4810 | n/a | |
---|
4811 | n/a | Py_XDECREF(state->last_str); |
---|
4812 | n/a | ExprList_Dealloc(&state->expr_list); |
---|
4813 | n/a | } |
---|
4814 | n/a | |
---|
4815 | n/a | /* Make a Str node, but decref the PyUnicode object being added. */ |
---|
4816 | n/a | static expr_ty |
---|
4817 | n/a | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
---|
4818 | n/a | { |
---|
4819 | n/a | PyObject *s = *str; |
---|
4820 | n/a | *str = NULL; |
---|
4821 | n/a | assert(PyUnicode_CheckExact(s)); |
---|
4822 | n/a | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
---|
4823 | n/a | Py_DECREF(s); |
---|
4824 | n/a | return NULL; |
---|
4825 | n/a | } |
---|
4826 | n/a | return Str(s, LINENO(n), n->n_col_offset, c->c_arena); |
---|
4827 | n/a | } |
---|
4828 | n/a | |
---|
4829 | n/a | /* Add a non-f-string (that is, a regular literal string). str is |
---|
4830 | n/a | decref'd. */ |
---|
4831 | n/a | static int |
---|
4832 | n/a | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
---|
4833 | n/a | { |
---|
4834 | n/a | FstringParser_check_invariants(state); |
---|
4835 | n/a | |
---|
4836 | n/a | assert(PyUnicode_CheckExact(str)); |
---|
4837 | n/a | |
---|
4838 | n/a | if (PyUnicode_GET_LENGTH(str) == 0) { |
---|
4839 | n/a | Py_DECREF(str); |
---|
4840 | n/a | return 0; |
---|
4841 | n/a | } |
---|
4842 | n/a | |
---|
4843 | n/a | if (!state->last_str) { |
---|
4844 | n/a | /* We didn't have a string before, so just remember this one. */ |
---|
4845 | n/a | state->last_str = str; |
---|
4846 | n/a | } else { |
---|
4847 | n/a | /* Concatenate this with the previous string. */ |
---|
4848 | n/a | PyUnicode_AppendAndDel(&state->last_str, str); |
---|
4849 | n/a | if (!state->last_str) |
---|
4850 | n/a | return -1; |
---|
4851 | n/a | } |
---|
4852 | n/a | FstringParser_check_invariants(state); |
---|
4853 | n/a | return 0; |
---|
4854 | n/a | } |
---|
4855 | n/a | |
---|
4856 | n/a | /* Parse an f-string. The f-string is in *str to end, with no |
---|
4857 | n/a | 'f' or quotes. */ |
---|
4858 | n/a | static int |
---|
4859 | n/a | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
---|
4860 | n/a | const char *end, int raw, int recurse_lvl, |
---|
4861 | n/a | struct compiling *c, const node *n) |
---|
4862 | n/a | { |
---|
4863 | n/a | FstringParser_check_invariants(state); |
---|
4864 | n/a | state->fmode = 1; |
---|
4865 | n/a | |
---|
4866 | n/a | /* Parse the f-string. */ |
---|
4867 | n/a | while (1) { |
---|
4868 | n/a | PyObject *literal = NULL; |
---|
4869 | n/a | expr_ty expression = NULL; |
---|
4870 | n/a | |
---|
4871 | n/a | /* If there's a zero length literal in front of the |
---|
4872 | n/a | expression, literal will be NULL. If we're at the end of |
---|
4873 | n/a | the f-string, expression will be NULL (unless result == 1, |
---|
4874 | n/a | see below). */ |
---|
4875 | n/a | int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl, |
---|
4876 | n/a | &literal, &expression, |
---|
4877 | n/a | c, n); |
---|
4878 | n/a | if (result < 0) |
---|
4879 | n/a | return -1; |
---|
4880 | n/a | |
---|
4881 | n/a | /* Add the literal, if any. */ |
---|
4882 | n/a | if (!literal) { |
---|
4883 | n/a | /* Do nothing. Just leave last_str alone (and possibly |
---|
4884 | n/a | NULL). */ |
---|
4885 | n/a | } else if (!state->last_str) { |
---|
4886 | n/a | state->last_str = literal; |
---|
4887 | n/a | literal = NULL; |
---|
4888 | n/a | } else { |
---|
4889 | n/a | /* We have a literal, concatenate it. */ |
---|
4890 | n/a | assert(PyUnicode_GET_LENGTH(literal) != 0); |
---|
4891 | n/a | if (FstringParser_ConcatAndDel(state, literal) < 0) |
---|
4892 | n/a | return -1; |
---|
4893 | n/a | literal = NULL; |
---|
4894 | n/a | } |
---|
4895 | n/a | assert(!state->last_str || |
---|
4896 | n/a | PyUnicode_GET_LENGTH(state->last_str) != 0); |
---|
4897 | n/a | |
---|
4898 | n/a | /* We've dealt with the literal now. It can't be leaked on further |
---|
4899 | n/a | errors. */ |
---|
4900 | n/a | assert(literal == NULL); |
---|
4901 | n/a | |
---|
4902 | n/a | /* See if we should just loop around to get the next literal |
---|
4903 | n/a | and expression, while ignoring the expression this |
---|
4904 | n/a | time. This is used for un-doubling braces, as an |
---|
4905 | n/a | optimization. */ |
---|
4906 | n/a | if (result == 1) |
---|
4907 | n/a | continue; |
---|
4908 | n/a | |
---|
4909 | n/a | if (!expression) |
---|
4910 | n/a | /* We're done with this f-string. */ |
---|
4911 | n/a | break; |
---|
4912 | n/a | |
---|
4913 | n/a | /* We know we have an expression. Convert any existing string |
---|
4914 | n/a | to a Str node. */ |
---|
4915 | n/a | if (!state->last_str) { |
---|
4916 | n/a | /* Do nothing. No previous literal. */ |
---|
4917 | n/a | } else { |
---|
4918 | n/a | /* Convert the existing last_str literal to a Str node. */ |
---|
4919 | n/a | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
---|
4920 | n/a | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
---|
4921 | n/a | return -1; |
---|
4922 | n/a | } |
---|
4923 | n/a | |
---|
4924 | n/a | if (ExprList_Append(&state->expr_list, expression) < 0) |
---|
4925 | n/a | return -1; |
---|
4926 | n/a | } |
---|
4927 | n/a | |
---|
4928 | n/a | /* If recurse_lvl is zero, then we must be at the end of the |
---|
4929 | n/a | string. Otherwise, we must be at a right brace. */ |
---|
4930 | n/a | |
---|
4931 | n/a | if (recurse_lvl == 0 && *str < end-1) { |
---|
4932 | n/a | ast_error(c, n, "f-string: unexpected end of string"); |
---|
4933 | n/a | return -1; |
---|
4934 | n/a | } |
---|
4935 | n/a | if (recurse_lvl != 0 && **str != '}') { |
---|
4936 | n/a | ast_error(c, n, "f-string: expecting '}'"); |
---|
4937 | n/a | return -1; |
---|
4938 | n/a | } |
---|
4939 | n/a | |
---|
4940 | n/a | FstringParser_check_invariants(state); |
---|
4941 | n/a | return 0; |
---|
4942 | n/a | } |
---|
4943 | n/a | |
---|
4944 | n/a | /* Convert the partial state reflected in last_str and expr_list to an |
---|
4945 | n/a | expr_ty. The expr_ty can be a Str, or a JoinedStr. */ |
---|
4946 | n/a | static expr_ty |
---|
4947 | n/a | FstringParser_Finish(FstringParser *state, struct compiling *c, |
---|
4948 | n/a | const node *n) |
---|
4949 | n/a | { |
---|
4950 | n/a | asdl_seq *seq; |
---|
4951 | n/a | |
---|
4952 | n/a | FstringParser_check_invariants(state); |
---|
4953 | n/a | |
---|
4954 | n/a | /* If we're just a constant string with no expressions, return |
---|
4955 | n/a | that. */ |
---|
4956 | n/a | if (!state->fmode) { |
---|
4957 | n/a | assert(!state->expr_list.size); |
---|
4958 | n/a | if (!state->last_str) { |
---|
4959 | n/a | /* Create a zero length string. */ |
---|
4960 | n/a | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
---|
4961 | n/a | if (!state->last_str) |
---|
4962 | n/a | goto error; |
---|
4963 | n/a | } |
---|
4964 | n/a | return make_str_node_and_del(&state->last_str, c, n); |
---|
4965 | n/a | } |
---|
4966 | n/a | |
---|
4967 | n/a | /* Create a Str node out of last_str, if needed. It will be the |
---|
4968 | n/a | last node in our expression list. */ |
---|
4969 | n/a | if (state->last_str) { |
---|
4970 | n/a | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
---|
4971 | n/a | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
---|
4972 | n/a | goto error; |
---|
4973 | n/a | } |
---|
4974 | n/a | /* This has already been freed. */ |
---|
4975 | n/a | assert(state->last_str == NULL); |
---|
4976 | n/a | |
---|
4977 | n/a | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
---|
4978 | n/a | if (!seq) |
---|
4979 | n/a | goto error; |
---|
4980 | n/a | |
---|
4981 | n/a | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
---|
4982 | n/a | |
---|
4983 | n/a | error: |
---|
4984 | n/a | FstringParser_Dealloc(state); |
---|
4985 | n/a | return NULL; |
---|
4986 | n/a | } |
---|
4987 | n/a | |
---|
4988 | n/a | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
---|
4989 | n/a | at end, parse it into an expr_ty. Return NULL on error. Adjust |
---|
4990 | n/a | str to point past the parsed portion. */ |
---|
4991 | n/a | static expr_ty |
---|
4992 | n/a | fstring_parse(const char **str, const char *end, int raw, int recurse_lvl, |
---|
4993 | n/a | struct compiling *c, const node *n) |
---|
4994 | n/a | { |
---|
4995 | n/a | FstringParser state; |
---|
4996 | n/a | |
---|
4997 | n/a | FstringParser_Init(&state); |
---|
4998 | n/a | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
---|
4999 | n/a | c, n) < 0) { |
---|
5000 | n/a | FstringParser_Dealloc(&state); |
---|
5001 | n/a | return NULL; |
---|
5002 | n/a | } |
---|
5003 | n/a | |
---|
5004 | n/a | return FstringParser_Finish(&state, c, n); |
---|
5005 | n/a | } |
---|
5006 | n/a | |
---|
5007 | n/a | /* n is a Python string literal, including the bracketing quote |
---|
5008 | n/a | characters, and r, b, u, &/or f prefixes (if any), and embedded |
---|
5009 | n/a | escape sequences (if any). parsestr parses it, and sets *result to |
---|
5010 | n/a | decoded Python string object. If the string is an f-string, set |
---|
5011 | n/a | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
---|
5012 | n/a | errors occurred. |
---|
5013 | n/a | */ |
---|
5014 | n/a | static int |
---|
5015 | n/a | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
---|
5016 | n/a | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
---|
5017 | n/a | { |
---|
5018 | n/a | size_t len; |
---|
5019 | n/a | const char *s = STR(n); |
---|
5020 | n/a | int quote = Py_CHARMASK(*s); |
---|
5021 | n/a | int fmode = 0; |
---|
5022 | n/a | *bytesmode = 0; |
---|
5023 | n/a | *rawmode = 0; |
---|
5024 | n/a | *result = NULL; |
---|
5025 | n/a | *fstr = NULL; |
---|
5026 | n/a | if (Py_ISALPHA(quote)) { |
---|
5027 | n/a | while (!*bytesmode || !*rawmode) { |
---|
5028 | n/a | if (quote == 'b' || quote == 'B') { |
---|
5029 | n/a | quote = *++s; |
---|
5030 | n/a | *bytesmode = 1; |
---|
5031 | n/a | } |
---|
5032 | n/a | else if (quote == 'u' || quote == 'U') { |
---|
5033 | n/a | quote = *++s; |
---|
5034 | n/a | } |
---|
5035 | n/a | else if (quote == 'r' || quote == 'R') { |
---|
5036 | n/a | quote = *++s; |
---|
5037 | n/a | *rawmode = 1; |
---|
5038 | n/a | } |
---|
5039 | n/a | else if (quote == 'f' || quote == 'F') { |
---|
5040 | n/a | quote = *++s; |
---|
5041 | n/a | fmode = 1; |
---|
5042 | n/a | } |
---|
5043 | n/a | else { |
---|
5044 | n/a | break; |
---|
5045 | n/a | } |
---|
5046 | n/a | } |
---|
5047 | n/a | } |
---|
5048 | n/a | if (fmode && *bytesmode) { |
---|
5049 | n/a | PyErr_BadInternalCall(); |
---|
5050 | n/a | return -1; |
---|
5051 | n/a | } |
---|
5052 | n/a | if (quote != '\'' && quote != '\"') { |
---|
5053 | n/a | PyErr_BadInternalCall(); |
---|
5054 | n/a | return -1; |
---|
5055 | n/a | } |
---|
5056 | n/a | /* Skip the leading quote char. */ |
---|
5057 | n/a | s++; |
---|
5058 | n/a | len = strlen(s); |
---|
5059 | n/a | if (len > INT_MAX) { |
---|
5060 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
5061 | n/a | "string to parse is too long"); |
---|
5062 | n/a | return -1; |
---|
5063 | n/a | } |
---|
5064 | n/a | if (s[--len] != quote) { |
---|
5065 | n/a | /* Last quote char must match the first. */ |
---|
5066 | n/a | PyErr_BadInternalCall(); |
---|
5067 | n/a | return -1; |
---|
5068 | n/a | } |
---|
5069 | n/a | if (len >= 4 && s[0] == quote && s[1] == quote) { |
---|
5070 | n/a | /* A triple quoted string. We've already skipped one quote at |
---|
5071 | n/a | the start and one at the end of the string. Now skip the |
---|
5072 | n/a | two at the start. */ |
---|
5073 | n/a | s += 2; |
---|
5074 | n/a | len -= 2; |
---|
5075 | n/a | /* And check that the last two match. */ |
---|
5076 | n/a | if (s[--len] != quote || s[--len] != quote) { |
---|
5077 | n/a | PyErr_BadInternalCall(); |
---|
5078 | n/a | return -1; |
---|
5079 | n/a | } |
---|
5080 | n/a | } |
---|
5081 | n/a | |
---|
5082 | n/a | if (fmode) { |
---|
5083 | n/a | /* Just return the bytes. The caller will parse the resulting |
---|
5084 | n/a | string. */ |
---|
5085 | n/a | *fstr = s; |
---|
5086 | n/a | *fstrlen = len; |
---|
5087 | n/a | return 0; |
---|
5088 | n/a | } |
---|
5089 | n/a | |
---|
5090 | n/a | /* Not an f-string. */ |
---|
5091 | n/a | /* Avoid invoking escape decoding routines if possible. */ |
---|
5092 | n/a | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
---|
5093 | n/a | if (*bytesmode) { |
---|
5094 | n/a | /* Disallow non-ASCII characters. */ |
---|
5095 | n/a | const char *ch; |
---|
5096 | n/a | for (ch = s; *ch; ch++) { |
---|
5097 | n/a | if (Py_CHARMASK(*ch) >= 0x80) { |
---|
5098 | n/a | ast_error(c, n, "bytes can only contain ASCII " |
---|
5099 | n/a | "literal characters."); |
---|
5100 | n/a | return -1; |
---|
5101 | n/a | } |
---|
5102 | n/a | } |
---|
5103 | n/a | if (*rawmode) |
---|
5104 | n/a | *result = PyBytes_FromStringAndSize(s, len); |
---|
5105 | n/a | else |
---|
5106 | n/a | *result = decode_bytes_with_escapes(c, n, s, len); |
---|
5107 | n/a | } else { |
---|
5108 | n/a | if (*rawmode) |
---|
5109 | n/a | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
---|
5110 | n/a | else |
---|
5111 | n/a | *result = decode_unicode_with_escapes(c, n, s, len); |
---|
5112 | n/a | } |
---|
5113 | n/a | return *result == NULL ? -1 : 0; |
---|
5114 | n/a | } |
---|
5115 | n/a | |
---|
5116 | n/a | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
---|
5117 | n/a | each STRING atom, and process it as needed. For bytes, just |
---|
5118 | n/a | concatenate them together, and the result will be a Bytes node. For |
---|
5119 | n/a | normal strings and f-strings, concatenate them together. The result |
---|
5120 | n/a | will be a Str node if there were no f-strings; a FormattedValue |
---|
5121 | n/a | node if there's just an f-string (with no leading or trailing |
---|
5122 | n/a | literals), or a JoinedStr node if there are multiple f-strings or |
---|
5123 | n/a | any literals involved. */ |
---|
5124 | n/a | static expr_ty |
---|
5125 | n/a | parsestrplus(struct compiling *c, const node *n) |
---|
5126 | n/a | { |
---|
5127 | n/a | int bytesmode = 0; |
---|
5128 | n/a | PyObject *bytes_str = NULL; |
---|
5129 | n/a | int i; |
---|
5130 | n/a | |
---|
5131 | n/a | FstringParser state; |
---|
5132 | n/a | FstringParser_Init(&state); |
---|
5133 | n/a | |
---|
5134 | n/a | for (i = 0; i < NCH(n); i++) { |
---|
5135 | n/a | int this_bytesmode; |
---|
5136 | n/a | int this_rawmode; |
---|
5137 | n/a | PyObject *s; |
---|
5138 | n/a | const char *fstr; |
---|
5139 | n/a | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
---|
5140 | n/a | |
---|
5141 | n/a | REQ(CHILD(n, i), STRING); |
---|
5142 | n/a | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
---|
5143 | n/a | &fstr, &fstrlen) != 0) |
---|
5144 | n/a | goto error; |
---|
5145 | n/a | |
---|
5146 | n/a | /* Check that we're not mixing bytes with unicode. */ |
---|
5147 | n/a | if (i != 0 && bytesmode != this_bytesmode) { |
---|
5148 | n/a | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
---|
5149 | n/a | /* s is NULL if the current string part is an f-string. */ |
---|
5150 | n/a | Py_XDECREF(s); |
---|
5151 | n/a | goto error; |
---|
5152 | n/a | } |
---|
5153 | n/a | bytesmode = this_bytesmode; |
---|
5154 | n/a | |
---|
5155 | n/a | if (fstr != NULL) { |
---|
5156 | n/a | int result; |
---|
5157 | n/a | assert(s == NULL && !bytesmode); |
---|
5158 | n/a | /* This is an f-string. Parse and concatenate it. */ |
---|
5159 | n/a | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
---|
5160 | n/a | this_rawmode, 0, c, n); |
---|
5161 | n/a | if (result < 0) |
---|
5162 | n/a | goto error; |
---|
5163 | n/a | } else { |
---|
5164 | n/a | /* A string or byte string. */ |
---|
5165 | n/a | assert(s != NULL && fstr == NULL); |
---|
5166 | n/a | |
---|
5167 | n/a | assert(bytesmode ? PyBytes_CheckExact(s) : |
---|
5168 | n/a | PyUnicode_CheckExact(s)); |
---|
5169 | n/a | |
---|
5170 | n/a | if (bytesmode) { |
---|
5171 | n/a | /* For bytes, concat as we go. */ |
---|
5172 | n/a | if (i == 0) { |
---|
5173 | n/a | /* First time, just remember this value. */ |
---|
5174 | n/a | bytes_str = s; |
---|
5175 | n/a | } else { |
---|
5176 | n/a | PyBytes_ConcatAndDel(&bytes_str, s); |
---|
5177 | n/a | if (!bytes_str) |
---|
5178 | n/a | goto error; |
---|
5179 | n/a | } |
---|
5180 | n/a | } else { |
---|
5181 | n/a | /* This is a regular string. Concatenate it. */ |
---|
5182 | n/a | if (FstringParser_ConcatAndDel(&state, s) < 0) |
---|
5183 | n/a | goto error; |
---|
5184 | n/a | } |
---|
5185 | n/a | } |
---|
5186 | n/a | } |
---|
5187 | n/a | if (bytesmode) { |
---|
5188 | n/a | /* Just return the bytes object and we're done. */ |
---|
5189 | n/a | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
---|
5190 | n/a | goto error; |
---|
5191 | n/a | return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena); |
---|
5192 | n/a | } |
---|
5193 | n/a | |
---|
5194 | n/a | /* We're not a bytes string, bytes_str should never have been set. */ |
---|
5195 | n/a | assert(bytes_str == NULL); |
---|
5196 | n/a | |
---|
5197 | n/a | return FstringParser_Finish(&state, c, n); |
---|
5198 | n/a | |
---|
5199 | n/a | error: |
---|
5200 | n/a | Py_XDECREF(bytes_str); |
---|
5201 | n/a | FstringParser_Dealloc(&state); |
---|
5202 | n/a | return NULL; |
---|
5203 | n/a | } |
---|