blob: e89ec225843f5b18d2dd8827a86abc3d2f6c6bbb [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100135validate_constant(PyObject *value)
136{
137 if (value == Py_None || value == Py_Ellipsis)
138 return 1;
139
140 if (PyLong_CheckExact(value)
141 || PyFloat_CheckExact(value)
142 || PyComplex_CheckExact(value)
143 || PyBool_Check(value)
144 || PyUnicode_CheckExact(value)
145 || PyBytes_CheckExact(value))
146 return 1;
147
148 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
149 PyObject *it;
150
151 it = PyObject_GetIter(value);
152 if (it == NULL)
153 return 0;
154
155 while (1) {
156 PyObject *item = PyIter_Next(it);
157 if (item == NULL) {
158 if (PyErr_Occurred()) {
159 Py_DECREF(it);
160 return 0;
161 }
162 break;
163 }
164
165 if (!validate_constant(item)) {
166 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100167 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100168 return 0;
169 }
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 }
172
173 Py_DECREF(it);
174 return 1;
175 }
176
177 return 0;
178}
179
180static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500181validate_expr(expr_ty exp, expr_context_ty ctx)
182{
183 int check_ctx = 1;
184 expr_context_ty actual_ctx;
185
186 /* First check expression context. */
187 switch (exp->kind) {
188 case Attribute_kind:
189 actual_ctx = exp->v.Attribute.ctx;
190 break;
191 case Subscript_kind:
192 actual_ctx = exp->v.Subscript.ctx;
193 break;
194 case Starred_kind:
195 actual_ctx = exp->v.Starred.ctx;
196 break;
197 case Name_kind:
198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100291 PyErr_Format(PyExc_TypeError,
292 "got an invalid type in Constant: %s",
293 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 return 0;
295 }
296 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 case Num_kind: {
298 PyObject *n = exp->v.Num.n;
299 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
300 !PyComplex_CheckExact(n)) {
301 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
302 return 0;
303 }
304 return 1;
305 }
306 case Str_kind: {
307 PyObject *s = exp->v.Str.s;
308 if (!PyUnicode_CheckExact(s)) {
309 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
310 return 0;
311 }
312 return 1;
313 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314 case JoinedStr_kind:
315 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
316 case FormattedValue_kind:
317 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
318 return 0;
319 if (exp->v.FormattedValue.format_spec)
320 return validate_expr(exp->v.FormattedValue.format_spec, Load);
321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Bytes_kind: {
323 PyObject *b = exp->v.Bytes.s;
324 if (!PyBytes_CheckExact(b)) {
325 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
326 return 0;
327 }
328 return 1;
329 }
330 case Attribute_kind:
331 return validate_expr(exp->v.Attribute.value, Load);
332 case Subscript_kind:
333 return validate_slice(exp->v.Subscript.slice) &&
334 validate_expr(exp->v.Subscript.value, Load);
335 case Starred_kind:
336 return validate_expr(exp->v.Starred.value, ctx);
337 case List_kind:
338 return validate_exprs(exp->v.List.elts, ctx, 0);
339 case Tuple_kind:
340 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
341 /* These last cases don't have any checking. */
342 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500343 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 case Ellipsis_kind:
345 return 1;
346 default:
347 PyErr_SetString(PyExc_SystemError, "unexpected expression");
348 return 0;
349 }
350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
369validate_body(asdl_seq *body, const char *owner)
370{
371 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
372}
373
374static int
375validate_stmt(stmt_ty stmt)
376{
377 int i;
378 switch (stmt->kind) {
379 case FunctionDef_kind:
380 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
381 validate_arguments(stmt->v.FunctionDef.args) &&
382 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
383 (!stmt->v.FunctionDef.returns ||
384 validate_expr(stmt->v.FunctionDef.returns, Load));
385 case ClassDef_kind:
386 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
387 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
388 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400389 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500390 case Return_kind:
391 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
392 case Delete_kind:
393 return validate_assignlist(stmt->v.Delete.targets, Del);
394 case Assign_kind:
395 return validate_assignlist(stmt->v.Assign.targets, Store) &&
396 validate_expr(stmt->v.Assign.value, Load);
397 case AugAssign_kind:
398 return validate_expr(stmt->v.AugAssign.target, Store) &&
399 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700400 case AnnAssign_kind:
401 if (stmt->v.AnnAssign.target->kind != Name_kind &&
402 stmt->v.AnnAssign.simple) {
403 PyErr_SetString(PyExc_TypeError,
404 "AnnAssign with simple non-Name target");
405 return 0;
406 }
407 return validate_expr(stmt->v.AnnAssign.target, Store) &&
408 (!stmt->v.AnnAssign.value ||
409 validate_expr(stmt->v.AnnAssign.value, Load)) &&
410 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500411 case For_kind:
412 return validate_expr(stmt->v.For.target, Store) &&
413 validate_expr(stmt->v.For.iter, Load) &&
414 validate_body(stmt->v.For.body, "For") &&
415 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400416 case AsyncFor_kind:
417 return validate_expr(stmt->v.AsyncFor.target, Store) &&
418 validate_expr(stmt->v.AsyncFor.iter, Load) &&
419 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
420 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500421 case While_kind:
422 return validate_expr(stmt->v.While.test, Load) &&
423 validate_body(stmt->v.While.body, "While") &&
424 validate_stmts(stmt->v.While.orelse);
425 case If_kind:
426 return validate_expr(stmt->v.If.test, Load) &&
427 validate_body(stmt->v.If.body, "If") &&
428 validate_stmts(stmt->v.If.orelse);
429 case With_kind:
430 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
431 return 0;
432 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
433 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
434 if (!validate_expr(item->context_expr, Load) ||
435 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
436 return 0;
437 }
438 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400439 case AsyncWith_kind:
440 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
441 return 0;
442 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
443 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
444 if (!validate_expr(item->context_expr, Load) ||
445 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
446 return 0;
447 }
448 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500449 case Raise_kind:
450 if (stmt->v.Raise.exc) {
451 return validate_expr(stmt->v.Raise.exc, Load) &&
452 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
453 }
454 if (stmt->v.Raise.cause) {
455 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
456 return 0;
457 }
458 return 1;
459 case Try_kind:
460 if (!validate_body(stmt->v.Try.body, "Try"))
461 return 0;
462 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
463 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
464 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
465 return 0;
466 }
467 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
468 asdl_seq_LEN(stmt->v.Try.orelse)) {
469 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
470 return 0;
471 }
472 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
473 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
474 if ((handler->v.ExceptHandler.type &&
475 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
476 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
477 return 0;
478 }
479 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
480 validate_stmts(stmt->v.Try.finalbody)) &&
481 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
482 validate_stmts(stmt->v.Try.orelse));
483 case Assert_kind:
484 return validate_expr(stmt->v.Assert.test, Load) &&
485 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
486 case Import_kind:
487 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
488 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300489 if (stmt->v.ImportFrom.level < 0) {
490 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500491 return 0;
492 }
493 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
494 case Global_kind:
495 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
496 case Nonlocal_kind:
497 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
498 case Expr_kind:
499 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400500 case AsyncFunctionDef_kind:
501 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
502 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
503 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
504 (!stmt->v.AsyncFunctionDef.returns ||
505 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500506 case Pass_kind:
507 case Break_kind:
508 case Continue_kind:
509 return 1;
510 default:
511 PyErr_SetString(PyExc_SystemError, "unexpected statement");
512 return 0;
513 }
514}
515
516static int
517validate_stmts(asdl_seq *seq)
518{
519 int i;
520 for (i = 0; i < asdl_seq_LEN(seq); i++) {
521 stmt_ty stmt = asdl_seq_GET(seq, i);
522 if (stmt) {
523 if (!validate_stmt(stmt))
524 return 0;
525 }
526 else {
527 PyErr_SetString(PyExc_ValueError,
528 "None disallowed in statement list");
529 return 0;
530 }
531 }
532 return 1;
533}
534
535static int
536validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
537{
538 int i;
539 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
540 expr_ty expr = asdl_seq_GET(exprs, i);
541 if (expr) {
542 if (!validate_expr(expr, ctx))
543 return 0;
544 }
545 else if (!null_ok) {
546 PyErr_SetString(PyExc_ValueError,
547 "None disallowed in expression list");
548 return 0;
549 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100550
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500551 }
552 return 1;
553}
554
555int
556PyAST_Validate(mod_ty mod)
557{
558 int res = 0;
559
560 switch (mod->kind) {
561 case Module_kind:
562 res = validate_stmts(mod->v.Module.body);
563 break;
564 case Interactive_kind:
565 res = validate_stmts(mod->v.Interactive.body);
566 break;
567 case Expression_kind:
568 res = validate_expr(mod->v.Expression.body, Load);
569 break;
570 case Suite_kind:
571 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
572 break;
573 default:
574 PyErr_SetString(PyExc_SystemError, "impossible module node");
575 res = 0;
576 break;
577 }
578 return res;
579}
580
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500581/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500582#include "grammar.h"
583#include "parsetok.h"
584#include "graminit.h"
585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586/* Data structure used internally */
587struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400588 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200589 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500590 PyObject *c_normalize; /* Normalization function from unicodedata. */
591 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592};
593
594static asdl_seq *seq_for_testlist(struct compiling *, const node *);
595static expr_ty ast_for_expr(struct compiling *, const node *);
596static stmt_ty ast_for_stmt(struct compiling *, const node *);
597static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000598static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
599 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000600static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000601static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Yury Selivanov75445082015-05-11 22:57:16 -0400603static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
604static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
605
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606/* Note different signature for ast_for_call */
607static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
608
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000609static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400610static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Nick Coghlan650f0d02007-04-15 12:05:43 +0000612#define COMP_GENEXP 0
613#define COMP_LISTCOMP 1
614#define COMP_SETCOMP 2
615
Benjamin Peterson55e00432012-01-16 17:22:31 -0500616static int
617init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000618{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500619 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
620 if (!m)
621 return 0;
622 c->c_normalize = PyObject_GetAttrString(m, "normalize");
623 Py_DECREF(m);
624 if (!c->c_normalize)
625 return 0;
626 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500627 if (!c->c_normalize_args) {
628 Py_CLEAR(c->c_normalize);
629 return 0;
630 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200631 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500632 return 1;
633}
634
635static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400636new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500637{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400638 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500639 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000640 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500641 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500642 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000643 /* Check whether there are non-ASCII characters in the
644 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500645 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200646 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500647 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500648 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200649 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500650 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500651 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
652 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500653 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 if (!id2)
655 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200656 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000657 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000658 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200659 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
660 Py_DECREF(id);
661 return NULL;
662 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000663 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664}
665
Benjamin Peterson55e00432012-01-16 17:22:31 -0500666#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400669ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400671 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
Victor Stinner14e461d2013-08-26 22:28:21 +0200673 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000675 Py_INCREF(Py_None);
676 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200678 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 if (!tmp)
680 return 0;
681 errstr = PyUnicode_FromString(errmsg);
682 if (!errstr) {
683 Py_DECREF(tmp);
684 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000685 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 Py_DECREF(errstr);
688 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400689 if (value) {
690 PyErr_SetObject(PyExc_SyntaxError, value);
691 Py_DECREF(value);
692 }
693 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694}
695
696/* num_stmts() returns number of contained statements.
697
698 Use this routine to determine how big a sequence is needed for
699 the statements in a parse tree. Its raison d'etre is this bit of
700 grammar:
701
702 stmt: simple_stmt | compound_stmt
703 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
704
705 A simple_stmt can contain multiple small_stmt elements joined
706 by semicolons. If the arg is a simple_stmt, the number of
707 small_stmt elements is returned.
708*/
709
710static int
711num_stmts(const node *n)
712{
713 int i, l;
714 node *ch;
715
716 switch (TYPE(n)) {
717 case single_input:
718 if (TYPE(CHILD(n, 0)) == NEWLINE)
719 return 0;
720 else
721 return num_stmts(CHILD(n, 0));
722 case file_input:
723 l = 0;
724 for (i = 0; i < NCH(n); i++) {
725 ch = CHILD(n, i);
726 if (TYPE(ch) == stmt)
727 l += num_stmts(ch);
728 }
729 return l;
730 case stmt:
731 return num_stmts(CHILD(n, 0));
732 case compound_stmt:
733 return 1;
734 case simple_stmt:
735 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
736 case suite:
737 if (NCH(n) == 1)
738 return num_stmts(CHILD(n, 0));
739 else {
740 l = 0;
741 for (i = 2; i < (NCH(n) - 1); i++)
742 l += num_stmts(CHILD(n, i));
743 return l;
744 }
745 default: {
746 char buf[128];
747
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000748 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 TYPE(n), NCH(n));
750 Py_FatalError(buf);
751 }
752 }
753 assert(0);
754 return 0;
755}
756
757/* Transform the CST rooted at node * to the appropriate AST
758*/
759
760mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200761PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
762 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000764 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 asdl_seq *stmts = NULL;
766 stmt_ty s;
767 node *ch;
768 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500769 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400771 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200772 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400773 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800774 c.c_normalize = NULL;
775 c.c_normalize_args = NULL;
776
777 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Jeremy Hyltona8293132006-02-28 17:58:27 +0000780 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 switch (TYPE(n)) {
782 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200783 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500785 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 for (i = 0; i < NCH(n) - 1; i++) {
787 ch = CHILD(n, i);
788 if (TYPE(ch) == NEWLINE)
789 continue;
790 REQ(ch, stmt);
791 num = num_stmts(ch);
792 if (num == 1) {
793 s = ast_for_stmt(&c, ch);
794 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500795 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000796 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 }
798 else {
799 ch = CHILD(ch, 0);
800 REQ(ch, simple_stmt);
801 for (j = 0; j < num; j++) {
802 s = ast_for_stmt(&c, CHILD(ch, j * 2));
803 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500804 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000805 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
807 }
808 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 res = Module(stmts, arena);
810 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 case eval_input: {
812 expr_ty testlist_ast;
813
Nick Coghlan650f0d02007-04-15 12:05:43 +0000814 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000815 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
818 res = Expression(testlist_ast, arena);
819 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 case single_input:
822 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200823 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500825 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
827 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000828 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500829 goto out;
830 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 }
832 else {
833 n = CHILD(n, 0);
834 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200835 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500837 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000839 s = ast_for_stmt(&c, n);
840 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500841 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 asdl_seq_SET(stmts, 0, s);
843 }
844 else {
845 /* Only a simple_stmt can contain multiple statements. */
846 REQ(n, simple_stmt);
847 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (TYPE(CHILD(n, i)) == NEWLINE)
849 break;
850 s = ast_for_stmt(&c, CHILD(n, i));
851 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500852 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 asdl_seq_SET(stmts, i / 2, s);
854 }
855 }
856
Benjamin Peterson55e00432012-01-16 17:22:31 -0500857 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500859 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000861 PyErr_Format(PyExc_SystemError,
862 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 out:
866 if (c.c_normalize) {
867 Py_DECREF(c.c_normalize);
868 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
869 Py_DECREF(c.c_normalize_args);
870 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872}
873
Victor Stinner14e461d2013-08-26 22:28:21 +0200874mod_ty
875PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
876 PyArena *arena)
877{
878 mod_ty mod;
879 PyObject *filename;
880 filename = PyUnicode_DecodeFSDefault(filename_str);
881 if (filename == NULL)
882 return NULL;
883 mod = PyAST_FromNodeObject(n, flags, filename, arena);
884 Py_DECREF(filename);
885 return mod;
886
887}
888
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
890*/
891
892static operator_ty
893get_operator(const node *n)
894{
895 switch (TYPE(n)) {
896 case VBAR:
897 return BitOr;
898 case CIRCUMFLEX:
899 return BitXor;
900 case AMPER:
901 return BitAnd;
902 case LEFTSHIFT:
903 return LShift;
904 case RIGHTSHIFT:
905 return RShift;
906 case PLUS:
907 return Add;
908 case MINUS:
909 return Sub;
910 case STAR:
911 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400912 case AT:
913 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 case SLASH:
915 return Div;
916 case DOUBLESLASH:
917 return FloorDiv;
918 case PERCENT:
919 return Mod;
920 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 }
923}
924
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200925static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000926 "None",
927 "True",
928 "False",
929 NULL,
930};
931
932static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400933forbidden_name(struct compiling *c, identifier name, const node *n,
934 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000935{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000936 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000937 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400938 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000939 return 1;
940 }
941 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200942 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000943 for (p = FORBIDDEN; *p; p++) {
944 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400945 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000946 return 1;
947 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000948 }
949 }
950 return 0;
951}
952
Jeremy Hyltona8293132006-02-28 17:58:27 +0000953/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
955 Only sets context for expr kinds that "can appear in assignment context"
956 (according to ../Parser/Python.asdl). For other expr kinds, it sets
957 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958*/
959
960static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000961set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962{
963 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 /* If a particular expression type can't be used for assign / delete,
965 set expr_name to its name and an error message will be generated.
966 */
967 const char* expr_name = NULL;
968
969 /* The ast defines augmented store and load contexts, but the
970 implementation here doesn't actually use them. The code may be
971 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000972 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000973 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000974 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 */
976 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977
978 switch (e->kind) {
979 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000980 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400981 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000982 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000983 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000985 e->v.Subscript.ctx = ctx;
986 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000987 case Starred_kind:
988 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000989 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000990 return 0;
991 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000993 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500994 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000995 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000996 }
997 e->v.Name.ctx = ctx;
998 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001000 e->v.List.ctx = ctx;
1001 s = e->v.List.elts;
1002 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001004 e->v.Tuple.ctx = ctx;
1005 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001006 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001007 case Lambda_kind:
1008 expr_name = "lambda";
1009 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001011 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001012 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001013 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001015 case UnaryOp_kind:
1016 expr_name = "operator";
1017 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001019 expr_name = "generator expression";
1020 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001021 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001022 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001023 expr_name = "yield expression";
1024 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001025 case Await_kind:
1026 expr_name = "await expression";
1027 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001028 case ListComp_kind:
1029 expr_name = "list comprehension";
1030 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001031 case SetComp_kind:
1032 expr_name = "set comprehension";
1033 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001034 case DictComp_kind:
1035 expr_name = "dict comprehension";
1036 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001037 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001038 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 case Num_kind:
1040 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001041 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001042 case JoinedStr_kind:
1043 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001044 expr_name = "literal";
1045 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001046 case NameConstant_kind:
1047 expr_name = "keyword";
1048 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001049 case Ellipsis_kind:
1050 expr_name = "Ellipsis";
1051 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001052 case Compare_kind:
1053 expr_name = "comparison";
1054 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055 case IfExp_kind:
1056 expr_name = "conditional expression";
1057 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001058 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 PyErr_Format(PyExc_SystemError,
1060 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001061 e->kind, e->lineno);
1062 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001064 /* Check for error string set by switch */
1065 if (expr_name) {
1066 char buf[300];
1067 PyOS_snprintf(buf, sizeof(buf),
1068 "can't %s %s",
1069 ctx == Store ? "assign to" : "delete",
1070 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001071 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001072 }
1073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 */
1077 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001078 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079
Thomas Wouters89f507f2006-12-13 04:49:30 +00001080 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001081 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001082 return 0;
1083 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 }
1085 return 1;
1086}
1087
1088static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001089ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090{
1091 REQ(n, augassign);
1092 n = CHILD(n, 0);
1093 switch (STR(n)[0]) {
1094 case '+':
1095 return Add;
1096 case '-':
1097 return Sub;
1098 case '/':
1099 if (STR(n)[1] == '/')
1100 return FloorDiv;
1101 else
1102 return Div;
1103 case '%':
1104 return Mod;
1105 case '<':
1106 return LShift;
1107 case '>':
1108 return RShift;
1109 case '&':
1110 return BitAnd;
1111 case '^':
1112 return BitXor;
1113 case '|':
1114 return BitOr;
1115 case '*':
1116 if (STR(n)[1] == '*')
1117 return Pow;
1118 else
1119 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001120 case '@':
1121 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001123 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001124 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 }
1126}
1127
1128static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001129ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001131 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 |'is' 'not'
1133 */
1134 REQ(n, comp_op);
1135 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001136 n = CHILD(n, 0);
1137 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 case LESS:
1139 return Lt;
1140 case GREATER:
1141 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001142 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 return Eq;
1144 case LESSEQUAL:
1145 return LtE;
1146 case GREATEREQUAL:
1147 return GtE;
1148 case NOTEQUAL:
1149 return NotEq;
1150 case NAME:
1151 if (strcmp(STR(n), "in") == 0)
1152 return In;
1153 if (strcmp(STR(n), "is") == 0)
1154 return Is;
1155 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001156 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 }
1161 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 /* handle "not in" and "is not" */
1163 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 case NAME:
1165 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1166 return NotIn;
1167 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1168 return IsNot;
1169 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001170 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001172 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001173 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 }
Neal Norwitz79792652005-11-14 04:25:03 +00001175 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001177 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178}
1179
1180static asdl_seq *
1181seq_for_testlist(struct compiling *c, const node *n)
1182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001184 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1185 */
Armin Rigo31441302005-10-21 12:57:31 +00001186 asdl_seq *seq;
1187 expr_ty expression;
1188 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001189 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001191 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 if (!seq)
1193 return NULL;
1194
1195 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001197 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198
Benjamin Peterson4905e802009-09-27 02:43:28 +00001199 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202
1203 assert(i / 2 < seq->size);
1204 asdl_seq_SET(seq, i / 2, expression);
1205 }
1206 return seq;
1207}
1208
Neal Norwitzc1505362006-12-28 06:47:50 +00001209static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001210ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001211{
1212 identifier name;
1213 expr_ty annotation = NULL;
1214 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001215 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001216
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001217 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001218 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 name = NEW_IDENTIFIER(ch);
1220 if (!name)
1221 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001222 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001223 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001224
1225 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1226 annotation = ast_for_expr(c, CHILD(n, 2));
1227 if (!annotation)
1228 return NULL;
1229 }
1230
Victor Stinnerc106c682015-11-06 17:01:48 +01001231 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001232 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001233 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001234 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235}
1236
Guido van Rossum4f72a782006-10-27 23:31:49 +00001237/* returns -1 if failed to handle keyword only arguments
1238 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001239 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240 ^^^
1241 start pointing here
1242 */
1243static int
1244handle_keywordonly_args(struct compiling *c, const node *n, int start,
1245 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1246{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001247 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001248 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001249 expr_ty expression, annotation;
1250 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001251 int i = start;
1252 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001253
1254 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001255 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001256 return -1;
1257 }
1258 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001259 while (i < NCH(n)) {
1260 ch = CHILD(n, i);
1261 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001262 case vfpdef:
1263 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001264 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001265 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001266 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001267 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001268 asdl_seq_SET(kwdefaults, j, expression);
1269 i += 2; /* '=' and test */
1270 }
1271 else { /* setting NULL if no default value exists */
1272 asdl_seq_SET(kwdefaults, j, NULL);
1273 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001274 if (NCH(ch) == 3) {
1275 /* ch is NAME ':' test */
1276 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001277 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001278 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001279 }
1280 else {
1281 annotation = NULL;
1282 }
1283 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001284 argname = NEW_IDENTIFIER(ch);
1285 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001287 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001288 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001289 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1290 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001291 if (!arg)
1292 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001294 i += 2; /* the name and the comma */
1295 break;
1296 case DOUBLESTAR:
1297 return i;
1298 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001299 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001300 goto error;
1301 }
1302 }
1303 return i;
1304 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307
Jeremy Hyltona8293132006-02-28 17:58:27 +00001308/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309
1310static arguments_ty
1311ast_for_arguments(struct compiling *c, const node *n)
1312{
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 /* This function handles both typedargslist (function definition)
1314 and varargslist (lambda definition).
1315
1316 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001317 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1318 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1319 | '**' tfpdef [',']]]
1320 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1321 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001322 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001323 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1324 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1325 | '**' vfpdef [',']]]
1326 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1327 | '**' vfpdef [',']
1328 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001329 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001330
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1333 int nposdefaults = 0, found_default = 0;
1334 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001335 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 node *ch;
1338
1339 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001340 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001341 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001342 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345
Jeremy Hyltone921e022008-07-17 16:37:17 +00001346 /* First count the number of positional args & defaults. The
1347 variable i is the loop index for this for loop and the next.
1348 The next loop picks up where the first leaves off.
1349 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351 ch = CHILD(n, i);
1352 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001353 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001354 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001355 if (i < NCH(n) && /* skip argument following star */
1356 (TYPE(CHILD(n, i)) == tfpdef ||
1357 TYPE(CHILD(n, i)) == vfpdef)) {
1358 i++;
1359 }
1360 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001362 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001363 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 defaults for keyword only args */
1368 for ( ; i < NCH(n); ++i) {
1369 ch = CHILD(n, i);
1370 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001371 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001373 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001375 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001377 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001379 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001381 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001383 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 since we set NULL as default for keyword only argument w/o default
1386 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001387 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001388 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001390 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391
1392 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001393 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001394 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001397 /* tfpdef: NAME [':' test]
1398 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 */
1400 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001401 j = 0; /* index for defaults */
1402 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 ch = CHILD(n, i);
1405 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001406 case tfpdef:
1407 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1409 anything other than EQUAL or a comma? */
1410 /* XXX Should NCH(n) check be made a separate check? */
1411 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001412 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1413 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001414 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 assert(posdefaults != NULL);
1416 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001418 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001421 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001422 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001423 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001425 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001426 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001427 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001428 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 i += 2; /* the name and the comma */
1430 break;
1431 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001432 if (i+1 >= NCH(n) ||
1433 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001434 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001435 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001436 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001437 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001438 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001439 if (TYPE(ch) == COMMA) {
1440 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 i += 2; /* now follows keyword only arguments */
1442 res = handle_keywordonly_args(c, n, i,
1443 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001444 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 i = res; /* res has new position to process */
1446 }
1447 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001448 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001449 if (!vararg)
1450 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001451
Guido van Rossum4f72a782006-10-27 23:31:49 +00001452 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001453 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1454 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001455 int res = 0;
1456 res = handle_keywordonly_args(c, n, i,
1457 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001458 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001459 i = res; /* res has new position to process */
1460 }
1461 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 break;
1463 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001464 ch = CHILD(n, i+1); /* tfpdef */
1465 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001466 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001467 if (!kwarg)
1468 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 i += 3;
1470 break;
1471 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001472 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 "unexpected node in varargslist: %d @ %d",
1474 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001475 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001476 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001478 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479}
1480
1481static expr_ty
1482ast_for_dotted_name(struct compiling *c, const node *n)
1483{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001484 expr_ty e;
1485 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001486 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 int i;
1488
1489 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001490
1491 lineno = LINENO(n);
1492 col_offset = n->n_col_offset;
1493
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 id = NEW_IDENTIFIER(CHILD(n, 0));
1495 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001496 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001497 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001499 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500
1501 for (i = 2; i < NCH(n); i+=2) {
1502 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001503 if (!id)
1504 return NULL;
1505 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1506 if (!e)
1507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 }
1509
1510 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511}
1512
1513static expr_ty
1514ast_for_decorator(struct compiling *c, const node *n)
1515{
1516 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1517 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001518 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001521 REQ(CHILD(n, 0), AT);
1522 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1525 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001526 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001529 d = name_expr;
1530 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 }
1532 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001533 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001534 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001535 if (!d)
1536 return NULL;
1537 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 }
1539 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 d = ast_for_call(c, CHILD(n, 3), name_expr);
1541 if (!d)
1542 return NULL;
1543 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 }
1545
1546 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547}
1548
1549static asdl_seq*
1550ast_for_decorators(struct compiling *c, const node *n)
1551{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001552 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001553 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001557 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 if (!decorator_seq)
1559 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001562 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001563 if (!d)
1564 return NULL;
1565 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 }
1567 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568}
1569
1570static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001571ast_for_funcdef_impl(struct compiling *c, const node *n,
1572 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001574 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001575 identifier name;
1576 arguments_ty args;
1577 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001578 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001579 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580
1581 REQ(n, funcdef);
1582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 name = NEW_IDENTIFIER(CHILD(n, name_i));
1584 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001585 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001586 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1589 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001590 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001591 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1592 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1593 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001594 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001595 name_i += 2;
1596 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 body = ast_for_suite(c, CHILD(n, name_i + 3));
1598 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001599 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600
Yury Selivanov75445082015-05-11 22:57:16 -04001601 if (is_async)
1602 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1603 LINENO(n),
1604 n->n_col_offset, c->c_arena);
1605 else
1606 return FunctionDef(name, args, body, decorator_seq, returns,
1607 LINENO(n),
1608 n->n_col_offset, c->c_arena);
1609}
1610
1611static stmt_ty
1612ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1613{
1614 /* async_funcdef: ASYNC funcdef */
1615 REQ(n, async_funcdef);
1616 REQ(CHILD(n, 0), ASYNC);
1617 REQ(CHILD(n, 1), funcdef);
1618
1619 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1620 1 /* is_async */);
1621}
1622
1623static stmt_ty
1624ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1625{
1626 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1627 return ast_for_funcdef_impl(c, n, decorator_seq,
1628 0 /* is_async */);
1629}
1630
1631
1632static stmt_ty
1633ast_for_async_stmt(struct compiling *c, const node *n)
1634{
1635 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1636 REQ(n, async_stmt);
1637 REQ(CHILD(n, 0), ASYNC);
1638
1639 switch (TYPE(CHILD(n, 1))) {
1640 case funcdef:
1641 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1642 1 /* is_async */);
1643 case with_stmt:
1644 return ast_for_with_stmt(c, CHILD(n, 1),
1645 1 /* is_async */);
1646
1647 case for_stmt:
1648 return ast_for_for_stmt(c, CHILD(n, 1),
1649 1 /* is_async */);
1650
1651 default:
1652 PyErr_Format(PyExc_SystemError,
1653 "invalid async stament: %s",
1654 STR(CHILD(n, 1)));
1655 return NULL;
1656 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657}
1658
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001659static stmt_ty
1660ast_for_decorated(struct compiling *c, const node *n)
1661{
Yury Selivanov75445082015-05-11 22:57:16 -04001662 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001663 stmt_ty thing = NULL;
1664 asdl_seq *decorator_seq = NULL;
1665
1666 REQ(n, decorated);
1667
1668 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1669 if (!decorator_seq)
1670 return NULL;
1671
1672 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001673 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001675
1676 if (TYPE(CHILD(n, 1)) == funcdef) {
1677 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1678 } else if (TYPE(CHILD(n, 1)) == classdef) {
1679 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001680 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1681 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001682 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001683 /* we count the decorators in when talking about the class' or
1684 * function's line number */
1685 if (thing) {
1686 thing->lineno = LINENO(n);
1687 thing->col_offset = n->n_col_offset;
1688 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001689 return thing;
1690}
1691
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692static expr_ty
1693ast_for_lambdef(struct compiling *c, const node *n)
1694{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001695 /* lambdef: 'lambda' [varargslist] ':' test
1696 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 arguments_ty args;
1698 expr_ty expression;
1699
1700 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001701 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 if (!args)
1703 return NULL;
1704 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001705 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 }
1708 else {
1709 args = ast_for_arguments(c, CHILD(n, 1));
1710 if (!args)
1711 return NULL;
1712 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001713 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 }
1716
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001717 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718}
1719
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001720static expr_ty
1721ast_for_ifexpr(struct compiling *c, const node *n)
1722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001724 expr_ty expression, body, orelse;
1725
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001726 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001727 body = ast_for_expr(c, CHILD(n, 0));
1728 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001729 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001730 expression = ast_for_expr(c, CHILD(n, 2));
1731 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001732 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001733 orelse = ast_for_expr(c, CHILD(n, 4));
1734 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001735 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001736 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1737 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001738}
1739
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001741 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742
Nick Coghlan650f0d02007-04-15 12:05:43 +00001743 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744*/
1745
1746static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001747count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001749 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750
Guido van Rossumd8faa362007-04-27 19:54:29 +00001751 count_comp_for:
1752 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001753 REQ(n, comp_for);
1754 if (NCH(n) == 5)
1755 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001756 else
1757 return n_fors;
1758 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001759 REQ(n, comp_iter);
1760 n = CHILD(n, 0);
1761 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001762 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001763 else if (TYPE(n) == comp_if) {
1764 if (NCH(n) == 3) {
1765 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001766 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001767 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001768 else
1769 return n_fors;
1770 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001771
Guido van Rossumd8faa362007-04-27 19:54:29 +00001772 /* Should never be reached */
1773 PyErr_SetString(PyExc_SystemError,
1774 "logic error in count_comp_fors");
1775 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776}
1777
Nick Coghlan650f0d02007-04-15 12:05:43 +00001778/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779
Nick Coghlan650f0d02007-04-15 12:05:43 +00001780 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781*/
1782
1783static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001784count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001786 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787
Guido van Rossumd8faa362007-04-27 19:54:29 +00001788 while (1) {
1789 REQ(n, comp_iter);
1790 if (TYPE(CHILD(n, 0)) == comp_for)
1791 return n_ifs;
1792 n = CHILD(n, 0);
1793 REQ(n, comp_if);
1794 n_ifs++;
1795 if (NCH(n) == 2)
1796 return n_ifs;
1797 n = CHILD(n, 2);
1798 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799}
1800
Guido van Rossum992d4a32007-07-11 13:09:30 +00001801static asdl_seq *
1802ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001805 asdl_seq *comps;
1806
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001807 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 if (n_fors == -1)
1809 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001810
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001811 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001812 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001814
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001816 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001818 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001819 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820
Guido van Rossum992d4a32007-07-11 13:09:30 +00001821 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822
Guido van Rossum992d4a32007-07-11 13:09:30 +00001823 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001824 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001825 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001827 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001828 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830
Thomas Wouters89f507f2006-12-13 04:49:30 +00001831 /* Check the # of children rather than the length of t, since
1832 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001833 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001834 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001835 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001837 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1838 c->c_arena),
1839 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001840 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001842
Guido van Rossum992d4a32007-07-11 13:09:30 +00001843 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 int j, n_ifs;
1845 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846
Guido van Rossum992d4a32007-07-11 13:09:30 +00001847 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001848 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001851
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001852 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001853 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001855
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001857 REQ(n, comp_iter);
1858 n = CHILD(n, 0);
1859 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860
Guido van Rossum992d4a32007-07-11 13:09:30 +00001861 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001862 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001863 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001864 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001865 if (NCH(n) == 3)
1866 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001868 /* on exit, must guarantee that n is a comp_for */
1869 if (TYPE(n) == comp_iter)
1870 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001871 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001873 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001875 return comps;
1876}
1877
1878static expr_ty
1879ast_for_itercomp(struct compiling *c, const node *n, int type)
1880{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001881 /* testlist_comp: (test|star_expr)
1882 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001883 expr_ty elt;
1884 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001885 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886
Guido van Rossum992d4a32007-07-11 13:09:30 +00001887 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001889 ch = CHILD(n, 0);
1890 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001891 if (!elt)
1892 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001893 if (elt->kind == Starred_kind) {
1894 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1895 return NULL;
1896 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897
Guido van Rossum992d4a32007-07-11 13:09:30 +00001898 comps = ast_for_comprehension(c, CHILD(n, 1));
1899 if (!comps)
1900 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001901
1902 if (type == COMP_GENEXP)
1903 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1904 else if (type == COMP_LISTCOMP)
1905 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1906 else if (type == COMP_SETCOMP)
1907 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1908 else
1909 /* Should never happen */
1910 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911}
1912
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001913/* Fills in the key, value pair corresponding to the dict element. In case
1914 * of an unpacking, key is NULL. *i is advanced by the number of ast
1915 * elements. Iff successful, nonzero is returned.
1916 */
1917static int
1918ast_for_dictelement(struct compiling *c, const node *n, int *i,
1919 expr_ty *key, expr_ty *value)
1920{
1921 expr_ty expression;
1922 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1923 assert(NCH(n) - *i >= 2);
1924
1925 expression = ast_for_expr(c, CHILD(n, *i + 1));
1926 if (!expression)
1927 return 0;
1928 *key = NULL;
1929 *value = expression;
1930
1931 *i += 2;
1932 }
1933 else {
1934 assert(NCH(n) - *i >= 3);
1935
1936 expression = ast_for_expr(c, CHILD(n, *i));
1937 if (!expression)
1938 return 0;
1939 *key = expression;
1940
1941 REQ(CHILD(n, *i + 1), COLON);
1942
1943 expression = ast_for_expr(c, CHILD(n, *i + 2));
1944 if (!expression)
1945 return 0;
1946 *value = expression;
1947
1948 *i += 3;
1949 }
1950 return 1;
1951}
1952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001954ast_for_dictcomp(struct compiling *c, const node *n)
1955{
1956 expr_ty key, value;
1957 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001958 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001960 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001961 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001962 assert(key);
1963 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001965 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001966 if (!comps)
1967 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968
Guido van Rossum992d4a32007-07-11 13:09:30 +00001969 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1970}
1971
1972static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001973ast_for_dictdisplay(struct compiling *c, const node *n)
1974{
1975 int i;
1976 int j;
1977 int size;
1978 asdl_seq *keys, *values;
1979
1980 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1981 keys = _Py_asdl_seq_new(size, c->c_arena);
1982 if (!keys)
1983 return NULL;
1984
1985 values = _Py_asdl_seq_new(size, c->c_arena);
1986 if (!values)
1987 return NULL;
1988
1989 j = 0;
1990 for (i = 0; i < NCH(n); i++) {
1991 expr_ty key, value;
1992
1993 if (!ast_for_dictelement(c, n, &i, &key, &value))
1994 return NULL;
1995 asdl_seq_SET(keys, j, key);
1996 asdl_seq_SET(values, j, value);
1997
1998 j++;
1999 }
2000 keys->size = j;
2001 values->size = j;
2002 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2003}
2004
2005static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002006ast_for_genexp(struct compiling *c, const node *n)
2007{
2008 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002009 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002010}
2011
2012static expr_ty
2013ast_for_listcomp(struct compiling *c, const node *n)
2014{
2015 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002016 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002017}
2018
2019static expr_ty
2020ast_for_setcomp(struct compiling *c, const node *n)
2021{
2022 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002023 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002024}
2025
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002026static expr_ty
2027ast_for_setdisplay(struct compiling *c, const node *n)
2028{
2029 int i;
2030 int size;
2031 asdl_seq *elts;
2032
2033 assert(TYPE(n) == (dictorsetmaker));
2034 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2035 elts = _Py_asdl_seq_new(size, c->c_arena);
2036 if (!elts)
2037 return NULL;
2038 for (i = 0; i < NCH(n); i += 2) {
2039 expr_ty expression;
2040 expression = ast_for_expr(c, CHILD(n, i));
2041 if (!expression)
2042 return NULL;
2043 asdl_seq_SET(elts, i / 2, expression);
2044 }
2045 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2046}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002047
2048static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049ast_for_atom(struct compiling *c, const node *n)
2050{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002051 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2052 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002053 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 */
2055 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002058 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002059 PyObject *name;
2060 const char *s = STR(ch);
2061 size_t len = strlen(s);
2062 if (len >= 4 && len <= 5) {
2063 if (!strcmp(s, "None"))
2064 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2065 if (!strcmp(s, "True"))
2066 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2067 if (!strcmp(s, "False"))
2068 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2069 }
2070 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002071 if (!name)
2072 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002073 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002074 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2075 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002077 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002078 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002079 const char *errtype = NULL;
2080 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2081 errtype = "unicode error";
2082 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2083 errtype = "value error";
2084 if (errtype) {
2085 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002086 PyObject *type, *value, *tback, *errstr;
2087 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002088 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002089 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002090 char *s = _PyUnicode_AsString(errstr);
2091 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002092 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002093 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002094 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002095 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002096 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002097 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002098 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002099 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002100 Py_XDECREF(tback);
2101 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002102 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002103 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002104 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 }
2106 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002107 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002108 if (!pynum)
2109 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002110
Victor Stinner43d81952013-07-17 00:57:58 +02002111 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2112 Py_DECREF(pynum);
2113 return NULL;
2114 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002115 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 }
Georg Brandldde00282007-03-18 19:01:53 +00002117 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002118 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002120 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121
Thomas Wouters89f507f2006-12-13 04:49:30 +00002122 if (TYPE(ch) == RPAR)
2123 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124
Thomas Wouters89f507f2006-12-13 04:49:30 +00002125 if (TYPE(ch) == yield_expr)
2126 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002129 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002130 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002131
Nick Coghlan650f0d02007-04-15 12:05:43 +00002132 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002134 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135
Thomas Wouters89f507f2006-12-13 04:49:30 +00002136 if (TYPE(ch) == RSQB)
2137 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138
Nick Coghlan650f0d02007-04-15 12:05:43 +00002139 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002140 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2141 asdl_seq *elts = seq_for_testlist(c, ch);
2142 if (!elts)
2143 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002144
Thomas Wouters89f507f2006-12-13 04:49:30 +00002145 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2146 }
2147 else
2148 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002150 /* dictorsetmaker: ( ((test ':' test | '**' test)
2151 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2152 * ((test | '*' test)
2153 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002154 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002155 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002156 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002157 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002158 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002159 }
2160 else {
2161 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2162 if (NCH(ch) == 1 ||
2163 (NCH(ch) > 1 &&
2164 TYPE(CHILD(ch, 1)) == COMMA)) {
2165 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002166 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002167 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002168 else if (NCH(ch) > 1 &&
2169 TYPE(CHILD(ch, 1)) == comp_for) {
2170 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002171 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002172 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002173 else if (NCH(ch) > 3 - is_dict &&
2174 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2175 /* It's a dictionary comprehension. */
2176 if (is_dict) {
2177 ast_error(c, n, "dict unpacking cannot be used in "
2178 "dict comprehension");
2179 return NULL;
2180 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002181 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002182 }
2183 else {
2184 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002185 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002186 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002187 if (res) {
2188 res->lineno = LINENO(n);
2189 res->col_offset = n->n_col_offset;
2190 }
2191 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002195 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2196 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 }
2198}
2199
2200static slice_ty
2201ast_for_slice(struct compiling *c, const node *n)
2202{
2203 node *ch;
2204 expr_ty lower = NULL, upper = NULL, step = NULL;
2205
2206 REQ(n, subscript);
2207
2208 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002209 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 sliceop: ':' [test]
2211 */
2212 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 if (NCH(n) == 1 && TYPE(ch) == test) {
2214 /* 'step' variable hold no significance in terms of being used over
2215 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 if (!step)
2218 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219
Thomas Wouters89f507f2006-12-13 04:49:30 +00002220 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221 }
2222
2223 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002224 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 if (!lower)
2226 return NULL;
2227 }
2228
2229 /* If there's an upper bound it's in the second or third position. */
2230 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002231 if (NCH(n) > 1) {
2232 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233
Thomas Wouters89f507f2006-12-13 04:49:30 +00002234 if (TYPE(n2) == test) {
2235 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 if (!upper)
2237 return NULL;
2238 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002239 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002241 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242
Thomas Wouters89f507f2006-12-13 04:49:30 +00002243 if (TYPE(n2) == test) {
2244 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 if (!upper)
2246 return NULL;
2247 }
2248 }
2249
2250 ch = CHILD(n, NCH(n) - 1);
2251 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002252 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002253 ch = CHILD(ch, 1);
2254 if (TYPE(ch) == test) {
2255 step = ast_for_expr(c, ch);
2256 if (!step)
2257 return NULL;
2258 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 }
2260 }
2261
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002262 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263}
2264
2265static expr_ty
2266ast_for_binop(struct compiling *c, const node *n)
2267{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002268 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002270 BinOp(BinOp(A, op, B), op, C).
2271 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272
Guido van Rossumd8faa362007-04-27 19:54:29 +00002273 int i, nops;
2274 expr_ty expr1, expr2, result;
2275 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276
Guido van Rossumd8faa362007-04-27 19:54:29 +00002277 expr1 = ast_for_expr(c, CHILD(n, 0));
2278 if (!expr1)
2279 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280
Guido van Rossumd8faa362007-04-27 19:54:29 +00002281 expr2 = ast_for_expr(c, CHILD(n, 2));
2282 if (!expr2)
2283 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284
Guido van Rossumd8faa362007-04-27 19:54:29 +00002285 newoperator = get_operator(CHILD(n, 1));
2286 if (!newoperator)
2287 return NULL;
2288
2289 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2290 c->c_arena);
2291 if (!result)
2292 return NULL;
2293
2294 nops = (NCH(n) - 1) / 2;
2295 for (i = 1; i < nops; i++) {
2296 expr_ty tmp_result, tmp;
2297 const node* next_oper = CHILD(n, i * 2 + 1);
2298
2299 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002300 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 return NULL;
2302
Guido van Rossumd8faa362007-04-27 19:54:29 +00002303 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2304 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 return NULL;
2306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002308 LINENO(next_oper), next_oper->n_col_offset,
2309 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311 return NULL;
2312 result = tmp_result;
2313 }
2314 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315}
2316
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002317static expr_ty
2318ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002321 subscriptlist: subscript (',' subscript)* [',']
2322 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2323 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002324 REQ(n, trailer);
2325 if (TYPE(CHILD(n, 0)) == LPAR) {
2326 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002327 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002328 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002329 else
2330 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002331 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002332 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002333 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2334 if (!attr_id)
2335 return NULL;
2336 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002337 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002338 }
2339 else {
2340 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002341 REQ(CHILD(n, 2), RSQB);
2342 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002343 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002344 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2345 if (!slc)
2346 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002347 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2348 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002349 }
2350 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002352 by treating the sequence as a tuple literal if there are
2353 no slice features.
2354 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002355 int j;
2356 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002357 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002358 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002359 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002360 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002361 if (!slices)
2362 return NULL;
2363 for (j = 0; j < NCH(n); j += 2) {
2364 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002365 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002366 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002367 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002368 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002369 asdl_seq_SET(slices, j / 2, slc);
2370 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002371 if (!simple) {
2372 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002373 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002374 }
2375 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002376 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002377 if (!elts)
2378 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002379 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2380 slc = (slice_ty)asdl_seq_GET(slices, j);
2381 assert(slc->kind == Index_kind && slc->v.Index.value);
2382 asdl_seq_SET(elts, j, slc->v.Index.value);
2383 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002384 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002385 if (!e)
2386 return NULL;
2387 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002388 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002389 }
2390 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002391}
2392
2393static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002394ast_for_factor(struct compiling *c, const node *n)
2395{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002396 expr_ty expression;
2397
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002398 expression = ast_for_expr(c, CHILD(n, 1));
2399 if (!expression)
2400 return NULL;
2401
2402 switch (TYPE(CHILD(n, 0))) {
2403 case PLUS:
2404 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2405 c->c_arena);
2406 case MINUS:
2407 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2408 c->c_arena);
2409 case TILDE:
2410 return UnaryOp(Invert, expression, LINENO(n),
2411 n->n_col_offset, c->c_arena);
2412 }
2413 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2414 TYPE(CHILD(n, 0)));
2415 return NULL;
2416}
2417
2418static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002419ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002420{
Yury Selivanov75445082015-05-11 22:57:16 -04002421 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002422 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002423
2424 REQ(n, atom_expr);
2425 nch = NCH(n);
2426
2427 if (TYPE(CHILD(n, 0)) == AWAIT) {
2428 start = 1;
2429 assert(nch > 1);
2430 }
2431
2432 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002433 if (!e)
2434 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002435 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002436 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002437 if (start && nch == 2) {
2438 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2439 }
2440
2441 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002442 node *ch = CHILD(n, i);
2443 if (TYPE(ch) != trailer)
2444 break;
2445 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002446 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002447 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002448 tmp->lineno = e->lineno;
2449 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002450 e = tmp;
2451 }
Yury Selivanov75445082015-05-11 22:57:16 -04002452
2453 if (start) {
2454 /* there was an AWAIT */
2455 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2456 }
2457 else {
2458 return e;
2459 }
2460}
2461
2462static expr_ty
2463ast_for_power(struct compiling *c, const node *n)
2464{
2465 /* power: atom trailer* ('**' factor)*
2466 */
2467 expr_ty e;
2468 REQ(n, power);
2469 e = ast_for_atom_expr(c, CHILD(n, 0));
2470 if (!e)
2471 return NULL;
2472 if (NCH(n) == 1)
2473 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002474 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2475 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002476 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002477 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002478 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002479 }
2480 return e;
2481}
2482
Guido van Rossum0368b722007-05-11 16:50:42 +00002483static expr_ty
2484ast_for_starred(struct compiling *c, const node *n)
2485{
2486 expr_ty tmp;
2487 REQ(n, star_expr);
2488
2489 tmp = ast_for_expr(c, CHILD(n, 1));
2490 if (!tmp)
2491 return NULL;
2492
2493 /* The Load context is changed later. */
2494 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2495}
2496
2497
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498/* Do not name a variable 'expr'! Will cause a compile error.
2499*/
2500
2501static expr_ty
2502ast_for_expr(struct compiling *c, const node *n)
2503{
2504 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002505 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002506 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 and_test: not_test ('and' not_test)*
2509 not_test: 'not' not_test | comparison
2510 comparison: expr (comp_op expr)*
2511 expr: xor_expr ('|' xor_expr)*
2512 xor_expr: and_expr ('^' and_expr)*
2513 and_expr: shift_expr ('&' shift_expr)*
2514 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2515 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002516 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002518 power: atom_expr ['**' factor]
2519 atom_expr: [AWAIT] atom trailer*
2520 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 */
2522
2523 asdl_seq *seq;
2524 int i;
2525
2526 loop:
2527 switch (TYPE(n)) {
2528 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002529 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002530 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002531 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002533 else if (NCH(n) > 1)
2534 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002535 /* Fallthrough */
2536 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 case and_test:
2538 if (NCH(n) == 1) {
2539 n = CHILD(n, 0);
2540 goto loop;
2541 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002542 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 if (!seq)
2544 return NULL;
2545 for (i = 0; i < NCH(n); i += 2) {
2546 expr_ty e = ast_for_expr(c, CHILD(n, i));
2547 if (!e)
2548 return NULL;
2549 asdl_seq_SET(seq, i / 2, e);
2550 }
2551 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002552 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2553 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002554 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002555 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 case not_test:
2557 if (NCH(n) == 1) {
2558 n = CHILD(n, 0);
2559 goto loop;
2560 }
2561 else {
2562 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2563 if (!expression)
2564 return NULL;
2565
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002566 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2567 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 }
2569 case comparison:
2570 if (NCH(n) == 1) {
2571 n = CHILD(n, 0);
2572 goto loop;
2573 }
2574 else {
2575 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002576 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002577 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002578 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579 if (!ops)
2580 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002581 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 return NULL;
2584 }
2585 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002586 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002588 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002589 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002591 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592
2593 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002594 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002596 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002598 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 asdl_seq_SET(cmps, i / 2, expression);
2600 }
2601 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002602 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002604 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002606 return Compare(expression, ops, cmps, LINENO(n),
2607 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 }
2609 break;
2610
Guido van Rossum0368b722007-05-11 16:50:42 +00002611 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 /* The next five cases all handle BinOps. The main body of code
2614 is the same in each case, but the switch turned inside out to
2615 reuse the code for each type of operator.
2616 */
2617 case expr:
2618 case xor_expr:
2619 case and_expr:
2620 case shift_expr:
2621 case arith_expr:
2622 case term:
2623 if (NCH(n) == 1) {
2624 n = CHILD(n, 0);
2625 goto loop;
2626 }
2627 return ast_for_binop(c, n);
2628 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002629 node *an = NULL;
2630 node *en = NULL;
2631 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002633 if (NCH(n) > 1)
2634 an = CHILD(n, 1); /* yield_arg */
2635 if (an) {
2636 en = CHILD(an, NCH(an) - 1);
2637 if (NCH(an) == 2) {
2638 is_from = 1;
2639 exp = ast_for_expr(c, en);
2640 }
2641 else
2642 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002643 if (!exp)
2644 return NULL;
2645 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002646 if (is_from)
2647 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2648 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002649 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002650 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 if (NCH(n) == 1) {
2652 n = CHILD(n, 0);
2653 goto loop;
2654 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002655 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002656 case power:
2657 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002659 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 return NULL;
2661 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002662 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 return NULL;
2664}
2665
2666static expr_ty
2667ast_for_call(struct compiling *c, const node *n, expr_ty func)
2668{
2669 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002670 arglist: argument (',' argument)* [',']
2671 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 */
2673
2674 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002675 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002676 asdl_seq *args;
2677 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678
2679 REQ(n, arglist);
2680
2681 nargs = 0;
2682 nkeywords = 0;
2683 ngens = 0;
2684 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002685 node *ch = CHILD(n, i);
2686 if (TYPE(ch) == argument) {
2687 if (NCH(ch) == 1)
2688 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002689 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002690 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002691 else if (TYPE(CHILD(ch, 0)) == STAR)
2692 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002694 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002695 nkeywords++;
2696 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 }
2698 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002699 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002700 "if not sole argument");
2701 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 }
2703
2704 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002705 ast_error(c, n, "more than 255 arguments");
2706 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 }
2708
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002709 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002711 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002712 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002714 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002715
2716 nargs = 0; /* positional arguments + iterable argument unpackings */
2717 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2718 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002720 node *ch = CHILD(n, i);
2721 if (TYPE(ch) == argument) {
2722 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002723 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002724 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002725 /* a positional argument */
2726 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002727 if (ndoublestars) {
2728 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002729 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002732 else {
2733 ast_error(c, chch,
2734 "positional argument follows "
2735 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002736 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002737 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002738 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002739 e = ast_for_expr(c, chch);
2740 if (!e)
2741 return NULL;
2742 asdl_seq_SET(args, nargs++, e);
2743 }
2744 else if (TYPE(chch) == STAR) {
2745 /* an iterable argument unpacking */
2746 expr_ty starred;
2747 if (ndoublestars) {
2748 ast_error(c, chch,
2749 "iterable argument unpacking follows "
2750 "keyword argument unpacking");
2751 return NULL;
2752 }
2753 e = ast_for_expr(c, CHILD(ch, 1));
2754 if (!e)
2755 return NULL;
2756 starred = Starred(e, Load, LINENO(chch),
2757 chch->n_col_offset,
2758 c->c_arena);
2759 if (!starred)
2760 return NULL;
2761 asdl_seq_SET(args, nargs++, starred);
2762
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 }
2764 else if (TYPE(chch) == DOUBLESTAR) {
2765 /* a keyword argument unpacking */
2766 keyword_ty kw;
2767 i++;
2768 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002770 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002771 kw = keyword(NULL, e, c->c_arena);
2772 asdl_seq_SET(keywords, nkeywords++, kw);
2773 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002775 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002776 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002777 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002779 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002780 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002782 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002783 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002784 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002785 identifier key, tmp;
2786 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002788 /* chch is test, but must be an identifier? */
2789 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 /* f(lambda x: x[0] = 3) ends up getting parsed with
2793 * LHS test = lambda x: x[0], and RHS test = 3.
2794 * SF bug 132313 points out that complaining about a keyword
2795 * then is very confusing.
2796 */
2797 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002798 ast_error(c, chch,
2799 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002800 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 }
2802 else if (e->kind != Name_kind) {
2803 ast_error(c, chch,
2804 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002805 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002806 }
2807 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002808 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002810 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002811 for (k = 0; k < nkeywords; k++) {
2812 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002813 if (tmp && !PyUnicode_Compare(tmp, key)) {
2814 ast_error(c, chch,
2815 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002816 return NULL;
2817 }
2818 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002819 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002821 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002822 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002824 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002825 asdl_seq_SET(keywords, nkeywords++, kw);
2826 }
2827 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 }
2829
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002830 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831}
2832
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002834ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002836 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002837 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002839 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002840 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002841 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002842 }
2843 else {
2844 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002845 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002848 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 else {
2850 asdl_seq *tmp = seq_for_testlist(c, n);
2851 if (!tmp)
2852 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002853 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002855}
2856
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857static stmt_ty
2858ast_for_expr_stmt(struct compiling *c, const node *n)
2859{
2860 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002861 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2862 ('=' (yield_expr|testlist_star_expr))*)
2863 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002864 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002865 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002866 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002867 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 */
2869
2870 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002871 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872 if (!e)
2873 return NULL;
2874
Thomas Wouters89f507f2006-12-13 04:49:30 +00002875 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 }
2877 else if (TYPE(CHILD(n, 1)) == augassign) {
2878 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002879 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002880 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881
Thomas Wouters89f507f2006-12-13 04:49:30 +00002882 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 if (!expr1)
2884 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002885 if(!set_context(c, expr1, Store, ch))
2886 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002887 /* set_context checks that most expressions are not the left side.
2888 Augmented assignments can only have a name, a subscript, or an
2889 attribute on the left, though, so we have to explicitly check for
2890 those. */
2891 switch (expr1->kind) {
2892 case Name_kind:
2893 case Attribute_kind:
2894 case Subscript_kind:
2895 break;
2896 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002897 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002898 return NULL;
2899 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900
Thomas Wouters89f507f2006-12-13 04:49:30 +00002901 ch = CHILD(n, 2);
2902 if (TYPE(ch) == testlist)
2903 expr2 = ast_for_testlist(c, ch);
2904 else
2905 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002906 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 return NULL;
2908
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002909 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002910 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911 return NULL;
2912
Thomas Wouters89f507f2006-12-13 04:49:30 +00002913 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002915 else if (TYPE(CHILD(n, 1)) == annassign) {
2916 expr_ty expr1, expr2, expr3;
2917 node *ch = CHILD(n, 0);
2918 node *deep, *ann = CHILD(n, 1);
2919 int simple = 1;
2920
2921 /* we keep track of parens to qualify (x) as expression not name */
2922 deep = ch;
2923 while (NCH(deep) == 1) {
2924 deep = CHILD(deep, 0);
2925 }
2926 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2927 simple = 0;
2928 }
2929 expr1 = ast_for_testlist(c, ch);
2930 if (!expr1) {
2931 return NULL;
2932 }
2933 switch (expr1->kind) {
2934 case Name_kind:
2935 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2936 return NULL;
2937 }
2938 expr1->v.Name.ctx = Store;
2939 break;
2940 case Attribute_kind:
2941 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2942 return NULL;
2943 }
2944 expr1->v.Attribute.ctx = Store;
2945 break;
2946 case Subscript_kind:
2947 expr1->v.Subscript.ctx = Store;
2948 break;
2949 case List_kind:
2950 ast_error(c, ch,
2951 "only single target (not list) can be annotated");
2952 return NULL;
2953 case Tuple_kind:
2954 ast_error(c, ch,
2955 "only single target (not tuple) can be annotated");
2956 return NULL;
2957 default:
2958 ast_error(c, ch,
2959 "illegal target for annotation");
2960 return NULL;
2961 }
2962
2963 if (expr1->kind != Name_kind) {
2964 simple = 0;
2965 }
2966 ch = CHILD(ann, 1);
2967 expr2 = ast_for_expr(c, ch);
2968 if (!expr2) {
2969 return NULL;
2970 }
2971 if (NCH(ann) == 2) {
2972 return AnnAssign(expr1, expr2, NULL, simple,
2973 LINENO(n), n->n_col_offset, c->c_arena);
2974 }
2975 else {
2976 ch = CHILD(ann, 3);
2977 expr3 = ast_for_expr(c, ch);
2978 if (!expr3) {
2979 return NULL;
2980 }
2981 return AnnAssign(expr1, expr2, expr3, simple,
2982 LINENO(n), n->n_col_offset, c->c_arena);
2983 }
2984 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002986 int i;
2987 asdl_seq *targets;
2988 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 expr_ty expression;
2990
Thomas Wouters89f507f2006-12-13 04:49:30 +00002991 /* a normal assignment */
2992 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002993 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002994 if (!targets)
2995 return NULL;
2996 for (i = 0; i < NCH(n) - 2; i += 2) {
2997 expr_ty e;
2998 node *ch = CHILD(n, i);
2999 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003000 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003001 return NULL;
3002 }
3003 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003007 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003008 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010
Thomas Wouters89f507f2006-12-13 04:49:30 +00003011 asdl_seq_SET(targets, i / 2, e);
3012 }
3013 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003014 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003015 expression = ast_for_testlist(c, value);
3016 else
3017 expression = ast_for_expr(c, value);
3018 if (!expression)
3019 return NULL;
3020 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022}
3023
Benjamin Peterson78565b22009-06-28 19:19:51 +00003024
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003026ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027{
3028 asdl_seq *seq;
3029 int i;
3030 expr_ty e;
3031
3032 REQ(n, exprlist);
3033
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003034 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003038 e = ast_for_expr(c, CHILD(n, i));
3039 if (!e)
3040 return NULL;
3041 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003042 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 }
3045 return seq;
3046}
3047
3048static stmt_ty
3049ast_for_del_stmt(struct compiling *c, const node *n)
3050{
3051 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053 /* del_stmt: 'del' exprlist */
3054 REQ(n, del_stmt);
3055
3056 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3057 if (!expr_list)
3058 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003059 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060}
3061
3062static stmt_ty
3063ast_for_flow_stmt(struct compiling *c, const node *n)
3064{
3065 /*
3066 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3067 | yield_stmt
3068 break_stmt: 'break'
3069 continue_stmt: 'continue'
3070 return_stmt: 'return' [testlist]
3071 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003072 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 raise_stmt: 'raise' [test [',' test [',' test]]]
3074 */
3075 node *ch;
3076
3077 REQ(n, flow_stmt);
3078 ch = CHILD(n, 0);
3079 switch (TYPE(ch)) {
3080 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003081 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003083 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003085 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3086 if (!exp)
3087 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003088 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089 }
3090 case return_stmt:
3091 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003092 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003094 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095 if (!expression)
3096 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003097 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003098 }
3099 case raise_stmt:
3100 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003101 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3102 else if (NCH(ch) >= 2) {
3103 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3105 if (!expression)
3106 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003107 if (NCH(ch) == 4) {
3108 cause = ast_for_expr(c, CHILD(ch, 3));
3109 if (!cause)
3110 return NULL;
3111 }
3112 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 }
3114 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003115 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 "unexpected flow_stmt: %d", TYPE(ch));
3117 return NULL;
3118 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003119
3120 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3121 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122}
3123
3124static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003125alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126{
3127 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003128 import_as_name: NAME ['as' NAME]
3129 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 dotted_name: NAME ('.' NAME)*
3131 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003132 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 loop:
3135 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003136 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003137 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003138 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003139 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003140 if (!name)
3141 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003142 if (NCH(n) == 3) {
3143 node *str_node = CHILD(n, 2);
3144 str = NEW_IDENTIFIER(str_node);
3145 if (!str)
3146 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003147 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003148 return NULL;
3149 }
3150 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003151 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003152 return NULL;
3153 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003154 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003155 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 case dotted_as_name:
3157 if (NCH(n) == 1) {
3158 n = CHILD(n, 0);
3159 goto loop;
3160 }
3161 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003162 node *asname_node = CHILD(n, 2);
3163 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003164 if (!a)
3165 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003167 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003168 if (!a->asname)
3169 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003170 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003171 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 return a;
3173 }
3174 break;
3175 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003176 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003177 node *name_node = CHILD(n, 0);
3178 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003179 if (!name)
3180 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003181 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003182 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003183 return alias(name, NULL, c->c_arena);
3184 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 else {
3186 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003187 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003188 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191
3192 len = 0;
3193 for (i = 0; i < NCH(n); i += 2)
3194 /* length of string plus one for the dot */
3195 len += strlen(STR(CHILD(n, i))) + 1;
3196 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003197 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 if (!str)
3199 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003200 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 if (!s)
3202 return NULL;
3203 for (i = 0; i < NCH(n); i += 2) {
3204 char *sch = STR(CHILD(n, i));
3205 strcpy(s, STR(CHILD(n, i)));
3206 s += strlen(sch);
3207 *s++ = '.';
3208 }
3209 --s;
3210 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3212 PyBytes_GET_SIZE(str),
3213 NULL);
3214 Py_DECREF(str);
3215 if (!uni)
3216 return NULL;
3217 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003218 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003219 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3220 Py_DECREF(str);
3221 return NULL;
3222 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003223 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 }
3225 break;
3226 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003227 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003228 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3229 Py_DECREF(str);
3230 return NULL;
3231 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003232 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003234 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235 "unexpected import name: %d", TYPE(n));
3236 return NULL;
3237 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003238
3239 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240 return NULL;
3241}
3242
3243static stmt_ty
3244ast_for_import_stmt(struct compiling *c, const node *n)
3245{
3246 /*
3247 import_stmt: import_name | import_from
3248 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003249 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3250 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003252 int lineno;
3253 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254 int i;
3255 asdl_seq *aliases;
3256
3257 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003258 lineno = LINENO(n);
3259 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003261 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003263 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003264 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003265 if (!aliases)
3266 return NULL;
3267 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003268 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003269 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003271 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003273 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003275 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003276 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003277 int idx, ndots = 0;
3278 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003279 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003281 /* Count the number of dots (for relative imports) and check for the
3282 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003283 for (idx = 1; idx < NCH(n); idx++) {
3284 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003285 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3286 if (!mod)
3287 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003288 idx++;
3289 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003290 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003292 ndots += 3;
3293 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003294 } else if (TYPE(CHILD(n, idx)) != DOT) {
3295 break;
3296 }
3297 ndots++;
3298 }
3299 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003300 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003301 case STAR:
3302 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 n = CHILD(n, idx);
3304 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003305 break;
3306 case LPAR:
3307 /* from ... import (x, y, z) */
3308 n = CHILD(n, idx + 1);
3309 n_children = NCH(n);
3310 break;
3311 case import_as_names:
3312 /* from ... import x, y, z */
3313 n = CHILD(n, idx);
3314 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003315 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003316 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317 " surrounding parentheses");
3318 return NULL;
3319 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 break;
3321 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003322 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 return NULL;
3324 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003326 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003327 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329
3330 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003331 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003332 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003333 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003335 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003336 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003337 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003339 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003340 if (!import_alias)
3341 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003342 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003343 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003345 if (mod != NULL)
3346 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003347 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003348 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349 }
Neal Norwitz79792652005-11-14 04:25:03 +00003350 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351 "unknown import statement: starts with command '%s'",
3352 STR(CHILD(n, 0)));
3353 return NULL;
3354}
3355
3356static stmt_ty
3357ast_for_global_stmt(struct compiling *c, const node *n)
3358{
3359 /* global_stmt: 'global' NAME (',' NAME)* */
3360 identifier name;
3361 asdl_seq *s;
3362 int i;
3363
3364 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003365 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003369 name = NEW_IDENTIFIER(CHILD(n, i));
3370 if (!name)
3371 return NULL;
3372 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003374 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375}
3376
3377static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003378ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3379{
3380 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3381 identifier name;
3382 asdl_seq *s;
3383 int i;
3384
3385 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003386 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003387 if (!s)
3388 return NULL;
3389 for (i = 1; i < NCH(n); i += 2) {
3390 name = NEW_IDENTIFIER(CHILD(n, i));
3391 if (!name)
3392 return NULL;
3393 asdl_seq_SET(s, i / 2, name);
3394 }
3395 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3396}
3397
3398static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399ast_for_assert_stmt(struct compiling *c, const node *n)
3400{
3401 /* assert_stmt: 'assert' test [',' test] */
3402 REQ(n, assert_stmt);
3403 if (NCH(n) == 2) {
3404 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3405 if (!expression)
3406 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003407 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408 }
3409 else if (NCH(n) == 4) {
3410 expr_ty expr1, expr2;
3411
3412 expr1 = ast_for_expr(c, CHILD(n, 1));
3413 if (!expr1)
3414 return NULL;
3415 expr2 = ast_for_expr(c, CHILD(n, 3));
3416 if (!expr2)
3417 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418
Thomas Wouters89f507f2006-12-13 04:49:30 +00003419 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 }
Neal Norwitz79792652005-11-14 04:25:03 +00003421 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422 "improper number of parts to 'assert' statement: %d",
3423 NCH(n));
3424 return NULL;
3425}
3426
3427static asdl_seq *
3428ast_for_suite(struct compiling *c, const node *n)
3429{
3430 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003431 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432 stmt_ty s;
3433 int i, total, num, end, pos = 0;
3434 node *ch;
3435
3436 REQ(n, suite);
3437
3438 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003439 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003443 n = CHILD(n, 0);
3444 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003446 */
3447 end = NCH(n) - 1;
3448 if (TYPE(CHILD(n, end - 1)) == SEMI)
3449 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003451 for (i = 0; i < end; i += 2) {
3452 ch = CHILD(n, i);
3453 s = ast_for_stmt(c, ch);
3454 if (!s)
3455 return NULL;
3456 asdl_seq_SET(seq, pos++, s);
3457 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 }
3459 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003460 for (i = 2; i < (NCH(n) - 1); i++) {
3461 ch = CHILD(n, i);
3462 REQ(ch, stmt);
3463 num = num_stmts(ch);
3464 if (num == 1) {
3465 /* small_stmt or compound_stmt with only one child */
3466 s = ast_for_stmt(c, ch);
3467 if (!s)
3468 return NULL;
3469 asdl_seq_SET(seq, pos++, s);
3470 }
3471 else {
3472 int j;
3473 ch = CHILD(ch, 0);
3474 REQ(ch, simple_stmt);
3475 for (j = 0; j < NCH(ch); j += 2) {
3476 /* statement terminates with a semi-colon ';' */
3477 if (NCH(CHILD(ch, j)) == 0) {
3478 assert((j + 1) == NCH(ch));
3479 break;
3480 }
3481 s = ast_for_stmt(c, CHILD(ch, j));
3482 if (!s)
3483 return NULL;
3484 asdl_seq_SET(seq, pos++, s);
3485 }
3486 }
3487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 }
3489 assert(pos == seq->size);
3490 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491}
3492
3493static stmt_ty
3494ast_for_if_stmt(struct compiling *c, const node *n)
3495{
3496 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3497 ['else' ':' suite]
3498 */
3499 char *s;
3500
3501 REQ(n, if_stmt);
3502
3503 if (NCH(n) == 4) {
3504 expr_ty expression;
3505 asdl_seq *suite_seq;
3506
3507 expression = ast_for_expr(c, CHILD(n, 1));
3508 if (!expression)
3509 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003511 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513
Guido van Rossumd8faa362007-04-27 19:54:29 +00003514 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3515 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518 s = STR(CHILD(n, 4));
3519 /* s[2], the third character in the string, will be
3520 's' for el_s_e, or
3521 'i' for el_i_f
3522 */
3523 if (s[2] == 's') {
3524 expr_ty expression;
3525 asdl_seq *seq1, *seq2;
3526
3527 expression = ast_for_expr(c, CHILD(n, 1));
3528 if (!expression)
3529 return NULL;
3530 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003531 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532 return NULL;
3533 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003534 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003535 return NULL;
3536
Guido van Rossumd8faa362007-04-27 19:54:29 +00003537 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3538 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 }
3540 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003541 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003542 expr_ty expression;
3543 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003544 asdl_seq *orelse = NULL;
3545 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546 /* must reference the child n_elif+1 since 'else' token is third,
3547 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003548 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3549 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3550 has_else = 1;
3551 n_elif -= 3;
3552 }
3553 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554
Thomas Wouters89f507f2006-12-13 04:49:30 +00003555 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003556 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003558 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003559 if (!orelse)
3560 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003562 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003564 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3565 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003567 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3568 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 asdl_seq_SET(orelse, 0,
3572 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003573 LINENO(CHILD(n, NCH(n) - 6)),
3574 CHILD(n, NCH(n) - 6)->n_col_offset,
3575 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003576 /* the just-created orelse handled the last elif */
3577 n_elif--;
3578 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579
Thomas Wouters89f507f2006-12-13 04:49:30 +00003580 for (i = 0; i < n_elif; i++) {
3581 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003582 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003583 if (!newobj)
3584 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003586 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003589 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591
Thomas Wouters89f507f2006-12-13 04:49:30 +00003592 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003594 LINENO(CHILD(n, off)),
3595 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003596 orelse = newobj;
3597 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003598 expression = ast_for_expr(c, CHILD(n, 1));
3599 if (!expression)
3600 return NULL;
3601 suite_seq = ast_for_suite(c, CHILD(n, 3));
3602 if (!suite_seq)
3603 return NULL;
3604 return If(expression, suite_seq, orelse,
3605 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003607
3608 PyErr_Format(PyExc_SystemError,
3609 "unexpected token in 'if' statement: %s", s);
3610 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611}
3612
3613static stmt_ty
3614ast_for_while_stmt(struct compiling *c, const node *n)
3615{
3616 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3617 REQ(n, while_stmt);
3618
3619 if (NCH(n) == 4) {
3620 expr_ty expression;
3621 asdl_seq *suite_seq;
3622
3623 expression = ast_for_expr(c, CHILD(n, 1));
3624 if (!expression)
3625 return NULL;
3626 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003627 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003629 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 }
3631 else if (NCH(n) == 7) {
3632 expr_ty expression;
3633 asdl_seq *seq1, *seq2;
3634
3635 expression = ast_for_expr(c, CHILD(n, 1));
3636 if (!expression)
3637 return NULL;
3638 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003639 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640 return NULL;
3641 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003642 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 return NULL;
3644
Thomas Wouters89f507f2006-12-13 04:49:30 +00003645 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003647
3648 PyErr_Format(PyExc_SystemError,
3649 "wrong number of tokens for 'while' statement: %d",
3650 NCH(n));
3651 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652}
3653
3654static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003655ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003657 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003658 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003659 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003660 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3662 REQ(n, for_stmt);
3663
3664 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003665 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666 if (!seq)
3667 return NULL;
3668 }
3669
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003670 node_target = CHILD(n, 1);
3671 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003672 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003674 /* Check the # of children rather than the length of _target, since
3675 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003676 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003677 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003678 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003680 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003682 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003683 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 return NULL;
3685 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003686 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 return NULL;
3688
Yury Selivanov75445082015-05-11 22:57:16 -04003689 if (is_async)
3690 return AsyncFor(target, expression, suite_seq, seq,
3691 LINENO(n), n->n_col_offset,
3692 c->c_arena);
3693 else
3694 return For(target, expression, suite_seq, seq,
3695 LINENO(n), n->n_col_offset,
3696 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697}
3698
3699static excepthandler_ty
3700ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3701{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003702 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 REQ(exc, except_clause);
3704 REQ(body, suite);
3705
3706 if (NCH(exc) == 1) {
3707 asdl_seq *suite_seq = ast_for_suite(c, body);
3708 if (!suite_seq)
3709 return NULL;
3710
Neal Norwitzad74aa82008-03-31 05:14:30 +00003711 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003712 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713 }
3714 else if (NCH(exc) == 2) {
3715 expr_ty expression;
3716 asdl_seq *suite_seq;
3717
3718 expression = ast_for_expr(c, CHILD(exc, 1));
3719 if (!expression)
3720 return NULL;
3721 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003722 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 return NULL;
3724
Neal Norwitzad74aa82008-03-31 05:14:30 +00003725 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003726 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 }
3728 else if (NCH(exc) == 4) {
3729 asdl_seq *suite_seq;
3730 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003731 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003732 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003734 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003735 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003737 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 return NULL;
3739 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003740 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 return NULL;
3742
Neal Norwitzad74aa82008-03-31 05:14:30 +00003743 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003744 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003746
3747 PyErr_Format(PyExc_SystemError,
3748 "wrong number of children for 'except' clause: %d",
3749 NCH(exc));
3750 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751}
3752
3753static stmt_ty
3754ast_for_try_stmt(struct compiling *c, const node *n)
3755{
Neal Norwitzf599f422005-12-17 21:33:47 +00003756 const int nch = NCH(n);
3757 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003758 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003759
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 REQ(n, try_stmt);
3761
Neal Norwitzf599f422005-12-17 21:33:47 +00003762 body = ast_for_suite(c, CHILD(n, 2));
3763 if (body == NULL)
3764 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765
Neal Norwitzf599f422005-12-17 21:33:47 +00003766 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3767 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3768 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3769 /* we can assume it's an "else",
3770 because nch >= 9 for try-else-finally and
3771 it would otherwise have a type of except_clause */
3772 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3773 if (orelse == NULL)
3774 return NULL;
3775 n_except--;
3776 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777
Neal Norwitzf599f422005-12-17 21:33:47 +00003778 finally = ast_for_suite(c, CHILD(n, nch - 1));
3779 if (finally == NULL)
3780 return NULL;
3781 n_except--;
3782 }
3783 else {
3784 /* we can assume it's an "else",
3785 otherwise it would have a type of except_clause */
3786 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3787 if (orelse == NULL)
3788 return NULL;
3789 n_except--;
3790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003792 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003793 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 return NULL;
3795 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796
Neal Norwitzf599f422005-12-17 21:33:47 +00003797 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003798 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003799 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003800 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003801 if (handlers == NULL)
3802 return NULL;
3803
3804 for (i = 0; i < n_except; i++) {
3805 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3806 CHILD(n, 5 + i * 3));
3807 if (!e)
3808 return NULL;
3809 asdl_seq_SET(handlers, i, e);
3810 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003811 }
3812
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003813 assert(finally != NULL || asdl_seq_LEN(handlers));
3814 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815}
3816
Georg Brandl0c315622009-05-25 21:10:36 +00003817/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003818static withitem_ty
3819ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003820{
3821 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003822
Georg Brandl0c315622009-05-25 21:10:36 +00003823 REQ(n, with_item);
3824 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003825 if (!context_expr)
3826 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003827 if (NCH(n) == 3) {
3828 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003829
3830 if (!optional_vars) {
3831 return NULL;
3832 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003833 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003834 return NULL;
3835 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003836 }
3837
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003838 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003839}
3840
Georg Brandl0c315622009-05-25 21:10:36 +00003841/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3842static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003843ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003844{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003845 int i, n_items;
3846 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003847
3848 REQ(n, with_stmt);
3849
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003850 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003851 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003852 if (!items)
3853 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003854 for (i = 1; i < NCH(n) - 2; i += 2) {
3855 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3856 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003857 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003858 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003859 }
3860
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003861 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3862 if (!body)
3863 return NULL;
3864
Yury Selivanov75445082015-05-11 22:57:16 -04003865 if (is_async)
3866 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3867 else
3868 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003869}
3870
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003871static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003872ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003874 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003875 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003876 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003877 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003878
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 REQ(n, classdef);
3880
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003881 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882 s = ast_for_suite(c, CHILD(n, 3));
3883 if (!s)
3884 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003885 classname = NEW_IDENTIFIER(CHILD(n, 1));
3886 if (!classname)
3887 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003888 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003889 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003890 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3891 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003892 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003893
3894 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003895 s = ast_for_suite(c, CHILD(n,5));
3896 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003897 return NULL;
3898 classname = NEW_IDENTIFIER(CHILD(n, 1));
3899 if (!classname)
3900 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003901 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003902 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003903 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3904 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 }
3906
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003907 /* class NAME '(' arglist ')' ':' suite */
3908 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003909 {
3910 PyObject *dummy_name;
3911 expr_ty dummy;
3912 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3913 if (!dummy_name)
3914 return NULL;
3915 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3916 call = ast_for_call(c, CHILD(n, 3), dummy);
3917 if (!call)
3918 return NULL;
3919 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003921 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003923 classname = NEW_IDENTIFIER(CHILD(n, 1));
3924 if (!classname)
3925 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003926 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003927 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003928
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003929 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003930 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931}
3932
3933static stmt_ty
3934ast_for_stmt(struct compiling *c, const node *n)
3935{
3936 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003937 assert(NCH(n) == 1);
3938 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 }
3940 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003941 assert(num_stmts(n) == 1);
3942 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 }
3944 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003945 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003946 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3947 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003948 */
3949 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 case expr_stmt:
3951 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 case del_stmt:
3953 return ast_for_del_stmt(c, n);
3954 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003955 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 case flow_stmt:
3957 return ast_for_flow_stmt(c, n);
3958 case import_stmt:
3959 return ast_for_import_stmt(c, n);
3960 case global_stmt:
3961 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003962 case nonlocal_stmt:
3963 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964 case assert_stmt:
3965 return ast_for_assert_stmt(c, n);
3966 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003967 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3969 TYPE(n), NCH(n));
3970 return NULL;
3971 }
3972 }
3973 else {
3974 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003975 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003976 */
3977 node *ch = CHILD(n, 0);
3978 REQ(n, compound_stmt);
3979 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 case if_stmt:
3981 return ast_for_if_stmt(c, ch);
3982 case while_stmt:
3983 return ast_for_while_stmt(c, ch);
3984 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003985 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 case try_stmt:
3987 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003988 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003989 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003991 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003993 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 case decorated:
3995 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003996 case async_stmt:
3997 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003998 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003999 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4001 TYPE(n), NCH(n));
4002 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004003 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 }
4005}
4006
4007static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004008parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004009{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004010 const char *end;
4011 long x;
4012 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004013 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004014 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004016 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004017 errno = 0;
4018 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004019 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004020 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004021 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004022 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004023 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004024 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004025 }
4026 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004027 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004028 if (*end == '\0') {
4029 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004030 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004031 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004032 }
4033 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004034 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004035 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004036 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4037 if (compl.imag == -1.0 && PyErr_Occurred())
4038 return NULL;
4039 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004040 }
4041 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004042 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004043 dx = PyOS_string_to_double(s, NULL, NULL);
4044 if (dx == -1.0 && PyErr_Occurred())
4045 return NULL;
4046 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048}
4049
4050static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004051decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004053 const char *s, *t;
4054 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4056 while (s < end && (*s & 0x80)) s++;
4057 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004058 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059}
4060
4061static PyObject *
Benjamin Peterson768921c2016-02-25 23:13:53 -08004062decode_unicode_with_escapes(struct compiling *c, const char *s, size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004064 PyObject *v, *u;
4065 char *buf;
4066 char *p;
4067 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004068
Benjamin Peterson202803a2016-02-25 22:34:45 -08004069 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004070 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004071 return NULL;
4072 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4073 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4074 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4075 if (u == NULL)
4076 return NULL;
4077 p = buf = PyBytes_AsString(u);
4078 end = s + len;
4079 while (s < end) {
4080 if (*s == '\\') {
4081 *p++ = *s++;
4082 if (*s & 0x80) {
4083 strcpy(p, "u005c");
4084 p += 5;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004085 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004086 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004087 if (*s & 0x80) { /* XXX inefficient */
4088 PyObject *w;
4089 int kind;
4090 void *data;
4091 Py_ssize_t len, i;
4092 w = decode_utf8(c, &s, end);
4093 if (w == NULL) {
4094 Py_DECREF(u);
4095 return NULL;
4096 }
4097 kind = PyUnicode_KIND(w);
4098 data = PyUnicode_DATA(w);
4099 len = PyUnicode_GET_LENGTH(w);
4100 for (i = 0; i < len; i++) {
4101 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4102 sprintf(p, "\\U%08x", chr);
4103 p += 10;
4104 }
4105 /* Should be impossible to overflow */
4106 assert(p - buf <= Py_SIZE(u));
4107 Py_DECREF(w);
4108 } else {
4109 *p++ = *s++;
4110 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004111 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004112 len = p - buf;
4113 s = buf;
4114
Eric V. Smith5567f892015-09-21 13:36:09 -04004115 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004116 Py_XDECREF(u);
4117 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118}
4119
Eric V. Smith235a6f02015-09-19 14:51:32 -04004120/* Compile this expression in to an expr_ty. We know that we can
4121 temporarily modify the character before the start of this string
4122 (it's '{'), and we know we can temporarily modify the character
4123 after this string (it is a '}'). Leverage this to create a
4124 sub-string with enough room for us to add parens around the
4125 expression. This is to allow strings with embedded newlines, for
4126 example. */
4127static expr_ty
Eric V. Smith1d44c412015-09-23 07:49:00 -04004128fstring_compile_expr(PyObject *str, Py_ssize_t expr_start,
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004129 Py_ssize_t expr_end, struct compiling *c, const node *n)
4130
Eric V. Smith235a6f02015-09-19 14:51:32 -04004131{
4132 PyCompilerFlags cf;
4133 mod_ty mod;
4134 char *utf_expr;
4135 Py_ssize_t i;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004136 Py_UCS4 end_ch = -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004137 int all_whitespace;
4138 PyObject *sub = NULL;
4139
4140 /* We only decref sub if we allocated it with a PyUnicode_Substring.
4141 decref_sub records that. */
4142 int decref_sub = 0;
4143
4144 assert(str);
4145
Eric V. Smith1d44c412015-09-23 07:49:00 -04004146 assert(expr_start >= 0 && expr_start < PyUnicode_GET_LENGTH(str));
4147 assert(expr_end >= 0 && expr_end < PyUnicode_GET_LENGTH(str));
4148 assert(expr_end >= expr_start);
4149
Martin Panterc2432f62015-10-07 11:15:15 +00004150 /* There has to be at least one character on each side of the
Eric V. Smith1d44c412015-09-23 07:49:00 -04004151 expression inside this str. This will have been caught before
4152 we're called. */
4153 assert(expr_start >= 1);
4154 assert(expr_end <= PyUnicode_GET_LENGTH(str)-1);
4155
Eric V. Smith235a6f02015-09-19 14:51:32 -04004156 /* If the substring is all whitespace, it's an error. We need to
4157 catch this here, and not when we call PyParser_ASTFromString,
4158 because turning the expression '' in to '()' would go from
4159 being invalid to valid. */
4160 /* Note that this code says an empty string is all
4161 whitespace. That's important. There's a test for it: f'{}'. */
4162 all_whitespace = 1;
4163 for (i = expr_start; i < expr_end; i++) {
4164 if (!Py_UNICODE_ISSPACE(PyUnicode_READ_CHAR(str, i))) {
4165 all_whitespace = 0;
4166 break;
4167 }
4168 }
4169 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004170 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004171 goto error;
4172 }
4173
4174 /* If the substring will be the entire source string, we can't use
4175 PyUnicode_Substring, since it will return another reference to
4176 our original string. Because we're modifying the string in
4177 place, that's a no-no. So, detect that case and just use our
4178 string directly. */
4179
4180 if (expr_start-1 == 0 && expr_end+1 == PyUnicode_GET_LENGTH(str)) {
Eric V. Smith1d44c412015-09-23 07:49:00 -04004181 /* If str is well formed, then the first and last chars must
4182 be '{' and '}', respectively. But, if there's a syntax
4183 error, for example f'{3!', then the last char won't be a
4184 closing brace. So, remember the last character we read in
4185 order for us to restore it. */
4186 end_ch = PyUnicode_ReadChar(str, expr_end-expr_start+1);
4187 assert(end_ch != (Py_UCS4)-1);
4188
4189 /* In all cases, however, start_ch must be '{'. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004190 assert(PyUnicode_ReadChar(str, 0) == '{');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004191
Eric V. Smith235a6f02015-09-19 14:51:32 -04004192 sub = str;
4193 } else {
4194 /* Create a substring object. It must be a new object, with
4195 refcount==1, so that we can modify it. */
4196 sub = PyUnicode_Substring(str, expr_start-1, expr_end+1);
4197 if (!sub)
4198 goto error;
4199 assert(sub != str); /* Make sure it's a new string. */
4200 decref_sub = 1; /* Remember to deallocate it on error. */
4201 }
4202
Eric V. Smith1d44c412015-09-23 07:49:00 -04004203 /* Put () around the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004204 if (PyUnicode_WriteChar(sub, 0, '(') < 0 ||
4205 PyUnicode_WriteChar(sub, expr_end-expr_start+1, ')') < 0)
4206 goto error;
4207
Eric V. Smith235a6f02015-09-19 14:51:32 -04004208 /* No need to free the memory returned here: it's managed by the
4209 string. */
4210 utf_expr = PyUnicode_AsUTF8(sub);
4211 if (!utf_expr)
4212 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004213
4214 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004215 mod = PyParser_ASTFromString(utf_expr, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004216 Py_eval_input, &cf, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004217 if (!mod)
4218 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004219
Eric V. Smith235a6f02015-09-19 14:51:32 -04004220 if (sub != str)
4221 /* Clear instead of decref in case we ever modify this code to change
4222 the error handling: this is safest because the XDECREF won't try
4223 and decref it when it's NULL. */
4224 /* No need to restore the chars in sub, since we know it's getting
4225 ready to get deleted (refcount must be 1, since we got a new string
4226 in PyUnicode_Substring). */
4227 Py_CLEAR(sub);
4228 else {
4229 assert(!decref_sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004230 assert(end_ch != (Py_UCS4)-1);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004231 /* Restore str, which we earlier modified directly. */
4232 if (PyUnicode_WriteChar(str, 0, '{') < 0 ||
Eric V. Smith1d44c412015-09-23 07:49:00 -04004233 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004234 goto error;
4235 }
4236 return mod->v.Expression.body;
4237
4238error:
4239 /* Only decref sub if it was the result of a call to SubString. */
4240 if (decref_sub)
4241 Py_XDECREF(sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004242
4243 if (end_ch != (Py_UCS4)-1) {
4244 /* We only get here if we modified str. Make sure that's the
4245 case: str will be equal to sub. */
4246 if (str == sub) {
4247 /* Don't check the error, because we've already set the
4248 error state (that's why we're in 'error', after
4249 all). */
4250 PyUnicode_WriteChar(str, 0, '{');
4251 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch);
4252 }
4253 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004254 return NULL;
4255}
4256
4257/* Return -1 on error.
4258
4259 Return 0 if we reached the end of the literal.
4260
4261 Return 1 if we haven't reached the end of the literal, but we want
4262 the caller to process the literal up to this point. Used for
4263 doubled braces.
4264*/
4265static int
4266fstring_find_literal(PyObject *str, Py_ssize_t *ofs, PyObject **literal,
4267 int recurse_lvl, struct compiling *c, const node *n)
4268{
4269 /* Get any literal string. It ends when we hit an un-doubled brace, or the
4270 end of the string. */
4271
4272 Py_ssize_t literal_start, literal_end;
4273 int result = 0;
4274
4275 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4276 void *data = PyUnicode_DATA(str);
4277
4278 assert(*literal == NULL);
4279
4280 literal_start = *ofs;
4281 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4282 Py_UCS4 ch = PyUnicode_READ(kind, data, *ofs);
4283 if (ch == '{' || ch == '}') {
4284 /* Check for doubled braces, but only at the top level. If
4285 we checked at every level, then f'{0:{3}}' would fail
4286 with the two closing braces. */
4287 if (recurse_lvl == 0) {
4288 if (*ofs + 1 < PyUnicode_GET_LENGTH(str) &&
4289 PyUnicode_READ(kind, data, *ofs + 1) == ch) {
4290 /* We're going to tell the caller that the literal ends
4291 here, but that they should continue scanning. But also
4292 skip over the second brace when we resume scanning. */
4293 literal_end = *ofs + 1;
4294 *ofs += 2;
4295 result = 1;
4296 goto done;
4297 }
4298
4299 /* Where a single '{' is the start of a new expression, a
4300 single '}' is not allowed. */
4301 if (ch == '}') {
4302 ast_error(c, n, "f-string: single '}' is not allowed");
4303 return -1;
4304 }
4305 }
4306
4307 /* We're either at a '{', which means we're starting another
4308 expression; or a '}', which means we're at the end of this
4309 f-string (for a nested format_spec). */
4310 break;
4311 }
4312 }
4313 literal_end = *ofs;
4314
4315 assert(*ofs == PyUnicode_GET_LENGTH(str) ||
4316 PyUnicode_READ(kind, data, *ofs) == '{' ||
4317 PyUnicode_READ(kind, data, *ofs) == '}');
4318done:
4319 if (literal_start != literal_end) {
4320 *literal = PyUnicode_Substring(str, literal_start, literal_end);
4321 if (!*literal)
4322 return -1;
4323 }
4324
4325 return result;
4326}
4327
4328/* Forward declaration because parsing is recursive. */
4329static expr_ty
4330fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4331 struct compiling *c, const node *n);
4332
4333/* Parse the f-string str, starting at ofs. We know *ofs starts an
4334 expression (so it must be a '{'). Returns the FormattedValue node,
4335 which includes the expression, conversion character, and
4336 format_spec expression.
4337
4338 Note that I don't do a perfect job here: I don't make sure that a
4339 closing brace doesn't match an opening paren, for example. It
4340 doesn't need to error on all invalid expressions, just correctly
4341 find the end of all valid ones. Any errors inside the expression
4342 will be caught when we parse it later. */
4343static int
4344fstring_find_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4345 expr_ty *expression, struct compiling *c, const node *n)
4346{
4347 /* Return -1 on error, else 0. */
4348
4349 Py_ssize_t expr_start;
4350 Py_ssize_t expr_end;
4351 expr_ty simple_expression;
4352 expr_ty format_spec = NULL; /* Optional format specifier. */
4353 Py_UCS4 conversion = -1; /* The conversion char. -1 if not specified. */
4354
4355 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4356 void *data = PyUnicode_DATA(str);
4357
4358 /* 0 if we're not in a string, else the quote char we're trying to
4359 match (single or double quote). */
4360 Py_UCS4 quote_char = 0;
4361
4362 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4363 int string_type = 0;
4364
4365 /* Keep track of nesting level for braces/parens/brackets in
4366 expressions. */
4367 Py_ssize_t nested_depth = 0;
4368
4369 /* Can only nest one level deep. */
4370 if (recurse_lvl >= 2) {
4371 ast_error(c, n, "f-string: expressions nested too deeply");
4372 return -1;
4373 }
4374
4375 /* The first char must be a left brace, or we wouldn't have gotten
4376 here. Skip over it. */
4377 assert(PyUnicode_READ(kind, data, *ofs) == '{');
4378 *ofs += 1;
4379
4380 expr_start = *ofs;
4381 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4382 Py_UCS4 ch;
4383
4384 /* Loop invariants. */
4385 assert(nested_depth >= 0);
4386 assert(*ofs >= expr_start);
4387 if (quote_char)
4388 assert(string_type == 1 || string_type == 3);
4389 else
4390 assert(string_type == 0);
4391
4392 ch = PyUnicode_READ(kind, data, *ofs);
4393 if (quote_char) {
4394 /* We're inside a string. See if we're at the end. */
4395 /* This code needs to implement the same non-error logic
4396 as tok_get from tokenizer.c, at the letter_quote
4397 label. To actually share that code would be a
4398 nightmare. But, it's unlikely to change and is small,
4399 so duplicate it here. Note we don't need to catch all
4400 of the errors, since they'll be caught when parsing the
4401 expression. We just need to match the non-error
4402 cases. Thus we can ignore \n in single-quoted strings,
4403 for example. Or non-terminated strings. */
4404 if (ch == quote_char) {
4405 /* Does this match the string_type (single or triple
4406 quoted)? */
4407 if (string_type == 3) {
4408 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4409 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4410 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4411 /* We're at the end of a triple quoted string. */
4412 *ofs += 2;
4413 string_type = 0;
4414 quote_char = 0;
4415 continue;
4416 }
4417 } else {
4418 /* We're at the end of a normal string. */
4419 quote_char = 0;
4420 string_type = 0;
4421 continue;
4422 }
4423 }
4424 /* We're inside a string, and not finished with the
4425 string. If this is a backslash, skip the next char (it
4426 might be an end quote that needs skipping). Otherwise,
4427 just consume this character normally. */
4428 if (ch == '\\' && *ofs+1 < PyUnicode_GET_LENGTH(str)) {
4429 /* Just skip the next char, whatever it is. */
4430 *ofs += 1;
4431 }
4432 } else if (ch == '\'' || ch == '"') {
4433 /* Is this a triple quoted string? */
4434 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4435 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4436 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4437 string_type = 3;
4438 *ofs += 2;
4439 } else {
4440 /* Start of a normal string. */
4441 string_type = 1;
4442 }
4443 /* Start looking for the end of the string. */
4444 quote_char = ch;
4445 } else if (ch == '[' || ch == '{' || ch == '(') {
4446 nested_depth++;
4447 } else if (nested_depth != 0 &&
4448 (ch == ']' || ch == '}' || ch == ')')) {
4449 nested_depth--;
4450 } else if (ch == '#') {
4451 /* Error: can't include a comment character, inside parens
4452 or not. */
4453 ast_error(c, n, "f-string cannot include '#'");
4454 return -1;
4455 } else if (nested_depth == 0 &&
4456 (ch == '!' || ch == ':' || ch == '}')) {
4457 /* First, test for the special case of "!=". Since '=' is
4458 not an allowed conversion character, nothing is lost in
4459 this test. */
4460 if (ch == '!' && *ofs+1 < PyUnicode_GET_LENGTH(str) &&
4461 PyUnicode_READ(kind, data, *ofs+1) == '=')
4462 /* This isn't a conversion character, just continue. */
4463 continue;
4464
4465 /* Normal way out of this loop. */
4466 break;
4467 } else {
4468 /* Just consume this char and loop around. */
4469 }
4470 }
4471 expr_end = *ofs;
4472 /* If we leave this loop in a string or with mismatched parens, we
4473 don't care. We'll get a syntax error when compiling the
4474 expression. But, we can produce a better error message, so
4475 let's just do that.*/
4476 if (quote_char) {
4477 ast_error(c, n, "f-string: unterminated string");
4478 return -1;
4479 }
4480 if (nested_depth) {
4481 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4482 return -1;
4483 }
4484
Eric V. Smith235a6f02015-09-19 14:51:32 -04004485 if (*ofs >= PyUnicode_GET_LENGTH(str))
4486 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004487
4488 /* Compile the expression as soon as possible, so we show errors
4489 related to the expression before errors related to the
4490 conversion or format_spec. */
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004491 simple_expression = fstring_compile_expr(str, expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004492 if (!simple_expression)
4493 return -1;
4494
4495 /* Check for a conversion char, if present. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004496 if (PyUnicode_READ(kind, data, *ofs) == '!') {
4497 *ofs += 1;
4498 if (*ofs >= PyUnicode_GET_LENGTH(str))
4499 goto unexpected_end_of_string;
4500
4501 conversion = PyUnicode_READ(kind, data, *ofs);
4502 *ofs += 1;
4503
4504 /* Validate the conversion. */
4505 if (!(conversion == 's' || conversion == 'r'
4506 || conversion == 'a')) {
4507 ast_error(c, n, "f-string: invalid conversion character: "
4508 "expected 's', 'r', or 'a'");
4509 return -1;
4510 }
4511 }
4512
4513 /* Check for the format spec, if present. */
4514 if (*ofs >= PyUnicode_GET_LENGTH(str))
4515 goto unexpected_end_of_string;
4516 if (PyUnicode_READ(kind, data, *ofs) == ':') {
4517 *ofs += 1;
4518 if (*ofs >= PyUnicode_GET_LENGTH(str))
4519 goto unexpected_end_of_string;
4520
4521 /* Parse the format spec. */
4522 format_spec = fstring_parse(str, ofs, recurse_lvl+1, c, n);
4523 if (!format_spec)
4524 return -1;
4525 }
4526
4527 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4528 PyUnicode_READ(kind, data, *ofs) != '}')
4529 goto unexpected_end_of_string;
4530
4531 /* We're at a right brace. Consume it. */
4532 assert(*ofs < PyUnicode_GET_LENGTH(str));
4533 assert(PyUnicode_READ(kind, data, *ofs) == '}');
4534 *ofs += 1;
4535
Eric V. Smith235a6f02015-09-19 14:51:32 -04004536 /* And now create the FormattedValue node that represents this entire
4537 expression with the conversion and format spec. */
4538 *expression = FormattedValue(simple_expression, (int)conversion,
4539 format_spec, LINENO(n), n->n_col_offset,
4540 c->c_arena);
4541 if (!*expression)
4542 return -1;
4543
4544 return 0;
4545
4546unexpected_end_of_string:
4547 ast_error(c, n, "f-string: expecting '}'");
4548 return -1;
4549}
4550
4551/* Return -1 on error.
4552
4553 Return 0 if we have a literal (possible zero length) and an
4554 expression (zero length if at the end of the string.
4555
4556 Return 1 if we have a literal, but no expression, and we want the
4557 caller to call us again. This is used to deal with doubled
4558 braces.
4559
4560 When called multiple times on the string 'a{{b{0}c', this function
4561 will return:
4562
4563 1. the literal 'a{' with no expression, and a return value
4564 of 1. Despite the fact that there's no expression, the return
4565 value of 1 means we're not finished yet.
4566
4567 2. the literal 'b' and the expression '0', with a return value of
4568 0. The fact that there's an expression means we're not finished.
4569
4570 3. literal 'c' with no expression and a return value of 0. The
4571 combination of the return value of 0 with no expression means
4572 we're finished.
4573*/
4574static int
4575fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4576 PyObject **literal, expr_ty *expression,
4577 struct compiling *c, const node *n)
4578{
4579 int result;
4580
4581 assert(*literal == NULL && *expression == NULL);
4582
4583 /* Get any literal string. */
4584 result = fstring_find_literal(str, ofs, literal, recurse_lvl, c, n);
4585 if (result < 0)
4586 goto error;
4587
4588 assert(result == 0 || result == 1);
4589
4590 if (result == 1)
4591 /* We have a literal, but don't look at the expression. */
4592 return 1;
4593
4594 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4595
4596 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4597 PyUnicode_READ_CHAR(str, *ofs) == '}')
4598 /* We're at the end of the string or the end of a nested
4599 f-string: no expression. The top-level error case where we
4600 expect to be at the end of the string but we're at a '}' is
4601 handled later. */
4602 return 0;
4603
4604 /* We must now be the start of an expression, on a '{'. */
4605 assert(*ofs < PyUnicode_GET_LENGTH(str) &&
4606 PyUnicode_READ_CHAR(str, *ofs) == '{');
4607
4608 if (fstring_find_expr(str, ofs, recurse_lvl, expression, c, n) < 0)
4609 goto error;
4610
4611 return 0;
4612
4613error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004614 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004615 return -1;
4616}
4617
4618#define EXPRLIST_N_CACHED 64
4619
4620typedef struct {
4621 /* Incrementally build an array of expr_ty, so be used in an
4622 asdl_seq. Cache some small but reasonably sized number of
4623 expr_ty's, and then after that start dynamically allocating,
4624 doubling the number allocated each time. Note that the f-string
4625 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4626 Str for the literal 'a'. So you add expr_ty's about twice as
4627 fast as you add exressions in an f-string. */
4628
4629 Py_ssize_t allocated; /* Number we've allocated. */
4630 Py_ssize_t size; /* Number we've used. */
4631 expr_ty *p; /* Pointer to the memory we're actually
4632 using. Will point to 'data' until we
4633 start dynamically allocating. */
4634 expr_ty data[EXPRLIST_N_CACHED];
4635} ExprList;
4636
4637#ifdef NDEBUG
4638#define ExprList_check_invariants(l)
4639#else
4640static void
4641ExprList_check_invariants(ExprList *l)
4642{
4643 /* Check our invariants. Make sure this object is "live", and
4644 hasn't been deallocated. */
4645 assert(l->size >= 0);
4646 assert(l->p != NULL);
4647 if (l->size <= EXPRLIST_N_CACHED)
4648 assert(l->data == l->p);
4649}
4650#endif
4651
4652static void
4653ExprList_Init(ExprList *l)
4654{
4655 l->allocated = EXPRLIST_N_CACHED;
4656 l->size = 0;
4657
4658 /* Until we start allocating dynamically, p points to data. */
4659 l->p = l->data;
4660
4661 ExprList_check_invariants(l);
4662}
4663
4664static int
4665ExprList_Append(ExprList *l, expr_ty exp)
4666{
4667 ExprList_check_invariants(l);
4668 if (l->size >= l->allocated) {
4669 /* We need to alloc (or realloc) the memory. */
4670 Py_ssize_t new_size = l->allocated * 2;
4671
4672 /* See if we've ever allocated anything dynamically. */
4673 if (l->p == l->data) {
4674 Py_ssize_t i;
4675 /* We're still using the cached data. Switch to
4676 alloc-ing. */
4677 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4678 if (!l->p)
4679 return -1;
4680 /* Copy the cached data into the new buffer. */
4681 for (i = 0; i < l->size; i++)
4682 l->p[i] = l->data[i];
4683 } else {
4684 /* Just realloc. */
4685 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4686 if (!tmp) {
4687 PyMem_RawFree(l->p);
4688 l->p = NULL;
4689 return -1;
4690 }
4691 l->p = tmp;
4692 }
4693
4694 l->allocated = new_size;
4695 assert(l->allocated == 2 * l->size);
4696 }
4697
4698 l->p[l->size++] = exp;
4699
4700 ExprList_check_invariants(l);
4701 return 0;
4702}
4703
4704static void
4705ExprList_Dealloc(ExprList *l)
4706{
4707 ExprList_check_invariants(l);
4708
4709 /* If there's been an error, or we've never dynamically allocated,
4710 do nothing. */
4711 if (!l->p || l->p == l->data) {
4712 /* Do nothing. */
4713 } else {
4714 /* We have dynamically allocated. Free the memory. */
4715 PyMem_RawFree(l->p);
4716 }
4717 l->p = NULL;
4718 l->size = -1;
4719}
4720
4721static asdl_seq *
4722ExprList_Finish(ExprList *l, PyArena *arena)
4723{
4724 asdl_seq *seq;
4725
4726 ExprList_check_invariants(l);
4727
4728 /* Allocate the asdl_seq and copy the expressions in to it. */
4729 seq = _Py_asdl_seq_new(l->size, arena);
4730 if (seq) {
4731 Py_ssize_t i;
4732 for (i = 0; i < l->size; i++)
4733 asdl_seq_SET(seq, i, l->p[i]);
4734 }
4735 ExprList_Dealloc(l);
4736 return seq;
4737}
4738
4739/* The FstringParser is designed to add a mix of strings and
4740 f-strings, and concat them together as needed. Ultimately, it
4741 generates an expr_ty. */
4742typedef struct {
4743 PyObject *last_str;
4744 ExprList expr_list;
4745} FstringParser;
4746
4747#ifdef NDEBUG
4748#define FstringParser_check_invariants(state)
4749#else
4750static void
4751FstringParser_check_invariants(FstringParser *state)
4752{
4753 if (state->last_str)
4754 assert(PyUnicode_CheckExact(state->last_str));
4755 ExprList_check_invariants(&state->expr_list);
4756}
4757#endif
4758
4759static void
4760FstringParser_Init(FstringParser *state)
4761{
4762 state->last_str = NULL;
4763 ExprList_Init(&state->expr_list);
4764 FstringParser_check_invariants(state);
4765}
4766
4767static void
4768FstringParser_Dealloc(FstringParser *state)
4769{
4770 FstringParser_check_invariants(state);
4771
4772 Py_XDECREF(state->last_str);
4773 ExprList_Dealloc(&state->expr_list);
4774}
4775
4776/* Make a Str node, but decref the PyUnicode object being added. */
4777static expr_ty
4778make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4779{
4780 PyObject *s = *str;
4781 *str = NULL;
4782 assert(PyUnicode_CheckExact(s));
4783 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4784 Py_DECREF(s);
4785 return NULL;
4786 }
4787 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4788}
4789
4790/* Add a non-f-string (that is, a regular literal string). str is
4791 decref'd. */
4792static int
4793FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4794{
4795 FstringParser_check_invariants(state);
4796
4797 assert(PyUnicode_CheckExact(str));
4798
4799 if (PyUnicode_GET_LENGTH(str) == 0) {
4800 Py_DECREF(str);
4801 return 0;
4802 }
4803
4804 if (!state->last_str) {
4805 /* We didn't have a string before, so just remember this one. */
4806 state->last_str = str;
4807 } else {
4808 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004809 PyUnicode_AppendAndDel(&state->last_str, str);
4810 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004811 return -1;
4812 }
4813 FstringParser_check_invariants(state);
4814 return 0;
4815}
4816
4817/* Parse an f-string. The f-string is in str, starting at ofs, with no 'f'
4818 or quotes. str is not decref'd, since we don't know if it's used elsewhere.
4819 And if we're only looking at a part of a string, then decref'ing is
4820 definitely not the right thing to do! */
4821static int
4822FstringParser_ConcatFstring(FstringParser *state, PyObject *str,
4823 Py_ssize_t *ofs, int recurse_lvl,
4824 struct compiling *c, const node *n)
4825{
4826 FstringParser_check_invariants(state);
4827
4828 /* Parse the f-string. */
4829 while (1) {
4830 PyObject *literal = NULL;
4831 expr_ty expression = NULL;
4832
4833 /* If there's a zero length literal in front of the
4834 expression, literal will be NULL. If we're at the end of
4835 the f-string, expression will be NULL (unless result == 1,
4836 see below). */
4837 int result = fstring_find_literal_and_expr(str, ofs, recurse_lvl,
4838 &literal, &expression,
4839 c, n);
4840 if (result < 0)
4841 return -1;
4842
4843 /* Add the literal, if any. */
4844 if (!literal) {
4845 /* Do nothing. Just leave last_str alone (and possibly
4846 NULL). */
4847 } else if (!state->last_str) {
4848 state->last_str = literal;
4849 literal = NULL;
4850 } else {
4851 /* We have a literal, concatenate it. */
4852 assert(PyUnicode_GET_LENGTH(literal) != 0);
4853 if (FstringParser_ConcatAndDel(state, literal) < 0)
4854 return -1;
4855 literal = NULL;
4856 }
4857 assert(!state->last_str ||
4858 PyUnicode_GET_LENGTH(state->last_str) != 0);
4859
4860 /* We've dealt with the literal now. It can't be leaked on further
4861 errors. */
4862 assert(literal == NULL);
4863
4864 /* See if we should just loop around to get the next literal
4865 and expression, while ignoring the expression this
4866 time. This is used for un-doubling braces, as an
4867 optimization. */
4868 if (result == 1)
4869 continue;
4870
4871 if (!expression)
4872 /* We're done with this f-string. */
4873 break;
4874
4875 /* We know we have an expression. Convert any existing string
4876 to a Str node. */
4877 if (!state->last_str) {
4878 /* Do nothing. No previous literal. */
4879 } else {
4880 /* Convert the existing last_str literal to a Str node. */
4881 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4882 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4883 return -1;
4884 }
4885
4886 if (ExprList_Append(&state->expr_list, expression) < 0)
4887 return -1;
4888 }
4889
4890 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4891
4892 /* If recurse_lvl is zero, then we must be at the end of the
4893 string. Otherwise, we must be at a right brace. */
4894
4895 if (recurse_lvl == 0 && *ofs < PyUnicode_GET_LENGTH(str)) {
4896 ast_error(c, n, "f-string: unexpected end of string");
4897 return -1;
4898 }
4899 if (recurse_lvl != 0 && PyUnicode_READ_CHAR(str, *ofs) != '}') {
4900 ast_error(c, n, "f-string: expecting '}'");
4901 return -1;
4902 }
4903
4904 FstringParser_check_invariants(state);
4905 return 0;
4906}
4907
4908/* Convert the partial state reflected in last_str and expr_list to an
4909 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4910static expr_ty
4911FstringParser_Finish(FstringParser *state, struct compiling *c,
4912 const node *n)
4913{
4914 asdl_seq *seq;
4915
4916 FstringParser_check_invariants(state);
4917
4918 /* If we're just a constant string with no expressions, return
4919 that. */
4920 if(state->expr_list.size == 0) {
4921 if (!state->last_str) {
4922 /* Create a zero length string. */
4923 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4924 if (!state->last_str)
4925 goto error;
4926 }
4927 return make_str_node_and_del(&state->last_str, c, n);
4928 }
4929
4930 /* Create a Str node out of last_str, if needed. It will be the
4931 last node in our expression list. */
4932 if (state->last_str) {
4933 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4934 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4935 goto error;
4936 }
4937 /* This has already been freed. */
4938 assert(state->last_str == NULL);
4939
4940 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4941 if (!seq)
4942 goto error;
4943
4944 /* If there's only one expression, return it. Otherwise, we need
4945 to join them together. */
4946 if (seq->size == 1)
4947 return seq->elements[0];
4948
4949 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4950
4951error:
4952 FstringParser_Dealloc(state);
4953 return NULL;
4954}
4955
4956/* Given an f-string (with no 'f' or quotes) that's in str starting at
4957 ofs, parse it into an expr_ty. Return NULL on error. Does not
4958 decref str. */
4959static expr_ty
4960fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4961 struct compiling *c, const node *n)
4962{
4963 FstringParser state;
4964
4965 FstringParser_Init(&state);
4966 if (FstringParser_ConcatFstring(&state, str, ofs, recurse_lvl,
4967 c, n) < 0) {
4968 FstringParser_Dealloc(&state);
4969 return NULL;
4970 }
4971
4972 return FstringParser_Finish(&state, c, n);
4973}
4974
4975/* n is a Python string literal, including the bracketing quote
4976 characters, and r, b, u, &/or f prefixes (if any), and embedded
4977 escape sequences (if any). parsestr parses it, and returns the
4978 decoded Python string object. If the string is an f-string, set
4979 *fmode and return the unparsed string object.
4980*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004981static PyObject *
Eric V. Smith235a6f02015-09-19 14:51:32 -04004982parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004983{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004984 size_t len;
4985 const char *s = STR(n);
4986 int quote = Py_CHARMASK(*s);
4987 int rawmode = 0;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004988 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004989 while (!*bytesmode || !rawmode) {
4990 if (quote == 'b' || quote == 'B') {
4991 quote = *++s;
4992 *bytesmode = 1;
4993 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004994 else if (quote == 'u' || quote == 'U') {
4995 quote = *++s;
4996 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004997 else if (quote == 'r' || quote == 'R') {
4998 quote = *++s;
4999 rawmode = 1;
5000 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005001 else if (quote == 'f' || quote == 'F') {
5002 quote = *++s;
5003 *fmode = 1;
5004 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005005 else {
5006 break;
5007 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005008 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005009 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005010 if (*fmode && *bytesmode) {
5011 PyErr_BadInternalCall();
5012 return NULL;
5013 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005014 if (quote != '\'' && quote != '\"') {
5015 PyErr_BadInternalCall();
5016 return NULL;
5017 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005018 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005019 s++;
5020 len = strlen(s);
5021 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005023 "string to parse is too long");
5024 return NULL;
5025 }
5026 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005028 PyErr_BadInternalCall();
5029 return NULL;
5030 }
5031 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005032 /* A triple quoted string. We've already skipped one quote at
5033 the start and one at the end of the string. Now skip the
5034 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005035 s += 2;
5036 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005037 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005038 if (s[--len] != quote || s[--len] != quote) {
5039 PyErr_BadInternalCall();
5040 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005041 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005042 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005043
5044 /* Temporary hack: if this is an f-string, no backslashes are allowed. */
5045 /* See issue 27921. */
5046 if (*fmode && strchr(s, '\\') != NULL) {
5047 /* Syntax error. At a later date fix this so it only checks for
5048 backslashes within the braces. */
5049 ast_error(c, n, "backslashes not allowed in f-strings");
5050 return NULL;
5051 }
5052
Benjamin Peterson768921c2016-02-25 23:13:53 -08005053 /* Avoid invoking escape decoding routines if possible. */
5054 rawmode = rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005055 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005056 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005057 const char *ch;
5058 for (ch = s; *ch; ch++) {
5059 if (Py_CHARMASK(*ch) >= 0x80) {
5060 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005061 "literal characters.");
5062 return NULL;
5063 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005064 }
Benjamin Peterson768921c2016-02-25 23:13:53 -08005065 if (rawmode)
Christian Heimes72b710a2008-05-26 13:28:38 +00005066 return PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005067 else
5068 return PyBytes_DecodeEscape(s, len, NULL, /* ignored */ 0, NULL);
5069 } else {
5070 if (rawmode)
5071 return PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
5072 else
5073 return decode_unicode_with_escapes(c, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005074 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005075}
5076
Eric V. Smith235a6f02015-09-19 14:51:32 -04005077/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5078 each STRING atom, and process it as needed. For bytes, just
5079 concatenate them together, and the result will be a Bytes node. For
5080 normal strings and f-strings, concatenate them together. The result
5081 will be a Str node if there were no f-strings; a FormattedValue
5082 node if there's just an f-string (with no leading or trailing
5083 literals), or a JoinedStr node if there are multiple f-strings or
5084 any literals involved. */
5085static expr_ty
5086parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005087{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005088 int bytesmode = 0;
5089 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005090 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005091
5092 FstringParser state;
5093 FstringParser_Init(&state);
5094
5095 for (i = 0; i < NCH(n); i++) {
5096 int this_bytesmode = 0;
5097 int this_fmode = 0;
5098 PyObject *s;
5099
5100 REQ(CHILD(n, i), STRING);
5101 s = parsestr(c, CHILD(n, i), &this_bytesmode, &this_fmode);
5102 if (!s)
5103 goto error;
5104
5105 /* Check that we're not mixing bytes with unicode. */
5106 if (i != 0 && bytesmode != this_bytesmode) {
5107 ast_error(c, n, "cannot mix bytes and nonbytes literals");
5108 Py_DECREF(s);
5109 goto error;
5110 }
5111 bytesmode = this_bytesmode;
5112
5113 assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
5114
5115 if (bytesmode) {
5116 /* For bytes, concat as we go. */
5117 if (i == 0) {
5118 /* First time, just remember this value. */
5119 bytes_str = s;
5120 } else {
5121 PyBytes_ConcatAndDel(&bytes_str, s);
5122 if (!bytes_str)
5123 goto error;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005124 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005125 } else if (this_fmode) {
5126 /* This is an f-string. Concatenate and decref it. */
5127 Py_ssize_t ofs = 0;
5128 int result = FstringParser_ConcatFstring(&state, s, &ofs, 0, c, n);
5129 Py_DECREF(s);
5130 if (result < 0)
5131 goto error;
5132 } else {
5133 /* This is a regular string. Concatenate it. */
5134 if (FstringParser_ConcatAndDel(&state, s) < 0)
5135 goto error;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005136 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005137 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005138 if (bytesmode) {
5139 /* Just return the bytes object and we're done. */
5140 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5141 goto error;
5142 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5143 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005144
Eric V. Smith235a6f02015-09-19 14:51:32 -04005145 /* We're not a bytes string, bytes_str should never have been set. */
5146 assert(bytes_str == NULL);
5147
5148 return FstringParser_Finish(&state, c, n);
5149
5150error:
5151 Py_XDECREF(bytes_str);
5152 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005153 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005154}