blob: 0f9c19333d7c22ef8bb473bffa2902a5fde05bc7 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100135validate_constant(PyObject *value)
136{
137 if (value == Py_None || value == Py_Ellipsis)
138 return 1;
139
140 if (PyLong_CheckExact(value)
141 || PyFloat_CheckExact(value)
142 || PyComplex_CheckExact(value)
143 || PyBool_Check(value)
144 || PyUnicode_CheckExact(value)
145 || PyBytes_CheckExact(value))
146 return 1;
147
148 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
149 PyObject *it;
150
151 it = PyObject_GetIter(value);
152 if (it == NULL)
153 return 0;
154
155 while (1) {
156 PyObject *item = PyIter_Next(it);
157 if (item == NULL) {
158 if (PyErr_Occurred()) {
159 Py_DECREF(it);
160 return 0;
161 }
162 break;
163 }
164
165 if (!validate_constant(item)) {
166 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100167 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100168 return 0;
169 }
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 }
172
173 Py_DECREF(it);
174 return 1;
175 }
176
177 return 0;
178}
179
180static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500181validate_expr(expr_ty exp, expr_context_ty ctx)
182{
183 int check_ctx = 1;
184 expr_context_ty actual_ctx;
185
186 /* First check expression context. */
187 switch (exp->kind) {
188 case Attribute_kind:
189 actual_ctx = exp->v.Attribute.ctx;
190 break;
191 case Subscript_kind:
192 actual_ctx = exp->v.Subscript.ctx;
193 break;
194 case Starred_kind:
195 actual_ctx = exp->v.Starred.ctx;
196 break;
197 case Name_kind:
198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100291 PyErr_Format(PyExc_TypeError,
292 "got an invalid type in Constant: %s",
293 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 return 0;
295 }
296 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 case Num_kind: {
298 PyObject *n = exp->v.Num.n;
299 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
300 !PyComplex_CheckExact(n)) {
301 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
302 return 0;
303 }
304 return 1;
305 }
306 case Str_kind: {
307 PyObject *s = exp->v.Str.s;
308 if (!PyUnicode_CheckExact(s)) {
309 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
310 return 0;
311 }
312 return 1;
313 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314 case JoinedStr_kind:
315 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
316 case FormattedValue_kind:
317 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
318 return 0;
319 if (exp->v.FormattedValue.format_spec)
320 return validate_expr(exp->v.FormattedValue.format_spec, Load);
321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Bytes_kind: {
323 PyObject *b = exp->v.Bytes.s;
324 if (!PyBytes_CheckExact(b)) {
325 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
326 return 0;
327 }
328 return 1;
329 }
330 case Attribute_kind:
331 return validate_expr(exp->v.Attribute.value, Load);
332 case Subscript_kind:
333 return validate_slice(exp->v.Subscript.slice) &&
334 validate_expr(exp->v.Subscript.value, Load);
335 case Starred_kind:
336 return validate_expr(exp->v.Starred.value, ctx);
337 case List_kind:
338 return validate_exprs(exp->v.List.elts, ctx, 0);
339 case Tuple_kind:
340 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
341 /* These last cases don't have any checking. */
342 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500343 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 case Ellipsis_kind:
345 return 1;
346 default:
347 PyErr_SetString(PyExc_SystemError, "unexpected expression");
348 return 0;
349 }
350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
369validate_body(asdl_seq *body, const char *owner)
370{
371 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
372}
373
374static int
375validate_stmt(stmt_ty stmt)
376{
377 int i;
378 switch (stmt->kind) {
379 case FunctionDef_kind:
380 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
381 validate_arguments(stmt->v.FunctionDef.args) &&
382 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
383 (!stmt->v.FunctionDef.returns ||
384 validate_expr(stmt->v.FunctionDef.returns, Load));
385 case ClassDef_kind:
386 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
387 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
388 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400389 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500390 case Return_kind:
391 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
392 case Delete_kind:
393 return validate_assignlist(stmt->v.Delete.targets, Del);
394 case Assign_kind:
395 return validate_assignlist(stmt->v.Assign.targets, Store) &&
396 validate_expr(stmt->v.Assign.value, Load);
397 case AugAssign_kind:
398 return validate_expr(stmt->v.AugAssign.target, Store) &&
399 validate_expr(stmt->v.AugAssign.value, Load);
400 case For_kind:
401 return validate_expr(stmt->v.For.target, Store) &&
402 validate_expr(stmt->v.For.iter, Load) &&
403 validate_body(stmt->v.For.body, "For") &&
404 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400405 case AsyncFor_kind:
406 return validate_expr(stmt->v.AsyncFor.target, Store) &&
407 validate_expr(stmt->v.AsyncFor.iter, Load) &&
408 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
409 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500410 case While_kind:
411 return validate_expr(stmt->v.While.test, Load) &&
412 validate_body(stmt->v.While.body, "While") &&
413 validate_stmts(stmt->v.While.orelse);
414 case If_kind:
415 return validate_expr(stmt->v.If.test, Load) &&
416 validate_body(stmt->v.If.body, "If") &&
417 validate_stmts(stmt->v.If.orelse);
418 case With_kind:
419 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
420 return 0;
421 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
422 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
423 if (!validate_expr(item->context_expr, Load) ||
424 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
425 return 0;
426 }
427 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400428 case AsyncWith_kind:
429 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
430 return 0;
431 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
432 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
433 if (!validate_expr(item->context_expr, Load) ||
434 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
435 return 0;
436 }
437 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500438 case Raise_kind:
439 if (stmt->v.Raise.exc) {
440 return validate_expr(stmt->v.Raise.exc, Load) &&
441 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
442 }
443 if (stmt->v.Raise.cause) {
444 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
445 return 0;
446 }
447 return 1;
448 case Try_kind:
449 if (!validate_body(stmt->v.Try.body, "Try"))
450 return 0;
451 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
452 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
453 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
454 return 0;
455 }
456 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
457 asdl_seq_LEN(stmt->v.Try.orelse)) {
458 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
459 return 0;
460 }
461 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
462 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
463 if ((handler->v.ExceptHandler.type &&
464 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
465 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
466 return 0;
467 }
468 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
469 validate_stmts(stmt->v.Try.finalbody)) &&
470 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
471 validate_stmts(stmt->v.Try.orelse));
472 case Assert_kind:
473 return validate_expr(stmt->v.Assert.test, Load) &&
474 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
475 case Import_kind:
476 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
477 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300478 if (stmt->v.ImportFrom.level < 0) {
479 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500480 return 0;
481 }
482 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
483 case Global_kind:
484 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
485 case Nonlocal_kind:
486 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
487 case Expr_kind:
488 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400489 case AsyncFunctionDef_kind:
490 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
491 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
492 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
493 (!stmt->v.AsyncFunctionDef.returns ||
494 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500495 case Pass_kind:
496 case Break_kind:
497 case Continue_kind:
498 return 1;
499 default:
500 PyErr_SetString(PyExc_SystemError, "unexpected statement");
501 return 0;
502 }
503}
504
505static int
506validate_stmts(asdl_seq *seq)
507{
508 int i;
509 for (i = 0; i < asdl_seq_LEN(seq); i++) {
510 stmt_ty stmt = asdl_seq_GET(seq, i);
511 if (stmt) {
512 if (!validate_stmt(stmt))
513 return 0;
514 }
515 else {
516 PyErr_SetString(PyExc_ValueError,
517 "None disallowed in statement list");
518 return 0;
519 }
520 }
521 return 1;
522}
523
524static int
525validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
526{
527 int i;
528 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
529 expr_ty expr = asdl_seq_GET(exprs, i);
530 if (expr) {
531 if (!validate_expr(expr, ctx))
532 return 0;
533 }
534 else if (!null_ok) {
535 PyErr_SetString(PyExc_ValueError,
536 "None disallowed in expression list");
537 return 0;
538 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100539
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500540 }
541 return 1;
542}
543
544int
545PyAST_Validate(mod_ty mod)
546{
547 int res = 0;
548
549 switch (mod->kind) {
550 case Module_kind:
551 res = validate_stmts(mod->v.Module.body);
552 break;
553 case Interactive_kind:
554 res = validate_stmts(mod->v.Interactive.body);
555 break;
556 case Expression_kind:
557 res = validate_expr(mod->v.Expression.body, Load);
558 break;
559 case Suite_kind:
560 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
561 break;
562 default:
563 PyErr_SetString(PyExc_SystemError, "impossible module node");
564 res = 0;
565 break;
566 }
567 return res;
568}
569
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500570/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500571#include "grammar.h"
572#include "parsetok.h"
573#include "graminit.h"
574
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575/* Data structure used internally */
576struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400577 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200578 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500579 PyObject *c_normalize; /* Normalization function from unicodedata. */
580 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581};
582
583static asdl_seq *seq_for_testlist(struct compiling *, const node *);
584static expr_ty ast_for_expr(struct compiling *, const node *);
585static stmt_ty ast_for_stmt(struct compiling *, const node *);
586static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000587static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
588 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000589static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000590static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
Yury Selivanov75445082015-05-11 22:57:16 -0400592static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
593static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
594
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595/* Note different signature for ast_for_call */
596static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
597
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000598static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400599static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Nick Coghlan650f0d02007-04-15 12:05:43 +0000601#define COMP_GENEXP 0
602#define COMP_LISTCOMP 1
603#define COMP_SETCOMP 2
604
Benjamin Peterson55e00432012-01-16 17:22:31 -0500605static int
606init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000607{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500608 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
609 if (!m)
610 return 0;
611 c->c_normalize = PyObject_GetAttrString(m, "normalize");
612 Py_DECREF(m);
613 if (!c->c_normalize)
614 return 0;
615 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500616 if (!c->c_normalize_args) {
617 Py_CLEAR(c->c_normalize);
618 return 0;
619 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200620 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500621 return 1;
622}
623
624static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400625new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500626{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400627 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500628 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000629 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500630 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500631 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000632 /* Check whether there are non-ASCII characters in the
633 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500634 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200635 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500636 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500637 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200638 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500639 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500640 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
641 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500642 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 if (!id2)
644 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200645 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000646 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000647 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200648 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
649 Py_DECREF(id);
650 return NULL;
651 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000652 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653}
654
Benjamin Peterson55e00432012-01-16 17:22:31 -0500655#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400658ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400660 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661
Victor Stinner14e461d2013-08-26 22:28:21 +0200662 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000664 Py_INCREF(Py_None);
665 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200667 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400668 if (!tmp)
669 return 0;
670 errstr = PyUnicode_FromString(errmsg);
671 if (!errstr) {
672 Py_DECREF(tmp);
673 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000674 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 Py_DECREF(errstr);
677 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400678 if (value) {
679 PyErr_SetObject(PyExc_SyntaxError, value);
680 Py_DECREF(value);
681 }
682 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683}
684
685/* num_stmts() returns number of contained statements.
686
687 Use this routine to determine how big a sequence is needed for
688 the statements in a parse tree. Its raison d'etre is this bit of
689 grammar:
690
691 stmt: simple_stmt | compound_stmt
692 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
693
694 A simple_stmt can contain multiple small_stmt elements joined
695 by semicolons. If the arg is a simple_stmt, the number of
696 small_stmt elements is returned.
697*/
698
699static int
700num_stmts(const node *n)
701{
702 int i, l;
703 node *ch;
704
705 switch (TYPE(n)) {
706 case single_input:
707 if (TYPE(CHILD(n, 0)) == NEWLINE)
708 return 0;
709 else
710 return num_stmts(CHILD(n, 0));
711 case file_input:
712 l = 0;
713 for (i = 0; i < NCH(n); i++) {
714 ch = CHILD(n, i);
715 if (TYPE(ch) == stmt)
716 l += num_stmts(ch);
717 }
718 return l;
719 case stmt:
720 return num_stmts(CHILD(n, 0));
721 case compound_stmt:
722 return 1;
723 case simple_stmt:
724 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
725 case suite:
726 if (NCH(n) == 1)
727 return num_stmts(CHILD(n, 0));
728 else {
729 l = 0;
730 for (i = 2; i < (NCH(n) - 1); i++)
731 l += num_stmts(CHILD(n, i));
732 return l;
733 }
734 default: {
735 char buf[128];
736
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000737 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 TYPE(n), NCH(n));
739 Py_FatalError(buf);
740 }
741 }
742 assert(0);
743 return 0;
744}
745
746/* Transform the CST rooted at node * to the appropriate AST
747*/
748
749mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200750PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
751 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000753 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 asdl_seq *stmts = NULL;
755 stmt_ty s;
756 node *ch;
757 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500758 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400760 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200761 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400762 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800763 c.c_normalize = NULL;
764 c.c_normalize_args = NULL;
765
766 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768
Jeremy Hyltona8293132006-02-28 17:58:27 +0000769 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 switch (TYPE(n)) {
771 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200772 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500774 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 for (i = 0; i < NCH(n) - 1; i++) {
776 ch = CHILD(n, i);
777 if (TYPE(ch) == NEWLINE)
778 continue;
779 REQ(ch, stmt);
780 num = num_stmts(ch);
781 if (num == 1) {
782 s = ast_for_stmt(&c, ch);
783 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500784 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000785 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 }
787 else {
788 ch = CHILD(ch, 0);
789 REQ(ch, simple_stmt);
790 for (j = 0; j < num; j++) {
791 s = ast_for_stmt(&c, CHILD(ch, j * 2));
792 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500793 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000794 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 }
796 }
797 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500798 res = Module(stmts, arena);
799 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 case eval_input: {
801 expr_ty testlist_ast;
802
Nick Coghlan650f0d02007-04-15 12:05:43 +0000803 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000804 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500806 goto out;
807 res = Expression(testlist_ast, arena);
808 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 }
810 case single_input:
811 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200812 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500814 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
816 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000817 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500818 goto out;
819 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 else {
822 n = CHILD(n, 0);
823 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200824 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500826 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000828 s = ast_for_stmt(&c, n);
829 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500830 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 asdl_seq_SET(stmts, 0, s);
832 }
833 else {
834 /* Only a simple_stmt can contain multiple statements. */
835 REQ(n, simple_stmt);
836 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (TYPE(CHILD(n, i)) == NEWLINE)
838 break;
839 s = ast_for_stmt(&c, CHILD(n, i));
840 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500841 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 asdl_seq_SET(stmts, i / 2, s);
843 }
844 }
845
Benjamin Peterson55e00432012-01-16 17:22:31 -0500846 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500848 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000850 PyErr_Format(PyExc_SystemError,
851 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500852 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500854 out:
855 if (c.c_normalize) {
856 Py_DECREF(c.c_normalize);
857 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
858 Py_DECREF(c.c_normalize_args);
859 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500860 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861}
862
Victor Stinner14e461d2013-08-26 22:28:21 +0200863mod_ty
864PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
865 PyArena *arena)
866{
867 mod_ty mod;
868 PyObject *filename;
869 filename = PyUnicode_DecodeFSDefault(filename_str);
870 if (filename == NULL)
871 return NULL;
872 mod = PyAST_FromNodeObject(n, flags, filename, arena);
873 Py_DECREF(filename);
874 return mod;
875
876}
877
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
879*/
880
881static operator_ty
882get_operator(const node *n)
883{
884 switch (TYPE(n)) {
885 case VBAR:
886 return BitOr;
887 case CIRCUMFLEX:
888 return BitXor;
889 case AMPER:
890 return BitAnd;
891 case LEFTSHIFT:
892 return LShift;
893 case RIGHTSHIFT:
894 return RShift;
895 case PLUS:
896 return Add;
897 case MINUS:
898 return Sub;
899 case STAR:
900 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400901 case AT:
902 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 case SLASH:
904 return Div;
905 case DOUBLESLASH:
906 return FloorDiv;
907 case PERCENT:
908 return Mod;
909 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000910 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 }
912}
913
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200914static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000915 "None",
916 "True",
917 "False",
918 NULL,
919};
920
921static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400922forbidden_name(struct compiling *c, identifier name, const node *n,
923 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000924{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000925 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000926 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400927 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000928 return 1;
929 }
930 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200931 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000932 for (p = FORBIDDEN; *p; p++) {
933 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400934 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000935 return 1;
936 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000937 }
938 }
939 return 0;
940}
941
Jeremy Hyltona8293132006-02-28 17:58:27 +0000942/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943
944 Only sets context for expr kinds that "can appear in assignment context"
945 (according to ../Parser/Python.asdl). For other expr kinds, it sets
946 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947*/
948
949static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000950set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951{
952 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000953 /* If a particular expression type can't be used for assign / delete,
954 set expr_name to its name and an error message will be generated.
955 */
956 const char* expr_name = NULL;
957
958 /* The ast defines augmented store and load contexts, but the
959 implementation here doesn't actually use them. The code may be
960 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000961 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000962 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000963 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 */
965 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966
967 switch (e->kind) {
968 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000969 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400970 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000971 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000972 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000974 e->v.Subscript.ctx = ctx;
975 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000976 case Starred_kind:
977 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000978 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000979 return 0;
980 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000982 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500983 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000984 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000985 }
986 e->v.Name.ctx = ctx;
987 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000989 e->v.List.ctx = ctx;
990 s = e->v.List.elts;
991 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +0300993 e->v.Tuple.ctx = ctx;
994 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000995 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000996 case Lambda_kind:
997 expr_name = "lambda";
998 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001000 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001001 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001002 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001004 case UnaryOp_kind:
1005 expr_name = "operator";
1006 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001008 expr_name = "generator expression";
1009 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001010 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001011 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001012 expr_name = "yield expression";
1013 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001014 case Await_kind:
1015 expr_name = "await expression";
1016 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001017 case ListComp_kind:
1018 expr_name = "list comprehension";
1019 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001020 case SetComp_kind:
1021 expr_name = "set comprehension";
1022 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001023 case DictComp_kind:
1024 expr_name = "dict comprehension";
1025 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001026 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001027 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 case Num_kind:
1029 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001030 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001031 case JoinedStr_kind:
1032 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001033 expr_name = "literal";
1034 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001035 case NameConstant_kind:
1036 expr_name = "keyword";
1037 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001038 case Ellipsis_kind:
1039 expr_name = "Ellipsis";
1040 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001041 case Compare_kind:
1042 expr_name = "comparison";
1043 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044 case IfExp_kind:
1045 expr_name = "conditional expression";
1046 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001047 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 PyErr_Format(PyExc_SystemError,
1049 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001050 e->kind, e->lineno);
1051 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001053 /* Check for error string set by switch */
1054 if (expr_name) {
1055 char buf[300];
1056 PyOS_snprintf(buf, sizeof(buf),
1057 "can't %s %s",
1058 ctx == Store ? "assign to" : "delete",
1059 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001060 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001061 }
1062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 */
1066 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001067 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068
Thomas Wouters89f507f2006-12-13 04:49:30 +00001069 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001070 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001071 return 0;
1072 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 }
1074 return 1;
1075}
1076
1077static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001078ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079{
1080 REQ(n, augassign);
1081 n = CHILD(n, 0);
1082 switch (STR(n)[0]) {
1083 case '+':
1084 return Add;
1085 case '-':
1086 return Sub;
1087 case '/':
1088 if (STR(n)[1] == '/')
1089 return FloorDiv;
1090 else
1091 return Div;
1092 case '%':
1093 return Mod;
1094 case '<':
1095 return LShift;
1096 case '>':
1097 return RShift;
1098 case '&':
1099 return BitAnd;
1100 case '^':
1101 return BitXor;
1102 case '|':
1103 return BitOr;
1104 case '*':
1105 if (STR(n)[1] == '*')
1106 return Pow;
1107 else
1108 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001109 case '@':
1110 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001112 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 }
1115}
1116
1117static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001118ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001120 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 |'is' 'not'
1122 */
1123 REQ(n, comp_op);
1124 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001125 n = CHILD(n, 0);
1126 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 case LESS:
1128 return Lt;
1129 case GREATER:
1130 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001131 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 return Eq;
1133 case LESSEQUAL:
1134 return LtE;
1135 case GREATEREQUAL:
1136 return GtE;
1137 case NOTEQUAL:
1138 return NotEq;
1139 case NAME:
1140 if (strcmp(STR(n), "in") == 0)
1141 return In;
1142 if (strcmp(STR(n), "is") == 0)
1143 return Is;
1144 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001145 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001148 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 }
1150 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001151 /* handle "not in" and "is not" */
1152 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 case NAME:
1154 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1155 return NotIn;
1156 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1157 return IsNot;
1158 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001159 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001161 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 }
Neal Norwitz79792652005-11-14 04:25:03 +00001164 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167}
1168
1169static asdl_seq *
1170seq_for_testlist(struct compiling *c, const node *n)
1171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001173 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1174 */
Armin Rigo31441302005-10-21 12:57:31 +00001175 asdl_seq *seq;
1176 expr_ty expression;
1177 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001178 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001180 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 if (!seq)
1182 return NULL;
1183
1184 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001186 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187
Benjamin Peterson4905e802009-09-27 02:43:28 +00001188 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001189 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191
1192 assert(i / 2 < seq->size);
1193 asdl_seq_SET(seq, i / 2, expression);
1194 }
1195 return seq;
1196}
1197
Neal Norwitzc1505362006-12-28 06:47:50 +00001198static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001199ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001200{
1201 identifier name;
1202 expr_ty annotation = NULL;
1203 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001204 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001205
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001206 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001207 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001208 name = NEW_IDENTIFIER(ch);
1209 if (!name)
1210 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001211 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001212 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001213
1214 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1215 annotation = ast_for_expr(c, CHILD(n, 2));
1216 if (!annotation)
1217 return NULL;
1218 }
1219
Victor Stinnerc106c682015-11-06 17:01:48 +01001220 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001221 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001222 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001223 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224}
1225
Guido van Rossum4f72a782006-10-27 23:31:49 +00001226/* returns -1 if failed to handle keyword only arguments
1227 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001228 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001229 ^^^
1230 start pointing here
1231 */
1232static int
1233handle_keywordonly_args(struct compiling *c, const node *n, int start,
1234 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1235{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001236 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001237 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001238 expr_ty expression, annotation;
1239 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240 int i = start;
1241 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001242
1243 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001244 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001245 return -1;
1246 }
1247 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001248 while (i < NCH(n)) {
1249 ch = CHILD(n, i);
1250 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001251 case vfpdef:
1252 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001253 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001254 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001255 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001256 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001257 asdl_seq_SET(kwdefaults, j, expression);
1258 i += 2; /* '=' and test */
1259 }
1260 else { /* setting NULL if no default value exists */
1261 asdl_seq_SET(kwdefaults, j, NULL);
1262 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001263 if (NCH(ch) == 3) {
1264 /* ch is NAME ':' test */
1265 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001266 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001267 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001268 }
1269 else {
1270 annotation = NULL;
1271 }
1272 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001273 argname = NEW_IDENTIFIER(ch);
1274 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001275 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001276 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001277 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001278 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1279 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001280 if (!arg)
1281 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001282 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001283 i += 2; /* the name and the comma */
1284 break;
1285 case DOUBLESTAR:
1286 return i;
1287 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001288 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001289 goto error;
1290 }
1291 }
1292 return i;
1293 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001295}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296
Jeremy Hyltona8293132006-02-28 17:58:27 +00001297/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298
1299static arguments_ty
1300ast_for_arguments(struct compiling *c, const node *n)
1301{
Neal Norwitzc1505362006-12-28 06:47:50 +00001302 /* This function handles both typedargslist (function definition)
1303 and varargslist (lambda definition).
1304
1305 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001306 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1307 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1308 | '**' tfpdef [',']]]
1309 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1310 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001311 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001312 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1313 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1314 | '**' vfpdef [',']]]
1315 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1316 | '**' vfpdef [',']
1317 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001318 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001319
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001321 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1322 int nposdefaults = 0, found_default = 0;
1323 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001324 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001325 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 node *ch;
1327
1328 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001329 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001330 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001333 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334
Jeremy Hyltone921e022008-07-17 16:37:17 +00001335 /* First count the number of positional args & defaults. The
1336 variable i is the loop index for this for loop and the next.
1337 The next loop picks up where the first leaves off.
1338 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001340 ch = CHILD(n, i);
1341 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001342 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001343 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001344 if (i < NCH(n) && /* skip argument following star */
1345 (TYPE(CHILD(n, i)) == tfpdef ||
1346 TYPE(CHILD(n, i)) == vfpdef)) {
1347 i++;
1348 }
1349 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001350 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001351 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001352 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001353 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001356 defaults for keyword only args */
1357 for ( ; i < NCH(n); ++i) {
1358 ch = CHILD(n, i);
1359 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001360 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001362 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001363 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001364 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001366 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001368 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001370 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001372 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 since we set NULL as default for keyword only argument w/o default
1375 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001376 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001377 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001379 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380
1381 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001382 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001383 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001386 /* tfpdef: NAME [':' test]
1387 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 */
1389 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001390 j = 0; /* index for defaults */
1391 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001393 ch = CHILD(n, i);
1394 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001395 case tfpdef:
1396 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1398 anything other than EQUAL or a comma? */
1399 /* XXX Should NCH(n) check be made a separate check? */
1400 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001401 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1402 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001403 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 assert(posdefaults != NULL);
1405 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001409 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001410 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001412 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001414 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001415 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001416 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001417 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 i += 2; /* the name and the comma */
1419 break;
1420 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001421 if (i+1 >= NCH(n) ||
1422 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001423 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001424 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001425 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001426 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001427 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001428 if (TYPE(ch) == COMMA) {
1429 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001430 i += 2; /* now follows keyword only arguments */
1431 res = handle_keywordonly_args(c, n, i,
1432 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001433 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001434 i = res; /* res has new position to process */
1435 }
1436 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001437 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001438 if (!vararg)
1439 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001440
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001442 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1443 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001444 int res = 0;
1445 res = handle_keywordonly_args(c, n, i,
1446 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001447 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 i = res; /* res has new position to process */
1449 }
1450 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 break;
1452 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001453 ch = CHILD(n, i+1); /* tfpdef */
1454 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001455 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001456 if (!kwarg)
1457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 i += 3;
1459 break;
1460 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001461 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 "unexpected node in varargslist: %d @ %d",
1463 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001464 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001467 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468}
1469
1470static expr_ty
1471ast_for_dotted_name(struct compiling *c, const node *n)
1472{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001473 expr_ty e;
1474 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001475 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 int i;
1477
1478 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479
1480 lineno = LINENO(n);
1481 col_offset = n->n_col_offset;
1482
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483 id = NEW_IDENTIFIER(CHILD(n, 0));
1484 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001485 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001486 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489
1490 for (i = 2; i < NCH(n); i+=2) {
1491 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001492 if (!id)
1493 return NULL;
1494 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1495 if (!e)
1496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 }
1498
1499 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
1502static expr_ty
1503ast_for_decorator(struct compiling *c, const node *n)
1504{
1505 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1506 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001507 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001510 REQ(CHILD(n, 0), AT);
1511 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1514 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001515 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001518 d = name_expr;
1519 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 }
1521 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001522 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001524 if (!d)
1525 return NULL;
1526 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 }
1528 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001529 d = ast_for_call(c, CHILD(n, 3), name_expr);
1530 if (!d)
1531 return NULL;
1532 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 }
1534
1535 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536}
1537
1538static asdl_seq*
1539ast_for_decorators(struct compiling *c, const node *n)
1540{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001541 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001542 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001546 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 if (!decorator_seq)
1548 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001551 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001552 if (!d)
1553 return NULL;
1554 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 }
1556 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557}
1558
1559static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001560ast_for_funcdef_impl(struct compiling *c, const node *n,
1561 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001563 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001564 identifier name;
1565 arguments_ty args;
1566 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001567 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001568 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569
1570 REQ(n, funcdef);
1571
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 name = NEW_IDENTIFIER(CHILD(n, name_i));
1573 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001574 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001575 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001576 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1578 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001579 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001580 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1581 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1582 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001583 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001584 name_i += 2;
1585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 body = ast_for_suite(c, CHILD(n, name_i + 3));
1587 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001588 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589
Yury Selivanov75445082015-05-11 22:57:16 -04001590 if (is_async)
1591 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1592 LINENO(n),
1593 n->n_col_offset, c->c_arena);
1594 else
1595 return FunctionDef(name, args, body, decorator_seq, returns,
1596 LINENO(n),
1597 n->n_col_offset, c->c_arena);
1598}
1599
1600static stmt_ty
1601ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1602{
1603 /* async_funcdef: ASYNC funcdef */
1604 REQ(n, async_funcdef);
1605 REQ(CHILD(n, 0), ASYNC);
1606 REQ(CHILD(n, 1), funcdef);
1607
1608 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1609 1 /* is_async */);
1610}
1611
1612static stmt_ty
1613ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1614{
1615 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1616 return ast_for_funcdef_impl(c, n, decorator_seq,
1617 0 /* is_async */);
1618}
1619
1620
1621static stmt_ty
1622ast_for_async_stmt(struct compiling *c, const node *n)
1623{
1624 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1625 REQ(n, async_stmt);
1626 REQ(CHILD(n, 0), ASYNC);
1627
1628 switch (TYPE(CHILD(n, 1))) {
1629 case funcdef:
1630 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1631 1 /* is_async */);
1632 case with_stmt:
1633 return ast_for_with_stmt(c, CHILD(n, 1),
1634 1 /* is_async */);
1635
1636 case for_stmt:
1637 return ast_for_for_stmt(c, CHILD(n, 1),
1638 1 /* is_async */);
1639
1640 default:
1641 PyErr_Format(PyExc_SystemError,
1642 "invalid async stament: %s",
1643 STR(CHILD(n, 1)));
1644 return NULL;
1645 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646}
1647
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001648static stmt_ty
1649ast_for_decorated(struct compiling *c, const node *n)
1650{
Yury Selivanov75445082015-05-11 22:57:16 -04001651 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001652 stmt_ty thing = NULL;
1653 asdl_seq *decorator_seq = NULL;
1654
1655 REQ(n, decorated);
1656
1657 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1658 if (!decorator_seq)
1659 return NULL;
1660
1661 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001662 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001664
1665 if (TYPE(CHILD(n, 1)) == funcdef) {
1666 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1667 } else if (TYPE(CHILD(n, 1)) == classdef) {
1668 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001669 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1670 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001671 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001672 /* we count the decorators in when talking about the class' or
1673 * function's line number */
1674 if (thing) {
1675 thing->lineno = LINENO(n);
1676 thing->col_offset = n->n_col_offset;
1677 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001678 return thing;
1679}
1680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681static expr_ty
1682ast_for_lambdef(struct compiling *c, const node *n)
1683{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001684 /* lambdef: 'lambda' [varargslist] ':' test
1685 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 arguments_ty args;
1687 expr_ty expression;
1688
1689 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001690 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 if (!args)
1692 return NULL;
1693 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001694 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 }
1697 else {
1698 args = ast_for_arguments(c, CHILD(n, 1));
1699 if (!args)
1700 return NULL;
1701 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001702 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 }
1705
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001706 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707}
1708
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001709static expr_ty
1710ast_for_ifexpr(struct compiling *c, const node *n)
1711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001713 expr_ty expression, body, orelse;
1714
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001715 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001716 body = ast_for_expr(c, CHILD(n, 0));
1717 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001718 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001719 expression = ast_for_expr(c, CHILD(n, 2));
1720 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001721 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001722 orelse = ast_for_expr(c, CHILD(n, 4));
1723 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001724 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001725 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1726 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001727}
1728
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001730 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731
Nick Coghlan650f0d02007-04-15 12:05:43 +00001732 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733*/
1734
1735static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001736count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001738 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739
Guido van Rossumd8faa362007-04-27 19:54:29 +00001740 count_comp_for:
1741 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001742 REQ(n, comp_for);
1743 if (NCH(n) == 5)
1744 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001745 else
1746 return n_fors;
1747 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001748 REQ(n, comp_iter);
1749 n = CHILD(n, 0);
1750 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001751 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001752 else if (TYPE(n) == comp_if) {
1753 if (NCH(n) == 3) {
1754 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001755 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001756 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001757 else
1758 return n_fors;
1759 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001760
Guido van Rossumd8faa362007-04-27 19:54:29 +00001761 /* Should never be reached */
1762 PyErr_SetString(PyExc_SystemError,
1763 "logic error in count_comp_fors");
1764 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765}
1766
Nick Coghlan650f0d02007-04-15 12:05:43 +00001767/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768
Nick Coghlan650f0d02007-04-15 12:05:43 +00001769 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770*/
1771
1772static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001773count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001775 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776
Guido van Rossumd8faa362007-04-27 19:54:29 +00001777 while (1) {
1778 REQ(n, comp_iter);
1779 if (TYPE(CHILD(n, 0)) == comp_for)
1780 return n_ifs;
1781 n = CHILD(n, 0);
1782 REQ(n, comp_if);
1783 n_ifs++;
1784 if (NCH(n) == 2)
1785 return n_ifs;
1786 n = CHILD(n, 2);
1787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788}
1789
Guido van Rossum992d4a32007-07-11 13:09:30 +00001790static asdl_seq *
1791ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001794 asdl_seq *comps;
1795
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001796 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 if (n_fors == -1)
1798 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001799
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001800 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001801 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001803
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001805 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001807 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001808 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809
Guido van Rossum992d4a32007-07-11 13:09:30 +00001810 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811
Guido van Rossum992d4a32007-07-11 13:09:30 +00001812 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001813 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001814 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001816 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001819
Thomas Wouters89f507f2006-12-13 04:49:30 +00001820 /* Check the # of children rather than the length of t, since
1821 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001822 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001823 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001824 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001826 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1827 c->c_arena),
1828 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001829 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001831
Guido van Rossum992d4a32007-07-11 13:09:30 +00001832 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 int j, n_ifs;
1834 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835
Guido van Rossum992d4a32007-07-11 13:09:30 +00001836 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001837 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001838 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001840
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001841 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001842 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001846 REQ(n, comp_iter);
1847 n = CHILD(n, 0);
1848 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849
Guido van Rossum992d4a32007-07-11 13:09:30 +00001850 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001851 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001852 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001853 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001854 if (NCH(n) == 3)
1855 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001857 /* on exit, must guarantee that n is a comp_for */
1858 if (TYPE(n) == comp_iter)
1859 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001860 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001862 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001864 return comps;
1865}
1866
1867static expr_ty
1868ast_for_itercomp(struct compiling *c, const node *n, int type)
1869{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001870 /* testlist_comp: (test|star_expr)
1871 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001872 expr_ty elt;
1873 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001874 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875
Guido van Rossum992d4a32007-07-11 13:09:30 +00001876 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001878 ch = CHILD(n, 0);
1879 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001880 if (!elt)
1881 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001882 if (elt->kind == Starred_kind) {
1883 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1884 return NULL;
1885 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886
Guido van Rossum992d4a32007-07-11 13:09:30 +00001887 comps = ast_for_comprehension(c, CHILD(n, 1));
1888 if (!comps)
1889 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001890
1891 if (type == COMP_GENEXP)
1892 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1893 else if (type == COMP_LISTCOMP)
1894 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1895 else if (type == COMP_SETCOMP)
1896 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1897 else
1898 /* Should never happen */
1899 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900}
1901
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001902/* Fills in the key, value pair corresponding to the dict element. In case
1903 * of an unpacking, key is NULL. *i is advanced by the number of ast
1904 * elements. Iff successful, nonzero is returned.
1905 */
1906static int
1907ast_for_dictelement(struct compiling *c, const node *n, int *i,
1908 expr_ty *key, expr_ty *value)
1909{
1910 expr_ty expression;
1911 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1912 assert(NCH(n) - *i >= 2);
1913
1914 expression = ast_for_expr(c, CHILD(n, *i + 1));
1915 if (!expression)
1916 return 0;
1917 *key = NULL;
1918 *value = expression;
1919
1920 *i += 2;
1921 }
1922 else {
1923 assert(NCH(n) - *i >= 3);
1924
1925 expression = ast_for_expr(c, CHILD(n, *i));
1926 if (!expression)
1927 return 0;
1928 *key = expression;
1929
1930 REQ(CHILD(n, *i + 1), COLON);
1931
1932 expression = ast_for_expr(c, CHILD(n, *i + 2));
1933 if (!expression)
1934 return 0;
1935 *value = expression;
1936
1937 *i += 3;
1938 }
1939 return 1;
1940}
1941
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001943ast_for_dictcomp(struct compiling *c, const node *n)
1944{
1945 expr_ty key, value;
1946 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001947 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001949 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001950 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001951 assert(key);
1952 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001954 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001955 if (!comps)
1956 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957
Guido van Rossum992d4a32007-07-11 13:09:30 +00001958 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1959}
1960
1961static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001962ast_for_dictdisplay(struct compiling *c, const node *n)
1963{
1964 int i;
1965 int j;
1966 int size;
1967 asdl_seq *keys, *values;
1968
1969 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1970 keys = _Py_asdl_seq_new(size, c->c_arena);
1971 if (!keys)
1972 return NULL;
1973
1974 values = _Py_asdl_seq_new(size, c->c_arena);
1975 if (!values)
1976 return NULL;
1977
1978 j = 0;
1979 for (i = 0; i < NCH(n); i++) {
1980 expr_ty key, value;
1981
1982 if (!ast_for_dictelement(c, n, &i, &key, &value))
1983 return NULL;
1984 asdl_seq_SET(keys, j, key);
1985 asdl_seq_SET(values, j, value);
1986
1987 j++;
1988 }
1989 keys->size = j;
1990 values->size = j;
1991 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1992}
1993
1994static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001995ast_for_genexp(struct compiling *c, const node *n)
1996{
1997 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001998 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001999}
2000
2001static expr_ty
2002ast_for_listcomp(struct compiling *c, const node *n)
2003{
2004 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002005 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002006}
2007
2008static expr_ty
2009ast_for_setcomp(struct compiling *c, const node *n)
2010{
2011 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002012 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002013}
2014
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002015static expr_ty
2016ast_for_setdisplay(struct compiling *c, const node *n)
2017{
2018 int i;
2019 int size;
2020 asdl_seq *elts;
2021
2022 assert(TYPE(n) == (dictorsetmaker));
2023 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2024 elts = _Py_asdl_seq_new(size, c->c_arena);
2025 if (!elts)
2026 return NULL;
2027 for (i = 0; i < NCH(n); i += 2) {
2028 expr_ty expression;
2029 expression = ast_for_expr(c, CHILD(n, i));
2030 if (!expression)
2031 return NULL;
2032 asdl_seq_SET(elts, i / 2, expression);
2033 }
2034 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2035}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002036
2037static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038ast_for_atom(struct compiling *c, const node *n)
2039{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002040 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2041 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002042 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 */
2044 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002047 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002048 PyObject *name;
2049 const char *s = STR(ch);
2050 size_t len = strlen(s);
2051 if (len >= 4 && len <= 5) {
2052 if (!strcmp(s, "None"))
2053 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2054 if (!strcmp(s, "True"))
2055 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2056 if (!strcmp(s, "False"))
2057 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2058 }
2059 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002060 if (!name)
2061 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002062 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002063 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2064 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002066 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002067 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002068 const char *errtype = NULL;
2069 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2070 errtype = "unicode error";
2071 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2072 errtype = "value error";
2073 if (errtype) {
2074 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002075 PyObject *type, *value, *tback, *errstr;
2076 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002077 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002078 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002079 char *s = _PyUnicode_AsString(errstr);
2080 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002081 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002082 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002083 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002084 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002085 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002086 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002087 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002088 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002089 Py_XDECREF(tback);
2090 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002091 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002092 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002093 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 }
2095 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002096 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002097 if (!pynum)
2098 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099
Victor Stinner43d81952013-07-17 00:57:58 +02002100 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2101 Py_DECREF(pynum);
2102 return NULL;
2103 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002104 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 }
Georg Brandldde00282007-03-18 19:01:53 +00002106 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002107 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002109 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110
Thomas Wouters89f507f2006-12-13 04:49:30 +00002111 if (TYPE(ch) == RPAR)
2112 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113
Thomas Wouters89f507f2006-12-13 04:49:30 +00002114 if (TYPE(ch) == yield_expr)
2115 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002118 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002119 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002120
Nick Coghlan650f0d02007-04-15 12:05:43 +00002121 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002123 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124
Thomas Wouters89f507f2006-12-13 04:49:30 +00002125 if (TYPE(ch) == RSQB)
2126 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127
Nick Coghlan650f0d02007-04-15 12:05:43 +00002128 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002129 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2130 asdl_seq *elts = seq_for_testlist(c, ch);
2131 if (!elts)
2132 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002133
Thomas Wouters89f507f2006-12-13 04:49:30 +00002134 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2135 }
2136 else
2137 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002139 /* dictorsetmaker: ( ((test ':' test | '**' test)
2140 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2141 * ((test | '*' test)
2142 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002143 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002144 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002145 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002146 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002147 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002148 }
2149 else {
2150 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2151 if (NCH(ch) == 1 ||
2152 (NCH(ch) > 1 &&
2153 TYPE(CHILD(ch, 1)) == COMMA)) {
2154 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002155 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002156 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002157 else if (NCH(ch) > 1 &&
2158 TYPE(CHILD(ch, 1)) == comp_for) {
2159 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002160 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002161 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002162 else if (NCH(ch) > 3 - is_dict &&
2163 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2164 /* It's a dictionary comprehension. */
2165 if (is_dict) {
2166 ast_error(c, n, "dict unpacking cannot be used in "
2167 "dict comprehension");
2168 return NULL;
2169 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002170 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002171 }
2172 else {
2173 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002174 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002175 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002176 if (res) {
2177 res->lineno = LINENO(n);
2178 res->col_offset = n->n_col_offset;
2179 }
2180 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002181 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002184 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2185 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 }
2187}
2188
2189static slice_ty
2190ast_for_slice(struct compiling *c, const node *n)
2191{
2192 node *ch;
2193 expr_ty lower = NULL, upper = NULL, step = NULL;
2194
2195 REQ(n, subscript);
2196
2197 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002198 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 sliceop: ':' [test]
2200 */
2201 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 if (NCH(n) == 1 && TYPE(ch) == test) {
2203 /* 'step' variable hold no significance in terms of being used over
2204 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 if (!step)
2207 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208
Thomas Wouters89f507f2006-12-13 04:49:30 +00002209 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 }
2211
2212 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002213 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214 if (!lower)
2215 return NULL;
2216 }
2217
2218 /* If there's an upper bound it's in the second or third position. */
2219 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002220 if (NCH(n) > 1) {
2221 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222
Thomas Wouters89f507f2006-12-13 04:49:30 +00002223 if (TYPE(n2) == test) {
2224 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 if (!upper)
2226 return NULL;
2227 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002228 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002230 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231
Thomas Wouters89f507f2006-12-13 04:49:30 +00002232 if (TYPE(n2) == test) {
2233 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 if (!upper)
2235 return NULL;
2236 }
2237 }
2238
2239 ch = CHILD(n, NCH(n) - 1);
2240 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002241 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002242 ch = CHILD(ch, 1);
2243 if (TYPE(ch) == test) {
2244 step = ast_for_expr(c, ch);
2245 if (!step)
2246 return NULL;
2247 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 }
2249 }
2250
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002251 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252}
2253
2254static expr_ty
2255ast_for_binop(struct compiling *c, const node *n)
2256{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002257 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002259 BinOp(BinOp(A, op, B), op, C).
2260 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261
Guido van Rossumd8faa362007-04-27 19:54:29 +00002262 int i, nops;
2263 expr_ty expr1, expr2, result;
2264 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265
Guido van Rossumd8faa362007-04-27 19:54:29 +00002266 expr1 = ast_for_expr(c, CHILD(n, 0));
2267 if (!expr1)
2268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269
Guido van Rossumd8faa362007-04-27 19:54:29 +00002270 expr2 = ast_for_expr(c, CHILD(n, 2));
2271 if (!expr2)
2272 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273
Guido van Rossumd8faa362007-04-27 19:54:29 +00002274 newoperator = get_operator(CHILD(n, 1));
2275 if (!newoperator)
2276 return NULL;
2277
2278 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2279 c->c_arena);
2280 if (!result)
2281 return NULL;
2282
2283 nops = (NCH(n) - 1) / 2;
2284 for (i = 1; i < nops; i++) {
2285 expr_ty tmp_result, tmp;
2286 const node* next_oper = CHILD(n, i * 2 + 1);
2287
2288 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002289 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 return NULL;
2291
Guido van Rossumd8faa362007-04-27 19:54:29 +00002292 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2293 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 return NULL;
2295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002297 LINENO(next_oper), next_oper->n_col_offset,
2298 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002300 return NULL;
2301 result = tmp_result;
2302 }
2303 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304}
2305
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002306static expr_ty
2307ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002310 subscriptlist: subscript (',' subscript)* [',']
2311 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2312 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002313 REQ(n, trailer);
2314 if (TYPE(CHILD(n, 0)) == LPAR) {
2315 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002316 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002317 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002318 else
2319 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002320 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002321 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002322 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2323 if (!attr_id)
2324 return NULL;
2325 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002326 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002327 }
2328 else {
2329 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002330 REQ(CHILD(n, 2), RSQB);
2331 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002332 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002333 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2334 if (!slc)
2335 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002336 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2337 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002338 }
2339 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002341 by treating the sequence as a tuple literal if there are
2342 no slice features.
2343 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002344 int j;
2345 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002346 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002347 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002348 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002349 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002350 if (!slices)
2351 return NULL;
2352 for (j = 0; j < NCH(n); j += 2) {
2353 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002354 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002355 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002356 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002357 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002358 asdl_seq_SET(slices, j / 2, slc);
2359 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002360 if (!simple) {
2361 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002362 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002363 }
2364 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002365 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002366 if (!elts)
2367 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002368 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2369 slc = (slice_ty)asdl_seq_GET(slices, j);
2370 assert(slc->kind == Index_kind && slc->v.Index.value);
2371 asdl_seq_SET(elts, j, slc->v.Index.value);
2372 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002373 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002374 if (!e)
2375 return NULL;
2376 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002377 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002378 }
2379 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002380}
2381
2382static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002383ast_for_factor(struct compiling *c, const node *n)
2384{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002385 expr_ty expression;
2386
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002387 expression = ast_for_expr(c, CHILD(n, 1));
2388 if (!expression)
2389 return NULL;
2390
2391 switch (TYPE(CHILD(n, 0))) {
2392 case PLUS:
2393 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2394 c->c_arena);
2395 case MINUS:
2396 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2397 c->c_arena);
2398 case TILDE:
2399 return UnaryOp(Invert, expression, LINENO(n),
2400 n->n_col_offset, c->c_arena);
2401 }
2402 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2403 TYPE(CHILD(n, 0)));
2404 return NULL;
2405}
2406
2407static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002408ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002409{
Yury Selivanov75445082015-05-11 22:57:16 -04002410 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002411 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002412
2413 REQ(n, atom_expr);
2414 nch = NCH(n);
2415
2416 if (TYPE(CHILD(n, 0)) == AWAIT) {
2417 start = 1;
2418 assert(nch > 1);
2419 }
2420
2421 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002422 if (!e)
2423 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002424 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002425 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002426 if (start && nch == 2) {
2427 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2428 }
2429
2430 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002431 node *ch = CHILD(n, i);
2432 if (TYPE(ch) != trailer)
2433 break;
2434 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002435 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002436 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002437 tmp->lineno = e->lineno;
2438 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002439 e = tmp;
2440 }
Yury Selivanov75445082015-05-11 22:57:16 -04002441
2442 if (start) {
2443 /* there was an AWAIT */
2444 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2445 }
2446 else {
2447 return e;
2448 }
2449}
2450
2451static expr_ty
2452ast_for_power(struct compiling *c, const node *n)
2453{
2454 /* power: atom trailer* ('**' factor)*
2455 */
2456 expr_ty e;
2457 REQ(n, power);
2458 e = ast_for_atom_expr(c, CHILD(n, 0));
2459 if (!e)
2460 return NULL;
2461 if (NCH(n) == 1)
2462 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002463 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2464 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002465 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002466 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002467 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002468 }
2469 return e;
2470}
2471
Guido van Rossum0368b722007-05-11 16:50:42 +00002472static expr_ty
2473ast_for_starred(struct compiling *c, const node *n)
2474{
2475 expr_ty tmp;
2476 REQ(n, star_expr);
2477
2478 tmp = ast_for_expr(c, CHILD(n, 1));
2479 if (!tmp)
2480 return NULL;
2481
2482 /* The Load context is changed later. */
2483 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2484}
2485
2486
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487/* Do not name a variable 'expr'! Will cause a compile error.
2488*/
2489
2490static expr_ty
2491ast_for_expr(struct compiling *c, const node *n)
2492{
2493 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002494 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002495 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 and_test: not_test ('and' not_test)*
2498 not_test: 'not' not_test | comparison
2499 comparison: expr (comp_op expr)*
2500 expr: xor_expr ('|' xor_expr)*
2501 xor_expr: and_expr ('^' and_expr)*
2502 and_expr: shift_expr ('&' shift_expr)*
2503 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2504 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002505 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002507 power: atom_expr ['**' factor]
2508 atom_expr: [AWAIT] atom trailer*
2509 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 */
2511
2512 asdl_seq *seq;
2513 int i;
2514
2515 loop:
2516 switch (TYPE(n)) {
2517 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002518 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002519 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002520 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002522 else if (NCH(n) > 1)
2523 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002524 /* Fallthrough */
2525 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 case and_test:
2527 if (NCH(n) == 1) {
2528 n = CHILD(n, 0);
2529 goto loop;
2530 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002531 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 if (!seq)
2533 return NULL;
2534 for (i = 0; i < NCH(n); i += 2) {
2535 expr_ty e = ast_for_expr(c, CHILD(n, i));
2536 if (!e)
2537 return NULL;
2538 asdl_seq_SET(seq, i / 2, e);
2539 }
2540 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002541 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2542 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002543 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002544 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 case not_test:
2546 if (NCH(n) == 1) {
2547 n = CHILD(n, 0);
2548 goto loop;
2549 }
2550 else {
2551 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2552 if (!expression)
2553 return NULL;
2554
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002555 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2556 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 }
2558 case comparison:
2559 if (NCH(n) == 1) {
2560 n = CHILD(n, 0);
2561 goto loop;
2562 }
2563 else {
2564 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002565 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002566 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002567 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 if (!ops)
2569 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002570 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 return NULL;
2573 }
2574 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002575 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002577 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002578 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581
2582 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002583 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002585 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002587 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 asdl_seq_SET(cmps, i / 2, expression);
2589 }
2590 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002591 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002595 return Compare(expression, ops, cmps, LINENO(n),
2596 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 }
2598 break;
2599
Guido van Rossum0368b722007-05-11 16:50:42 +00002600 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 /* The next five cases all handle BinOps. The main body of code
2603 is the same in each case, but the switch turned inside out to
2604 reuse the code for each type of operator.
2605 */
2606 case expr:
2607 case xor_expr:
2608 case and_expr:
2609 case shift_expr:
2610 case arith_expr:
2611 case term:
2612 if (NCH(n) == 1) {
2613 n = CHILD(n, 0);
2614 goto loop;
2615 }
2616 return ast_for_binop(c, n);
2617 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002618 node *an = NULL;
2619 node *en = NULL;
2620 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002621 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002622 if (NCH(n) > 1)
2623 an = CHILD(n, 1); /* yield_arg */
2624 if (an) {
2625 en = CHILD(an, NCH(an) - 1);
2626 if (NCH(an) == 2) {
2627 is_from = 1;
2628 exp = ast_for_expr(c, en);
2629 }
2630 else
2631 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 if (!exp)
2633 return NULL;
2634 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002635 if (is_from)
2636 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2637 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002638 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002639 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 if (NCH(n) == 1) {
2641 n = CHILD(n, 0);
2642 goto loop;
2643 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002644 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002645 case power:
2646 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002648 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 return NULL;
2650 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002651 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 return NULL;
2653}
2654
2655static expr_ty
2656ast_for_call(struct compiling *c, const node *n, expr_ty func)
2657{
2658 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002659 arglist: argument (',' argument)* [',']
2660 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 */
2662
2663 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002664 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002665 asdl_seq *args;
2666 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667
2668 REQ(n, arglist);
2669
2670 nargs = 0;
2671 nkeywords = 0;
2672 ngens = 0;
2673 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002674 node *ch = CHILD(n, i);
2675 if (TYPE(ch) == argument) {
2676 if (NCH(ch) == 1)
2677 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002678 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002679 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002680 else if (TYPE(CHILD(ch, 0)) == STAR)
2681 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002683 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002684 nkeywords++;
2685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 }
2687 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002688 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002689 "if not sole argument");
2690 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 }
2692
2693 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002694 ast_error(c, n, "more than 255 arguments");
2695 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 }
2697
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002698 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002700 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002701 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002703 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002704
2705 nargs = 0; /* positional arguments + iterable argument unpackings */
2706 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2707 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002709 node *ch = CHILD(n, i);
2710 if (TYPE(ch) == argument) {
2711 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002712 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002713 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002714 /* a positional argument */
2715 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 if (ndoublestars) {
2717 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002718 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002719 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002720 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002721 else {
2722 ast_error(c, chch,
2723 "positional argument follows "
2724 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002725 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002726 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002727 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002728 e = ast_for_expr(c, chch);
2729 if (!e)
2730 return NULL;
2731 asdl_seq_SET(args, nargs++, e);
2732 }
2733 else if (TYPE(chch) == STAR) {
2734 /* an iterable argument unpacking */
2735 expr_ty starred;
2736 if (ndoublestars) {
2737 ast_error(c, chch,
2738 "iterable argument unpacking follows "
2739 "keyword argument unpacking");
2740 return NULL;
2741 }
2742 e = ast_for_expr(c, CHILD(ch, 1));
2743 if (!e)
2744 return NULL;
2745 starred = Starred(e, Load, LINENO(chch),
2746 chch->n_col_offset,
2747 c->c_arena);
2748 if (!starred)
2749 return NULL;
2750 asdl_seq_SET(args, nargs++, starred);
2751
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002752 }
2753 else if (TYPE(chch) == DOUBLESTAR) {
2754 /* a keyword argument unpacking */
2755 keyword_ty kw;
2756 i++;
2757 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002759 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 kw = keyword(NULL, e, c->c_arena);
2761 asdl_seq_SET(keywords, nkeywords++, kw);
2762 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002764 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002765 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002766 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002768 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002769 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002771 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002772 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002773 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002774 identifier key, tmp;
2775 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002777 /* chch is test, but must be an identifier? */
2778 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 /* f(lambda x: x[0] = 3) ends up getting parsed with
2782 * LHS test = lambda x: x[0], and RHS test = 3.
2783 * SF bug 132313 points out that complaining about a keyword
2784 * then is very confusing.
2785 */
2786 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002787 ast_error(c, chch,
2788 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002789 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002790 }
2791 else if (e->kind != Name_kind) {
2792 ast_error(c, chch,
2793 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002794 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002795 }
2796 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002797 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002799 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002800 for (k = 0; k < nkeywords; k++) {
2801 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002802 if (tmp && !PyUnicode_Compare(tmp, key)) {
2803 ast_error(c, chch,
2804 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002805 return NULL;
2806 }
2807 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002808 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002810 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002811 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002813 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002814 asdl_seq_SET(keywords, nkeywords++, kw);
2815 }
2816 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 }
2818
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002819 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820}
2821
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002823ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002825 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002826 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002828 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002829 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002830 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002831 }
2832 else {
2833 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002834 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002835 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002837 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838 else {
2839 asdl_seq *tmp = seq_for_testlist(c, n);
2840 if (!tmp)
2841 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002842 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002844}
2845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846static stmt_ty
2847ast_for_expr_stmt(struct compiling *c, const node *n)
2848{
2849 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002852 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002853 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002854 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002855 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 */
2857
2858 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 if (!e)
2861 return NULL;
2862
Thomas Wouters89f507f2006-12-13 04:49:30 +00002863 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 }
2865 else if (TYPE(CHILD(n, 1)) == augassign) {
2866 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002867 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002868 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869
Thomas Wouters89f507f2006-12-13 04:49:30 +00002870 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 if (!expr1)
2872 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002873 if(!set_context(c, expr1, Store, ch))
2874 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002875 /* set_context checks that most expressions are not the left side.
2876 Augmented assignments can only have a name, a subscript, or an
2877 attribute on the left, though, so we have to explicitly check for
2878 those. */
2879 switch (expr1->kind) {
2880 case Name_kind:
2881 case Attribute_kind:
2882 case Subscript_kind:
2883 break;
2884 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002885 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002886 return NULL;
2887 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888
Thomas Wouters89f507f2006-12-13 04:49:30 +00002889 ch = CHILD(n, 2);
2890 if (TYPE(ch) == testlist)
2891 expr2 = ast_for_testlist(c, ch);
2892 else
2893 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002894 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 return NULL;
2896
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002897 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002898 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 return NULL;
2900
Thomas Wouters89f507f2006-12-13 04:49:30 +00002901 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902 }
2903 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002904 int i;
2905 asdl_seq *targets;
2906 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 expr_ty expression;
2908
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 /* a normal assignment */
2910 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002911 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002912 if (!targets)
2913 return NULL;
2914 for (i = 0; i < NCH(n) - 2; i += 2) {
2915 expr_ty e;
2916 node *ch = CHILD(n, i);
2917 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002918 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 return NULL;
2920 }
2921 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002923 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002925 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002926 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002927 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002928
Thomas Wouters89f507f2006-12-13 04:49:30 +00002929 asdl_seq_SET(targets, i / 2, e);
2930 }
2931 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002932 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002933 expression = ast_for_testlist(c, value);
2934 else
2935 expression = ast_for_expr(c, value);
2936 if (!expression)
2937 return NULL;
2938 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940}
2941
Benjamin Peterson78565b22009-06-28 19:19:51 +00002942
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002944ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945{
2946 asdl_seq *seq;
2947 int i;
2948 expr_ty e;
2949
2950 REQ(n, exprlist);
2951
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002952 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002956 e = ast_for_expr(c, CHILD(n, i));
2957 if (!e)
2958 return NULL;
2959 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002960 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002961 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 }
2963 return seq;
2964}
2965
2966static stmt_ty
2967ast_for_del_stmt(struct compiling *c, const node *n)
2968{
2969 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 /* del_stmt: 'del' exprlist */
2972 REQ(n, del_stmt);
2973
2974 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2975 if (!expr_list)
2976 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002977 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978}
2979
2980static stmt_ty
2981ast_for_flow_stmt(struct compiling *c, const node *n)
2982{
2983 /*
2984 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2985 | yield_stmt
2986 break_stmt: 'break'
2987 continue_stmt: 'continue'
2988 return_stmt: 'return' [testlist]
2989 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002990 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991 raise_stmt: 'raise' [test [',' test [',' test]]]
2992 */
2993 node *ch;
2994
2995 REQ(n, flow_stmt);
2996 ch = CHILD(n, 0);
2997 switch (TYPE(ch)) {
2998 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002999 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003001 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003003 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3004 if (!exp)
3005 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003006 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007 }
3008 case return_stmt:
3009 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003010 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003012 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 if (!expression)
3014 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003015 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 }
3017 case raise_stmt:
3018 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003019 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3020 else if (NCH(ch) >= 2) {
3021 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3023 if (!expression)
3024 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003025 if (NCH(ch) == 4) {
3026 cause = ast_for_expr(c, CHILD(ch, 3));
3027 if (!cause)
3028 return NULL;
3029 }
3030 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 }
3032 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003033 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034 "unexpected flow_stmt: %d", TYPE(ch));
3035 return NULL;
3036 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003037
3038 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3039 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040}
3041
3042static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003043alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044{
3045 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003046 import_as_name: NAME ['as' NAME]
3047 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 dotted_name: NAME ('.' NAME)*
3049 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003050 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 loop:
3053 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003054 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003055 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003056 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003057 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003058 if (!name)
3059 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003060 if (NCH(n) == 3) {
3061 node *str_node = CHILD(n, 2);
3062 str = NEW_IDENTIFIER(str_node);
3063 if (!str)
3064 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003065 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003066 return NULL;
3067 }
3068 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003069 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003070 return NULL;
3071 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003072 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003073 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 case dotted_as_name:
3075 if (NCH(n) == 1) {
3076 n = CHILD(n, 0);
3077 goto loop;
3078 }
3079 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003080 node *asname_node = CHILD(n, 2);
3081 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003082 if (!a)
3083 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003085 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003086 if (!a->asname)
3087 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003088 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003089 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090 return a;
3091 }
3092 break;
3093 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003094 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003095 node *name_node = CHILD(n, 0);
3096 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003097 if (!name)
3098 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003099 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003100 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003101 return alias(name, NULL, c->c_arena);
3102 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103 else {
3104 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003105 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003106 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109
3110 len = 0;
3111 for (i = 0; i < NCH(n); i += 2)
3112 /* length of string plus one for the dot */
3113 len += strlen(STR(CHILD(n, i))) + 1;
3114 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003115 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 if (!str)
3117 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003118 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 if (!s)
3120 return NULL;
3121 for (i = 0; i < NCH(n); i += 2) {
3122 char *sch = STR(CHILD(n, i));
3123 strcpy(s, STR(CHILD(n, i)));
3124 s += strlen(sch);
3125 *s++ = '.';
3126 }
3127 --s;
3128 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3130 PyBytes_GET_SIZE(str),
3131 NULL);
3132 Py_DECREF(str);
3133 if (!uni)
3134 return NULL;
3135 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003136 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003137 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3138 Py_DECREF(str);
3139 return NULL;
3140 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003141 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 }
3143 break;
3144 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003145 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003146 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3147 Py_DECREF(str);
3148 return NULL;
3149 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003150 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003152 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 "unexpected import name: %d", TYPE(n));
3154 return NULL;
3155 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003156
3157 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 return NULL;
3159}
3160
3161static stmt_ty
3162ast_for_import_stmt(struct compiling *c, const node *n)
3163{
3164 /*
3165 import_stmt: import_name | import_from
3166 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003167 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3168 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003170 int lineno;
3171 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 int i;
3173 asdl_seq *aliases;
3174
3175 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003176 lineno = LINENO(n);
3177 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003179 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003181 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003182 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003183 if (!aliases)
3184 return NULL;
3185 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003186 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003187 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003189 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003191 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003193 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003195 int idx, ndots = 0;
3196 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003197 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003199 /* Count the number of dots (for relative imports) and check for the
3200 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003201 for (idx = 1; idx < NCH(n); idx++) {
3202 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003203 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3204 if (!mod)
3205 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003206 idx++;
3207 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003208 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003210 ndots += 3;
3211 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003212 } else if (TYPE(CHILD(n, idx)) != DOT) {
3213 break;
3214 }
3215 ndots++;
3216 }
3217 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003218 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003219 case STAR:
3220 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003221 n = CHILD(n, idx);
3222 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003223 break;
3224 case LPAR:
3225 /* from ... import (x, y, z) */
3226 n = CHILD(n, idx + 1);
3227 n_children = NCH(n);
3228 break;
3229 case import_as_names:
3230 /* from ... import x, y, z */
3231 n = CHILD(n, idx);
3232 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003233 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003234 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235 " surrounding parentheses");
3236 return NULL;
3237 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003238 break;
3239 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003240 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003241 return NULL;
3242 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003243
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003244 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003245 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247
3248 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003249 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003250 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003251 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003253 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003255 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003256 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003257 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003258 if (!import_alias)
3259 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003260 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003263 if (mod != NULL)
3264 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003265 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003266 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 }
Neal Norwitz79792652005-11-14 04:25:03 +00003268 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269 "unknown import statement: starts with command '%s'",
3270 STR(CHILD(n, 0)));
3271 return NULL;
3272}
3273
3274static stmt_ty
3275ast_for_global_stmt(struct compiling *c, const node *n)
3276{
3277 /* global_stmt: 'global' NAME (',' NAME)* */
3278 identifier name;
3279 asdl_seq *s;
3280 int i;
3281
3282 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003283 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003285 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003287 name = NEW_IDENTIFIER(CHILD(n, i));
3288 if (!name)
3289 return NULL;
3290 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003292 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293}
3294
3295static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003296ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3297{
3298 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3299 identifier name;
3300 asdl_seq *s;
3301 int i;
3302
3303 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003304 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003305 if (!s)
3306 return NULL;
3307 for (i = 1; i < NCH(n); i += 2) {
3308 name = NEW_IDENTIFIER(CHILD(n, i));
3309 if (!name)
3310 return NULL;
3311 asdl_seq_SET(s, i / 2, name);
3312 }
3313 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3314}
3315
3316static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317ast_for_assert_stmt(struct compiling *c, const node *n)
3318{
3319 /* assert_stmt: 'assert' test [',' test] */
3320 REQ(n, assert_stmt);
3321 if (NCH(n) == 2) {
3322 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3323 if (!expression)
3324 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326 }
3327 else if (NCH(n) == 4) {
3328 expr_ty expr1, expr2;
3329
3330 expr1 = ast_for_expr(c, CHILD(n, 1));
3331 if (!expr1)
3332 return NULL;
3333 expr2 = ast_for_expr(c, CHILD(n, 3));
3334 if (!expr2)
3335 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336
Thomas Wouters89f507f2006-12-13 04:49:30 +00003337 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338 }
Neal Norwitz79792652005-11-14 04:25:03 +00003339 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340 "improper number of parts to 'assert' statement: %d",
3341 NCH(n));
3342 return NULL;
3343}
3344
3345static asdl_seq *
3346ast_for_suite(struct compiling *c, const node *n)
3347{
3348 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003349 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350 stmt_ty s;
3351 int i, total, num, end, pos = 0;
3352 node *ch;
3353
3354 REQ(n, suite);
3355
3356 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003357 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003359 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003361 n = CHILD(n, 0);
3362 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003364 */
3365 end = NCH(n) - 1;
3366 if (TYPE(CHILD(n, end - 1)) == SEMI)
3367 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003369 for (i = 0; i < end; i += 2) {
3370 ch = CHILD(n, i);
3371 s = ast_for_stmt(c, ch);
3372 if (!s)
3373 return NULL;
3374 asdl_seq_SET(seq, pos++, s);
3375 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376 }
3377 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003378 for (i = 2; i < (NCH(n) - 1); i++) {
3379 ch = CHILD(n, i);
3380 REQ(ch, stmt);
3381 num = num_stmts(ch);
3382 if (num == 1) {
3383 /* small_stmt or compound_stmt with only one child */
3384 s = ast_for_stmt(c, ch);
3385 if (!s)
3386 return NULL;
3387 asdl_seq_SET(seq, pos++, s);
3388 }
3389 else {
3390 int j;
3391 ch = CHILD(ch, 0);
3392 REQ(ch, simple_stmt);
3393 for (j = 0; j < NCH(ch); j += 2) {
3394 /* statement terminates with a semi-colon ';' */
3395 if (NCH(CHILD(ch, j)) == 0) {
3396 assert((j + 1) == NCH(ch));
3397 break;
3398 }
3399 s = ast_for_stmt(c, CHILD(ch, j));
3400 if (!s)
3401 return NULL;
3402 asdl_seq_SET(seq, pos++, s);
3403 }
3404 }
3405 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 }
3407 assert(pos == seq->size);
3408 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409}
3410
3411static stmt_ty
3412ast_for_if_stmt(struct compiling *c, const node *n)
3413{
3414 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3415 ['else' ':' suite]
3416 */
3417 char *s;
3418
3419 REQ(n, if_stmt);
3420
3421 if (NCH(n) == 4) {
3422 expr_ty expression;
3423 asdl_seq *suite_seq;
3424
3425 expression = ast_for_expr(c, CHILD(n, 1));
3426 if (!expression)
3427 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003429 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431
Guido van Rossumd8faa362007-04-27 19:54:29 +00003432 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3433 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003435
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 s = STR(CHILD(n, 4));
3437 /* s[2], the third character in the string, will be
3438 's' for el_s_e, or
3439 'i' for el_i_f
3440 */
3441 if (s[2] == 's') {
3442 expr_ty expression;
3443 asdl_seq *seq1, *seq2;
3444
3445 expression = ast_for_expr(c, CHILD(n, 1));
3446 if (!expression)
3447 return NULL;
3448 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003449 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 return NULL;
3451 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003452 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 return NULL;
3454
Guido van Rossumd8faa362007-04-27 19:54:29 +00003455 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3456 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 }
3458 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003459 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003460 expr_ty expression;
3461 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003462 asdl_seq *orelse = NULL;
3463 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 /* must reference the child n_elif+1 since 'else' token is third,
3465 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003466 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3467 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3468 has_else = 1;
3469 n_elif -= 3;
3470 }
3471 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472
Thomas Wouters89f507f2006-12-13 04:49:30 +00003473 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003474 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003476 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003477 if (!orelse)
3478 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003480 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003482 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3483 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003485 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3486 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 asdl_seq_SET(orelse, 0,
3490 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003491 LINENO(CHILD(n, NCH(n) - 6)),
3492 CHILD(n, NCH(n) - 6)->n_col_offset,
3493 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003494 /* the just-created orelse handled the last elif */
3495 n_elif--;
3496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497
Thomas Wouters89f507f2006-12-13 04:49:30 +00003498 for (i = 0; i < n_elif; i++) {
3499 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003500 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003501 if (!newobj)
3502 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003504 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003507 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509
Thomas Wouters89f507f2006-12-13 04:49:30 +00003510 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003512 LINENO(CHILD(n, off)),
3513 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003514 orelse = newobj;
3515 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003516 expression = ast_for_expr(c, CHILD(n, 1));
3517 if (!expression)
3518 return NULL;
3519 suite_seq = ast_for_suite(c, CHILD(n, 3));
3520 if (!suite_seq)
3521 return NULL;
3522 return If(expression, suite_seq, orelse,
3523 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003525
3526 PyErr_Format(PyExc_SystemError,
3527 "unexpected token in 'if' statement: %s", s);
3528 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529}
3530
3531static stmt_ty
3532ast_for_while_stmt(struct compiling *c, const node *n)
3533{
3534 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3535 REQ(n, while_stmt);
3536
3537 if (NCH(n) == 4) {
3538 expr_ty expression;
3539 asdl_seq *suite_seq;
3540
3541 expression = ast_for_expr(c, CHILD(n, 1));
3542 if (!expression)
3543 return NULL;
3544 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003545 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003547 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 }
3549 else if (NCH(n) == 7) {
3550 expr_ty expression;
3551 asdl_seq *seq1, *seq2;
3552
3553 expression = ast_for_expr(c, CHILD(n, 1));
3554 if (!expression)
3555 return NULL;
3556 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003557 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 return NULL;
3559 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003560 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561 return NULL;
3562
Thomas Wouters89f507f2006-12-13 04:49:30 +00003563 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003565
3566 PyErr_Format(PyExc_SystemError,
3567 "wrong number of tokens for 'while' statement: %d",
3568 NCH(n));
3569 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570}
3571
3572static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003573ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003575 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003577 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003578 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3580 REQ(n, for_stmt);
3581
3582 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003583 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 if (!seq)
3585 return NULL;
3586 }
3587
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003588 node_target = CHILD(n, 1);
3589 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003590 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003592 /* Check the # of children rather than the length of _target, since
3593 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003594 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003595 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003596 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003598 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003600 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003601 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 return NULL;
3603 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003604 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 return NULL;
3606
Yury Selivanov75445082015-05-11 22:57:16 -04003607 if (is_async)
3608 return AsyncFor(target, expression, suite_seq, seq,
3609 LINENO(n), n->n_col_offset,
3610 c->c_arena);
3611 else
3612 return For(target, expression, suite_seq, seq,
3613 LINENO(n), n->n_col_offset,
3614 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615}
3616
3617static excepthandler_ty
3618ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3619{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003620 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621 REQ(exc, except_clause);
3622 REQ(body, suite);
3623
3624 if (NCH(exc) == 1) {
3625 asdl_seq *suite_seq = ast_for_suite(c, body);
3626 if (!suite_seq)
3627 return NULL;
3628
Neal Norwitzad74aa82008-03-31 05:14:30 +00003629 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003630 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 }
3632 else if (NCH(exc) == 2) {
3633 expr_ty expression;
3634 asdl_seq *suite_seq;
3635
3636 expression = ast_for_expr(c, CHILD(exc, 1));
3637 if (!expression)
3638 return NULL;
3639 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003640 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641 return NULL;
3642
Neal Norwitzad74aa82008-03-31 05:14:30 +00003643 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003644 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 }
3646 else if (NCH(exc) == 4) {
3647 asdl_seq *suite_seq;
3648 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003649 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003650 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003652 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003653 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003655 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 return NULL;
3657 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003658 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 return NULL;
3660
Neal Norwitzad74aa82008-03-31 05:14:30 +00003661 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003662 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003664
3665 PyErr_Format(PyExc_SystemError,
3666 "wrong number of children for 'except' clause: %d",
3667 NCH(exc));
3668 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669}
3670
3671static stmt_ty
3672ast_for_try_stmt(struct compiling *c, const node *n)
3673{
Neal Norwitzf599f422005-12-17 21:33:47 +00003674 const int nch = NCH(n);
3675 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003676 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003677
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678 REQ(n, try_stmt);
3679
Neal Norwitzf599f422005-12-17 21:33:47 +00003680 body = ast_for_suite(c, CHILD(n, 2));
3681 if (body == NULL)
3682 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683
Neal Norwitzf599f422005-12-17 21:33:47 +00003684 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3685 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3686 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3687 /* we can assume it's an "else",
3688 because nch >= 9 for try-else-finally and
3689 it would otherwise have a type of except_clause */
3690 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3691 if (orelse == NULL)
3692 return NULL;
3693 n_except--;
3694 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695
Neal Norwitzf599f422005-12-17 21:33:47 +00003696 finally = ast_for_suite(c, CHILD(n, nch - 1));
3697 if (finally == NULL)
3698 return NULL;
3699 n_except--;
3700 }
3701 else {
3702 /* we can assume it's an "else",
3703 otherwise it would have a type of except_clause */
3704 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3705 if (orelse == NULL)
3706 return NULL;
3707 n_except--;
3708 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003710 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003711 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712 return NULL;
3713 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714
Neal Norwitzf599f422005-12-17 21:33:47 +00003715 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003716 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003717 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003718 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003719 if (handlers == NULL)
3720 return NULL;
3721
3722 for (i = 0; i < n_except; i++) {
3723 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3724 CHILD(n, 5 + i * 3));
3725 if (!e)
3726 return NULL;
3727 asdl_seq_SET(handlers, i, e);
3728 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003729 }
3730
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003731 assert(finally != NULL || asdl_seq_LEN(handlers));
3732 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733}
3734
Georg Brandl0c315622009-05-25 21:10:36 +00003735/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003736static withitem_ty
3737ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003738{
3739 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003740
Georg Brandl0c315622009-05-25 21:10:36 +00003741 REQ(n, with_item);
3742 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003743 if (!context_expr)
3744 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003745 if (NCH(n) == 3) {
3746 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003747
3748 if (!optional_vars) {
3749 return NULL;
3750 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003751 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003752 return NULL;
3753 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003754 }
3755
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003756 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003757}
3758
Georg Brandl0c315622009-05-25 21:10:36 +00003759/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3760static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003761ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003762{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003763 int i, n_items;
3764 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003765
3766 REQ(n, with_stmt);
3767
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003768 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003769 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003770 if (!items)
3771 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003772 for (i = 1; i < NCH(n) - 2; i += 2) {
3773 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3774 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003775 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003776 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003777 }
3778
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003779 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3780 if (!body)
3781 return NULL;
3782
Yury Selivanov75445082015-05-11 22:57:16 -04003783 if (is_async)
3784 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3785 else
3786 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003787}
3788
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003790ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003792 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003793 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003794 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003795 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003796
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797 REQ(n, classdef);
3798
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003799 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 s = ast_for_suite(c, CHILD(n, 3));
3801 if (!s)
3802 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003803 classname = NEW_IDENTIFIER(CHILD(n, 1));
3804 if (!classname)
3805 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003806 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003807 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003808 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3809 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003811
3812 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003813 s = ast_for_suite(c, CHILD(n,5));
3814 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003815 return NULL;
3816 classname = NEW_IDENTIFIER(CHILD(n, 1));
3817 if (!classname)
3818 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003819 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003820 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003821 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3822 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823 }
3824
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003825 /* class NAME '(' arglist ')' ':' suite */
3826 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003827 {
3828 PyObject *dummy_name;
3829 expr_ty dummy;
3830 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3831 if (!dummy_name)
3832 return NULL;
3833 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3834 call = ast_for_call(c, CHILD(n, 3), dummy);
3835 if (!call)
3836 return NULL;
3837 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003839 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003841 classname = NEW_IDENTIFIER(CHILD(n, 1));
3842 if (!classname)
3843 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003844 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003845 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003846
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003847 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003848 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003849}
3850
3851static stmt_ty
3852ast_for_stmt(struct compiling *c, const node *n)
3853{
3854 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003855 assert(NCH(n) == 1);
3856 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 }
3858 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003859 assert(num_stmts(n) == 1);
3860 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861 }
3862 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003863 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003864 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3865 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003866 */
3867 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 case expr_stmt:
3869 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870 case del_stmt:
3871 return ast_for_del_stmt(c, n);
3872 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003873 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874 case flow_stmt:
3875 return ast_for_flow_stmt(c, n);
3876 case import_stmt:
3877 return ast_for_import_stmt(c, n);
3878 case global_stmt:
3879 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003880 case nonlocal_stmt:
3881 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882 case assert_stmt:
3883 return ast_for_assert_stmt(c, n);
3884 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003885 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3887 TYPE(n), NCH(n));
3888 return NULL;
3889 }
3890 }
3891 else {
3892 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003893 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003894 */
3895 node *ch = CHILD(n, 0);
3896 REQ(n, compound_stmt);
3897 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898 case if_stmt:
3899 return ast_for_if_stmt(c, ch);
3900 case while_stmt:
3901 return ast_for_while_stmt(c, ch);
3902 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003903 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904 case try_stmt:
3905 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003906 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003907 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003909 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003911 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 case decorated:
3913 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003914 case async_stmt:
3915 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003917 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3919 TYPE(n), NCH(n));
3920 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003921 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 }
3923}
3924
3925static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003926parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003928 const char *end;
3929 long x;
3930 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003931 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003932 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003934 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003935 errno = 0;
3936 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003937 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003938 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003939 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003940 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003941 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003942 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003943 }
3944 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003945 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003946 if (*end == '\0') {
3947 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003948 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003949 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003950 }
3951 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003952 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003953 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003954 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3955 if (compl.imag == -1.0 && PyErr_Occurred())
3956 return NULL;
3957 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003958 }
3959 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003960 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003961 dx = PyOS_string_to_double(s, NULL, NULL);
3962 if (dx == -1.0 && PyErr_Occurred())
3963 return NULL;
3964 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003965 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966}
3967
3968static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003969decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003971 const char *s, *t;
3972 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003973 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3974 while (s < end && (*s & 0x80)) s++;
3975 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003976 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977}
3978
3979static PyObject *
Benjamin Peterson768921c2016-02-25 23:13:53 -08003980decode_unicode_with_escapes(struct compiling *c, const char *s, size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003982 PyObject *v, *u;
3983 char *buf;
3984 char *p;
3985 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003986
Benjamin Peterson202803a2016-02-25 22:34:45 -08003987 /* check for integer overflow */
3988 if (len > PY_SIZE_MAX / 6)
3989 return NULL;
3990 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3991 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3992 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
3993 if (u == NULL)
3994 return NULL;
3995 p = buf = PyBytes_AsString(u);
3996 end = s + len;
3997 while (s < end) {
3998 if (*s == '\\') {
3999 *p++ = *s++;
4000 if (*s & 0x80) {
4001 strcpy(p, "u005c");
4002 p += 5;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004003 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004004 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004005 if (*s & 0x80) { /* XXX inefficient */
4006 PyObject *w;
4007 int kind;
4008 void *data;
4009 Py_ssize_t len, i;
4010 w = decode_utf8(c, &s, end);
4011 if (w == NULL) {
4012 Py_DECREF(u);
4013 return NULL;
4014 }
4015 kind = PyUnicode_KIND(w);
4016 data = PyUnicode_DATA(w);
4017 len = PyUnicode_GET_LENGTH(w);
4018 for (i = 0; i < len; i++) {
4019 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4020 sprintf(p, "\\U%08x", chr);
4021 p += 10;
4022 }
4023 /* Should be impossible to overflow */
4024 assert(p - buf <= Py_SIZE(u));
4025 Py_DECREF(w);
4026 } else {
4027 *p++ = *s++;
4028 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004029 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004030 len = p - buf;
4031 s = buf;
4032
Eric V. Smith5567f892015-09-21 13:36:09 -04004033 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004034 Py_XDECREF(u);
4035 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036}
4037
Eric V. Smith235a6f02015-09-19 14:51:32 -04004038/* Compile this expression in to an expr_ty. We know that we can
4039 temporarily modify the character before the start of this string
4040 (it's '{'), and we know we can temporarily modify the character
4041 after this string (it is a '}'). Leverage this to create a
4042 sub-string with enough room for us to add parens around the
4043 expression. This is to allow strings with embedded newlines, for
4044 example. */
4045static expr_ty
Eric V. Smith1d44c412015-09-23 07:49:00 -04004046fstring_compile_expr(PyObject *str, Py_ssize_t expr_start,
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004047 Py_ssize_t expr_end, struct compiling *c, const node *n)
4048
Eric V. Smith235a6f02015-09-19 14:51:32 -04004049{
4050 PyCompilerFlags cf;
4051 mod_ty mod;
4052 char *utf_expr;
4053 Py_ssize_t i;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004054 Py_UCS4 end_ch = -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004055 int all_whitespace;
4056 PyObject *sub = NULL;
4057
4058 /* We only decref sub if we allocated it with a PyUnicode_Substring.
4059 decref_sub records that. */
4060 int decref_sub = 0;
4061
4062 assert(str);
4063
Eric V. Smith1d44c412015-09-23 07:49:00 -04004064 assert(expr_start >= 0 && expr_start < PyUnicode_GET_LENGTH(str));
4065 assert(expr_end >= 0 && expr_end < PyUnicode_GET_LENGTH(str));
4066 assert(expr_end >= expr_start);
4067
Martin Panterc2432f62015-10-07 11:15:15 +00004068 /* There has to be at least one character on each side of the
Eric V. Smith1d44c412015-09-23 07:49:00 -04004069 expression inside this str. This will have been caught before
4070 we're called. */
4071 assert(expr_start >= 1);
4072 assert(expr_end <= PyUnicode_GET_LENGTH(str)-1);
4073
Eric V. Smith235a6f02015-09-19 14:51:32 -04004074 /* If the substring is all whitespace, it's an error. We need to
4075 catch this here, and not when we call PyParser_ASTFromString,
4076 because turning the expression '' in to '()' would go from
4077 being invalid to valid. */
4078 /* Note that this code says an empty string is all
4079 whitespace. That's important. There's a test for it: f'{}'. */
4080 all_whitespace = 1;
4081 for (i = expr_start; i < expr_end; i++) {
4082 if (!Py_UNICODE_ISSPACE(PyUnicode_READ_CHAR(str, i))) {
4083 all_whitespace = 0;
4084 break;
4085 }
4086 }
4087 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004088 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004089 goto error;
4090 }
4091
4092 /* If the substring will be the entire source string, we can't use
4093 PyUnicode_Substring, since it will return another reference to
4094 our original string. Because we're modifying the string in
4095 place, that's a no-no. So, detect that case and just use our
4096 string directly. */
4097
4098 if (expr_start-1 == 0 && expr_end+1 == PyUnicode_GET_LENGTH(str)) {
Eric V. Smith1d44c412015-09-23 07:49:00 -04004099 /* If str is well formed, then the first and last chars must
4100 be '{' and '}', respectively. But, if there's a syntax
4101 error, for example f'{3!', then the last char won't be a
4102 closing brace. So, remember the last character we read in
4103 order for us to restore it. */
4104 end_ch = PyUnicode_ReadChar(str, expr_end-expr_start+1);
4105 assert(end_ch != (Py_UCS4)-1);
4106
4107 /* In all cases, however, start_ch must be '{'. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004108 assert(PyUnicode_ReadChar(str, 0) == '{');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004109
Eric V. Smith235a6f02015-09-19 14:51:32 -04004110 sub = str;
4111 } else {
4112 /* Create a substring object. It must be a new object, with
4113 refcount==1, so that we can modify it. */
4114 sub = PyUnicode_Substring(str, expr_start-1, expr_end+1);
4115 if (!sub)
4116 goto error;
4117 assert(sub != str); /* Make sure it's a new string. */
4118 decref_sub = 1; /* Remember to deallocate it on error. */
4119 }
4120
Eric V. Smith1d44c412015-09-23 07:49:00 -04004121 /* Put () around the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004122 if (PyUnicode_WriteChar(sub, 0, '(') < 0 ||
4123 PyUnicode_WriteChar(sub, expr_end-expr_start+1, ')') < 0)
4124 goto error;
4125
Eric V. Smith235a6f02015-09-19 14:51:32 -04004126 /* No need to free the memory returned here: it's managed by the
4127 string. */
4128 utf_expr = PyUnicode_AsUTF8(sub);
4129 if (!utf_expr)
4130 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004131
4132 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004133 mod = PyParser_ASTFromString(utf_expr, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004134 Py_eval_input, &cf, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004135 if (!mod)
4136 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004137
Eric V. Smith235a6f02015-09-19 14:51:32 -04004138 if (sub != str)
4139 /* Clear instead of decref in case we ever modify this code to change
4140 the error handling: this is safest because the XDECREF won't try
4141 and decref it when it's NULL. */
4142 /* No need to restore the chars in sub, since we know it's getting
4143 ready to get deleted (refcount must be 1, since we got a new string
4144 in PyUnicode_Substring). */
4145 Py_CLEAR(sub);
4146 else {
4147 assert(!decref_sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004148 assert(end_ch != (Py_UCS4)-1);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004149 /* Restore str, which we earlier modified directly. */
4150 if (PyUnicode_WriteChar(str, 0, '{') < 0 ||
Eric V. Smith1d44c412015-09-23 07:49:00 -04004151 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004152 goto error;
4153 }
4154 return mod->v.Expression.body;
4155
4156error:
4157 /* Only decref sub if it was the result of a call to SubString. */
4158 if (decref_sub)
4159 Py_XDECREF(sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004160
4161 if (end_ch != (Py_UCS4)-1) {
4162 /* We only get here if we modified str. Make sure that's the
4163 case: str will be equal to sub. */
4164 if (str == sub) {
4165 /* Don't check the error, because we've already set the
4166 error state (that's why we're in 'error', after
4167 all). */
4168 PyUnicode_WriteChar(str, 0, '{');
4169 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch);
4170 }
4171 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004172 return NULL;
4173}
4174
4175/* Return -1 on error.
4176
4177 Return 0 if we reached the end of the literal.
4178
4179 Return 1 if we haven't reached the end of the literal, but we want
4180 the caller to process the literal up to this point. Used for
4181 doubled braces.
4182*/
4183static int
4184fstring_find_literal(PyObject *str, Py_ssize_t *ofs, PyObject **literal,
4185 int recurse_lvl, struct compiling *c, const node *n)
4186{
4187 /* Get any literal string. It ends when we hit an un-doubled brace, or the
4188 end of the string. */
4189
4190 Py_ssize_t literal_start, literal_end;
4191 int result = 0;
4192
4193 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4194 void *data = PyUnicode_DATA(str);
4195
4196 assert(*literal == NULL);
4197
4198 literal_start = *ofs;
4199 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4200 Py_UCS4 ch = PyUnicode_READ(kind, data, *ofs);
4201 if (ch == '{' || ch == '}') {
4202 /* Check for doubled braces, but only at the top level. If
4203 we checked at every level, then f'{0:{3}}' would fail
4204 with the two closing braces. */
4205 if (recurse_lvl == 0) {
4206 if (*ofs + 1 < PyUnicode_GET_LENGTH(str) &&
4207 PyUnicode_READ(kind, data, *ofs + 1) == ch) {
4208 /* We're going to tell the caller that the literal ends
4209 here, but that they should continue scanning. But also
4210 skip over the second brace when we resume scanning. */
4211 literal_end = *ofs + 1;
4212 *ofs += 2;
4213 result = 1;
4214 goto done;
4215 }
4216
4217 /* Where a single '{' is the start of a new expression, a
4218 single '}' is not allowed. */
4219 if (ch == '}') {
4220 ast_error(c, n, "f-string: single '}' is not allowed");
4221 return -1;
4222 }
4223 }
4224
4225 /* We're either at a '{', which means we're starting another
4226 expression; or a '}', which means we're at the end of this
4227 f-string (for a nested format_spec). */
4228 break;
4229 }
4230 }
4231 literal_end = *ofs;
4232
4233 assert(*ofs == PyUnicode_GET_LENGTH(str) ||
4234 PyUnicode_READ(kind, data, *ofs) == '{' ||
4235 PyUnicode_READ(kind, data, *ofs) == '}');
4236done:
4237 if (literal_start != literal_end) {
4238 *literal = PyUnicode_Substring(str, literal_start, literal_end);
4239 if (!*literal)
4240 return -1;
4241 }
4242
4243 return result;
4244}
4245
4246/* Forward declaration because parsing is recursive. */
4247static expr_ty
4248fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4249 struct compiling *c, const node *n);
4250
4251/* Parse the f-string str, starting at ofs. We know *ofs starts an
4252 expression (so it must be a '{'). Returns the FormattedValue node,
4253 which includes the expression, conversion character, and
4254 format_spec expression.
4255
4256 Note that I don't do a perfect job here: I don't make sure that a
4257 closing brace doesn't match an opening paren, for example. It
4258 doesn't need to error on all invalid expressions, just correctly
4259 find the end of all valid ones. Any errors inside the expression
4260 will be caught when we parse it later. */
4261static int
4262fstring_find_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4263 expr_ty *expression, struct compiling *c, const node *n)
4264{
4265 /* Return -1 on error, else 0. */
4266
4267 Py_ssize_t expr_start;
4268 Py_ssize_t expr_end;
4269 expr_ty simple_expression;
4270 expr_ty format_spec = NULL; /* Optional format specifier. */
4271 Py_UCS4 conversion = -1; /* The conversion char. -1 if not specified. */
4272
4273 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4274 void *data = PyUnicode_DATA(str);
4275
4276 /* 0 if we're not in a string, else the quote char we're trying to
4277 match (single or double quote). */
4278 Py_UCS4 quote_char = 0;
4279
4280 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4281 int string_type = 0;
4282
4283 /* Keep track of nesting level for braces/parens/brackets in
4284 expressions. */
4285 Py_ssize_t nested_depth = 0;
4286
4287 /* Can only nest one level deep. */
4288 if (recurse_lvl >= 2) {
4289 ast_error(c, n, "f-string: expressions nested too deeply");
4290 return -1;
4291 }
4292
4293 /* The first char must be a left brace, or we wouldn't have gotten
4294 here. Skip over it. */
4295 assert(PyUnicode_READ(kind, data, *ofs) == '{');
4296 *ofs += 1;
4297
4298 expr_start = *ofs;
4299 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4300 Py_UCS4 ch;
4301
4302 /* Loop invariants. */
4303 assert(nested_depth >= 0);
4304 assert(*ofs >= expr_start);
4305 if (quote_char)
4306 assert(string_type == 1 || string_type == 3);
4307 else
4308 assert(string_type == 0);
4309
4310 ch = PyUnicode_READ(kind, data, *ofs);
4311 if (quote_char) {
4312 /* We're inside a string. See if we're at the end. */
4313 /* This code needs to implement the same non-error logic
4314 as tok_get from tokenizer.c, at the letter_quote
4315 label. To actually share that code would be a
4316 nightmare. But, it's unlikely to change and is small,
4317 so duplicate it here. Note we don't need to catch all
4318 of the errors, since they'll be caught when parsing the
4319 expression. We just need to match the non-error
4320 cases. Thus we can ignore \n in single-quoted strings,
4321 for example. Or non-terminated strings. */
4322 if (ch == quote_char) {
4323 /* Does this match the string_type (single or triple
4324 quoted)? */
4325 if (string_type == 3) {
4326 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4327 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4328 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4329 /* We're at the end of a triple quoted string. */
4330 *ofs += 2;
4331 string_type = 0;
4332 quote_char = 0;
4333 continue;
4334 }
4335 } else {
4336 /* We're at the end of a normal string. */
4337 quote_char = 0;
4338 string_type = 0;
4339 continue;
4340 }
4341 }
4342 /* We're inside a string, and not finished with the
4343 string. If this is a backslash, skip the next char (it
4344 might be an end quote that needs skipping). Otherwise,
4345 just consume this character normally. */
4346 if (ch == '\\' && *ofs+1 < PyUnicode_GET_LENGTH(str)) {
4347 /* Just skip the next char, whatever it is. */
4348 *ofs += 1;
4349 }
4350 } else if (ch == '\'' || ch == '"') {
4351 /* Is this a triple quoted string? */
4352 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4353 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4354 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4355 string_type = 3;
4356 *ofs += 2;
4357 } else {
4358 /* Start of a normal string. */
4359 string_type = 1;
4360 }
4361 /* Start looking for the end of the string. */
4362 quote_char = ch;
4363 } else if (ch == '[' || ch == '{' || ch == '(') {
4364 nested_depth++;
4365 } else if (nested_depth != 0 &&
4366 (ch == ']' || ch == '}' || ch == ')')) {
4367 nested_depth--;
4368 } else if (ch == '#') {
4369 /* Error: can't include a comment character, inside parens
4370 or not. */
4371 ast_error(c, n, "f-string cannot include '#'");
4372 return -1;
4373 } else if (nested_depth == 0 &&
4374 (ch == '!' || ch == ':' || ch == '}')) {
4375 /* First, test for the special case of "!=". Since '=' is
4376 not an allowed conversion character, nothing is lost in
4377 this test. */
4378 if (ch == '!' && *ofs+1 < PyUnicode_GET_LENGTH(str) &&
4379 PyUnicode_READ(kind, data, *ofs+1) == '=')
4380 /* This isn't a conversion character, just continue. */
4381 continue;
4382
4383 /* Normal way out of this loop. */
4384 break;
4385 } else {
4386 /* Just consume this char and loop around. */
4387 }
4388 }
4389 expr_end = *ofs;
4390 /* If we leave this loop in a string or with mismatched parens, we
4391 don't care. We'll get a syntax error when compiling the
4392 expression. But, we can produce a better error message, so
4393 let's just do that.*/
4394 if (quote_char) {
4395 ast_error(c, n, "f-string: unterminated string");
4396 return -1;
4397 }
4398 if (nested_depth) {
4399 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4400 return -1;
4401 }
4402
Eric V. Smith235a6f02015-09-19 14:51:32 -04004403 if (*ofs >= PyUnicode_GET_LENGTH(str))
4404 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004405
4406 /* Compile the expression as soon as possible, so we show errors
4407 related to the expression before errors related to the
4408 conversion or format_spec. */
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004409 simple_expression = fstring_compile_expr(str, expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004410 if (!simple_expression)
4411 return -1;
4412
4413 /* Check for a conversion char, if present. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004414 if (PyUnicode_READ(kind, data, *ofs) == '!') {
4415 *ofs += 1;
4416 if (*ofs >= PyUnicode_GET_LENGTH(str))
4417 goto unexpected_end_of_string;
4418
4419 conversion = PyUnicode_READ(kind, data, *ofs);
4420 *ofs += 1;
4421
4422 /* Validate the conversion. */
4423 if (!(conversion == 's' || conversion == 'r'
4424 || conversion == 'a')) {
4425 ast_error(c, n, "f-string: invalid conversion character: "
4426 "expected 's', 'r', or 'a'");
4427 return -1;
4428 }
4429 }
4430
4431 /* Check for the format spec, if present. */
4432 if (*ofs >= PyUnicode_GET_LENGTH(str))
4433 goto unexpected_end_of_string;
4434 if (PyUnicode_READ(kind, data, *ofs) == ':') {
4435 *ofs += 1;
4436 if (*ofs >= PyUnicode_GET_LENGTH(str))
4437 goto unexpected_end_of_string;
4438
4439 /* Parse the format spec. */
4440 format_spec = fstring_parse(str, ofs, recurse_lvl+1, c, n);
4441 if (!format_spec)
4442 return -1;
4443 }
4444
4445 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4446 PyUnicode_READ(kind, data, *ofs) != '}')
4447 goto unexpected_end_of_string;
4448
4449 /* We're at a right brace. Consume it. */
4450 assert(*ofs < PyUnicode_GET_LENGTH(str));
4451 assert(PyUnicode_READ(kind, data, *ofs) == '}');
4452 *ofs += 1;
4453
Eric V. Smith235a6f02015-09-19 14:51:32 -04004454 /* And now create the FormattedValue node that represents this entire
4455 expression with the conversion and format spec. */
4456 *expression = FormattedValue(simple_expression, (int)conversion,
4457 format_spec, LINENO(n), n->n_col_offset,
4458 c->c_arena);
4459 if (!*expression)
4460 return -1;
4461
4462 return 0;
4463
4464unexpected_end_of_string:
4465 ast_error(c, n, "f-string: expecting '}'");
4466 return -1;
4467}
4468
4469/* Return -1 on error.
4470
4471 Return 0 if we have a literal (possible zero length) and an
4472 expression (zero length if at the end of the string.
4473
4474 Return 1 if we have a literal, but no expression, and we want the
4475 caller to call us again. This is used to deal with doubled
4476 braces.
4477
4478 When called multiple times on the string 'a{{b{0}c', this function
4479 will return:
4480
4481 1. the literal 'a{' with no expression, and a return value
4482 of 1. Despite the fact that there's no expression, the return
4483 value of 1 means we're not finished yet.
4484
4485 2. the literal 'b' and the expression '0', with a return value of
4486 0. The fact that there's an expression means we're not finished.
4487
4488 3. literal 'c' with no expression and a return value of 0. The
4489 combination of the return value of 0 with no expression means
4490 we're finished.
4491*/
4492static int
4493fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4494 PyObject **literal, expr_ty *expression,
4495 struct compiling *c, const node *n)
4496{
4497 int result;
4498
4499 assert(*literal == NULL && *expression == NULL);
4500
4501 /* Get any literal string. */
4502 result = fstring_find_literal(str, ofs, literal, recurse_lvl, c, n);
4503 if (result < 0)
4504 goto error;
4505
4506 assert(result == 0 || result == 1);
4507
4508 if (result == 1)
4509 /* We have a literal, but don't look at the expression. */
4510 return 1;
4511
4512 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4513
4514 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4515 PyUnicode_READ_CHAR(str, *ofs) == '}')
4516 /* We're at the end of the string or the end of a nested
4517 f-string: no expression. The top-level error case where we
4518 expect to be at the end of the string but we're at a '}' is
4519 handled later. */
4520 return 0;
4521
4522 /* We must now be the start of an expression, on a '{'. */
4523 assert(*ofs < PyUnicode_GET_LENGTH(str) &&
4524 PyUnicode_READ_CHAR(str, *ofs) == '{');
4525
4526 if (fstring_find_expr(str, ofs, recurse_lvl, expression, c, n) < 0)
4527 goto error;
4528
4529 return 0;
4530
4531error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004532 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004533 return -1;
4534}
4535
4536#define EXPRLIST_N_CACHED 64
4537
4538typedef struct {
4539 /* Incrementally build an array of expr_ty, so be used in an
4540 asdl_seq. Cache some small but reasonably sized number of
4541 expr_ty's, and then after that start dynamically allocating,
4542 doubling the number allocated each time. Note that the f-string
4543 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4544 Str for the literal 'a'. So you add expr_ty's about twice as
4545 fast as you add exressions in an f-string. */
4546
4547 Py_ssize_t allocated; /* Number we've allocated. */
4548 Py_ssize_t size; /* Number we've used. */
4549 expr_ty *p; /* Pointer to the memory we're actually
4550 using. Will point to 'data' until we
4551 start dynamically allocating. */
4552 expr_ty data[EXPRLIST_N_CACHED];
4553} ExprList;
4554
4555#ifdef NDEBUG
4556#define ExprList_check_invariants(l)
4557#else
4558static void
4559ExprList_check_invariants(ExprList *l)
4560{
4561 /* Check our invariants. Make sure this object is "live", and
4562 hasn't been deallocated. */
4563 assert(l->size >= 0);
4564 assert(l->p != NULL);
4565 if (l->size <= EXPRLIST_N_CACHED)
4566 assert(l->data == l->p);
4567}
4568#endif
4569
4570static void
4571ExprList_Init(ExprList *l)
4572{
4573 l->allocated = EXPRLIST_N_CACHED;
4574 l->size = 0;
4575
4576 /* Until we start allocating dynamically, p points to data. */
4577 l->p = l->data;
4578
4579 ExprList_check_invariants(l);
4580}
4581
4582static int
4583ExprList_Append(ExprList *l, expr_ty exp)
4584{
4585 ExprList_check_invariants(l);
4586 if (l->size >= l->allocated) {
4587 /* We need to alloc (or realloc) the memory. */
4588 Py_ssize_t new_size = l->allocated * 2;
4589
4590 /* See if we've ever allocated anything dynamically. */
4591 if (l->p == l->data) {
4592 Py_ssize_t i;
4593 /* We're still using the cached data. Switch to
4594 alloc-ing. */
4595 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4596 if (!l->p)
4597 return -1;
4598 /* Copy the cached data into the new buffer. */
4599 for (i = 0; i < l->size; i++)
4600 l->p[i] = l->data[i];
4601 } else {
4602 /* Just realloc. */
4603 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4604 if (!tmp) {
4605 PyMem_RawFree(l->p);
4606 l->p = NULL;
4607 return -1;
4608 }
4609 l->p = tmp;
4610 }
4611
4612 l->allocated = new_size;
4613 assert(l->allocated == 2 * l->size);
4614 }
4615
4616 l->p[l->size++] = exp;
4617
4618 ExprList_check_invariants(l);
4619 return 0;
4620}
4621
4622static void
4623ExprList_Dealloc(ExprList *l)
4624{
4625 ExprList_check_invariants(l);
4626
4627 /* If there's been an error, or we've never dynamically allocated,
4628 do nothing. */
4629 if (!l->p || l->p == l->data) {
4630 /* Do nothing. */
4631 } else {
4632 /* We have dynamically allocated. Free the memory. */
4633 PyMem_RawFree(l->p);
4634 }
4635 l->p = NULL;
4636 l->size = -1;
4637}
4638
4639static asdl_seq *
4640ExprList_Finish(ExprList *l, PyArena *arena)
4641{
4642 asdl_seq *seq;
4643
4644 ExprList_check_invariants(l);
4645
4646 /* Allocate the asdl_seq and copy the expressions in to it. */
4647 seq = _Py_asdl_seq_new(l->size, arena);
4648 if (seq) {
4649 Py_ssize_t i;
4650 for (i = 0; i < l->size; i++)
4651 asdl_seq_SET(seq, i, l->p[i]);
4652 }
4653 ExprList_Dealloc(l);
4654 return seq;
4655}
4656
4657/* The FstringParser is designed to add a mix of strings and
4658 f-strings, and concat them together as needed. Ultimately, it
4659 generates an expr_ty. */
4660typedef struct {
4661 PyObject *last_str;
4662 ExprList expr_list;
4663} FstringParser;
4664
4665#ifdef NDEBUG
4666#define FstringParser_check_invariants(state)
4667#else
4668static void
4669FstringParser_check_invariants(FstringParser *state)
4670{
4671 if (state->last_str)
4672 assert(PyUnicode_CheckExact(state->last_str));
4673 ExprList_check_invariants(&state->expr_list);
4674}
4675#endif
4676
4677static void
4678FstringParser_Init(FstringParser *state)
4679{
4680 state->last_str = NULL;
4681 ExprList_Init(&state->expr_list);
4682 FstringParser_check_invariants(state);
4683}
4684
4685static void
4686FstringParser_Dealloc(FstringParser *state)
4687{
4688 FstringParser_check_invariants(state);
4689
4690 Py_XDECREF(state->last_str);
4691 ExprList_Dealloc(&state->expr_list);
4692}
4693
4694/* Make a Str node, but decref the PyUnicode object being added. */
4695static expr_ty
4696make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4697{
4698 PyObject *s = *str;
4699 *str = NULL;
4700 assert(PyUnicode_CheckExact(s));
4701 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4702 Py_DECREF(s);
4703 return NULL;
4704 }
4705 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4706}
4707
4708/* Add a non-f-string (that is, a regular literal string). str is
4709 decref'd. */
4710static int
4711FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4712{
4713 FstringParser_check_invariants(state);
4714
4715 assert(PyUnicode_CheckExact(str));
4716
4717 if (PyUnicode_GET_LENGTH(str) == 0) {
4718 Py_DECREF(str);
4719 return 0;
4720 }
4721
4722 if (!state->last_str) {
4723 /* We didn't have a string before, so just remember this one. */
4724 state->last_str = str;
4725 } else {
4726 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004727 PyUnicode_AppendAndDel(&state->last_str, str);
4728 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004729 return -1;
4730 }
4731 FstringParser_check_invariants(state);
4732 return 0;
4733}
4734
4735/* Parse an f-string. The f-string is in str, starting at ofs, with no 'f'
4736 or quotes. str is not decref'd, since we don't know if it's used elsewhere.
4737 And if we're only looking at a part of a string, then decref'ing is
4738 definitely not the right thing to do! */
4739static int
4740FstringParser_ConcatFstring(FstringParser *state, PyObject *str,
4741 Py_ssize_t *ofs, int recurse_lvl,
4742 struct compiling *c, const node *n)
4743{
4744 FstringParser_check_invariants(state);
4745
4746 /* Parse the f-string. */
4747 while (1) {
4748 PyObject *literal = NULL;
4749 expr_ty expression = NULL;
4750
4751 /* If there's a zero length literal in front of the
4752 expression, literal will be NULL. If we're at the end of
4753 the f-string, expression will be NULL (unless result == 1,
4754 see below). */
4755 int result = fstring_find_literal_and_expr(str, ofs, recurse_lvl,
4756 &literal, &expression,
4757 c, n);
4758 if (result < 0)
4759 return -1;
4760
4761 /* Add the literal, if any. */
4762 if (!literal) {
4763 /* Do nothing. Just leave last_str alone (and possibly
4764 NULL). */
4765 } else if (!state->last_str) {
4766 state->last_str = literal;
4767 literal = NULL;
4768 } else {
4769 /* We have a literal, concatenate it. */
4770 assert(PyUnicode_GET_LENGTH(literal) != 0);
4771 if (FstringParser_ConcatAndDel(state, literal) < 0)
4772 return -1;
4773 literal = NULL;
4774 }
4775 assert(!state->last_str ||
4776 PyUnicode_GET_LENGTH(state->last_str) != 0);
4777
4778 /* We've dealt with the literal now. It can't be leaked on further
4779 errors. */
4780 assert(literal == NULL);
4781
4782 /* See if we should just loop around to get the next literal
4783 and expression, while ignoring the expression this
4784 time. This is used for un-doubling braces, as an
4785 optimization. */
4786 if (result == 1)
4787 continue;
4788
4789 if (!expression)
4790 /* We're done with this f-string. */
4791 break;
4792
4793 /* We know we have an expression. Convert any existing string
4794 to a Str node. */
4795 if (!state->last_str) {
4796 /* Do nothing. No previous literal. */
4797 } else {
4798 /* Convert the existing last_str literal to a Str node. */
4799 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4800 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4801 return -1;
4802 }
4803
4804 if (ExprList_Append(&state->expr_list, expression) < 0)
4805 return -1;
4806 }
4807
4808 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4809
4810 /* If recurse_lvl is zero, then we must be at the end of the
4811 string. Otherwise, we must be at a right brace. */
4812
4813 if (recurse_lvl == 0 && *ofs < PyUnicode_GET_LENGTH(str)) {
4814 ast_error(c, n, "f-string: unexpected end of string");
4815 return -1;
4816 }
4817 if (recurse_lvl != 0 && PyUnicode_READ_CHAR(str, *ofs) != '}') {
4818 ast_error(c, n, "f-string: expecting '}'");
4819 return -1;
4820 }
4821
4822 FstringParser_check_invariants(state);
4823 return 0;
4824}
4825
4826/* Convert the partial state reflected in last_str and expr_list to an
4827 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4828static expr_ty
4829FstringParser_Finish(FstringParser *state, struct compiling *c,
4830 const node *n)
4831{
4832 asdl_seq *seq;
4833
4834 FstringParser_check_invariants(state);
4835
4836 /* If we're just a constant string with no expressions, return
4837 that. */
4838 if(state->expr_list.size == 0) {
4839 if (!state->last_str) {
4840 /* Create a zero length string. */
4841 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4842 if (!state->last_str)
4843 goto error;
4844 }
4845 return make_str_node_and_del(&state->last_str, c, n);
4846 }
4847
4848 /* Create a Str node out of last_str, if needed. It will be the
4849 last node in our expression list. */
4850 if (state->last_str) {
4851 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4852 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4853 goto error;
4854 }
4855 /* This has already been freed. */
4856 assert(state->last_str == NULL);
4857
4858 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4859 if (!seq)
4860 goto error;
4861
4862 /* If there's only one expression, return it. Otherwise, we need
4863 to join them together. */
4864 if (seq->size == 1)
4865 return seq->elements[0];
4866
4867 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4868
4869error:
4870 FstringParser_Dealloc(state);
4871 return NULL;
4872}
4873
4874/* Given an f-string (with no 'f' or quotes) that's in str starting at
4875 ofs, parse it into an expr_ty. Return NULL on error. Does not
4876 decref str. */
4877static expr_ty
4878fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4879 struct compiling *c, const node *n)
4880{
4881 FstringParser state;
4882
4883 FstringParser_Init(&state);
4884 if (FstringParser_ConcatFstring(&state, str, ofs, recurse_lvl,
4885 c, n) < 0) {
4886 FstringParser_Dealloc(&state);
4887 return NULL;
4888 }
4889
4890 return FstringParser_Finish(&state, c, n);
4891}
4892
4893/* n is a Python string literal, including the bracketing quote
4894 characters, and r, b, u, &/or f prefixes (if any), and embedded
4895 escape sequences (if any). parsestr parses it, and returns the
4896 decoded Python string object. If the string is an f-string, set
4897 *fmode and return the unparsed string object.
4898*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004899static PyObject *
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004901{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004902 size_t len;
4903 const char *s = STR(n);
4904 int quote = Py_CHARMASK(*s);
4905 int rawmode = 0;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004906 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004907 while (!*bytesmode || !rawmode) {
4908 if (quote == 'b' || quote == 'B') {
4909 quote = *++s;
4910 *bytesmode = 1;
4911 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004912 else if (quote == 'u' || quote == 'U') {
4913 quote = *++s;
4914 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004915 else if (quote == 'r' || quote == 'R') {
4916 quote = *++s;
4917 rawmode = 1;
4918 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919 else if (quote == 'f' || quote == 'F') {
4920 quote = *++s;
4921 *fmode = 1;
4922 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004923 else {
4924 break;
4925 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004926 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004927 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004928 if (*fmode && *bytesmode) {
4929 PyErr_BadInternalCall();
4930 return NULL;
4931 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004932 if (quote != '\'' && quote != '\"') {
4933 PyErr_BadInternalCall();
4934 return NULL;
4935 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004936 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004937 s++;
4938 len = strlen(s);
4939 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004941 "string to parse is too long");
4942 return NULL;
4943 }
4944 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004945 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004946 PyErr_BadInternalCall();
4947 return NULL;
4948 }
4949 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004950 /* A triple quoted string. We've already skipped one quote at
4951 the start and one at the end of the string. Now skip the
4952 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004953 s += 2;
4954 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004955 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004956 if (s[--len] != quote || s[--len] != quote) {
4957 PyErr_BadInternalCall();
4958 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004959 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004960 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04004961
4962 /* Temporary hack: if this is an f-string, no backslashes are allowed. */
4963 /* See issue 27921. */
4964 if (*fmode && strchr(s, '\\') != NULL) {
4965 /* Syntax error. At a later date fix this so it only checks for
4966 backslashes within the braces. */
4967 ast_error(c, n, "backslashes not allowed in f-strings");
4968 return NULL;
4969 }
4970
Benjamin Peterson768921c2016-02-25 23:13:53 -08004971 /* Avoid invoking escape decoding routines if possible. */
4972 rawmode = rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004973 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08004974 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004975 const char *ch;
4976 for (ch = s; *ch; ch++) {
4977 if (Py_CHARMASK(*ch) >= 0x80) {
4978 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004979 "literal characters.");
4980 return NULL;
4981 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004982 }
Benjamin Peterson768921c2016-02-25 23:13:53 -08004983 if (rawmode)
Christian Heimes72b710a2008-05-26 13:28:38 +00004984 return PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08004985 else
4986 return PyBytes_DecodeEscape(s, len, NULL, /* ignored */ 0, NULL);
4987 } else {
4988 if (rawmode)
4989 return PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
4990 else
4991 return decode_unicode_with_escapes(c, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004992 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004993}
4994
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
4996 each STRING atom, and process it as needed. For bytes, just
4997 concatenate them together, and the result will be a Bytes node. For
4998 normal strings and f-strings, concatenate them together. The result
4999 will be a Str node if there were no f-strings; a FormattedValue
5000 node if there's just an f-string (with no leading or trailing
5001 literals), or a JoinedStr node if there are multiple f-strings or
5002 any literals involved. */
5003static expr_ty
5004parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005005{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005006 int bytesmode = 0;
5007 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005008 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005009
5010 FstringParser state;
5011 FstringParser_Init(&state);
5012
5013 for (i = 0; i < NCH(n); i++) {
5014 int this_bytesmode = 0;
5015 int this_fmode = 0;
5016 PyObject *s;
5017
5018 REQ(CHILD(n, i), STRING);
5019 s = parsestr(c, CHILD(n, i), &this_bytesmode, &this_fmode);
5020 if (!s)
5021 goto error;
5022
5023 /* Check that we're not mixing bytes with unicode. */
5024 if (i != 0 && bytesmode != this_bytesmode) {
5025 ast_error(c, n, "cannot mix bytes and nonbytes literals");
5026 Py_DECREF(s);
5027 goto error;
5028 }
5029 bytesmode = this_bytesmode;
5030
5031 assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
5032
5033 if (bytesmode) {
5034 /* For bytes, concat as we go. */
5035 if (i == 0) {
5036 /* First time, just remember this value. */
5037 bytes_str = s;
5038 } else {
5039 PyBytes_ConcatAndDel(&bytes_str, s);
5040 if (!bytes_str)
5041 goto error;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005042 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005043 } else if (this_fmode) {
5044 /* This is an f-string. Concatenate and decref it. */
5045 Py_ssize_t ofs = 0;
5046 int result = FstringParser_ConcatFstring(&state, s, &ofs, 0, c, n);
5047 Py_DECREF(s);
5048 if (result < 0)
5049 goto error;
5050 } else {
5051 /* This is a regular string. Concatenate it. */
5052 if (FstringParser_ConcatAndDel(&state, s) < 0)
5053 goto error;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005054 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005055 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005056 if (bytesmode) {
5057 /* Just return the bytes object and we're done. */
5058 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5059 goto error;
5060 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5061 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005062
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063 /* We're not a bytes string, bytes_str should never have been set. */
5064 assert(bytes_str == NULL);
5065
5066 return FstringParser_Finish(&state, c, n);
5067
5068error:
5069 Py_XDECREF(bytes_str);
5070 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005071 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005072}