blob: 5422e9c29b2cb30ff8e8d7da0e564d37331b2879 [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);
167 return 0;
168 }
169 }
170
171 Py_DECREF(it);
172 return 1;
173 }
174
175 return 0;
176}
177
178static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500179validate_expr(expr_ty exp, expr_context_ty ctx)
180{
181 int check_ctx = 1;
182 expr_context_ty actual_ctx;
183
184 /* First check expression context. */
185 switch (exp->kind) {
186 case Attribute_kind:
187 actual_ctx = exp->v.Attribute.ctx;
188 break;
189 case Subscript_kind:
190 actual_ctx = exp->v.Subscript.ctx;
191 break;
192 case Starred_kind:
193 actual_ctx = exp->v.Starred.ctx;
194 break;
195 case Name_kind:
196 actual_ctx = exp->v.Name.ctx;
197 break;
198 case List_kind:
199 actual_ctx = exp->v.List.ctx;
200 break;
201 case Tuple_kind:
202 actual_ctx = exp->v.Tuple.ctx;
203 break;
204 default:
205 if (ctx != Load) {
206 PyErr_Format(PyExc_ValueError, "expression which can't be "
207 "assigned to in %s context", expr_context_name(ctx));
208 return 0;
209 }
210 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100211 /* set actual_ctx to prevent gcc warning */
212 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500213 }
214 if (check_ctx && actual_ctx != ctx) {
215 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
216 expr_context_name(ctx), expr_context_name(actual_ctx));
217 return 0;
218 }
219
220 /* Now validate expression. */
221 switch (exp->kind) {
222 case BoolOp_kind:
223 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
224 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
225 return 0;
226 }
227 return validate_exprs(exp->v.BoolOp.values, Load, 0);
228 case BinOp_kind:
229 return validate_expr(exp->v.BinOp.left, Load) &&
230 validate_expr(exp->v.BinOp.right, Load);
231 case UnaryOp_kind:
232 return validate_expr(exp->v.UnaryOp.operand, Load);
233 case Lambda_kind:
234 return validate_arguments(exp->v.Lambda.args) &&
235 validate_expr(exp->v.Lambda.body, Load);
236 case IfExp_kind:
237 return validate_expr(exp->v.IfExp.test, Load) &&
238 validate_expr(exp->v.IfExp.body, Load) &&
239 validate_expr(exp->v.IfExp.orelse, Load);
240 case Dict_kind:
241 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
242 PyErr_SetString(PyExc_ValueError,
243 "Dict doesn't have the same number of keys as values");
244 return 0;
245 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400246 /* null_ok=1 for keys expressions to allow dict unpacking to work in
247 dict literals, i.e. ``{**{a:b}}`` */
248 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
249 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500250 case Set_kind:
251 return validate_exprs(exp->v.Set.elts, Load, 0);
252#define COMP(NAME) \
253 case NAME ## _kind: \
254 return validate_comprehension(exp->v.NAME.generators) && \
255 validate_expr(exp->v.NAME.elt, Load);
256 COMP(ListComp)
257 COMP(SetComp)
258 COMP(GeneratorExp)
259#undef COMP
260 case DictComp_kind:
261 return validate_comprehension(exp->v.DictComp.generators) &&
262 validate_expr(exp->v.DictComp.key, Load) &&
263 validate_expr(exp->v.DictComp.value, Load);
264 case Yield_kind:
265 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500266 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000267 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400268 case Await_kind:
269 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500270 case Compare_kind:
271 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
272 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
273 return 0;
274 }
275 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
276 asdl_seq_LEN(exp->v.Compare.ops)) {
277 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
278 "of comparators and operands");
279 return 0;
280 }
281 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
282 validate_expr(exp->v.Compare.left, Load);
283 case Call_kind:
284 return validate_expr(exp->v.Call.func, Load) &&
285 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400286 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100287 case Constant_kind:
288 if (!validate_constant(exp->v.Constant.value)) {
289 PyErr_SetString(PyExc_TypeError, "invalid type in Constant");
290 return 0;
291 }
292 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500293 case Num_kind: {
294 PyObject *n = exp->v.Num.n;
295 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
296 !PyComplex_CheckExact(n)) {
297 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
298 return 0;
299 }
300 return 1;
301 }
302 case Str_kind: {
303 PyObject *s = exp->v.Str.s;
304 if (!PyUnicode_CheckExact(s)) {
305 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
306 return 0;
307 }
308 return 1;
309 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400310 case JoinedStr_kind:
311 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
312 case FormattedValue_kind:
313 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
314 return 0;
315 if (exp->v.FormattedValue.format_spec)
316 return validate_expr(exp->v.FormattedValue.format_spec, Load);
317 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500318 case Bytes_kind: {
319 PyObject *b = exp->v.Bytes.s;
320 if (!PyBytes_CheckExact(b)) {
321 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
322 return 0;
323 }
324 return 1;
325 }
326 case Attribute_kind:
327 return validate_expr(exp->v.Attribute.value, Load);
328 case Subscript_kind:
329 return validate_slice(exp->v.Subscript.slice) &&
330 validate_expr(exp->v.Subscript.value, Load);
331 case Starred_kind:
332 return validate_expr(exp->v.Starred.value, ctx);
333 case List_kind:
334 return validate_exprs(exp->v.List.elts, ctx, 0);
335 case Tuple_kind:
336 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
337 /* These last cases don't have any checking. */
338 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500339 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500340 case Ellipsis_kind:
341 return 1;
342 default:
343 PyErr_SetString(PyExc_SystemError, "unexpected expression");
344 return 0;
345 }
346}
347
348static int
349validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
350{
351 if (asdl_seq_LEN(seq))
352 return 1;
353 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
354 return 0;
355}
356
357static int
358validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
359{
360 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
361 validate_exprs(targets, ctx, 0);
362}
363
364static int
365validate_body(asdl_seq *body, const char *owner)
366{
367 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
368}
369
370static int
371validate_stmt(stmt_ty stmt)
372{
373 int i;
374 switch (stmt->kind) {
375 case FunctionDef_kind:
376 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
377 validate_arguments(stmt->v.FunctionDef.args) &&
378 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
379 (!stmt->v.FunctionDef.returns ||
380 validate_expr(stmt->v.FunctionDef.returns, Load));
381 case ClassDef_kind:
382 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
383 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
384 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400385 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500386 case Return_kind:
387 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
388 case Delete_kind:
389 return validate_assignlist(stmt->v.Delete.targets, Del);
390 case Assign_kind:
391 return validate_assignlist(stmt->v.Assign.targets, Store) &&
392 validate_expr(stmt->v.Assign.value, Load);
393 case AugAssign_kind:
394 return validate_expr(stmt->v.AugAssign.target, Store) &&
395 validate_expr(stmt->v.AugAssign.value, Load);
396 case For_kind:
397 return validate_expr(stmt->v.For.target, Store) &&
398 validate_expr(stmt->v.For.iter, Load) &&
399 validate_body(stmt->v.For.body, "For") &&
400 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400401 case AsyncFor_kind:
402 return validate_expr(stmt->v.AsyncFor.target, Store) &&
403 validate_expr(stmt->v.AsyncFor.iter, Load) &&
404 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
405 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 case While_kind:
407 return validate_expr(stmt->v.While.test, Load) &&
408 validate_body(stmt->v.While.body, "While") &&
409 validate_stmts(stmt->v.While.orelse);
410 case If_kind:
411 return validate_expr(stmt->v.If.test, Load) &&
412 validate_body(stmt->v.If.body, "If") &&
413 validate_stmts(stmt->v.If.orelse);
414 case With_kind:
415 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
416 return 0;
417 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
418 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
419 if (!validate_expr(item->context_expr, Load) ||
420 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
421 return 0;
422 }
423 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400424 case AsyncWith_kind:
425 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
426 return 0;
427 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
428 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
429 if (!validate_expr(item->context_expr, Load) ||
430 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
431 return 0;
432 }
433 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500434 case Raise_kind:
435 if (stmt->v.Raise.exc) {
436 return validate_expr(stmt->v.Raise.exc, Load) &&
437 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
438 }
439 if (stmt->v.Raise.cause) {
440 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
441 return 0;
442 }
443 return 1;
444 case Try_kind:
445 if (!validate_body(stmt->v.Try.body, "Try"))
446 return 0;
447 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
448 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
449 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
450 return 0;
451 }
452 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
453 asdl_seq_LEN(stmt->v.Try.orelse)) {
454 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
455 return 0;
456 }
457 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
458 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
459 if ((handler->v.ExceptHandler.type &&
460 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
461 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
462 return 0;
463 }
464 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
465 validate_stmts(stmt->v.Try.finalbody)) &&
466 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
467 validate_stmts(stmt->v.Try.orelse));
468 case Assert_kind:
469 return validate_expr(stmt->v.Assert.test, Load) &&
470 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
471 case Import_kind:
472 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
473 case ImportFrom_kind:
474 if (stmt->v.ImportFrom.level < -1) {
475 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
476 return 0;
477 }
478 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
479 case Global_kind:
480 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
481 case Nonlocal_kind:
482 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
483 case Expr_kind:
484 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400485 case AsyncFunctionDef_kind:
486 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
487 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
488 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
489 (!stmt->v.AsyncFunctionDef.returns ||
490 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500491 case Pass_kind:
492 case Break_kind:
493 case Continue_kind:
494 return 1;
495 default:
496 PyErr_SetString(PyExc_SystemError, "unexpected statement");
497 return 0;
498 }
499}
500
501static int
502validate_stmts(asdl_seq *seq)
503{
504 int i;
505 for (i = 0; i < asdl_seq_LEN(seq); i++) {
506 stmt_ty stmt = asdl_seq_GET(seq, i);
507 if (stmt) {
508 if (!validate_stmt(stmt))
509 return 0;
510 }
511 else {
512 PyErr_SetString(PyExc_ValueError,
513 "None disallowed in statement list");
514 return 0;
515 }
516 }
517 return 1;
518}
519
520static int
521validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
522{
523 int i;
524 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
525 expr_ty expr = asdl_seq_GET(exprs, i);
526 if (expr) {
527 if (!validate_expr(expr, ctx))
528 return 0;
529 }
530 else if (!null_ok) {
531 PyErr_SetString(PyExc_ValueError,
532 "None disallowed in expression list");
533 return 0;
534 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100535
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500536 }
537 return 1;
538}
539
540int
541PyAST_Validate(mod_ty mod)
542{
543 int res = 0;
544
545 switch (mod->kind) {
546 case Module_kind:
547 res = validate_stmts(mod->v.Module.body);
548 break;
549 case Interactive_kind:
550 res = validate_stmts(mod->v.Interactive.body);
551 break;
552 case Expression_kind:
553 res = validate_expr(mod->v.Expression.body, Load);
554 break;
555 case Suite_kind:
556 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
557 break;
558 default:
559 PyErr_SetString(PyExc_SystemError, "impossible module node");
560 res = 0;
561 break;
562 }
563 return res;
564}
565
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500566/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500567#include "grammar.h"
568#include "parsetok.h"
569#include "graminit.h"
570
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571/* Data structure used internally */
572struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000573 char *c_encoding; /* source encoding */
Eric V. Smith163b5c62015-08-21 09:40:38 -0400574 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200575 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500576 PyObject *c_normalize; /* Normalization function from unicodedata. */
577 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578};
579
580static asdl_seq *seq_for_testlist(struct compiling *, const node *);
581static expr_ty ast_for_expr(struct compiling *, const node *);
582static stmt_ty ast_for_stmt(struct compiling *, const node *);
583static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000584static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
585 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000586static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000587static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Yury Selivanov75445082015-05-11 22:57:16 -0400589static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
590static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592/* Note different signature for ast_for_call */
593static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
594
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000595static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400596static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597
Nick Coghlan650f0d02007-04-15 12:05:43 +0000598#define COMP_GENEXP 0
599#define COMP_LISTCOMP 1
600#define COMP_SETCOMP 2
601
Benjamin Peterson55e00432012-01-16 17:22:31 -0500602static int
603init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000604{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500605 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
606 if (!m)
607 return 0;
608 c->c_normalize = PyObject_GetAttrString(m, "normalize");
609 Py_DECREF(m);
610 if (!c->c_normalize)
611 return 0;
612 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500613 if (!c->c_normalize_args) {
614 Py_CLEAR(c->c_normalize);
615 return 0;
616 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200617 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500618 return 1;
619}
620
621static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400622new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500623{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400624 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500625 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000626 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500627 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500628 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000629 /* Check whether there are non-ASCII characters in the
630 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500631 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200632 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500633 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500634 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200635 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500636 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500637 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
638 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500639 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640 if (!id2)
641 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200642 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000643 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000644 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200645 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
646 Py_DECREF(id);
647 return NULL;
648 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000649 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650}
651
Benjamin Peterson55e00432012-01-16 17:22:31 -0500652#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400655ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400657 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Victor Stinner14e461d2013-08-26 22:28:21 +0200659 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000661 Py_INCREF(Py_None);
662 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200664 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400665 if (!tmp)
666 return 0;
667 errstr = PyUnicode_FromString(errmsg);
668 if (!errstr) {
669 Py_DECREF(tmp);
670 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000671 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 Py_DECREF(errstr);
674 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400675 if (value) {
676 PyErr_SetObject(PyExc_SyntaxError, value);
677 Py_DECREF(value);
678 }
679 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680}
681
682/* num_stmts() returns number of contained statements.
683
684 Use this routine to determine how big a sequence is needed for
685 the statements in a parse tree. Its raison d'etre is this bit of
686 grammar:
687
688 stmt: simple_stmt | compound_stmt
689 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
690
691 A simple_stmt can contain multiple small_stmt elements joined
692 by semicolons. If the arg is a simple_stmt, the number of
693 small_stmt elements is returned.
694*/
695
696static int
697num_stmts(const node *n)
698{
699 int i, l;
700 node *ch;
701
702 switch (TYPE(n)) {
703 case single_input:
704 if (TYPE(CHILD(n, 0)) == NEWLINE)
705 return 0;
706 else
707 return num_stmts(CHILD(n, 0));
708 case file_input:
709 l = 0;
710 for (i = 0; i < NCH(n); i++) {
711 ch = CHILD(n, i);
712 if (TYPE(ch) == stmt)
713 l += num_stmts(ch);
714 }
715 return l;
716 case stmt:
717 return num_stmts(CHILD(n, 0));
718 case compound_stmt:
719 return 1;
720 case simple_stmt:
721 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
722 case suite:
723 if (NCH(n) == 1)
724 return num_stmts(CHILD(n, 0));
725 else {
726 l = 0;
727 for (i = 2; i < (NCH(n) - 1); i++)
728 l += num_stmts(CHILD(n, i));
729 return l;
730 }
731 default: {
732 char buf[128];
733
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000734 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 TYPE(n), NCH(n));
736 Py_FatalError(buf);
737 }
738 }
739 assert(0);
740 return 0;
741}
742
743/* Transform the CST rooted at node * to the appropriate AST
744*/
745
746mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200747PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
748 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000750 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 asdl_seq *stmts = NULL;
752 stmt_ty s;
753 node *ch;
754 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500755 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400757 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200758 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400759 c.c_filename = filename;
760 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000762 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000764#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400765 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500766 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000767#endif
768 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 } else if (TYPE(n) == encoding_decl) {
771 c.c_encoding = STR(n);
772 n = CHILD(n, 0);
773 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000775 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 }
777
Jeremy Hyltona8293132006-02-28 17:58:27 +0000778 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 switch (TYPE(n)) {
780 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200781 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500783 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 for (i = 0; i < NCH(n) - 1; i++) {
785 ch = CHILD(n, i);
786 if (TYPE(ch) == NEWLINE)
787 continue;
788 REQ(ch, stmt);
789 num = num_stmts(ch);
790 if (num == 1) {
791 s = ast_for_stmt(&c, ch);
792 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500793 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000794 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 }
796 else {
797 ch = CHILD(ch, 0);
798 REQ(ch, simple_stmt);
799 for (j = 0; j < num; j++) {
800 s = ast_for_stmt(&c, CHILD(ch, j * 2));
801 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500802 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000803 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
805 }
806 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500807 res = Module(stmts, arena);
808 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 case eval_input: {
810 expr_ty testlist_ast;
811
Nick Coghlan650f0d02007-04-15 12:05:43 +0000812 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000813 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500815 goto out;
816 res = Expression(testlist_ast, arena);
817 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 }
819 case single_input:
820 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200821 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500823 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
825 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000826 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500827 goto out;
828 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 }
830 else {
831 n = CHILD(n, 0);
832 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200833 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500835 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000837 s = ast_for_stmt(&c, n);
838 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500839 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 asdl_seq_SET(stmts, 0, s);
841 }
842 else {
843 /* Only a simple_stmt can contain multiple statements. */
844 REQ(n, simple_stmt);
845 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 if (TYPE(CHILD(n, i)) == NEWLINE)
847 break;
848 s = ast_for_stmt(&c, CHILD(n, i));
849 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500850 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 asdl_seq_SET(stmts, i / 2, s);
852 }
853 }
854
Benjamin Peterson55e00432012-01-16 17:22:31 -0500855 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500857 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000859 PyErr_Format(PyExc_SystemError,
860 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500861 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 out:
864 if (c.c_normalize) {
865 Py_DECREF(c.c_normalize);
866 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
867 Py_DECREF(c.c_normalize_args);
868 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500869 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870}
871
Victor Stinner14e461d2013-08-26 22:28:21 +0200872mod_ty
873PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
874 PyArena *arena)
875{
876 mod_ty mod;
877 PyObject *filename;
878 filename = PyUnicode_DecodeFSDefault(filename_str);
879 if (filename == NULL)
880 return NULL;
881 mod = PyAST_FromNodeObject(n, flags, filename, arena);
882 Py_DECREF(filename);
883 return mod;
884
885}
886
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
888*/
889
890static operator_ty
891get_operator(const node *n)
892{
893 switch (TYPE(n)) {
894 case VBAR:
895 return BitOr;
896 case CIRCUMFLEX:
897 return BitXor;
898 case AMPER:
899 return BitAnd;
900 case LEFTSHIFT:
901 return LShift;
902 case RIGHTSHIFT:
903 return RShift;
904 case PLUS:
905 return Add;
906 case MINUS:
907 return Sub;
908 case STAR:
909 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400910 case AT:
911 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 case SLASH:
913 return Div;
914 case DOUBLESLASH:
915 return FloorDiv;
916 case PERCENT:
917 return Mod;
918 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 }
921}
922
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200923static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000924 "None",
925 "True",
926 "False",
927 NULL,
928};
929
930static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400931forbidden_name(struct compiling *c, identifier name, const node *n,
932 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000933{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000934 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000935 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400936 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000937 return 1;
938 }
939 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200940 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000941 for (p = FORBIDDEN; *p; p++) {
942 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400943 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000944 return 1;
945 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000946 }
947 }
948 return 0;
949}
950
Jeremy Hyltona8293132006-02-28 17:58:27 +0000951/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952
953 Only sets context for expr kinds that "can appear in assignment context"
954 (according to ../Parser/Python.asdl). For other expr kinds, it sets
955 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956*/
957
958static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000959set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960{
961 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000962 /* If a particular expression type can't be used for assign / delete,
963 set expr_name to its name and an error message will be generated.
964 */
965 const char* expr_name = NULL;
966
967 /* The ast defines augmented store and load contexts, but the
968 implementation here doesn't actually use them. The code may be
969 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000970 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000971 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000972 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000973 */
974 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975
976 switch (e->kind) {
977 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000978 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400979 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000980 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000981 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000983 e->v.Subscript.ctx = ctx;
984 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000985 case Starred_kind:
986 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000987 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000988 return 0;
989 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000991 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500992 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000993 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000994 }
995 e->v.Name.ctx = ctx;
996 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000998 e->v.List.ctx = ctx;
999 s = e->v.List.elts;
1000 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +00001002 if (asdl_seq_LEN(e->v.Tuple.elts)) {
1003 e->v.Tuple.ctx = ctx;
1004 s = e->v.Tuple.elts;
1005 }
1006 else {
1007 expr_name = "()";
1008 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001009 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001010 case Lambda_kind:
1011 expr_name = "lambda";
1012 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001014 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001015 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001016 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001018 case UnaryOp_kind:
1019 expr_name = "operator";
1020 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001022 expr_name = "generator expression";
1023 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001024 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001025 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001026 expr_name = "yield expression";
1027 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001028 case Await_kind:
1029 expr_name = "await expression";
1030 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001031 case ListComp_kind:
1032 expr_name = "list comprehension";
1033 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001034 case SetComp_kind:
1035 expr_name = "set comprehension";
1036 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001037 case DictComp_kind:
1038 expr_name = "dict comprehension";
1039 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001040 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001041 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 case Num_kind:
1043 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001044 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001045 case JoinedStr_kind:
1046 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001047 expr_name = "literal";
1048 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001049 case NameConstant_kind:
1050 expr_name = "keyword";
1051 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001052 case Ellipsis_kind:
1053 expr_name = "Ellipsis";
1054 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001055 case Compare_kind:
1056 expr_name = "comparison";
1057 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058 case IfExp_kind:
1059 expr_name = "conditional expression";
1060 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001061 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyErr_Format(PyExc_SystemError,
1063 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001064 e->kind, e->lineno);
1065 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001067 /* Check for error string set by switch */
1068 if (expr_name) {
1069 char buf[300];
1070 PyOS_snprintf(buf, sizeof(buf),
1071 "can't %s %s",
1072 ctx == Store ? "assign to" : "delete",
1073 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001074 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001075 }
1076
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 */
1080 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001081 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082
Thomas Wouters89f507f2006-12-13 04:49:30 +00001083 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001084 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001085 return 0;
1086 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 }
1088 return 1;
1089}
1090
1091static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001092ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093{
1094 REQ(n, augassign);
1095 n = CHILD(n, 0);
1096 switch (STR(n)[0]) {
1097 case '+':
1098 return Add;
1099 case '-':
1100 return Sub;
1101 case '/':
1102 if (STR(n)[1] == '/')
1103 return FloorDiv;
1104 else
1105 return Div;
1106 case '%':
1107 return Mod;
1108 case '<':
1109 return LShift;
1110 case '>':
1111 return RShift;
1112 case '&':
1113 return BitAnd;
1114 case '^':
1115 return BitXor;
1116 case '|':
1117 return BitOr;
1118 case '*':
1119 if (STR(n)[1] == '*')
1120 return Pow;
1121 else
1122 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001123 case '@':
1124 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001126 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 }
1129}
1130
1131static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001132ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001134 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 |'is' 'not'
1136 */
1137 REQ(n, comp_op);
1138 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 n = CHILD(n, 0);
1140 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 case LESS:
1142 return Lt;
1143 case GREATER:
1144 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001145 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 return Eq;
1147 case LESSEQUAL:
1148 return LtE;
1149 case GREATEREQUAL:
1150 return GtE;
1151 case NOTEQUAL:
1152 return NotEq;
1153 case NAME:
1154 if (strcmp(STR(n), "in") == 0)
1155 return In;
1156 if (strcmp(STR(n), "is") == 0)
1157 return Is;
1158 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001159 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001161 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 }
1164 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 /* handle "not in" and "is not" */
1166 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 case NAME:
1168 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1169 return NotIn;
1170 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1171 return IsNot;
1172 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001173 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001175 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001176 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 }
Neal Norwitz79792652005-11-14 04:25:03 +00001178 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001180 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181}
1182
1183static asdl_seq *
1184seq_for_testlist(struct compiling *c, const node *n)
1185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001187 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1188 */
Armin Rigo31441302005-10-21 12:57:31 +00001189 asdl_seq *seq;
1190 expr_ty expression;
1191 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001192 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001194 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 if (!seq)
1196 return NULL;
1197
1198 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001200 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201
Benjamin Peterson4905e802009-09-27 02:43:28 +00001202 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001203 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205
1206 assert(i / 2 < seq->size);
1207 asdl_seq_SET(seq, i / 2, expression);
1208 }
1209 return seq;
1210}
1211
Neal Norwitzc1505362006-12-28 06:47:50 +00001212static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001213ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001214{
1215 identifier name;
1216 expr_ty annotation = NULL;
1217 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001218 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001219
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001220 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001221 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001222 name = NEW_IDENTIFIER(ch);
1223 if (!name)
1224 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001225 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001226 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001227
1228 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1229 annotation = ast_for_expr(c, CHILD(n, 2));
1230 if (!annotation)
1231 return NULL;
1232 }
1233
Victor Stinnerc106c682015-11-06 17:01:48 +01001234 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001235 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001236 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001237 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238}
1239
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240/* returns -1 if failed to handle keyword only arguments
1241 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001242 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001243 ^^^
1244 start pointing here
1245 */
1246static int
1247handle_keywordonly_args(struct compiling *c, const node *n, int start,
1248 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1249{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001250 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001251 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001252 expr_ty expression, annotation;
1253 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001254 int i = start;
1255 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001256
1257 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001258 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001259 return -1;
1260 }
1261 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001262 while (i < NCH(n)) {
1263 ch = CHILD(n, i);
1264 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001265 case vfpdef:
1266 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001267 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001268 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001269 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001270 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001271 asdl_seq_SET(kwdefaults, j, expression);
1272 i += 2; /* '=' and test */
1273 }
1274 else { /* setting NULL if no default value exists */
1275 asdl_seq_SET(kwdefaults, j, NULL);
1276 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001277 if (NCH(ch) == 3) {
1278 /* ch is NAME ':' test */
1279 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001280 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001281 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001282 }
1283 else {
1284 annotation = NULL;
1285 }
1286 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001287 argname = NEW_IDENTIFIER(ch);
1288 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001289 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001290 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001291 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001292 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1293 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001294 if (!arg)
1295 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001296 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001297 i += 2; /* the name and the comma */
1298 break;
1299 case DOUBLESTAR:
1300 return i;
1301 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001302 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001303 goto error;
1304 }
1305 }
1306 return i;
1307 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001309}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310
Jeremy Hyltona8293132006-02-28 17:58:27 +00001311/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312
1313static arguments_ty
1314ast_for_arguments(struct compiling *c, const node *n)
1315{
Neal Norwitzc1505362006-12-28 06:47:50 +00001316 /* This function handles both typedargslist (function definition)
1317 and varargslist (lambda definition).
1318
1319 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001320 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1321 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1322 | '**' tfpdef [',']]]
1323 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1324 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001325 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001326 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1327 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1328 | '**' vfpdef [',']]]
1329 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1330 | '**' vfpdef [',']
1331 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001332 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1336 int nposdefaults = 0, found_default = 0;
1337 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001338 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001339 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 node *ch;
1341
1342 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001343 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001344 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001345 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001347 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348
Jeremy Hyltone921e022008-07-17 16:37:17 +00001349 /* First count the number of positional args & defaults. The
1350 variable i is the loop index for this for loop and the next.
1351 The next loop picks up where the first leaves off.
1352 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001354 ch = CHILD(n, i);
1355 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001356 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001357 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001358 if (i < NCH(n) && /* skip argument following star */
1359 (TYPE(CHILD(n, i)) == tfpdef ||
1360 TYPE(CHILD(n, i)) == vfpdef)) {
1361 i++;
1362 }
1363 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001365 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001366 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 defaults for keyword only args */
1371 for ( ; i < NCH(n); ++i) {
1372 ch = CHILD(n, i);
1373 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001374 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001376 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001377 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001378 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001379 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001380 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001382 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001384 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001386 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001388 since we set NULL as default for keyword only argument w/o default
1389 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001390 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001391 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001393 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394
1395 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001396 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001397 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001400 /* tfpdef: NAME [':' test]
1401 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 */
1403 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001404 j = 0; /* index for defaults */
1405 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 ch = CHILD(n, i);
1408 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001409 case tfpdef:
1410 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1412 anything other than EQUAL or a comma? */
1413 /* XXX Should NCH(n) check be made a separate check? */
1414 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001415 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1416 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001417 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001418 assert(posdefaults != NULL);
1419 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001424 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001425 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001426 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001427 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001428 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001429 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001430 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001431 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 i += 2; /* the name and the comma */
1433 break;
1434 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001435 if (i+1 >= NCH(n) ||
1436 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001437 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001438 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001439 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001440 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001441 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001442 if (TYPE(ch) == COMMA) {
1443 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001444 i += 2; /* now follows keyword only arguments */
1445 res = handle_keywordonly_args(c, n, i,
1446 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001447 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 i = res; /* res has new position to process */
1449 }
1450 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001451 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001452 if (!vararg)
1453 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001454
Guido van Rossum4f72a782006-10-27 23:31:49 +00001455 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001456 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1457 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001458 int res = 0;
1459 res = handle_keywordonly_args(c, n, i,
1460 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001461 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001462 i = res; /* res has new position to process */
1463 }
1464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 break;
1466 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001467 ch = CHILD(n, i+1); /* tfpdef */
1468 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001469 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001470 if (!kwarg)
1471 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 i += 3;
1473 break;
1474 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001475 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 "unexpected node in varargslist: %d @ %d",
1477 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001478 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001479 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001481 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482}
1483
1484static expr_ty
1485ast_for_dotted_name(struct compiling *c, const node *n)
1486{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001487 expr_ty e;
1488 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001489 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 int i;
1491
1492 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001493
1494 lineno = LINENO(n);
1495 col_offset = n->n_col_offset;
1496
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 id = NEW_IDENTIFIER(CHILD(n, 0));
1498 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001499 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001500 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001502 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503
1504 for (i = 2; i < NCH(n); i+=2) {
1505 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001506 if (!id)
1507 return NULL;
1508 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1509 if (!e)
1510 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 }
1512
1513 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514}
1515
1516static expr_ty
1517ast_for_decorator(struct compiling *c, const node *n)
1518{
1519 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1520 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001521 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001524 REQ(CHILD(n, 0), AT);
1525 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1528 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001529 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001532 d = name_expr;
1533 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 }
1535 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001536 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001538 if (!d)
1539 return NULL;
1540 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 }
1542 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001543 d = ast_for_call(c, CHILD(n, 3), name_expr);
1544 if (!d)
1545 return NULL;
1546 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 }
1548
1549 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550}
1551
1552static asdl_seq*
1553ast_for_decorators(struct compiling *c, const node *n)
1554{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001555 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001556 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001560 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 if (!decorator_seq)
1562 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001565 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001566 if (!d)
1567 return NULL;
1568 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 }
1570 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571}
1572
1573static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001574ast_for_funcdef_impl(struct compiling *c, const node *n,
1575 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001577 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001578 identifier name;
1579 arguments_ty args;
1580 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001581 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001582 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583
1584 REQ(n, funcdef);
1585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 name = NEW_IDENTIFIER(CHILD(n, name_i));
1587 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001588 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001589 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1592 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001593 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001594 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1595 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1596 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001597 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001598 name_i += 2;
1599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 body = ast_for_suite(c, CHILD(n, name_i + 3));
1601 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001602 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603
Yury Selivanov75445082015-05-11 22:57:16 -04001604 if (is_async)
1605 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1606 LINENO(n),
1607 n->n_col_offset, c->c_arena);
1608 else
1609 return FunctionDef(name, args, body, decorator_seq, returns,
1610 LINENO(n),
1611 n->n_col_offset, c->c_arena);
1612}
1613
1614static stmt_ty
1615ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1616{
1617 /* async_funcdef: ASYNC funcdef */
1618 REQ(n, async_funcdef);
1619 REQ(CHILD(n, 0), ASYNC);
1620 REQ(CHILD(n, 1), funcdef);
1621
1622 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1623 1 /* is_async */);
1624}
1625
1626static stmt_ty
1627ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1628{
1629 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1630 return ast_for_funcdef_impl(c, n, decorator_seq,
1631 0 /* is_async */);
1632}
1633
1634
1635static stmt_ty
1636ast_for_async_stmt(struct compiling *c, const node *n)
1637{
1638 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1639 REQ(n, async_stmt);
1640 REQ(CHILD(n, 0), ASYNC);
1641
1642 switch (TYPE(CHILD(n, 1))) {
1643 case funcdef:
1644 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1645 1 /* is_async */);
1646 case with_stmt:
1647 return ast_for_with_stmt(c, CHILD(n, 1),
1648 1 /* is_async */);
1649
1650 case for_stmt:
1651 return ast_for_for_stmt(c, CHILD(n, 1),
1652 1 /* is_async */);
1653
1654 default:
1655 PyErr_Format(PyExc_SystemError,
1656 "invalid async stament: %s",
1657 STR(CHILD(n, 1)));
1658 return NULL;
1659 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660}
1661
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001662static stmt_ty
1663ast_for_decorated(struct compiling *c, const node *n)
1664{
Yury Selivanov75445082015-05-11 22:57:16 -04001665 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001666 stmt_ty thing = NULL;
1667 asdl_seq *decorator_seq = NULL;
1668
1669 REQ(n, decorated);
1670
1671 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1672 if (!decorator_seq)
1673 return NULL;
1674
1675 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001676 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001678
1679 if (TYPE(CHILD(n, 1)) == funcdef) {
1680 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1681 } else if (TYPE(CHILD(n, 1)) == classdef) {
1682 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001683 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1684 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001685 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001686 /* we count the decorators in when talking about the class' or
1687 * function's line number */
1688 if (thing) {
1689 thing->lineno = LINENO(n);
1690 thing->col_offset = n->n_col_offset;
1691 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001692 return thing;
1693}
1694
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695static expr_ty
1696ast_for_lambdef(struct compiling *c, const node *n)
1697{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001698 /* lambdef: 'lambda' [varargslist] ':' test
1699 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 arguments_ty args;
1701 expr_ty expression;
1702
1703 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001704 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 if (!args)
1706 return NULL;
1707 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001708 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 }
1711 else {
1712 args = ast_for_arguments(c, CHILD(n, 1));
1713 if (!args)
1714 return NULL;
1715 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001716 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 }
1719
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001720 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721}
1722
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001723static expr_ty
1724ast_for_ifexpr(struct compiling *c, const node *n)
1725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001727 expr_ty expression, body, orelse;
1728
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001729 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001730 body = ast_for_expr(c, CHILD(n, 0));
1731 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001732 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001733 expression = ast_for_expr(c, CHILD(n, 2));
1734 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001735 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001736 orelse = ast_for_expr(c, CHILD(n, 4));
1737 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001738 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001739 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1740 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001741}
1742
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001744 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745
Nick Coghlan650f0d02007-04-15 12:05:43 +00001746 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747*/
1748
1749static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001750count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001752 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753
Guido van Rossumd8faa362007-04-27 19:54:29 +00001754 count_comp_for:
1755 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001756 REQ(n, comp_for);
1757 if (NCH(n) == 5)
1758 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001759 else
1760 return n_fors;
1761 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001762 REQ(n, comp_iter);
1763 n = CHILD(n, 0);
1764 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001765 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001766 else if (TYPE(n) == comp_if) {
1767 if (NCH(n) == 3) {
1768 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001769 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001770 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001771 else
1772 return n_fors;
1773 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001774
Guido van Rossumd8faa362007-04-27 19:54:29 +00001775 /* Should never be reached */
1776 PyErr_SetString(PyExc_SystemError,
1777 "logic error in count_comp_fors");
1778 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779}
1780
Nick Coghlan650f0d02007-04-15 12:05:43 +00001781/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782
Nick Coghlan650f0d02007-04-15 12:05:43 +00001783 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784*/
1785
1786static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001787count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001789 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790
Guido van Rossumd8faa362007-04-27 19:54:29 +00001791 while (1) {
1792 REQ(n, comp_iter);
1793 if (TYPE(CHILD(n, 0)) == comp_for)
1794 return n_ifs;
1795 n = CHILD(n, 0);
1796 REQ(n, comp_if);
1797 n_ifs++;
1798 if (NCH(n) == 2)
1799 return n_ifs;
1800 n = CHILD(n, 2);
1801 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802}
1803
Guido van Rossum992d4a32007-07-11 13:09:30 +00001804static asdl_seq *
1805ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001808 asdl_seq *comps;
1809
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001810 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 if (n_fors == -1)
1812 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001813
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001814 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001815 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001819 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001821 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001822 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823
Guido van Rossum992d4a32007-07-11 13:09:30 +00001824 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825
Guido van Rossum992d4a32007-07-11 13:09:30 +00001826 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001827 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001828 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001830 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001831 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001833
Thomas Wouters89f507f2006-12-13 04:49:30 +00001834 /* Check the # of children rather than the length of t, since
1835 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001836 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001837 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001838 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001840 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1841 c->c_arena),
1842 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001843 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845
Guido van Rossum992d4a32007-07-11 13:09:30 +00001846 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 int j, n_ifs;
1848 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849
Guido van Rossum992d4a32007-07-11 13:09:30 +00001850 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001851 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001852 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001854
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001855 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001860 REQ(n, comp_iter);
1861 n = CHILD(n, 0);
1862 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863
Guido van Rossum992d4a32007-07-11 13:09:30 +00001864 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001865 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001866 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001867 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001868 if (NCH(n) == 3)
1869 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001871 /* on exit, must guarantee that n is a comp_for */
1872 if (TYPE(n) == comp_iter)
1873 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001874 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001876 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001878 return comps;
1879}
1880
1881static expr_ty
1882ast_for_itercomp(struct compiling *c, const node *n, int type)
1883{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001884 /* testlist_comp: (test|star_expr)
1885 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001886 expr_ty elt;
1887 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001888 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889
Guido van Rossum992d4a32007-07-11 13:09:30 +00001890 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001892 ch = CHILD(n, 0);
1893 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001894 if (!elt)
1895 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001896 if (elt->kind == Starred_kind) {
1897 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1898 return NULL;
1899 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 comps = ast_for_comprehension(c, CHILD(n, 1));
1902 if (!comps)
1903 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001904
1905 if (type == COMP_GENEXP)
1906 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1907 else if (type == COMP_LISTCOMP)
1908 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1909 else if (type == COMP_SETCOMP)
1910 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1911 else
1912 /* Should never happen */
1913 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914}
1915
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001916/* Fills in the key, value pair corresponding to the dict element. In case
1917 * of an unpacking, key is NULL. *i is advanced by the number of ast
1918 * elements. Iff successful, nonzero is returned.
1919 */
1920static int
1921ast_for_dictelement(struct compiling *c, const node *n, int *i,
1922 expr_ty *key, expr_ty *value)
1923{
1924 expr_ty expression;
1925 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1926 assert(NCH(n) - *i >= 2);
1927
1928 expression = ast_for_expr(c, CHILD(n, *i + 1));
1929 if (!expression)
1930 return 0;
1931 *key = NULL;
1932 *value = expression;
1933
1934 *i += 2;
1935 }
1936 else {
1937 assert(NCH(n) - *i >= 3);
1938
1939 expression = ast_for_expr(c, CHILD(n, *i));
1940 if (!expression)
1941 return 0;
1942 *key = expression;
1943
1944 REQ(CHILD(n, *i + 1), COLON);
1945
1946 expression = ast_for_expr(c, CHILD(n, *i + 2));
1947 if (!expression)
1948 return 0;
1949 *value = expression;
1950
1951 *i += 3;
1952 }
1953 return 1;
1954}
1955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001957ast_for_dictcomp(struct compiling *c, const node *n)
1958{
1959 expr_ty key, value;
1960 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001961 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001963 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001964 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001965 assert(key);
1966 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001968 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001969 if (!comps)
1970 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971
Guido van Rossum992d4a32007-07-11 13:09:30 +00001972 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1973}
1974
1975static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001976ast_for_dictdisplay(struct compiling *c, const node *n)
1977{
1978 int i;
1979 int j;
1980 int size;
1981 asdl_seq *keys, *values;
1982
1983 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1984 keys = _Py_asdl_seq_new(size, c->c_arena);
1985 if (!keys)
1986 return NULL;
1987
1988 values = _Py_asdl_seq_new(size, c->c_arena);
1989 if (!values)
1990 return NULL;
1991
1992 j = 0;
1993 for (i = 0; i < NCH(n); i++) {
1994 expr_ty key, value;
1995
1996 if (!ast_for_dictelement(c, n, &i, &key, &value))
1997 return NULL;
1998 asdl_seq_SET(keys, j, key);
1999 asdl_seq_SET(values, j, value);
2000
2001 j++;
2002 }
2003 keys->size = j;
2004 values->size = j;
2005 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2006}
2007
2008static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002009ast_for_genexp(struct compiling *c, const node *n)
2010{
2011 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002012 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002013}
2014
2015static expr_ty
2016ast_for_listcomp(struct compiling *c, const node *n)
2017{
2018 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002019 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002020}
2021
2022static expr_ty
2023ast_for_setcomp(struct compiling *c, const node *n)
2024{
2025 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002026 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002027}
2028
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002029static expr_ty
2030ast_for_setdisplay(struct compiling *c, const node *n)
2031{
2032 int i;
2033 int size;
2034 asdl_seq *elts;
2035
2036 assert(TYPE(n) == (dictorsetmaker));
2037 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2038 elts = _Py_asdl_seq_new(size, c->c_arena);
2039 if (!elts)
2040 return NULL;
2041 for (i = 0; i < NCH(n); i += 2) {
2042 expr_ty expression;
2043 expression = ast_for_expr(c, CHILD(n, i));
2044 if (!expression)
2045 return NULL;
2046 asdl_seq_SET(elts, i / 2, expression);
2047 }
2048 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2049}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002050
2051static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052ast_for_atom(struct compiling *c, const node *n)
2053{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002054 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2055 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002056 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 */
2058 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002061 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002062 PyObject *name;
2063 const char *s = STR(ch);
2064 size_t len = strlen(s);
2065 if (len >= 4 && len <= 5) {
2066 if (!strcmp(s, "None"))
2067 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2068 if (!strcmp(s, "True"))
2069 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2070 if (!strcmp(s, "False"))
2071 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2072 }
2073 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002074 if (!name)
2075 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002076 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002077 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2078 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002080 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002081 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002082 const char *errtype = NULL;
2083 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2084 errtype = "unicode error";
2085 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2086 errtype = "value error";
2087 if (errtype) {
2088 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002089 PyObject *type, *value, *tback, *errstr;
2090 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002091 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002092 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002093 char *s = _PyUnicode_AsString(errstr);
2094 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002095 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002096 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002097 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002098 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002099 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002100 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002101 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002102 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002103 Py_XDECREF(tback);
2104 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002105 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002106 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002107 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 }
2109 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002110 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002111 if (!pynum)
2112 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002113
Victor Stinner43d81952013-07-17 00:57:58 +02002114 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2115 Py_DECREF(pynum);
2116 return NULL;
2117 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002118 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 }
Georg Brandldde00282007-03-18 19:01:53 +00002120 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002121 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002123 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124
Thomas Wouters89f507f2006-12-13 04:49:30 +00002125 if (TYPE(ch) == RPAR)
2126 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127
Thomas Wouters89f507f2006-12-13 04:49:30 +00002128 if (TYPE(ch) == yield_expr)
2129 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002132 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002133 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002134
Nick Coghlan650f0d02007-04-15 12:05:43 +00002135 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002137 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138
Thomas Wouters89f507f2006-12-13 04:49:30 +00002139 if (TYPE(ch) == RSQB)
2140 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141
Nick Coghlan650f0d02007-04-15 12:05:43 +00002142 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2144 asdl_seq *elts = seq_for_testlist(c, ch);
2145 if (!elts)
2146 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002147
Thomas Wouters89f507f2006-12-13 04:49:30 +00002148 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2149 }
2150 else
2151 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002153 /* dictorsetmaker: ( ((test ':' test | '**' test)
2154 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2155 * ((test | '*' test)
2156 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002157 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002158 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002159 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002160 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002161 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002162 }
2163 else {
2164 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2165 if (NCH(ch) == 1 ||
2166 (NCH(ch) > 1 &&
2167 TYPE(CHILD(ch, 1)) == COMMA)) {
2168 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002169 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002170 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002171 else if (NCH(ch) > 1 &&
2172 TYPE(CHILD(ch, 1)) == comp_for) {
2173 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002174 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002175 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002176 else if (NCH(ch) > 3 - is_dict &&
2177 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2178 /* It's a dictionary comprehension. */
2179 if (is_dict) {
2180 ast_error(c, n, "dict unpacking cannot be used in "
2181 "dict comprehension");
2182 return NULL;
2183 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002184 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002185 }
2186 else {
2187 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002188 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002189 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002190 if (res) {
2191 res->lineno = LINENO(n);
2192 res->col_offset = n->n_col_offset;
2193 }
2194 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002198 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 }
2201}
2202
2203static slice_ty
2204ast_for_slice(struct compiling *c, const node *n)
2205{
2206 node *ch;
2207 expr_ty lower = NULL, upper = NULL, step = NULL;
2208
2209 REQ(n, subscript);
2210
2211 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002212 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 sliceop: ':' [test]
2214 */
2215 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 if (NCH(n) == 1 && TYPE(ch) == test) {
2217 /* 'step' variable hold no significance in terms of being used over
2218 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 if (!step)
2221 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222
Thomas Wouters89f507f2006-12-13 04:49:30 +00002223 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 }
2225
2226 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002227 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 if (!lower)
2229 return NULL;
2230 }
2231
2232 /* If there's an upper bound it's in the second or third position. */
2233 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002234 if (NCH(n) > 1) {
2235 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236
Thomas Wouters89f507f2006-12-13 04:49:30 +00002237 if (TYPE(n2) == test) {
2238 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 if (!upper)
2240 return NULL;
2241 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002242 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002244 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245
Thomas Wouters89f507f2006-12-13 04:49:30 +00002246 if (TYPE(n2) == test) {
2247 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 if (!upper)
2249 return NULL;
2250 }
2251 }
2252
2253 ch = CHILD(n, NCH(n) - 1);
2254 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002255 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002256 ch = CHILD(ch, 1);
2257 if (TYPE(ch) == test) {
2258 step = ast_for_expr(c, ch);
2259 if (!step)
2260 return NULL;
2261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 }
2263 }
2264
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002265 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266}
2267
2268static expr_ty
2269ast_for_binop(struct compiling *c, const node *n)
2270{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002271 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002273 BinOp(BinOp(A, op, B), op, C).
2274 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Guido van Rossumd8faa362007-04-27 19:54:29 +00002276 int i, nops;
2277 expr_ty expr1, expr2, result;
2278 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279
Guido van Rossumd8faa362007-04-27 19:54:29 +00002280 expr1 = ast_for_expr(c, CHILD(n, 0));
2281 if (!expr1)
2282 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Guido van Rossumd8faa362007-04-27 19:54:29 +00002284 expr2 = ast_for_expr(c, CHILD(n, 2));
2285 if (!expr2)
2286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287
Guido van Rossumd8faa362007-04-27 19:54:29 +00002288 newoperator = get_operator(CHILD(n, 1));
2289 if (!newoperator)
2290 return NULL;
2291
2292 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2293 c->c_arena);
2294 if (!result)
2295 return NULL;
2296
2297 nops = (NCH(n) - 1) / 2;
2298 for (i = 1; i < nops; i++) {
2299 expr_ty tmp_result, tmp;
2300 const node* next_oper = CHILD(n, i * 2 + 1);
2301
2302 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002303 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 return NULL;
2305
Guido van Rossumd8faa362007-04-27 19:54:29 +00002306 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2307 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 return NULL;
2309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311 LINENO(next_oper), next_oper->n_col_offset,
2312 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 return NULL;
2315 result = tmp_result;
2316 }
2317 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318}
2319
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002320static expr_ty
2321ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002324 subscriptlist: subscript (',' subscript)* [',']
2325 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2326 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002327 REQ(n, trailer);
2328 if (TYPE(CHILD(n, 0)) == LPAR) {
2329 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002330 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002331 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002332 else
2333 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002334 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002335 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002336 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2337 if (!attr_id)
2338 return NULL;
2339 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002340 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002341 }
2342 else {
2343 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002344 REQ(CHILD(n, 2), RSQB);
2345 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002346 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002347 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2348 if (!slc)
2349 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002350 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2351 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002352 }
2353 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002355 by treating the sequence as a tuple literal if there are
2356 no slice features.
2357 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002358 int j;
2359 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002360 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002361 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002362 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002363 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364 if (!slices)
2365 return NULL;
2366 for (j = 0; j < NCH(n); j += 2) {
2367 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002368 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002369 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002370 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002371 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002372 asdl_seq_SET(slices, j / 2, slc);
2373 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002374 if (!simple) {
2375 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002376 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002377 }
2378 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002379 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002380 if (!elts)
2381 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002382 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2383 slc = (slice_ty)asdl_seq_GET(slices, j);
2384 assert(slc->kind == Index_kind && slc->v.Index.value);
2385 asdl_seq_SET(elts, j, slc->v.Index.value);
2386 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002387 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002388 if (!e)
2389 return NULL;
2390 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002391 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002392 }
2393 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002394}
2395
2396static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002397ast_for_factor(struct compiling *c, const node *n)
2398{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002399 expr_ty expression;
2400
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002401 expression = ast_for_expr(c, CHILD(n, 1));
2402 if (!expression)
2403 return NULL;
2404
2405 switch (TYPE(CHILD(n, 0))) {
2406 case PLUS:
2407 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2408 c->c_arena);
2409 case MINUS:
2410 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2411 c->c_arena);
2412 case TILDE:
2413 return UnaryOp(Invert, expression, LINENO(n),
2414 n->n_col_offset, c->c_arena);
2415 }
2416 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2417 TYPE(CHILD(n, 0)));
2418 return NULL;
2419}
2420
2421static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002422ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002423{
Yury Selivanov75445082015-05-11 22:57:16 -04002424 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002425 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002426
2427 REQ(n, atom_expr);
2428 nch = NCH(n);
2429
2430 if (TYPE(CHILD(n, 0)) == AWAIT) {
2431 start = 1;
2432 assert(nch > 1);
2433 }
2434
2435 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002436 if (!e)
2437 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002438 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002439 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002440 if (start && nch == 2) {
2441 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2442 }
2443
2444 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002445 node *ch = CHILD(n, i);
2446 if (TYPE(ch) != trailer)
2447 break;
2448 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002449 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002450 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002451 tmp->lineno = e->lineno;
2452 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002453 e = tmp;
2454 }
Yury Selivanov75445082015-05-11 22:57:16 -04002455
2456 if (start) {
2457 /* there was an AWAIT */
2458 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2459 }
2460 else {
2461 return e;
2462 }
2463}
2464
2465static expr_ty
2466ast_for_power(struct compiling *c, const node *n)
2467{
2468 /* power: atom trailer* ('**' factor)*
2469 */
2470 expr_ty e;
2471 REQ(n, power);
2472 e = ast_for_atom_expr(c, CHILD(n, 0));
2473 if (!e)
2474 return NULL;
2475 if (NCH(n) == 1)
2476 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002477 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2478 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002479 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002480 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002481 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002482 }
2483 return e;
2484}
2485
Guido van Rossum0368b722007-05-11 16:50:42 +00002486static expr_ty
2487ast_for_starred(struct compiling *c, const node *n)
2488{
2489 expr_ty tmp;
2490 REQ(n, star_expr);
2491
2492 tmp = ast_for_expr(c, CHILD(n, 1));
2493 if (!tmp)
2494 return NULL;
2495
2496 /* The Load context is changed later. */
2497 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2498}
2499
2500
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501/* Do not name a variable 'expr'! Will cause a compile error.
2502*/
2503
2504static expr_ty
2505ast_for_expr(struct compiling *c, const node *n)
2506{
2507 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002508 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002509 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 and_test: not_test ('and' not_test)*
2512 not_test: 'not' not_test | comparison
2513 comparison: expr (comp_op expr)*
2514 expr: xor_expr ('|' xor_expr)*
2515 xor_expr: and_expr ('^' and_expr)*
2516 and_expr: shift_expr ('&' shift_expr)*
2517 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2518 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002519 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002521 power: atom_expr ['**' factor]
2522 atom_expr: [AWAIT] atom trailer*
2523 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 */
2525
2526 asdl_seq *seq;
2527 int i;
2528
2529 loop:
2530 switch (TYPE(n)) {
2531 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002532 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002533 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002534 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002536 else if (NCH(n) > 1)
2537 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002538 /* Fallthrough */
2539 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 case and_test:
2541 if (NCH(n) == 1) {
2542 n = CHILD(n, 0);
2543 goto loop;
2544 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002545 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 if (!seq)
2547 return NULL;
2548 for (i = 0; i < NCH(n); i += 2) {
2549 expr_ty e = ast_for_expr(c, CHILD(n, i));
2550 if (!e)
2551 return NULL;
2552 asdl_seq_SET(seq, i / 2, e);
2553 }
2554 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002555 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2556 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002557 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002558 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 case not_test:
2560 if (NCH(n) == 1) {
2561 n = CHILD(n, 0);
2562 goto loop;
2563 }
2564 else {
2565 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2566 if (!expression)
2567 return NULL;
2568
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002569 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2570 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 }
2572 case comparison:
2573 if (NCH(n) == 1) {
2574 n = CHILD(n, 0);
2575 goto loop;
2576 }
2577 else {
2578 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002579 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002580 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002581 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 if (!ops)
2583 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002584 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 return NULL;
2587 }
2588 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002589 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002591 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002592 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002594 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595
2596 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002597 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002599 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002601 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 asdl_seq_SET(cmps, i / 2, expression);
2603 }
2604 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002605 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002607 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002609 return Compare(expression, ops, cmps, LINENO(n),
2610 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 }
2612 break;
2613
Guido van Rossum0368b722007-05-11 16:50:42 +00002614 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 /* The next five cases all handle BinOps. The main body of code
2617 is the same in each case, but the switch turned inside out to
2618 reuse the code for each type of operator.
2619 */
2620 case expr:
2621 case xor_expr:
2622 case and_expr:
2623 case shift_expr:
2624 case arith_expr:
2625 case term:
2626 if (NCH(n) == 1) {
2627 n = CHILD(n, 0);
2628 goto loop;
2629 }
2630 return ast_for_binop(c, n);
2631 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002632 node *an = NULL;
2633 node *en = NULL;
2634 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002635 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002636 if (NCH(n) > 1)
2637 an = CHILD(n, 1); /* yield_arg */
2638 if (an) {
2639 en = CHILD(an, NCH(an) - 1);
2640 if (NCH(an) == 2) {
2641 is_from = 1;
2642 exp = ast_for_expr(c, en);
2643 }
2644 else
2645 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002646 if (!exp)
2647 return NULL;
2648 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002649 if (is_from)
2650 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2651 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002652 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002653 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 if (NCH(n) == 1) {
2655 n = CHILD(n, 0);
2656 goto loop;
2657 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002658 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002659 case power:
2660 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002662 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 return NULL;
2664 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002665 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 return NULL;
2667}
2668
2669static expr_ty
2670ast_for_call(struct compiling *c, const node *n, expr_ty func)
2671{
2672 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002673 arglist: argument (',' argument)* [',']
2674 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 */
2676
2677 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002678 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002679 asdl_seq *args;
2680 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681
2682 REQ(n, arglist);
2683
2684 nargs = 0;
2685 nkeywords = 0;
2686 ngens = 0;
2687 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002688 node *ch = CHILD(n, i);
2689 if (TYPE(ch) == argument) {
2690 if (NCH(ch) == 1)
2691 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002692 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002693 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002694 else if (TYPE(CHILD(ch, 0)) == STAR)
2695 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002697 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002698 nkeywords++;
2699 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 }
2701 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002702 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002703 "if not sole argument");
2704 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 }
2706
2707 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002708 ast_error(c, n, "more than 255 arguments");
2709 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 }
2711
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002712 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002714 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002715 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002717 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718
2719 nargs = 0; /* positional arguments + iterable argument unpackings */
2720 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2721 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002723 node *ch = CHILD(n, i);
2724 if (TYPE(ch) == argument) {
2725 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002726 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002728 /* a positional argument */
2729 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 if (ndoublestars) {
2731 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002732 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002733 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002734 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002735 else {
2736 ast_error(c, chch,
2737 "positional argument follows "
2738 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002739 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002740 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002741 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002742 e = ast_for_expr(c, chch);
2743 if (!e)
2744 return NULL;
2745 asdl_seq_SET(args, nargs++, e);
2746 }
2747 else if (TYPE(chch) == STAR) {
2748 /* an iterable argument unpacking */
2749 expr_ty starred;
2750 if (ndoublestars) {
2751 ast_error(c, chch,
2752 "iterable argument unpacking follows "
2753 "keyword argument unpacking");
2754 return NULL;
2755 }
2756 e = ast_for_expr(c, CHILD(ch, 1));
2757 if (!e)
2758 return NULL;
2759 starred = Starred(e, Load, LINENO(chch),
2760 chch->n_col_offset,
2761 c->c_arena);
2762 if (!starred)
2763 return NULL;
2764 asdl_seq_SET(args, nargs++, starred);
2765
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002766 }
2767 else if (TYPE(chch) == DOUBLESTAR) {
2768 /* a keyword argument unpacking */
2769 keyword_ty kw;
2770 i++;
2771 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002773 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774 kw = keyword(NULL, e, c->c_arena);
2775 asdl_seq_SET(keywords, nkeywords++, kw);
2776 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002778 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002779 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002780 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002782 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002783 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002785 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002786 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002787 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002788 identifier key, tmp;
2789 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002791 /* chch is test, but must be an identifier? */
2792 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 /* f(lambda x: x[0] = 3) ends up getting parsed with
2796 * LHS test = lambda x: x[0], and RHS test = 3.
2797 * SF bug 132313 points out that complaining about a keyword
2798 * then is very confusing.
2799 */
2800 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 ast_error(c, chch,
2802 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002803 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002804 }
2805 else if (e->kind != Name_kind) {
2806 ast_error(c, chch,
2807 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002808 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002809 }
2810 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002811 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002813 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002814 for (k = 0; k < nkeywords; k++) {
2815 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002816 if (tmp && !PyUnicode_Compare(tmp, key)) {
2817 ast_error(c, chch,
2818 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002819 return NULL;
2820 }
2821 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002822 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002824 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002825 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002827 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002828 asdl_seq_SET(keywords, nkeywords++, kw);
2829 }
2830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 }
2832
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002833 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834}
2835
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002837ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002839 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002840 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002842 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002843 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002844 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002845 }
2846 else {
2847 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002848 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002849 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002851 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 else {
2853 asdl_seq *tmp = seq_for_testlist(c, n);
2854 if (!tmp)
2855 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002856 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002858}
2859
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860static stmt_ty
2861ast_for_expr_stmt(struct compiling *c, const node *n)
2862{
2863 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002866 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002867 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002868 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 test: ... here starts the operator precendence dance
2870 */
2871
2872 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002873 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 if (!e)
2875 return NULL;
2876
Thomas Wouters89f507f2006-12-13 04:49:30 +00002877 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 }
2879 else if (TYPE(CHILD(n, 1)) == augassign) {
2880 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002881 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002882 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 if (!expr1)
2886 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002887 if(!set_context(c, expr1, Store, ch))
2888 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002889 /* set_context checks that most expressions are not the left side.
2890 Augmented assignments can only have a name, a subscript, or an
2891 attribute on the left, though, so we have to explicitly check for
2892 those. */
2893 switch (expr1->kind) {
2894 case Name_kind:
2895 case Attribute_kind:
2896 case Subscript_kind:
2897 break;
2898 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002899 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002900 return NULL;
2901 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902
Thomas Wouters89f507f2006-12-13 04:49:30 +00002903 ch = CHILD(n, 2);
2904 if (TYPE(ch) == testlist)
2905 expr2 = ast_for_testlist(c, ch);
2906 else
2907 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002908 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 return NULL;
2910
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002911 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002912 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 return NULL;
2914
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 }
2917 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002918 int i;
2919 asdl_seq *targets;
2920 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921 expr_ty expression;
2922
Thomas Wouters89f507f2006-12-13 04:49:30 +00002923 /* a normal assignment */
2924 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002925 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002926 if (!targets)
2927 return NULL;
2928 for (i = 0; i < NCH(n) - 2; i += 2) {
2929 expr_ty e;
2930 node *ch = CHILD(n, i);
2931 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002932 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002933 return NULL;
2934 }
2935 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002937 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002939 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002940 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942
Thomas Wouters89f507f2006-12-13 04:49:30 +00002943 asdl_seq_SET(targets, i / 2, e);
2944 }
2945 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002946 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002947 expression = ast_for_testlist(c, value);
2948 else
2949 expression = ast_for_expr(c, value);
2950 if (!expression)
2951 return NULL;
2952 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954}
2955
Benjamin Peterson78565b22009-06-28 19:19:51 +00002956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002958ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959{
2960 asdl_seq *seq;
2961 int i;
2962 expr_ty e;
2963
2964 REQ(n, exprlist);
2965
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002966 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002968 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002970 e = ast_for_expr(c, CHILD(n, i));
2971 if (!e)
2972 return NULL;
2973 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002974 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 }
2977 return seq;
2978}
2979
2980static stmt_ty
2981ast_for_del_stmt(struct compiling *c, const node *n)
2982{
2983 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 /* del_stmt: 'del' exprlist */
2986 REQ(n, del_stmt);
2987
2988 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2989 if (!expr_list)
2990 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002991 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992}
2993
2994static stmt_ty
2995ast_for_flow_stmt(struct compiling *c, const node *n)
2996{
2997 /*
2998 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2999 | yield_stmt
3000 break_stmt: 'break'
3001 continue_stmt: 'continue'
3002 return_stmt: 'return' [testlist]
3003 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003004 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005 raise_stmt: 'raise' [test [',' test [',' test]]]
3006 */
3007 node *ch;
3008
3009 REQ(n, flow_stmt);
3010 ch = CHILD(n, 0);
3011 switch (TYPE(ch)) {
3012 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003013 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003015 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003017 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3018 if (!exp)
3019 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003020 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 }
3022 case return_stmt:
3023 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003024 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003026 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 if (!expression)
3028 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003029 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030 }
3031 case raise_stmt:
3032 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003033 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3034 else if (NCH(ch) >= 2) {
3035 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3037 if (!expression)
3038 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003039 if (NCH(ch) == 4) {
3040 cause = ast_for_expr(c, CHILD(ch, 3));
3041 if (!cause)
3042 return NULL;
3043 }
3044 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 }
3046 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003047 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 "unexpected flow_stmt: %d", TYPE(ch));
3049 return NULL;
3050 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003051
3052 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3053 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054}
3055
3056static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003057alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058{
3059 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003060 import_as_name: NAME ['as' NAME]
3061 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062 dotted_name: NAME ('.' NAME)*
3063 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003064 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066 loop:
3067 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003068 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003069 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003070 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003071 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003072 if (!name)
3073 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003074 if (NCH(n) == 3) {
3075 node *str_node = CHILD(n, 2);
3076 str = NEW_IDENTIFIER(str_node);
3077 if (!str)
3078 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003079 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003080 return NULL;
3081 }
3082 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003083 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003084 return NULL;
3085 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003086 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003087 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088 case dotted_as_name:
3089 if (NCH(n) == 1) {
3090 n = CHILD(n, 0);
3091 goto loop;
3092 }
3093 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003094 node *asname_node = CHILD(n, 2);
3095 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003096 if (!a)
3097 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003098 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003099 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003100 if (!a->asname)
3101 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003102 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003103 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 return a;
3105 }
3106 break;
3107 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003108 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003109 node *name_node = CHILD(n, 0);
3110 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003111 if (!name)
3112 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003113 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003114 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003115 return alias(name, NULL, c->c_arena);
3116 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 else {
3118 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003119 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003120 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123
3124 len = 0;
3125 for (i = 0; i < NCH(n); i += 2)
3126 /* length of string plus one for the dot */
3127 len += strlen(STR(CHILD(n, i))) + 1;
3128 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003129 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 if (!str)
3131 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003132 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 if (!s)
3134 return NULL;
3135 for (i = 0; i < NCH(n); i += 2) {
3136 char *sch = STR(CHILD(n, i));
3137 strcpy(s, STR(CHILD(n, i)));
3138 s += strlen(sch);
3139 *s++ = '.';
3140 }
3141 --s;
3142 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3144 PyBytes_GET_SIZE(str),
3145 NULL);
3146 Py_DECREF(str);
3147 if (!uni)
3148 return NULL;
3149 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003150 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003151 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3152 Py_DECREF(str);
3153 return NULL;
3154 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003155 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 }
3157 break;
3158 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003159 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003160 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3161 Py_DECREF(str);
3162 return NULL;
3163 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003164 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003166 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 "unexpected import name: %d", TYPE(n));
3168 return NULL;
3169 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003170
3171 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 return NULL;
3173}
3174
3175static stmt_ty
3176ast_for_import_stmt(struct compiling *c, const node *n)
3177{
3178 /*
3179 import_stmt: import_name | import_from
3180 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003181 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3182 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003184 int lineno;
3185 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186 int i;
3187 asdl_seq *aliases;
3188
3189 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003190 lineno = LINENO(n);
3191 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003193 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003195 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003196 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003197 if (!aliases)
3198 return NULL;
3199 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003200 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003201 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003203 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003205 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003207 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003209 int idx, ndots = 0;
3210 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003211 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003213 /* Count the number of dots (for relative imports) and check for the
3214 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003215 for (idx = 1; idx < NCH(n); idx++) {
3216 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003217 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3218 if (!mod)
3219 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003220 idx++;
3221 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003222 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003224 ndots += 3;
3225 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003226 } else if (TYPE(CHILD(n, idx)) != DOT) {
3227 break;
3228 }
3229 ndots++;
3230 }
3231 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003232 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003233 case STAR:
3234 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003235 n = CHILD(n, idx);
3236 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003237 break;
3238 case LPAR:
3239 /* from ... import (x, y, z) */
3240 n = CHILD(n, idx + 1);
3241 n_children = NCH(n);
3242 break;
3243 case import_as_names:
3244 /* from ... import x, y, z */
3245 n = CHILD(n, idx);
3246 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003247 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003248 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 " surrounding parentheses");
3250 return NULL;
3251 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003252 break;
3253 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003254 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003255 return NULL;
3256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003258 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003259 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261
3262 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003263 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003264 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003265 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003267 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003269 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003270 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003271 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003272 if (!import_alias)
3273 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003274 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003275 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003276 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003277 if (mod != NULL)
3278 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003279 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003280 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281 }
Neal Norwitz79792652005-11-14 04:25:03 +00003282 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283 "unknown import statement: starts with command '%s'",
3284 STR(CHILD(n, 0)));
3285 return NULL;
3286}
3287
3288static stmt_ty
3289ast_for_global_stmt(struct compiling *c, const node *n)
3290{
3291 /* global_stmt: 'global' NAME (',' NAME)* */
3292 identifier name;
3293 asdl_seq *s;
3294 int i;
3295
3296 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003297 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003299 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003301 name = NEW_IDENTIFIER(CHILD(n, i));
3302 if (!name)
3303 return NULL;
3304 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003306 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307}
3308
3309static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003310ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3311{
3312 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3313 identifier name;
3314 asdl_seq *s;
3315 int i;
3316
3317 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003318 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003319 if (!s)
3320 return NULL;
3321 for (i = 1; i < NCH(n); i += 2) {
3322 name = NEW_IDENTIFIER(CHILD(n, i));
3323 if (!name)
3324 return NULL;
3325 asdl_seq_SET(s, i / 2, name);
3326 }
3327 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3328}
3329
3330static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331ast_for_assert_stmt(struct compiling *c, const node *n)
3332{
3333 /* assert_stmt: 'assert' test [',' test] */
3334 REQ(n, assert_stmt);
3335 if (NCH(n) == 2) {
3336 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3337 if (!expression)
3338 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003339 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340 }
3341 else if (NCH(n) == 4) {
3342 expr_ty expr1, expr2;
3343
3344 expr1 = ast_for_expr(c, CHILD(n, 1));
3345 if (!expr1)
3346 return NULL;
3347 expr2 = ast_for_expr(c, CHILD(n, 3));
3348 if (!expr2)
3349 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350
Thomas Wouters89f507f2006-12-13 04:49:30 +00003351 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 }
Neal Norwitz79792652005-11-14 04:25:03 +00003353 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 "improper number of parts to 'assert' statement: %d",
3355 NCH(n));
3356 return NULL;
3357}
3358
3359static asdl_seq *
3360ast_for_suite(struct compiling *c, const node *n)
3361{
3362 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003363 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 stmt_ty s;
3365 int i, total, num, end, pos = 0;
3366 node *ch;
3367
3368 REQ(n, suite);
3369
3370 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003371 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003375 n = CHILD(n, 0);
3376 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003378 */
3379 end = NCH(n) - 1;
3380 if (TYPE(CHILD(n, end - 1)) == SEMI)
3381 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003383 for (i = 0; i < end; i += 2) {
3384 ch = CHILD(n, i);
3385 s = ast_for_stmt(c, ch);
3386 if (!s)
3387 return NULL;
3388 asdl_seq_SET(seq, pos++, s);
3389 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390 }
3391 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003392 for (i = 2; i < (NCH(n) - 1); i++) {
3393 ch = CHILD(n, i);
3394 REQ(ch, stmt);
3395 num = num_stmts(ch);
3396 if (num == 1) {
3397 /* small_stmt or compound_stmt with only one child */
3398 s = ast_for_stmt(c, ch);
3399 if (!s)
3400 return NULL;
3401 asdl_seq_SET(seq, pos++, s);
3402 }
3403 else {
3404 int j;
3405 ch = CHILD(ch, 0);
3406 REQ(ch, simple_stmt);
3407 for (j = 0; j < NCH(ch); j += 2) {
3408 /* statement terminates with a semi-colon ';' */
3409 if (NCH(CHILD(ch, j)) == 0) {
3410 assert((j + 1) == NCH(ch));
3411 break;
3412 }
3413 s = ast_for_stmt(c, CHILD(ch, j));
3414 if (!s)
3415 return NULL;
3416 asdl_seq_SET(seq, pos++, s);
3417 }
3418 }
3419 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 }
3421 assert(pos == seq->size);
3422 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423}
3424
3425static stmt_ty
3426ast_for_if_stmt(struct compiling *c, const node *n)
3427{
3428 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3429 ['else' ':' suite]
3430 */
3431 char *s;
3432
3433 REQ(n, if_stmt);
3434
3435 if (NCH(n) == 4) {
3436 expr_ty expression;
3437 asdl_seq *suite_seq;
3438
3439 expression = ast_for_expr(c, CHILD(n, 1));
3440 if (!expression)
3441 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003443 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445
Guido van Rossumd8faa362007-04-27 19:54:29 +00003446 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3447 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003449
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 s = STR(CHILD(n, 4));
3451 /* s[2], the third character in the string, will be
3452 's' for el_s_e, or
3453 'i' for el_i_f
3454 */
3455 if (s[2] == 's') {
3456 expr_ty expression;
3457 asdl_seq *seq1, *seq2;
3458
3459 expression = ast_for_expr(c, CHILD(n, 1));
3460 if (!expression)
3461 return NULL;
3462 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003463 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 return NULL;
3465 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003466 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 return NULL;
3468
Guido van Rossumd8faa362007-04-27 19:54:29 +00003469 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3470 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 }
3472 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003473 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003474 expr_ty expression;
3475 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 asdl_seq *orelse = NULL;
3477 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 /* must reference the child n_elif+1 since 'else' token is third,
3479 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3481 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3482 has_else = 1;
3483 n_elif -= 3;
3484 }
3485 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486
Thomas Wouters89f507f2006-12-13 04:49:30 +00003487 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003488 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003490 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003491 if (!orelse)
3492 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003494 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003496 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3497 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003499 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3500 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 asdl_seq_SET(orelse, 0,
3504 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003505 LINENO(CHILD(n, NCH(n) - 6)),
3506 CHILD(n, NCH(n) - 6)->n_col_offset,
3507 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003508 /* the just-created orelse handled the last elif */
3509 n_elif--;
3510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511
Thomas Wouters89f507f2006-12-13 04:49:30 +00003512 for (i = 0; i < n_elif; i++) {
3513 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003514 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003515 if (!newobj)
3516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003518 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003520 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003521 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523
Thomas Wouters89f507f2006-12-13 04:49:30 +00003524 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003526 LINENO(CHILD(n, off)),
3527 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003528 orelse = newobj;
3529 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003530 expression = ast_for_expr(c, CHILD(n, 1));
3531 if (!expression)
3532 return NULL;
3533 suite_seq = ast_for_suite(c, CHILD(n, 3));
3534 if (!suite_seq)
3535 return NULL;
3536 return If(expression, suite_seq, orelse,
3537 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003539
3540 PyErr_Format(PyExc_SystemError,
3541 "unexpected token in 'if' statement: %s", s);
3542 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543}
3544
3545static stmt_ty
3546ast_for_while_stmt(struct compiling *c, const node *n)
3547{
3548 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3549 REQ(n, while_stmt);
3550
3551 if (NCH(n) == 4) {
3552 expr_ty expression;
3553 asdl_seq *suite_seq;
3554
3555 expression = ast_for_expr(c, CHILD(n, 1));
3556 if (!expression)
3557 return NULL;
3558 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003559 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003561 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562 }
3563 else if (NCH(n) == 7) {
3564 expr_ty expression;
3565 asdl_seq *seq1, *seq2;
3566
3567 expression = ast_for_expr(c, CHILD(n, 1));
3568 if (!expression)
3569 return NULL;
3570 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003571 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572 return NULL;
3573 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003574 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575 return NULL;
3576
Thomas Wouters89f507f2006-12-13 04:49:30 +00003577 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003579
3580 PyErr_Format(PyExc_SystemError,
3581 "wrong number of tokens for 'while' statement: %d",
3582 NCH(n));
3583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584}
3585
3586static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003587ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003589 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003591 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003592 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3594 REQ(n, for_stmt);
3595
3596 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003597 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 if (!seq)
3599 return NULL;
3600 }
3601
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003602 node_target = CHILD(n, 1);
3603 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003604 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003606 /* Check the # of children rather than the length of _target, since
3607 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003608 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003609 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003610 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003612 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003614 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003615 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616 return NULL;
3617 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003618 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 return NULL;
3620
Yury Selivanov75445082015-05-11 22:57:16 -04003621 if (is_async)
3622 return AsyncFor(target, expression, suite_seq, seq,
3623 LINENO(n), n->n_col_offset,
3624 c->c_arena);
3625 else
3626 return For(target, expression, suite_seq, seq,
3627 LINENO(n), n->n_col_offset,
3628 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003629}
3630
3631static excepthandler_ty
3632ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3633{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003634 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635 REQ(exc, except_clause);
3636 REQ(body, suite);
3637
3638 if (NCH(exc) == 1) {
3639 asdl_seq *suite_seq = ast_for_suite(c, body);
3640 if (!suite_seq)
3641 return NULL;
3642
Neal Norwitzad74aa82008-03-31 05:14:30 +00003643 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003644 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 }
3646 else if (NCH(exc) == 2) {
3647 expr_ty expression;
3648 asdl_seq *suite_seq;
3649
3650 expression = ast_for_expr(c, CHILD(exc, 1));
3651 if (!expression)
3652 return NULL;
3653 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003654 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 return NULL;
3656
Neal Norwitzad74aa82008-03-31 05:14:30 +00003657 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003658 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 }
3660 else if (NCH(exc) == 4) {
3661 asdl_seq *suite_seq;
3662 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003663 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003664 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003666 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003667 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003669 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 return NULL;
3671 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003672 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 return NULL;
3674
Neal Norwitzad74aa82008-03-31 05:14:30 +00003675 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003676 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003678
3679 PyErr_Format(PyExc_SystemError,
3680 "wrong number of children for 'except' clause: %d",
3681 NCH(exc));
3682 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683}
3684
3685static stmt_ty
3686ast_for_try_stmt(struct compiling *c, const node *n)
3687{
Neal Norwitzf599f422005-12-17 21:33:47 +00003688 const int nch = NCH(n);
3689 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003690 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003691
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003692 REQ(n, try_stmt);
3693
Neal Norwitzf599f422005-12-17 21:33:47 +00003694 body = ast_for_suite(c, CHILD(n, 2));
3695 if (body == NULL)
3696 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697
Neal Norwitzf599f422005-12-17 21:33:47 +00003698 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3699 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3700 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3701 /* we can assume it's an "else",
3702 because nch >= 9 for try-else-finally and
3703 it would otherwise have a type of except_clause */
3704 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3705 if (orelse == NULL)
3706 return NULL;
3707 n_except--;
3708 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709
Neal Norwitzf599f422005-12-17 21:33:47 +00003710 finally = ast_for_suite(c, CHILD(n, nch - 1));
3711 if (finally == NULL)
3712 return NULL;
3713 n_except--;
3714 }
3715 else {
3716 /* we can assume it's an "else",
3717 otherwise it would have a type of except_clause */
3718 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3719 if (orelse == NULL)
3720 return NULL;
3721 n_except--;
3722 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003724 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003725 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 return NULL;
3727 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728
Neal Norwitzf599f422005-12-17 21:33:47 +00003729 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003730 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003731 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003732 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003733 if (handlers == NULL)
3734 return NULL;
3735
3736 for (i = 0; i < n_except; i++) {
3737 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3738 CHILD(n, 5 + i * 3));
3739 if (!e)
3740 return NULL;
3741 asdl_seq_SET(handlers, i, e);
3742 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003743 }
3744
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003745 assert(finally != NULL || asdl_seq_LEN(handlers));
3746 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747}
3748
Georg Brandl0c315622009-05-25 21:10:36 +00003749/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003750static withitem_ty
3751ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003752{
3753 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003754
Georg Brandl0c315622009-05-25 21:10:36 +00003755 REQ(n, with_item);
3756 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003757 if (!context_expr)
3758 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003759 if (NCH(n) == 3) {
3760 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003761
3762 if (!optional_vars) {
3763 return NULL;
3764 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003765 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003766 return NULL;
3767 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003768 }
3769
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003770 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003771}
3772
Georg Brandl0c315622009-05-25 21:10:36 +00003773/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3774static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003775ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003776{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003777 int i, n_items;
3778 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003779
3780 REQ(n, with_stmt);
3781
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003782 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003783 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003784 if (!items)
3785 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003786 for (i = 1; i < NCH(n) - 2; i += 2) {
3787 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3788 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003789 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003790 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003791 }
3792
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003793 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3794 if (!body)
3795 return NULL;
3796
Yury Selivanov75445082015-05-11 22:57:16 -04003797 if (is_async)
3798 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3799 else
3800 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003801}
3802
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003804ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003806 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003807 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003808 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003809 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003810
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811 REQ(n, classdef);
3812
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003813 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814 s = ast_for_suite(c, CHILD(n, 3));
3815 if (!s)
3816 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003817 classname = NEW_IDENTIFIER(CHILD(n, 1));
3818 if (!classname)
3819 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003820 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003821 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003822 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3823 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003825
3826 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003827 s = ast_for_suite(c, CHILD(n,5));
3828 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003829 return NULL;
3830 classname = NEW_IDENTIFIER(CHILD(n, 1));
3831 if (!classname)
3832 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003833 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003834 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003835 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3836 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837 }
3838
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003839 /* class NAME '(' arglist ')' ':' suite */
3840 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003841 {
3842 PyObject *dummy_name;
3843 expr_ty dummy;
3844 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3845 if (!dummy_name)
3846 return NULL;
3847 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3848 call = ast_for_call(c, CHILD(n, 3), dummy);
3849 if (!call)
3850 return NULL;
3851 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003853 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003855 classname = NEW_IDENTIFIER(CHILD(n, 1));
3856 if (!classname)
3857 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003858 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003859 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003860
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003861 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003862 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863}
3864
3865static stmt_ty
3866ast_for_stmt(struct compiling *c, const node *n)
3867{
3868 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003869 assert(NCH(n) == 1);
3870 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003871 }
3872 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003873 assert(num_stmts(n) == 1);
3874 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 }
3876 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003877 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003878 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3879 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003880 */
3881 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882 case expr_stmt:
3883 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 case del_stmt:
3885 return ast_for_del_stmt(c, n);
3886 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003887 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888 case flow_stmt:
3889 return ast_for_flow_stmt(c, n);
3890 case import_stmt:
3891 return ast_for_import_stmt(c, n);
3892 case global_stmt:
3893 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003894 case nonlocal_stmt:
3895 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003896 case assert_stmt:
3897 return ast_for_assert_stmt(c, n);
3898 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003899 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3901 TYPE(n), NCH(n));
3902 return NULL;
3903 }
3904 }
3905 else {
3906 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003907 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003908 */
3909 node *ch = CHILD(n, 0);
3910 REQ(n, compound_stmt);
3911 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003912 case if_stmt:
3913 return ast_for_if_stmt(c, ch);
3914 case while_stmt:
3915 return ast_for_while_stmt(c, ch);
3916 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003917 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 case try_stmt:
3919 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003920 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003921 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003923 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003925 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 case decorated:
3927 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003928 case async_stmt:
3929 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003931 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3933 TYPE(n), NCH(n));
3934 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003935 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 }
3937}
3938
3939static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003940parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003942 const char *end;
3943 long x;
3944 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003945 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003946 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003947
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003948 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003949 errno = 0;
3950 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003951 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003952 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003953 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003954 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003955 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003956 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003957 }
3958 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003959 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003960 if (*end == '\0') {
3961 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003962 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003963 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003964 }
3965 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003966 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003967 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003968 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3969 if (compl.imag == -1.0 && PyErr_Occurred())
3970 return NULL;
3971 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003972 }
3973 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003974 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003975 dx = PyOS_string_to_double(s, NULL, NULL);
3976 if (dx == -1.0 && PyErr_Occurred())
3977 return NULL;
3978 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003979 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980}
3981
3982static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003983decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003985 const char *s, *t;
3986 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003987 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3988 while (s < end && (*s & 0x80)) s++;
3989 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003990 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991}
3992
3993static PyObject *
Eric V. Smith5567f892015-09-21 13:36:09 -04003994decode_unicode(struct compiling *c, const char *s, size_t len, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003996 PyObject *v, *u;
3997 char *buf;
3998 char *p;
3999 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004000
Guido van Rossumd8faa362007-04-27 19:54:29 +00004001 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004002 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004003 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00004004 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00004005 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00004006 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00004007 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4008 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4009 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004010 if (u == NULL)
4011 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00004012 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004013 end = s + len;
4014 while (s < end) {
4015 if (*s == '\\') {
4016 *p++ = *s++;
4017 if (*s & 0x80) {
4018 strcpy(p, "u005c");
4019 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004020 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004021 }
4022 if (*s & 0x80) { /* XXX inefficient */
4023 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004024 int kind;
4025 void *data;
4026 Py_ssize_t len, i;
4027 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004028 if (w == NULL) {
4029 Py_DECREF(u);
4030 return NULL;
4031 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004032 kind = PyUnicode_KIND(w);
4033 data = PyUnicode_DATA(w);
4034 len = PyUnicode_GET_LENGTH(w);
4035 for (i = 0; i < len; i++) {
4036 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4037 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00004038 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004039 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00004040 /* Should be impossible to overflow */
4041 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00004042 Py_DECREF(w);
4043 } else {
4044 *p++ = *s++;
4045 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004046 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 len = p - buf;
4048 s = buf;
4049 }
Eric V. Smith5567f892015-09-21 13:36:09 -04004050 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004051 Py_XDECREF(u);
4052 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053}
4054
Eric V. Smith235a6f02015-09-19 14:51:32 -04004055/* Compile this expression in to an expr_ty. We know that we can
4056 temporarily modify the character before the start of this string
4057 (it's '{'), and we know we can temporarily modify the character
4058 after this string (it is a '}'). Leverage this to create a
4059 sub-string with enough room for us to add parens around the
4060 expression. This is to allow strings with embedded newlines, for
4061 example. */
4062static expr_ty
Eric V. Smith1d44c412015-09-23 07:49:00 -04004063fstring_compile_expr(PyObject *str, Py_ssize_t expr_start,
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004064 Py_ssize_t expr_end, struct compiling *c, const node *n)
4065
Eric V. Smith235a6f02015-09-19 14:51:32 -04004066{
4067 PyCompilerFlags cf;
4068 mod_ty mod;
4069 char *utf_expr;
4070 Py_ssize_t i;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004071 Py_UCS4 end_ch = -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004072 int all_whitespace;
4073 PyObject *sub = NULL;
4074
4075 /* We only decref sub if we allocated it with a PyUnicode_Substring.
4076 decref_sub records that. */
4077 int decref_sub = 0;
4078
4079 assert(str);
4080
Eric V. Smith1d44c412015-09-23 07:49:00 -04004081 assert(expr_start >= 0 && expr_start < PyUnicode_GET_LENGTH(str));
4082 assert(expr_end >= 0 && expr_end < PyUnicode_GET_LENGTH(str));
4083 assert(expr_end >= expr_start);
4084
Martin Panterc2432f62015-10-07 11:15:15 +00004085 /* There has to be at least one character on each side of the
Eric V. Smith1d44c412015-09-23 07:49:00 -04004086 expression inside this str. This will have been caught before
4087 we're called. */
4088 assert(expr_start >= 1);
4089 assert(expr_end <= PyUnicode_GET_LENGTH(str)-1);
4090
Eric V. Smith235a6f02015-09-19 14:51:32 -04004091 /* If the substring is all whitespace, it's an error. We need to
4092 catch this here, and not when we call PyParser_ASTFromString,
4093 because turning the expression '' in to '()' would go from
4094 being invalid to valid. */
4095 /* Note that this code says an empty string is all
4096 whitespace. That's important. There's a test for it: f'{}'. */
4097 all_whitespace = 1;
4098 for (i = expr_start; i < expr_end; i++) {
4099 if (!Py_UNICODE_ISSPACE(PyUnicode_READ_CHAR(str, i))) {
4100 all_whitespace = 0;
4101 break;
4102 }
4103 }
4104 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004105 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004106 goto error;
4107 }
4108
4109 /* If the substring will be the entire source string, we can't use
4110 PyUnicode_Substring, since it will return another reference to
4111 our original string. Because we're modifying the string in
4112 place, that's a no-no. So, detect that case and just use our
4113 string directly. */
4114
4115 if (expr_start-1 == 0 && expr_end+1 == PyUnicode_GET_LENGTH(str)) {
Eric V. Smith1d44c412015-09-23 07:49:00 -04004116 /* If str is well formed, then the first and last chars must
4117 be '{' and '}', respectively. But, if there's a syntax
4118 error, for example f'{3!', then the last char won't be a
4119 closing brace. So, remember the last character we read in
4120 order for us to restore it. */
4121 end_ch = PyUnicode_ReadChar(str, expr_end-expr_start+1);
4122 assert(end_ch != (Py_UCS4)-1);
4123
4124 /* In all cases, however, start_ch must be '{'. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004125 assert(PyUnicode_ReadChar(str, 0) == '{');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004126
Eric V. Smith235a6f02015-09-19 14:51:32 -04004127 sub = str;
4128 } else {
4129 /* Create a substring object. It must be a new object, with
4130 refcount==1, so that we can modify it. */
4131 sub = PyUnicode_Substring(str, expr_start-1, expr_end+1);
4132 if (!sub)
4133 goto error;
4134 assert(sub != str); /* Make sure it's a new string. */
4135 decref_sub = 1; /* Remember to deallocate it on error. */
4136 }
4137
Eric V. Smith1d44c412015-09-23 07:49:00 -04004138 /* Put () around the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004139 if (PyUnicode_WriteChar(sub, 0, '(') < 0 ||
4140 PyUnicode_WriteChar(sub, expr_end-expr_start+1, ')') < 0)
4141 goto error;
4142
Eric V. Smith235a6f02015-09-19 14:51:32 -04004143 /* No need to free the memory returned here: it's managed by the
4144 string. */
4145 utf_expr = PyUnicode_AsUTF8(sub);
4146 if (!utf_expr)
4147 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004148
4149 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004150 mod = PyParser_ASTFromString(utf_expr, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004151 Py_eval_input, &cf, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004152 if (!mod)
4153 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004154
Eric V. Smith235a6f02015-09-19 14:51:32 -04004155 if (sub != str)
4156 /* Clear instead of decref in case we ever modify this code to change
4157 the error handling: this is safest because the XDECREF won't try
4158 and decref it when it's NULL. */
4159 /* No need to restore the chars in sub, since we know it's getting
4160 ready to get deleted (refcount must be 1, since we got a new string
4161 in PyUnicode_Substring). */
4162 Py_CLEAR(sub);
4163 else {
4164 assert(!decref_sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004165 assert(end_ch != (Py_UCS4)-1);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004166 /* Restore str, which we earlier modified directly. */
4167 if (PyUnicode_WriteChar(str, 0, '{') < 0 ||
Eric V. Smith1d44c412015-09-23 07:49:00 -04004168 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004169 goto error;
4170 }
4171 return mod->v.Expression.body;
4172
4173error:
4174 /* Only decref sub if it was the result of a call to SubString. */
4175 if (decref_sub)
4176 Py_XDECREF(sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004177
4178 if (end_ch != (Py_UCS4)-1) {
4179 /* We only get here if we modified str. Make sure that's the
4180 case: str will be equal to sub. */
4181 if (str == sub) {
4182 /* Don't check the error, because we've already set the
4183 error state (that's why we're in 'error', after
4184 all). */
4185 PyUnicode_WriteChar(str, 0, '{');
4186 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch);
4187 }
4188 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004189 return NULL;
4190}
4191
4192/* Return -1 on error.
4193
4194 Return 0 if we reached the end of the literal.
4195
4196 Return 1 if we haven't reached the end of the literal, but we want
4197 the caller to process the literal up to this point. Used for
4198 doubled braces.
4199*/
4200static int
4201fstring_find_literal(PyObject *str, Py_ssize_t *ofs, PyObject **literal,
4202 int recurse_lvl, struct compiling *c, const node *n)
4203{
4204 /* Get any literal string. It ends when we hit an un-doubled brace, or the
4205 end of the string. */
4206
4207 Py_ssize_t literal_start, literal_end;
4208 int result = 0;
4209
4210 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4211 void *data = PyUnicode_DATA(str);
4212
4213 assert(*literal == NULL);
4214
4215 literal_start = *ofs;
4216 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4217 Py_UCS4 ch = PyUnicode_READ(kind, data, *ofs);
4218 if (ch == '{' || ch == '}') {
4219 /* Check for doubled braces, but only at the top level. If
4220 we checked at every level, then f'{0:{3}}' would fail
4221 with the two closing braces. */
4222 if (recurse_lvl == 0) {
4223 if (*ofs + 1 < PyUnicode_GET_LENGTH(str) &&
4224 PyUnicode_READ(kind, data, *ofs + 1) == ch) {
4225 /* We're going to tell the caller that the literal ends
4226 here, but that they should continue scanning. But also
4227 skip over the second brace when we resume scanning. */
4228 literal_end = *ofs + 1;
4229 *ofs += 2;
4230 result = 1;
4231 goto done;
4232 }
4233
4234 /* Where a single '{' is the start of a new expression, a
4235 single '}' is not allowed. */
4236 if (ch == '}') {
4237 ast_error(c, n, "f-string: single '}' is not allowed");
4238 return -1;
4239 }
4240 }
4241
4242 /* We're either at a '{', which means we're starting another
4243 expression; or a '}', which means we're at the end of this
4244 f-string (for a nested format_spec). */
4245 break;
4246 }
4247 }
4248 literal_end = *ofs;
4249
4250 assert(*ofs == PyUnicode_GET_LENGTH(str) ||
4251 PyUnicode_READ(kind, data, *ofs) == '{' ||
4252 PyUnicode_READ(kind, data, *ofs) == '}');
4253done:
4254 if (literal_start != literal_end) {
4255 *literal = PyUnicode_Substring(str, literal_start, literal_end);
4256 if (!*literal)
4257 return -1;
4258 }
4259
4260 return result;
4261}
4262
4263/* Forward declaration because parsing is recursive. */
4264static expr_ty
4265fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4266 struct compiling *c, const node *n);
4267
4268/* Parse the f-string str, starting at ofs. We know *ofs starts an
4269 expression (so it must be a '{'). Returns the FormattedValue node,
4270 which includes the expression, conversion character, and
4271 format_spec expression.
4272
4273 Note that I don't do a perfect job here: I don't make sure that a
4274 closing brace doesn't match an opening paren, for example. It
4275 doesn't need to error on all invalid expressions, just correctly
4276 find the end of all valid ones. Any errors inside the expression
4277 will be caught when we parse it later. */
4278static int
4279fstring_find_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4280 expr_ty *expression, struct compiling *c, const node *n)
4281{
4282 /* Return -1 on error, else 0. */
4283
4284 Py_ssize_t expr_start;
4285 Py_ssize_t expr_end;
4286 expr_ty simple_expression;
4287 expr_ty format_spec = NULL; /* Optional format specifier. */
4288 Py_UCS4 conversion = -1; /* The conversion char. -1 if not specified. */
4289
4290 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4291 void *data = PyUnicode_DATA(str);
4292
4293 /* 0 if we're not in a string, else the quote char we're trying to
4294 match (single or double quote). */
4295 Py_UCS4 quote_char = 0;
4296
4297 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4298 int string_type = 0;
4299
4300 /* Keep track of nesting level for braces/parens/brackets in
4301 expressions. */
4302 Py_ssize_t nested_depth = 0;
4303
4304 /* Can only nest one level deep. */
4305 if (recurse_lvl >= 2) {
4306 ast_error(c, n, "f-string: expressions nested too deeply");
4307 return -1;
4308 }
4309
4310 /* The first char must be a left brace, or we wouldn't have gotten
4311 here. Skip over it. */
4312 assert(PyUnicode_READ(kind, data, *ofs) == '{');
4313 *ofs += 1;
4314
4315 expr_start = *ofs;
4316 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4317 Py_UCS4 ch;
4318
4319 /* Loop invariants. */
4320 assert(nested_depth >= 0);
4321 assert(*ofs >= expr_start);
4322 if (quote_char)
4323 assert(string_type == 1 || string_type == 3);
4324 else
4325 assert(string_type == 0);
4326
4327 ch = PyUnicode_READ(kind, data, *ofs);
4328 if (quote_char) {
4329 /* We're inside a string. See if we're at the end. */
4330 /* This code needs to implement the same non-error logic
4331 as tok_get from tokenizer.c, at the letter_quote
4332 label. To actually share that code would be a
4333 nightmare. But, it's unlikely to change and is small,
4334 so duplicate it here. Note we don't need to catch all
4335 of the errors, since they'll be caught when parsing the
4336 expression. We just need to match the non-error
4337 cases. Thus we can ignore \n in single-quoted strings,
4338 for example. Or non-terminated strings. */
4339 if (ch == quote_char) {
4340 /* Does this match the string_type (single or triple
4341 quoted)? */
4342 if (string_type == 3) {
4343 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4344 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4345 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4346 /* We're at the end of a triple quoted string. */
4347 *ofs += 2;
4348 string_type = 0;
4349 quote_char = 0;
4350 continue;
4351 }
4352 } else {
4353 /* We're at the end of a normal string. */
4354 quote_char = 0;
4355 string_type = 0;
4356 continue;
4357 }
4358 }
4359 /* We're inside a string, and not finished with the
4360 string. If this is a backslash, skip the next char (it
4361 might be an end quote that needs skipping). Otherwise,
4362 just consume this character normally. */
4363 if (ch == '\\' && *ofs+1 < PyUnicode_GET_LENGTH(str)) {
4364 /* Just skip the next char, whatever it is. */
4365 *ofs += 1;
4366 }
4367 } else if (ch == '\'' || ch == '"') {
4368 /* Is this a triple quoted string? */
4369 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4370 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4371 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4372 string_type = 3;
4373 *ofs += 2;
4374 } else {
4375 /* Start of a normal string. */
4376 string_type = 1;
4377 }
4378 /* Start looking for the end of the string. */
4379 quote_char = ch;
4380 } else if (ch == '[' || ch == '{' || ch == '(') {
4381 nested_depth++;
4382 } else if (nested_depth != 0 &&
4383 (ch == ']' || ch == '}' || ch == ')')) {
4384 nested_depth--;
4385 } else if (ch == '#') {
4386 /* Error: can't include a comment character, inside parens
4387 or not. */
4388 ast_error(c, n, "f-string cannot include '#'");
4389 return -1;
4390 } else if (nested_depth == 0 &&
4391 (ch == '!' || ch == ':' || ch == '}')) {
4392 /* First, test for the special case of "!=". Since '=' is
4393 not an allowed conversion character, nothing is lost in
4394 this test. */
4395 if (ch == '!' && *ofs+1 < PyUnicode_GET_LENGTH(str) &&
4396 PyUnicode_READ(kind, data, *ofs+1) == '=')
4397 /* This isn't a conversion character, just continue. */
4398 continue;
4399
4400 /* Normal way out of this loop. */
4401 break;
4402 } else {
4403 /* Just consume this char and loop around. */
4404 }
4405 }
4406 expr_end = *ofs;
4407 /* If we leave this loop in a string or with mismatched parens, we
4408 don't care. We'll get a syntax error when compiling the
4409 expression. But, we can produce a better error message, so
4410 let's just do that.*/
4411 if (quote_char) {
4412 ast_error(c, n, "f-string: unterminated string");
4413 return -1;
4414 }
4415 if (nested_depth) {
4416 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4417 return -1;
4418 }
4419
Eric V. Smith235a6f02015-09-19 14:51:32 -04004420 if (*ofs >= PyUnicode_GET_LENGTH(str))
4421 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004422
4423 /* Compile the expression as soon as possible, so we show errors
4424 related to the expression before errors related to the
4425 conversion or format_spec. */
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004426 simple_expression = fstring_compile_expr(str, expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004427 if (!simple_expression)
4428 return -1;
4429
4430 /* Check for a conversion char, if present. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004431 if (PyUnicode_READ(kind, data, *ofs) == '!') {
4432 *ofs += 1;
4433 if (*ofs >= PyUnicode_GET_LENGTH(str))
4434 goto unexpected_end_of_string;
4435
4436 conversion = PyUnicode_READ(kind, data, *ofs);
4437 *ofs += 1;
4438
4439 /* Validate the conversion. */
4440 if (!(conversion == 's' || conversion == 'r'
4441 || conversion == 'a')) {
4442 ast_error(c, n, "f-string: invalid conversion character: "
4443 "expected 's', 'r', or 'a'");
4444 return -1;
4445 }
4446 }
4447
4448 /* Check for the format spec, if present. */
4449 if (*ofs >= PyUnicode_GET_LENGTH(str))
4450 goto unexpected_end_of_string;
4451 if (PyUnicode_READ(kind, data, *ofs) == ':') {
4452 *ofs += 1;
4453 if (*ofs >= PyUnicode_GET_LENGTH(str))
4454 goto unexpected_end_of_string;
4455
4456 /* Parse the format spec. */
4457 format_spec = fstring_parse(str, ofs, recurse_lvl+1, c, n);
4458 if (!format_spec)
4459 return -1;
4460 }
4461
4462 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4463 PyUnicode_READ(kind, data, *ofs) != '}')
4464 goto unexpected_end_of_string;
4465
4466 /* We're at a right brace. Consume it. */
4467 assert(*ofs < PyUnicode_GET_LENGTH(str));
4468 assert(PyUnicode_READ(kind, data, *ofs) == '}');
4469 *ofs += 1;
4470
Eric V. Smith235a6f02015-09-19 14:51:32 -04004471 /* And now create the FormattedValue node that represents this entire
4472 expression with the conversion and format spec. */
4473 *expression = FormattedValue(simple_expression, (int)conversion,
4474 format_spec, LINENO(n), n->n_col_offset,
4475 c->c_arena);
4476 if (!*expression)
4477 return -1;
4478
4479 return 0;
4480
4481unexpected_end_of_string:
4482 ast_error(c, n, "f-string: expecting '}'");
4483 return -1;
4484}
4485
4486/* Return -1 on error.
4487
4488 Return 0 if we have a literal (possible zero length) and an
4489 expression (zero length if at the end of the string.
4490
4491 Return 1 if we have a literal, but no expression, and we want the
4492 caller to call us again. This is used to deal with doubled
4493 braces.
4494
4495 When called multiple times on the string 'a{{b{0}c', this function
4496 will return:
4497
4498 1. the literal 'a{' with no expression, and a return value
4499 of 1. Despite the fact that there's no expression, the return
4500 value of 1 means we're not finished yet.
4501
4502 2. the literal 'b' and the expression '0', with a return value of
4503 0. The fact that there's an expression means we're not finished.
4504
4505 3. literal 'c' with no expression and a return value of 0. The
4506 combination of the return value of 0 with no expression means
4507 we're finished.
4508*/
4509static int
4510fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4511 PyObject **literal, expr_ty *expression,
4512 struct compiling *c, const node *n)
4513{
4514 int result;
4515
4516 assert(*literal == NULL && *expression == NULL);
4517
4518 /* Get any literal string. */
4519 result = fstring_find_literal(str, ofs, literal, recurse_lvl, c, n);
4520 if (result < 0)
4521 goto error;
4522
4523 assert(result == 0 || result == 1);
4524
4525 if (result == 1)
4526 /* We have a literal, but don't look at the expression. */
4527 return 1;
4528
4529 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4530
4531 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4532 PyUnicode_READ_CHAR(str, *ofs) == '}')
4533 /* We're at the end of the string or the end of a nested
4534 f-string: no expression. The top-level error case where we
4535 expect to be at the end of the string but we're at a '}' is
4536 handled later. */
4537 return 0;
4538
4539 /* We must now be the start of an expression, on a '{'. */
4540 assert(*ofs < PyUnicode_GET_LENGTH(str) &&
4541 PyUnicode_READ_CHAR(str, *ofs) == '{');
4542
4543 if (fstring_find_expr(str, ofs, recurse_lvl, expression, c, n) < 0)
4544 goto error;
4545
4546 return 0;
4547
4548error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004549 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004550 return -1;
4551}
4552
4553#define EXPRLIST_N_CACHED 64
4554
4555typedef struct {
4556 /* Incrementally build an array of expr_ty, so be used in an
4557 asdl_seq. Cache some small but reasonably sized number of
4558 expr_ty's, and then after that start dynamically allocating,
4559 doubling the number allocated each time. Note that the f-string
4560 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4561 Str for the literal 'a'. So you add expr_ty's about twice as
4562 fast as you add exressions in an f-string. */
4563
4564 Py_ssize_t allocated; /* Number we've allocated. */
4565 Py_ssize_t size; /* Number we've used. */
4566 expr_ty *p; /* Pointer to the memory we're actually
4567 using. Will point to 'data' until we
4568 start dynamically allocating. */
4569 expr_ty data[EXPRLIST_N_CACHED];
4570} ExprList;
4571
4572#ifdef NDEBUG
4573#define ExprList_check_invariants(l)
4574#else
4575static void
4576ExprList_check_invariants(ExprList *l)
4577{
4578 /* Check our invariants. Make sure this object is "live", and
4579 hasn't been deallocated. */
4580 assert(l->size >= 0);
4581 assert(l->p != NULL);
4582 if (l->size <= EXPRLIST_N_CACHED)
4583 assert(l->data == l->p);
4584}
4585#endif
4586
4587static void
4588ExprList_Init(ExprList *l)
4589{
4590 l->allocated = EXPRLIST_N_CACHED;
4591 l->size = 0;
4592
4593 /* Until we start allocating dynamically, p points to data. */
4594 l->p = l->data;
4595
4596 ExprList_check_invariants(l);
4597}
4598
4599static int
4600ExprList_Append(ExprList *l, expr_ty exp)
4601{
4602 ExprList_check_invariants(l);
4603 if (l->size >= l->allocated) {
4604 /* We need to alloc (or realloc) the memory. */
4605 Py_ssize_t new_size = l->allocated * 2;
4606
4607 /* See if we've ever allocated anything dynamically. */
4608 if (l->p == l->data) {
4609 Py_ssize_t i;
4610 /* We're still using the cached data. Switch to
4611 alloc-ing. */
4612 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4613 if (!l->p)
4614 return -1;
4615 /* Copy the cached data into the new buffer. */
4616 for (i = 0; i < l->size; i++)
4617 l->p[i] = l->data[i];
4618 } else {
4619 /* Just realloc. */
4620 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4621 if (!tmp) {
4622 PyMem_RawFree(l->p);
4623 l->p = NULL;
4624 return -1;
4625 }
4626 l->p = tmp;
4627 }
4628
4629 l->allocated = new_size;
4630 assert(l->allocated == 2 * l->size);
4631 }
4632
4633 l->p[l->size++] = exp;
4634
4635 ExprList_check_invariants(l);
4636 return 0;
4637}
4638
4639static void
4640ExprList_Dealloc(ExprList *l)
4641{
4642 ExprList_check_invariants(l);
4643
4644 /* If there's been an error, or we've never dynamically allocated,
4645 do nothing. */
4646 if (!l->p || l->p == l->data) {
4647 /* Do nothing. */
4648 } else {
4649 /* We have dynamically allocated. Free the memory. */
4650 PyMem_RawFree(l->p);
4651 }
4652 l->p = NULL;
4653 l->size = -1;
4654}
4655
4656static asdl_seq *
4657ExprList_Finish(ExprList *l, PyArena *arena)
4658{
4659 asdl_seq *seq;
4660
4661 ExprList_check_invariants(l);
4662
4663 /* Allocate the asdl_seq and copy the expressions in to it. */
4664 seq = _Py_asdl_seq_new(l->size, arena);
4665 if (seq) {
4666 Py_ssize_t i;
4667 for (i = 0; i < l->size; i++)
4668 asdl_seq_SET(seq, i, l->p[i]);
4669 }
4670 ExprList_Dealloc(l);
4671 return seq;
4672}
4673
4674/* The FstringParser is designed to add a mix of strings and
4675 f-strings, and concat them together as needed. Ultimately, it
4676 generates an expr_ty. */
4677typedef struct {
4678 PyObject *last_str;
4679 ExprList expr_list;
4680} FstringParser;
4681
4682#ifdef NDEBUG
4683#define FstringParser_check_invariants(state)
4684#else
4685static void
4686FstringParser_check_invariants(FstringParser *state)
4687{
4688 if (state->last_str)
4689 assert(PyUnicode_CheckExact(state->last_str));
4690 ExprList_check_invariants(&state->expr_list);
4691}
4692#endif
4693
4694static void
4695FstringParser_Init(FstringParser *state)
4696{
4697 state->last_str = NULL;
4698 ExprList_Init(&state->expr_list);
4699 FstringParser_check_invariants(state);
4700}
4701
4702static void
4703FstringParser_Dealloc(FstringParser *state)
4704{
4705 FstringParser_check_invariants(state);
4706
4707 Py_XDECREF(state->last_str);
4708 ExprList_Dealloc(&state->expr_list);
4709}
4710
4711/* Make a Str node, but decref the PyUnicode object being added. */
4712static expr_ty
4713make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4714{
4715 PyObject *s = *str;
4716 *str = NULL;
4717 assert(PyUnicode_CheckExact(s));
4718 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4719 Py_DECREF(s);
4720 return NULL;
4721 }
4722 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4723}
4724
4725/* Add a non-f-string (that is, a regular literal string). str is
4726 decref'd. */
4727static int
4728FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4729{
4730 FstringParser_check_invariants(state);
4731
4732 assert(PyUnicode_CheckExact(str));
4733
4734 if (PyUnicode_GET_LENGTH(str) == 0) {
4735 Py_DECREF(str);
4736 return 0;
4737 }
4738
4739 if (!state->last_str) {
4740 /* We didn't have a string before, so just remember this one. */
4741 state->last_str = str;
4742 } else {
4743 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004744 PyUnicode_AppendAndDel(&state->last_str, str);
4745 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004746 return -1;
4747 }
4748 FstringParser_check_invariants(state);
4749 return 0;
4750}
4751
4752/* Parse an f-string. The f-string is in str, starting at ofs, with no 'f'
4753 or quotes. str is not decref'd, since we don't know if it's used elsewhere.
4754 And if we're only looking at a part of a string, then decref'ing is
4755 definitely not the right thing to do! */
4756static int
4757FstringParser_ConcatFstring(FstringParser *state, PyObject *str,
4758 Py_ssize_t *ofs, int recurse_lvl,
4759 struct compiling *c, const node *n)
4760{
4761 FstringParser_check_invariants(state);
4762
4763 /* Parse the f-string. */
4764 while (1) {
4765 PyObject *literal = NULL;
4766 expr_ty expression = NULL;
4767
4768 /* If there's a zero length literal in front of the
4769 expression, literal will be NULL. If we're at the end of
4770 the f-string, expression will be NULL (unless result == 1,
4771 see below). */
4772 int result = fstring_find_literal_and_expr(str, ofs, recurse_lvl,
4773 &literal, &expression,
4774 c, n);
4775 if (result < 0)
4776 return -1;
4777
4778 /* Add the literal, if any. */
4779 if (!literal) {
4780 /* Do nothing. Just leave last_str alone (and possibly
4781 NULL). */
4782 } else if (!state->last_str) {
4783 state->last_str = literal;
4784 literal = NULL;
4785 } else {
4786 /* We have a literal, concatenate it. */
4787 assert(PyUnicode_GET_LENGTH(literal) != 0);
4788 if (FstringParser_ConcatAndDel(state, literal) < 0)
4789 return -1;
4790 literal = NULL;
4791 }
4792 assert(!state->last_str ||
4793 PyUnicode_GET_LENGTH(state->last_str) != 0);
4794
4795 /* We've dealt with the literal now. It can't be leaked on further
4796 errors. */
4797 assert(literal == NULL);
4798
4799 /* See if we should just loop around to get the next literal
4800 and expression, while ignoring the expression this
4801 time. This is used for un-doubling braces, as an
4802 optimization. */
4803 if (result == 1)
4804 continue;
4805
4806 if (!expression)
4807 /* We're done with this f-string. */
4808 break;
4809
4810 /* We know we have an expression. Convert any existing string
4811 to a Str node. */
4812 if (!state->last_str) {
4813 /* Do nothing. No previous literal. */
4814 } else {
4815 /* Convert the existing last_str literal to a Str node. */
4816 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4817 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4818 return -1;
4819 }
4820
4821 if (ExprList_Append(&state->expr_list, expression) < 0)
4822 return -1;
4823 }
4824
4825 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4826
4827 /* If recurse_lvl is zero, then we must be at the end of the
4828 string. Otherwise, we must be at a right brace. */
4829
4830 if (recurse_lvl == 0 && *ofs < PyUnicode_GET_LENGTH(str)) {
4831 ast_error(c, n, "f-string: unexpected end of string");
4832 return -1;
4833 }
4834 if (recurse_lvl != 0 && PyUnicode_READ_CHAR(str, *ofs) != '}') {
4835 ast_error(c, n, "f-string: expecting '}'");
4836 return -1;
4837 }
4838
4839 FstringParser_check_invariants(state);
4840 return 0;
4841}
4842
4843/* Convert the partial state reflected in last_str and expr_list to an
4844 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4845static expr_ty
4846FstringParser_Finish(FstringParser *state, struct compiling *c,
4847 const node *n)
4848{
4849 asdl_seq *seq;
4850
4851 FstringParser_check_invariants(state);
4852
4853 /* If we're just a constant string with no expressions, return
4854 that. */
4855 if(state->expr_list.size == 0) {
4856 if (!state->last_str) {
4857 /* Create a zero length string. */
4858 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4859 if (!state->last_str)
4860 goto error;
4861 }
4862 return make_str_node_and_del(&state->last_str, c, n);
4863 }
4864
4865 /* Create a Str node out of last_str, if needed. It will be the
4866 last node in our expression list. */
4867 if (state->last_str) {
4868 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4869 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4870 goto error;
4871 }
4872 /* This has already been freed. */
4873 assert(state->last_str == NULL);
4874
4875 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4876 if (!seq)
4877 goto error;
4878
4879 /* If there's only one expression, return it. Otherwise, we need
4880 to join them together. */
4881 if (seq->size == 1)
4882 return seq->elements[0];
4883
4884 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4885
4886error:
4887 FstringParser_Dealloc(state);
4888 return NULL;
4889}
4890
4891/* Given an f-string (with no 'f' or quotes) that's in str starting at
4892 ofs, parse it into an expr_ty. Return NULL on error. Does not
4893 decref str. */
4894static expr_ty
4895fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4896 struct compiling *c, const node *n)
4897{
4898 FstringParser state;
4899
4900 FstringParser_Init(&state);
4901 if (FstringParser_ConcatFstring(&state, str, ofs, recurse_lvl,
4902 c, n) < 0) {
4903 FstringParser_Dealloc(&state);
4904 return NULL;
4905 }
4906
4907 return FstringParser_Finish(&state, c, n);
4908}
4909
4910/* n is a Python string literal, including the bracketing quote
4911 characters, and r, b, u, &/or f prefixes (if any), and embedded
4912 escape sequences (if any). parsestr parses it, and returns the
4913 decoded Python string object. If the string is an f-string, set
4914 *fmode and return the unparsed string object.
4915*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004916static PyObject *
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004918{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004919 size_t len;
4920 const char *s = STR(n);
4921 int quote = Py_CHARMASK(*s);
4922 int rawmode = 0;
4923 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004924 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004925 while (!*bytesmode || !rawmode) {
4926 if (quote == 'b' || quote == 'B') {
4927 quote = *++s;
4928 *bytesmode = 1;
4929 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004930 else if (quote == 'u' || quote == 'U') {
4931 quote = *++s;
4932 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004933 else if (quote == 'r' || quote == 'R') {
4934 quote = *++s;
4935 rawmode = 1;
4936 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004937 else if (quote == 'f' || quote == 'F') {
4938 quote = *++s;
4939 *fmode = 1;
4940 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004941 else {
4942 break;
4943 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004944 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004945 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004946 if (*fmode && *bytesmode) {
4947 PyErr_BadInternalCall();
4948 return NULL;
4949 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004950 if (quote != '\'' && quote != '\"') {
4951 PyErr_BadInternalCall();
4952 return NULL;
4953 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004954 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004955 s++;
4956 len = strlen(s);
4957 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004959 "string to parse is too long");
4960 return NULL;
4961 }
4962 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004964 PyErr_BadInternalCall();
4965 return NULL;
4966 }
4967 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004968 /* A triple quoted string. We've already skipped one quote at
4969 the start and one at the end of the string. Now skip the
4970 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004971 s += 2;
4972 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004973 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004974 if (s[--len] != quote || s[--len] != quote) {
4975 PyErr_BadInternalCall();
4976 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004977 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004978 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00004979 if (!*bytesmode && !rawmode) {
Eric V. Smith5567f892015-09-21 13:36:09 -04004980 return decode_unicode(c, s, len, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004981 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004982 if (*bytesmode) {
4983 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004984 const char *ch;
4985 for (ch = s; *ch; ch++) {
4986 if (Py_CHARMASK(*ch) >= 0x80) {
4987 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004988 "literal characters.");
4989 return NULL;
4990 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004991 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004992 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00004993 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00004994 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004995 if (rawmode || strchr(s, '\\') == NULL) {
4996 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004997 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00004998 if (u == NULL || !*bytesmode)
4999 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00005000 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005001 Py_DECREF(u);
5002 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00005003 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00005004 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00005005 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00005006 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00005008 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005009 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005010 }
Christian Heimes72b710a2008-05-26 13:28:38 +00005011 return PyBytes_DecodeEscape(s, len, NULL, 1,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005012 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005013}
5014
Eric V. Smith235a6f02015-09-19 14:51:32 -04005015/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5016 each STRING atom, and process it as needed. For bytes, just
5017 concatenate them together, and the result will be a Bytes node. For
5018 normal strings and f-strings, concatenate them together. The result
5019 will be a Str node if there were no f-strings; a FormattedValue
5020 node if there's just an f-string (with no leading or trailing
5021 literals), or a JoinedStr node if there are multiple f-strings or
5022 any literals involved. */
5023static expr_ty
5024parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005025{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 int bytesmode = 0;
5027 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005028 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005029
5030 FstringParser state;
5031 FstringParser_Init(&state);
5032
5033 for (i = 0; i < NCH(n); i++) {
5034 int this_bytesmode = 0;
5035 int this_fmode = 0;
5036 PyObject *s;
5037
5038 REQ(CHILD(n, i), STRING);
5039 s = parsestr(c, CHILD(n, i), &this_bytesmode, &this_fmode);
5040 if (!s)
5041 goto error;
5042
5043 /* Check that we're not mixing bytes with unicode. */
5044 if (i != 0 && bytesmode != this_bytesmode) {
5045 ast_error(c, n, "cannot mix bytes and nonbytes literals");
5046 Py_DECREF(s);
5047 goto error;
5048 }
5049 bytesmode = this_bytesmode;
5050
5051 assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
5052
5053 if (bytesmode) {
5054 /* For bytes, concat as we go. */
5055 if (i == 0) {
5056 /* First time, just remember this value. */
5057 bytes_str = s;
5058 } else {
5059 PyBytes_ConcatAndDel(&bytes_str, s);
5060 if (!bytes_str)
5061 goto error;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005062 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063 } else if (this_fmode) {
5064 /* This is an f-string. Concatenate and decref it. */
5065 Py_ssize_t ofs = 0;
5066 int result = FstringParser_ConcatFstring(&state, s, &ofs, 0, c, n);
5067 Py_DECREF(s);
5068 if (result < 0)
5069 goto error;
5070 } else {
5071 /* This is a regular string. Concatenate it. */
5072 if (FstringParser_ConcatAndDel(&state, s) < 0)
5073 goto error;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005074 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005075 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005076 if (bytesmode) {
5077 /* Just return the bytes object and we're done. */
5078 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5079 goto error;
5080 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5081 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005082
Eric V. Smith235a6f02015-09-19 14:51:32 -04005083 /* We're not a bytes string, bytes_str should never have been set. */
5084 assert(bytes_str == NULL);
5085
5086 return FstringParser_Finish(&state, c, n);
5087
5088error:
5089 Py_XDECREF(bytes_str);
5090 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005091 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005092}