blob: ad771cf702355f642f281684055e7f817efd5c4e [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:
478 if (stmt->v.ImportFrom.level < -1) {
479 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
480 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:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000993 if (asdl_seq_LEN(e->v.Tuple.elts)) {
994 e->v.Tuple.ctx = ctx;
995 s = e->v.Tuple.elts;
996 }
997 else {
998 expr_name = "()";
999 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001000 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001001 case Lambda_kind:
1002 expr_name = "lambda";
1003 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001005 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001006 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001007 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001009 case UnaryOp_kind:
1010 expr_name = "operator";
1011 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001013 expr_name = "generator expression";
1014 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001016 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001017 expr_name = "yield expression";
1018 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001019 case Await_kind:
1020 expr_name = "await expression";
1021 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001022 case ListComp_kind:
1023 expr_name = "list comprehension";
1024 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001025 case SetComp_kind:
1026 expr_name = "set comprehension";
1027 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001028 case DictComp_kind:
1029 expr_name = "dict comprehension";
1030 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001031 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001032 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 case Num_kind:
1034 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001035 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001036 case JoinedStr_kind:
1037 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001038 expr_name = "literal";
1039 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001040 case NameConstant_kind:
1041 expr_name = "keyword";
1042 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001043 case Ellipsis_kind:
1044 expr_name = "Ellipsis";
1045 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001046 case Compare_kind:
1047 expr_name = "comparison";
1048 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049 case IfExp_kind:
1050 expr_name = "conditional expression";
1051 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001052 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 PyErr_Format(PyExc_SystemError,
1054 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001055 e->kind, e->lineno);
1056 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001058 /* Check for error string set by switch */
1059 if (expr_name) {
1060 char buf[300];
1061 PyOS_snprintf(buf, sizeof(buf),
1062 "can't %s %s",
1063 ctx == Store ? "assign to" : "delete",
1064 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001065 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001066 }
1067
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 */
1071 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001072 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073
Thomas Wouters89f507f2006-12-13 04:49:30 +00001074 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001075 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001076 return 0;
1077 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 }
1079 return 1;
1080}
1081
1082static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001083ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084{
1085 REQ(n, augassign);
1086 n = CHILD(n, 0);
1087 switch (STR(n)[0]) {
1088 case '+':
1089 return Add;
1090 case '-':
1091 return Sub;
1092 case '/':
1093 if (STR(n)[1] == '/')
1094 return FloorDiv;
1095 else
1096 return Div;
1097 case '%':
1098 return Mod;
1099 case '<':
1100 return LShift;
1101 case '>':
1102 return RShift;
1103 case '&':
1104 return BitAnd;
1105 case '^':
1106 return BitXor;
1107 case '|':
1108 return BitOr;
1109 case '*':
1110 if (STR(n)[1] == '*')
1111 return Pow;
1112 else
1113 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001114 case '@':
1115 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001117 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001118 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 }
1120}
1121
1122static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001123ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001125 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 |'is' 'not'
1127 */
1128 REQ(n, comp_op);
1129 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001130 n = CHILD(n, 0);
1131 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 case LESS:
1133 return Lt;
1134 case GREATER:
1135 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001136 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return Eq;
1138 case LESSEQUAL:
1139 return LtE;
1140 case GREATEREQUAL:
1141 return GtE;
1142 case NOTEQUAL:
1143 return NotEq;
1144 case NAME:
1145 if (strcmp(STR(n), "in") == 0)
1146 return In;
1147 if (strcmp(STR(n), "is") == 0)
1148 return Is;
1149 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001150 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001153 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 }
1155 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001156 /* handle "not in" and "is not" */
1157 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 case NAME:
1159 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1160 return NotIn;
1161 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1162 return IsNot;
1163 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001164 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001167 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 }
Neal Norwitz79792652005-11-14 04:25:03 +00001169 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001171 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172}
1173
1174static asdl_seq *
1175seq_for_testlist(struct compiling *c, const node *n)
1176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001178 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1179 */
Armin Rigo31441302005-10-21 12:57:31 +00001180 asdl_seq *seq;
1181 expr_ty expression;
1182 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001183 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001185 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 if (!seq)
1187 return NULL;
1188
1189 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001191 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192
Benjamin Peterson4905e802009-09-27 02:43:28 +00001193 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001194 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196
1197 assert(i / 2 < seq->size);
1198 asdl_seq_SET(seq, i / 2, expression);
1199 }
1200 return seq;
1201}
1202
Neal Norwitzc1505362006-12-28 06:47:50 +00001203static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001204ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001205{
1206 identifier name;
1207 expr_ty annotation = NULL;
1208 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001209 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001210
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001211 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001212 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001213 name = NEW_IDENTIFIER(ch);
1214 if (!name)
1215 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001216 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001217 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001218
1219 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1220 annotation = ast_for_expr(c, CHILD(n, 2));
1221 if (!annotation)
1222 return NULL;
1223 }
1224
Victor Stinnerc106c682015-11-06 17:01:48 +01001225 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001226 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001227 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001228 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229}
1230
Guido van Rossum4f72a782006-10-27 23:31:49 +00001231/* returns -1 if failed to handle keyword only arguments
1232 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001233 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001234 ^^^
1235 start pointing here
1236 */
1237static int
1238handle_keywordonly_args(struct compiling *c, const node *n, int start,
1239 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1240{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001241 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001242 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001243 expr_ty expression, annotation;
1244 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001245 int i = start;
1246 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001247
1248 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001249 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001250 return -1;
1251 }
1252 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001253 while (i < NCH(n)) {
1254 ch = CHILD(n, i);
1255 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001256 case vfpdef:
1257 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001258 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001259 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001260 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001261 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001262 asdl_seq_SET(kwdefaults, j, expression);
1263 i += 2; /* '=' and test */
1264 }
1265 else { /* setting NULL if no default value exists */
1266 asdl_seq_SET(kwdefaults, j, NULL);
1267 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001268 if (NCH(ch) == 3) {
1269 /* ch is NAME ':' test */
1270 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001271 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001272 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001273 }
1274 else {
1275 annotation = NULL;
1276 }
1277 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001278 argname = NEW_IDENTIFIER(ch);
1279 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001280 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001281 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001282 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001283 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1284 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001285 if (!arg)
1286 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001287 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001288 i += 2; /* the name and the comma */
1289 break;
1290 case DOUBLESTAR:
1291 return i;
1292 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001293 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001294 goto error;
1295 }
1296 }
1297 return i;
1298 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001300}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301
Jeremy Hyltona8293132006-02-28 17:58:27 +00001302/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303
1304static arguments_ty
1305ast_for_arguments(struct compiling *c, const node *n)
1306{
Neal Norwitzc1505362006-12-28 06:47:50 +00001307 /* This function handles both typedargslist (function definition)
1308 and varargslist (lambda definition).
1309
1310 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001311 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1312 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1313 | '**' tfpdef [',']]]
1314 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1315 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001316 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001317 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1318 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1319 | '**' vfpdef [',']]]
1320 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1321 | '**' vfpdef [',']
1322 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001323 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001324
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001326 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1327 int nposdefaults = 0, found_default = 0;
1328 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001329 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001330 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 node *ch;
1332
1333 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001334 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001335 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001336 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001338 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339
Jeremy Hyltone921e022008-07-17 16:37:17 +00001340 /* First count the number of positional args & defaults. The
1341 variable i is the loop index for this for loop and the next.
1342 The next loop picks up where the first leaves off.
1343 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001345 ch = CHILD(n, i);
1346 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001347 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001348 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001349 if (i < NCH(n) && /* skip argument following star */
1350 (TYPE(CHILD(n, i)) == tfpdef ||
1351 TYPE(CHILD(n, i)) == vfpdef)) {
1352 i++;
1353 }
1354 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001355 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001356 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001357 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001358 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 defaults for keyword only args */
1362 for ( ; i < NCH(n); ++i) {
1363 ch = CHILD(n, i);
1364 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001365 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001366 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001367 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001368 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001369 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001371 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001373 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001375 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001377 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001379 since we set NULL as default for keyword only argument w/o default
1380 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001381 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001382 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001384 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385
1386 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001387 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001388 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001391 /* tfpdef: NAME [':' test]
1392 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 */
1394 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001395 j = 0; /* index for defaults */
1396 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 ch = CHILD(n, i);
1399 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001400 case tfpdef:
1401 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1403 anything other than EQUAL or a comma? */
1404 /* XXX Should NCH(n) check be made a separate check? */
1405 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001406 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1407 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001408 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001409 assert(posdefaults != NULL);
1410 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001412 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001414 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001415 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001417 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001418 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001419 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001420 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001421 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001422 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 i += 2; /* the name and the comma */
1424 break;
1425 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001426 if (i+1 >= NCH(n) ||
1427 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001428 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001429 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001430 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001431 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001432 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001433 if (TYPE(ch) == COMMA) {
1434 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001435 i += 2; /* now follows keyword only arguments */
1436 res = handle_keywordonly_args(c, n, i,
1437 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001438 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001439 i = res; /* res has new position to process */
1440 }
1441 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001442 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001443 if (!vararg)
1444 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001445
Guido van Rossum4f72a782006-10-27 23:31:49 +00001446 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001447 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1448 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449 int res = 0;
1450 res = handle_keywordonly_args(c, n, i,
1451 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001452 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001453 i = res; /* res has new position to process */
1454 }
1455 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 break;
1457 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001458 ch = CHILD(n, i+1); /* tfpdef */
1459 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001460 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001461 if (!kwarg)
1462 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 i += 3;
1464 break;
1465 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001466 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 "unexpected node in varargslist: %d @ %d",
1468 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001469 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001472 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473}
1474
1475static expr_ty
1476ast_for_dotted_name(struct compiling *c, const node *n)
1477{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001478 expr_ty e;
1479 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001480 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 int i;
1482
1483 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001484
1485 lineno = LINENO(n);
1486 col_offset = n->n_col_offset;
1487
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 id = NEW_IDENTIFIER(CHILD(n, 0));
1489 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001490 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001491 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
1495 for (i = 2; i < NCH(n); i+=2) {
1496 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001497 if (!id)
1498 return NULL;
1499 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1500 if (!e)
1501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 }
1503
1504 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505}
1506
1507static expr_ty
1508ast_for_decorator(struct compiling *c, const node *n)
1509{
1510 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1511 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001512 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001515 REQ(CHILD(n, 0), AT);
1516 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1519 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001520 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001523 d = name_expr;
1524 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 }
1526 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001527 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001528 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001529 if (!d)
1530 return NULL;
1531 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 }
1533 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001534 d = ast_for_call(c, CHILD(n, 3), name_expr);
1535 if (!d)
1536 return NULL;
1537 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 }
1539
1540 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541}
1542
1543static asdl_seq*
1544ast_for_decorators(struct compiling *c, const node *n)
1545{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001546 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001547 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001551 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552 if (!decorator_seq)
1553 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001556 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001557 if (!d)
1558 return NULL;
1559 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560 }
1561 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562}
1563
1564static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001565ast_for_funcdef_impl(struct compiling *c, const node *n,
1566 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001568 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001569 identifier name;
1570 arguments_ty args;
1571 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001572 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001573 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574
1575 REQ(n, funcdef);
1576
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 name = NEW_IDENTIFIER(CHILD(n, name_i));
1578 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001579 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001580 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001581 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1583 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001584 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001585 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1586 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1587 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001588 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001589 name_i += 2;
1590 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 body = ast_for_suite(c, CHILD(n, name_i + 3));
1592 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594
Yury Selivanov75445082015-05-11 22:57:16 -04001595 if (is_async)
1596 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1597 LINENO(n),
1598 n->n_col_offset, c->c_arena);
1599 else
1600 return FunctionDef(name, args, body, decorator_seq, returns,
1601 LINENO(n),
1602 n->n_col_offset, c->c_arena);
1603}
1604
1605static stmt_ty
1606ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1607{
1608 /* async_funcdef: ASYNC funcdef */
1609 REQ(n, async_funcdef);
1610 REQ(CHILD(n, 0), ASYNC);
1611 REQ(CHILD(n, 1), funcdef);
1612
1613 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1614 1 /* is_async */);
1615}
1616
1617static stmt_ty
1618ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1619{
1620 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1621 return ast_for_funcdef_impl(c, n, decorator_seq,
1622 0 /* is_async */);
1623}
1624
1625
1626static stmt_ty
1627ast_for_async_stmt(struct compiling *c, const node *n)
1628{
1629 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1630 REQ(n, async_stmt);
1631 REQ(CHILD(n, 0), ASYNC);
1632
1633 switch (TYPE(CHILD(n, 1))) {
1634 case funcdef:
1635 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1636 1 /* is_async */);
1637 case with_stmt:
1638 return ast_for_with_stmt(c, CHILD(n, 1),
1639 1 /* is_async */);
1640
1641 case for_stmt:
1642 return ast_for_for_stmt(c, CHILD(n, 1),
1643 1 /* is_async */);
1644
1645 default:
1646 PyErr_Format(PyExc_SystemError,
1647 "invalid async stament: %s",
1648 STR(CHILD(n, 1)));
1649 return NULL;
1650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651}
1652
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001653static stmt_ty
1654ast_for_decorated(struct compiling *c, const node *n)
1655{
Yury Selivanov75445082015-05-11 22:57:16 -04001656 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001657 stmt_ty thing = NULL;
1658 asdl_seq *decorator_seq = NULL;
1659
1660 REQ(n, decorated);
1661
1662 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1663 if (!decorator_seq)
1664 return NULL;
1665
1666 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001667 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001669
1670 if (TYPE(CHILD(n, 1)) == funcdef) {
1671 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1672 } else if (TYPE(CHILD(n, 1)) == classdef) {
1673 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001674 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1675 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001676 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001677 /* we count the decorators in when talking about the class' or
1678 * function's line number */
1679 if (thing) {
1680 thing->lineno = LINENO(n);
1681 thing->col_offset = n->n_col_offset;
1682 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001683 return thing;
1684}
1685
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686static expr_ty
1687ast_for_lambdef(struct compiling *c, const node *n)
1688{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001689 /* lambdef: 'lambda' [varargslist] ':' test
1690 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 arguments_ty args;
1692 expr_ty expression;
1693
1694 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001695 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 if (!args)
1697 return NULL;
1698 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001699 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 }
1702 else {
1703 args = ast_for_arguments(c, CHILD(n, 1));
1704 if (!args)
1705 return NULL;
1706 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001707 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 }
1710
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001711 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712}
1713
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001714static expr_ty
1715ast_for_ifexpr(struct compiling *c, const node *n)
1716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001718 expr_ty expression, body, orelse;
1719
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001720 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001721 body = ast_for_expr(c, CHILD(n, 0));
1722 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001723 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001724 expression = ast_for_expr(c, CHILD(n, 2));
1725 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001726 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001727 orelse = ast_for_expr(c, CHILD(n, 4));
1728 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001729 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001730 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1731 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001732}
1733
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001735 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736
Nick Coghlan650f0d02007-04-15 12:05:43 +00001737 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738*/
1739
1740static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001741count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001743 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744
Guido van Rossumd8faa362007-04-27 19:54:29 +00001745 count_comp_for:
1746 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001747 REQ(n, comp_for);
1748 if (NCH(n) == 5)
1749 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001750 else
1751 return n_fors;
1752 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001753 REQ(n, comp_iter);
1754 n = CHILD(n, 0);
1755 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001756 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001757 else if (TYPE(n) == comp_if) {
1758 if (NCH(n) == 3) {
1759 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001760 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001761 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001762 else
1763 return n_fors;
1764 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001765
Guido van Rossumd8faa362007-04-27 19:54:29 +00001766 /* Should never be reached */
1767 PyErr_SetString(PyExc_SystemError,
1768 "logic error in count_comp_fors");
1769 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770}
1771
Nick Coghlan650f0d02007-04-15 12:05:43 +00001772/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773
Nick Coghlan650f0d02007-04-15 12:05:43 +00001774 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775*/
1776
1777static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001778count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781
Guido van Rossumd8faa362007-04-27 19:54:29 +00001782 while (1) {
1783 REQ(n, comp_iter);
1784 if (TYPE(CHILD(n, 0)) == comp_for)
1785 return n_ifs;
1786 n = CHILD(n, 0);
1787 REQ(n, comp_if);
1788 n_ifs++;
1789 if (NCH(n) == 2)
1790 return n_ifs;
1791 n = CHILD(n, 2);
1792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793}
1794
Guido van Rossum992d4a32007-07-11 13:09:30 +00001795static asdl_seq *
1796ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001799 asdl_seq *comps;
1800
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001801 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 if (n_fors == -1)
1803 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001804
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001805 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001806 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001808
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001810 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001812 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001813 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814
Guido van Rossum992d4a32007-07-11 13:09:30 +00001815 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816
Guido van Rossum992d4a32007-07-11 13:09:30 +00001817 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001818 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001819 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001821 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001822 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001824
Thomas Wouters89f507f2006-12-13 04:49:30 +00001825 /* Check the # of children rather than the length of t, since
1826 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001827 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001828 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001829 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001831 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1832 c->c_arena),
1833 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001834 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001836
Guido van Rossum992d4a32007-07-11 13:09:30 +00001837 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 int j, n_ifs;
1839 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840
Guido van Rossum992d4a32007-07-11 13:09:30 +00001841 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001842 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001843 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001846 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001847 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001851 REQ(n, comp_iter);
1852 n = CHILD(n, 0);
1853 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854
Guido van Rossum992d4a32007-07-11 13:09:30 +00001855 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001857 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001858 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001859 if (NCH(n) == 3)
1860 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001862 /* on exit, must guarantee that n is a comp_for */
1863 if (TYPE(n) == comp_iter)
1864 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001865 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001867 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001869 return comps;
1870}
1871
1872static expr_ty
1873ast_for_itercomp(struct compiling *c, const node *n, int type)
1874{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001875 /* testlist_comp: (test|star_expr)
1876 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001877 expr_ty elt;
1878 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001879 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880
Guido van Rossum992d4a32007-07-11 13:09:30 +00001881 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001883 ch = CHILD(n, 0);
1884 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001885 if (!elt)
1886 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001887 if (elt->kind == Starred_kind) {
1888 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1889 return NULL;
1890 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891
Guido van Rossum992d4a32007-07-11 13:09:30 +00001892 comps = ast_for_comprehension(c, CHILD(n, 1));
1893 if (!comps)
1894 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001895
1896 if (type == COMP_GENEXP)
1897 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1898 else if (type == COMP_LISTCOMP)
1899 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1900 else if (type == COMP_SETCOMP)
1901 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1902 else
1903 /* Should never happen */
1904 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905}
1906
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001907/* Fills in the key, value pair corresponding to the dict element. In case
1908 * of an unpacking, key is NULL. *i is advanced by the number of ast
1909 * elements. Iff successful, nonzero is returned.
1910 */
1911static int
1912ast_for_dictelement(struct compiling *c, const node *n, int *i,
1913 expr_ty *key, expr_ty *value)
1914{
1915 expr_ty expression;
1916 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1917 assert(NCH(n) - *i >= 2);
1918
1919 expression = ast_for_expr(c, CHILD(n, *i + 1));
1920 if (!expression)
1921 return 0;
1922 *key = NULL;
1923 *value = expression;
1924
1925 *i += 2;
1926 }
1927 else {
1928 assert(NCH(n) - *i >= 3);
1929
1930 expression = ast_for_expr(c, CHILD(n, *i));
1931 if (!expression)
1932 return 0;
1933 *key = expression;
1934
1935 REQ(CHILD(n, *i + 1), COLON);
1936
1937 expression = ast_for_expr(c, CHILD(n, *i + 2));
1938 if (!expression)
1939 return 0;
1940 *value = expression;
1941
1942 *i += 3;
1943 }
1944 return 1;
1945}
1946
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001948ast_for_dictcomp(struct compiling *c, const node *n)
1949{
1950 expr_ty key, value;
1951 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001952 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001954 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001955 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001956 assert(key);
1957 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001959 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001960 if (!comps)
1961 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962
Guido van Rossum992d4a32007-07-11 13:09:30 +00001963 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1964}
1965
1966static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001967ast_for_dictdisplay(struct compiling *c, const node *n)
1968{
1969 int i;
1970 int j;
1971 int size;
1972 asdl_seq *keys, *values;
1973
1974 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1975 keys = _Py_asdl_seq_new(size, c->c_arena);
1976 if (!keys)
1977 return NULL;
1978
1979 values = _Py_asdl_seq_new(size, c->c_arena);
1980 if (!values)
1981 return NULL;
1982
1983 j = 0;
1984 for (i = 0; i < NCH(n); i++) {
1985 expr_ty key, value;
1986
1987 if (!ast_for_dictelement(c, n, &i, &key, &value))
1988 return NULL;
1989 asdl_seq_SET(keys, j, key);
1990 asdl_seq_SET(values, j, value);
1991
1992 j++;
1993 }
1994 keys->size = j;
1995 values->size = j;
1996 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1997}
1998
1999static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002000ast_for_genexp(struct compiling *c, const node *n)
2001{
2002 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002003 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002004}
2005
2006static expr_ty
2007ast_for_listcomp(struct compiling *c, const node *n)
2008{
2009 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002010 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002011}
2012
2013static expr_ty
2014ast_for_setcomp(struct compiling *c, const node *n)
2015{
2016 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002017 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002018}
2019
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002020static expr_ty
2021ast_for_setdisplay(struct compiling *c, const node *n)
2022{
2023 int i;
2024 int size;
2025 asdl_seq *elts;
2026
2027 assert(TYPE(n) == (dictorsetmaker));
2028 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2029 elts = _Py_asdl_seq_new(size, c->c_arena);
2030 if (!elts)
2031 return NULL;
2032 for (i = 0; i < NCH(n); i += 2) {
2033 expr_ty expression;
2034 expression = ast_for_expr(c, CHILD(n, i));
2035 if (!expression)
2036 return NULL;
2037 asdl_seq_SET(elts, i / 2, expression);
2038 }
2039 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2040}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002041
2042static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043ast_for_atom(struct compiling *c, const node *n)
2044{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002045 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2046 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002047 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048 */
2049 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002052 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002053 PyObject *name;
2054 const char *s = STR(ch);
2055 size_t len = strlen(s);
2056 if (len >= 4 && len <= 5) {
2057 if (!strcmp(s, "None"))
2058 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2059 if (!strcmp(s, "True"))
2060 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2061 if (!strcmp(s, "False"))
2062 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2063 }
2064 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002065 if (!name)
2066 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002067 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002068 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2069 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002071 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002072 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002073 const char *errtype = NULL;
2074 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2075 errtype = "unicode error";
2076 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2077 errtype = "value error";
2078 if (errtype) {
2079 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002080 PyObject *type, *value, *tback, *errstr;
2081 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002082 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002083 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002084 char *s = _PyUnicode_AsString(errstr);
2085 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002086 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002087 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002088 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002089 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002090 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002091 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002092 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002093 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002094 Py_XDECREF(tback);
2095 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002096 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002097 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002098 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 }
2100 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002101 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002102 if (!pynum)
2103 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002104
Victor Stinner43d81952013-07-17 00:57:58 +02002105 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2106 Py_DECREF(pynum);
2107 return NULL;
2108 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002109 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 }
Georg Brandldde00282007-03-18 19:01:53 +00002111 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002112 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002114 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115
Thomas Wouters89f507f2006-12-13 04:49:30 +00002116 if (TYPE(ch) == RPAR)
2117 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118
Thomas Wouters89f507f2006-12-13 04:49:30 +00002119 if (TYPE(ch) == yield_expr)
2120 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002123 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002124 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002125
Nick Coghlan650f0d02007-04-15 12:05:43 +00002126 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002128 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129
Thomas Wouters89f507f2006-12-13 04:49:30 +00002130 if (TYPE(ch) == RSQB)
2131 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132
Nick Coghlan650f0d02007-04-15 12:05:43 +00002133 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002134 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2135 asdl_seq *elts = seq_for_testlist(c, ch);
2136 if (!elts)
2137 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002138
Thomas Wouters89f507f2006-12-13 04:49:30 +00002139 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2140 }
2141 else
2142 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002144 /* dictorsetmaker: ( ((test ':' test | '**' test)
2145 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2146 * ((test | '*' test)
2147 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002148 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002149 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002150 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002151 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002152 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002153 }
2154 else {
2155 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2156 if (NCH(ch) == 1 ||
2157 (NCH(ch) > 1 &&
2158 TYPE(CHILD(ch, 1)) == COMMA)) {
2159 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002160 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002161 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002162 else if (NCH(ch) > 1 &&
2163 TYPE(CHILD(ch, 1)) == comp_for) {
2164 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002165 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002166 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002167 else if (NCH(ch) > 3 - is_dict &&
2168 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2169 /* It's a dictionary comprehension. */
2170 if (is_dict) {
2171 ast_error(c, n, "dict unpacking cannot be used in "
2172 "dict comprehension");
2173 return NULL;
2174 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002175 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002176 }
2177 else {
2178 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002179 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002180 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002181 if (res) {
2182 res->lineno = LINENO(n);
2183 res->col_offset = n->n_col_offset;
2184 }
2185 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002186 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002189 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2190 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 }
2192}
2193
2194static slice_ty
2195ast_for_slice(struct compiling *c, const node *n)
2196{
2197 node *ch;
2198 expr_ty lower = NULL, upper = NULL, step = NULL;
2199
2200 REQ(n, subscript);
2201
2202 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002203 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 sliceop: ':' [test]
2205 */
2206 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 if (NCH(n) == 1 && TYPE(ch) == test) {
2208 /* 'step' variable hold no significance in terms of being used over
2209 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 if (!step)
2212 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213
Thomas Wouters89f507f2006-12-13 04:49:30 +00002214 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 }
2216
2217 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002218 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 if (!lower)
2220 return NULL;
2221 }
2222
2223 /* If there's an upper bound it's in the second or third position. */
2224 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002225 if (NCH(n) > 1) {
2226 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227
Thomas Wouters89f507f2006-12-13 04:49:30 +00002228 if (TYPE(n2) == test) {
2229 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 if (!upper)
2231 return NULL;
2232 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002233 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002235 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236
Thomas Wouters89f507f2006-12-13 04:49:30 +00002237 if (TYPE(n2) == test) {
2238 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 if (!upper)
2240 return NULL;
2241 }
2242 }
2243
2244 ch = CHILD(n, NCH(n) - 1);
2245 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002246 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002247 ch = CHILD(ch, 1);
2248 if (TYPE(ch) == test) {
2249 step = ast_for_expr(c, ch);
2250 if (!step)
2251 return NULL;
2252 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 }
2254 }
2255
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002256 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257}
2258
2259static expr_ty
2260ast_for_binop(struct compiling *c, const node *n)
2261{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002262 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002264 BinOp(BinOp(A, op, B), op, C).
2265 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266
Guido van Rossumd8faa362007-04-27 19:54:29 +00002267 int i, nops;
2268 expr_ty expr1, expr2, result;
2269 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270
Guido van Rossumd8faa362007-04-27 19:54:29 +00002271 expr1 = ast_for_expr(c, CHILD(n, 0));
2272 if (!expr1)
2273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274
Guido van Rossumd8faa362007-04-27 19:54:29 +00002275 expr2 = ast_for_expr(c, CHILD(n, 2));
2276 if (!expr2)
2277 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278
Guido van Rossumd8faa362007-04-27 19:54:29 +00002279 newoperator = get_operator(CHILD(n, 1));
2280 if (!newoperator)
2281 return NULL;
2282
2283 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2284 c->c_arena);
2285 if (!result)
2286 return NULL;
2287
2288 nops = (NCH(n) - 1) / 2;
2289 for (i = 1; i < nops; i++) {
2290 expr_ty tmp_result, tmp;
2291 const node* next_oper = CHILD(n, i * 2 + 1);
2292
2293 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 return NULL;
2296
Guido van Rossumd8faa362007-04-27 19:54:29 +00002297 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2298 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 return NULL;
2300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002302 LINENO(next_oper), next_oper->n_col_offset,
2303 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002305 return NULL;
2306 result = tmp_result;
2307 }
2308 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309}
2310
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002311static expr_ty
2312ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002315 subscriptlist: subscript (',' subscript)* [',']
2316 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2317 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002318 REQ(n, trailer);
2319 if (TYPE(CHILD(n, 0)) == LPAR) {
2320 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002321 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002322 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002323 else
2324 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002325 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002326 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002327 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2328 if (!attr_id)
2329 return NULL;
2330 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002331 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002332 }
2333 else {
2334 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002335 REQ(CHILD(n, 2), RSQB);
2336 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002337 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002338 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2339 if (!slc)
2340 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2342 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002343 }
2344 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002346 by treating the sequence as a tuple literal if there are
2347 no slice features.
2348 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002349 int j;
2350 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002351 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002352 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002353 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002354 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002355 if (!slices)
2356 return NULL;
2357 for (j = 0; j < NCH(n); j += 2) {
2358 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002359 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002360 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002361 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002362 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002363 asdl_seq_SET(slices, j / 2, slc);
2364 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002365 if (!simple) {
2366 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002367 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002368 }
2369 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002370 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002371 if (!elts)
2372 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002373 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2374 slc = (slice_ty)asdl_seq_GET(slices, j);
2375 assert(slc->kind == Index_kind && slc->v.Index.value);
2376 asdl_seq_SET(elts, j, slc->v.Index.value);
2377 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002378 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002379 if (!e)
2380 return NULL;
2381 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002382 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002383 }
2384 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002385}
2386
2387static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002388ast_for_factor(struct compiling *c, const node *n)
2389{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002390 expr_ty expression;
2391
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002392 expression = ast_for_expr(c, CHILD(n, 1));
2393 if (!expression)
2394 return NULL;
2395
2396 switch (TYPE(CHILD(n, 0))) {
2397 case PLUS:
2398 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2399 c->c_arena);
2400 case MINUS:
2401 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2402 c->c_arena);
2403 case TILDE:
2404 return UnaryOp(Invert, expression, LINENO(n),
2405 n->n_col_offset, c->c_arena);
2406 }
2407 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2408 TYPE(CHILD(n, 0)));
2409 return NULL;
2410}
2411
2412static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002413ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002414{
Yury Selivanov75445082015-05-11 22:57:16 -04002415 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002416 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002417
2418 REQ(n, atom_expr);
2419 nch = NCH(n);
2420
2421 if (TYPE(CHILD(n, 0)) == AWAIT) {
2422 start = 1;
2423 assert(nch > 1);
2424 }
2425
2426 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002427 if (!e)
2428 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002429 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002430 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002431 if (start && nch == 2) {
2432 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2433 }
2434
2435 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002436 node *ch = CHILD(n, i);
2437 if (TYPE(ch) != trailer)
2438 break;
2439 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002440 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002441 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002442 tmp->lineno = e->lineno;
2443 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002444 e = tmp;
2445 }
Yury Selivanov75445082015-05-11 22:57:16 -04002446
2447 if (start) {
2448 /* there was an AWAIT */
2449 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2450 }
2451 else {
2452 return e;
2453 }
2454}
2455
2456static expr_ty
2457ast_for_power(struct compiling *c, const node *n)
2458{
2459 /* power: atom trailer* ('**' factor)*
2460 */
2461 expr_ty e;
2462 REQ(n, power);
2463 e = ast_for_atom_expr(c, CHILD(n, 0));
2464 if (!e)
2465 return NULL;
2466 if (NCH(n) == 1)
2467 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002468 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2469 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002470 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002471 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002472 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002473 }
2474 return e;
2475}
2476
Guido van Rossum0368b722007-05-11 16:50:42 +00002477static expr_ty
2478ast_for_starred(struct compiling *c, const node *n)
2479{
2480 expr_ty tmp;
2481 REQ(n, star_expr);
2482
2483 tmp = ast_for_expr(c, CHILD(n, 1));
2484 if (!tmp)
2485 return NULL;
2486
2487 /* The Load context is changed later. */
2488 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2489}
2490
2491
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492/* Do not name a variable 'expr'! Will cause a compile error.
2493*/
2494
2495static expr_ty
2496ast_for_expr(struct compiling *c, const node *n)
2497{
2498 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002499 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002500 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 and_test: not_test ('and' not_test)*
2503 not_test: 'not' not_test | comparison
2504 comparison: expr (comp_op expr)*
2505 expr: xor_expr ('|' xor_expr)*
2506 xor_expr: and_expr ('^' and_expr)*
2507 and_expr: shift_expr ('&' shift_expr)*
2508 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2509 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002510 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002512 power: atom_expr ['**' factor]
2513 atom_expr: [AWAIT] atom trailer*
2514 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 */
2516
2517 asdl_seq *seq;
2518 int i;
2519
2520 loop:
2521 switch (TYPE(n)) {
2522 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002523 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002524 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002525 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002527 else if (NCH(n) > 1)
2528 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002529 /* Fallthrough */
2530 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 case and_test:
2532 if (NCH(n) == 1) {
2533 n = CHILD(n, 0);
2534 goto loop;
2535 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002536 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 if (!seq)
2538 return NULL;
2539 for (i = 0; i < NCH(n); i += 2) {
2540 expr_ty e = ast_for_expr(c, CHILD(n, i));
2541 if (!e)
2542 return NULL;
2543 asdl_seq_SET(seq, i / 2, e);
2544 }
2545 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002546 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2547 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002548 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002549 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 case not_test:
2551 if (NCH(n) == 1) {
2552 n = CHILD(n, 0);
2553 goto loop;
2554 }
2555 else {
2556 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2557 if (!expression)
2558 return NULL;
2559
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002560 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2561 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 }
2563 case comparison:
2564 if (NCH(n) == 1) {
2565 n = CHILD(n, 0);
2566 goto loop;
2567 }
2568 else {
2569 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002570 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002571 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002572 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 if (!ops)
2574 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002575 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 return NULL;
2578 }
2579 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002580 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002582 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002583 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586
2587 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002588 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002590 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002592 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 asdl_seq_SET(cmps, i / 2, expression);
2594 }
2595 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002596 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002598 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002600 return Compare(expression, ops, cmps, LINENO(n),
2601 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 }
2603 break;
2604
Guido van Rossum0368b722007-05-11 16:50:42 +00002605 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 /* The next five cases all handle BinOps. The main body of code
2608 is the same in each case, but the switch turned inside out to
2609 reuse the code for each type of operator.
2610 */
2611 case expr:
2612 case xor_expr:
2613 case and_expr:
2614 case shift_expr:
2615 case arith_expr:
2616 case term:
2617 if (NCH(n) == 1) {
2618 n = CHILD(n, 0);
2619 goto loop;
2620 }
2621 return ast_for_binop(c, n);
2622 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002623 node *an = NULL;
2624 node *en = NULL;
2625 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002626 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002627 if (NCH(n) > 1)
2628 an = CHILD(n, 1); /* yield_arg */
2629 if (an) {
2630 en = CHILD(an, NCH(an) - 1);
2631 if (NCH(an) == 2) {
2632 is_from = 1;
2633 exp = ast_for_expr(c, en);
2634 }
2635 else
2636 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 if (!exp)
2638 return NULL;
2639 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002640 if (is_from)
2641 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2642 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002643 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002644 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 if (NCH(n) == 1) {
2646 n = CHILD(n, 0);
2647 goto loop;
2648 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002649 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002650 case power:
2651 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002653 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 return NULL;
2655 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002656 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 return NULL;
2658}
2659
2660static expr_ty
2661ast_for_call(struct compiling *c, const node *n, expr_ty func)
2662{
2663 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002664 arglist: argument (',' argument)* [',']
2665 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 */
2667
2668 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002669 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002670 asdl_seq *args;
2671 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672
2673 REQ(n, arglist);
2674
2675 nargs = 0;
2676 nkeywords = 0;
2677 ngens = 0;
2678 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002679 node *ch = CHILD(n, i);
2680 if (TYPE(ch) == argument) {
2681 if (NCH(ch) == 1)
2682 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002683 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002684 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002685 else if (TYPE(CHILD(ch, 0)) == STAR)
2686 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002688 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002689 nkeywords++;
2690 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 }
2692 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002693 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002694 "if not sole argument");
2695 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 }
2697
2698 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002699 ast_error(c, n, "more than 255 arguments");
2700 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 }
2702
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002703 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002705 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002706 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002708 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002709
2710 nargs = 0; /* positional arguments + iterable argument unpackings */
2711 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2712 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002714 node *ch = CHILD(n, i);
2715 if (TYPE(ch) == argument) {
2716 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002717 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002718 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002719 /* a positional argument */
2720 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002721 if (ndoublestars) {
2722 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002723 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002724 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002725 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002726 else {
2727 ast_error(c, chch,
2728 "positional argument follows "
2729 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002731 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002732 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002733 e = ast_for_expr(c, chch);
2734 if (!e)
2735 return NULL;
2736 asdl_seq_SET(args, nargs++, e);
2737 }
2738 else if (TYPE(chch) == STAR) {
2739 /* an iterable argument unpacking */
2740 expr_ty starred;
2741 if (ndoublestars) {
2742 ast_error(c, chch,
2743 "iterable argument unpacking follows "
2744 "keyword argument unpacking");
2745 return NULL;
2746 }
2747 e = ast_for_expr(c, CHILD(ch, 1));
2748 if (!e)
2749 return NULL;
2750 starred = Starred(e, Load, LINENO(chch),
2751 chch->n_col_offset,
2752 c->c_arena);
2753 if (!starred)
2754 return NULL;
2755 asdl_seq_SET(args, nargs++, starred);
2756
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002757 }
2758 else if (TYPE(chch) == DOUBLESTAR) {
2759 /* a keyword argument unpacking */
2760 keyword_ty kw;
2761 i++;
2762 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002764 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002765 kw = keyword(NULL, e, c->c_arena);
2766 asdl_seq_SET(keywords, nkeywords++, kw);
2767 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002769 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002771 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002773 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002774 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002776 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002777 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002778 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002779 identifier key, tmp;
2780 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002782 /* chch is test, but must be an identifier? */
2783 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002785 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 /* f(lambda x: x[0] = 3) ends up getting parsed with
2787 * LHS test = lambda x: x[0], and RHS test = 3.
2788 * SF bug 132313 points out that complaining about a keyword
2789 * then is very confusing.
2790 */
2791 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002792 ast_error(c, chch,
2793 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002794 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002795 }
2796 else if (e->kind != Name_kind) {
2797 ast_error(c, chch,
2798 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002799 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002800 }
2801 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002802 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002804 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002805 for (k = 0; k < nkeywords; k++) {
2806 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002807 if (tmp && !PyUnicode_Compare(tmp, key)) {
2808 ast_error(c, chch,
2809 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002810 return NULL;
2811 }
2812 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002813 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002815 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002816 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002818 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002819 asdl_seq_SET(keywords, nkeywords++, kw);
2820 }
2821 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 }
2823
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002824 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825}
2826
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002828ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002830 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002831 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002833 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002834 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002835 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002836 }
2837 else {
2838 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002839 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002842 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 else {
2844 asdl_seq *tmp = seq_for_testlist(c, n);
2845 if (!tmp)
2846 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002847 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002849}
2850
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851static stmt_ty
2852ast_for_expr_stmt(struct compiling *c, const node *n)
2853{
2854 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002857 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002858 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 test: ... here starts the operator precendence dance
2861 */
2862
2863 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002864 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 if (!e)
2866 return NULL;
2867
Thomas Wouters89f507f2006-12-13 04:49:30 +00002868 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 }
2870 else if (TYPE(CHILD(n, 1)) == augassign) {
2871 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002872 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002873 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874
Thomas Wouters89f507f2006-12-13 04:49:30 +00002875 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 if (!expr1)
2877 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002878 if(!set_context(c, expr1, Store, ch))
2879 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002880 /* set_context checks that most expressions are not the left side.
2881 Augmented assignments can only have a name, a subscript, or an
2882 attribute on the left, though, so we have to explicitly check for
2883 those. */
2884 switch (expr1->kind) {
2885 case Name_kind:
2886 case Attribute_kind:
2887 case Subscript_kind:
2888 break;
2889 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002890 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002891 return NULL;
2892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893
Thomas Wouters89f507f2006-12-13 04:49:30 +00002894 ch = CHILD(n, 2);
2895 if (TYPE(ch) == testlist)
2896 expr2 = ast_for_testlist(c, ch);
2897 else
2898 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002899 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 return NULL;
2901
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002902 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002903 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 return NULL;
2905
Thomas Wouters89f507f2006-12-13 04:49:30 +00002906 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 }
2908 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 int i;
2910 asdl_seq *targets;
2911 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 expr_ty expression;
2913
Thomas Wouters89f507f2006-12-13 04:49:30 +00002914 /* a normal assignment */
2915 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002916 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002917 if (!targets)
2918 return NULL;
2919 for (i = 0; i < NCH(n) - 2; i += 2) {
2920 expr_ty e;
2921 node *ch = CHILD(n, i);
2922 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002923 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002924 return NULL;
2925 }
2926 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002928 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002930 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002931 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002932 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933
Thomas Wouters89f507f2006-12-13 04:49:30 +00002934 asdl_seq_SET(targets, i / 2, e);
2935 }
2936 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002937 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002938 expression = ast_for_testlist(c, value);
2939 else
2940 expression = ast_for_expr(c, value);
2941 if (!expression)
2942 return NULL;
2943 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945}
2946
Benjamin Peterson78565b22009-06-28 19:19:51 +00002947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002949ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950{
2951 asdl_seq *seq;
2952 int i;
2953 expr_ty e;
2954
2955 REQ(n, exprlist);
2956
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002957 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002959 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002961 e = ast_for_expr(c, CHILD(n, i));
2962 if (!e)
2963 return NULL;
2964 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002965 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967 }
2968 return seq;
2969}
2970
2971static stmt_ty
2972ast_for_del_stmt(struct compiling *c, const node *n)
2973{
2974 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 /* del_stmt: 'del' exprlist */
2977 REQ(n, del_stmt);
2978
2979 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2980 if (!expr_list)
2981 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002982 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983}
2984
2985static stmt_ty
2986ast_for_flow_stmt(struct compiling *c, const node *n)
2987{
2988 /*
2989 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2990 | yield_stmt
2991 break_stmt: 'break'
2992 continue_stmt: 'continue'
2993 return_stmt: 'return' [testlist]
2994 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002995 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 raise_stmt: 'raise' [test [',' test [',' test]]]
2997 */
2998 node *ch;
2999
3000 REQ(n, flow_stmt);
3001 ch = CHILD(n, 0);
3002 switch (TYPE(ch)) {
3003 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003004 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003006 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003008 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3009 if (!exp)
3010 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003011 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 }
3013 case return_stmt:
3014 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003015 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003017 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 if (!expression)
3019 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003020 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 }
3022 case raise_stmt:
3023 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003024 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3025 else if (NCH(ch) >= 2) {
3026 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3028 if (!expression)
3029 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003030 if (NCH(ch) == 4) {
3031 cause = ast_for_expr(c, CHILD(ch, 3));
3032 if (!cause)
3033 return NULL;
3034 }
3035 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 }
3037 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003038 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 "unexpected flow_stmt: %d", TYPE(ch));
3040 return NULL;
3041 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003042
3043 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045}
3046
3047static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003048alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049{
3050 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003051 import_as_name: NAME ['as' NAME]
3052 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053 dotted_name: NAME ('.' NAME)*
3054 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003055 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003056
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 loop:
3058 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003059 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003060 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003061 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003062 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003063 if (!name)
3064 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003065 if (NCH(n) == 3) {
3066 node *str_node = CHILD(n, 2);
3067 str = NEW_IDENTIFIER(str_node);
3068 if (!str)
3069 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003070 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003071 return NULL;
3072 }
3073 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003074 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003075 return NULL;
3076 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003077 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003078 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 case dotted_as_name:
3080 if (NCH(n) == 1) {
3081 n = CHILD(n, 0);
3082 goto loop;
3083 }
3084 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003085 node *asname_node = CHILD(n, 2);
3086 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003087 if (!a)
3088 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003090 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003091 if (!a->asname)
3092 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003093 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003094 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095 return a;
3096 }
3097 break;
3098 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003099 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003100 node *name_node = CHILD(n, 0);
3101 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003102 if (!name)
3103 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003104 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003105 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003106 return alias(name, NULL, c->c_arena);
3107 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 else {
3109 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003110 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003111 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114
3115 len = 0;
3116 for (i = 0; i < NCH(n); i += 2)
3117 /* length of string plus one for the dot */
3118 len += strlen(STR(CHILD(n, i))) + 1;
3119 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003120 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 if (!str)
3122 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003123 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 if (!s)
3125 return NULL;
3126 for (i = 0; i < NCH(n); i += 2) {
3127 char *sch = STR(CHILD(n, i));
3128 strcpy(s, STR(CHILD(n, i)));
3129 s += strlen(sch);
3130 *s++ = '.';
3131 }
3132 --s;
3133 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3135 PyBytes_GET_SIZE(str),
3136 NULL);
3137 Py_DECREF(str);
3138 if (!uni)
3139 return NULL;
3140 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003141 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003142 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3143 Py_DECREF(str);
3144 return NULL;
3145 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003146 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 }
3148 break;
3149 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003150 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003151 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3152 Py_DECREF(str);
3153 return NULL;
3154 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003155 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003157 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 "unexpected import name: %d", TYPE(n));
3159 return NULL;
3160 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003161
3162 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 return NULL;
3164}
3165
3166static stmt_ty
3167ast_for_import_stmt(struct compiling *c, const node *n)
3168{
3169 /*
3170 import_stmt: import_name | import_from
3171 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003172 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3173 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003175 int lineno;
3176 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 int i;
3178 asdl_seq *aliases;
3179
3180 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003181 lineno = LINENO(n);
3182 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003184 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003186 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003187 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003188 if (!aliases)
3189 return NULL;
3190 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003191 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003192 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003194 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003196 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003198 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003200 int idx, ndots = 0;
3201 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003202 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003204 /* Count the number of dots (for relative imports) and check for the
3205 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003206 for (idx = 1; idx < NCH(n); idx++) {
3207 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003208 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3209 if (!mod)
3210 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003211 idx++;
3212 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003213 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003215 ndots += 3;
3216 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003217 } else if (TYPE(CHILD(n, idx)) != DOT) {
3218 break;
3219 }
3220 ndots++;
3221 }
3222 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003223 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003224 case STAR:
3225 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003226 n = CHILD(n, idx);
3227 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003228 break;
3229 case LPAR:
3230 /* from ... import (x, y, z) */
3231 n = CHILD(n, idx + 1);
3232 n_children = NCH(n);
3233 break;
3234 case import_as_names:
3235 /* from ... import x, y, z */
3236 n = CHILD(n, idx);
3237 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003238 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003239 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240 " surrounding parentheses");
3241 return NULL;
3242 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003243 break;
3244 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003245 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003246 return NULL;
3247 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003249 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003250 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252
3253 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003254 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003255 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003256 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003258 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003260 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003261 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003262 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003263 if (!import_alias)
3264 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003265 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003266 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003268 if (mod != NULL)
3269 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003270 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003271 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 }
Neal Norwitz79792652005-11-14 04:25:03 +00003273 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 "unknown import statement: starts with command '%s'",
3275 STR(CHILD(n, 0)));
3276 return NULL;
3277}
3278
3279static stmt_ty
3280ast_for_global_stmt(struct compiling *c, const node *n)
3281{
3282 /* global_stmt: 'global' NAME (',' NAME)* */
3283 identifier name;
3284 asdl_seq *s;
3285 int i;
3286
3287 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003288 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003290 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003292 name = NEW_IDENTIFIER(CHILD(n, i));
3293 if (!name)
3294 return NULL;
3295 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003297 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298}
3299
3300static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003301ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3302{
3303 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3304 identifier name;
3305 asdl_seq *s;
3306 int i;
3307
3308 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003309 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003310 if (!s)
3311 return NULL;
3312 for (i = 1; i < NCH(n); i += 2) {
3313 name = NEW_IDENTIFIER(CHILD(n, i));
3314 if (!name)
3315 return NULL;
3316 asdl_seq_SET(s, i / 2, name);
3317 }
3318 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3319}
3320
3321static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322ast_for_assert_stmt(struct compiling *c, const node *n)
3323{
3324 /* assert_stmt: 'assert' test [',' test] */
3325 REQ(n, assert_stmt);
3326 if (NCH(n) == 2) {
3327 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3328 if (!expression)
3329 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003330 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331 }
3332 else if (NCH(n) == 4) {
3333 expr_ty expr1, expr2;
3334
3335 expr1 = ast_for_expr(c, CHILD(n, 1));
3336 if (!expr1)
3337 return NULL;
3338 expr2 = ast_for_expr(c, CHILD(n, 3));
3339 if (!expr2)
3340 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341
Thomas Wouters89f507f2006-12-13 04:49:30 +00003342 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343 }
Neal Norwitz79792652005-11-14 04:25:03 +00003344 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345 "improper number of parts to 'assert' statement: %d",
3346 NCH(n));
3347 return NULL;
3348}
3349
3350static asdl_seq *
3351ast_for_suite(struct compiling *c, const node *n)
3352{
3353 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003354 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355 stmt_ty s;
3356 int i, total, num, end, pos = 0;
3357 node *ch;
3358
3359 REQ(n, suite);
3360
3361 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003362 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003364 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003366 n = CHILD(n, 0);
3367 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003369 */
3370 end = NCH(n) - 1;
3371 if (TYPE(CHILD(n, end - 1)) == SEMI)
3372 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003374 for (i = 0; i < end; i += 2) {
3375 ch = CHILD(n, i);
3376 s = ast_for_stmt(c, ch);
3377 if (!s)
3378 return NULL;
3379 asdl_seq_SET(seq, pos++, s);
3380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 }
3382 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003383 for (i = 2; i < (NCH(n) - 1); i++) {
3384 ch = CHILD(n, i);
3385 REQ(ch, stmt);
3386 num = num_stmts(ch);
3387 if (num == 1) {
3388 /* small_stmt or compound_stmt with only one child */
3389 s = ast_for_stmt(c, ch);
3390 if (!s)
3391 return NULL;
3392 asdl_seq_SET(seq, pos++, s);
3393 }
3394 else {
3395 int j;
3396 ch = CHILD(ch, 0);
3397 REQ(ch, simple_stmt);
3398 for (j = 0; j < NCH(ch); j += 2) {
3399 /* statement terminates with a semi-colon ';' */
3400 if (NCH(CHILD(ch, j)) == 0) {
3401 assert((j + 1) == NCH(ch));
3402 break;
3403 }
3404 s = ast_for_stmt(c, CHILD(ch, j));
3405 if (!s)
3406 return NULL;
3407 asdl_seq_SET(seq, pos++, s);
3408 }
3409 }
3410 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411 }
3412 assert(pos == seq->size);
3413 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414}
3415
3416static stmt_ty
3417ast_for_if_stmt(struct compiling *c, const node *n)
3418{
3419 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3420 ['else' ':' suite]
3421 */
3422 char *s;
3423
3424 REQ(n, if_stmt);
3425
3426 if (NCH(n) == 4) {
3427 expr_ty expression;
3428 asdl_seq *suite_seq;
3429
3430 expression = ast_for_expr(c, CHILD(n, 1));
3431 if (!expression)
3432 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003434 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436
Guido van Rossumd8faa362007-04-27 19:54:29 +00003437 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3438 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003440
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 s = STR(CHILD(n, 4));
3442 /* s[2], the third character in the string, will be
3443 's' for el_s_e, or
3444 'i' for el_i_f
3445 */
3446 if (s[2] == 's') {
3447 expr_ty expression;
3448 asdl_seq *seq1, *seq2;
3449
3450 expression = ast_for_expr(c, CHILD(n, 1));
3451 if (!expression)
3452 return NULL;
3453 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003454 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 return NULL;
3456 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003457 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 return NULL;
3459
Guido van Rossumd8faa362007-04-27 19:54:29 +00003460 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3461 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462 }
3463 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003464 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003465 expr_ty expression;
3466 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003467 asdl_seq *orelse = NULL;
3468 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 /* must reference the child n_elif+1 since 'else' token is third,
3470 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003471 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3472 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3473 has_else = 1;
3474 n_elif -= 3;
3475 }
3476 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003479 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003481 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003482 if (!orelse)
3483 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003485 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003487 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3488 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003490 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3491 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 asdl_seq_SET(orelse, 0,
3495 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003496 LINENO(CHILD(n, NCH(n) - 6)),
3497 CHILD(n, NCH(n) - 6)->n_col_offset,
3498 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003499 /* the just-created orelse handled the last elif */
3500 n_elif--;
3501 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502
Thomas Wouters89f507f2006-12-13 04:49:30 +00003503 for (i = 0; i < n_elif; i++) {
3504 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003505 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003506 if (!newobj)
3507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003509 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003512 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514
Thomas Wouters89f507f2006-12-13 04:49:30 +00003515 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003517 LINENO(CHILD(n, off)),
3518 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003519 orelse = newobj;
3520 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003521 expression = ast_for_expr(c, CHILD(n, 1));
3522 if (!expression)
3523 return NULL;
3524 suite_seq = ast_for_suite(c, CHILD(n, 3));
3525 if (!suite_seq)
3526 return NULL;
3527 return If(expression, suite_seq, orelse,
3528 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003530
3531 PyErr_Format(PyExc_SystemError,
3532 "unexpected token in 'if' statement: %s", s);
3533 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534}
3535
3536static stmt_ty
3537ast_for_while_stmt(struct compiling *c, const node *n)
3538{
3539 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3540 REQ(n, while_stmt);
3541
3542 if (NCH(n) == 4) {
3543 expr_ty expression;
3544 asdl_seq *suite_seq;
3545
3546 expression = ast_for_expr(c, CHILD(n, 1));
3547 if (!expression)
3548 return NULL;
3549 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003550 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003552 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 }
3554 else if (NCH(n) == 7) {
3555 expr_ty expression;
3556 asdl_seq *seq1, *seq2;
3557
3558 expression = ast_for_expr(c, CHILD(n, 1));
3559 if (!expression)
3560 return NULL;
3561 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003562 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 return NULL;
3564 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003565 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 return NULL;
3567
Thomas Wouters89f507f2006-12-13 04:49:30 +00003568 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003570
3571 PyErr_Format(PyExc_SystemError,
3572 "wrong number of tokens for 'while' statement: %d",
3573 NCH(n));
3574 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575}
3576
3577static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003578ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003580 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003582 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003583 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3585 REQ(n, for_stmt);
3586
3587 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003588 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589 if (!seq)
3590 return NULL;
3591 }
3592
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003593 node_target = CHILD(n, 1);
3594 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003595 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003597 /* Check the # of children rather than the length of _target, since
3598 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003599 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003600 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003601 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003603 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003605 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003606 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 return NULL;
3608 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003609 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 return NULL;
3611
Yury Selivanov75445082015-05-11 22:57:16 -04003612 if (is_async)
3613 return AsyncFor(target, expression, suite_seq, seq,
3614 LINENO(n), n->n_col_offset,
3615 c->c_arena);
3616 else
3617 return For(target, expression, suite_seq, seq,
3618 LINENO(n), n->n_col_offset,
3619 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620}
3621
3622static excepthandler_ty
3623ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3624{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003625 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 REQ(exc, except_clause);
3627 REQ(body, suite);
3628
3629 if (NCH(exc) == 1) {
3630 asdl_seq *suite_seq = ast_for_suite(c, body);
3631 if (!suite_seq)
3632 return NULL;
3633
Neal Norwitzad74aa82008-03-31 05:14:30 +00003634 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003635 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 }
3637 else if (NCH(exc) == 2) {
3638 expr_ty expression;
3639 asdl_seq *suite_seq;
3640
3641 expression = ast_for_expr(c, CHILD(exc, 1));
3642 if (!expression)
3643 return NULL;
3644 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003645 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646 return NULL;
3647
Neal Norwitzad74aa82008-03-31 05:14:30 +00003648 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003649 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650 }
3651 else if (NCH(exc) == 4) {
3652 asdl_seq *suite_seq;
3653 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003654 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003655 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003657 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003658 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003660 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 return NULL;
3662 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003663 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 return NULL;
3665
Neal Norwitzad74aa82008-03-31 05:14:30 +00003666 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003667 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003669
3670 PyErr_Format(PyExc_SystemError,
3671 "wrong number of children for 'except' clause: %d",
3672 NCH(exc));
3673 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674}
3675
3676static stmt_ty
3677ast_for_try_stmt(struct compiling *c, const node *n)
3678{
Neal Norwitzf599f422005-12-17 21:33:47 +00003679 const int nch = NCH(n);
3680 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003681 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 REQ(n, try_stmt);
3684
Neal Norwitzf599f422005-12-17 21:33:47 +00003685 body = ast_for_suite(c, CHILD(n, 2));
3686 if (body == NULL)
3687 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688
Neal Norwitzf599f422005-12-17 21:33:47 +00003689 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3690 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3691 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3692 /* we can assume it's an "else",
3693 because nch >= 9 for try-else-finally and
3694 it would otherwise have a type of except_clause */
3695 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3696 if (orelse == NULL)
3697 return NULL;
3698 n_except--;
3699 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700
Neal Norwitzf599f422005-12-17 21:33:47 +00003701 finally = ast_for_suite(c, CHILD(n, nch - 1));
3702 if (finally == NULL)
3703 return NULL;
3704 n_except--;
3705 }
3706 else {
3707 /* we can assume it's an "else",
3708 otherwise it would have a type of except_clause */
3709 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3710 if (orelse == NULL)
3711 return NULL;
3712 n_except--;
3713 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003715 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003716 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 return NULL;
3718 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719
Neal Norwitzf599f422005-12-17 21:33:47 +00003720 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003721 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003722 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003723 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003724 if (handlers == NULL)
3725 return NULL;
3726
3727 for (i = 0; i < n_except; i++) {
3728 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3729 CHILD(n, 5 + i * 3));
3730 if (!e)
3731 return NULL;
3732 asdl_seq_SET(handlers, i, e);
3733 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003734 }
3735
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003736 assert(finally != NULL || asdl_seq_LEN(handlers));
3737 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738}
3739
Georg Brandl0c315622009-05-25 21:10:36 +00003740/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003741static withitem_ty
3742ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003743{
3744 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003745
Georg Brandl0c315622009-05-25 21:10:36 +00003746 REQ(n, with_item);
3747 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003748 if (!context_expr)
3749 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003750 if (NCH(n) == 3) {
3751 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003752
3753 if (!optional_vars) {
3754 return NULL;
3755 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003756 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003757 return NULL;
3758 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003759 }
3760
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003761 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003762}
3763
Georg Brandl0c315622009-05-25 21:10:36 +00003764/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3765static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003766ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003767{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003768 int i, n_items;
3769 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003770
3771 REQ(n, with_stmt);
3772
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003773 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003774 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003775 if (!items)
3776 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003777 for (i = 1; i < NCH(n) - 2; i += 2) {
3778 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3779 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003780 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003781 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003782 }
3783
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003784 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3785 if (!body)
3786 return NULL;
3787
Yury Selivanov75445082015-05-11 22:57:16 -04003788 if (is_async)
3789 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3790 else
3791 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003792}
3793
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003795ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003797 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003798 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003799 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003800 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003801
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 REQ(n, classdef);
3803
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003804 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805 s = ast_for_suite(c, CHILD(n, 3));
3806 if (!s)
3807 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003808 classname = NEW_IDENTIFIER(CHILD(n, 1));
3809 if (!classname)
3810 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003811 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003812 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003813 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3814 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003816
3817 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003818 s = ast_for_suite(c, CHILD(n,5));
3819 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003820 return NULL;
3821 classname = NEW_IDENTIFIER(CHILD(n, 1));
3822 if (!classname)
3823 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003824 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003825 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003826 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3827 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 }
3829
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003830 /* class NAME '(' arglist ')' ':' suite */
3831 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003832 {
3833 PyObject *dummy_name;
3834 expr_ty dummy;
3835 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3836 if (!dummy_name)
3837 return NULL;
3838 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3839 call = ast_for_call(c, CHILD(n, 3), dummy);
3840 if (!call)
3841 return NULL;
3842 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003844 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003846 classname = NEW_IDENTIFIER(CHILD(n, 1));
3847 if (!classname)
3848 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003849 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003850 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003851
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003852 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003853 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854}
3855
3856static stmt_ty
3857ast_for_stmt(struct compiling *c, const node *n)
3858{
3859 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003860 assert(NCH(n) == 1);
3861 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 }
3863 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003864 assert(num_stmts(n) == 1);
3865 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866 }
3867 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003868 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003869 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3870 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003871 */
3872 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 case expr_stmt:
3874 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 case del_stmt:
3876 return ast_for_del_stmt(c, n);
3877 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003878 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 case flow_stmt:
3880 return ast_for_flow_stmt(c, n);
3881 case import_stmt:
3882 return ast_for_import_stmt(c, n);
3883 case global_stmt:
3884 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003885 case nonlocal_stmt:
3886 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887 case assert_stmt:
3888 return ast_for_assert_stmt(c, n);
3889 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003890 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3892 TYPE(n), NCH(n));
3893 return NULL;
3894 }
3895 }
3896 else {
3897 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003898 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003899 */
3900 node *ch = CHILD(n, 0);
3901 REQ(n, compound_stmt);
3902 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 case if_stmt:
3904 return ast_for_if_stmt(c, ch);
3905 case while_stmt:
3906 return ast_for_while_stmt(c, ch);
3907 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003908 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909 case try_stmt:
3910 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003911 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003912 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003914 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003915 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003916 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 case decorated:
3918 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003919 case async_stmt:
3920 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003922 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3924 TYPE(n), NCH(n));
3925 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003926 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927 }
3928}
3929
3930static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003931parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003933 const char *end;
3934 long x;
3935 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003936 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003937 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003939 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003940 errno = 0;
3941 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003942 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003943 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003944 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003945 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003946 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003947 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003948 }
3949 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003950 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003951 if (*end == '\0') {
3952 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003953 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003954 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003955 }
3956 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003957 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003958 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003959 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3960 if (compl.imag == -1.0 && PyErr_Occurred())
3961 return NULL;
3962 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003963 }
3964 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003965 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003966 dx = PyOS_string_to_double(s, NULL, NULL);
3967 if (dx == -1.0 && PyErr_Occurred())
3968 return NULL;
3969 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003970 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971}
3972
3973static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003974decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003976 const char *s, *t;
3977 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003978 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3979 while (s < end && (*s & 0x80)) s++;
3980 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003981 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982}
3983
3984static PyObject *
Benjamin Peterson768921c2016-02-25 23:13:53 -08003985decode_unicode_with_escapes(struct compiling *c, const char *s, size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003987 PyObject *v, *u;
3988 char *buf;
3989 char *p;
3990 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003991
Benjamin Peterson202803a2016-02-25 22:34:45 -08003992 /* check for integer overflow */
3993 if (len > PY_SIZE_MAX / 6)
3994 return NULL;
3995 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3996 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3997 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
3998 if (u == NULL)
3999 return NULL;
4000 p = buf = PyBytes_AsString(u);
4001 end = s + len;
4002 while (s < end) {
4003 if (*s == '\\') {
4004 *p++ = *s++;
4005 if (*s & 0x80) {
4006 strcpy(p, "u005c");
4007 p += 5;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004008 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004009 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004010 if (*s & 0x80) { /* XXX inefficient */
4011 PyObject *w;
4012 int kind;
4013 void *data;
4014 Py_ssize_t len, i;
4015 w = decode_utf8(c, &s, end);
4016 if (w == NULL) {
4017 Py_DECREF(u);
4018 return NULL;
4019 }
4020 kind = PyUnicode_KIND(w);
4021 data = PyUnicode_DATA(w);
4022 len = PyUnicode_GET_LENGTH(w);
4023 for (i = 0; i < len; i++) {
4024 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4025 sprintf(p, "\\U%08x", chr);
4026 p += 10;
4027 }
4028 /* Should be impossible to overflow */
4029 assert(p - buf <= Py_SIZE(u));
4030 Py_DECREF(w);
4031 } else {
4032 *p++ = *s++;
4033 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004034 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004035 len = p - buf;
4036 s = buf;
4037
Eric V. Smith5567f892015-09-21 13:36:09 -04004038 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004039 Py_XDECREF(u);
4040 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041}
4042
Eric V. Smith235a6f02015-09-19 14:51:32 -04004043/* Compile this expression in to an expr_ty. We know that we can
4044 temporarily modify the character before the start of this string
4045 (it's '{'), and we know we can temporarily modify the character
4046 after this string (it is a '}'). Leverage this to create a
4047 sub-string with enough room for us to add parens around the
4048 expression. This is to allow strings with embedded newlines, for
4049 example. */
4050static expr_ty
Eric V. Smith1d44c412015-09-23 07:49:00 -04004051fstring_compile_expr(PyObject *str, Py_ssize_t expr_start,
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004052 Py_ssize_t expr_end, struct compiling *c, const node *n)
4053
Eric V. Smith235a6f02015-09-19 14:51:32 -04004054{
4055 PyCompilerFlags cf;
4056 mod_ty mod;
4057 char *utf_expr;
4058 Py_ssize_t i;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004059 Py_UCS4 end_ch = -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004060 int all_whitespace;
4061 PyObject *sub = NULL;
4062
4063 /* We only decref sub if we allocated it with a PyUnicode_Substring.
4064 decref_sub records that. */
4065 int decref_sub = 0;
4066
4067 assert(str);
4068
Eric V. Smith1d44c412015-09-23 07:49:00 -04004069 assert(expr_start >= 0 && expr_start < PyUnicode_GET_LENGTH(str));
4070 assert(expr_end >= 0 && expr_end < PyUnicode_GET_LENGTH(str));
4071 assert(expr_end >= expr_start);
4072
Martin Panterc2432f62015-10-07 11:15:15 +00004073 /* There has to be at least one character on each side of the
Eric V. Smith1d44c412015-09-23 07:49:00 -04004074 expression inside this str. This will have been caught before
4075 we're called. */
4076 assert(expr_start >= 1);
4077 assert(expr_end <= PyUnicode_GET_LENGTH(str)-1);
4078
Eric V. Smith235a6f02015-09-19 14:51:32 -04004079 /* If the substring is all whitespace, it's an error. We need to
4080 catch this here, and not when we call PyParser_ASTFromString,
4081 because turning the expression '' in to '()' would go from
4082 being invalid to valid. */
4083 /* Note that this code says an empty string is all
4084 whitespace. That's important. There's a test for it: f'{}'. */
4085 all_whitespace = 1;
4086 for (i = expr_start; i < expr_end; i++) {
4087 if (!Py_UNICODE_ISSPACE(PyUnicode_READ_CHAR(str, i))) {
4088 all_whitespace = 0;
4089 break;
4090 }
4091 }
4092 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004093 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004094 goto error;
4095 }
4096
4097 /* If the substring will be the entire source string, we can't use
4098 PyUnicode_Substring, since it will return another reference to
4099 our original string. Because we're modifying the string in
4100 place, that's a no-no. So, detect that case and just use our
4101 string directly. */
4102
4103 if (expr_start-1 == 0 && expr_end+1 == PyUnicode_GET_LENGTH(str)) {
Eric V. Smith1d44c412015-09-23 07:49:00 -04004104 /* If str is well formed, then the first and last chars must
4105 be '{' and '}', respectively. But, if there's a syntax
4106 error, for example f'{3!', then the last char won't be a
4107 closing brace. So, remember the last character we read in
4108 order for us to restore it. */
4109 end_ch = PyUnicode_ReadChar(str, expr_end-expr_start+1);
4110 assert(end_ch != (Py_UCS4)-1);
4111
4112 /* In all cases, however, start_ch must be '{'. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004113 assert(PyUnicode_ReadChar(str, 0) == '{');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004114
Eric V. Smith235a6f02015-09-19 14:51:32 -04004115 sub = str;
4116 } else {
4117 /* Create a substring object. It must be a new object, with
4118 refcount==1, so that we can modify it. */
4119 sub = PyUnicode_Substring(str, expr_start-1, expr_end+1);
4120 if (!sub)
4121 goto error;
4122 assert(sub != str); /* Make sure it's a new string. */
4123 decref_sub = 1; /* Remember to deallocate it on error. */
4124 }
4125
Eric V. Smith1d44c412015-09-23 07:49:00 -04004126 /* Put () around the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004127 if (PyUnicode_WriteChar(sub, 0, '(') < 0 ||
4128 PyUnicode_WriteChar(sub, expr_end-expr_start+1, ')') < 0)
4129 goto error;
4130
Eric V. Smith235a6f02015-09-19 14:51:32 -04004131 /* No need to free the memory returned here: it's managed by the
4132 string. */
4133 utf_expr = PyUnicode_AsUTF8(sub);
4134 if (!utf_expr)
4135 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004136
4137 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004138 mod = PyParser_ASTFromString(utf_expr, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004139 Py_eval_input, &cf, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004140 if (!mod)
4141 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004142
Eric V. Smith235a6f02015-09-19 14:51:32 -04004143 if (sub != str)
4144 /* Clear instead of decref in case we ever modify this code to change
4145 the error handling: this is safest because the XDECREF won't try
4146 and decref it when it's NULL. */
4147 /* No need to restore the chars in sub, since we know it's getting
4148 ready to get deleted (refcount must be 1, since we got a new string
4149 in PyUnicode_Substring). */
4150 Py_CLEAR(sub);
4151 else {
4152 assert(!decref_sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004153 assert(end_ch != (Py_UCS4)-1);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004154 /* Restore str, which we earlier modified directly. */
4155 if (PyUnicode_WriteChar(str, 0, '{') < 0 ||
Eric V. Smith1d44c412015-09-23 07:49:00 -04004156 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004157 goto error;
4158 }
4159 return mod->v.Expression.body;
4160
4161error:
4162 /* Only decref sub if it was the result of a call to SubString. */
4163 if (decref_sub)
4164 Py_XDECREF(sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004165
4166 if (end_ch != (Py_UCS4)-1) {
4167 /* We only get here if we modified str. Make sure that's the
4168 case: str will be equal to sub. */
4169 if (str == sub) {
4170 /* Don't check the error, because we've already set the
4171 error state (that's why we're in 'error', after
4172 all). */
4173 PyUnicode_WriteChar(str, 0, '{');
4174 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch);
4175 }
4176 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004177 return NULL;
4178}
4179
4180/* Return -1 on error.
4181
4182 Return 0 if we reached the end of the literal.
4183
4184 Return 1 if we haven't reached the end of the literal, but we want
4185 the caller to process the literal up to this point. Used for
4186 doubled braces.
4187*/
4188static int
4189fstring_find_literal(PyObject *str, Py_ssize_t *ofs, PyObject **literal,
4190 int recurse_lvl, struct compiling *c, const node *n)
4191{
4192 /* Get any literal string. It ends when we hit an un-doubled brace, or the
4193 end of the string. */
4194
4195 Py_ssize_t literal_start, literal_end;
4196 int result = 0;
4197
4198 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4199 void *data = PyUnicode_DATA(str);
4200
4201 assert(*literal == NULL);
4202
4203 literal_start = *ofs;
4204 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4205 Py_UCS4 ch = PyUnicode_READ(kind, data, *ofs);
4206 if (ch == '{' || ch == '}') {
4207 /* Check for doubled braces, but only at the top level. If
4208 we checked at every level, then f'{0:{3}}' would fail
4209 with the two closing braces. */
4210 if (recurse_lvl == 0) {
4211 if (*ofs + 1 < PyUnicode_GET_LENGTH(str) &&
4212 PyUnicode_READ(kind, data, *ofs + 1) == ch) {
4213 /* We're going to tell the caller that the literal ends
4214 here, but that they should continue scanning. But also
4215 skip over the second brace when we resume scanning. */
4216 literal_end = *ofs + 1;
4217 *ofs += 2;
4218 result = 1;
4219 goto done;
4220 }
4221
4222 /* Where a single '{' is the start of a new expression, a
4223 single '}' is not allowed. */
4224 if (ch == '}') {
4225 ast_error(c, n, "f-string: single '}' is not allowed");
4226 return -1;
4227 }
4228 }
4229
4230 /* We're either at a '{', which means we're starting another
4231 expression; or a '}', which means we're at the end of this
4232 f-string (for a nested format_spec). */
4233 break;
4234 }
4235 }
4236 literal_end = *ofs;
4237
4238 assert(*ofs == PyUnicode_GET_LENGTH(str) ||
4239 PyUnicode_READ(kind, data, *ofs) == '{' ||
4240 PyUnicode_READ(kind, data, *ofs) == '}');
4241done:
4242 if (literal_start != literal_end) {
4243 *literal = PyUnicode_Substring(str, literal_start, literal_end);
4244 if (!*literal)
4245 return -1;
4246 }
4247
4248 return result;
4249}
4250
4251/* Forward declaration because parsing is recursive. */
4252static expr_ty
4253fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4254 struct compiling *c, const node *n);
4255
4256/* Parse the f-string str, starting at ofs. We know *ofs starts an
4257 expression (so it must be a '{'). Returns the FormattedValue node,
4258 which includes the expression, conversion character, and
4259 format_spec expression.
4260
4261 Note that I don't do a perfect job here: I don't make sure that a
4262 closing brace doesn't match an opening paren, for example. It
4263 doesn't need to error on all invalid expressions, just correctly
4264 find the end of all valid ones. Any errors inside the expression
4265 will be caught when we parse it later. */
4266static int
4267fstring_find_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4268 expr_ty *expression, struct compiling *c, const node *n)
4269{
4270 /* Return -1 on error, else 0. */
4271
4272 Py_ssize_t expr_start;
4273 Py_ssize_t expr_end;
4274 expr_ty simple_expression;
4275 expr_ty format_spec = NULL; /* Optional format specifier. */
4276 Py_UCS4 conversion = -1; /* The conversion char. -1 if not specified. */
4277
4278 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4279 void *data = PyUnicode_DATA(str);
4280
4281 /* 0 if we're not in a string, else the quote char we're trying to
4282 match (single or double quote). */
4283 Py_UCS4 quote_char = 0;
4284
4285 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4286 int string_type = 0;
4287
4288 /* Keep track of nesting level for braces/parens/brackets in
4289 expressions. */
4290 Py_ssize_t nested_depth = 0;
4291
4292 /* Can only nest one level deep. */
4293 if (recurse_lvl >= 2) {
4294 ast_error(c, n, "f-string: expressions nested too deeply");
4295 return -1;
4296 }
4297
4298 /* The first char must be a left brace, or we wouldn't have gotten
4299 here. Skip over it. */
4300 assert(PyUnicode_READ(kind, data, *ofs) == '{');
4301 *ofs += 1;
4302
4303 expr_start = *ofs;
4304 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4305 Py_UCS4 ch;
4306
4307 /* Loop invariants. */
4308 assert(nested_depth >= 0);
4309 assert(*ofs >= expr_start);
4310 if (quote_char)
4311 assert(string_type == 1 || string_type == 3);
4312 else
4313 assert(string_type == 0);
4314
4315 ch = PyUnicode_READ(kind, data, *ofs);
4316 if (quote_char) {
4317 /* We're inside a string. See if we're at the end. */
4318 /* This code needs to implement the same non-error logic
4319 as tok_get from tokenizer.c, at the letter_quote
4320 label. To actually share that code would be a
4321 nightmare. But, it's unlikely to change and is small,
4322 so duplicate it here. Note we don't need to catch all
4323 of the errors, since they'll be caught when parsing the
4324 expression. We just need to match the non-error
4325 cases. Thus we can ignore \n in single-quoted strings,
4326 for example. Or non-terminated strings. */
4327 if (ch == quote_char) {
4328 /* Does this match the string_type (single or triple
4329 quoted)? */
4330 if (string_type == 3) {
4331 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4332 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4333 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4334 /* We're at the end of a triple quoted string. */
4335 *ofs += 2;
4336 string_type = 0;
4337 quote_char = 0;
4338 continue;
4339 }
4340 } else {
4341 /* We're at the end of a normal string. */
4342 quote_char = 0;
4343 string_type = 0;
4344 continue;
4345 }
4346 }
4347 /* We're inside a string, and not finished with the
4348 string. If this is a backslash, skip the next char (it
4349 might be an end quote that needs skipping). Otherwise,
4350 just consume this character normally. */
4351 if (ch == '\\' && *ofs+1 < PyUnicode_GET_LENGTH(str)) {
4352 /* Just skip the next char, whatever it is. */
4353 *ofs += 1;
4354 }
4355 } else if (ch == '\'' || ch == '"') {
4356 /* Is this a triple quoted string? */
4357 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4358 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4359 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4360 string_type = 3;
4361 *ofs += 2;
4362 } else {
4363 /* Start of a normal string. */
4364 string_type = 1;
4365 }
4366 /* Start looking for the end of the string. */
4367 quote_char = ch;
4368 } else if (ch == '[' || ch == '{' || ch == '(') {
4369 nested_depth++;
4370 } else if (nested_depth != 0 &&
4371 (ch == ']' || ch == '}' || ch == ')')) {
4372 nested_depth--;
4373 } else if (ch == '#') {
4374 /* Error: can't include a comment character, inside parens
4375 or not. */
4376 ast_error(c, n, "f-string cannot include '#'");
4377 return -1;
4378 } else if (nested_depth == 0 &&
4379 (ch == '!' || ch == ':' || ch == '}')) {
4380 /* First, test for the special case of "!=". Since '=' is
4381 not an allowed conversion character, nothing is lost in
4382 this test. */
4383 if (ch == '!' && *ofs+1 < PyUnicode_GET_LENGTH(str) &&
4384 PyUnicode_READ(kind, data, *ofs+1) == '=')
4385 /* This isn't a conversion character, just continue. */
4386 continue;
4387
4388 /* Normal way out of this loop. */
4389 break;
4390 } else {
4391 /* Just consume this char and loop around. */
4392 }
4393 }
4394 expr_end = *ofs;
4395 /* If we leave this loop in a string or with mismatched parens, we
4396 don't care. We'll get a syntax error when compiling the
4397 expression. But, we can produce a better error message, so
4398 let's just do that.*/
4399 if (quote_char) {
4400 ast_error(c, n, "f-string: unterminated string");
4401 return -1;
4402 }
4403 if (nested_depth) {
4404 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4405 return -1;
4406 }
4407
Eric V. Smith235a6f02015-09-19 14:51:32 -04004408 if (*ofs >= PyUnicode_GET_LENGTH(str))
4409 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004410
4411 /* Compile the expression as soon as possible, so we show errors
4412 related to the expression before errors related to the
4413 conversion or format_spec. */
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004414 simple_expression = fstring_compile_expr(str, expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004415 if (!simple_expression)
4416 return -1;
4417
4418 /* Check for a conversion char, if present. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004419 if (PyUnicode_READ(kind, data, *ofs) == '!') {
4420 *ofs += 1;
4421 if (*ofs >= PyUnicode_GET_LENGTH(str))
4422 goto unexpected_end_of_string;
4423
4424 conversion = PyUnicode_READ(kind, data, *ofs);
4425 *ofs += 1;
4426
4427 /* Validate the conversion. */
4428 if (!(conversion == 's' || conversion == 'r'
4429 || conversion == 'a')) {
4430 ast_error(c, n, "f-string: invalid conversion character: "
4431 "expected 's', 'r', or 'a'");
4432 return -1;
4433 }
4434 }
4435
4436 /* Check for the format spec, if present. */
4437 if (*ofs >= PyUnicode_GET_LENGTH(str))
4438 goto unexpected_end_of_string;
4439 if (PyUnicode_READ(kind, data, *ofs) == ':') {
4440 *ofs += 1;
4441 if (*ofs >= PyUnicode_GET_LENGTH(str))
4442 goto unexpected_end_of_string;
4443
4444 /* Parse the format spec. */
4445 format_spec = fstring_parse(str, ofs, recurse_lvl+1, c, n);
4446 if (!format_spec)
4447 return -1;
4448 }
4449
4450 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4451 PyUnicode_READ(kind, data, *ofs) != '}')
4452 goto unexpected_end_of_string;
4453
4454 /* We're at a right brace. Consume it. */
4455 assert(*ofs < PyUnicode_GET_LENGTH(str));
4456 assert(PyUnicode_READ(kind, data, *ofs) == '}');
4457 *ofs += 1;
4458
Eric V. Smith235a6f02015-09-19 14:51:32 -04004459 /* And now create the FormattedValue node that represents this entire
4460 expression with the conversion and format spec. */
4461 *expression = FormattedValue(simple_expression, (int)conversion,
4462 format_spec, LINENO(n), n->n_col_offset,
4463 c->c_arena);
4464 if (!*expression)
4465 return -1;
4466
4467 return 0;
4468
4469unexpected_end_of_string:
4470 ast_error(c, n, "f-string: expecting '}'");
4471 return -1;
4472}
4473
4474/* Return -1 on error.
4475
4476 Return 0 if we have a literal (possible zero length) and an
4477 expression (zero length if at the end of the string.
4478
4479 Return 1 if we have a literal, but no expression, and we want the
4480 caller to call us again. This is used to deal with doubled
4481 braces.
4482
4483 When called multiple times on the string 'a{{b{0}c', this function
4484 will return:
4485
4486 1. the literal 'a{' with no expression, and a return value
4487 of 1. Despite the fact that there's no expression, the return
4488 value of 1 means we're not finished yet.
4489
4490 2. the literal 'b' and the expression '0', with a return value of
4491 0. The fact that there's an expression means we're not finished.
4492
4493 3. literal 'c' with no expression and a return value of 0. The
4494 combination of the return value of 0 with no expression means
4495 we're finished.
4496*/
4497static int
4498fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4499 PyObject **literal, expr_ty *expression,
4500 struct compiling *c, const node *n)
4501{
4502 int result;
4503
4504 assert(*literal == NULL && *expression == NULL);
4505
4506 /* Get any literal string. */
4507 result = fstring_find_literal(str, ofs, literal, recurse_lvl, c, n);
4508 if (result < 0)
4509 goto error;
4510
4511 assert(result == 0 || result == 1);
4512
4513 if (result == 1)
4514 /* We have a literal, but don't look at the expression. */
4515 return 1;
4516
4517 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4518
4519 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4520 PyUnicode_READ_CHAR(str, *ofs) == '}')
4521 /* We're at the end of the string or the end of a nested
4522 f-string: no expression. The top-level error case where we
4523 expect to be at the end of the string but we're at a '}' is
4524 handled later. */
4525 return 0;
4526
4527 /* We must now be the start of an expression, on a '{'. */
4528 assert(*ofs < PyUnicode_GET_LENGTH(str) &&
4529 PyUnicode_READ_CHAR(str, *ofs) == '{');
4530
4531 if (fstring_find_expr(str, ofs, recurse_lvl, expression, c, n) < 0)
4532 goto error;
4533
4534 return 0;
4535
4536error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004537 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004538 return -1;
4539}
4540
4541#define EXPRLIST_N_CACHED 64
4542
4543typedef struct {
4544 /* Incrementally build an array of expr_ty, so be used in an
4545 asdl_seq. Cache some small but reasonably sized number of
4546 expr_ty's, and then after that start dynamically allocating,
4547 doubling the number allocated each time. Note that the f-string
4548 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4549 Str for the literal 'a'. So you add expr_ty's about twice as
4550 fast as you add exressions in an f-string. */
4551
4552 Py_ssize_t allocated; /* Number we've allocated. */
4553 Py_ssize_t size; /* Number we've used. */
4554 expr_ty *p; /* Pointer to the memory we're actually
4555 using. Will point to 'data' until we
4556 start dynamically allocating. */
4557 expr_ty data[EXPRLIST_N_CACHED];
4558} ExprList;
4559
4560#ifdef NDEBUG
4561#define ExprList_check_invariants(l)
4562#else
4563static void
4564ExprList_check_invariants(ExprList *l)
4565{
4566 /* Check our invariants. Make sure this object is "live", and
4567 hasn't been deallocated. */
4568 assert(l->size >= 0);
4569 assert(l->p != NULL);
4570 if (l->size <= EXPRLIST_N_CACHED)
4571 assert(l->data == l->p);
4572}
4573#endif
4574
4575static void
4576ExprList_Init(ExprList *l)
4577{
4578 l->allocated = EXPRLIST_N_CACHED;
4579 l->size = 0;
4580
4581 /* Until we start allocating dynamically, p points to data. */
4582 l->p = l->data;
4583
4584 ExprList_check_invariants(l);
4585}
4586
4587static int
4588ExprList_Append(ExprList *l, expr_ty exp)
4589{
4590 ExprList_check_invariants(l);
4591 if (l->size >= l->allocated) {
4592 /* We need to alloc (or realloc) the memory. */
4593 Py_ssize_t new_size = l->allocated * 2;
4594
4595 /* See if we've ever allocated anything dynamically. */
4596 if (l->p == l->data) {
4597 Py_ssize_t i;
4598 /* We're still using the cached data. Switch to
4599 alloc-ing. */
4600 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4601 if (!l->p)
4602 return -1;
4603 /* Copy the cached data into the new buffer. */
4604 for (i = 0; i < l->size; i++)
4605 l->p[i] = l->data[i];
4606 } else {
4607 /* Just realloc. */
4608 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4609 if (!tmp) {
4610 PyMem_RawFree(l->p);
4611 l->p = NULL;
4612 return -1;
4613 }
4614 l->p = tmp;
4615 }
4616
4617 l->allocated = new_size;
4618 assert(l->allocated == 2 * l->size);
4619 }
4620
4621 l->p[l->size++] = exp;
4622
4623 ExprList_check_invariants(l);
4624 return 0;
4625}
4626
4627static void
4628ExprList_Dealloc(ExprList *l)
4629{
4630 ExprList_check_invariants(l);
4631
4632 /* If there's been an error, or we've never dynamically allocated,
4633 do nothing. */
4634 if (!l->p || l->p == l->data) {
4635 /* Do nothing. */
4636 } else {
4637 /* We have dynamically allocated. Free the memory. */
4638 PyMem_RawFree(l->p);
4639 }
4640 l->p = NULL;
4641 l->size = -1;
4642}
4643
4644static asdl_seq *
4645ExprList_Finish(ExprList *l, PyArena *arena)
4646{
4647 asdl_seq *seq;
4648
4649 ExprList_check_invariants(l);
4650
4651 /* Allocate the asdl_seq and copy the expressions in to it. */
4652 seq = _Py_asdl_seq_new(l->size, arena);
4653 if (seq) {
4654 Py_ssize_t i;
4655 for (i = 0; i < l->size; i++)
4656 asdl_seq_SET(seq, i, l->p[i]);
4657 }
4658 ExprList_Dealloc(l);
4659 return seq;
4660}
4661
4662/* The FstringParser is designed to add a mix of strings and
4663 f-strings, and concat them together as needed. Ultimately, it
4664 generates an expr_ty. */
4665typedef struct {
4666 PyObject *last_str;
4667 ExprList expr_list;
4668} FstringParser;
4669
4670#ifdef NDEBUG
4671#define FstringParser_check_invariants(state)
4672#else
4673static void
4674FstringParser_check_invariants(FstringParser *state)
4675{
4676 if (state->last_str)
4677 assert(PyUnicode_CheckExact(state->last_str));
4678 ExprList_check_invariants(&state->expr_list);
4679}
4680#endif
4681
4682static void
4683FstringParser_Init(FstringParser *state)
4684{
4685 state->last_str = NULL;
4686 ExprList_Init(&state->expr_list);
4687 FstringParser_check_invariants(state);
4688}
4689
4690static void
4691FstringParser_Dealloc(FstringParser *state)
4692{
4693 FstringParser_check_invariants(state);
4694
4695 Py_XDECREF(state->last_str);
4696 ExprList_Dealloc(&state->expr_list);
4697}
4698
4699/* Make a Str node, but decref the PyUnicode object being added. */
4700static expr_ty
4701make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4702{
4703 PyObject *s = *str;
4704 *str = NULL;
4705 assert(PyUnicode_CheckExact(s));
4706 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4707 Py_DECREF(s);
4708 return NULL;
4709 }
4710 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4711}
4712
4713/* Add a non-f-string (that is, a regular literal string). str is
4714 decref'd. */
4715static int
4716FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4717{
4718 FstringParser_check_invariants(state);
4719
4720 assert(PyUnicode_CheckExact(str));
4721
4722 if (PyUnicode_GET_LENGTH(str) == 0) {
4723 Py_DECREF(str);
4724 return 0;
4725 }
4726
4727 if (!state->last_str) {
4728 /* We didn't have a string before, so just remember this one. */
4729 state->last_str = str;
4730 } else {
4731 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004732 PyUnicode_AppendAndDel(&state->last_str, str);
4733 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004734 return -1;
4735 }
4736 FstringParser_check_invariants(state);
4737 return 0;
4738}
4739
4740/* Parse an f-string. The f-string is in str, starting at ofs, with no 'f'
4741 or quotes. str is not decref'd, since we don't know if it's used elsewhere.
4742 And if we're only looking at a part of a string, then decref'ing is
4743 definitely not the right thing to do! */
4744static int
4745FstringParser_ConcatFstring(FstringParser *state, PyObject *str,
4746 Py_ssize_t *ofs, int recurse_lvl,
4747 struct compiling *c, const node *n)
4748{
4749 FstringParser_check_invariants(state);
4750
4751 /* Parse the f-string. */
4752 while (1) {
4753 PyObject *literal = NULL;
4754 expr_ty expression = NULL;
4755
4756 /* If there's a zero length literal in front of the
4757 expression, literal will be NULL. If we're at the end of
4758 the f-string, expression will be NULL (unless result == 1,
4759 see below). */
4760 int result = fstring_find_literal_and_expr(str, ofs, recurse_lvl,
4761 &literal, &expression,
4762 c, n);
4763 if (result < 0)
4764 return -1;
4765
4766 /* Add the literal, if any. */
4767 if (!literal) {
4768 /* Do nothing. Just leave last_str alone (and possibly
4769 NULL). */
4770 } else if (!state->last_str) {
4771 state->last_str = literal;
4772 literal = NULL;
4773 } else {
4774 /* We have a literal, concatenate it. */
4775 assert(PyUnicode_GET_LENGTH(literal) != 0);
4776 if (FstringParser_ConcatAndDel(state, literal) < 0)
4777 return -1;
4778 literal = NULL;
4779 }
4780 assert(!state->last_str ||
4781 PyUnicode_GET_LENGTH(state->last_str) != 0);
4782
4783 /* We've dealt with the literal now. It can't be leaked on further
4784 errors. */
4785 assert(literal == NULL);
4786
4787 /* See if we should just loop around to get the next literal
4788 and expression, while ignoring the expression this
4789 time. This is used for un-doubling braces, as an
4790 optimization. */
4791 if (result == 1)
4792 continue;
4793
4794 if (!expression)
4795 /* We're done with this f-string. */
4796 break;
4797
4798 /* We know we have an expression. Convert any existing string
4799 to a Str node. */
4800 if (!state->last_str) {
4801 /* Do nothing. No previous literal. */
4802 } else {
4803 /* Convert the existing last_str literal to a Str node. */
4804 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4805 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4806 return -1;
4807 }
4808
4809 if (ExprList_Append(&state->expr_list, expression) < 0)
4810 return -1;
4811 }
4812
4813 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4814
4815 /* If recurse_lvl is zero, then we must be at the end of the
4816 string. Otherwise, we must be at a right brace. */
4817
4818 if (recurse_lvl == 0 && *ofs < PyUnicode_GET_LENGTH(str)) {
4819 ast_error(c, n, "f-string: unexpected end of string");
4820 return -1;
4821 }
4822 if (recurse_lvl != 0 && PyUnicode_READ_CHAR(str, *ofs) != '}') {
4823 ast_error(c, n, "f-string: expecting '}'");
4824 return -1;
4825 }
4826
4827 FstringParser_check_invariants(state);
4828 return 0;
4829}
4830
4831/* Convert the partial state reflected in last_str and expr_list to an
4832 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4833static expr_ty
4834FstringParser_Finish(FstringParser *state, struct compiling *c,
4835 const node *n)
4836{
4837 asdl_seq *seq;
4838
4839 FstringParser_check_invariants(state);
4840
4841 /* If we're just a constant string with no expressions, return
4842 that. */
4843 if(state->expr_list.size == 0) {
4844 if (!state->last_str) {
4845 /* Create a zero length string. */
4846 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4847 if (!state->last_str)
4848 goto error;
4849 }
4850 return make_str_node_and_del(&state->last_str, c, n);
4851 }
4852
4853 /* Create a Str node out of last_str, if needed. It will be the
4854 last node in our expression list. */
4855 if (state->last_str) {
4856 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4857 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4858 goto error;
4859 }
4860 /* This has already been freed. */
4861 assert(state->last_str == NULL);
4862
4863 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4864 if (!seq)
4865 goto error;
4866
4867 /* If there's only one expression, return it. Otherwise, we need
4868 to join them together. */
4869 if (seq->size == 1)
4870 return seq->elements[0];
4871
4872 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4873
4874error:
4875 FstringParser_Dealloc(state);
4876 return NULL;
4877}
4878
4879/* Given an f-string (with no 'f' or quotes) that's in str starting at
4880 ofs, parse it into an expr_ty. Return NULL on error. Does not
4881 decref str. */
4882static expr_ty
4883fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4884 struct compiling *c, const node *n)
4885{
4886 FstringParser state;
4887
4888 FstringParser_Init(&state);
4889 if (FstringParser_ConcatFstring(&state, str, ofs, recurse_lvl,
4890 c, n) < 0) {
4891 FstringParser_Dealloc(&state);
4892 return NULL;
4893 }
4894
4895 return FstringParser_Finish(&state, c, n);
4896}
4897
4898/* n is a Python string literal, including the bracketing quote
4899 characters, and r, b, u, &/or f prefixes (if any), and embedded
4900 escape sequences (if any). parsestr parses it, and returns the
4901 decoded Python string object. If the string is an f-string, set
4902 *fmode and return the unparsed string object.
4903*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004904static PyObject *
Eric V. Smith235a6f02015-09-19 14:51:32 -04004905parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004906{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004907 size_t len;
4908 const char *s = STR(n);
4909 int quote = Py_CHARMASK(*s);
4910 int rawmode = 0;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004911 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004912 while (!*bytesmode || !rawmode) {
4913 if (quote == 'b' || quote == 'B') {
4914 quote = *++s;
4915 *bytesmode = 1;
4916 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004917 else if (quote == 'u' || quote == 'U') {
4918 quote = *++s;
4919 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004920 else if (quote == 'r' || quote == 'R') {
4921 quote = *++s;
4922 rawmode = 1;
4923 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924 else if (quote == 'f' || quote == 'F') {
4925 quote = *++s;
4926 *fmode = 1;
4927 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004928 else {
4929 break;
4930 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004931 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004932 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004933 if (*fmode && *bytesmode) {
4934 PyErr_BadInternalCall();
4935 return NULL;
4936 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004937 if (quote != '\'' && quote != '\"') {
4938 PyErr_BadInternalCall();
4939 return NULL;
4940 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004941 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004942 s++;
4943 len = strlen(s);
4944 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004946 "string to parse is too long");
4947 return NULL;
4948 }
4949 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004950 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004951 PyErr_BadInternalCall();
4952 return NULL;
4953 }
4954 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004955 /* A triple quoted string. We've already skipped one quote at
4956 the start and one at the end of the string. Now skip the
4957 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004958 s += 2;
4959 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004960 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004961 if (s[--len] != quote || s[--len] != quote) {
4962 PyErr_BadInternalCall();
4963 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004964 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004965 }
Benjamin Peterson768921c2016-02-25 23:13:53 -08004966 /* Avoid invoking escape decoding routines if possible. */
4967 rawmode = rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004968 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08004969 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004970 const char *ch;
4971 for (ch = s; *ch; ch++) {
4972 if (Py_CHARMASK(*ch) >= 0x80) {
4973 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004974 "literal characters.");
4975 return NULL;
4976 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004977 }
Benjamin Peterson768921c2016-02-25 23:13:53 -08004978 if (rawmode)
Christian Heimes72b710a2008-05-26 13:28:38 +00004979 return PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08004980 else
4981 return PyBytes_DecodeEscape(s, len, NULL, /* ignored */ 0, NULL);
4982 } else {
4983 if (rawmode)
4984 return PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
4985 else
4986 return decode_unicode_with_escapes(c, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004987 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004988}
4989
Eric V. Smith235a6f02015-09-19 14:51:32 -04004990/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
4991 each STRING atom, and process it as needed. For bytes, just
4992 concatenate them together, and the result will be a Bytes node. For
4993 normal strings and f-strings, concatenate them together. The result
4994 will be a Str node if there were no f-strings; a FormattedValue
4995 node if there's just an f-string (with no leading or trailing
4996 literals), or a JoinedStr node if there are multiple f-strings or
4997 any literals involved. */
4998static expr_ty
4999parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005000{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005001 int bytesmode = 0;
5002 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005003 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005004
5005 FstringParser state;
5006 FstringParser_Init(&state);
5007
5008 for (i = 0; i < NCH(n); i++) {
5009 int this_bytesmode = 0;
5010 int this_fmode = 0;
5011 PyObject *s;
5012
5013 REQ(CHILD(n, i), STRING);
5014 s = parsestr(c, CHILD(n, i), &this_bytesmode, &this_fmode);
5015 if (!s)
5016 goto error;
5017
5018 /* Check that we're not mixing bytes with unicode. */
5019 if (i != 0 && bytesmode != this_bytesmode) {
5020 ast_error(c, n, "cannot mix bytes and nonbytes literals");
5021 Py_DECREF(s);
5022 goto error;
5023 }
5024 bytesmode = this_bytesmode;
5025
5026 assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
5027
5028 if (bytesmode) {
5029 /* For bytes, concat as we go. */
5030 if (i == 0) {
5031 /* First time, just remember this value. */
5032 bytes_str = s;
5033 } else {
5034 PyBytes_ConcatAndDel(&bytes_str, s);
5035 if (!bytes_str)
5036 goto error;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005037 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005038 } else if (this_fmode) {
5039 /* This is an f-string. Concatenate and decref it. */
5040 Py_ssize_t ofs = 0;
5041 int result = FstringParser_ConcatFstring(&state, s, &ofs, 0, c, n);
5042 Py_DECREF(s);
5043 if (result < 0)
5044 goto error;
5045 } else {
5046 /* This is a regular string. Concatenate it. */
5047 if (FstringParser_ConcatAndDel(&state, s) < 0)
5048 goto error;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005049 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005050 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005051 if (bytesmode) {
5052 /* Just return the bytes object and we're done. */
5053 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5054 goto error;
5055 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5056 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005057
Eric V. Smith235a6f02015-09-19 14:51:32 -04005058 /* We're not a bytes string, bytes_str should never have been set. */
5059 assert(bytes_str == NULL);
5060
5061 return FstringParser_Finish(&state, c, n);
5062
5063error:
5064 Py_XDECREF(bytes_str);
5065 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005066 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005067}