blob: 76daf6f446205a72ff73097aa384e5101bbdd71e [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 }
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400941 if (PyUnicode_CompareWithASCIIString(name, "async") == 0 ||
942 PyUnicode_CompareWithASCIIString(name, "await") == 0)
943 {
944 PyObject *message = PyUnicode_FromString(
945 "'async' and 'await' will become reserved keywords"
946 " in Python 3.7");
947 if (message == NULL) {
948 return 1;
949 }
950 if (PyErr_WarnExplicitObject(
951 PyExc_DeprecationWarning,
952 message,
953 c->c_filename,
954 LINENO(n),
955 NULL,
956 NULL) < 0)
957 {
958 return 1;
959 }
960 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000961 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200962 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000963 for (p = FORBIDDEN; *p; p++) {
964 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400965 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000966 return 1;
967 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000968 }
969 }
970 return 0;
971}
972
Jeremy Hyltona8293132006-02-28 17:58:27 +0000973/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974
975 Only sets context for expr kinds that "can appear in assignment context"
976 (according to ../Parser/Python.asdl). For other expr kinds, it sets
977 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978*/
979
980static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000981set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982{
983 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000984 /* If a particular expression type can't be used for assign / delete,
985 set expr_name to its name and an error message will be generated.
986 */
987 const char* expr_name = NULL;
988
989 /* The ast defines augmented store and load contexts, but the
990 implementation here doesn't actually use them. The code may be
991 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000992 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000993 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000994 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000995 */
996 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
998 switch (e->kind) {
999 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001000 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001001 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001002 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001003 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001005 e->v.Subscript.ctx = ctx;
1006 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001007 case Starred_kind:
1008 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001009 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001010 return 0;
1011 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001013 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001014 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001015 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001016 }
1017 e->v.Name.ctx = ctx;
1018 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001020 e->v.List.ctx = ctx;
1021 s = e->v.List.elts;
1022 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001024 e->v.Tuple.ctx = ctx;
1025 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001026 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001027 case Lambda_kind:
1028 expr_name = "lambda";
1029 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001031 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001032 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001033 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001035 case UnaryOp_kind:
1036 expr_name = "operator";
1037 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001039 expr_name = "generator expression";
1040 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001041 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001042 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001043 expr_name = "yield expression";
1044 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001045 case Await_kind:
1046 expr_name = "await expression";
1047 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001048 case ListComp_kind:
1049 expr_name = "list comprehension";
1050 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001051 case SetComp_kind:
1052 expr_name = "set comprehension";
1053 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001054 case DictComp_kind:
1055 expr_name = "dict comprehension";
1056 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001057 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001058 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 case Num_kind:
1060 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001061 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001062 case JoinedStr_kind:
1063 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001064 expr_name = "literal";
1065 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001066 case NameConstant_kind:
1067 expr_name = "keyword";
1068 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001069 case Ellipsis_kind:
1070 expr_name = "Ellipsis";
1071 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001072 case Compare_kind:
1073 expr_name = "comparison";
1074 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001075 case IfExp_kind:
1076 expr_name = "conditional expression";
1077 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001078 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 PyErr_Format(PyExc_SystemError,
1080 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001081 e->kind, e->lineno);
1082 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001084 /* Check for error string set by switch */
1085 if (expr_name) {
1086 char buf[300];
1087 PyOS_snprintf(buf, sizeof(buf),
1088 "can't %s %s",
1089 ctx == Store ? "assign to" : "delete",
1090 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001091 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001092 }
1093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 */
1097 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001098 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099
Thomas Wouters89f507f2006-12-13 04:49:30 +00001100 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001101 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001102 return 0;
1103 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 }
1105 return 1;
1106}
1107
1108static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001109ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110{
1111 REQ(n, augassign);
1112 n = CHILD(n, 0);
1113 switch (STR(n)[0]) {
1114 case '+':
1115 return Add;
1116 case '-':
1117 return Sub;
1118 case '/':
1119 if (STR(n)[1] == '/')
1120 return FloorDiv;
1121 else
1122 return Div;
1123 case '%':
1124 return Mod;
1125 case '<':
1126 return LShift;
1127 case '>':
1128 return RShift;
1129 case '&':
1130 return BitAnd;
1131 case '^':
1132 return BitXor;
1133 case '|':
1134 return BitOr;
1135 case '*':
1136 if (STR(n)[1] == '*')
1137 return Pow;
1138 else
1139 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001140 case '@':
1141 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001143 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 }
1146}
1147
1148static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001149ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001151 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 |'is' 'not'
1153 */
1154 REQ(n, comp_op);
1155 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001156 n = CHILD(n, 0);
1157 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 case LESS:
1159 return Lt;
1160 case GREATER:
1161 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 return Eq;
1164 case LESSEQUAL:
1165 return LtE;
1166 case GREATEREQUAL:
1167 return GtE;
1168 case NOTEQUAL:
1169 return NotEq;
1170 case NAME:
1171 if (strcmp(STR(n), "in") == 0)
1172 return In;
1173 if (strcmp(STR(n), "is") == 0)
1174 return Is;
1175 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001176 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001178 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001179 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 }
1181 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001182 /* handle "not in" and "is not" */
1183 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 case NAME:
1185 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1186 return NotIn;
1187 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1188 return IsNot;
1189 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001190 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001192 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 }
Neal Norwitz79792652005-11-14 04:25:03 +00001195 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198}
1199
1200static asdl_seq *
1201seq_for_testlist(struct compiling *c, const node *n)
1202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001204 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1205 */
Armin Rigo31441302005-10-21 12:57:31 +00001206 asdl_seq *seq;
1207 expr_ty expression;
1208 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001209 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001211 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 if (!seq)
1213 return NULL;
1214
1215 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001217 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218
Benjamin Peterson4905e802009-09-27 02:43:28 +00001219 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001220 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222
1223 assert(i / 2 < seq->size);
1224 asdl_seq_SET(seq, i / 2, expression);
1225 }
1226 return seq;
1227}
1228
Neal Norwitzc1505362006-12-28 06:47:50 +00001229static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001230ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001231{
1232 identifier name;
1233 expr_ty annotation = NULL;
1234 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001235 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001236
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001237 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001238 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001239 name = NEW_IDENTIFIER(ch);
1240 if (!name)
1241 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001242 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001243 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001244
1245 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1246 annotation = ast_for_expr(c, CHILD(n, 2));
1247 if (!annotation)
1248 return NULL;
1249 }
1250
Victor Stinnerc106c682015-11-06 17:01:48 +01001251 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001252 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001253 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001254 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255}
1256
Guido van Rossum4f72a782006-10-27 23:31:49 +00001257/* returns -1 if failed to handle keyword only arguments
1258 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001259 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001260 ^^^
1261 start pointing here
1262 */
1263static int
1264handle_keywordonly_args(struct compiling *c, const node *n, int start,
1265 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1266{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001267 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001268 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001269 expr_ty expression, annotation;
1270 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001271 int i = start;
1272 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001273
1274 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001275 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001276 return -1;
1277 }
1278 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001279 while (i < NCH(n)) {
1280 ch = CHILD(n, i);
1281 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001282 case vfpdef:
1283 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001285 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001286 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001287 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001288 asdl_seq_SET(kwdefaults, j, expression);
1289 i += 2; /* '=' and test */
1290 }
1291 else { /* setting NULL if no default value exists */
1292 asdl_seq_SET(kwdefaults, j, NULL);
1293 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001294 if (NCH(ch) == 3) {
1295 /* ch is NAME ':' test */
1296 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001297 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001298 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001299 }
1300 else {
1301 annotation = NULL;
1302 }
1303 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001304 argname = NEW_IDENTIFIER(ch);
1305 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001307 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001308 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001309 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1310 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001311 if (!arg)
1312 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001314 i += 2; /* the name and the comma */
1315 break;
1316 case DOUBLESTAR:
1317 return i;
1318 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001319 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 goto error;
1321 }
1322 }
1323 return i;
1324 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001326}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327
Jeremy Hyltona8293132006-02-28 17:58:27 +00001328/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329
1330static arguments_ty
1331ast_for_arguments(struct compiling *c, const node *n)
1332{
Neal Norwitzc1505362006-12-28 06:47:50 +00001333 /* This function handles both typedargslist (function definition)
1334 and varargslist (lambda definition).
1335
1336 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001337 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1338 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1339 | '**' tfpdef [',']]]
1340 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1341 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001342 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001343 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1344 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1345 | '**' vfpdef [',']]]
1346 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1347 | '**' vfpdef [',']
1348 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001350
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001352 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1353 int nposdefaults = 0, found_default = 0;
1354 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001355 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001356 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 node *ch;
1358
1359 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001360 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001361 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001364 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365
Jeremy Hyltone921e022008-07-17 16:37:17 +00001366 /* First count the number of positional args & defaults. The
1367 variable i is the loop index for this for loop and the next.
1368 The next loop picks up where the first leaves off.
1369 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 ch = CHILD(n, i);
1372 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001373 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001374 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001375 if (i < NCH(n) && /* skip argument following star */
1376 (TYPE(CHILD(n, i)) == tfpdef ||
1377 TYPE(CHILD(n, i)) == vfpdef)) {
1378 i++;
1379 }
1380 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001382 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001383 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001387 defaults for keyword only args */
1388 for ( ; i < NCH(n); ++i) {
1389 ch = CHILD(n, i);
1390 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001391 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001393 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001395 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001397 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001399 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001401 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001403 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 since we set NULL as default for keyword only argument w/o default
1406 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001407 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001408 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001409 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001410 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411
1412 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001413 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001414 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001417 /* tfpdef: NAME [':' test]
1418 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 */
1420 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001421 j = 0; /* index for defaults */
1422 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 ch = CHILD(n, i);
1425 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001426 case tfpdef:
1427 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1429 anything other than EQUAL or a comma? */
1430 /* XXX Should NCH(n) check be made a separate check? */
1431 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001432 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1433 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001434 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001435 assert(posdefaults != NULL);
1436 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001438 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001440 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001441 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001442 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001443 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001444 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001445 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001446 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001447 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001448 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 i += 2; /* the name and the comma */
1450 break;
1451 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001452 if (i+1 >= NCH(n) ||
1453 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001454 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001455 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001456 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001457 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001458 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001459 if (TYPE(ch) == COMMA) {
1460 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001461 i += 2; /* now follows keyword only arguments */
1462 res = handle_keywordonly_args(c, n, i,
1463 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001464 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001465 i = res; /* res has new position to process */
1466 }
1467 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001468 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001469 if (!vararg)
1470 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001471
Guido van Rossum4f72a782006-10-27 23:31:49 +00001472 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001473 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1474 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001475 int res = 0;
1476 res = handle_keywordonly_args(c, n, i,
1477 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001478 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001479 i = res; /* res has new position to process */
1480 }
1481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 break;
1483 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001484 ch = CHILD(n, i+1); /* tfpdef */
1485 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001486 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001487 if (!kwarg)
1488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 i += 3;
1490 break;
1491 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001492 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 "unexpected node in varargslist: %d @ %d",
1494 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001495 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001498 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499}
1500
1501static expr_ty
1502ast_for_dotted_name(struct compiling *c, const node *n)
1503{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001504 expr_ty e;
1505 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001506 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 int i;
1508
1509 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001510
1511 lineno = LINENO(n);
1512 col_offset = n->n_col_offset;
1513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 id = NEW_IDENTIFIER(CHILD(n, 0));
1515 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001516 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001517 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001519 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
1521 for (i = 2; i < NCH(n); i+=2) {
1522 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001523 if (!id)
1524 return NULL;
1525 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1526 if (!e)
1527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 }
1529
1530 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531}
1532
1533static expr_ty
1534ast_for_decorator(struct compiling *c, const node *n)
1535{
1536 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1537 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001538 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001541 REQ(CHILD(n, 0), AT);
1542 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1545 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001546 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001549 d = name_expr;
1550 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 }
1552 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001553 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001554 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001555 if (!d)
1556 return NULL;
1557 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 }
1559 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001560 d = ast_for_call(c, CHILD(n, 3), name_expr);
1561 if (!d)
1562 return NULL;
1563 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 }
1565
1566 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567}
1568
1569static asdl_seq*
1570ast_for_decorators(struct compiling *c, const node *n)
1571{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001572 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001573 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001577 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 if (!decorator_seq)
1579 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001582 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001583 if (!d)
1584 return NULL;
1585 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 }
1587 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588}
1589
1590static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001591ast_for_funcdef_impl(struct compiling *c, const node *n,
1592 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001594 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001595 identifier name;
1596 arguments_ty args;
1597 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001598 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001599 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600
1601 REQ(n, funcdef);
1602
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 name = NEW_IDENTIFIER(CHILD(n, name_i));
1604 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001605 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001606 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1609 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001610 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001611 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1612 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1613 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001614 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001615 name_i += 2;
1616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 body = ast_for_suite(c, CHILD(n, name_i + 3));
1618 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620
Yury Selivanov75445082015-05-11 22:57:16 -04001621 if (is_async)
1622 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1623 LINENO(n),
1624 n->n_col_offset, c->c_arena);
1625 else
1626 return FunctionDef(name, args, body, decorator_seq, returns,
1627 LINENO(n),
1628 n->n_col_offset, c->c_arena);
1629}
1630
1631static stmt_ty
1632ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1633{
1634 /* async_funcdef: ASYNC funcdef */
1635 REQ(n, async_funcdef);
1636 REQ(CHILD(n, 0), ASYNC);
1637 REQ(CHILD(n, 1), funcdef);
1638
1639 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1640 1 /* is_async */);
1641}
1642
1643static stmt_ty
1644ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1645{
1646 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1647 return ast_for_funcdef_impl(c, n, decorator_seq,
1648 0 /* is_async */);
1649}
1650
1651
1652static stmt_ty
1653ast_for_async_stmt(struct compiling *c, const node *n)
1654{
1655 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1656 REQ(n, async_stmt);
1657 REQ(CHILD(n, 0), ASYNC);
1658
1659 switch (TYPE(CHILD(n, 1))) {
1660 case funcdef:
1661 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1662 1 /* is_async */);
1663 case with_stmt:
1664 return ast_for_with_stmt(c, CHILD(n, 1),
1665 1 /* is_async */);
1666
1667 case for_stmt:
1668 return ast_for_for_stmt(c, CHILD(n, 1),
1669 1 /* is_async */);
1670
1671 default:
1672 PyErr_Format(PyExc_SystemError,
1673 "invalid async stament: %s",
1674 STR(CHILD(n, 1)));
1675 return NULL;
1676 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677}
1678
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001679static stmt_ty
1680ast_for_decorated(struct compiling *c, const node *n)
1681{
Yury Selivanov75445082015-05-11 22:57:16 -04001682 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001683 stmt_ty thing = NULL;
1684 asdl_seq *decorator_seq = NULL;
1685
1686 REQ(n, decorated);
1687
1688 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1689 if (!decorator_seq)
1690 return NULL;
1691
1692 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001693 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001695
1696 if (TYPE(CHILD(n, 1)) == funcdef) {
1697 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1698 } else if (TYPE(CHILD(n, 1)) == classdef) {
1699 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001700 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1701 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001702 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001703 /* we count the decorators in when talking about the class' or
1704 * function's line number */
1705 if (thing) {
1706 thing->lineno = LINENO(n);
1707 thing->col_offset = n->n_col_offset;
1708 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001709 return thing;
1710}
1711
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712static expr_ty
1713ast_for_lambdef(struct compiling *c, const node *n)
1714{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001715 /* lambdef: 'lambda' [varargslist] ':' test
1716 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 arguments_ty args;
1718 expr_ty expression;
1719
1720 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001721 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 if (!args)
1723 return NULL;
1724 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001725 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727 }
1728 else {
1729 args = ast_for_arguments(c, CHILD(n, 1));
1730 if (!args)
1731 return NULL;
1732 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001733 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 }
1736
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001737 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738}
1739
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001740static expr_ty
1741ast_for_ifexpr(struct compiling *c, const node *n)
1742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001744 expr_ty expression, body, orelse;
1745
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001746 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001747 body = ast_for_expr(c, CHILD(n, 0));
1748 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001749 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001750 expression = ast_for_expr(c, CHILD(n, 2));
1751 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001752 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001753 orelse = ast_for_expr(c, CHILD(n, 4));
1754 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001755 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001756 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1757 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001758}
1759
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001761 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762
Nick Coghlan650f0d02007-04-15 12:05:43 +00001763 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764*/
1765
1766static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001767count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001769 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001770 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771
Guido van Rossumd8faa362007-04-27 19:54:29 +00001772 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001773 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001774 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001775 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001776 if (TYPE(CHILD(n, 0)) == ASYNC) {
1777 is_async = 1;
1778 }
1779 if (NCH(n) == (5 + is_async)) {
1780 n = CHILD(n, 4 + is_async);
1781 }
1782 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001783 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001784 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001785 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001786 REQ(n, comp_iter);
1787 n = CHILD(n, 0);
1788 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001789 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001790 else if (TYPE(n) == comp_if) {
1791 if (NCH(n) == 3) {
1792 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001793 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001794 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001795 else
1796 return n_fors;
1797 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001798
Guido van Rossumd8faa362007-04-27 19:54:29 +00001799 /* Should never be reached */
1800 PyErr_SetString(PyExc_SystemError,
1801 "logic error in count_comp_fors");
1802 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803}
1804
Nick Coghlan650f0d02007-04-15 12:05:43 +00001805/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806
Nick Coghlan650f0d02007-04-15 12:05:43 +00001807 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808*/
1809
1810static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001811count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001813 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814
Guido van Rossumd8faa362007-04-27 19:54:29 +00001815 while (1) {
1816 REQ(n, comp_iter);
1817 if (TYPE(CHILD(n, 0)) == comp_for)
1818 return n_ifs;
1819 n = CHILD(n, 0);
1820 REQ(n, comp_if);
1821 n_ifs++;
1822 if (NCH(n) == 2)
1823 return n_ifs;
1824 n = CHILD(n, 2);
1825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826}
1827
Guido van Rossum992d4a32007-07-11 13:09:30 +00001828static asdl_seq *
1829ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001832 asdl_seq *comps;
1833
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001834 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 if (n_fors == -1)
1836 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001837
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001838 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001839 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001841
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001843 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001845 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001846 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001847 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848
Guido van Rossum992d4a32007-07-11 13:09:30 +00001849 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001851 if (TYPE(CHILD(n, 0)) == ASYNC) {
1852 is_async = 1;
1853 }
1854
1855 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001856 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001857 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001859 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001862
Thomas Wouters89f507f2006-12-13 04:49:30 +00001863 /* Check the # of children rather than the length of t, since
1864 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001865 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001866 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001867 comp = comprehension(first, expression, NULL,
1868 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001870 comp = comprehension(Tuple(t, Store, first->lineno,
1871 first->col_offset, c->c_arena),
1872 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001873 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001875
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001876 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 int j, n_ifs;
1878 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001880 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001881 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001882 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001885 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001890 REQ(n, comp_iter);
1891 n = CHILD(n, 0);
1892 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893
Guido van Rossum992d4a32007-07-11 13:09:30 +00001894 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001896 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001897 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001898 if (NCH(n) == 3)
1899 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 /* on exit, must guarantee that n is a comp_for */
1902 if (TYPE(n) == comp_iter)
1903 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001904 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001906 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001908 return comps;
1909}
1910
1911static expr_ty
1912ast_for_itercomp(struct compiling *c, const node *n, int type)
1913{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001914 /* testlist_comp: (test|star_expr)
1915 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001916 expr_ty elt;
1917 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001918 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919
Guido van Rossum992d4a32007-07-11 13:09:30 +00001920 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001922 ch = CHILD(n, 0);
1923 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001924 if (!elt)
1925 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001926 if (elt->kind == Starred_kind) {
1927 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1928 return NULL;
1929 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930
Guido van Rossum992d4a32007-07-11 13:09:30 +00001931 comps = ast_for_comprehension(c, CHILD(n, 1));
1932 if (!comps)
1933 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001934
1935 if (type == COMP_GENEXP)
1936 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1937 else if (type == COMP_LISTCOMP)
1938 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1939 else if (type == COMP_SETCOMP)
1940 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1941 else
1942 /* Should never happen */
1943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944}
1945
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001946/* Fills in the key, value pair corresponding to the dict element. In case
1947 * of an unpacking, key is NULL. *i is advanced by the number of ast
1948 * elements. Iff successful, nonzero is returned.
1949 */
1950static int
1951ast_for_dictelement(struct compiling *c, const node *n, int *i,
1952 expr_ty *key, expr_ty *value)
1953{
1954 expr_ty expression;
1955 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1956 assert(NCH(n) - *i >= 2);
1957
1958 expression = ast_for_expr(c, CHILD(n, *i + 1));
1959 if (!expression)
1960 return 0;
1961 *key = NULL;
1962 *value = expression;
1963
1964 *i += 2;
1965 }
1966 else {
1967 assert(NCH(n) - *i >= 3);
1968
1969 expression = ast_for_expr(c, CHILD(n, *i));
1970 if (!expression)
1971 return 0;
1972 *key = expression;
1973
1974 REQ(CHILD(n, *i + 1), COLON);
1975
1976 expression = ast_for_expr(c, CHILD(n, *i + 2));
1977 if (!expression)
1978 return 0;
1979 *value = expression;
1980
1981 *i += 3;
1982 }
1983 return 1;
1984}
1985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001987ast_for_dictcomp(struct compiling *c, const node *n)
1988{
1989 expr_ty key, value;
1990 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001991 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001993 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001994 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001995 assert(key);
1996 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001998 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001999 if (!comps)
2000 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001
Guido van Rossum992d4a32007-07-11 13:09:30 +00002002 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2003}
2004
2005static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002006ast_for_dictdisplay(struct compiling *c, const node *n)
2007{
2008 int i;
2009 int j;
2010 int size;
2011 asdl_seq *keys, *values;
2012
2013 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2014 keys = _Py_asdl_seq_new(size, c->c_arena);
2015 if (!keys)
2016 return NULL;
2017
2018 values = _Py_asdl_seq_new(size, c->c_arena);
2019 if (!values)
2020 return NULL;
2021
2022 j = 0;
2023 for (i = 0; i < NCH(n); i++) {
2024 expr_ty key, value;
2025
2026 if (!ast_for_dictelement(c, n, &i, &key, &value))
2027 return NULL;
2028 asdl_seq_SET(keys, j, key);
2029 asdl_seq_SET(values, j, value);
2030
2031 j++;
2032 }
2033 keys->size = j;
2034 values->size = j;
2035 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2036}
2037
2038static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002039ast_for_genexp(struct compiling *c, const node *n)
2040{
2041 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002042 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002043}
2044
2045static expr_ty
2046ast_for_listcomp(struct compiling *c, const node *n)
2047{
2048 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002049 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002050}
2051
2052static expr_ty
2053ast_for_setcomp(struct compiling *c, const node *n)
2054{
2055 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002056 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002057}
2058
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002059static expr_ty
2060ast_for_setdisplay(struct compiling *c, const node *n)
2061{
2062 int i;
2063 int size;
2064 asdl_seq *elts;
2065
2066 assert(TYPE(n) == (dictorsetmaker));
2067 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2068 elts = _Py_asdl_seq_new(size, c->c_arena);
2069 if (!elts)
2070 return NULL;
2071 for (i = 0; i < NCH(n); i += 2) {
2072 expr_ty expression;
2073 expression = ast_for_expr(c, CHILD(n, i));
2074 if (!expression)
2075 return NULL;
2076 asdl_seq_SET(elts, i / 2, expression);
2077 }
2078 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2079}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002080
2081static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082ast_for_atom(struct compiling *c, const node *n)
2083{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002084 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2085 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002086 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 */
2088 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002091 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002092 PyObject *name;
2093 const char *s = STR(ch);
2094 size_t len = strlen(s);
2095 if (len >= 4 && len <= 5) {
2096 if (!strcmp(s, "None"))
2097 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2098 if (!strcmp(s, "True"))
2099 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2100 if (!strcmp(s, "False"))
2101 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2102 }
2103 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002104 if (!name)
2105 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002106 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002107 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2108 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002110 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002111 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002112 const char *errtype = NULL;
2113 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2114 errtype = "unicode error";
2115 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2116 errtype = "value error";
2117 if (errtype) {
2118 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002119 PyObject *type, *value, *tback, *errstr;
2120 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002121 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002122 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002123 char *s = _PyUnicode_AsString(errstr);
2124 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002125 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002126 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002127 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002128 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002129 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002130 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002131 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002132 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002133 Py_XDECREF(tback);
2134 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002135 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002136 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002137 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 }
2139 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002140 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002141 if (!pynum)
2142 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002143
Victor Stinner43d81952013-07-17 00:57:58 +02002144 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2145 Py_DECREF(pynum);
2146 return NULL;
2147 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002148 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 }
Georg Brandldde00282007-03-18 19:01:53 +00002150 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002151 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002153 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154
Thomas Wouters89f507f2006-12-13 04:49:30 +00002155 if (TYPE(ch) == RPAR)
2156 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157
Thomas Wouters89f507f2006-12-13 04:49:30 +00002158 if (TYPE(ch) == yield_expr)
2159 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002162 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002163 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002164
Nick Coghlan650f0d02007-04-15 12:05:43 +00002165 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168
Thomas Wouters89f507f2006-12-13 04:49:30 +00002169 if (TYPE(ch) == RSQB)
2170 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171
Nick Coghlan650f0d02007-04-15 12:05:43 +00002172 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002173 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2174 asdl_seq *elts = seq_for_testlist(c, ch);
2175 if (!elts)
2176 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002177
Thomas Wouters89f507f2006-12-13 04:49:30 +00002178 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2179 }
2180 else
2181 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002183 /* dictorsetmaker: ( ((test ':' test | '**' test)
2184 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2185 * ((test | '*' test)
2186 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002187 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002188 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002189 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002190 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002191 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002192 }
2193 else {
2194 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2195 if (NCH(ch) == 1 ||
2196 (NCH(ch) > 1 &&
2197 TYPE(CHILD(ch, 1)) == COMMA)) {
2198 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002199 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002200 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002201 else if (NCH(ch) > 1 &&
2202 TYPE(CHILD(ch, 1)) == comp_for) {
2203 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002204 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002205 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002206 else if (NCH(ch) > 3 - is_dict &&
2207 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2208 /* It's a dictionary comprehension. */
2209 if (is_dict) {
2210 ast_error(c, n, "dict unpacking cannot be used in "
2211 "dict comprehension");
2212 return NULL;
2213 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002214 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002215 }
2216 else {
2217 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002218 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002219 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002220 if (res) {
2221 res->lineno = LINENO(n);
2222 res->col_offset = n->n_col_offset;
2223 }
2224 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002225 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002228 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2229 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 }
2231}
2232
2233static slice_ty
2234ast_for_slice(struct compiling *c, const node *n)
2235{
2236 node *ch;
2237 expr_ty lower = NULL, upper = NULL, step = NULL;
2238
2239 REQ(n, subscript);
2240
2241 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002242 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 sliceop: ':' [test]
2244 */
2245 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 if (NCH(n) == 1 && TYPE(ch) == test) {
2247 /* 'step' variable hold no significance in terms of being used over
2248 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 if (!step)
2251 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252
Thomas Wouters89f507f2006-12-13 04:49:30 +00002253 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 }
2255
2256 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002257 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 if (!lower)
2259 return NULL;
2260 }
2261
2262 /* If there's an upper bound it's in the second or third position. */
2263 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002264 if (NCH(n) > 1) {
2265 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266
Thomas Wouters89f507f2006-12-13 04:49:30 +00002267 if (TYPE(n2) == test) {
2268 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 if (!upper)
2270 return NULL;
2271 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002274 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Thomas Wouters89f507f2006-12-13 04:49:30 +00002276 if (TYPE(n2) == test) {
2277 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 if (!upper)
2279 return NULL;
2280 }
2281 }
2282
2283 ch = CHILD(n, NCH(n) - 1);
2284 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002285 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002286 ch = CHILD(ch, 1);
2287 if (TYPE(ch) == test) {
2288 step = ast_for_expr(c, ch);
2289 if (!step)
2290 return NULL;
2291 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 }
2293 }
2294
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002295 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296}
2297
2298static expr_ty
2299ast_for_binop(struct compiling *c, const node *n)
2300{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002301 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002303 BinOp(BinOp(A, op, B), op, C).
2304 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305
Guido van Rossumd8faa362007-04-27 19:54:29 +00002306 int i, nops;
2307 expr_ty expr1, expr2, result;
2308 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309
Guido van Rossumd8faa362007-04-27 19:54:29 +00002310 expr1 = ast_for_expr(c, CHILD(n, 0));
2311 if (!expr1)
2312 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 expr2 = ast_for_expr(c, CHILD(n, 2));
2315 if (!expr2)
2316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317
Guido van Rossumd8faa362007-04-27 19:54:29 +00002318 newoperator = get_operator(CHILD(n, 1));
2319 if (!newoperator)
2320 return NULL;
2321
2322 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2323 c->c_arena);
2324 if (!result)
2325 return NULL;
2326
2327 nops = (NCH(n) - 1) / 2;
2328 for (i = 1; i < nops; i++) {
2329 expr_ty tmp_result, tmp;
2330 const node* next_oper = CHILD(n, i * 2 + 1);
2331
2332 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002333 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 return NULL;
2335
Guido van Rossumd8faa362007-04-27 19:54:29 +00002336 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2337 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338 return NULL;
2339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002341 LINENO(next_oper), next_oper->n_col_offset,
2342 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002344 return NULL;
2345 result = tmp_result;
2346 }
2347 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348}
2349
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002350static expr_ty
2351ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002354 subscriptlist: subscript (',' subscript)* [',']
2355 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2356 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002357 REQ(n, trailer);
2358 if (TYPE(CHILD(n, 0)) == LPAR) {
2359 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002360 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002361 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002362 else
2363 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002365 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002366 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2367 if (!attr_id)
2368 return NULL;
2369 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002370 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002371 }
2372 else {
2373 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002374 REQ(CHILD(n, 2), RSQB);
2375 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002376 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002377 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2378 if (!slc)
2379 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002380 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2381 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002382 }
2383 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002385 by treating the sequence as a tuple literal if there are
2386 no slice features.
2387 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002388 int j;
2389 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002390 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002391 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002392 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002393 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002394 if (!slices)
2395 return NULL;
2396 for (j = 0; j < NCH(n); j += 2) {
2397 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002398 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002399 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002400 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002401 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002402 asdl_seq_SET(slices, j / 2, slc);
2403 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002404 if (!simple) {
2405 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002406 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002407 }
2408 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002409 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002410 if (!elts)
2411 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002412 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2413 slc = (slice_ty)asdl_seq_GET(slices, j);
2414 assert(slc->kind == Index_kind && slc->v.Index.value);
2415 asdl_seq_SET(elts, j, slc->v.Index.value);
2416 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002417 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002418 if (!e)
2419 return NULL;
2420 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002421 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002422 }
2423 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002424}
2425
2426static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002427ast_for_factor(struct compiling *c, const node *n)
2428{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002429 expr_ty expression;
2430
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002431 expression = ast_for_expr(c, CHILD(n, 1));
2432 if (!expression)
2433 return NULL;
2434
2435 switch (TYPE(CHILD(n, 0))) {
2436 case PLUS:
2437 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2438 c->c_arena);
2439 case MINUS:
2440 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2441 c->c_arena);
2442 case TILDE:
2443 return UnaryOp(Invert, expression, LINENO(n),
2444 n->n_col_offset, c->c_arena);
2445 }
2446 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2447 TYPE(CHILD(n, 0)));
2448 return NULL;
2449}
2450
2451static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002452ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002453{
Yury Selivanov75445082015-05-11 22:57:16 -04002454 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002455 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002456
2457 REQ(n, atom_expr);
2458 nch = NCH(n);
2459
2460 if (TYPE(CHILD(n, 0)) == AWAIT) {
2461 start = 1;
2462 assert(nch > 1);
2463 }
2464
2465 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002466 if (!e)
2467 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002468 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002469 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002470 if (start && nch == 2) {
2471 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2472 }
2473
2474 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002475 node *ch = CHILD(n, i);
2476 if (TYPE(ch) != trailer)
2477 break;
2478 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002479 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002480 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002481 tmp->lineno = e->lineno;
2482 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002483 e = tmp;
2484 }
Yury Selivanov75445082015-05-11 22:57:16 -04002485
2486 if (start) {
2487 /* there was an AWAIT */
2488 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2489 }
2490 else {
2491 return e;
2492 }
2493}
2494
2495static expr_ty
2496ast_for_power(struct compiling *c, const node *n)
2497{
2498 /* power: atom trailer* ('**' factor)*
2499 */
2500 expr_ty e;
2501 REQ(n, power);
2502 e = ast_for_atom_expr(c, CHILD(n, 0));
2503 if (!e)
2504 return NULL;
2505 if (NCH(n) == 1)
2506 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002507 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2508 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002509 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002510 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002511 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002512 }
2513 return e;
2514}
2515
Guido van Rossum0368b722007-05-11 16:50:42 +00002516static expr_ty
2517ast_for_starred(struct compiling *c, const node *n)
2518{
2519 expr_ty tmp;
2520 REQ(n, star_expr);
2521
2522 tmp = ast_for_expr(c, CHILD(n, 1));
2523 if (!tmp)
2524 return NULL;
2525
2526 /* The Load context is changed later. */
2527 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2528}
2529
2530
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531/* Do not name a variable 'expr'! Will cause a compile error.
2532*/
2533
2534static expr_ty
2535ast_for_expr(struct compiling *c, const node *n)
2536{
2537 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002538 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002539 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 and_test: not_test ('and' not_test)*
2542 not_test: 'not' not_test | comparison
2543 comparison: expr (comp_op expr)*
2544 expr: xor_expr ('|' xor_expr)*
2545 xor_expr: and_expr ('^' and_expr)*
2546 and_expr: shift_expr ('&' shift_expr)*
2547 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2548 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002549 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002551 power: atom_expr ['**' factor]
2552 atom_expr: [AWAIT] atom trailer*
2553 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 */
2555
2556 asdl_seq *seq;
2557 int i;
2558
2559 loop:
2560 switch (TYPE(n)) {
2561 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002562 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002563 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002564 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002566 else if (NCH(n) > 1)
2567 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002568 /* Fallthrough */
2569 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 case and_test:
2571 if (NCH(n) == 1) {
2572 n = CHILD(n, 0);
2573 goto loop;
2574 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002575 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 if (!seq)
2577 return NULL;
2578 for (i = 0; i < NCH(n); i += 2) {
2579 expr_ty e = ast_for_expr(c, CHILD(n, i));
2580 if (!e)
2581 return NULL;
2582 asdl_seq_SET(seq, i / 2, e);
2583 }
2584 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002585 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2586 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002587 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002588 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 case not_test:
2590 if (NCH(n) == 1) {
2591 n = CHILD(n, 0);
2592 goto loop;
2593 }
2594 else {
2595 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2596 if (!expression)
2597 return NULL;
2598
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002599 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2600 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 }
2602 case comparison:
2603 if (NCH(n) == 1) {
2604 n = CHILD(n, 0);
2605 goto loop;
2606 }
2607 else {
2608 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002609 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002610 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002611 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 if (!ops)
2613 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002614 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 return NULL;
2617 }
2618 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002619 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002621 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002622 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002624 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625
2626 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002627 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002629 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002631 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 asdl_seq_SET(cmps, i / 2, expression);
2633 }
2634 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002635 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639 return Compare(expression, ops, cmps, LINENO(n),
2640 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 }
2642 break;
2643
Guido van Rossum0368b722007-05-11 16:50:42 +00002644 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 /* The next five cases all handle BinOps. The main body of code
2647 is the same in each case, but the switch turned inside out to
2648 reuse the code for each type of operator.
2649 */
2650 case expr:
2651 case xor_expr:
2652 case and_expr:
2653 case shift_expr:
2654 case arith_expr:
2655 case term:
2656 if (NCH(n) == 1) {
2657 n = CHILD(n, 0);
2658 goto loop;
2659 }
2660 return ast_for_binop(c, n);
2661 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002662 node *an = NULL;
2663 node *en = NULL;
2664 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002665 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002666 if (NCH(n) > 1)
2667 an = CHILD(n, 1); /* yield_arg */
2668 if (an) {
2669 en = CHILD(an, NCH(an) - 1);
2670 if (NCH(an) == 2) {
2671 is_from = 1;
2672 exp = ast_for_expr(c, en);
2673 }
2674 else
2675 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002676 if (!exp)
2677 return NULL;
2678 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002679 if (is_from)
2680 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2681 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002682 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002683 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 if (NCH(n) == 1) {
2685 n = CHILD(n, 0);
2686 goto loop;
2687 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002688 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002689 case power:
2690 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002692 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 return NULL;
2694 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002695 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 return NULL;
2697}
2698
2699static expr_ty
2700ast_for_call(struct compiling *c, const node *n, expr_ty func)
2701{
2702 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002703 arglist: argument (',' argument)* [',']
2704 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 */
2706
2707 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002708 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002709 asdl_seq *args;
2710 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711
2712 REQ(n, arglist);
2713
2714 nargs = 0;
2715 nkeywords = 0;
2716 ngens = 0;
2717 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002718 node *ch = CHILD(n, i);
2719 if (TYPE(ch) == argument) {
2720 if (NCH(ch) == 1)
2721 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002722 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002723 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002724 else if (TYPE(CHILD(ch, 0)) == STAR)
2725 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002727 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002728 nkeywords++;
2729 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 }
2731 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002732 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002733 "if not sole argument");
2734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 }
2736
2737 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002738 ast_error(c, n, "more than 255 arguments");
2739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 }
2741
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002742 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002744 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002745 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002747 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002748
2749 nargs = 0; /* positional arguments + iterable argument unpackings */
2750 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2751 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002753 node *ch = CHILD(n, i);
2754 if (TYPE(ch) == argument) {
2755 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002756 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002757 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002758 /* a positional argument */
2759 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 if (ndoublestars) {
2761 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002762 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002764 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002765 else {
2766 ast_error(c, chch,
2767 "positional argument follows "
2768 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002769 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002770 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002771 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002772 e = ast_for_expr(c, chch);
2773 if (!e)
2774 return NULL;
2775 asdl_seq_SET(args, nargs++, e);
2776 }
2777 else if (TYPE(chch) == STAR) {
2778 /* an iterable argument unpacking */
2779 expr_ty starred;
2780 if (ndoublestars) {
2781 ast_error(c, chch,
2782 "iterable argument unpacking follows "
2783 "keyword argument unpacking");
2784 return NULL;
2785 }
2786 e = ast_for_expr(c, CHILD(ch, 1));
2787 if (!e)
2788 return NULL;
2789 starred = Starred(e, Load, LINENO(chch),
2790 chch->n_col_offset,
2791 c->c_arena);
2792 if (!starred)
2793 return NULL;
2794 asdl_seq_SET(args, nargs++, starred);
2795
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002796 }
2797 else if (TYPE(chch) == DOUBLESTAR) {
2798 /* a keyword argument unpacking */
2799 keyword_ty kw;
2800 i++;
2801 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002803 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002804 kw = keyword(NULL, e, c->c_arena);
2805 asdl_seq_SET(keywords, nkeywords++, kw);
2806 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002808 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002809 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002810 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002812 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002813 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002816 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002818 identifier key, tmp;
2819 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002821 /* chch is test, but must be an identifier? */
2822 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 /* f(lambda x: x[0] = 3) ends up getting parsed with
2826 * LHS test = lambda x: x[0], and RHS test = 3.
2827 * SF bug 132313 points out that complaining about a keyword
2828 * then is very confusing.
2829 */
2830 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002831 ast_error(c, chch,
2832 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002833 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 }
2835 else if (e->kind != Name_kind) {
2836 ast_error(c, chch,
2837 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002838 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002839 }
2840 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002841 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002843 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002844 for (k = 0; k < nkeywords; k++) {
2845 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002846 if (tmp && !PyUnicode_Compare(tmp, key)) {
2847 ast_error(c, chch,
2848 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002849 return NULL;
2850 }
2851 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002852 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002854 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002857 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002858 asdl_seq_SET(keywords, nkeywords++, kw);
2859 }
2860 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 }
2862
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002863 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864}
2865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002867ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002869 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002870 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002872 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002873 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002874 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875 }
2876 else {
2877 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002878 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002879 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002881 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 else {
2883 asdl_seq *tmp = seq_for_testlist(c, n);
2884 if (!tmp)
2885 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002888}
2889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890static stmt_ty
2891ast_for_expr_stmt(struct compiling *c, const node *n)
2892{
2893 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002894 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2895 ('=' (yield_expr|testlist_star_expr))*)
2896 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002897 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002898 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002899 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002900 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 */
2902
2903 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002904 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 if (!e)
2906 return NULL;
2907
Thomas Wouters89f507f2006-12-13 04:49:30 +00002908 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 }
2910 else if (TYPE(CHILD(n, 1)) == augassign) {
2911 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002912 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002913 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 if (!expr1)
2917 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002918 if(!set_context(c, expr1, Store, ch))
2919 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002920 /* set_context checks that most expressions are not the left side.
2921 Augmented assignments can only have a name, a subscript, or an
2922 attribute on the left, though, so we have to explicitly check for
2923 those. */
2924 switch (expr1->kind) {
2925 case Name_kind:
2926 case Attribute_kind:
2927 case Subscript_kind:
2928 break;
2929 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002930 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002931 return NULL;
2932 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933
Thomas Wouters89f507f2006-12-13 04:49:30 +00002934 ch = CHILD(n, 2);
2935 if (TYPE(ch) == testlist)
2936 expr2 = ast_for_testlist(c, ch);
2937 else
2938 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002939 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 return NULL;
2941
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002942 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002943 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 return NULL;
2945
Thomas Wouters89f507f2006-12-13 04:49:30 +00002946 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002948 else if (TYPE(CHILD(n, 1)) == annassign) {
2949 expr_ty expr1, expr2, expr3;
2950 node *ch = CHILD(n, 0);
2951 node *deep, *ann = CHILD(n, 1);
2952 int simple = 1;
2953
2954 /* we keep track of parens to qualify (x) as expression not name */
2955 deep = ch;
2956 while (NCH(deep) == 1) {
2957 deep = CHILD(deep, 0);
2958 }
2959 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2960 simple = 0;
2961 }
2962 expr1 = ast_for_testlist(c, ch);
2963 if (!expr1) {
2964 return NULL;
2965 }
2966 switch (expr1->kind) {
2967 case Name_kind:
2968 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2969 return NULL;
2970 }
2971 expr1->v.Name.ctx = Store;
2972 break;
2973 case Attribute_kind:
2974 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2975 return NULL;
2976 }
2977 expr1->v.Attribute.ctx = Store;
2978 break;
2979 case Subscript_kind:
2980 expr1->v.Subscript.ctx = Store;
2981 break;
2982 case List_kind:
2983 ast_error(c, ch,
2984 "only single target (not list) can be annotated");
2985 return NULL;
2986 case Tuple_kind:
2987 ast_error(c, ch,
2988 "only single target (not tuple) can be annotated");
2989 return NULL;
2990 default:
2991 ast_error(c, ch,
2992 "illegal target for annotation");
2993 return NULL;
2994 }
2995
2996 if (expr1->kind != Name_kind) {
2997 simple = 0;
2998 }
2999 ch = CHILD(ann, 1);
3000 expr2 = ast_for_expr(c, ch);
3001 if (!expr2) {
3002 return NULL;
3003 }
3004 if (NCH(ann) == 2) {
3005 return AnnAssign(expr1, expr2, NULL, simple,
3006 LINENO(n), n->n_col_offset, c->c_arena);
3007 }
3008 else {
3009 ch = CHILD(ann, 3);
3010 expr3 = ast_for_expr(c, ch);
3011 if (!expr3) {
3012 return NULL;
3013 }
3014 return AnnAssign(expr1, expr2, expr3, simple,
3015 LINENO(n), n->n_col_offset, c->c_arena);
3016 }
3017 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003019 int i;
3020 asdl_seq *targets;
3021 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 expr_ty expression;
3023
Thomas Wouters89f507f2006-12-13 04:49:30 +00003024 /* a normal assignment */
3025 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003026 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003027 if (!targets)
3028 return NULL;
3029 for (i = 0; i < NCH(n) - 2; i += 2) {
3030 expr_ty e;
3031 node *ch = CHILD(n, i);
3032 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003033 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003034 return NULL;
3035 }
3036 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003040 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003041 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043
Thomas Wouters89f507f2006-12-13 04:49:30 +00003044 asdl_seq_SET(targets, i / 2, e);
3045 }
3046 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003047 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003048 expression = ast_for_testlist(c, value);
3049 else
3050 expression = ast_for_expr(c, value);
3051 if (!expression)
3052 return NULL;
3053 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055}
3056
Benjamin Peterson78565b22009-06-28 19:19:51 +00003057
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003059ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060{
3061 asdl_seq *seq;
3062 int i;
3063 expr_ty e;
3064
3065 REQ(n, exprlist);
3066
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003067 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003069 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003071 e = ast_for_expr(c, CHILD(n, i));
3072 if (!e)
3073 return NULL;
3074 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003075 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003076 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 }
3078 return seq;
3079}
3080
3081static stmt_ty
3082ast_for_del_stmt(struct compiling *c, const node *n)
3083{
3084 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 /* del_stmt: 'del' exprlist */
3087 REQ(n, del_stmt);
3088
3089 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3090 if (!expr_list)
3091 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003092 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093}
3094
3095static stmt_ty
3096ast_for_flow_stmt(struct compiling *c, const node *n)
3097{
3098 /*
3099 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3100 | yield_stmt
3101 break_stmt: 'break'
3102 continue_stmt: 'continue'
3103 return_stmt: 'return' [testlist]
3104 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003105 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 raise_stmt: 'raise' [test [',' test [',' test]]]
3107 */
3108 node *ch;
3109
3110 REQ(n, flow_stmt);
3111 ch = CHILD(n, 0);
3112 switch (TYPE(ch)) {
3113 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003114 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003116 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003118 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3119 if (!exp)
3120 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003121 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 }
3123 case return_stmt:
3124 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003125 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003127 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 if (!expression)
3129 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003130 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 }
3132 case raise_stmt:
3133 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003134 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3135 else if (NCH(ch) >= 2) {
3136 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3138 if (!expression)
3139 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003140 if (NCH(ch) == 4) {
3141 cause = ast_for_expr(c, CHILD(ch, 3));
3142 if (!cause)
3143 return NULL;
3144 }
3145 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 }
3147 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003148 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 "unexpected flow_stmt: %d", TYPE(ch));
3150 return NULL;
3151 }
3152}
3153
3154static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003155alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156{
3157 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003158 import_as_name: NAME ['as' NAME]
3159 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 dotted_name: NAME ('.' NAME)*
3161 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003162 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 loop:
3165 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003166 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003167 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003168 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003169 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003170 if (!name)
3171 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 if (NCH(n) == 3) {
3173 node *str_node = CHILD(n, 2);
3174 str = NEW_IDENTIFIER(str_node);
3175 if (!str)
3176 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003177 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003178 return NULL;
3179 }
3180 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003181 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003182 return NULL;
3183 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003184 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003185 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186 case dotted_as_name:
3187 if (NCH(n) == 1) {
3188 n = CHILD(n, 0);
3189 goto loop;
3190 }
3191 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003192 node *asname_node = CHILD(n, 2);
3193 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003194 if (!a)
3195 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003197 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003198 if (!a->asname)
3199 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003200 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 return a;
3203 }
3204 break;
3205 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003206 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003207 node *name_node = CHILD(n, 0);
3208 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003209 if (!name)
3210 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003211 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003212 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003213 return alias(name, NULL, c->c_arena);
3214 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 else {
3216 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003217 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003218 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221
3222 len = 0;
3223 for (i = 0; i < NCH(n); i += 2)
3224 /* length of string plus one for the dot */
3225 len += strlen(STR(CHILD(n, i))) + 1;
3226 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003227 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 if (!str)
3229 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003230 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 if (!s)
3232 return NULL;
3233 for (i = 0; i < NCH(n); i += 2) {
3234 char *sch = STR(CHILD(n, i));
3235 strcpy(s, STR(CHILD(n, i)));
3236 s += strlen(sch);
3237 *s++ = '.';
3238 }
3239 --s;
3240 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3242 PyBytes_GET_SIZE(str),
3243 NULL);
3244 Py_DECREF(str);
3245 if (!uni)
3246 return NULL;
3247 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003248 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003249 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3250 Py_DECREF(str);
3251 return NULL;
3252 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003253 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254 }
3255 break;
3256 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003257 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003258 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3259 Py_DECREF(str);
3260 return NULL;
3261 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003262 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003264 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265 "unexpected import name: %d", TYPE(n));
3266 return NULL;
3267 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003268
3269 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 return NULL;
3271}
3272
3273static stmt_ty
3274ast_for_import_stmt(struct compiling *c, const node *n)
3275{
3276 /*
3277 import_stmt: import_name | import_from
3278 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003279 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3280 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003282 int lineno;
3283 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 int i;
3285 asdl_seq *aliases;
3286
3287 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003288 lineno = LINENO(n);
3289 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003291 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003293 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003294 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003295 if (!aliases)
3296 return NULL;
3297 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003298 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003299 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003301 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003305 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 int idx, ndots = 0;
3308 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003309 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003311 /* Count the number of dots (for relative imports) and check for the
3312 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003313 for (idx = 1; idx < NCH(n); idx++) {
3314 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003315 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3316 if (!mod)
3317 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 idx++;
3319 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003320 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003322 ndots += 3;
3323 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003324 } else if (TYPE(CHILD(n, idx)) != DOT) {
3325 break;
3326 }
3327 ndots++;
3328 }
3329 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003330 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003331 case STAR:
3332 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 n = CHILD(n, idx);
3334 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003335 break;
3336 case LPAR:
3337 /* from ... import (x, y, z) */
3338 n = CHILD(n, idx + 1);
3339 n_children = NCH(n);
3340 break;
3341 case import_as_names:
3342 /* from ... import x, y, z */
3343 n = CHILD(n, idx);
3344 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003345 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003346 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347 " surrounding parentheses");
3348 return NULL;
3349 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003350 break;
3351 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003352 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353 return NULL;
3354 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003356 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003357 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359
3360 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003361 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003362 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003363 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003365 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003367 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003368 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003369 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003370 if (!import_alias)
3371 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003372 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003375 if (mod != NULL)
3376 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003377 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003378 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379 }
Neal Norwitz79792652005-11-14 04:25:03 +00003380 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 "unknown import statement: starts with command '%s'",
3382 STR(CHILD(n, 0)));
3383 return NULL;
3384}
3385
3386static stmt_ty
3387ast_for_global_stmt(struct compiling *c, const node *n)
3388{
3389 /* global_stmt: 'global' NAME (',' NAME)* */
3390 identifier name;
3391 asdl_seq *s;
3392 int i;
3393
3394 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003395 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003397 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003399 name = NEW_IDENTIFIER(CHILD(n, i));
3400 if (!name)
3401 return NULL;
3402 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003404 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405}
3406
3407static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003408ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3409{
3410 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3411 identifier name;
3412 asdl_seq *s;
3413 int i;
3414
3415 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003416 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003417 if (!s)
3418 return NULL;
3419 for (i = 1; i < NCH(n); i += 2) {
3420 name = NEW_IDENTIFIER(CHILD(n, i));
3421 if (!name)
3422 return NULL;
3423 asdl_seq_SET(s, i / 2, name);
3424 }
3425 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3426}
3427
3428static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003429ast_for_assert_stmt(struct compiling *c, const node *n)
3430{
3431 /* assert_stmt: 'assert' test [',' test] */
3432 REQ(n, assert_stmt);
3433 if (NCH(n) == 2) {
3434 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3435 if (!expression)
3436 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003437 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438 }
3439 else if (NCH(n) == 4) {
3440 expr_ty expr1, expr2;
3441
3442 expr1 = ast_for_expr(c, CHILD(n, 1));
3443 if (!expr1)
3444 return NULL;
3445 expr2 = ast_for_expr(c, CHILD(n, 3));
3446 if (!expr2)
3447 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448
Thomas Wouters89f507f2006-12-13 04:49:30 +00003449 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 }
Neal Norwitz79792652005-11-14 04:25:03 +00003451 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 "improper number of parts to 'assert' statement: %d",
3453 NCH(n));
3454 return NULL;
3455}
3456
3457static asdl_seq *
3458ast_for_suite(struct compiling *c, const node *n)
3459{
3460 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003461 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462 stmt_ty s;
3463 int i, total, num, end, pos = 0;
3464 node *ch;
3465
3466 REQ(n, suite);
3467
3468 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003469 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003471 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003473 n = CHILD(n, 0);
3474 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 */
3477 end = NCH(n) - 1;
3478 if (TYPE(CHILD(n, end - 1)) == SEMI)
3479 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003481 for (i = 0; i < end; i += 2) {
3482 ch = CHILD(n, i);
3483 s = ast_for_stmt(c, ch);
3484 if (!s)
3485 return NULL;
3486 asdl_seq_SET(seq, pos++, s);
3487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 }
3489 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003490 for (i = 2; i < (NCH(n) - 1); i++) {
3491 ch = CHILD(n, i);
3492 REQ(ch, stmt);
3493 num = num_stmts(ch);
3494 if (num == 1) {
3495 /* small_stmt or compound_stmt with only one child */
3496 s = ast_for_stmt(c, ch);
3497 if (!s)
3498 return NULL;
3499 asdl_seq_SET(seq, pos++, s);
3500 }
3501 else {
3502 int j;
3503 ch = CHILD(ch, 0);
3504 REQ(ch, simple_stmt);
3505 for (j = 0; j < NCH(ch); j += 2) {
3506 /* statement terminates with a semi-colon ';' */
3507 if (NCH(CHILD(ch, j)) == 0) {
3508 assert((j + 1) == NCH(ch));
3509 break;
3510 }
3511 s = ast_for_stmt(c, CHILD(ch, j));
3512 if (!s)
3513 return NULL;
3514 asdl_seq_SET(seq, pos++, s);
3515 }
3516 }
3517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518 }
3519 assert(pos == seq->size);
3520 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521}
3522
3523static stmt_ty
3524ast_for_if_stmt(struct compiling *c, const node *n)
3525{
3526 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3527 ['else' ':' suite]
3528 */
3529 char *s;
3530
3531 REQ(n, if_stmt);
3532
3533 if (NCH(n) == 4) {
3534 expr_ty expression;
3535 asdl_seq *suite_seq;
3536
3537 expression = ast_for_expr(c, CHILD(n, 1));
3538 if (!expression)
3539 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003541 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543
Guido van Rossumd8faa362007-04-27 19:54:29 +00003544 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3545 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 s = STR(CHILD(n, 4));
3549 /* s[2], the third character in the string, will be
3550 's' for el_s_e, or
3551 'i' for el_i_f
3552 */
3553 if (s[2] == 's') {
3554 expr_ty expression;
3555 asdl_seq *seq1, *seq2;
3556
3557 expression = ast_for_expr(c, CHILD(n, 1));
3558 if (!expression)
3559 return NULL;
3560 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003561 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562 return NULL;
3563 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003564 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565 return NULL;
3566
Guido van Rossumd8faa362007-04-27 19:54:29 +00003567 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3568 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 }
3570 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003571 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003572 expr_ty expression;
3573 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003574 asdl_seq *orelse = NULL;
3575 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 /* must reference the child n_elif+1 since 'else' token is third,
3577 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003578 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3579 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3580 has_else = 1;
3581 n_elif -= 3;
3582 }
3583 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584
Thomas Wouters89f507f2006-12-13 04:49:30 +00003585 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003586 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003588 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003589 if (!orelse)
3590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003592 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003594 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3595 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003597 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3598 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 asdl_seq_SET(orelse, 0,
3602 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003603 LINENO(CHILD(n, NCH(n) - 6)),
3604 CHILD(n, NCH(n) - 6)->n_col_offset,
3605 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003606 /* the just-created orelse handled the last elif */
3607 n_elif--;
3608 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 for (i = 0; i < n_elif; i++) {
3611 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003612 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003613 if (!newobj)
3614 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003616 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003619 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621
Thomas Wouters89f507f2006-12-13 04:49:30 +00003622 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003624 LINENO(CHILD(n, off)),
3625 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003626 orelse = newobj;
3627 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003628 expression = ast_for_expr(c, CHILD(n, 1));
3629 if (!expression)
3630 return NULL;
3631 suite_seq = ast_for_suite(c, CHILD(n, 3));
3632 if (!suite_seq)
3633 return NULL;
3634 return If(expression, suite_seq, orelse,
3635 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003637
3638 PyErr_Format(PyExc_SystemError,
3639 "unexpected token in 'if' statement: %s", s);
3640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641}
3642
3643static stmt_ty
3644ast_for_while_stmt(struct compiling *c, const node *n)
3645{
3646 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3647 REQ(n, while_stmt);
3648
3649 if (NCH(n) == 4) {
3650 expr_ty expression;
3651 asdl_seq *suite_seq;
3652
3653 expression = ast_for_expr(c, CHILD(n, 1));
3654 if (!expression)
3655 return NULL;
3656 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003657 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003658 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003659 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 }
3661 else if (NCH(n) == 7) {
3662 expr_ty expression;
3663 asdl_seq *seq1, *seq2;
3664
3665 expression = ast_for_expr(c, CHILD(n, 1));
3666 if (!expression)
3667 return NULL;
3668 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003669 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 return NULL;
3671 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003672 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 return NULL;
3674
Thomas Wouters89f507f2006-12-13 04:49:30 +00003675 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003677
3678 PyErr_Format(PyExc_SystemError,
3679 "wrong number of tokens for 'while' statement: %d",
3680 NCH(n));
3681 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003682}
3683
3684static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003685ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003687 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003689 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003690 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3692 REQ(n, for_stmt);
3693
3694 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003695 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 if (!seq)
3697 return NULL;
3698 }
3699
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003700 node_target = CHILD(n, 1);
3701 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003702 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003704 /* Check the # of children rather than the length of _target, since
3705 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003706 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003707 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003708 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003710 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003712 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003713 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 return NULL;
3715 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003716 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 return NULL;
3718
Yury Selivanov75445082015-05-11 22:57:16 -04003719 if (is_async)
3720 return AsyncFor(target, expression, suite_seq, seq,
3721 LINENO(n), n->n_col_offset,
3722 c->c_arena);
3723 else
3724 return For(target, expression, suite_seq, seq,
3725 LINENO(n), n->n_col_offset,
3726 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727}
3728
3729static excepthandler_ty
3730ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3731{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003732 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 REQ(exc, except_clause);
3734 REQ(body, suite);
3735
3736 if (NCH(exc) == 1) {
3737 asdl_seq *suite_seq = ast_for_suite(c, body);
3738 if (!suite_seq)
3739 return NULL;
3740
Neal Norwitzad74aa82008-03-31 05:14:30 +00003741 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003742 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743 }
3744 else if (NCH(exc) == 2) {
3745 expr_ty expression;
3746 asdl_seq *suite_seq;
3747
3748 expression = ast_for_expr(c, CHILD(exc, 1));
3749 if (!expression)
3750 return NULL;
3751 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003752 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003753 return NULL;
3754
Neal Norwitzad74aa82008-03-31 05:14:30 +00003755 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003756 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 }
3758 else if (NCH(exc) == 4) {
3759 asdl_seq *suite_seq;
3760 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003761 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003762 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003764 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003767 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 return NULL;
3769 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003770 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 return NULL;
3772
Neal Norwitzad74aa82008-03-31 05:14:30 +00003773 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003774 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003776
3777 PyErr_Format(PyExc_SystemError,
3778 "wrong number of children for 'except' clause: %d",
3779 NCH(exc));
3780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781}
3782
3783static stmt_ty
3784ast_for_try_stmt(struct compiling *c, const node *n)
3785{
Neal Norwitzf599f422005-12-17 21:33:47 +00003786 const int nch = NCH(n);
3787 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003788 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790 REQ(n, try_stmt);
3791
Neal Norwitzf599f422005-12-17 21:33:47 +00003792 body = ast_for_suite(c, CHILD(n, 2));
3793 if (body == NULL)
3794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795
Neal Norwitzf599f422005-12-17 21:33:47 +00003796 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3797 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3798 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3799 /* we can assume it's an "else",
3800 because nch >= 9 for try-else-finally and
3801 it would otherwise have a type of except_clause */
3802 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3803 if (orelse == NULL)
3804 return NULL;
3805 n_except--;
3806 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003807
Neal Norwitzf599f422005-12-17 21:33:47 +00003808 finally = ast_for_suite(c, CHILD(n, nch - 1));
3809 if (finally == NULL)
3810 return NULL;
3811 n_except--;
3812 }
3813 else {
3814 /* we can assume it's an "else",
3815 otherwise it would have a type of except_clause */
3816 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3817 if (orelse == NULL)
3818 return NULL;
3819 n_except--;
3820 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003822 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003823 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 return NULL;
3825 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826
Neal Norwitzf599f422005-12-17 21:33:47 +00003827 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003828 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003829 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003830 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003831 if (handlers == NULL)
3832 return NULL;
3833
3834 for (i = 0; i < n_except; i++) {
3835 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3836 CHILD(n, 5 + i * 3));
3837 if (!e)
3838 return NULL;
3839 asdl_seq_SET(handlers, i, e);
3840 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003841 }
3842
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003843 assert(finally != NULL || asdl_seq_LEN(handlers));
3844 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845}
3846
Georg Brandl0c315622009-05-25 21:10:36 +00003847/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003848static withitem_ty
3849ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003850{
3851 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003852
Georg Brandl0c315622009-05-25 21:10:36 +00003853 REQ(n, with_item);
3854 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003855 if (!context_expr)
3856 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003857 if (NCH(n) == 3) {
3858 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003859
3860 if (!optional_vars) {
3861 return NULL;
3862 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003863 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003864 return NULL;
3865 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003866 }
3867
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003868 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003869}
3870
Georg Brandl0c315622009-05-25 21:10:36 +00003871/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3872static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003873ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003874{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003875 int i, n_items;
3876 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003877
3878 REQ(n, with_stmt);
3879
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003880 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003881 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003882 if (!items)
3883 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003884 for (i = 1; i < NCH(n) - 2; i += 2) {
3885 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3886 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003887 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003888 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003889 }
3890
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003891 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3892 if (!body)
3893 return NULL;
3894
Yury Selivanov75445082015-05-11 22:57:16 -04003895 if (is_async)
3896 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3897 else
3898 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003899}
3900
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003902ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003904 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003905 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003906 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003907 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003908
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909 REQ(n, classdef);
3910
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003911 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003912 s = ast_for_suite(c, CHILD(n, 3));
3913 if (!s)
3914 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003915 classname = NEW_IDENTIFIER(CHILD(n, 1));
3916 if (!classname)
3917 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003918 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003919 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003920 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3921 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003923
3924 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003925 s = ast_for_suite(c, CHILD(n,5));
3926 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003927 return NULL;
3928 classname = NEW_IDENTIFIER(CHILD(n, 1));
3929 if (!classname)
3930 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003931 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003932 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003933 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3934 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935 }
3936
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003937 /* class NAME '(' arglist ')' ':' suite */
3938 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003939 {
3940 PyObject *dummy_name;
3941 expr_ty dummy;
3942 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3943 if (!dummy_name)
3944 return NULL;
3945 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3946 call = ast_for_call(c, CHILD(n, 3), dummy);
3947 if (!call)
3948 return NULL;
3949 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003951 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003953 classname = NEW_IDENTIFIER(CHILD(n, 1));
3954 if (!classname)
3955 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003956 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003957 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003958
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003959 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003960 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961}
3962
3963static stmt_ty
3964ast_for_stmt(struct compiling *c, const node *n)
3965{
3966 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003967 assert(NCH(n) == 1);
3968 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969 }
3970 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003971 assert(num_stmts(n) == 1);
3972 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 }
3974 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003975 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003976 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3977 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003978 */
3979 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 case expr_stmt:
3981 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 case del_stmt:
3983 return ast_for_del_stmt(c, n);
3984 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003985 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 case flow_stmt:
3987 return ast_for_flow_stmt(c, n);
3988 case import_stmt:
3989 return ast_for_import_stmt(c, n);
3990 case global_stmt:
3991 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003992 case nonlocal_stmt:
3993 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003994 case assert_stmt:
3995 return ast_for_assert_stmt(c, n);
3996 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003997 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003998 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3999 TYPE(n), NCH(n));
4000 return NULL;
4001 }
4002 }
4003 else {
4004 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004005 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004006 */
4007 node *ch = CHILD(n, 0);
4008 REQ(n, compound_stmt);
4009 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 case if_stmt:
4011 return ast_for_if_stmt(c, ch);
4012 case while_stmt:
4013 return ast_for_while_stmt(c, ch);
4014 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004015 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 case try_stmt:
4017 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004018 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004019 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004021 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004023 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 case decorated:
4025 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004026 case async_stmt:
4027 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004029 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4031 TYPE(n), NCH(n));
4032 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004033 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034 }
4035}
4036
4037static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004038parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004040 const char *end;
4041 long x;
4042 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004043 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004044 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004046 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 errno = 0;
4048 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004049 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004050 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004051 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004052 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004053 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004054 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 }
4056 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004057 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004058 if (*end == '\0') {
4059 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004060 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004061 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004062 }
4063 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004064 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004065 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004066 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4067 if (compl.imag == -1.0 && PyErr_Occurred())
4068 return NULL;
4069 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004070 }
4071 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004072 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004073 dx = PyOS_string_to_double(s, NULL, NULL);
4074 if (dx == -1.0 && PyErr_Occurred())
4075 return NULL;
4076 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004077 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004078}
4079
4080static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004081parsenumber(struct compiling *c, const char *s)
4082{
4083 char *dup, *end;
4084 PyObject *res = NULL;
4085
4086 assert(s != NULL);
4087
4088 if (strchr(s, '_') == NULL) {
4089 return parsenumber_raw(c, s);
4090 }
4091 /* Create a duplicate without underscores. */
4092 dup = PyMem_Malloc(strlen(s) + 1);
4093 end = dup;
4094 for (; *s; s++) {
4095 if (*s != '_') {
4096 *end++ = *s;
4097 }
4098 }
4099 *end = '\0';
4100 res = parsenumber_raw(c, dup);
4101 PyMem_Free(dup);
4102 return res;
4103}
4104
4105static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004106decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004107{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004108 const char *s, *t;
4109 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004110 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4111 while (s < end && (*s & 0x80)) s++;
4112 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004113 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004114}
4115
4116static PyObject *
Benjamin Peterson768921c2016-02-25 23:13:53 -08004117decode_unicode_with_escapes(struct compiling *c, const char *s, size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004119 PyObject *v, *u;
4120 char *buf;
4121 char *p;
4122 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004123
Benjamin Peterson202803a2016-02-25 22:34:45 -08004124 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004125 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004126 return NULL;
4127 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4128 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4129 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4130 if (u == NULL)
4131 return NULL;
4132 p = buf = PyBytes_AsString(u);
4133 end = s + len;
4134 while (s < end) {
4135 if (*s == '\\') {
4136 *p++ = *s++;
4137 if (*s & 0x80) {
4138 strcpy(p, "u005c");
4139 p += 5;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004140 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004141 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004142 if (*s & 0x80) { /* XXX inefficient */
4143 PyObject *w;
4144 int kind;
4145 void *data;
4146 Py_ssize_t len, i;
4147 w = decode_utf8(c, &s, end);
4148 if (w == NULL) {
4149 Py_DECREF(u);
4150 return NULL;
4151 }
4152 kind = PyUnicode_KIND(w);
4153 data = PyUnicode_DATA(w);
4154 len = PyUnicode_GET_LENGTH(w);
4155 for (i = 0; i < len; i++) {
4156 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4157 sprintf(p, "\\U%08x", chr);
4158 p += 10;
4159 }
4160 /* Should be impossible to overflow */
4161 assert(p - buf <= Py_SIZE(u));
4162 Py_DECREF(w);
4163 } else {
4164 *p++ = *s++;
4165 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004166 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004167 len = p - buf;
4168 s = buf;
4169
Eric V. Smith5567f892015-09-21 13:36:09 -04004170 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004171 Py_XDECREF(u);
4172 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173}
4174
Eric V. Smith451d0e32016-09-09 21:56:20 -04004175/* Compile this expression in to an expr_ty. Add parens around the
4176 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004177static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004178fstring_compile_expr(const char *expr_start, const char *expr_end,
4179 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004180
Eric V. Smith235a6f02015-09-19 14:51:32 -04004181{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004182 int all_whitespace = 1;
4183 int kind;
4184 void *data;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004185 PyCompilerFlags cf;
4186 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004187 char *str;
4188 PyObject *o;
4189 Py_ssize_t len;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004190 Py_ssize_t i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004191
Eric V. Smith1d44c412015-09-23 07:49:00 -04004192 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004193 assert(*(expr_start-1) == '{');
4194 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004195
Eric V. Smith451d0e32016-09-09 21:56:20 -04004196 /* We know there are no escapes here, because backslashes are not allowed,
4197 and we know it's utf-8 encoded (per PEP 263). But, in order to check
4198 that each char is not whitespace, we need to decode it to unicode.
4199 Which is unfortunate, but such is life. */
Eric V. Smith1d44c412015-09-23 07:49:00 -04004200
Eric V. Smith451d0e32016-09-09 21:56:20 -04004201 /* If the substring is all whitespace, it's an error. We need to catch
4202 this here, and not when we call PyParser_ASTFromString, because turning
4203 the expression '' in to '()' would go from being invalid to valid. */
4204 /* Note that this code says an empty string is all whitespace. That's
4205 important. There's a test for it: f'{}'. */
4206 o = PyUnicode_DecodeUTF8(expr_start, expr_end-expr_start, NULL);
4207 if (o == NULL)
4208 return NULL;
4209 len = PyUnicode_GET_LENGTH(o);
4210 kind = PyUnicode_KIND(o);
4211 data = PyUnicode_DATA(o);
4212 for (i = 0; i < len; i++) {
4213 if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004214 all_whitespace = 0;
4215 break;
4216 }
4217 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004218 Py_DECREF(o);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004219 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004220 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004221 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004222 }
4223
Eric V. Smith451d0e32016-09-09 21:56:20 -04004224 /* Reuse len to be the length of the utf-8 input string. */
4225 len = expr_end - expr_start;
4226 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4227 str = PyMem_RawMalloc(len + 3);
4228 if (str == NULL)
4229 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004230
Eric V. Smith451d0e32016-09-09 21:56:20 -04004231 str[0] = '(';
4232 memcpy(str+1, expr_start, len);
4233 str[len+1] = ')';
4234 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004235
4236 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004237 mod = PyParser_ASTFromString(str, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004238 Py_eval_input, &cf, c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004239 PyMem_RawFree(str);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004240 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004241 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004242 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004243}
4244
4245/* Return -1 on error.
4246
4247 Return 0 if we reached the end of the literal.
4248
4249 Return 1 if we haven't reached the end of the literal, but we want
4250 the caller to process the literal up to this point. Used for
4251 doubled braces.
4252*/
4253static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004254fstring_find_literal(const char **str, const char *end, int raw,
4255 PyObject **literal, int recurse_lvl,
4256 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004257{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004258 /* Get any literal string. It ends when we hit an un-doubled left
4259 brace (which isn't part of a unicode name escape such as
4260 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004261
Eric V. Smith451d0e32016-09-09 21:56:20 -04004262 const char *literal_start = *str;
4263 const char *literal_end;
4264 int in_named_escape = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004265 int result = 0;
4266
Eric V. Smith235a6f02015-09-19 14:51:32 -04004267 assert(*literal == NULL);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004268 for (; *str < end; (*str)++) {
4269 char ch = **str;
4270 if (!in_named_escape && ch == '{' && (*str)-literal_start >= 2 &&
4271 *(*str-2) == '\\' && *(*str-1) == 'N') {
4272 in_named_escape = 1;
4273 } else if (in_named_escape && ch == '}') {
4274 in_named_escape = 0;
4275 } else if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004276 /* Check for doubled braces, but only at the top level. If
4277 we checked at every level, then f'{0:{3}}' would fail
4278 with the two closing braces. */
4279 if (recurse_lvl == 0) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004280 if (*str+1 < end && *(*str+1) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004281 /* We're going to tell the caller that the literal ends
4282 here, but that they should continue scanning. But also
4283 skip over the second brace when we resume scanning. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004284 literal_end = *str+1;
4285 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004286 result = 1;
4287 goto done;
4288 }
4289
4290 /* Where a single '{' is the start of a new expression, a
4291 single '}' is not allowed. */
4292 if (ch == '}') {
4293 ast_error(c, n, "f-string: single '}' is not allowed");
4294 return -1;
4295 }
4296 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004297 /* We're either at a '{', which means we're starting another
4298 expression; or a '}', which means we're at the end of this
4299 f-string (for a nested format_spec). */
4300 break;
4301 }
4302 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004303 literal_end = *str;
4304 assert(*str <= end);
4305 assert(*str == end || **str == '{' || **str == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004306done:
4307 if (literal_start != literal_end) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004308 if (raw)
4309 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
4310 literal_end-literal_start,
4311 NULL, NULL);
4312 else
4313 *literal = decode_unicode_with_escapes(c, literal_start,
4314 literal_end-literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004315 if (!*literal)
4316 return -1;
4317 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004318 return result;
4319}
4320
4321/* Forward declaration because parsing is recursive. */
4322static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004323fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004324 struct compiling *c, const node *n);
4325
Eric V. Smith451d0e32016-09-09 21:56:20 -04004326/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004327 expression (so it must be a '{'). Returns the FormattedValue node,
4328 which includes the expression, conversion character, and
4329 format_spec expression.
4330
4331 Note that I don't do a perfect job here: I don't make sure that a
4332 closing brace doesn't match an opening paren, for example. It
4333 doesn't need to error on all invalid expressions, just correctly
4334 find the end of all valid ones. Any errors inside the expression
4335 will be caught when we parse it later. */
4336static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004337fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004338 expr_ty *expression, struct compiling *c, const node *n)
4339{
4340 /* Return -1 on error, else 0. */
4341
Eric V. Smith451d0e32016-09-09 21:56:20 -04004342 const char *expr_start;
4343 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004344 expr_ty simple_expression;
4345 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004346 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004347
4348 /* 0 if we're not in a string, else the quote char we're trying to
4349 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004350 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004351
4352 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4353 int string_type = 0;
4354
4355 /* Keep track of nesting level for braces/parens/brackets in
4356 expressions. */
4357 Py_ssize_t nested_depth = 0;
4358
4359 /* Can only nest one level deep. */
4360 if (recurse_lvl >= 2) {
4361 ast_error(c, n, "f-string: expressions nested too deeply");
4362 return -1;
4363 }
4364
4365 /* The first char must be a left brace, or we wouldn't have gotten
4366 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004367 assert(**str == '{');
4368 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004369
Eric V. Smith451d0e32016-09-09 21:56:20 -04004370 expr_start = *str;
4371 for (; *str < end; (*str)++) {
4372 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004373
4374 /* Loop invariants. */
4375 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004376 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004377 if (quote_char)
4378 assert(string_type == 1 || string_type == 3);
4379 else
4380 assert(string_type == 0);
4381
Eric V. Smith451d0e32016-09-09 21:56:20 -04004382 ch = **str;
4383 /* Nowhere inside an expression is a backslash allowed. */
4384 if (ch == '\\') {
4385 /* Error: can't include a backslash character, inside
4386 parens or strings or not. */
4387 ast_error(c, n, "f-string expression part "
4388 "cannot include a backslash");
4389 return -1;
4390 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004391 if (quote_char) {
4392 /* We're inside a string. See if we're at the end. */
4393 /* This code needs to implement the same non-error logic
4394 as tok_get from tokenizer.c, at the letter_quote
4395 label. To actually share that code would be a
4396 nightmare. But, it's unlikely to change and is small,
4397 so duplicate it here. Note we don't need to catch all
4398 of the errors, since they'll be caught when parsing the
4399 expression. We just need to match the non-error
4400 cases. Thus we can ignore \n in single-quoted strings,
4401 for example. Or non-terminated strings. */
4402 if (ch == quote_char) {
4403 /* Does this match the string_type (single or triple
4404 quoted)? */
4405 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004406 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004407 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004408 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004409 string_type = 0;
4410 quote_char = 0;
4411 continue;
4412 }
4413 } else {
4414 /* We're at the end of a normal string. */
4415 quote_char = 0;
4416 string_type = 0;
4417 continue;
4418 }
4419 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004420 } else if (ch == '\'' || ch == '"') {
4421 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004422 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004423 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004424 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004425 } else {
4426 /* Start of a normal string. */
4427 string_type = 1;
4428 }
4429 /* Start looking for the end of the string. */
4430 quote_char = ch;
4431 } else if (ch == '[' || ch == '{' || ch == '(') {
4432 nested_depth++;
4433 } else if (nested_depth != 0 &&
4434 (ch == ']' || ch == '}' || ch == ')')) {
4435 nested_depth--;
4436 } else if (ch == '#') {
4437 /* Error: can't include a comment character, inside parens
4438 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004439 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004440 return -1;
4441 } else if (nested_depth == 0 &&
4442 (ch == '!' || ch == ':' || ch == '}')) {
4443 /* First, test for the special case of "!=". Since '=' is
4444 not an allowed conversion character, nothing is lost in
4445 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004446 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004447 /* This isn't a conversion character, just continue. */
4448 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004449 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004450 /* Normal way out of this loop. */
4451 break;
4452 } else {
4453 /* Just consume this char and loop around. */
4454 }
4455 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004456 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004457 /* If we leave this loop in a string or with mismatched parens, we
4458 don't care. We'll get a syntax error when compiling the
4459 expression. But, we can produce a better error message, so
4460 let's just do that.*/
4461 if (quote_char) {
4462 ast_error(c, n, "f-string: unterminated string");
4463 return -1;
4464 }
4465 if (nested_depth) {
4466 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4467 return -1;
4468 }
4469
Eric V. Smith451d0e32016-09-09 21:56:20 -04004470 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004471 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004472
4473 /* Compile the expression as soon as possible, so we show errors
4474 related to the expression before errors related to the
4475 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004476 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004477 if (!simple_expression)
4478 return -1;
4479
4480 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004481 if (**str == '!') {
4482 *str += 1;
4483 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004484 goto unexpected_end_of_string;
4485
Eric V. Smith451d0e32016-09-09 21:56:20 -04004486 conversion = **str;
4487 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004488
4489 /* Validate the conversion. */
4490 if (!(conversion == 's' || conversion == 'r'
4491 || conversion == 'a')) {
4492 ast_error(c, n, "f-string: invalid conversion character: "
4493 "expected 's', 'r', or 'a'");
4494 return -1;
4495 }
4496 }
4497
4498 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004499 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004500 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004501 if (**str == ':') {
4502 *str += 1;
4503 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004504 goto unexpected_end_of_string;
4505
4506 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004507 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004508 if (!format_spec)
4509 return -1;
4510 }
4511
Eric V. Smith451d0e32016-09-09 21:56:20 -04004512 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004513 goto unexpected_end_of_string;
4514
4515 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004516 assert(*str < end);
4517 assert(**str == '}');
4518 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004519
Eric V. Smith451d0e32016-09-09 21:56:20 -04004520 /* And now create the FormattedValue node that represents this
4521 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004522 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004523 format_spec, LINENO(n), n->n_col_offset,
4524 c->c_arena);
4525 if (!*expression)
4526 return -1;
4527
4528 return 0;
4529
4530unexpected_end_of_string:
4531 ast_error(c, n, "f-string: expecting '}'");
4532 return -1;
4533}
4534
4535/* Return -1 on error.
4536
4537 Return 0 if we have a literal (possible zero length) and an
4538 expression (zero length if at the end of the string.
4539
4540 Return 1 if we have a literal, but no expression, and we want the
4541 caller to call us again. This is used to deal with doubled
4542 braces.
4543
4544 When called multiple times on the string 'a{{b{0}c', this function
4545 will return:
4546
4547 1. the literal 'a{' with no expression, and a return value
4548 of 1. Despite the fact that there's no expression, the return
4549 value of 1 means we're not finished yet.
4550
4551 2. the literal 'b' and the expression '0', with a return value of
4552 0. The fact that there's an expression means we're not finished.
4553
4554 3. literal 'c' with no expression and a return value of 0. The
4555 combination of the return value of 0 with no expression means
4556 we're finished.
4557*/
4558static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004559fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4560 int recurse_lvl, PyObject **literal,
4561 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004562 struct compiling *c, const node *n)
4563{
4564 int result;
4565
4566 assert(*literal == NULL && *expression == NULL);
4567
4568 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004569 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004570 if (result < 0)
4571 goto error;
4572
4573 assert(result == 0 || result == 1);
4574
4575 if (result == 1)
4576 /* We have a literal, but don't look at the expression. */
4577 return 1;
4578
Eric V. Smith451d0e32016-09-09 21:56:20 -04004579 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004580 /* We're at the end of the string or the end of a nested
4581 f-string: no expression. The top-level error case where we
4582 expect to be at the end of the string but we're at a '}' is
4583 handled later. */
4584 return 0;
4585
4586 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004587 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004588
Eric V. Smith451d0e32016-09-09 21:56:20 -04004589 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004590 goto error;
4591
4592 return 0;
4593
4594error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004595 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004596 return -1;
4597}
4598
4599#define EXPRLIST_N_CACHED 64
4600
4601typedef struct {
4602 /* Incrementally build an array of expr_ty, so be used in an
4603 asdl_seq. Cache some small but reasonably sized number of
4604 expr_ty's, and then after that start dynamically allocating,
4605 doubling the number allocated each time. Note that the f-string
4606 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4607 Str for the literal 'a'. So you add expr_ty's about twice as
4608 fast as you add exressions in an f-string. */
4609
4610 Py_ssize_t allocated; /* Number we've allocated. */
4611 Py_ssize_t size; /* Number we've used. */
4612 expr_ty *p; /* Pointer to the memory we're actually
4613 using. Will point to 'data' until we
4614 start dynamically allocating. */
4615 expr_ty data[EXPRLIST_N_CACHED];
4616} ExprList;
4617
4618#ifdef NDEBUG
4619#define ExprList_check_invariants(l)
4620#else
4621static void
4622ExprList_check_invariants(ExprList *l)
4623{
4624 /* Check our invariants. Make sure this object is "live", and
4625 hasn't been deallocated. */
4626 assert(l->size >= 0);
4627 assert(l->p != NULL);
4628 if (l->size <= EXPRLIST_N_CACHED)
4629 assert(l->data == l->p);
4630}
4631#endif
4632
4633static void
4634ExprList_Init(ExprList *l)
4635{
4636 l->allocated = EXPRLIST_N_CACHED;
4637 l->size = 0;
4638
4639 /* Until we start allocating dynamically, p points to data. */
4640 l->p = l->data;
4641
4642 ExprList_check_invariants(l);
4643}
4644
4645static int
4646ExprList_Append(ExprList *l, expr_ty exp)
4647{
4648 ExprList_check_invariants(l);
4649 if (l->size >= l->allocated) {
4650 /* We need to alloc (or realloc) the memory. */
4651 Py_ssize_t new_size = l->allocated * 2;
4652
4653 /* See if we've ever allocated anything dynamically. */
4654 if (l->p == l->data) {
4655 Py_ssize_t i;
4656 /* We're still using the cached data. Switch to
4657 alloc-ing. */
4658 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4659 if (!l->p)
4660 return -1;
4661 /* Copy the cached data into the new buffer. */
4662 for (i = 0; i < l->size; i++)
4663 l->p[i] = l->data[i];
4664 } else {
4665 /* Just realloc. */
4666 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4667 if (!tmp) {
4668 PyMem_RawFree(l->p);
4669 l->p = NULL;
4670 return -1;
4671 }
4672 l->p = tmp;
4673 }
4674
4675 l->allocated = new_size;
4676 assert(l->allocated == 2 * l->size);
4677 }
4678
4679 l->p[l->size++] = exp;
4680
4681 ExprList_check_invariants(l);
4682 return 0;
4683}
4684
4685static void
4686ExprList_Dealloc(ExprList *l)
4687{
4688 ExprList_check_invariants(l);
4689
4690 /* If there's been an error, or we've never dynamically allocated,
4691 do nothing. */
4692 if (!l->p || l->p == l->data) {
4693 /* Do nothing. */
4694 } else {
4695 /* We have dynamically allocated. Free the memory. */
4696 PyMem_RawFree(l->p);
4697 }
4698 l->p = NULL;
4699 l->size = -1;
4700}
4701
4702static asdl_seq *
4703ExprList_Finish(ExprList *l, PyArena *arena)
4704{
4705 asdl_seq *seq;
4706
4707 ExprList_check_invariants(l);
4708
4709 /* Allocate the asdl_seq and copy the expressions in to it. */
4710 seq = _Py_asdl_seq_new(l->size, arena);
4711 if (seq) {
4712 Py_ssize_t i;
4713 for (i = 0; i < l->size; i++)
4714 asdl_seq_SET(seq, i, l->p[i]);
4715 }
4716 ExprList_Dealloc(l);
4717 return seq;
4718}
4719
4720/* The FstringParser is designed to add a mix of strings and
4721 f-strings, and concat them together as needed. Ultimately, it
4722 generates an expr_ty. */
4723typedef struct {
4724 PyObject *last_str;
4725 ExprList expr_list;
4726} FstringParser;
4727
4728#ifdef NDEBUG
4729#define FstringParser_check_invariants(state)
4730#else
4731static void
4732FstringParser_check_invariants(FstringParser *state)
4733{
4734 if (state->last_str)
4735 assert(PyUnicode_CheckExact(state->last_str));
4736 ExprList_check_invariants(&state->expr_list);
4737}
4738#endif
4739
4740static void
4741FstringParser_Init(FstringParser *state)
4742{
4743 state->last_str = NULL;
4744 ExprList_Init(&state->expr_list);
4745 FstringParser_check_invariants(state);
4746}
4747
4748static void
4749FstringParser_Dealloc(FstringParser *state)
4750{
4751 FstringParser_check_invariants(state);
4752
4753 Py_XDECREF(state->last_str);
4754 ExprList_Dealloc(&state->expr_list);
4755}
4756
4757/* Make a Str node, but decref the PyUnicode object being added. */
4758static expr_ty
4759make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4760{
4761 PyObject *s = *str;
4762 *str = NULL;
4763 assert(PyUnicode_CheckExact(s));
4764 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4765 Py_DECREF(s);
4766 return NULL;
4767 }
4768 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4769}
4770
4771/* Add a non-f-string (that is, a regular literal string). str is
4772 decref'd. */
4773static int
4774FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4775{
4776 FstringParser_check_invariants(state);
4777
4778 assert(PyUnicode_CheckExact(str));
4779
4780 if (PyUnicode_GET_LENGTH(str) == 0) {
4781 Py_DECREF(str);
4782 return 0;
4783 }
4784
4785 if (!state->last_str) {
4786 /* We didn't have a string before, so just remember this one. */
4787 state->last_str = str;
4788 } else {
4789 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004790 PyUnicode_AppendAndDel(&state->last_str, str);
4791 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004792 return -1;
4793 }
4794 FstringParser_check_invariants(state);
4795 return 0;
4796}
4797
Eric V. Smith451d0e32016-09-09 21:56:20 -04004798/* Parse an f-string. The f-string is in *str to end, with no
4799 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004800static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004801FstringParser_ConcatFstring(FstringParser *state, const char **str,
4802 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004803 struct compiling *c, const node *n)
4804{
4805 FstringParser_check_invariants(state);
4806
4807 /* Parse the f-string. */
4808 while (1) {
4809 PyObject *literal = NULL;
4810 expr_ty expression = NULL;
4811
4812 /* If there's a zero length literal in front of the
4813 expression, literal will be NULL. If we're at the end of
4814 the f-string, expression will be NULL (unless result == 1,
4815 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004816 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004817 &literal, &expression,
4818 c, n);
4819 if (result < 0)
4820 return -1;
4821
4822 /* Add the literal, if any. */
4823 if (!literal) {
4824 /* Do nothing. Just leave last_str alone (and possibly
4825 NULL). */
4826 } else if (!state->last_str) {
4827 state->last_str = literal;
4828 literal = NULL;
4829 } else {
4830 /* We have a literal, concatenate it. */
4831 assert(PyUnicode_GET_LENGTH(literal) != 0);
4832 if (FstringParser_ConcatAndDel(state, literal) < 0)
4833 return -1;
4834 literal = NULL;
4835 }
4836 assert(!state->last_str ||
4837 PyUnicode_GET_LENGTH(state->last_str) != 0);
4838
4839 /* We've dealt with the literal now. It can't be leaked on further
4840 errors. */
4841 assert(literal == NULL);
4842
4843 /* See if we should just loop around to get the next literal
4844 and expression, while ignoring the expression this
4845 time. This is used for un-doubling braces, as an
4846 optimization. */
4847 if (result == 1)
4848 continue;
4849
4850 if (!expression)
4851 /* We're done with this f-string. */
4852 break;
4853
4854 /* We know we have an expression. Convert any existing string
4855 to a Str node. */
4856 if (!state->last_str) {
4857 /* Do nothing. No previous literal. */
4858 } else {
4859 /* Convert the existing last_str literal to a Str node. */
4860 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4861 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4862 return -1;
4863 }
4864
4865 if (ExprList_Append(&state->expr_list, expression) < 0)
4866 return -1;
4867 }
4868
Eric V. Smith235a6f02015-09-19 14:51:32 -04004869 /* If recurse_lvl is zero, then we must be at the end of the
4870 string. Otherwise, we must be at a right brace. */
4871
Eric V. Smith451d0e32016-09-09 21:56:20 -04004872 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004873 ast_error(c, n, "f-string: unexpected end of string");
4874 return -1;
4875 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004876 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004877 ast_error(c, n, "f-string: expecting '}'");
4878 return -1;
4879 }
4880
4881 FstringParser_check_invariants(state);
4882 return 0;
4883}
4884
4885/* Convert the partial state reflected in last_str and expr_list to an
4886 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4887static expr_ty
4888FstringParser_Finish(FstringParser *state, struct compiling *c,
4889 const node *n)
4890{
4891 asdl_seq *seq;
4892
4893 FstringParser_check_invariants(state);
4894
4895 /* If we're just a constant string with no expressions, return
4896 that. */
4897 if(state->expr_list.size == 0) {
4898 if (!state->last_str) {
4899 /* Create a zero length string. */
4900 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4901 if (!state->last_str)
4902 goto error;
4903 }
4904 return make_str_node_and_del(&state->last_str, c, n);
4905 }
4906
4907 /* Create a Str node out of last_str, if needed. It will be the
4908 last node in our expression list. */
4909 if (state->last_str) {
4910 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4911 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4912 goto error;
4913 }
4914 /* This has already been freed. */
4915 assert(state->last_str == NULL);
4916
4917 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4918 if (!seq)
4919 goto error;
4920
4921 /* If there's only one expression, return it. Otherwise, we need
4922 to join them together. */
4923 if (seq->size == 1)
4924 return seq->elements[0];
4925
4926 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4927
4928error:
4929 FstringParser_Dealloc(state);
4930 return NULL;
4931}
4932
Eric V. Smith451d0e32016-09-09 21:56:20 -04004933/* Given an f-string (with no 'f' or quotes) that's in *str and ends
4934 at end, parse it into an expr_ty. Return NULL on error. Adjust
4935 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004936static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004937fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004938 struct compiling *c, const node *n)
4939{
4940 FstringParser state;
4941
4942 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004943 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944 c, n) < 0) {
4945 FstringParser_Dealloc(&state);
4946 return NULL;
4947 }
4948
4949 return FstringParser_Finish(&state, c, n);
4950}
4951
4952/* n is a Python string literal, including the bracketing quote
4953 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04004954 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04004955 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04004956 *fstr and *fstrlen to the unparsed string object. Return 0 if no
4957 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004958*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04004959static int
4960parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
4961 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004962{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004963 size_t len;
4964 const char *s = STR(n);
4965 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004966 int fmode = 0;
4967 *bytesmode = 0;
4968 *rawmode = 0;
4969 *result = NULL;
4970 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004971 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004972 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004973 if (quote == 'b' || quote == 'B') {
4974 quote = *++s;
4975 *bytesmode = 1;
4976 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004977 else if (quote == 'u' || quote == 'U') {
4978 quote = *++s;
4979 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004980 else if (quote == 'r' || quote == 'R') {
4981 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004982 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004983 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 else if (quote == 'f' || quote == 'F') {
4985 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004986 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004988 else {
4989 break;
4990 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004991 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004992 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004993 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004994 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004995 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004997 if (quote != '\'' && quote != '\"') {
4998 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004999 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005000 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005001 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005002 s++;
5003 len = strlen(s);
5004 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005005 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005006 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005007 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005008 }
5009 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005010 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005011 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005012 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005013 }
5014 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005015 /* A triple quoted string. We've already skipped one quote at
5016 the start and one at the end of the string. Now skip the
5017 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005018 s += 2;
5019 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005020 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005021 if (s[--len] != quote || s[--len] != quote) {
5022 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005023 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005024 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005025 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005026
Eric V. Smith451d0e32016-09-09 21:56:20 -04005027 if (fmode) {
5028 /* Just return the bytes. The caller will parse the resulting
5029 string. */
5030 *fstr = s;
5031 *fstrlen = len;
5032 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005033 }
5034
Eric V. Smith451d0e32016-09-09 21:56:20 -04005035 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005036 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005037 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005038 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005039 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005040 const char *ch;
5041 for (ch = s; *ch; ch++) {
5042 if (Py_CHARMASK(*ch) >= 0x80) {
5043 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005044 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005045 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005046 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005047 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005048 if (*rawmode)
5049 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005050 else
Eric V. Smith451d0e32016-09-09 21:56:20 -04005051 *result = PyBytes_DecodeEscape(s, len, NULL, /* ignored */ 0, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005052 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005053 if (*rawmode)
5054 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005055 else
Eric V. Smith451d0e32016-09-09 21:56:20 -04005056 *result = decode_unicode_with_escapes(c, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005057 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005058 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005059}
5060
Eric V. Smith235a6f02015-09-19 14:51:32 -04005061/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5062 each STRING atom, and process it as needed. For bytes, just
5063 concatenate them together, and the result will be a Bytes node. For
5064 normal strings and f-strings, concatenate them together. The result
5065 will be a Str node if there were no f-strings; a FormattedValue
5066 node if there's just an f-string (with no leading or trailing
5067 literals), or a JoinedStr node if there are multiple f-strings or
5068 any literals involved. */
5069static expr_ty
5070parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005071{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005072 int bytesmode = 0;
5073 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005074 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005075
5076 FstringParser state;
5077 FstringParser_Init(&state);
5078
5079 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005080 int this_bytesmode;
5081 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005082 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005083 const char *fstr;
5084 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005085
5086 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005087 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5088 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005089 goto error;
5090
5091 /* Check that we're not mixing bytes with unicode. */
5092 if (i != 0 && bytesmode != this_bytesmode) {
5093 ast_error(c, n, "cannot mix bytes and nonbytes literals");
5094 Py_DECREF(s);
5095 goto error;
5096 }
5097 bytesmode = this_bytesmode;
5098
Eric V. Smith451d0e32016-09-09 21:56:20 -04005099 if (fstr != NULL) {
5100 int result;
5101 assert(s == NULL && !bytesmode);
5102 /* This is an f-string. Parse and concatenate it. */
5103 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5104 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005105 if (result < 0)
5106 goto error;
5107 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005108 assert(bytesmode ? PyBytes_CheckExact(s) :
5109 PyUnicode_CheckExact(s));
5110
5111 /* A string or byte string. */
5112 assert(s != NULL && fstr == NULL);
5113 if (bytesmode) {
5114 /* For bytes, concat as we go. */
5115 if (i == 0) {
5116 /* First time, just remember this value. */
5117 bytes_str = s;
5118 } else {
5119 PyBytes_ConcatAndDel(&bytes_str, s);
5120 if (!bytes_str)
5121 goto error;
5122 }
5123 } else {
5124 assert(s != NULL && fstr == NULL);
5125 /* This is a regular string. Concatenate it. */
5126 if (FstringParser_ConcatAndDel(&state, s) < 0)
5127 goto error;
5128 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005129 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005130 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005131 if (bytesmode) {
5132 /* Just return the bytes object and we're done. */
5133 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5134 goto error;
5135 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5136 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005137
Eric V. Smith235a6f02015-09-19 14:51:32 -04005138 /* We're not a bytes string, bytes_str should never have been set. */
5139 assert(bytes_str == NULL);
5140
5141 return FstringParser_Finish(&state, c, n);
5142
5143error:
5144 Py_XDECREF(bytes_str);
5145 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005146 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005147}