blob: d19546a2f7ec1fc7df0bccaffd5e9444ef6e757f [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 {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000577 char *c_encoding; /* source encoding */
Eric V. Smith163b5c62015-08-21 09:40:38 -0400578 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200579 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500580 PyObject *c_normalize; /* Normalization function from unicodedata. */
581 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582};
583
584static asdl_seq *seq_for_testlist(struct compiling *, const node *);
585static expr_ty ast_for_expr(struct compiling *, const node *);
586static stmt_ty ast_for_stmt(struct compiling *, const node *);
587static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000588static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
589 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000590static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000591static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592
Yury Selivanov75445082015-05-11 22:57:16 -0400593static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
594static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
595
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596/* Note different signature for ast_for_call */
597static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
598
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000599static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400600static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601
Nick Coghlan650f0d02007-04-15 12:05:43 +0000602#define COMP_GENEXP 0
603#define COMP_LISTCOMP 1
604#define COMP_SETCOMP 2
605
Benjamin Peterson55e00432012-01-16 17:22:31 -0500606static int
607init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000608{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500609 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
610 if (!m)
611 return 0;
612 c->c_normalize = PyObject_GetAttrString(m, "normalize");
613 Py_DECREF(m);
614 if (!c->c_normalize)
615 return 0;
616 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500617 if (!c->c_normalize_args) {
618 Py_CLEAR(c->c_normalize);
619 return 0;
620 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200621 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500622 return 1;
623}
624
625static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400626new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500627{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400628 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500629 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000630 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500631 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500632 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000633 /* Check whether there are non-ASCII characters in the
634 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500635 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200636 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500637 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500638 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200639 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500640 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500641 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
642 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500643 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 if (!id2)
645 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200646 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000647 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000648 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200649 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
650 Py_DECREF(id);
651 return NULL;
652 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000653 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654}
655
Benjamin Peterson55e00432012-01-16 17:22:31 -0500656#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400659ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400661 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662
Victor Stinner14e461d2013-08-26 22:28:21 +0200663 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000665 Py_INCREF(Py_None);
666 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200668 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400669 if (!tmp)
670 return 0;
671 errstr = PyUnicode_FromString(errmsg);
672 if (!errstr) {
673 Py_DECREF(tmp);
674 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000675 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000676 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 Py_DECREF(errstr);
678 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 if (value) {
680 PyErr_SetObject(PyExc_SyntaxError, value);
681 Py_DECREF(value);
682 }
683 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684}
685
686/* num_stmts() returns number of contained statements.
687
688 Use this routine to determine how big a sequence is needed for
689 the statements in a parse tree. Its raison d'etre is this bit of
690 grammar:
691
692 stmt: simple_stmt | compound_stmt
693 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
694
695 A simple_stmt can contain multiple small_stmt elements joined
696 by semicolons. If the arg is a simple_stmt, the number of
697 small_stmt elements is returned.
698*/
699
700static int
701num_stmts(const node *n)
702{
703 int i, l;
704 node *ch;
705
706 switch (TYPE(n)) {
707 case single_input:
708 if (TYPE(CHILD(n, 0)) == NEWLINE)
709 return 0;
710 else
711 return num_stmts(CHILD(n, 0));
712 case file_input:
713 l = 0;
714 for (i = 0; i < NCH(n); i++) {
715 ch = CHILD(n, i);
716 if (TYPE(ch) == stmt)
717 l += num_stmts(ch);
718 }
719 return l;
720 case stmt:
721 return num_stmts(CHILD(n, 0));
722 case compound_stmt:
723 return 1;
724 case simple_stmt:
725 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
726 case suite:
727 if (NCH(n) == 1)
728 return num_stmts(CHILD(n, 0));
729 else {
730 l = 0;
731 for (i = 2; i < (NCH(n) - 1); i++)
732 l += num_stmts(CHILD(n, i));
733 return l;
734 }
735 default: {
736 char buf[128];
737
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000738 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 TYPE(n), NCH(n));
740 Py_FatalError(buf);
741 }
742 }
743 assert(0);
744 return 0;
745}
746
747/* Transform the CST rooted at node * to the appropriate AST
748*/
749
750mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200751PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
752 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000754 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 asdl_seq *stmts = NULL;
756 stmt_ty s;
757 node *ch;
758 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500759 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400761 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200762 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400763 c.c_filename = filename;
764 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000766 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000768#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400769 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500770 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000771#endif
772 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 } else if (TYPE(n) == encoding_decl) {
775 c.c_encoding = STR(n);
776 n = CHILD(n, 0);
777 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000779 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 }
781
Jeremy Hyltona8293132006-02-28 17:58:27 +0000782 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 switch (TYPE(n)) {
784 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200785 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500787 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 for (i = 0; i < NCH(n) - 1; i++) {
789 ch = CHILD(n, i);
790 if (TYPE(ch) == NEWLINE)
791 continue;
792 REQ(ch, stmt);
793 num = num_stmts(ch);
794 if (num == 1) {
795 s = ast_for_stmt(&c, ch);
796 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500797 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000798 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 }
800 else {
801 ch = CHILD(ch, 0);
802 REQ(ch, simple_stmt);
803 for (j = 0; j < num; j++) {
804 s = ast_for_stmt(&c, CHILD(ch, j * 2));
805 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500806 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000807 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 }
809 }
810 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500811 res = Module(stmts, arena);
812 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 case eval_input: {
814 expr_ty testlist_ast;
815
Nick Coghlan650f0d02007-04-15 12:05:43 +0000816 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000817 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500819 goto out;
820 res = Expression(testlist_ast, arena);
821 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 }
823 case single_input:
824 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200825 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500827 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000828 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
829 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000830 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500831 goto out;
832 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 }
834 else {
835 n = CHILD(n, 0);
836 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200837 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500839 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000841 s = ast_for_stmt(&c, n);
842 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 asdl_seq_SET(stmts, 0, s);
845 }
846 else {
847 /* Only a simple_stmt can contain multiple statements. */
848 REQ(n, simple_stmt);
849 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 if (TYPE(CHILD(n, i)) == NEWLINE)
851 break;
852 s = ast_for_stmt(&c, CHILD(n, i));
853 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500854 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 asdl_seq_SET(stmts, i / 2, s);
856 }
857 }
858
Benjamin Peterson55e00432012-01-16 17:22:31 -0500859 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500861 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000863 PyErr_Format(PyExc_SystemError,
864 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500867 out:
868 if (c.c_normalize) {
869 Py_DECREF(c.c_normalize);
870 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
871 Py_DECREF(c.c_normalize_args);
872 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500873 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874}
875
Victor Stinner14e461d2013-08-26 22:28:21 +0200876mod_ty
877PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
878 PyArena *arena)
879{
880 mod_ty mod;
881 PyObject *filename;
882 filename = PyUnicode_DecodeFSDefault(filename_str);
883 if (filename == NULL)
884 return NULL;
885 mod = PyAST_FromNodeObject(n, flags, filename, arena);
886 Py_DECREF(filename);
887 return mod;
888
889}
890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
892*/
893
894static operator_ty
895get_operator(const node *n)
896{
897 switch (TYPE(n)) {
898 case VBAR:
899 return BitOr;
900 case CIRCUMFLEX:
901 return BitXor;
902 case AMPER:
903 return BitAnd;
904 case LEFTSHIFT:
905 return LShift;
906 case RIGHTSHIFT:
907 return RShift;
908 case PLUS:
909 return Add;
910 case MINUS:
911 return Sub;
912 case STAR:
913 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400914 case AT:
915 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916 case SLASH:
917 return Div;
918 case DOUBLESLASH:
919 return FloorDiv;
920 case PERCENT:
921 return Mod;
922 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 }
925}
926
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200927static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000928 "None",
929 "True",
930 "False",
931 NULL,
932};
933
934static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400935forbidden_name(struct compiling *c, identifier name, const node *n,
936 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000937{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000938 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000939 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400940 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000941 return 1;
942 }
943 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200944 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000945 for (p = FORBIDDEN; *p; p++) {
946 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400947 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000948 return 1;
949 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000950 }
951 }
952 return 0;
953}
954
Jeremy Hyltona8293132006-02-28 17:58:27 +0000955/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956
957 Only sets context for expr kinds that "can appear in assignment context"
958 (according to ../Parser/Python.asdl). For other expr kinds, it sets
959 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960*/
961
962static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000963set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964{
965 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000966 /* If a particular expression type can't be used for assign / delete,
967 set expr_name to its name and an error message will be generated.
968 */
969 const char* expr_name = NULL;
970
971 /* The ast defines augmented store and load contexts, but the
972 implementation here doesn't actually use them. The code may be
973 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000974 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000976 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000977 */
978 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979
980 switch (e->kind) {
981 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000982 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400983 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000984 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000985 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000987 e->v.Subscript.ctx = ctx;
988 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000989 case Starred_kind:
990 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000991 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000992 return 0;
993 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000995 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500996 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000997 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000998 }
999 e->v.Name.ctx = ctx;
1000 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001002 e->v.List.ctx = ctx;
1003 s = e->v.List.elts;
1004 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +00001006 if (asdl_seq_LEN(e->v.Tuple.elts)) {
1007 e->v.Tuple.ctx = ctx;
1008 s = e->v.Tuple.elts;
1009 }
1010 else {
1011 expr_name = "()";
1012 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001013 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001014 case Lambda_kind:
1015 expr_name = "lambda";
1016 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001018 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001019 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001020 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001022 case UnaryOp_kind:
1023 expr_name = "operator";
1024 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001026 expr_name = "generator expression";
1027 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001028 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001029 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001030 expr_name = "yield expression";
1031 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001032 case Await_kind:
1033 expr_name = "await expression";
1034 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001035 case ListComp_kind:
1036 expr_name = "list comprehension";
1037 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001038 case SetComp_kind:
1039 expr_name = "set comprehension";
1040 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001041 case DictComp_kind:
1042 expr_name = "dict comprehension";
1043 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001044 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001045 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 case Num_kind:
1047 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001048 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001049 case JoinedStr_kind:
1050 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001051 expr_name = "literal";
1052 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001053 case NameConstant_kind:
1054 expr_name = "keyword";
1055 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001056 case Ellipsis_kind:
1057 expr_name = "Ellipsis";
1058 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001059 case Compare_kind:
1060 expr_name = "comparison";
1061 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001062 case IfExp_kind:
1063 expr_name = "conditional expression";
1064 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001065 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 PyErr_Format(PyExc_SystemError,
1067 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001068 e->kind, e->lineno);
1069 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001071 /* Check for error string set by switch */
1072 if (expr_name) {
1073 char buf[300];
1074 PyOS_snprintf(buf, sizeof(buf),
1075 "can't %s %s",
1076 ctx == Store ? "assign to" : "delete",
1077 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001078 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001079 }
1080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 */
1084 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001085 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086
Thomas Wouters89f507f2006-12-13 04:49:30 +00001087 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001088 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001089 return 0;
1090 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 }
1092 return 1;
1093}
1094
1095static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001096ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097{
1098 REQ(n, augassign);
1099 n = CHILD(n, 0);
1100 switch (STR(n)[0]) {
1101 case '+':
1102 return Add;
1103 case '-':
1104 return Sub;
1105 case '/':
1106 if (STR(n)[1] == '/')
1107 return FloorDiv;
1108 else
1109 return Div;
1110 case '%':
1111 return Mod;
1112 case '<':
1113 return LShift;
1114 case '>':
1115 return RShift;
1116 case '&':
1117 return BitAnd;
1118 case '^':
1119 return BitXor;
1120 case '|':
1121 return BitOr;
1122 case '*':
1123 if (STR(n)[1] == '*')
1124 return Pow;
1125 else
1126 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001127 case '@':
1128 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001130 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001131 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 }
1133}
1134
1135static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001136ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001138 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 |'is' 'not'
1140 */
1141 REQ(n, comp_op);
1142 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001143 n = CHILD(n, 0);
1144 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 case LESS:
1146 return Lt;
1147 case GREATER:
1148 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001149 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 return Eq;
1151 case LESSEQUAL:
1152 return LtE;
1153 case GREATEREQUAL:
1154 return GtE;
1155 case NOTEQUAL:
1156 return NotEq;
1157 case NAME:
1158 if (strcmp(STR(n), "in") == 0)
1159 return In;
1160 if (strcmp(STR(n), "is") == 0)
1161 return Is;
1162 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001163 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001165 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001166 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 }
1168 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001169 /* handle "not in" and "is not" */
1170 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 case NAME:
1172 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1173 return NotIn;
1174 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1175 return IsNot;
1176 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001177 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001179 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001180 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 }
Neal Norwitz79792652005-11-14 04:25:03 +00001182 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001184 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185}
1186
1187static asdl_seq *
1188seq_for_testlist(struct compiling *c, const node *n)
1189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001191 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1192 */
Armin Rigo31441302005-10-21 12:57:31 +00001193 asdl_seq *seq;
1194 expr_ty expression;
1195 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001196 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001198 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 if (!seq)
1200 return NULL;
1201
1202 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001204 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205
Benjamin Peterson4905e802009-09-27 02:43:28 +00001206 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001207 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209
1210 assert(i / 2 < seq->size);
1211 asdl_seq_SET(seq, i / 2, expression);
1212 }
1213 return seq;
1214}
1215
Neal Norwitzc1505362006-12-28 06:47:50 +00001216static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001217ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001218{
1219 identifier name;
1220 expr_ty annotation = NULL;
1221 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001222 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001223
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001224 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001225 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001226 name = NEW_IDENTIFIER(ch);
1227 if (!name)
1228 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001229 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001230 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001231
1232 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1233 annotation = ast_for_expr(c, CHILD(n, 2));
1234 if (!annotation)
1235 return NULL;
1236 }
1237
Victor Stinnerc106c682015-11-06 17:01:48 +01001238 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001239 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001240 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001241 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242}
1243
Guido van Rossum4f72a782006-10-27 23:31:49 +00001244/* returns -1 if failed to handle keyword only arguments
1245 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001246 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001247 ^^^
1248 start pointing here
1249 */
1250static int
1251handle_keywordonly_args(struct compiling *c, const node *n, int start,
1252 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1253{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001254 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001255 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001256 expr_ty expression, annotation;
1257 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001258 int i = start;
1259 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001260
1261 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001262 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001263 return -1;
1264 }
1265 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001266 while (i < NCH(n)) {
1267 ch = CHILD(n, i);
1268 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001269 case vfpdef:
1270 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001271 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001272 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001273 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001274 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001275 asdl_seq_SET(kwdefaults, j, expression);
1276 i += 2; /* '=' and test */
1277 }
1278 else { /* setting NULL if no default value exists */
1279 asdl_seq_SET(kwdefaults, j, NULL);
1280 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001281 if (NCH(ch) == 3) {
1282 /* ch is NAME ':' test */
1283 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001284 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001285 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001286 }
1287 else {
1288 annotation = NULL;
1289 }
1290 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001291 argname = NEW_IDENTIFIER(ch);
1292 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001293 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001294 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001295 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001296 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1297 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001298 if (!arg)
1299 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001300 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001301 i += 2; /* the name and the comma */
1302 break;
1303 case DOUBLESTAR:
1304 return i;
1305 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001306 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001307 goto error;
1308 }
1309 }
1310 return i;
1311 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001313}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314
Jeremy Hyltona8293132006-02-28 17:58:27 +00001315/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316
1317static arguments_ty
1318ast_for_arguments(struct compiling *c, const node *n)
1319{
Neal Norwitzc1505362006-12-28 06:47:50 +00001320 /* This function handles both typedargslist (function definition)
1321 and varargslist (lambda definition).
1322
1323 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001324 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1325 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1326 | '**' tfpdef [',']]]
1327 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1328 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001329 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001330 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1331 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1332 | '**' vfpdef [',']]]
1333 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1334 | '**' vfpdef [',']
1335 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001336 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001337
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001339 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1340 int nposdefaults = 0, found_default = 0;
1341 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001342 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001343 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 node *ch;
1345
1346 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001347 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001348 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001349 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001351 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352
Jeremy Hyltone921e022008-07-17 16:37:17 +00001353 /* First count the number of positional args & defaults. The
1354 variable i is the loop index for this for loop and the next.
1355 The next loop picks up where the first leaves off.
1356 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001358 ch = CHILD(n, i);
1359 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001360 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001361 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001362 if (i < NCH(n) && /* skip argument following star */
1363 (TYPE(CHILD(n, i)) == tfpdef ||
1364 TYPE(CHILD(n, i)) == vfpdef)) {
1365 i++;
1366 }
1367 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001368 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001369 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 defaults for keyword only args */
1375 for ( ; i < NCH(n); ++i) {
1376 ch = CHILD(n, i);
1377 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001378 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001379 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001380 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001382 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001384 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001386 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001388 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001390 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 since we set NULL as default for keyword only argument w/o default
1393 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001394 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001395 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001397 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398
1399 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001400 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001401 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001404 /* tfpdef: NAME [':' test]
1405 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 */
1407 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001408 j = 0; /* index for defaults */
1409 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 ch = CHILD(n, i);
1412 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001413 case tfpdef:
1414 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1416 anything other than EQUAL or a comma? */
1417 /* XXX Should NCH(n) check be made a separate check? */
1418 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001419 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1420 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001421 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001422 assert(posdefaults != NULL);
1423 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001425 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001427 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001428 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001429 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001430 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001431 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001432 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001433 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001434 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001435 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 i += 2; /* the name and the comma */
1437 break;
1438 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001439 if (i+1 >= NCH(n) ||
1440 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001441 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001442 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001443 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001444 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001445 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001446 if (TYPE(ch) == COMMA) {
1447 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 i += 2; /* now follows keyword only arguments */
1449 res = handle_keywordonly_args(c, n, i,
1450 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001451 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001452 i = res; /* res has new position to process */
1453 }
1454 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001455 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001456 if (!vararg)
1457 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001458
Guido van Rossum4f72a782006-10-27 23:31:49 +00001459 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001460 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1461 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001462 int res = 0;
1463 res = handle_keywordonly_args(c, n, i,
1464 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001465 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001466 i = res; /* res has new position to process */
1467 }
1468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 break;
1470 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001471 ch = CHILD(n, i+1); /* tfpdef */
1472 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001473 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001474 if (!kwarg)
1475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 i += 3;
1477 break;
1478 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001479 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 "unexpected node in varargslist: %d @ %d",
1481 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001482 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001483 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001485 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486}
1487
1488static expr_ty
1489ast_for_dotted_name(struct compiling *c, const node *n)
1490{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001491 expr_ty e;
1492 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001493 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 int i;
1495
1496 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001497
1498 lineno = LINENO(n);
1499 col_offset = n->n_col_offset;
1500
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 id = NEW_IDENTIFIER(CHILD(n, 0));
1502 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001503 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001504 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001506 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507
1508 for (i = 2; i < NCH(n); i+=2) {
1509 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001510 if (!id)
1511 return NULL;
1512 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1513 if (!e)
1514 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 }
1516
1517 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518}
1519
1520static expr_ty
1521ast_for_decorator(struct compiling *c, const node *n)
1522{
1523 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1524 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001525 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001528 REQ(CHILD(n, 0), AT);
1529 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1532 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001533 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001536 d = name_expr;
1537 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 }
1539 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001540 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001541 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001542 if (!d)
1543 return NULL;
1544 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 }
1546 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001547 d = ast_for_call(c, CHILD(n, 3), name_expr);
1548 if (!d)
1549 return NULL;
1550 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 }
1552
1553 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554}
1555
1556static asdl_seq*
1557ast_for_decorators(struct compiling *c, const node *n)
1558{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001559 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001560 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001564 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 if (!decorator_seq)
1566 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001569 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001570 if (!d)
1571 return NULL;
1572 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573 }
1574 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575}
1576
1577static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001578ast_for_funcdef_impl(struct compiling *c, const node *n,
1579 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001581 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001582 identifier name;
1583 arguments_ty args;
1584 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001585 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001586 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587
1588 REQ(n, funcdef);
1589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 name = NEW_IDENTIFIER(CHILD(n, name_i));
1591 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001592 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001593 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001594 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1596 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001597 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001598 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1599 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1600 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001601 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001602 name_i += 2;
1603 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 body = ast_for_suite(c, CHILD(n, name_i + 3));
1605 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001606 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607
Yury Selivanov75445082015-05-11 22:57:16 -04001608 if (is_async)
1609 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1610 LINENO(n),
1611 n->n_col_offset, c->c_arena);
1612 else
1613 return FunctionDef(name, args, body, decorator_seq, returns,
1614 LINENO(n),
1615 n->n_col_offset, c->c_arena);
1616}
1617
1618static stmt_ty
1619ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1620{
1621 /* async_funcdef: ASYNC funcdef */
1622 REQ(n, async_funcdef);
1623 REQ(CHILD(n, 0), ASYNC);
1624 REQ(CHILD(n, 1), funcdef);
1625
1626 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1627 1 /* is_async */);
1628}
1629
1630static stmt_ty
1631ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1632{
1633 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1634 return ast_for_funcdef_impl(c, n, decorator_seq,
1635 0 /* is_async */);
1636}
1637
1638
1639static stmt_ty
1640ast_for_async_stmt(struct compiling *c, const node *n)
1641{
1642 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1643 REQ(n, async_stmt);
1644 REQ(CHILD(n, 0), ASYNC);
1645
1646 switch (TYPE(CHILD(n, 1))) {
1647 case funcdef:
1648 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1649 1 /* is_async */);
1650 case with_stmt:
1651 return ast_for_with_stmt(c, CHILD(n, 1),
1652 1 /* is_async */);
1653
1654 case for_stmt:
1655 return ast_for_for_stmt(c, CHILD(n, 1),
1656 1 /* is_async */);
1657
1658 default:
1659 PyErr_Format(PyExc_SystemError,
1660 "invalid async stament: %s",
1661 STR(CHILD(n, 1)));
1662 return NULL;
1663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664}
1665
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001666static stmt_ty
1667ast_for_decorated(struct compiling *c, const node *n)
1668{
Yury Selivanov75445082015-05-11 22:57:16 -04001669 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001670 stmt_ty thing = NULL;
1671 asdl_seq *decorator_seq = NULL;
1672
1673 REQ(n, decorated);
1674
1675 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1676 if (!decorator_seq)
1677 return NULL;
1678
1679 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001680 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001682
1683 if (TYPE(CHILD(n, 1)) == funcdef) {
1684 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1685 } else if (TYPE(CHILD(n, 1)) == classdef) {
1686 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001687 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1688 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001689 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001690 /* we count the decorators in when talking about the class' or
1691 * function's line number */
1692 if (thing) {
1693 thing->lineno = LINENO(n);
1694 thing->col_offset = n->n_col_offset;
1695 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001696 return thing;
1697}
1698
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699static expr_ty
1700ast_for_lambdef(struct compiling *c, const node *n)
1701{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001702 /* lambdef: 'lambda' [varargslist] ':' test
1703 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 arguments_ty args;
1705 expr_ty expression;
1706
1707 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001708 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 if (!args)
1710 return NULL;
1711 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001712 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 }
1715 else {
1716 args = ast_for_arguments(c, CHILD(n, 1));
1717 if (!args)
1718 return NULL;
1719 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001720 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 }
1723
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001724 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725}
1726
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001727static expr_ty
1728ast_for_ifexpr(struct compiling *c, const node *n)
1729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001731 expr_ty expression, body, orelse;
1732
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001733 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001734 body = ast_for_expr(c, CHILD(n, 0));
1735 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001736 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001737 expression = ast_for_expr(c, CHILD(n, 2));
1738 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001739 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001740 orelse = ast_for_expr(c, CHILD(n, 4));
1741 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001742 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001743 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1744 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001745}
1746
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001748 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749
Nick Coghlan650f0d02007-04-15 12:05:43 +00001750 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751*/
1752
1753static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001754count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001756 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757
Guido van Rossumd8faa362007-04-27 19:54:29 +00001758 count_comp_for:
1759 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001760 REQ(n, comp_for);
1761 if (NCH(n) == 5)
1762 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001763 else
1764 return n_fors;
1765 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001766 REQ(n, comp_iter);
1767 n = CHILD(n, 0);
1768 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001769 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001770 else if (TYPE(n) == comp_if) {
1771 if (NCH(n) == 3) {
1772 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001773 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001774 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001775 else
1776 return n_fors;
1777 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001778
Guido van Rossumd8faa362007-04-27 19:54:29 +00001779 /* Should never be reached */
1780 PyErr_SetString(PyExc_SystemError,
1781 "logic error in count_comp_fors");
1782 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783}
1784
Nick Coghlan650f0d02007-04-15 12:05:43 +00001785/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786
Nick Coghlan650f0d02007-04-15 12:05:43 +00001787 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788*/
1789
1790static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001791count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001793 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794
Guido van Rossumd8faa362007-04-27 19:54:29 +00001795 while (1) {
1796 REQ(n, comp_iter);
1797 if (TYPE(CHILD(n, 0)) == comp_for)
1798 return n_ifs;
1799 n = CHILD(n, 0);
1800 REQ(n, comp_if);
1801 n_ifs++;
1802 if (NCH(n) == 2)
1803 return n_ifs;
1804 n = CHILD(n, 2);
1805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806}
1807
Guido van Rossum992d4a32007-07-11 13:09:30 +00001808static asdl_seq *
1809ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001812 asdl_seq *comps;
1813
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001814 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 if (n_fors == -1)
1816 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001818 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001819 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001821
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001823 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001825 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001826 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827
Guido van Rossum992d4a32007-07-11 13:09:30 +00001828 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829
Guido van Rossum992d4a32007-07-11 13:09:30 +00001830 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001831 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001832 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001834 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001835 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001837
Thomas Wouters89f507f2006-12-13 04:49:30 +00001838 /* Check the # of children rather than the length of t, since
1839 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001840 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001841 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001842 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001844 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1845 c->c_arena),
1846 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001847 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849
Guido van Rossum992d4a32007-07-11 13:09:30 +00001850 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 int j, n_ifs;
1852 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853
Guido van Rossum992d4a32007-07-11 13:09:30 +00001854 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001855 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001859 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001862
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001864 REQ(n, comp_iter);
1865 n = CHILD(n, 0);
1866 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867
Guido van Rossum992d4a32007-07-11 13:09:30 +00001868 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001870 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001871 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001872 if (NCH(n) == 3)
1873 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001875 /* on exit, must guarantee that n is a comp_for */
1876 if (TYPE(n) == comp_iter)
1877 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001878 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001880 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001882 return comps;
1883}
1884
1885static expr_ty
1886ast_for_itercomp(struct compiling *c, const node *n, int type)
1887{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001888 /* testlist_comp: (test|star_expr)
1889 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001890 expr_ty elt;
1891 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001892 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893
Guido van Rossum992d4a32007-07-11 13:09:30 +00001894 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001896 ch = CHILD(n, 0);
1897 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001898 if (!elt)
1899 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001900 if (elt->kind == Starred_kind) {
1901 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1902 return NULL;
1903 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904
Guido van Rossum992d4a32007-07-11 13:09:30 +00001905 comps = ast_for_comprehension(c, CHILD(n, 1));
1906 if (!comps)
1907 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001908
1909 if (type == COMP_GENEXP)
1910 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1911 else if (type == COMP_LISTCOMP)
1912 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1913 else if (type == COMP_SETCOMP)
1914 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1915 else
1916 /* Should never happen */
1917 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918}
1919
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001920/* Fills in the key, value pair corresponding to the dict element. In case
1921 * of an unpacking, key is NULL. *i is advanced by the number of ast
1922 * elements. Iff successful, nonzero is returned.
1923 */
1924static int
1925ast_for_dictelement(struct compiling *c, const node *n, int *i,
1926 expr_ty *key, expr_ty *value)
1927{
1928 expr_ty expression;
1929 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1930 assert(NCH(n) - *i >= 2);
1931
1932 expression = ast_for_expr(c, CHILD(n, *i + 1));
1933 if (!expression)
1934 return 0;
1935 *key = NULL;
1936 *value = expression;
1937
1938 *i += 2;
1939 }
1940 else {
1941 assert(NCH(n) - *i >= 3);
1942
1943 expression = ast_for_expr(c, CHILD(n, *i));
1944 if (!expression)
1945 return 0;
1946 *key = expression;
1947
1948 REQ(CHILD(n, *i + 1), COLON);
1949
1950 expression = ast_for_expr(c, CHILD(n, *i + 2));
1951 if (!expression)
1952 return 0;
1953 *value = expression;
1954
1955 *i += 3;
1956 }
1957 return 1;
1958}
1959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001961ast_for_dictcomp(struct compiling *c, const node *n)
1962{
1963 expr_ty key, value;
1964 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001965 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001967 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001968 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001969 assert(key);
1970 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001972 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001973 if (!comps)
1974 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975
Guido van Rossum992d4a32007-07-11 13:09:30 +00001976 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1977}
1978
1979static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001980ast_for_dictdisplay(struct compiling *c, const node *n)
1981{
1982 int i;
1983 int j;
1984 int size;
1985 asdl_seq *keys, *values;
1986
1987 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1988 keys = _Py_asdl_seq_new(size, c->c_arena);
1989 if (!keys)
1990 return NULL;
1991
1992 values = _Py_asdl_seq_new(size, c->c_arena);
1993 if (!values)
1994 return NULL;
1995
1996 j = 0;
1997 for (i = 0; i < NCH(n); i++) {
1998 expr_ty key, value;
1999
2000 if (!ast_for_dictelement(c, n, &i, &key, &value))
2001 return NULL;
2002 asdl_seq_SET(keys, j, key);
2003 asdl_seq_SET(values, j, value);
2004
2005 j++;
2006 }
2007 keys->size = j;
2008 values->size = j;
2009 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2010}
2011
2012static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002013ast_for_genexp(struct compiling *c, const node *n)
2014{
2015 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002016 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002017}
2018
2019static expr_ty
2020ast_for_listcomp(struct compiling *c, const node *n)
2021{
2022 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002023 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002024}
2025
2026static expr_ty
2027ast_for_setcomp(struct compiling *c, const node *n)
2028{
2029 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002030 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002031}
2032
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002033static expr_ty
2034ast_for_setdisplay(struct compiling *c, const node *n)
2035{
2036 int i;
2037 int size;
2038 asdl_seq *elts;
2039
2040 assert(TYPE(n) == (dictorsetmaker));
2041 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2042 elts = _Py_asdl_seq_new(size, c->c_arena);
2043 if (!elts)
2044 return NULL;
2045 for (i = 0; i < NCH(n); i += 2) {
2046 expr_ty expression;
2047 expression = ast_for_expr(c, CHILD(n, i));
2048 if (!expression)
2049 return NULL;
2050 asdl_seq_SET(elts, i / 2, expression);
2051 }
2052 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2053}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002054
2055static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056ast_for_atom(struct compiling *c, const node *n)
2057{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002058 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2059 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002060 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 */
2062 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002065 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002066 PyObject *name;
2067 const char *s = STR(ch);
2068 size_t len = strlen(s);
2069 if (len >= 4 && len <= 5) {
2070 if (!strcmp(s, "None"))
2071 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2072 if (!strcmp(s, "True"))
2073 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2074 if (!strcmp(s, "False"))
2075 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2076 }
2077 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002078 if (!name)
2079 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002080 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002081 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2082 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002084 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002085 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002086 const char *errtype = NULL;
2087 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2088 errtype = "unicode error";
2089 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2090 errtype = "value error";
2091 if (errtype) {
2092 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002093 PyObject *type, *value, *tback, *errstr;
2094 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002095 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002096 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002097 char *s = _PyUnicode_AsString(errstr);
2098 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002099 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002100 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002101 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002102 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002103 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002104 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002105 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002106 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002107 Py_XDECREF(tback);
2108 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002109 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002110 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002111 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 }
2113 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002114 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002115 if (!pynum)
2116 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002117
Victor Stinner43d81952013-07-17 00:57:58 +02002118 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2119 Py_DECREF(pynum);
2120 return NULL;
2121 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002122 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 }
Georg Brandldde00282007-03-18 19:01:53 +00002124 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002125 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002127 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128
Thomas Wouters89f507f2006-12-13 04:49:30 +00002129 if (TYPE(ch) == RPAR)
2130 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131
Thomas Wouters89f507f2006-12-13 04:49:30 +00002132 if (TYPE(ch) == yield_expr)
2133 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002136 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002137 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002138
Nick Coghlan650f0d02007-04-15 12:05:43 +00002139 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002141 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 if (TYPE(ch) == RSQB)
2144 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145
Nick Coghlan650f0d02007-04-15 12:05:43 +00002146 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002147 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2148 asdl_seq *elts = seq_for_testlist(c, ch);
2149 if (!elts)
2150 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002151
Thomas Wouters89f507f2006-12-13 04:49:30 +00002152 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2153 }
2154 else
2155 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002157 /* dictorsetmaker: ( ((test ':' test | '**' test)
2158 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2159 * ((test | '*' test)
2160 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002161 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002162 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002163 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002164 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002165 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002166 }
2167 else {
2168 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2169 if (NCH(ch) == 1 ||
2170 (NCH(ch) > 1 &&
2171 TYPE(CHILD(ch, 1)) == COMMA)) {
2172 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002173 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002174 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002175 else if (NCH(ch) > 1 &&
2176 TYPE(CHILD(ch, 1)) == comp_for) {
2177 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002178 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002179 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002180 else if (NCH(ch) > 3 - is_dict &&
2181 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2182 /* It's a dictionary comprehension. */
2183 if (is_dict) {
2184 ast_error(c, n, "dict unpacking cannot be used in "
2185 "dict comprehension");
2186 return NULL;
2187 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002188 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002189 }
2190 else {
2191 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002192 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002193 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002194 if (res) {
2195 res->lineno = LINENO(n);
2196 res->col_offset = n->n_col_offset;
2197 }
2198 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002199 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002202 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 }
2205}
2206
2207static slice_ty
2208ast_for_slice(struct compiling *c, const node *n)
2209{
2210 node *ch;
2211 expr_ty lower = NULL, upper = NULL, step = NULL;
2212
2213 REQ(n, subscript);
2214
2215 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002216 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 sliceop: ':' [test]
2218 */
2219 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 if (NCH(n) == 1 && TYPE(ch) == test) {
2221 /* 'step' variable hold no significance in terms of being used over
2222 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 if (!step)
2225 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226
Thomas Wouters89f507f2006-12-13 04:49:30 +00002227 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 }
2229
2230 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002231 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 if (!lower)
2233 return NULL;
2234 }
2235
2236 /* If there's an upper bound it's in the second or third position. */
2237 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002238 if (NCH(n) > 1) {
2239 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240
Thomas Wouters89f507f2006-12-13 04:49:30 +00002241 if (TYPE(n2) == test) {
2242 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 if (!upper)
2244 return NULL;
2245 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002248 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249
Thomas Wouters89f507f2006-12-13 04:49:30 +00002250 if (TYPE(n2) == test) {
2251 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 if (!upper)
2253 return NULL;
2254 }
2255 }
2256
2257 ch = CHILD(n, NCH(n) - 1);
2258 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002259 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002260 ch = CHILD(ch, 1);
2261 if (TYPE(ch) == test) {
2262 step = ast_for_expr(c, ch);
2263 if (!step)
2264 return NULL;
2265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 }
2267 }
2268
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002269 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270}
2271
2272static expr_ty
2273ast_for_binop(struct compiling *c, const node *n)
2274{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002275 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002277 BinOp(BinOp(A, op, B), op, C).
2278 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279
Guido van Rossumd8faa362007-04-27 19:54:29 +00002280 int i, nops;
2281 expr_ty expr1, expr2, result;
2282 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Guido van Rossumd8faa362007-04-27 19:54:29 +00002284 expr1 = ast_for_expr(c, CHILD(n, 0));
2285 if (!expr1)
2286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287
Guido van Rossumd8faa362007-04-27 19:54:29 +00002288 expr2 = ast_for_expr(c, CHILD(n, 2));
2289 if (!expr2)
2290 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291
Guido van Rossumd8faa362007-04-27 19:54:29 +00002292 newoperator = get_operator(CHILD(n, 1));
2293 if (!newoperator)
2294 return NULL;
2295
2296 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2297 c->c_arena);
2298 if (!result)
2299 return NULL;
2300
2301 nops = (NCH(n) - 1) / 2;
2302 for (i = 1; i < nops; i++) {
2303 expr_ty tmp_result, tmp;
2304 const node* next_oper = CHILD(n, i * 2 + 1);
2305
2306 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002307 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 return NULL;
2309
Guido van Rossumd8faa362007-04-27 19:54:29 +00002310 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2311 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 return NULL;
2313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002315 LINENO(next_oper), next_oper->n_col_offset,
2316 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002318 return NULL;
2319 result = tmp_result;
2320 }
2321 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322}
2323
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002324static expr_ty
2325ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002328 subscriptlist: subscript (',' subscript)* [',']
2329 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2330 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002331 REQ(n, trailer);
2332 if (TYPE(CHILD(n, 0)) == LPAR) {
2333 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002334 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002335 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002336 else
2337 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002338 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002339 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002340 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2341 if (!attr_id)
2342 return NULL;
2343 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002344 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002345 }
2346 else {
2347 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002348 REQ(CHILD(n, 2), RSQB);
2349 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002350 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002351 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2352 if (!slc)
2353 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002354 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2355 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002356 }
2357 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002359 by treating the sequence as a tuple literal if there are
2360 no slice features.
2361 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002362 int j;
2363 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002364 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002365 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002366 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002367 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002368 if (!slices)
2369 return NULL;
2370 for (j = 0; j < NCH(n); j += 2) {
2371 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002372 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002373 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002374 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002375 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002376 asdl_seq_SET(slices, j / 2, slc);
2377 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002378 if (!simple) {
2379 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002380 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002381 }
2382 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002383 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002384 if (!elts)
2385 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002386 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2387 slc = (slice_ty)asdl_seq_GET(slices, j);
2388 assert(slc->kind == Index_kind && slc->v.Index.value);
2389 asdl_seq_SET(elts, j, slc->v.Index.value);
2390 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002391 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002392 if (!e)
2393 return NULL;
2394 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002395 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002396 }
2397 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002398}
2399
2400static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002401ast_for_factor(struct compiling *c, const node *n)
2402{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002403 expr_ty expression;
2404
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002405 expression = ast_for_expr(c, CHILD(n, 1));
2406 if (!expression)
2407 return NULL;
2408
2409 switch (TYPE(CHILD(n, 0))) {
2410 case PLUS:
2411 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2412 c->c_arena);
2413 case MINUS:
2414 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2415 c->c_arena);
2416 case TILDE:
2417 return UnaryOp(Invert, expression, LINENO(n),
2418 n->n_col_offset, c->c_arena);
2419 }
2420 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2421 TYPE(CHILD(n, 0)));
2422 return NULL;
2423}
2424
2425static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002426ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002427{
Yury Selivanov75445082015-05-11 22:57:16 -04002428 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002429 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002430
2431 REQ(n, atom_expr);
2432 nch = NCH(n);
2433
2434 if (TYPE(CHILD(n, 0)) == AWAIT) {
2435 start = 1;
2436 assert(nch > 1);
2437 }
2438
2439 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002440 if (!e)
2441 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002442 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002443 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002444 if (start && nch == 2) {
2445 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2446 }
2447
2448 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002449 node *ch = CHILD(n, i);
2450 if (TYPE(ch) != trailer)
2451 break;
2452 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002453 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002454 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002455 tmp->lineno = e->lineno;
2456 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002457 e = tmp;
2458 }
Yury Selivanov75445082015-05-11 22:57:16 -04002459
2460 if (start) {
2461 /* there was an AWAIT */
2462 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2463 }
2464 else {
2465 return e;
2466 }
2467}
2468
2469static expr_ty
2470ast_for_power(struct compiling *c, const node *n)
2471{
2472 /* power: atom trailer* ('**' factor)*
2473 */
2474 expr_ty e;
2475 REQ(n, power);
2476 e = ast_for_atom_expr(c, CHILD(n, 0));
2477 if (!e)
2478 return NULL;
2479 if (NCH(n) == 1)
2480 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002481 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2482 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002483 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002484 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002485 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002486 }
2487 return e;
2488}
2489
Guido van Rossum0368b722007-05-11 16:50:42 +00002490static expr_ty
2491ast_for_starred(struct compiling *c, const node *n)
2492{
2493 expr_ty tmp;
2494 REQ(n, star_expr);
2495
2496 tmp = ast_for_expr(c, CHILD(n, 1));
2497 if (!tmp)
2498 return NULL;
2499
2500 /* The Load context is changed later. */
2501 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2502}
2503
2504
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505/* Do not name a variable 'expr'! Will cause a compile error.
2506*/
2507
2508static expr_ty
2509ast_for_expr(struct compiling *c, const node *n)
2510{
2511 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002512 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002513 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 and_test: not_test ('and' not_test)*
2516 not_test: 'not' not_test | comparison
2517 comparison: expr (comp_op expr)*
2518 expr: xor_expr ('|' xor_expr)*
2519 xor_expr: and_expr ('^' and_expr)*
2520 and_expr: shift_expr ('&' shift_expr)*
2521 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2522 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002523 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002525 power: atom_expr ['**' factor]
2526 atom_expr: [AWAIT] atom trailer*
2527 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 */
2529
2530 asdl_seq *seq;
2531 int i;
2532
2533 loop:
2534 switch (TYPE(n)) {
2535 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002536 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002537 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002538 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002540 else if (NCH(n) > 1)
2541 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002542 /* Fallthrough */
2543 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 case and_test:
2545 if (NCH(n) == 1) {
2546 n = CHILD(n, 0);
2547 goto loop;
2548 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002549 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 if (!seq)
2551 return NULL;
2552 for (i = 0; i < NCH(n); i += 2) {
2553 expr_ty e = ast_for_expr(c, CHILD(n, i));
2554 if (!e)
2555 return NULL;
2556 asdl_seq_SET(seq, i / 2, e);
2557 }
2558 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002559 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2560 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002561 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002562 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 case not_test:
2564 if (NCH(n) == 1) {
2565 n = CHILD(n, 0);
2566 goto loop;
2567 }
2568 else {
2569 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2570 if (!expression)
2571 return NULL;
2572
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002573 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2574 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 }
2576 case comparison:
2577 if (NCH(n) == 1) {
2578 n = CHILD(n, 0);
2579 goto loop;
2580 }
2581 else {
2582 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002583 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002584 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002585 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 if (!ops)
2587 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002588 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 return NULL;
2591 }
2592 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002593 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002595 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002596 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002598 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599
2600 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002601 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002603 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002605 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 asdl_seq_SET(cmps, i / 2, expression);
2607 }
2608 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002609 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002611 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002613 return Compare(expression, ops, cmps, LINENO(n),
2614 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 }
2616 break;
2617
Guido van Rossum0368b722007-05-11 16:50:42 +00002618 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 /* The next five cases all handle BinOps. The main body of code
2621 is the same in each case, but the switch turned inside out to
2622 reuse the code for each type of operator.
2623 */
2624 case expr:
2625 case xor_expr:
2626 case and_expr:
2627 case shift_expr:
2628 case arith_expr:
2629 case term:
2630 if (NCH(n) == 1) {
2631 n = CHILD(n, 0);
2632 goto loop;
2633 }
2634 return ast_for_binop(c, n);
2635 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002636 node *an = NULL;
2637 node *en = NULL;
2638 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002639 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002640 if (NCH(n) > 1)
2641 an = CHILD(n, 1); /* yield_arg */
2642 if (an) {
2643 en = CHILD(an, NCH(an) - 1);
2644 if (NCH(an) == 2) {
2645 is_from = 1;
2646 exp = ast_for_expr(c, en);
2647 }
2648 else
2649 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002650 if (!exp)
2651 return NULL;
2652 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002653 if (is_from)
2654 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2655 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002656 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002657 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 if (NCH(n) == 1) {
2659 n = CHILD(n, 0);
2660 goto loop;
2661 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002662 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002663 case power:
2664 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002666 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 return NULL;
2668 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002669 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 return NULL;
2671}
2672
2673static expr_ty
2674ast_for_call(struct compiling *c, const node *n, expr_ty func)
2675{
2676 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002677 arglist: argument (',' argument)* [',']
2678 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 */
2680
2681 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002682 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002683 asdl_seq *args;
2684 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685
2686 REQ(n, arglist);
2687
2688 nargs = 0;
2689 nkeywords = 0;
2690 ngens = 0;
2691 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002692 node *ch = CHILD(n, i);
2693 if (TYPE(ch) == argument) {
2694 if (NCH(ch) == 1)
2695 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002696 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002697 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002698 else if (TYPE(CHILD(ch, 0)) == STAR)
2699 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002701 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002702 nkeywords++;
2703 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 }
2705 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002706 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002707 "if not sole argument");
2708 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 }
2710
2711 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002712 ast_error(c, n, "more than 255 arguments");
2713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 }
2715
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002716 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002718 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002719 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002721 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002722
2723 nargs = 0; /* positional arguments + iterable argument unpackings */
2724 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2725 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 node *ch = CHILD(n, i);
2728 if (TYPE(ch) == argument) {
2729 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002731 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002732 /* a positional argument */
2733 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002734 if (ndoublestars) {
2735 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002736 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002737 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002738 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002739 else {
2740 ast_error(c, chch,
2741 "positional argument follows "
2742 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002744 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002745 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002746 e = ast_for_expr(c, chch);
2747 if (!e)
2748 return NULL;
2749 asdl_seq_SET(args, nargs++, e);
2750 }
2751 else if (TYPE(chch) == STAR) {
2752 /* an iterable argument unpacking */
2753 expr_ty starred;
2754 if (ndoublestars) {
2755 ast_error(c, chch,
2756 "iterable argument unpacking follows "
2757 "keyword argument unpacking");
2758 return NULL;
2759 }
2760 e = ast_for_expr(c, CHILD(ch, 1));
2761 if (!e)
2762 return NULL;
2763 starred = Starred(e, Load, LINENO(chch),
2764 chch->n_col_offset,
2765 c->c_arena);
2766 if (!starred)
2767 return NULL;
2768 asdl_seq_SET(args, nargs++, starred);
2769
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 }
2771 else if (TYPE(chch) == DOUBLESTAR) {
2772 /* a keyword argument unpacking */
2773 keyword_ty kw;
2774 i++;
2775 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002777 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002778 kw = keyword(NULL, e, c->c_arena);
2779 asdl_seq_SET(keywords, nkeywords++, kw);
2780 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002782 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002783 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002784 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002786 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002787 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002789 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002790 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002791 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002792 identifier key, tmp;
2793 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002795 /* chch is test, but must be an identifier? */
2796 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 /* f(lambda x: x[0] = 3) ends up getting parsed with
2800 * LHS test = lambda x: x[0], and RHS test = 3.
2801 * SF bug 132313 points out that complaining about a keyword
2802 * then is very confusing.
2803 */
2804 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002805 ast_error(c, chch,
2806 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002807 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002808 }
2809 else if (e->kind != Name_kind) {
2810 ast_error(c, chch,
2811 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002812 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002813 }
2814 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002815 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002818 for (k = 0; k < nkeywords; k++) {
2819 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002820 if (tmp && !PyUnicode_Compare(tmp, key)) {
2821 ast_error(c, chch,
2822 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002823 return NULL;
2824 }
2825 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002826 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002828 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002829 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002831 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002832 asdl_seq_SET(keywords, nkeywords++, kw);
2833 }
2834 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 }
2836
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002837 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838}
2839
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002841ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002843 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002844 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002846 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002847 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002848 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002849 }
2850 else {
2851 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002852 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002853 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 else {
2857 asdl_seq *tmp = seq_for_testlist(c, n);
2858 if (!tmp)
2859 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002860 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002862}
2863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864static stmt_ty
2865ast_for_expr_stmt(struct compiling *c, const node *n)
2866{
2867 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002870 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002871 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002872 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 test: ... here starts the operator precendence dance
2874 */
2875
2876 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002877 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 if (!e)
2879 return NULL;
2880
Thomas Wouters89f507f2006-12-13 04:49:30 +00002881 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 }
2883 else if (TYPE(CHILD(n, 1)) == augassign) {
2884 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002885 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887
Thomas Wouters89f507f2006-12-13 04:49:30 +00002888 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 if (!expr1)
2890 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002891 if(!set_context(c, expr1, Store, ch))
2892 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002893 /* set_context checks that most expressions are not the left side.
2894 Augmented assignments can only have a name, a subscript, or an
2895 attribute on the left, though, so we have to explicitly check for
2896 those. */
2897 switch (expr1->kind) {
2898 case Name_kind:
2899 case Attribute_kind:
2900 case Subscript_kind:
2901 break;
2902 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002903 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002904 return NULL;
2905 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906
Thomas Wouters89f507f2006-12-13 04:49:30 +00002907 ch = CHILD(n, 2);
2908 if (TYPE(ch) == testlist)
2909 expr2 = ast_for_testlist(c, ch);
2910 else
2911 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002912 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 return NULL;
2914
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002915 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002916 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 return NULL;
2918
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 }
2921 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002922 int i;
2923 asdl_seq *targets;
2924 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 expr_ty expression;
2926
Thomas Wouters89f507f2006-12-13 04:49:30 +00002927 /* a normal assignment */
2928 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002929 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002930 if (!targets)
2931 return NULL;
2932 for (i = 0; i < NCH(n) - 2; i += 2) {
2933 expr_ty e;
2934 node *ch = CHILD(n, i);
2935 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002936 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002937 return NULL;
2938 }
2939 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002943 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002944 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002945 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946
Thomas Wouters89f507f2006-12-13 04:49:30 +00002947 asdl_seq_SET(targets, i / 2, e);
2948 }
2949 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002950 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002951 expression = ast_for_testlist(c, value);
2952 else
2953 expression = ast_for_expr(c, value);
2954 if (!expression)
2955 return NULL;
2956 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958}
2959
Benjamin Peterson78565b22009-06-28 19:19:51 +00002960
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002962ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963{
2964 asdl_seq *seq;
2965 int i;
2966 expr_ty e;
2967
2968 REQ(n, exprlist);
2969
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002970 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002974 e = ast_for_expr(c, CHILD(n, i));
2975 if (!e)
2976 return NULL;
2977 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002978 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002979 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 }
2981 return seq;
2982}
2983
2984static stmt_ty
2985ast_for_del_stmt(struct compiling *c, const node *n)
2986{
2987 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 /* del_stmt: 'del' exprlist */
2990 REQ(n, del_stmt);
2991
2992 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2993 if (!expr_list)
2994 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002995 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996}
2997
2998static stmt_ty
2999ast_for_flow_stmt(struct compiling *c, const node *n)
3000{
3001 /*
3002 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3003 | yield_stmt
3004 break_stmt: 'break'
3005 continue_stmt: 'continue'
3006 return_stmt: 'return' [testlist]
3007 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003008 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003009 raise_stmt: 'raise' [test [',' test [',' test]]]
3010 */
3011 node *ch;
3012
3013 REQ(n, flow_stmt);
3014 ch = CHILD(n, 0);
3015 switch (TYPE(ch)) {
3016 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003017 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003019 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003021 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3022 if (!exp)
3023 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003024 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 }
3026 case return_stmt:
3027 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003028 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003030 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 if (!expression)
3032 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003033 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034 }
3035 case raise_stmt:
3036 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003037 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3038 else if (NCH(ch) >= 2) {
3039 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3041 if (!expression)
3042 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003043 if (NCH(ch) == 4) {
3044 cause = ast_for_expr(c, CHILD(ch, 3));
3045 if (!cause)
3046 return NULL;
3047 }
3048 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 }
3050 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003051 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 "unexpected flow_stmt: %d", TYPE(ch));
3053 return NULL;
3054 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003055
3056 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3057 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058}
3059
3060static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003061alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062{
3063 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003064 import_as_name: NAME ['as' NAME]
3065 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066 dotted_name: NAME ('.' NAME)*
3067 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003068 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 loop:
3071 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003072 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003073 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003074 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003075 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003076 if (!name)
3077 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003078 if (NCH(n) == 3) {
3079 node *str_node = CHILD(n, 2);
3080 str = NEW_IDENTIFIER(str_node);
3081 if (!str)
3082 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003083 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003084 return NULL;
3085 }
3086 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003087 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003088 return NULL;
3089 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003090 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003091 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092 case dotted_as_name:
3093 if (NCH(n) == 1) {
3094 n = CHILD(n, 0);
3095 goto loop;
3096 }
3097 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003098 node *asname_node = CHILD(n, 2);
3099 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003100 if (!a)
3101 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003103 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003104 if (!a->asname)
3105 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003106 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003107 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 return a;
3109 }
3110 break;
3111 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003112 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003113 node *name_node = CHILD(n, 0);
3114 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003115 if (!name)
3116 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003117 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003118 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003119 return alias(name, NULL, c->c_arena);
3120 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 else {
3122 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003123 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003124 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127
3128 len = 0;
3129 for (i = 0; i < NCH(n); i += 2)
3130 /* length of string plus one for the dot */
3131 len += strlen(STR(CHILD(n, i))) + 1;
3132 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003133 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 if (!str)
3135 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003136 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 if (!s)
3138 return NULL;
3139 for (i = 0; i < NCH(n); i += 2) {
3140 char *sch = STR(CHILD(n, i));
3141 strcpy(s, STR(CHILD(n, i)));
3142 s += strlen(sch);
3143 *s++ = '.';
3144 }
3145 --s;
3146 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3148 PyBytes_GET_SIZE(str),
3149 NULL);
3150 Py_DECREF(str);
3151 if (!uni)
3152 return NULL;
3153 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003154 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003155 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3156 Py_DECREF(str);
3157 return NULL;
3158 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003159 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 }
3161 break;
3162 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003163 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003164 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3165 Py_DECREF(str);
3166 return NULL;
3167 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003168 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003170 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 "unexpected import name: %d", TYPE(n));
3172 return NULL;
3173 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003174
3175 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176 return NULL;
3177}
3178
3179static stmt_ty
3180ast_for_import_stmt(struct compiling *c, const node *n)
3181{
3182 /*
3183 import_stmt: import_name | import_from
3184 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003185 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3186 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003188 int lineno;
3189 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 int i;
3191 asdl_seq *aliases;
3192
3193 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003194 lineno = LINENO(n);
3195 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003197 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003199 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003200 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003201 if (!aliases)
3202 return NULL;
3203 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003204 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003205 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003207 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003209 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003211 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003213 int idx, ndots = 0;
3214 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003215 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003217 /* Count the number of dots (for relative imports) and check for the
3218 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003219 for (idx = 1; idx < NCH(n); idx++) {
3220 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003221 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3222 if (!mod)
3223 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003224 idx++;
3225 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003226 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003228 ndots += 3;
3229 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003230 } else if (TYPE(CHILD(n, idx)) != DOT) {
3231 break;
3232 }
3233 ndots++;
3234 }
3235 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003236 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003237 case STAR:
3238 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003239 n = CHILD(n, idx);
3240 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003241 break;
3242 case LPAR:
3243 /* from ... import (x, y, z) */
3244 n = CHILD(n, idx + 1);
3245 n_children = NCH(n);
3246 break;
3247 case import_as_names:
3248 /* from ... import x, y, z */
3249 n = CHILD(n, idx);
3250 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003251 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003252 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253 " surrounding parentheses");
3254 return NULL;
3255 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003256 break;
3257 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003258 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003259 return NULL;
3260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003262 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003263 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265
3266 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003267 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003268 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003269 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003271 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003273 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003274 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003275 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003276 if (!import_alias)
3277 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003278 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003279 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003281 if (mod != NULL)
3282 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003283 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003284 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 }
Neal Norwitz79792652005-11-14 04:25:03 +00003286 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 "unknown import statement: starts with command '%s'",
3288 STR(CHILD(n, 0)));
3289 return NULL;
3290}
3291
3292static stmt_ty
3293ast_for_global_stmt(struct compiling *c, const node *n)
3294{
3295 /* global_stmt: 'global' NAME (',' NAME)* */
3296 identifier name;
3297 asdl_seq *s;
3298 int i;
3299
3300 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003301 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003305 name = NEW_IDENTIFIER(CHILD(n, i));
3306 if (!name)
3307 return NULL;
3308 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003310 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311}
3312
3313static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003314ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3315{
3316 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3317 identifier name;
3318 asdl_seq *s;
3319 int i;
3320
3321 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003322 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003323 if (!s)
3324 return NULL;
3325 for (i = 1; i < NCH(n); i += 2) {
3326 name = NEW_IDENTIFIER(CHILD(n, i));
3327 if (!name)
3328 return NULL;
3329 asdl_seq_SET(s, i / 2, name);
3330 }
3331 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3332}
3333
3334static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003335ast_for_assert_stmt(struct compiling *c, const node *n)
3336{
3337 /* assert_stmt: 'assert' test [',' test] */
3338 REQ(n, assert_stmt);
3339 if (NCH(n) == 2) {
3340 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3341 if (!expression)
3342 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003343 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344 }
3345 else if (NCH(n) == 4) {
3346 expr_ty expr1, expr2;
3347
3348 expr1 = ast_for_expr(c, CHILD(n, 1));
3349 if (!expr1)
3350 return NULL;
3351 expr2 = ast_for_expr(c, CHILD(n, 3));
3352 if (!expr2)
3353 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 }
Neal Norwitz79792652005-11-14 04:25:03 +00003357 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358 "improper number of parts to 'assert' statement: %d",
3359 NCH(n));
3360 return NULL;
3361}
3362
3363static asdl_seq *
3364ast_for_suite(struct compiling *c, const node *n)
3365{
3366 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003367 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 stmt_ty s;
3369 int i, total, num, end, pos = 0;
3370 node *ch;
3371
3372 REQ(n, suite);
3373
3374 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003375 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003377 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003379 n = CHILD(n, 0);
3380 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003382 */
3383 end = NCH(n) - 1;
3384 if (TYPE(CHILD(n, end - 1)) == SEMI)
3385 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003387 for (i = 0; i < end; i += 2) {
3388 ch = CHILD(n, i);
3389 s = ast_for_stmt(c, ch);
3390 if (!s)
3391 return NULL;
3392 asdl_seq_SET(seq, pos++, s);
3393 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003394 }
3395 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003396 for (i = 2; i < (NCH(n) - 1); i++) {
3397 ch = CHILD(n, i);
3398 REQ(ch, stmt);
3399 num = num_stmts(ch);
3400 if (num == 1) {
3401 /* small_stmt or compound_stmt with only one child */
3402 s = ast_for_stmt(c, ch);
3403 if (!s)
3404 return NULL;
3405 asdl_seq_SET(seq, pos++, s);
3406 }
3407 else {
3408 int j;
3409 ch = CHILD(ch, 0);
3410 REQ(ch, simple_stmt);
3411 for (j = 0; j < NCH(ch); j += 2) {
3412 /* statement terminates with a semi-colon ';' */
3413 if (NCH(CHILD(ch, j)) == 0) {
3414 assert((j + 1) == NCH(ch));
3415 break;
3416 }
3417 s = ast_for_stmt(c, CHILD(ch, j));
3418 if (!s)
3419 return NULL;
3420 asdl_seq_SET(seq, pos++, s);
3421 }
3422 }
3423 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003424 }
3425 assert(pos == seq->size);
3426 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427}
3428
3429static stmt_ty
3430ast_for_if_stmt(struct compiling *c, const node *n)
3431{
3432 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3433 ['else' ':' suite]
3434 */
3435 char *s;
3436
3437 REQ(n, if_stmt);
3438
3439 if (NCH(n) == 4) {
3440 expr_ty expression;
3441 asdl_seq *suite_seq;
3442
3443 expression = ast_for_expr(c, CHILD(n, 1));
3444 if (!expression)
3445 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003447 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449
Guido van Rossumd8faa362007-04-27 19:54:29 +00003450 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3451 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003453
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454 s = STR(CHILD(n, 4));
3455 /* s[2], the third character in the string, will be
3456 's' for el_s_e, or
3457 'i' for el_i_f
3458 */
3459 if (s[2] == 's') {
3460 expr_ty expression;
3461 asdl_seq *seq1, *seq2;
3462
3463 expression = ast_for_expr(c, CHILD(n, 1));
3464 if (!expression)
3465 return NULL;
3466 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003467 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468 return NULL;
3469 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003470 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 return NULL;
3472
Guido van Rossumd8faa362007-04-27 19:54:29 +00003473 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3474 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 }
3476 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003477 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003478 expr_ty expression;
3479 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 asdl_seq *orelse = NULL;
3481 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482 /* must reference the child n_elif+1 since 'else' token is third,
3483 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003484 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3485 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3486 has_else = 1;
3487 n_elif -= 3;
3488 }
3489 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490
Thomas Wouters89f507f2006-12-13 04:49:30 +00003491 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003492 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003494 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003495 if (!orelse)
3496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003498 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003500 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3501 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003503 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3504 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 asdl_seq_SET(orelse, 0,
3508 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003509 LINENO(CHILD(n, NCH(n) - 6)),
3510 CHILD(n, NCH(n) - 6)->n_col_offset,
3511 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003512 /* the just-created orelse handled the last elif */
3513 n_elif--;
3514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515
Thomas Wouters89f507f2006-12-13 04:49:30 +00003516 for (i = 0; i < n_elif; i++) {
3517 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003518 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003519 if (!newobj)
3520 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003522 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003525 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527
Thomas Wouters89f507f2006-12-13 04:49:30 +00003528 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003530 LINENO(CHILD(n, off)),
3531 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003532 orelse = newobj;
3533 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003534 expression = ast_for_expr(c, CHILD(n, 1));
3535 if (!expression)
3536 return NULL;
3537 suite_seq = ast_for_suite(c, CHILD(n, 3));
3538 if (!suite_seq)
3539 return NULL;
3540 return If(expression, suite_seq, orelse,
3541 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003543
3544 PyErr_Format(PyExc_SystemError,
3545 "unexpected token in 'if' statement: %s", s);
3546 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547}
3548
3549static stmt_ty
3550ast_for_while_stmt(struct compiling *c, const node *n)
3551{
3552 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3553 REQ(n, while_stmt);
3554
3555 if (NCH(n) == 4) {
3556 expr_ty expression;
3557 asdl_seq *suite_seq;
3558
3559 expression = ast_for_expr(c, CHILD(n, 1));
3560 if (!expression)
3561 return NULL;
3562 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003563 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003565 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 }
3567 else if (NCH(n) == 7) {
3568 expr_ty expression;
3569 asdl_seq *seq1, *seq2;
3570
3571 expression = ast_for_expr(c, CHILD(n, 1));
3572 if (!expression)
3573 return NULL;
3574 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003575 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 return NULL;
3577 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003578 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 return NULL;
3580
Thomas Wouters89f507f2006-12-13 04:49:30 +00003581 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003583
3584 PyErr_Format(PyExc_SystemError,
3585 "wrong number of tokens for 'while' statement: %d",
3586 NCH(n));
3587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588}
3589
3590static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003591ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003593 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003595 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003596 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3598 REQ(n, for_stmt);
3599
3600 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003601 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 if (!seq)
3603 return NULL;
3604 }
3605
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003606 node_target = CHILD(n, 1);
3607 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003608 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003610 /* Check the # of children rather than the length of _target, since
3611 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003612 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003613 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003614 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003616 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003618 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003619 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 return NULL;
3621 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003622 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 return NULL;
3624
Yury Selivanov75445082015-05-11 22:57:16 -04003625 if (is_async)
3626 return AsyncFor(target, expression, suite_seq, seq,
3627 LINENO(n), n->n_col_offset,
3628 c->c_arena);
3629 else
3630 return For(target, expression, suite_seq, seq,
3631 LINENO(n), n->n_col_offset,
3632 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633}
3634
3635static excepthandler_ty
3636ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3637{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003638 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639 REQ(exc, except_clause);
3640 REQ(body, suite);
3641
3642 if (NCH(exc) == 1) {
3643 asdl_seq *suite_seq = ast_for_suite(c, body);
3644 if (!suite_seq)
3645 return NULL;
3646
Neal Norwitzad74aa82008-03-31 05:14:30 +00003647 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003648 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 }
3650 else if (NCH(exc) == 2) {
3651 expr_ty expression;
3652 asdl_seq *suite_seq;
3653
3654 expression = ast_for_expr(c, CHILD(exc, 1));
3655 if (!expression)
3656 return NULL;
3657 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003658 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 return NULL;
3660
Neal Norwitzad74aa82008-03-31 05:14:30 +00003661 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003662 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 }
3664 else if (NCH(exc) == 4) {
3665 asdl_seq *suite_seq;
3666 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003667 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003668 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003670 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003671 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003673 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674 return NULL;
3675 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003676 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677 return NULL;
3678
Neal Norwitzad74aa82008-03-31 05:14:30 +00003679 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003680 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003682
3683 PyErr_Format(PyExc_SystemError,
3684 "wrong number of children for 'except' clause: %d",
3685 NCH(exc));
3686 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687}
3688
3689static stmt_ty
3690ast_for_try_stmt(struct compiling *c, const node *n)
3691{
Neal Norwitzf599f422005-12-17 21:33:47 +00003692 const int nch = NCH(n);
3693 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003694 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003695
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 REQ(n, try_stmt);
3697
Neal Norwitzf599f422005-12-17 21:33:47 +00003698 body = ast_for_suite(c, CHILD(n, 2));
3699 if (body == NULL)
3700 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701
Neal Norwitzf599f422005-12-17 21:33:47 +00003702 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3703 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3704 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3705 /* we can assume it's an "else",
3706 because nch >= 9 for try-else-finally and
3707 it would otherwise have a type of except_clause */
3708 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3709 if (orelse == NULL)
3710 return NULL;
3711 n_except--;
3712 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713
Neal Norwitzf599f422005-12-17 21:33:47 +00003714 finally = ast_for_suite(c, CHILD(n, nch - 1));
3715 if (finally == NULL)
3716 return NULL;
3717 n_except--;
3718 }
3719 else {
3720 /* we can assume it's an "else",
3721 otherwise it would have a type of except_clause */
3722 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3723 if (orelse == NULL)
3724 return NULL;
3725 n_except--;
3726 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003728 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003729 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730 return NULL;
3731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732
Neal Norwitzf599f422005-12-17 21:33:47 +00003733 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003734 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003735 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003736 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003737 if (handlers == NULL)
3738 return NULL;
3739
3740 for (i = 0; i < n_except; i++) {
3741 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3742 CHILD(n, 5 + i * 3));
3743 if (!e)
3744 return NULL;
3745 asdl_seq_SET(handlers, i, e);
3746 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003747 }
3748
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003749 assert(finally != NULL || asdl_seq_LEN(handlers));
3750 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751}
3752
Georg Brandl0c315622009-05-25 21:10:36 +00003753/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003754static withitem_ty
3755ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003756{
3757 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003758
Georg Brandl0c315622009-05-25 21:10:36 +00003759 REQ(n, with_item);
3760 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003761 if (!context_expr)
3762 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003763 if (NCH(n) == 3) {
3764 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003765
3766 if (!optional_vars) {
3767 return NULL;
3768 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003769 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003770 return NULL;
3771 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003772 }
3773
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003774 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003775}
3776
Georg Brandl0c315622009-05-25 21:10:36 +00003777/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3778static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003779ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003780{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003781 int i, n_items;
3782 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003783
3784 REQ(n, with_stmt);
3785
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003786 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003787 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003788 if (!items)
3789 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003790 for (i = 1; i < NCH(n) - 2; i += 2) {
3791 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3792 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003793 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003794 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003795 }
3796
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003797 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3798 if (!body)
3799 return NULL;
3800
Yury Selivanov75445082015-05-11 22:57:16 -04003801 if (is_async)
3802 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3803 else
3804 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003805}
3806
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003807static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003808ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003810 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003811 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003812 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003813 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003814
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 REQ(n, classdef);
3816
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003817 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 s = ast_for_suite(c, CHILD(n, 3));
3819 if (!s)
3820 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003821 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 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003829
3830 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003831 s = ast_for_suite(c, CHILD(n,5));
3832 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003833 return NULL;
3834 classname = NEW_IDENTIFIER(CHILD(n, 1));
3835 if (!classname)
3836 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003837 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003838 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003839 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3840 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841 }
3842
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003843 /* class NAME '(' arglist ')' ':' suite */
3844 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003845 {
3846 PyObject *dummy_name;
3847 expr_ty dummy;
3848 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3849 if (!dummy_name)
3850 return NULL;
3851 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3852 call = ast_for_call(c, CHILD(n, 3), dummy);
3853 if (!call)
3854 return NULL;
3855 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003857 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003859 classname = NEW_IDENTIFIER(CHILD(n, 1));
3860 if (!classname)
3861 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003862 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003863 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003864
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003865 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003866 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867}
3868
3869static stmt_ty
3870ast_for_stmt(struct compiling *c, const node *n)
3871{
3872 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003873 assert(NCH(n) == 1);
3874 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 }
3876 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003877 assert(num_stmts(n) == 1);
3878 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 }
3880 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003881 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003882 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3883 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003884 */
3885 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886 case expr_stmt:
3887 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888 case del_stmt:
3889 return ast_for_del_stmt(c, n);
3890 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003891 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003892 case flow_stmt:
3893 return ast_for_flow_stmt(c, n);
3894 case import_stmt:
3895 return ast_for_import_stmt(c, n);
3896 case global_stmt:
3897 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003898 case nonlocal_stmt:
3899 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 case assert_stmt:
3901 return ast_for_assert_stmt(c, n);
3902 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003903 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3905 TYPE(n), NCH(n));
3906 return NULL;
3907 }
3908 }
3909 else {
3910 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003911 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003912 */
3913 node *ch = CHILD(n, 0);
3914 REQ(n, compound_stmt);
3915 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 case if_stmt:
3917 return ast_for_if_stmt(c, ch);
3918 case while_stmt:
3919 return ast_for_while_stmt(c, ch);
3920 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003921 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 case try_stmt:
3923 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003924 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003925 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003927 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003929 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 case decorated:
3931 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003932 case async_stmt:
3933 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003935 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3937 TYPE(n), NCH(n));
3938 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 }
3941}
3942
3943static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003944parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003946 const char *end;
3947 long x;
3948 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003949 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003950 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003952 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003953 errno = 0;
3954 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003955 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003956 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003957 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003958 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003959 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003960 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003961 }
3962 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003963 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003964 if (*end == '\0') {
3965 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003966 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003967 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003968 }
3969 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003970 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003971 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003972 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3973 if (compl.imag == -1.0 && PyErr_Occurred())
3974 return NULL;
3975 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003976 }
3977 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003978 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003979 dx = PyOS_string_to_double(s, NULL, NULL);
3980 if (dx == -1.0 && PyErr_Occurred())
3981 return NULL;
3982 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003983 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984}
3985
3986static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003987decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003989 const char *s, *t;
3990 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003991 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3992 while (s < end && (*s & 0x80)) s++;
3993 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003994 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995}
3996
3997static PyObject *
Eric V. Smith5567f892015-09-21 13:36:09 -04003998decode_unicode(struct compiling *c, const char *s, size_t len, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004000 PyObject *v, *u;
4001 char *buf;
4002 char *p;
4003 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004004
Guido van Rossumd8faa362007-04-27 19:54:29 +00004005 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004006 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004007 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00004008 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00004009 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00004010 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00004011 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4012 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4013 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004014 if (u == NULL)
4015 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00004016 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004017 end = s + len;
4018 while (s < end) {
4019 if (*s == '\\') {
4020 *p++ = *s++;
4021 if (*s & 0x80) {
4022 strcpy(p, "u005c");
4023 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004024 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004025 }
4026 if (*s & 0x80) { /* XXX inefficient */
4027 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004028 int kind;
4029 void *data;
4030 Py_ssize_t len, i;
4031 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004032 if (w == NULL) {
4033 Py_DECREF(u);
4034 return NULL;
4035 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004036 kind = PyUnicode_KIND(w);
4037 data = PyUnicode_DATA(w);
4038 len = PyUnicode_GET_LENGTH(w);
4039 for (i = 0; i < len; i++) {
4040 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4041 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00004042 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004043 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00004044 /* Should be impossible to overflow */
4045 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00004046 Py_DECREF(w);
4047 } else {
4048 *p++ = *s++;
4049 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004050 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004051 len = p - buf;
4052 s = buf;
4053 }
Eric V. Smith5567f892015-09-21 13:36:09 -04004054 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 Py_XDECREF(u);
4056 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057}
4058
Eric V. Smith235a6f02015-09-19 14:51:32 -04004059/* Compile this expression in to an expr_ty. We know that we can
4060 temporarily modify the character before the start of this string
4061 (it's '{'), and we know we can temporarily modify the character
4062 after this string (it is a '}'). Leverage this to create a
4063 sub-string with enough room for us to add parens around the
4064 expression. This is to allow strings with embedded newlines, for
4065 example. */
4066static expr_ty
Eric V. Smith1d44c412015-09-23 07:49:00 -04004067fstring_compile_expr(PyObject *str, Py_ssize_t expr_start,
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004068 Py_ssize_t expr_end, struct compiling *c, const node *n)
4069
Eric V. Smith235a6f02015-09-19 14:51:32 -04004070{
4071 PyCompilerFlags cf;
4072 mod_ty mod;
4073 char *utf_expr;
4074 Py_ssize_t i;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004075 Py_UCS4 end_ch = -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004076 int all_whitespace;
4077 PyObject *sub = NULL;
4078
4079 /* We only decref sub if we allocated it with a PyUnicode_Substring.
4080 decref_sub records that. */
4081 int decref_sub = 0;
4082
4083 assert(str);
4084
Eric V. Smith1d44c412015-09-23 07:49:00 -04004085 assert(expr_start >= 0 && expr_start < PyUnicode_GET_LENGTH(str));
4086 assert(expr_end >= 0 && expr_end < PyUnicode_GET_LENGTH(str));
4087 assert(expr_end >= expr_start);
4088
Martin Panterc2432f62015-10-07 11:15:15 +00004089 /* There has to be at least one character on each side of the
Eric V. Smith1d44c412015-09-23 07:49:00 -04004090 expression inside this str. This will have been caught before
4091 we're called. */
4092 assert(expr_start >= 1);
4093 assert(expr_end <= PyUnicode_GET_LENGTH(str)-1);
4094
Eric V. Smith235a6f02015-09-19 14:51:32 -04004095 /* If the substring is all whitespace, it's an error. We need to
4096 catch this here, and not when we call PyParser_ASTFromString,
4097 because turning the expression '' in to '()' would go from
4098 being invalid to valid. */
4099 /* Note that this code says an empty string is all
4100 whitespace. That's important. There's a test for it: f'{}'. */
4101 all_whitespace = 1;
4102 for (i = expr_start; i < expr_end; i++) {
4103 if (!Py_UNICODE_ISSPACE(PyUnicode_READ_CHAR(str, i))) {
4104 all_whitespace = 0;
4105 break;
4106 }
4107 }
4108 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004109 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004110 goto error;
4111 }
4112
4113 /* If the substring will be the entire source string, we can't use
4114 PyUnicode_Substring, since it will return another reference to
4115 our original string. Because we're modifying the string in
4116 place, that's a no-no. So, detect that case and just use our
4117 string directly. */
4118
4119 if (expr_start-1 == 0 && expr_end+1 == PyUnicode_GET_LENGTH(str)) {
Eric V. Smith1d44c412015-09-23 07:49:00 -04004120 /* If str is well formed, then the first and last chars must
4121 be '{' and '}', respectively. But, if there's a syntax
4122 error, for example f'{3!', then the last char won't be a
4123 closing brace. So, remember the last character we read in
4124 order for us to restore it. */
4125 end_ch = PyUnicode_ReadChar(str, expr_end-expr_start+1);
4126 assert(end_ch != (Py_UCS4)-1);
4127
4128 /* In all cases, however, start_ch must be '{'. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004129 assert(PyUnicode_ReadChar(str, 0) == '{');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004130
Eric V. Smith235a6f02015-09-19 14:51:32 -04004131 sub = str;
4132 } else {
4133 /* Create a substring object. It must be a new object, with
4134 refcount==1, so that we can modify it. */
4135 sub = PyUnicode_Substring(str, expr_start-1, expr_end+1);
4136 if (!sub)
4137 goto error;
4138 assert(sub != str); /* Make sure it's a new string. */
4139 decref_sub = 1; /* Remember to deallocate it on error. */
4140 }
4141
Eric V. Smith1d44c412015-09-23 07:49:00 -04004142 /* Put () around the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004143 if (PyUnicode_WriteChar(sub, 0, '(') < 0 ||
4144 PyUnicode_WriteChar(sub, expr_end-expr_start+1, ')') < 0)
4145 goto error;
4146
Eric V. Smith235a6f02015-09-19 14:51:32 -04004147 /* No need to free the memory returned here: it's managed by the
4148 string. */
4149 utf_expr = PyUnicode_AsUTF8(sub);
4150 if (!utf_expr)
4151 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004152
4153 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004154 mod = PyParser_ASTFromString(utf_expr, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004155 Py_eval_input, &cf, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004156 if (!mod)
4157 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004158
Eric V. Smith235a6f02015-09-19 14:51:32 -04004159 if (sub != str)
4160 /* Clear instead of decref in case we ever modify this code to change
4161 the error handling: this is safest because the XDECREF won't try
4162 and decref it when it's NULL. */
4163 /* No need to restore the chars in sub, since we know it's getting
4164 ready to get deleted (refcount must be 1, since we got a new string
4165 in PyUnicode_Substring). */
4166 Py_CLEAR(sub);
4167 else {
4168 assert(!decref_sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004169 assert(end_ch != (Py_UCS4)-1);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004170 /* Restore str, which we earlier modified directly. */
4171 if (PyUnicode_WriteChar(str, 0, '{') < 0 ||
Eric V. Smith1d44c412015-09-23 07:49:00 -04004172 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004173 goto error;
4174 }
4175 return mod->v.Expression.body;
4176
4177error:
4178 /* Only decref sub if it was the result of a call to SubString. */
4179 if (decref_sub)
4180 Py_XDECREF(sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004181
4182 if (end_ch != (Py_UCS4)-1) {
4183 /* We only get here if we modified str. Make sure that's the
4184 case: str will be equal to sub. */
4185 if (str == sub) {
4186 /* Don't check the error, because we've already set the
4187 error state (that's why we're in 'error', after
4188 all). */
4189 PyUnicode_WriteChar(str, 0, '{');
4190 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch);
4191 }
4192 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004193 return NULL;
4194}
4195
4196/* Return -1 on error.
4197
4198 Return 0 if we reached the end of the literal.
4199
4200 Return 1 if we haven't reached the end of the literal, but we want
4201 the caller to process the literal up to this point. Used for
4202 doubled braces.
4203*/
4204static int
4205fstring_find_literal(PyObject *str, Py_ssize_t *ofs, PyObject **literal,
4206 int recurse_lvl, struct compiling *c, const node *n)
4207{
4208 /* Get any literal string. It ends when we hit an un-doubled brace, or the
4209 end of the string. */
4210
4211 Py_ssize_t literal_start, literal_end;
4212 int result = 0;
4213
4214 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4215 void *data = PyUnicode_DATA(str);
4216
4217 assert(*literal == NULL);
4218
4219 literal_start = *ofs;
4220 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4221 Py_UCS4 ch = PyUnicode_READ(kind, data, *ofs);
4222 if (ch == '{' || ch == '}') {
4223 /* Check for doubled braces, but only at the top level. If
4224 we checked at every level, then f'{0:{3}}' would fail
4225 with the two closing braces. */
4226 if (recurse_lvl == 0) {
4227 if (*ofs + 1 < PyUnicode_GET_LENGTH(str) &&
4228 PyUnicode_READ(kind, data, *ofs + 1) == ch) {
4229 /* We're going to tell the caller that the literal ends
4230 here, but that they should continue scanning. But also
4231 skip over the second brace when we resume scanning. */
4232 literal_end = *ofs + 1;
4233 *ofs += 2;
4234 result = 1;
4235 goto done;
4236 }
4237
4238 /* Where a single '{' is the start of a new expression, a
4239 single '}' is not allowed. */
4240 if (ch == '}') {
4241 ast_error(c, n, "f-string: single '}' is not allowed");
4242 return -1;
4243 }
4244 }
4245
4246 /* We're either at a '{', which means we're starting another
4247 expression; or a '}', which means we're at the end of this
4248 f-string (for a nested format_spec). */
4249 break;
4250 }
4251 }
4252 literal_end = *ofs;
4253
4254 assert(*ofs == PyUnicode_GET_LENGTH(str) ||
4255 PyUnicode_READ(kind, data, *ofs) == '{' ||
4256 PyUnicode_READ(kind, data, *ofs) == '}');
4257done:
4258 if (literal_start != literal_end) {
4259 *literal = PyUnicode_Substring(str, literal_start, literal_end);
4260 if (!*literal)
4261 return -1;
4262 }
4263
4264 return result;
4265}
4266
4267/* Forward declaration because parsing is recursive. */
4268static expr_ty
4269fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4270 struct compiling *c, const node *n);
4271
4272/* Parse the f-string str, starting at ofs. We know *ofs starts an
4273 expression (so it must be a '{'). Returns the FormattedValue node,
4274 which includes the expression, conversion character, and
4275 format_spec expression.
4276
4277 Note that I don't do a perfect job here: I don't make sure that a
4278 closing brace doesn't match an opening paren, for example. It
4279 doesn't need to error on all invalid expressions, just correctly
4280 find the end of all valid ones. Any errors inside the expression
4281 will be caught when we parse it later. */
4282static int
4283fstring_find_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4284 expr_ty *expression, struct compiling *c, const node *n)
4285{
4286 /* Return -1 on error, else 0. */
4287
4288 Py_ssize_t expr_start;
4289 Py_ssize_t expr_end;
4290 expr_ty simple_expression;
4291 expr_ty format_spec = NULL; /* Optional format specifier. */
4292 Py_UCS4 conversion = -1; /* The conversion char. -1 if not specified. */
4293
4294 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4295 void *data = PyUnicode_DATA(str);
4296
4297 /* 0 if we're not in a string, else the quote char we're trying to
4298 match (single or double quote). */
4299 Py_UCS4 quote_char = 0;
4300
4301 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4302 int string_type = 0;
4303
4304 /* Keep track of nesting level for braces/parens/brackets in
4305 expressions. */
4306 Py_ssize_t nested_depth = 0;
4307
4308 /* Can only nest one level deep. */
4309 if (recurse_lvl >= 2) {
4310 ast_error(c, n, "f-string: expressions nested too deeply");
4311 return -1;
4312 }
4313
4314 /* The first char must be a left brace, or we wouldn't have gotten
4315 here. Skip over it. */
4316 assert(PyUnicode_READ(kind, data, *ofs) == '{');
4317 *ofs += 1;
4318
4319 expr_start = *ofs;
4320 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4321 Py_UCS4 ch;
4322
4323 /* Loop invariants. */
4324 assert(nested_depth >= 0);
4325 assert(*ofs >= expr_start);
4326 if (quote_char)
4327 assert(string_type == 1 || string_type == 3);
4328 else
4329 assert(string_type == 0);
4330
4331 ch = PyUnicode_READ(kind, data, *ofs);
4332 if (quote_char) {
4333 /* We're inside a string. See if we're at the end. */
4334 /* This code needs to implement the same non-error logic
4335 as tok_get from tokenizer.c, at the letter_quote
4336 label. To actually share that code would be a
4337 nightmare. But, it's unlikely to change and is small,
4338 so duplicate it here. Note we don't need to catch all
4339 of the errors, since they'll be caught when parsing the
4340 expression. We just need to match the non-error
4341 cases. Thus we can ignore \n in single-quoted strings,
4342 for example. Or non-terminated strings. */
4343 if (ch == quote_char) {
4344 /* Does this match the string_type (single or triple
4345 quoted)? */
4346 if (string_type == 3) {
4347 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4348 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4349 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4350 /* We're at the end of a triple quoted string. */
4351 *ofs += 2;
4352 string_type = 0;
4353 quote_char = 0;
4354 continue;
4355 }
4356 } else {
4357 /* We're at the end of a normal string. */
4358 quote_char = 0;
4359 string_type = 0;
4360 continue;
4361 }
4362 }
4363 /* We're inside a string, and not finished with the
4364 string. If this is a backslash, skip the next char (it
4365 might be an end quote that needs skipping). Otherwise,
4366 just consume this character normally. */
4367 if (ch == '\\' && *ofs+1 < PyUnicode_GET_LENGTH(str)) {
4368 /* Just skip the next char, whatever it is. */
4369 *ofs += 1;
4370 }
4371 } else if (ch == '\'' || ch == '"') {
4372 /* Is this a triple quoted string? */
4373 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4374 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4375 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4376 string_type = 3;
4377 *ofs += 2;
4378 } else {
4379 /* Start of a normal string. */
4380 string_type = 1;
4381 }
4382 /* Start looking for the end of the string. */
4383 quote_char = ch;
4384 } else if (ch == '[' || ch == '{' || ch == '(') {
4385 nested_depth++;
4386 } else if (nested_depth != 0 &&
4387 (ch == ']' || ch == '}' || ch == ')')) {
4388 nested_depth--;
4389 } else if (ch == '#') {
4390 /* Error: can't include a comment character, inside parens
4391 or not. */
4392 ast_error(c, n, "f-string cannot include '#'");
4393 return -1;
4394 } else if (nested_depth == 0 &&
4395 (ch == '!' || ch == ':' || ch == '}')) {
4396 /* First, test for the special case of "!=". Since '=' is
4397 not an allowed conversion character, nothing is lost in
4398 this test. */
4399 if (ch == '!' && *ofs+1 < PyUnicode_GET_LENGTH(str) &&
4400 PyUnicode_READ(kind, data, *ofs+1) == '=')
4401 /* This isn't a conversion character, just continue. */
4402 continue;
4403
4404 /* Normal way out of this loop. */
4405 break;
4406 } else {
4407 /* Just consume this char and loop around. */
4408 }
4409 }
4410 expr_end = *ofs;
4411 /* If we leave this loop in a string or with mismatched parens, we
4412 don't care. We'll get a syntax error when compiling the
4413 expression. But, we can produce a better error message, so
4414 let's just do that.*/
4415 if (quote_char) {
4416 ast_error(c, n, "f-string: unterminated string");
4417 return -1;
4418 }
4419 if (nested_depth) {
4420 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4421 return -1;
4422 }
4423
Eric V. Smith235a6f02015-09-19 14:51:32 -04004424 if (*ofs >= PyUnicode_GET_LENGTH(str))
4425 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004426
4427 /* Compile the expression as soon as possible, so we show errors
4428 related to the expression before errors related to the
4429 conversion or format_spec. */
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004430 simple_expression = fstring_compile_expr(str, expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004431 if (!simple_expression)
4432 return -1;
4433
4434 /* Check for a conversion char, if present. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004435 if (PyUnicode_READ(kind, data, *ofs) == '!') {
4436 *ofs += 1;
4437 if (*ofs >= PyUnicode_GET_LENGTH(str))
4438 goto unexpected_end_of_string;
4439
4440 conversion = PyUnicode_READ(kind, data, *ofs);
4441 *ofs += 1;
4442
4443 /* Validate the conversion. */
4444 if (!(conversion == 's' || conversion == 'r'
4445 || conversion == 'a')) {
4446 ast_error(c, n, "f-string: invalid conversion character: "
4447 "expected 's', 'r', or 'a'");
4448 return -1;
4449 }
4450 }
4451
4452 /* Check for the format spec, if present. */
4453 if (*ofs >= PyUnicode_GET_LENGTH(str))
4454 goto unexpected_end_of_string;
4455 if (PyUnicode_READ(kind, data, *ofs) == ':') {
4456 *ofs += 1;
4457 if (*ofs >= PyUnicode_GET_LENGTH(str))
4458 goto unexpected_end_of_string;
4459
4460 /* Parse the format spec. */
4461 format_spec = fstring_parse(str, ofs, recurse_lvl+1, c, n);
4462 if (!format_spec)
4463 return -1;
4464 }
4465
4466 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4467 PyUnicode_READ(kind, data, *ofs) != '}')
4468 goto unexpected_end_of_string;
4469
4470 /* We're at a right brace. Consume it. */
4471 assert(*ofs < PyUnicode_GET_LENGTH(str));
4472 assert(PyUnicode_READ(kind, data, *ofs) == '}');
4473 *ofs += 1;
4474
Eric V. Smith235a6f02015-09-19 14:51:32 -04004475 /* And now create the FormattedValue node that represents this entire
4476 expression with the conversion and format spec. */
4477 *expression = FormattedValue(simple_expression, (int)conversion,
4478 format_spec, LINENO(n), n->n_col_offset,
4479 c->c_arena);
4480 if (!*expression)
4481 return -1;
4482
4483 return 0;
4484
4485unexpected_end_of_string:
4486 ast_error(c, n, "f-string: expecting '}'");
4487 return -1;
4488}
4489
4490/* Return -1 on error.
4491
4492 Return 0 if we have a literal (possible zero length) and an
4493 expression (zero length if at the end of the string.
4494
4495 Return 1 if we have a literal, but no expression, and we want the
4496 caller to call us again. This is used to deal with doubled
4497 braces.
4498
4499 When called multiple times on the string 'a{{b{0}c', this function
4500 will return:
4501
4502 1. the literal 'a{' with no expression, and a return value
4503 of 1. Despite the fact that there's no expression, the return
4504 value of 1 means we're not finished yet.
4505
4506 2. the literal 'b' and the expression '0', with a return value of
4507 0. The fact that there's an expression means we're not finished.
4508
4509 3. literal 'c' with no expression and a return value of 0. The
4510 combination of the return value of 0 with no expression means
4511 we're finished.
4512*/
4513static int
4514fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4515 PyObject **literal, expr_ty *expression,
4516 struct compiling *c, const node *n)
4517{
4518 int result;
4519
4520 assert(*literal == NULL && *expression == NULL);
4521
4522 /* Get any literal string. */
4523 result = fstring_find_literal(str, ofs, literal, recurse_lvl, c, n);
4524 if (result < 0)
4525 goto error;
4526
4527 assert(result == 0 || result == 1);
4528
4529 if (result == 1)
4530 /* We have a literal, but don't look at the expression. */
4531 return 1;
4532
4533 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4534
4535 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4536 PyUnicode_READ_CHAR(str, *ofs) == '}')
4537 /* We're at the end of the string or the end of a nested
4538 f-string: no expression. The top-level error case where we
4539 expect to be at the end of the string but we're at a '}' is
4540 handled later. */
4541 return 0;
4542
4543 /* We must now be the start of an expression, on a '{'. */
4544 assert(*ofs < PyUnicode_GET_LENGTH(str) &&
4545 PyUnicode_READ_CHAR(str, *ofs) == '{');
4546
4547 if (fstring_find_expr(str, ofs, recurse_lvl, expression, c, n) < 0)
4548 goto error;
4549
4550 return 0;
4551
4552error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004553 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004554 return -1;
4555}
4556
4557#define EXPRLIST_N_CACHED 64
4558
4559typedef struct {
4560 /* Incrementally build an array of expr_ty, so be used in an
4561 asdl_seq. Cache some small but reasonably sized number of
4562 expr_ty's, and then after that start dynamically allocating,
4563 doubling the number allocated each time. Note that the f-string
4564 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4565 Str for the literal 'a'. So you add expr_ty's about twice as
4566 fast as you add exressions in an f-string. */
4567
4568 Py_ssize_t allocated; /* Number we've allocated. */
4569 Py_ssize_t size; /* Number we've used. */
4570 expr_ty *p; /* Pointer to the memory we're actually
4571 using. Will point to 'data' until we
4572 start dynamically allocating. */
4573 expr_ty data[EXPRLIST_N_CACHED];
4574} ExprList;
4575
4576#ifdef NDEBUG
4577#define ExprList_check_invariants(l)
4578#else
4579static void
4580ExprList_check_invariants(ExprList *l)
4581{
4582 /* Check our invariants. Make sure this object is "live", and
4583 hasn't been deallocated. */
4584 assert(l->size >= 0);
4585 assert(l->p != NULL);
4586 if (l->size <= EXPRLIST_N_CACHED)
4587 assert(l->data == l->p);
4588}
4589#endif
4590
4591static void
4592ExprList_Init(ExprList *l)
4593{
4594 l->allocated = EXPRLIST_N_CACHED;
4595 l->size = 0;
4596
4597 /* Until we start allocating dynamically, p points to data. */
4598 l->p = l->data;
4599
4600 ExprList_check_invariants(l);
4601}
4602
4603static int
4604ExprList_Append(ExprList *l, expr_ty exp)
4605{
4606 ExprList_check_invariants(l);
4607 if (l->size >= l->allocated) {
4608 /* We need to alloc (or realloc) the memory. */
4609 Py_ssize_t new_size = l->allocated * 2;
4610
4611 /* See if we've ever allocated anything dynamically. */
4612 if (l->p == l->data) {
4613 Py_ssize_t i;
4614 /* We're still using the cached data. Switch to
4615 alloc-ing. */
4616 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4617 if (!l->p)
4618 return -1;
4619 /* Copy the cached data into the new buffer. */
4620 for (i = 0; i < l->size; i++)
4621 l->p[i] = l->data[i];
4622 } else {
4623 /* Just realloc. */
4624 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4625 if (!tmp) {
4626 PyMem_RawFree(l->p);
4627 l->p = NULL;
4628 return -1;
4629 }
4630 l->p = tmp;
4631 }
4632
4633 l->allocated = new_size;
4634 assert(l->allocated == 2 * l->size);
4635 }
4636
4637 l->p[l->size++] = exp;
4638
4639 ExprList_check_invariants(l);
4640 return 0;
4641}
4642
4643static void
4644ExprList_Dealloc(ExprList *l)
4645{
4646 ExprList_check_invariants(l);
4647
4648 /* If there's been an error, or we've never dynamically allocated,
4649 do nothing. */
4650 if (!l->p || l->p == l->data) {
4651 /* Do nothing. */
4652 } else {
4653 /* We have dynamically allocated. Free the memory. */
4654 PyMem_RawFree(l->p);
4655 }
4656 l->p = NULL;
4657 l->size = -1;
4658}
4659
4660static asdl_seq *
4661ExprList_Finish(ExprList *l, PyArena *arena)
4662{
4663 asdl_seq *seq;
4664
4665 ExprList_check_invariants(l);
4666
4667 /* Allocate the asdl_seq and copy the expressions in to it. */
4668 seq = _Py_asdl_seq_new(l->size, arena);
4669 if (seq) {
4670 Py_ssize_t i;
4671 for (i = 0; i < l->size; i++)
4672 asdl_seq_SET(seq, i, l->p[i]);
4673 }
4674 ExprList_Dealloc(l);
4675 return seq;
4676}
4677
4678/* The FstringParser is designed to add a mix of strings and
4679 f-strings, and concat them together as needed. Ultimately, it
4680 generates an expr_ty. */
4681typedef struct {
4682 PyObject *last_str;
4683 ExprList expr_list;
4684} FstringParser;
4685
4686#ifdef NDEBUG
4687#define FstringParser_check_invariants(state)
4688#else
4689static void
4690FstringParser_check_invariants(FstringParser *state)
4691{
4692 if (state->last_str)
4693 assert(PyUnicode_CheckExact(state->last_str));
4694 ExprList_check_invariants(&state->expr_list);
4695}
4696#endif
4697
4698static void
4699FstringParser_Init(FstringParser *state)
4700{
4701 state->last_str = NULL;
4702 ExprList_Init(&state->expr_list);
4703 FstringParser_check_invariants(state);
4704}
4705
4706static void
4707FstringParser_Dealloc(FstringParser *state)
4708{
4709 FstringParser_check_invariants(state);
4710
4711 Py_XDECREF(state->last_str);
4712 ExprList_Dealloc(&state->expr_list);
4713}
4714
4715/* Make a Str node, but decref the PyUnicode object being added. */
4716static expr_ty
4717make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4718{
4719 PyObject *s = *str;
4720 *str = NULL;
4721 assert(PyUnicode_CheckExact(s));
4722 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4723 Py_DECREF(s);
4724 return NULL;
4725 }
4726 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4727}
4728
4729/* Add a non-f-string (that is, a regular literal string). str is
4730 decref'd. */
4731static int
4732FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4733{
4734 FstringParser_check_invariants(state);
4735
4736 assert(PyUnicode_CheckExact(str));
4737
4738 if (PyUnicode_GET_LENGTH(str) == 0) {
4739 Py_DECREF(str);
4740 return 0;
4741 }
4742
4743 if (!state->last_str) {
4744 /* We didn't have a string before, so just remember this one. */
4745 state->last_str = str;
4746 } else {
4747 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004748 PyUnicode_AppendAndDel(&state->last_str, str);
4749 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004750 return -1;
4751 }
4752 FstringParser_check_invariants(state);
4753 return 0;
4754}
4755
4756/* Parse an f-string. The f-string is in str, starting at ofs, with no 'f'
4757 or quotes. str is not decref'd, since we don't know if it's used elsewhere.
4758 And if we're only looking at a part of a string, then decref'ing is
4759 definitely not the right thing to do! */
4760static int
4761FstringParser_ConcatFstring(FstringParser *state, PyObject *str,
4762 Py_ssize_t *ofs, int recurse_lvl,
4763 struct compiling *c, const node *n)
4764{
4765 FstringParser_check_invariants(state);
4766
4767 /* Parse the f-string. */
4768 while (1) {
4769 PyObject *literal = NULL;
4770 expr_ty expression = NULL;
4771
4772 /* If there's a zero length literal in front of the
4773 expression, literal will be NULL. If we're at the end of
4774 the f-string, expression will be NULL (unless result == 1,
4775 see below). */
4776 int result = fstring_find_literal_and_expr(str, ofs, recurse_lvl,
4777 &literal, &expression,
4778 c, n);
4779 if (result < 0)
4780 return -1;
4781
4782 /* Add the literal, if any. */
4783 if (!literal) {
4784 /* Do nothing. Just leave last_str alone (and possibly
4785 NULL). */
4786 } else if (!state->last_str) {
4787 state->last_str = literal;
4788 literal = NULL;
4789 } else {
4790 /* We have a literal, concatenate it. */
4791 assert(PyUnicode_GET_LENGTH(literal) != 0);
4792 if (FstringParser_ConcatAndDel(state, literal) < 0)
4793 return -1;
4794 literal = NULL;
4795 }
4796 assert(!state->last_str ||
4797 PyUnicode_GET_LENGTH(state->last_str) != 0);
4798
4799 /* We've dealt with the literal now. It can't be leaked on further
4800 errors. */
4801 assert(literal == NULL);
4802
4803 /* See if we should just loop around to get the next literal
4804 and expression, while ignoring the expression this
4805 time. This is used for un-doubling braces, as an
4806 optimization. */
4807 if (result == 1)
4808 continue;
4809
4810 if (!expression)
4811 /* We're done with this f-string. */
4812 break;
4813
4814 /* We know we have an expression. Convert any existing string
4815 to a Str node. */
4816 if (!state->last_str) {
4817 /* Do nothing. No previous literal. */
4818 } else {
4819 /* Convert the existing last_str literal to a Str node. */
4820 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4821 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4822 return -1;
4823 }
4824
4825 if (ExprList_Append(&state->expr_list, expression) < 0)
4826 return -1;
4827 }
4828
4829 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4830
4831 /* If recurse_lvl is zero, then we must be at the end of the
4832 string. Otherwise, we must be at a right brace. */
4833
4834 if (recurse_lvl == 0 && *ofs < PyUnicode_GET_LENGTH(str)) {
4835 ast_error(c, n, "f-string: unexpected end of string");
4836 return -1;
4837 }
4838 if (recurse_lvl != 0 && PyUnicode_READ_CHAR(str, *ofs) != '}') {
4839 ast_error(c, n, "f-string: expecting '}'");
4840 return -1;
4841 }
4842
4843 FstringParser_check_invariants(state);
4844 return 0;
4845}
4846
4847/* Convert the partial state reflected in last_str and expr_list to an
4848 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4849static expr_ty
4850FstringParser_Finish(FstringParser *state, struct compiling *c,
4851 const node *n)
4852{
4853 asdl_seq *seq;
4854
4855 FstringParser_check_invariants(state);
4856
4857 /* If we're just a constant string with no expressions, return
4858 that. */
4859 if(state->expr_list.size == 0) {
4860 if (!state->last_str) {
4861 /* Create a zero length string. */
4862 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4863 if (!state->last_str)
4864 goto error;
4865 }
4866 return make_str_node_and_del(&state->last_str, c, n);
4867 }
4868
4869 /* Create a Str node out of last_str, if needed. It will be the
4870 last node in our expression list. */
4871 if (state->last_str) {
4872 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4873 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4874 goto error;
4875 }
4876 /* This has already been freed. */
4877 assert(state->last_str == NULL);
4878
4879 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4880 if (!seq)
4881 goto error;
4882
4883 /* If there's only one expression, return it. Otherwise, we need
4884 to join them together. */
4885 if (seq->size == 1)
4886 return seq->elements[0];
4887
4888 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4889
4890error:
4891 FstringParser_Dealloc(state);
4892 return NULL;
4893}
4894
4895/* Given an f-string (with no 'f' or quotes) that's in str starting at
4896 ofs, parse it into an expr_ty. Return NULL on error. Does not
4897 decref str. */
4898static expr_ty
4899fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4900 struct compiling *c, const node *n)
4901{
4902 FstringParser state;
4903
4904 FstringParser_Init(&state);
4905 if (FstringParser_ConcatFstring(&state, str, ofs, recurse_lvl,
4906 c, n) < 0) {
4907 FstringParser_Dealloc(&state);
4908 return NULL;
4909 }
4910
4911 return FstringParser_Finish(&state, c, n);
4912}
4913
4914/* n is a Python string literal, including the bracketing quote
4915 characters, and r, b, u, &/or f prefixes (if any), and embedded
4916 escape sequences (if any). parsestr parses it, and returns the
4917 decoded Python string object. If the string is an f-string, set
4918 *fmode and return the unparsed string object.
4919*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004920static PyObject *
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004922{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004923 size_t len;
4924 const char *s = STR(n);
4925 int quote = Py_CHARMASK(*s);
4926 int rawmode = 0;
4927 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004928 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004929 while (!*bytesmode || !rawmode) {
4930 if (quote == 'b' || quote == 'B') {
4931 quote = *++s;
4932 *bytesmode = 1;
4933 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004934 else if (quote == 'u' || quote == 'U') {
4935 quote = *++s;
4936 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004937 else if (quote == 'r' || quote == 'R') {
4938 quote = *++s;
4939 rawmode = 1;
4940 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004941 else if (quote == 'f' || quote == 'F') {
4942 quote = *++s;
4943 *fmode = 1;
4944 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004945 else {
4946 break;
4947 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004948 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004949 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004950 if (*fmode && *bytesmode) {
4951 PyErr_BadInternalCall();
4952 return NULL;
4953 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004954 if (quote != '\'' && quote != '\"') {
4955 PyErr_BadInternalCall();
4956 return NULL;
4957 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004958 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004959 s++;
4960 len = strlen(s);
4961 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004963 "string to parse is too long");
4964 return NULL;
4965 }
4966 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004967 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004968 PyErr_BadInternalCall();
4969 return NULL;
4970 }
4971 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004972 /* A triple quoted string. We've already skipped one quote at
4973 the start and one at the end of the string. Now skip the
4974 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004975 s += 2;
4976 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004977 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004978 if (s[--len] != quote || s[--len] != quote) {
4979 PyErr_BadInternalCall();
4980 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004981 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004982 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00004983 if (!*bytesmode && !rawmode) {
Eric V. Smith5567f892015-09-21 13:36:09 -04004984 return decode_unicode(c, s, len, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004985 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004986 if (*bytesmode) {
4987 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004988 const char *ch;
4989 for (ch = s; *ch; ch++) {
4990 if (Py_CHARMASK(*ch) >= 0x80) {
4991 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004992 "literal characters.");
4993 return NULL;
4994 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004995 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004996 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00004997 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00004998 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004999 if (rawmode || strchr(s, '\\') == NULL) {
5000 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00005001 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00005002 if (u == NULL || !*bytesmode)
5003 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00005004 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005005 Py_DECREF(u);
5006 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00005007 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00005008 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00005009 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00005010 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005011 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00005012 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005013 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005014 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005015 return PyBytes_DecodeEscape(s, len, NULL, 1,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005016 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005017}
5018
Eric V. Smith235a6f02015-09-19 14:51:32 -04005019/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5020 each STRING atom, and process it as needed. For bytes, just
5021 concatenate them together, and the result will be a Bytes node. For
5022 normal strings and f-strings, concatenate them together. The result
5023 will be a Str node if there were no f-strings; a FormattedValue
5024 node if there's just an f-string (with no leading or trailing
5025 literals), or a JoinedStr node if there are multiple f-strings or
5026 any literals involved. */
5027static expr_ty
5028parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005029{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005030 int bytesmode = 0;
5031 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005032 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005033
5034 FstringParser state;
5035 FstringParser_Init(&state);
5036
5037 for (i = 0; i < NCH(n); i++) {
5038 int this_bytesmode = 0;
5039 int this_fmode = 0;
5040 PyObject *s;
5041
5042 REQ(CHILD(n, i), STRING);
5043 s = parsestr(c, CHILD(n, i), &this_bytesmode, &this_fmode);
5044 if (!s)
5045 goto error;
5046
5047 /* Check that we're not mixing bytes with unicode. */
5048 if (i != 0 && bytesmode != this_bytesmode) {
5049 ast_error(c, n, "cannot mix bytes and nonbytes literals");
5050 Py_DECREF(s);
5051 goto error;
5052 }
5053 bytesmode = this_bytesmode;
5054
5055 assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
5056
5057 if (bytesmode) {
5058 /* For bytes, concat as we go. */
5059 if (i == 0) {
5060 /* First time, just remember this value. */
5061 bytes_str = s;
5062 } else {
5063 PyBytes_ConcatAndDel(&bytes_str, s);
5064 if (!bytes_str)
5065 goto error;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005066 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005067 } else if (this_fmode) {
5068 /* This is an f-string. Concatenate and decref it. */
5069 Py_ssize_t ofs = 0;
5070 int result = FstringParser_ConcatFstring(&state, s, &ofs, 0, c, n);
5071 Py_DECREF(s);
5072 if (result < 0)
5073 goto error;
5074 } else {
5075 /* This is a regular string. Concatenate it. */
5076 if (FstringParser_ConcatAndDel(&state, s) < 0)
5077 goto error;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005078 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005079 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005080 if (bytesmode) {
5081 /* Just return the bytes object and we're done. */
5082 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5083 goto error;
5084 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5085 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005086
Eric V. Smith235a6f02015-09-19 14:51:32 -04005087 /* We're not a bytes string, bytes_str should never have been set. */
5088 assert(bytes_str == NULL);
5089
5090 return FstringParser_Finish(&state, c, n);
5091
5092error:
5093 Py_XDECREF(bytes_str);
5094 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005096}