blob: 5c5738f6ed5e06854b9555ebaccaff8926a82678 [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));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200937 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
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 }
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +0200941 if (_PyUnicode_EqualToASCIIString(name, "async") ||
942 _PyUnicode_EqualToASCIIString(name, "await"))
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400943 {
944 PyObject *message = PyUnicode_FromString(
945 "'async' and 'await' will become reserved keywords"
946 " in Python 3.7");
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500947 int ret;
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400948 if (message == NULL) {
949 return 1;
950 }
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500951 ret = PyErr_WarnExplicitObject(
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400952 PyExc_DeprecationWarning,
953 message,
954 c->c_filename,
955 LINENO(n),
956 NULL,
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500957 NULL);
958 Py_DECREF(message);
959 if (ret < 0) {
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400960 return 1;
961 }
962 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000963 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200964 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000965 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200966 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400967 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000968 return 1;
969 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000970 }
971 }
972 return 0;
973}
974
Jeremy Hyltona8293132006-02-28 17:58:27 +0000975/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976
977 Only sets context for expr kinds that "can appear in assignment context"
978 (according to ../Parser/Python.asdl). For other expr kinds, it sets
979 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980*/
981
982static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000983set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984{
985 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000986 /* If a particular expression type can't be used for assign / delete,
987 set expr_name to its name and an error message will be generated.
988 */
989 const char* expr_name = NULL;
990
991 /* The ast defines augmented store and load contexts, but the
992 implementation here doesn't actually use them. The code may be
993 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000994 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000995 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000996 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000997 */
998 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999
1000 switch (e->kind) {
1001 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001002 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001003 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001004 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001005 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001007 e->v.Subscript.ctx = ctx;
1008 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001009 case Starred_kind:
1010 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001011 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001012 return 0;
1013 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001015 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001016 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001017 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001018 }
1019 e->v.Name.ctx = ctx;
1020 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001022 e->v.List.ctx = ctx;
1023 s = e->v.List.elts;
1024 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001026 e->v.Tuple.ctx = ctx;
1027 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001028 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001029 case Lambda_kind:
1030 expr_name = "lambda";
1031 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001033 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001034 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001035 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001037 case UnaryOp_kind:
1038 expr_name = "operator";
1039 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001041 expr_name = "generator expression";
1042 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001043 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001044 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001045 expr_name = "yield expression";
1046 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001047 case Await_kind:
1048 expr_name = "await expression";
1049 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001050 case ListComp_kind:
1051 expr_name = "list comprehension";
1052 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001053 case SetComp_kind:
1054 expr_name = "set comprehension";
1055 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001056 case DictComp_kind:
1057 expr_name = "dict comprehension";
1058 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001059 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001060 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 case Num_kind:
1062 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001063 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001064 case JoinedStr_kind:
1065 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001066 expr_name = "literal";
1067 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001068 case NameConstant_kind:
1069 expr_name = "keyword";
1070 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001071 case Ellipsis_kind:
1072 expr_name = "Ellipsis";
1073 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001074 case Compare_kind:
1075 expr_name = "comparison";
1076 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001077 case IfExp_kind:
1078 expr_name = "conditional expression";
1079 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001080 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 PyErr_Format(PyExc_SystemError,
1082 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001083 e->kind, e->lineno);
1084 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001086 /* Check for error string set by switch */
1087 if (expr_name) {
1088 char buf[300];
1089 PyOS_snprintf(buf, sizeof(buf),
1090 "can't %s %s",
1091 ctx == Store ? "assign to" : "delete",
1092 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001093 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001094 }
1095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 */
1099 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001100 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101
Thomas Wouters89f507f2006-12-13 04:49:30 +00001102 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001103 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001104 return 0;
1105 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 }
1107 return 1;
1108}
1109
1110static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001111ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112{
1113 REQ(n, augassign);
1114 n = CHILD(n, 0);
1115 switch (STR(n)[0]) {
1116 case '+':
1117 return Add;
1118 case '-':
1119 return Sub;
1120 case '/':
1121 if (STR(n)[1] == '/')
1122 return FloorDiv;
1123 else
1124 return Div;
1125 case '%':
1126 return Mod;
1127 case '<':
1128 return LShift;
1129 case '>':
1130 return RShift;
1131 case '&':
1132 return BitAnd;
1133 case '^':
1134 return BitXor;
1135 case '|':
1136 return BitOr;
1137 case '*':
1138 if (STR(n)[1] == '*')
1139 return Pow;
1140 else
1141 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001142 case '@':
1143 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001145 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 }
1148}
1149
1150static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001151ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001153 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 |'is' 'not'
1155 */
1156 REQ(n, comp_op);
1157 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001158 n = CHILD(n, 0);
1159 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 case LESS:
1161 return Lt;
1162 case GREATER:
1163 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001164 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 return Eq;
1166 case LESSEQUAL:
1167 return LtE;
1168 case GREATEREQUAL:
1169 return GtE;
1170 case NOTEQUAL:
1171 return NotEq;
1172 case NAME:
1173 if (strcmp(STR(n), "in") == 0)
1174 return In;
1175 if (strcmp(STR(n), "is") == 0)
1176 return Is;
1177 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001178 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001180 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001181 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 }
1183 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001184 /* handle "not in" and "is not" */
1185 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 case NAME:
1187 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1188 return NotIn;
1189 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1190 return IsNot;
1191 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001192 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001194 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 }
Neal Norwitz79792652005-11-14 04:25:03 +00001197 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001199 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200}
1201
1202static asdl_seq *
1203seq_for_testlist(struct compiling *c, const node *n)
1204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001206 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1207 */
Armin Rigo31441302005-10-21 12:57:31 +00001208 asdl_seq *seq;
1209 expr_ty expression;
1210 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001211 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001213 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 if (!seq)
1215 return NULL;
1216
1217 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001219 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220
Benjamin Peterson4905e802009-09-27 02:43:28 +00001221 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001222 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224
1225 assert(i / 2 < seq->size);
1226 asdl_seq_SET(seq, i / 2, expression);
1227 }
1228 return seq;
1229}
1230
Neal Norwitzc1505362006-12-28 06:47:50 +00001231static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001232ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001233{
1234 identifier name;
1235 expr_ty annotation = NULL;
1236 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001237 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001238
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001239 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001240 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001241 name = NEW_IDENTIFIER(ch);
1242 if (!name)
1243 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001244 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001245 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001246
1247 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1248 annotation = ast_for_expr(c, CHILD(n, 2));
1249 if (!annotation)
1250 return NULL;
1251 }
1252
Victor Stinnerc106c682015-11-06 17:01:48 +01001253 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001254 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001255 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001256 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257}
1258
Guido van Rossum4f72a782006-10-27 23:31:49 +00001259/* returns -1 if failed to handle keyword only arguments
1260 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001261 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001262 ^^^
1263 start pointing here
1264 */
1265static int
1266handle_keywordonly_args(struct compiling *c, const node *n, int start,
1267 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1268{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001269 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001270 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001271 expr_ty expression, annotation;
1272 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001273 int i = start;
1274 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001275
1276 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001277 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001278 return -1;
1279 }
1280 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001281 while (i < NCH(n)) {
1282 ch = CHILD(n, i);
1283 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001284 case vfpdef:
1285 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001287 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001288 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001289 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001290 asdl_seq_SET(kwdefaults, j, expression);
1291 i += 2; /* '=' and test */
1292 }
1293 else { /* setting NULL if no default value exists */
1294 asdl_seq_SET(kwdefaults, j, NULL);
1295 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001296 if (NCH(ch) == 3) {
1297 /* ch is NAME ':' test */
1298 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001299 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001300 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001301 }
1302 else {
1303 annotation = NULL;
1304 }
1305 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001306 argname = NEW_IDENTIFIER(ch);
1307 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001308 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001309 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001310 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001311 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1312 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001313 if (!arg)
1314 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001315 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001316 i += 2; /* the name and the comma */
1317 break;
1318 case DOUBLESTAR:
1319 return i;
1320 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001321 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001322 goto error;
1323 }
1324 }
1325 return i;
1326 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001328}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329
Jeremy Hyltona8293132006-02-28 17:58:27 +00001330/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331
1332static arguments_ty
1333ast_for_arguments(struct compiling *c, const node *n)
1334{
Neal Norwitzc1505362006-12-28 06:47:50 +00001335 /* This function handles both typedargslist (function definition)
1336 and varargslist (lambda definition).
1337
1338 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001339 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1340 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1341 | '**' tfpdef [',']]]
1342 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1343 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001344 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001345 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1346 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1347 | '**' vfpdef [',']]]
1348 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1349 | '**' vfpdef [',']
1350 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001351 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001352
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001354 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1355 int nposdefaults = 0, found_default = 0;
1356 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001357 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001358 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 node *ch;
1360
1361 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001363 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001366 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367
Jeremy Hyltone921e022008-07-17 16:37:17 +00001368 /* First count the number of positional args & defaults. The
1369 variable i is the loop index for this for loop and the next.
1370 The next loop picks up where the first leaves off.
1371 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 ch = CHILD(n, i);
1374 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001375 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001376 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001377 if (i < NCH(n) && /* skip argument following star */
1378 (TYPE(CHILD(n, i)) == tfpdef ||
1379 TYPE(CHILD(n, i)) == vfpdef)) {
1380 i++;
1381 }
1382 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001384 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001385 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001386 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 defaults for keyword only args */
1390 for ( ; i < NCH(n); ++i) {
1391 ch = CHILD(n, i);
1392 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001393 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001395 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001397 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001399 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001400 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001401 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001403 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001405 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 since we set NULL as default for keyword only argument w/o default
1408 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001409 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001410 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001412 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001414 /* tfpdef: NAME [':' test]
1415 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 */
1417 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001418 j = 0; /* index for defaults */
1419 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 ch = CHILD(n, i);
1422 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001423 case tfpdef:
1424 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1426 anything other than EQUAL or a comma? */
1427 /* XXX Should NCH(n) check be made a separate check? */
1428 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001429 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1430 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001431 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001432 assert(posdefaults != NULL);
1433 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001435 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001437 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001438 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001439 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001440 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001442 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001443 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001444 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001445 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 i += 2; /* the name and the comma */
1447 break;
1448 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001449 if (i+1 >= NCH(n) ||
1450 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001451 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001452 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001453 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001454 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001455 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001456 if (TYPE(ch) == COMMA) {
1457 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001458 i += 2; /* now follows keyword only arguments */
1459 res = handle_keywordonly_args(c, n, i,
1460 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001461 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001462 i = res; /* res has new position to process */
1463 }
1464 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001465 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001466 if (!vararg)
1467 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001468
Guido van Rossum4f72a782006-10-27 23:31:49 +00001469 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001470 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1471 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001472 int res = 0;
1473 res = handle_keywordonly_args(c, n, i,
1474 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001475 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001476 i = res; /* res has new position to process */
1477 }
1478 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 break;
1480 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001481 ch = CHILD(n, i+1); /* tfpdef */
1482 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001483 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001484 if (!kwarg)
1485 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 i += 3;
1487 break;
1488 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001489 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 "unexpected node in varargslist: %d @ %d",
1491 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001492 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001495 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496}
1497
1498static expr_ty
1499ast_for_dotted_name(struct compiling *c, const node *n)
1500{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001501 expr_ty e;
1502 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001503 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 int i;
1505
1506 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001507
1508 lineno = LINENO(n);
1509 col_offset = n->n_col_offset;
1510
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 id = NEW_IDENTIFIER(CHILD(n, 0));
1512 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001513 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001514 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517
1518 for (i = 2; i < NCH(n); i+=2) {
1519 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001520 if (!id)
1521 return NULL;
1522 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1523 if (!e)
1524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 }
1526
1527 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528}
1529
1530static expr_ty
1531ast_for_decorator(struct compiling *c, const node *n)
1532{
1533 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1534 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001535 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001538 REQ(CHILD(n, 0), AT);
1539 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1542 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001543 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001546 d = name_expr;
1547 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 }
1549 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001550 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001551 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001552 if (!d)
1553 return NULL;
1554 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 }
1556 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001557 d = ast_for_call(c, CHILD(n, 3), name_expr);
1558 if (!d)
1559 return NULL;
1560 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 }
1562
1563 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
1566static asdl_seq*
1567ast_for_decorators(struct compiling *c, const node *n)
1568{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001569 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001570 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001574 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 if (!decorator_seq)
1576 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001579 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001580 if (!d)
1581 return NULL;
1582 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 }
1584 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585}
1586
1587static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001588ast_for_funcdef_impl(struct compiling *c, const node *n,
1589 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001591 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001592 identifier name;
1593 arguments_ty args;
1594 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001595 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001596 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597
1598 REQ(n, funcdef);
1599
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 name = NEW_IDENTIFIER(CHILD(n, name_i));
1601 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001602 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001603 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001604 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1606 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001607 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001608 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1609 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1610 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001611 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001612 name_i += 2;
1613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614 body = ast_for_suite(c, CHILD(n, name_i + 3));
1615 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617
Yury Selivanov75445082015-05-11 22:57:16 -04001618 if (is_async)
1619 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1620 LINENO(n),
1621 n->n_col_offset, c->c_arena);
1622 else
1623 return FunctionDef(name, args, body, decorator_seq, returns,
1624 LINENO(n),
1625 n->n_col_offset, c->c_arena);
1626}
1627
1628static stmt_ty
1629ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1630{
1631 /* async_funcdef: ASYNC funcdef */
1632 REQ(n, async_funcdef);
1633 REQ(CHILD(n, 0), ASYNC);
1634 REQ(CHILD(n, 1), funcdef);
1635
1636 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1637 1 /* is_async */);
1638}
1639
1640static stmt_ty
1641ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1642{
1643 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1644 return ast_for_funcdef_impl(c, n, decorator_seq,
1645 0 /* is_async */);
1646}
1647
1648
1649static stmt_ty
1650ast_for_async_stmt(struct compiling *c, const node *n)
1651{
1652 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1653 REQ(n, async_stmt);
1654 REQ(CHILD(n, 0), ASYNC);
1655
1656 switch (TYPE(CHILD(n, 1))) {
1657 case funcdef:
1658 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1659 1 /* is_async */);
1660 case with_stmt:
1661 return ast_for_with_stmt(c, CHILD(n, 1),
1662 1 /* is_async */);
1663
1664 case for_stmt:
1665 return ast_for_for_stmt(c, CHILD(n, 1),
1666 1 /* is_async */);
1667
1668 default:
1669 PyErr_Format(PyExc_SystemError,
1670 "invalid async stament: %s",
1671 STR(CHILD(n, 1)));
1672 return NULL;
1673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674}
1675
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001676static stmt_ty
1677ast_for_decorated(struct compiling *c, const node *n)
1678{
Yury Selivanov75445082015-05-11 22:57:16 -04001679 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001680 stmt_ty thing = NULL;
1681 asdl_seq *decorator_seq = NULL;
1682
1683 REQ(n, decorated);
1684
1685 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1686 if (!decorator_seq)
1687 return NULL;
1688
1689 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001690 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001692
1693 if (TYPE(CHILD(n, 1)) == funcdef) {
1694 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1695 } else if (TYPE(CHILD(n, 1)) == classdef) {
1696 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001697 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1698 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001699 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001700 /* we count the decorators in when talking about the class' or
1701 * function's line number */
1702 if (thing) {
1703 thing->lineno = LINENO(n);
1704 thing->col_offset = n->n_col_offset;
1705 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001706 return thing;
1707}
1708
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709static expr_ty
1710ast_for_lambdef(struct compiling *c, const node *n)
1711{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001712 /* lambdef: 'lambda' [varargslist] ':' test
1713 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 arguments_ty args;
1715 expr_ty expression;
1716
1717 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001718 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 if (!args)
1720 return NULL;
1721 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001722 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 }
1725 else {
1726 args = ast_for_arguments(c, CHILD(n, 1));
1727 if (!args)
1728 return NULL;
1729 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001730 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 }
1733
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001734 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735}
1736
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001737static expr_ty
1738ast_for_ifexpr(struct compiling *c, const node *n)
1739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001741 expr_ty expression, body, orelse;
1742
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001743 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001744 body = ast_for_expr(c, CHILD(n, 0));
1745 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001746 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001747 expression = ast_for_expr(c, CHILD(n, 2));
1748 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001749 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001750 orelse = ast_for_expr(c, CHILD(n, 4));
1751 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001752 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001753 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1754 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001755}
1756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001758 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759
Nick Coghlan650f0d02007-04-15 12:05:43 +00001760 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761*/
1762
1763static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001764count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001766 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001767 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768
Guido van Rossumd8faa362007-04-27 19:54:29 +00001769 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001770 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001771 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001772 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001773 if (TYPE(CHILD(n, 0)) == ASYNC) {
1774 is_async = 1;
1775 }
1776 if (NCH(n) == (5 + is_async)) {
1777 n = CHILD(n, 4 + is_async);
1778 }
1779 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001781 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001782 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001783 REQ(n, comp_iter);
1784 n = CHILD(n, 0);
1785 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001786 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001787 else if (TYPE(n) == comp_if) {
1788 if (NCH(n) == 3) {
1789 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001791 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001792 else
1793 return n_fors;
1794 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001795
Guido van Rossumd8faa362007-04-27 19:54:29 +00001796 /* Should never be reached */
1797 PyErr_SetString(PyExc_SystemError,
1798 "logic error in count_comp_fors");
1799 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800}
1801
Nick Coghlan650f0d02007-04-15 12:05:43 +00001802/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803
Nick Coghlan650f0d02007-04-15 12:05:43 +00001804 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805*/
1806
1807static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001808count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001810 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811
Guido van Rossumd8faa362007-04-27 19:54:29 +00001812 while (1) {
1813 REQ(n, comp_iter);
1814 if (TYPE(CHILD(n, 0)) == comp_for)
1815 return n_ifs;
1816 n = CHILD(n, 0);
1817 REQ(n, comp_if);
1818 n_ifs++;
1819 if (NCH(n) == 2)
1820 return n_ifs;
1821 n = CHILD(n, 2);
1822 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823}
1824
Guido van Rossum992d4a32007-07-11 13:09:30 +00001825static asdl_seq *
1826ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001829 asdl_seq *comps;
1830
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001831 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 if (n_fors == -1)
1833 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001834
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001835 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001836 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001838
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001840 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001842 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001843 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001844 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845
Guido van Rossum992d4a32007-07-11 13:09:30 +00001846 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001848 if (TYPE(CHILD(n, 0)) == ASYNC) {
1849 is_async = 1;
1850 }
1851
1852 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001853 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001854 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001856 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001857 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001859
Thomas Wouters89f507f2006-12-13 04:49:30 +00001860 /* Check the # of children rather than the length of t, since
1861 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001862 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001863 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001864 comp = comprehension(first, expression, NULL,
1865 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001867 comp = comprehension(Tuple(t, Store, first->lineno,
1868 first->col_offset, c->c_arena),
1869 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001870 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001873 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 int j, n_ifs;
1875 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001877 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001878 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001879 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001881
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001882 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001883 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001885
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001887 REQ(n, comp_iter);
1888 n = CHILD(n, 0);
1889 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890
Guido van Rossum992d4a32007-07-11 13:09:30 +00001891 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001893 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001894 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001895 if (NCH(n) == 3)
1896 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001898 /* on exit, must guarantee that n is a comp_for */
1899 if (TYPE(n) == comp_iter)
1900 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001901 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001903 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001905 return comps;
1906}
1907
1908static expr_ty
1909ast_for_itercomp(struct compiling *c, const node *n, int type)
1910{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001911 /* testlist_comp: (test|star_expr)
1912 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001913 expr_ty elt;
1914 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001915 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916
Guido van Rossum992d4a32007-07-11 13:09:30 +00001917 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001919 ch = CHILD(n, 0);
1920 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001921 if (!elt)
1922 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001923 if (elt->kind == Starred_kind) {
1924 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1925 return NULL;
1926 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927
Guido van Rossum992d4a32007-07-11 13:09:30 +00001928 comps = ast_for_comprehension(c, CHILD(n, 1));
1929 if (!comps)
1930 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001931
1932 if (type == COMP_GENEXP)
1933 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1934 else if (type == COMP_LISTCOMP)
1935 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1936 else if (type == COMP_SETCOMP)
1937 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1938 else
1939 /* Should never happen */
1940 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941}
1942
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001943/* Fills in the key, value pair corresponding to the dict element. In case
1944 * of an unpacking, key is NULL. *i is advanced by the number of ast
1945 * elements. Iff successful, nonzero is returned.
1946 */
1947static int
1948ast_for_dictelement(struct compiling *c, const node *n, int *i,
1949 expr_ty *key, expr_ty *value)
1950{
1951 expr_ty expression;
1952 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1953 assert(NCH(n) - *i >= 2);
1954
1955 expression = ast_for_expr(c, CHILD(n, *i + 1));
1956 if (!expression)
1957 return 0;
1958 *key = NULL;
1959 *value = expression;
1960
1961 *i += 2;
1962 }
1963 else {
1964 assert(NCH(n) - *i >= 3);
1965
1966 expression = ast_for_expr(c, CHILD(n, *i));
1967 if (!expression)
1968 return 0;
1969 *key = expression;
1970
1971 REQ(CHILD(n, *i + 1), COLON);
1972
1973 expression = ast_for_expr(c, CHILD(n, *i + 2));
1974 if (!expression)
1975 return 0;
1976 *value = expression;
1977
1978 *i += 3;
1979 }
1980 return 1;
1981}
1982
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001984ast_for_dictcomp(struct compiling *c, const node *n)
1985{
1986 expr_ty key, value;
1987 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001988 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001990 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001991 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001992 assert(key);
1993 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001995 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001996 if (!comps)
1997 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998
Guido van Rossum992d4a32007-07-11 13:09:30 +00001999 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2000}
2001
2002static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002003ast_for_dictdisplay(struct compiling *c, const node *n)
2004{
2005 int i;
2006 int j;
2007 int size;
2008 asdl_seq *keys, *values;
2009
2010 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2011 keys = _Py_asdl_seq_new(size, c->c_arena);
2012 if (!keys)
2013 return NULL;
2014
2015 values = _Py_asdl_seq_new(size, c->c_arena);
2016 if (!values)
2017 return NULL;
2018
2019 j = 0;
2020 for (i = 0; i < NCH(n); i++) {
2021 expr_ty key, value;
2022
2023 if (!ast_for_dictelement(c, n, &i, &key, &value))
2024 return NULL;
2025 asdl_seq_SET(keys, j, key);
2026 asdl_seq_SET(values, j, value);
2027
2028 j++;
2029 }
2030 keys->size = j;
2031 values->size = j;
2032 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2033}
2034
2035static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002036ast_for_genexp(struct compiling *c, const node *n)
2037{
2038 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002039 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002040}
2041
2042static expr_ty
2043ast_for_listcomp(struct compiling *c, const node *n)
2044{
2045 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002046 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002047}
2048
2049static expr_ty
2050ast_for_setcomp(struct compiling *c, const node *n)
2051{
2052 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002053 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002054}
2055
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002056static expr_ty
2057ast_for_setdisplay(struct compiling *c, const node *n)
2058{
2059 int i;
2060 int size;
2061 asdl_seq *elts;
2062
2063 assert(TYPE(n) == (dictorsetmaker));
2064 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2065 elts = _Py_asdl_seq_new(size, c->c_arena);
2066 if (!elts)
2067 return NULL;
2068 for (i = 0; i < NCH(n); i += 2) {
2069 expr_ty expression;
2070 expression = ast_for_expr(c, CHILD(n, i));
2071 if (!expression)
2072 return NULL;
2073 asdl_seq_SET(elts, i / 2, expression);
2074 }
2075 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2076}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002077
2078static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079ast_for_atom(struct compiling *c, const node *n)
2080{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002081 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2082 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002083 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 */
2085 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002088 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002089 PyObject *name;
2090 const char *s = STR(ch);
2091 size_t len = strlen(s);
2092 if (len >= 4 && len <= 5) {
2093 if (!strcmp(s, "None"))
2094 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2095 if (!strcmp(s, "True"))
2096 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2097 if (!strcmp(s, "False"))
2098 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2099 }
2100 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002101 if (!name)
2102 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002103 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002104 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2105 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002107 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002108 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002109 const char *errtype = NULL;
2110 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2111 errtype = "unicode error";
2112 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2113 errtype = "value error";
2114 if (errtype) {
2115 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002116 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002117 PyObject *type, *value, *tback, *errstr;
2118 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002119 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002120 if (errstr)
2121 s = PyUnicode_AsUTF8(errstr);
2122 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002123 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002124 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002125 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002126 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002127 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002128 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002129 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002130 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002131 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002132 Py_XDECREF(tback);
2133 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002134 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002135 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002136 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 }
2138 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002139 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002140 if (!pynum)
2141 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002142
Victor Stinner43d81952013-07-17 00:57:58 +02002143 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2144 Py_DECREF(pynum);
2145 return NULL;
2146 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002147 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 }
Georg Brandldde00282007-03-18 19:01:53 +00002149 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002150 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002152 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153
Thomas Wouters89f507f2006-12-13 04:49:30 +00002154 if (TYPE(ch) == RPAR)
2155 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156
Thomas Wouters89f507f2006-12-13 04:49:30 +00002157 if (TYPE(ch) == yield_expr)
2158 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002161 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002162 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002163
Nick Coghlan650f0d02007-04-15 12:05:43 +00002164 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002166 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167
Thomas Wouters89f507f2006-12-13 04:49:30 +00002168 if (TYPE(ch) == RSQB)
2169 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170
Nick Coghlan650f0d02007-04-15 12:05:43 +00002171 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002172 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2173 asdl_seq *elts = seq_for_testlist(c, ch);
2174 if (!elts)
2175 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002176
Thomas Wouters89f507f2006-12-13 04:49:30 +00002177 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2178 }
2179 else
2180 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002182 /* dictorsetmaker: ( ((test ':' test | '**' test)
2183 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2184 * ((test | '*' test)
2185 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002186 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002187 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002188 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002189 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002190 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191 }
2192 else {
2193 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2194 if (NCH(ch) == 1 ||
2195 (NCH(ch) > 1 &&
2196 TYPE(CHILD(ch, 1)) == COMMA)) {
2197 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002198 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002199 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 else if (NCH(ch) > 1 &&
2201 TYPE(CHILD(ch, 1)) == comp_for) {
2202 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002203 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002204 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205 else if (NCH(ch) > 3 - is_dict &&
2206 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2207 /* It's a dictionary comprehension. */
2208 if (is_dict) {
2209 ast_error(c, n, "dict unpacking cannot be used in "
2210 "dict comprehension");
2211 return NULL;
2212 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002213 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214 }
2215 else {
2216 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002217 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002218 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002219 if (res) {
2220 res->lineno = LINENO(n);
2221 res->col_offset = n->n_col_offset;
2222 }
2223 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002224 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002227 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2228 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 }
2230}
2231
2232static slice_ty
2233ast_for_slice(struct compiling *c, const node *n)
2234{
2235 node *ch;
2236 expr_ty lower = NULL, upper = NULL, step = NULL;
2237
2238 REQ(n, subscript);
2239
2240 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002241 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 sliceop: ':' [test]
2243 */
2244 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 if (NCH(n) == 1 && TYPE(ch) == test) {
2246 /* 'step' variable hold no significance in terms of being used over
2247 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 if (!step)
2250 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251
Thomas Wouters89f507f2006-12-13 04:49:30 +00002252 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 }
2254
2255 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002256 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 if (!lower)
2258 return NULL;
2259 }
2260
2261 /* If there's an upper bound it's in the second or third position. */
2262 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002263 if (NCH(n) > 1) {
2264 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265
Thomas Wouters89f507f2006-12-13 04:49:30 +00002266 if (TYPE(n2) == test) {
2267 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 if (!upper)
2269 return NULL;
2270 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002271 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002273 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274
Thomas Wouters89f507f2006-12-13 04:49:30 +00002275 if (TYPE(n2) == test) {
2276 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 if (!upper)
2278 return NULL;
2279 }
2280 }
2281
2282 ch = CHILD(n, NCH(n) - 1);
2283 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002284 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002285 ch = CHILD(ch, 1);
2286 if (TYPE(ch) == test) {
2287 step = ast_for_expr(c, ch);
2288 if (!step)
2289 return NULL;
2290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 }
2292 }
2293
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002294 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295}
2296
2297static expr_ty
2298ast_for_binop(struct compiling *c, const node *n)
2299{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002300 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002302 BinOp(BinOp(A, op, B), op, C).
2303 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304
Guido van Rossumd8faa362007-04-27 19:54:29 +00002305 int i, nops;
2306 expr_ty expr1, expr2, result;
2307 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308
Guido van Rossumd8faa362007-04-27 19:54:29 +00002309 expr1 = ast_for_expr(c, CHILD(n, 0));
2310 if (!expr1)
2311 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312
Guido van Rossumd8faa362007-04-27 19:54:29 +00002313 expr2 = ast_for_expr(c, CHILD(n, 2));
2314 if (!expr2)
2315 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316
Guido van Rossumd8faa362007-04-27 19:54:29 +00002317 newoperator = get_operator(CHILD(n, 1));
2318 if (!newoperator)
2319 return NULL;
2320
2321 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2322 c->c_arena);
2323 if (!result)
2324 return NULL;
2325
2326 nops = (NCH(n) - 1) / 2;
2327 for (i = 1; i < nops; i++) {
2328 expr_ty tmp_result, tmp;
2329 const node* next_oper = CHILD(n, i * 2 + 1);
2330
2331 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002332 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 return NULL;
2334
Guido van Rossumd8faa362007-04-27 19:54:29 +00002335 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2336 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337 return NULL;
2338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002340 LINENO(next_oper), next_oper->n_col_offset,
2341 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002343 return NULL;
2344 result = tmp_result;
2345 }
2346 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347}
2348
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002349static expr_ty
2350ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002353 subscriptlist: subscript (',' subscript)* [',']
2354 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2355 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002356 REQ(n, trailer);
2357 if (TYPE(CHILD(n, 0)) == LPAR) {
2358 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002359 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002360 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002361 else
2362 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002363 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002364 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002365 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2366 if (!attr_id)
2367 return NULL;
2368 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002369 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002370 }
2371 else {
2372 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002373 REQ(CHILD(n, 2), RSQB);
2374 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002375 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002376 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2377 if (!slc)
2378 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002379 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2380 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002381 }
2382 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002384 by treating the sequence as a tuple literal if there are
2385 no slice features.
2386 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002387 int j;
2388 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002389 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002390 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002391 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002392 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002393 if (!slices)
2394 return NULL;
2395 for (j = 0; j < NCH(n); j += 2) {
2396 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002397 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002398 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002399 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002400 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002401 asdl_seq_SET(slices, j / 2, slc);
2402 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002403 if (!simple) {
2404 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002405 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002406 }
2407 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002408 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002409 if (!elts)
2410 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002411 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2412 slc = (slice_ty)asdl_seq_GET(slices, j);
2413 assert(slc->kind == Index_kind && slc->v.Index.value);
2414 asdl_seq_SET(elts, j, slc->v.Index.value);
2415 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002416 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002417 if (!e)
2418 return NULL;
2419 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002420 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002421 }
2422 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002423}
2424
2425static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002426ast_for_factor(struct compiling *c, const node *n)
2427{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002428 expr_ty expression;
2429
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002430 expression = ast_for_expr(c, CHILD(n, 1));
2431 if (!expression)
2432 return NULL;
2433
2434 switch (TYPE(CHILD(n, 0))) {
2435 case PLUS:
2436 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2437 c->c_arena);
2438 case MINUS:
2439 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2440 c->c_arena);
2441 case TILDE:
2442 return UnaryOp(Invert, expression, LINENO(n),
2443 n->n_col_offset, c->c_arena);
2444 }
2445 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2446 TYPE(CHILD(n, 0)));
2447 return NULL;
2448}
2449
2450static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002451ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002452{
Yury Selivanov75445082015-05-11 22:57:16 -04002453 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002454 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002455
2456 REQ(n, atom_expr);
2457 nch = NCH(n);
2458
2459 if (TYPE(CHILD(n, 0)) == AWAIT) {
2460 start = 1;
2461 assert(nch > 1);
2462 }
2463
2464 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002465 if (!e)
2466 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002467 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002468 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002469 if (start && nch == 2) {
2470 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2471 }
2472
2473 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002474 node *ch = CHILD(n, i);
2475 if (TYPE(ch) != trailer)
2476 break;
2477 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002478 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002479 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002480 tmp->lineno = e->lineno;
2481 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002482 e = tmp;
2483 }
Yury Selivanov75445082015-05-11 22:57:16 -04002484
2485 if (start) {
2486 /* there was an AWAIT */
2487 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2488 }
2489 else {
2490 return e;
2491 }
2492}
2493
2494static expr_ty
2495ast_for_power(struct compiling *c, const node *n)
2496{
2497 /* power: atom trailer* ('**' factor)*
2498 */
2499 expr_ty e;
2500 REQ(n, power);
2501 e = ast_for_atom_expr(c, CHILD(n, 0));
2502 if (!e)
2503 return NULL;
2504 if (NCH(n) == 1)
2505 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002506 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2507 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002508 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002509 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002510 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002511 }
2512 return e;
2513}
2514
Guido van Rossum0368b722007-05-11 16:50:42 +00002515static expr_ty
2516ast_for_starred(struct compiling *c, const node *n)
2517{
2518 expr_ty tmp;
2519 REQ(n, star_expr);
2520
2521 tmp = ast_for_expr(c, CHILD(n, 1));
2522 if (!tmp)
2523 return NULL;
2524
2525 /* The Load context is changed later. */
2526 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2527}
2528
2529
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530/* Do not name a variable 'expr'! Will cause a compile error.
2531*/
2532
2533static expr_ty
2534ast_for_expr(struct compiling *c, const node *n)
2535{
2536 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002537 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002538 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 and_test: not_test ('and' not_test)*
2541 not_test: 'not' not_test | comparison
2542 comparison: expr (comp_op expr)*
2543 expr: xor_expr ('|' xor_expr)*
2544 xor_expr: and_expr ('^' and_expr)*
2545 and_expr: shift_expr ('&' shift_expr)*
2546 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2547 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002548 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002550 power: atom_expr ['**' factor]
2551 atom_expr: [AWAIT] atom trailer*
2552 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 */
2554
2555 asdl_seq *seq;
2556 int i;
2557
2558 loop:
2559 switch (TYPE(n)) {
2560 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002561 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002562 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002563 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002565 else if (NCH(n) > 1)
2566 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002567 /* Fallthrough */
2568 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 case and_test:
2570 if (NCH(n) == 1) {
2571 n = CHILD(n, 0);
2572 goto loop;
2573 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002574 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 if (!seq)
2576 return NULL;
2577 for (i = 0; i < NCH(n); i += 2) {
2578 expr_ty e = ast_for_expr(c, CHILD(n, i));
2579 if (!e)
2580 return NULL;
2581 asdl_seq_SET(seq, i / 2, e);
2582 }
2583 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002584 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2585 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002586 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002587 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 case not_test:
2589 if (NCH(n) == 1) {
2590 n = CHILD(n, 0);
2591 goto loop;
2592 }
2593 else {
2594 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2595 if (!expression)
2596 return NULL;
2597
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002598 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2599 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 }
2601 case comparison:
2602 if (NCH(n) == 1) {
2603 n = CHILD(n, 0);
2604 goto loop;
2605 }
2606 else {
2607 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002608 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002609 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002610 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 if (!ops)
2612 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002613 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 return NULL;
2616 }
2617 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002618 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002620 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002621 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002623 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624
2625 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002626 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002628 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002630 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 asdl_seq_SET(cmps, i / 2, expression);
2632 }
2633 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002634 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002636 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002638 return Compare(expression, ops, cmps, LINENO(n),
2639 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 }
2641 break;
2642
Guido van Rossum0368b722007-05-11 16:50:42 +00002643 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 /* The next five cases all handle BinOps. The main body of code
2646 is the same in each case, but the switch turned inside out to
2647 reuse the code for each type of operator.
2648 */
2649 case expr:
2650 case xor_expr:
2651 case and_expr:
2652 case shift_expr:
2653 case arith_expr:
2654 case term:
2655 if (NCH(n) == 1) {
2656 n = CHILD(n, 0);
2657 goto loop;
2658 }
2659 return ast_for_binop(c, n);
2660 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002661 node *an = NULL;
2662 node *en = NULL;
2663 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002664 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002665 if (NCH(n) > 1)
2666 an = CHILD(n, 1); /* yield_arg */
2667 if (an) {
2668 en = CHILD(an, NCH(an) - 1);
2669 if (NCH(an) == 2) {
2670 is_from = 1;
2671 exp = ast_for_expr(c, en);
2672 }
2673 else
2674 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002675 if (!exp)
2676 return NULL;
2677 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002678 if (is_from)
2679 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2680 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002681 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002682 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683 if (NCH(n) == 1) {
2684 n = CHILD(n, 0);
2685 goto loop;
2686 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002687 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002688 case power:
2689 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002691 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 return NULL;
2693 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002694 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 return NULL;
2696}
2697
2698static expr_ty
2699ast_for_call(struct compiling *c, const node *n, expr_ty func)
2700{
2701 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002702 arglist: argument (',' argument)* [',']
2703 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 */
2705
2706 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002707 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002708 asdl_seq *args;
2709 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710
2711 REQ(n, arglist);
2712
2713 nargs = 0;
2714 nkeywords = 0;
2715 ngens = 0;
2716 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002717 node *ch = CHILD(n, i);
2718 if (TYPE(ch) == argument) {
2719 if (NCH(ch) == 1)
2720 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002721 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002722 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002723 else if (TYPE(CHILD(ch, 0)) == STAR)
2724 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002726 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 nkeywords++;
2728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 }
2730 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002731 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002732 "if not sole argument");
2733 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 }
2735
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002736 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002738 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002739 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002741 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002742
2743 nargs = 0; /* positional arguments + iterable argument unpackings */
2744 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2745 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002747 node *ch = CHILD(n, i);
2748 if (TYPE(ch) == argument) {
2749 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002750 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002751 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002752 /* a positional argument */
2753 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002754 if (ndoublestars) {
2755 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002756 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002757 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002758 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002759 else {
2760 ast_error(c, chch,
2761 "positional argument follows "
2762 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002764 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002765 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002766 e = ast_for_expr(c, chch);
2767 if (!e)
2768 return NULL;
2769 asdl_seq_SET(args, nargs++, e);
2770 }
2771 else if (TYPE(chch) == STAR) {
2772 /* an iterable argument unpacking */
2773 expr_ty starred;
2774 if (ndoublestars) {
2775 ast_error(c, chch,
2776 "iterable argument unpacking follows "
2777 "keyword argument unpacking");
2778 return NULL;
2779 }
2780 e = ast_for_expr(c, CHILD(ch, 1));
2781 if (!e)
2782 return NULL;
2783 starred = Starred(e, Load, LINENO(chch),
2784 chch->n_col_offset,
2785 c->c_arena);
2786 if (!starred)
2787 return NULL;
2788 asdl_seq_SET(args, nargs++, starred);
2789
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002790 }
2791 else if (TYPE(chch) == DOUBLESTAR) {
2792 /* a keyword argument unpacking */
2793 keyword_ty kw;
2794 i++;
2795 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002797 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002798 kw = keyword(NULL, e, c->c_arena);
2799 asdl_seq_SET(keywords, nkeywords++, kw);
2800 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002802 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002803 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002804 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002806 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002807 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002809 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002810 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002811 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002812 identifier key, tmp;
2813 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002815 /* chch is test, but must be an identifier? */
2816 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002818 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 /* f(lambda x: x[0] = 3) ends up getting parsed with
2820 * LHS test = lambda x: x[0], and RHS test = 3.
2821 * SF bug 132313 points out that complaining about a keyword
2822 * then is very confusing.
2823 */
2824 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002825 ast_error(c, chch,
2826 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002827 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002828 }
2829 else if (e->kind != Name_kind) {
2830 ast_error(c, chch,
2831 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002832 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002833 }
2834 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002835 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002837 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002838 for (k = 0; k < nkeywords; k++) {
2839 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002840 if (tmp && !PyUnicode_Compare(tmp, key)) {
2841 ast_error(c, chch,
2842 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002843 return NULL;
2844 }
2845 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002846 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002848 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002849 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002851 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002852 asdl_seq_SET(keywords, nkeywords++, kw);
2853 }
2854 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 }
2856
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002857 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858}
2859
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002861ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002863 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002864 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002866 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002867 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002868 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002869 }
2870 else {
2871 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002872 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002873 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002875 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 else {
2877 asdl_seq *tmp = seq_for_testlist(c, n);
2878 if (!tmp)
2879 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002880 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002882}
2883
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884static stmt_ty
2885ast_for_expr_stmt(struct compiling *c, const node *n)
2886{
2887 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002888 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2889 ('=' (yield_expr|testlist_star_expr))*)
2890 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002891 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002892 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002893 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002894 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 */
2896
2897 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002898 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 if (!e)
2900 return NULL;
2901
Thomas Wouters89f507f2006-12-13 04:49:30 +00002902 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 }
2904 else if (TYPE(CHILD(n, 1)) == augassign) {
2905 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002906 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002907 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 if (!expr1)
2911 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002912 if(!set_context(c, expr1, Store, ch))
2913 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002914 /* set_context checks that most expressions are not the left side.
2915 Augmented assignments can only have a name, a subscript, or an
2916 attribute on the left, though, so we have to explicitly check for
2917 those. */
2918 switch (expr1->kind) {
2919 case Name_kind:
2920 case Attribute_kind:
2921 case Subscript_kind:
2922 break;
2923 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002924 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002925 return NULL;
2926 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927
Thomas Wouters89f507f2006-12-13 04:49:30 +00002928 ch = CHILD(n, 2);
2929 if (TYPE(ch) == testlist)
2930 expr2 = ast_for_testlist(c, ch);
2931 else
2932 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002933 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934 return NULL;
2935
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002936 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002937 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 return NULL;
2939
Thomas Wouters89f507f2006-12-13 04:49:30 +00002940 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002942 else if (TYPE(CHILD(n, 1)) == annassign) {
2943 expr_ty expr1, expr2, expr3;
2944 node *ch = CHILD(n, 0);
2945 node *deep, *ann = CHILD(n, 1);
2946 int simple = 1;
2947
2948 /* we keep track of parens to qualify (x) as expression not name */
2949 deep = ch;
2950 while (NCH(deep) == 1) {
2951 deep = CHILD(deep, 0);
2952 }
2953 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2954 simple = 0;
2955 }
2956 expr1 = ast_for_testlist(c, ch);
2957 if (!expr1) {
2958 return NULL;
2959 }
2960 switch (expr1->kind) {
2961 case Name_kind:
2962 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2963 return NULL;
2964 }
2965 expr1->v.Name.ctx = Store;
2966 break;
2967 case Attribute_kind:
2968 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2969 return NULL;
2970 }
2971 expr1->v.Attribute.ctx = Store;
2972 break;
2973 case Subscript_kind:
2974 expr1->v.Subscript.ctx = Store;
2975 break;
2976 case List_kind:
2977 ast_error(c, ch,
2978 "only single target (not list) can be annotated");
2979 return NULL;
2980 case Tuple_kind:
2981 ast_error(c, ch,
2982 "only single target (not tuple) can be annotated");
2983 return NULL;
2984 default:
2985 ast_error(c, ch,
2986 "illegal target for annotation");
2987 return NULL;
2988 }
2989
2990 if (expr1->kind != Name_kind) {
2991 simple = 0;
2992 }
2993 ch = CHILD(ann, 1);
2994 expr2 = ast_for_expr(c, ch);
2995 if (!expr2) {
2996 return NULL;
2997 }
2998 if (NCH(ann) == 2) {
2999 return AnnAssign(expr1, expr2, NULL, simple,
3000 LINENO(n), n->n_col_offset, c->c_arena);
3001 }
3002 else {
3003 ch = CHILD(ann, 3);
3004 expr3 = ast_for_expr(c, ch);
3005 if (!expr3) {
3006 return NULL;
3007 }
3008 return AnnAssign(expr1, expr2, expr3, simple,
3009 LINENO(n), n->n_col_offset, c->c_arena);
3010 }
3011 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003013 int i;
3014 asdl_seq *targets;
3015 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 expr_ty expression;
3017
Thomas Wouters89f507f2006-12-13 04:49:30 +00003018 /* a normal assignment */
3019 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003020 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003021 if (!targets)
3022 return NULL;
3023 for (i = 0; i < NCH(n) - 2; i += 2) {
3024 expr_ty e;
3025 node *ch = CHILD(n, i);
3026 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003027 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 return NULL;
3029 }
3030 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003032 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003034 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003035 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037
Thomas Wouters89f507f2006-12-13 04:49:30 +00003038 asdl_seq_SET(targets, i / 2, e);
3039 }
3040 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003041 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003042 expression = ast_for_testlist(c, value);
3043 else
3044 expression = ast_for_expr(c, value);
3045 if (!expression)
3046 return NULL;
3047 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049}
3050
Benjamin Peterson78565b22009-06-28 19:19:51 +00003051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003053ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054{
3055 asdl_seq *seq;
3056 int i;
3057 expr_ty e;
3058
3059 REQ(n, exprlist);
3060
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003061 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003063 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003065 e = ast_for_expr(c, CHILD(n, i));
3066 if (!e)
3067 return NULL;
3068 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003069 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003070 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 }
3072 return seq;
3073}
3074
3075static stmt_ty
3076ast_for_del_stmt(struct compiling *c, const node *n)
3077{
3078 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 /* del_stmt: 'del' exprlist */
3081 REQ(n, del_stmt);
3082
3083 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3084 if (!expr_list)
3085 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003086 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087}
3088
3089static stmt_ty
3090ast_for_flow_stmt(struct compiling *c, const node *n)
3091{
3092 /*
3093 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3094 | yield_stmt
3095 break_stmt: 'break'
3096 continue_stmt: 'continue'
3097 return_stmt: 'return' [testlist]
3098 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003099 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100 raise_stmt: 'raise' [test [',' test [',' test]]]
3101 */
3102 node *ch;
3103
3104 REQ(n, flow_stmt);
3105 ch = CHILD(n, 0);
3106 switch (TYPE(ch)) {
3107 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003108 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003110 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003112 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3113 if (!exp)
3114 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003115 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 }
3117 case return_stmt:
3118 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003119 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003121 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 if (!expression)
3123 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003124 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 }
3126 case raise_stmt:
3127 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003128 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3129 else if (NCH(ch) >= 2) {
3130 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3132 if (!expression)
3133 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003134 if (NCH(ch) == 4) {
3135 cause = ast_for_expr(c, CHILD(ch, 3));
3136 if (!cause)
3137 return NULL;
3138 }
3139 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 }
3141 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003142 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 "unexpected flow_stmt: %d", TYPE(ch));
3144 return NULL;
3145 }
3146}
3147
3148static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003149alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150{
3151 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003152 import_as_name: NAME ['as' NAME]
3153 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 dotted_name: NAME ('.' NAME)*
3155 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003156 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003157
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 loop:
3159 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003160 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003161 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003162 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003163 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003164 if (!name)
3165 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003166 if (NCH(n) == 3) {
3167 node *str_node = CHILD(n, 2);
3168 str = NEW_IDENTIFIER(str_node);
3169 if (!str)
3170 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003171 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 return NULL;
3173 }
3174 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003175 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003176 return NULL;
3177 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003178 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003179 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180 case dotted_as_name:
3181 if (NCH(n) == 1) {
3182 n = CHILD(n, 0);
3183 goto loop;
3184 }
3185 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003186 node *asname_node = CHILD(n, 2);
3187 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003188 if (!a)
3189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003191 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003192 if (!a->asname)
3193 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003194 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003195 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 return a;
3197 }
3198 break;
3199 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003200 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003201 node *name_node = CHILD(n, 0);
3202 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003203 if (!name)
3204 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003205 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003206 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003207 return alias(name, NULL, c->c_arena);
3208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 else {
3210 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003211 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003212 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215
3216 len = 0;
3217 for (i = 0; i < NCH(n); i += 2)
3218 /* length of string plus one for the dot */
3219 len += strlen(STR(CHILD(n, i))) + 1;
3220 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003221 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 if (!str)
3223 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003224 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225 if (!s)
3226 return NULL;
3227 for (i = 0; i < NCH(n); i += 2) {
3228 char *sch = STR(CHILD(n, i));
3229 strcpy(s, STR(CHILD(n, i)));
3230 s += strlen(sch);
3231 *s++ = '.';
3232 }
3233 --s;
3234 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3236 PyBytes_GET_SIZE(str),
3237 NULL);
3238 Py_DECREF(str);
3239 if (!uni)
3240 return NULL;
3241 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003242 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003243 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3244 Py_DECREF(str);
3245 return NULL;
3246 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003247 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 }
3249 break;
3250 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003251 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003252 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3253 Py_DECREF(str);
3254 return NULL;
3255 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003256 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003258 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 "unexpected import name: %d", TYPE(n));
3260 return NULL;
3261 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003262
3263 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264 return NULL;
3265}
3266
3267static stmt_ty
3268ast_for_import_stmt(struct compiling *c, const node *n)
3269{
3270 /*
3271 import_stmt: import_name | import_from
3272 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003273 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3274 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003276 int lineno;
3277 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278 int i;
3279 asdl_seq *aliases;
3280
3281 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003282 lineno = LINENO(n);
3283 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003285 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003287 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003288 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003289 if (!aliases)
3290 return NULL;
3291 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003292 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003293 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003295 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003297 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003299 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003301 int idx, ndots = 0;
3302 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003303 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003305 /* Count the number of dots (for relative imports) and check for the
3306 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 for (idx = 1; idx < NCH(n); idx++) {
3308 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003309 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3310 if (!mod)
3311 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003312 idx++;
3313 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003314 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003316 ndots += 3;
3317 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 } else if (TYPE(CHILD(n, idx)) != DOT) {
3319 break;
3320 }
3321 ndots++;
3322 }
3323 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003324 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003325 case STAR:
3326 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003327 n = CHILD(n, idx);
3328 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003329 break;
3330 case LPAR:
3331 /* from ... import (x, y, z) */
3332 n = CHILD(n, idx + 1);
3333 n_children = NCH(n);
3334 break;
3335 case import_as_names:
3336 /* from ... import x, y, z */
3337 n = CHILD(n, idx);
3338 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003339 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003340 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341 " surrounding parentheses");
3342 return NULL;
3343 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003344 break;
3345 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003346 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003347 return NULL;
3348 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003350 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003351 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353
3354 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003355 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003356 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003357 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003359 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003361 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003363 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003364 if (!import_alias)
3365 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003366 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003367 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003369 if (mod != NULL)
3370 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003371 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003372 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373 }
Neal Norwitz79792652005-11-14 04:25:03 +00003374 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375 "unknown import statement: starts with command '%s'",
3376 STR(CHILD(n, 0)));
3377 return NULL;
3378}
3379
3380static stmt_ty
3381ast_for_global_stmt(struct compiling *c, const node *n)
3382{
3383 /* global_stmt: 'global' NAME (',' NAME)* */
3384 identifier name;
3385 asdl_seq *s;
3386 int i;
3387
3388 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003389 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003391 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003392 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003393 name = NEW_IDENTIFIER(CHILD(n, i));
3394 if (!name)
3395 return NULL;
3396 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003398 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399}
3400
3401static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003402ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3403{
3404 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3405 identifier name;
3406 asdl_seq *s;
3407 int i;
3408
3409 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003410 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003411 if (!s)
3412 return NULL;
3413 for (i = 1; i < NCH(n); i += 2) {
3414 name = NEW_IDENTIFIER(CHILD(n, i));
3415 if (!name)
3416 return NULL;
3417 asdl_seq_SET(s, i / 2, name);
3418 }
3419 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3420}
3421
3422static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423ast_for_assert_stmt(struct compiling *c, const node *n)
3424{
3425 /* assert_stmt: 'assert' test [',' test] */
3426 REQ(n, assert_stmt);
3427 if (NCH(n) == 2) {
3428 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3429 if (!expression)
3430 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003431 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432 }
3433 else if (NCH(n) == 4) {
3434 expr_ty expr1, expr2;
3435
3436 expr1 = ast_for_expr(c, CHILD(n, 1));
3437 if (!expr1)
3438 return NULL;
3439 expr2 = ast_for_expr(c, CHILD(n, 3));
3440 if (!expr2)
3441 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442
Thomas Wouters89f507f2006-12-13 04:49:30 +00003443 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444 }
Neal Norwitz79792652005-11-14 04:25:03 +00003445 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 "improper number of parts to 'assert' statement: %d",
3447 NCH(n));
3448 return NULL;
3449}
3450
3451static asdl_seq *
3452ast_for_suite(struct compiling *c, const node *n)
3453{
3454 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003455 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 stmt_ty s;
3457 int i, total, num, end, pos = 0;
3458 node *ch;
3459
3460 REQ(n, suite);
3461
3462 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003463 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003465 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003467 n = CHILD(n, 0);
3468 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003470 */
3471 end = NCH(n) - 1;
3472 if (TYPE(CHILD(n, end - 1)) == SEMI)
3473 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003475 for (i = 0; i < end; i += 2) {
3476 ch = CHILD(n, i);
3477 s = ast_for_stmt(c, ch);
3478 if (!s)
3479 return NULL;
3480 asdl_seq_SET(seq, pos++, s);
3481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482 }
3483 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003484 for (i = 2; i < (NCH(n) - 1); i++) {
3485 ch = CHILD(n, i);
3486 REQ(ch, stmt);
3487 num = num_stmts(ch);
3488 if (num == 1) {
3489 /* small_stmt or compound_stmt with only one child */
3490 s = ast_for_stmt(c, ch);
3491 if (!s)
3492 return NULL;
3493 asdl_seq_SET(seq, pos++, s);
3494 }
3495 else {
3496 int j;
3497 ch = CHILD(ch, 0);
3498 REQ(ch, simple_stmt);
3499 for (j = 0; j < NCH(ch); j += 2) {
3500 /* statement terminates with a semi-colon ';' */
3501 if (NCH(CHILD(ch, j)) == 0) {
3502 assert((j + 1) == NCH(ch));
3503 break;
3504 }
3505 s = ast_for_stmt(c, CHILD(ch, j));
3506 if (!s)
3507 return NULL;
3508 asdl_seq_SET(seq, pos++, s);
3509 }
3510 }
3511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512 }
3513 assert(pos == seq->size);
3514 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515}
3516
3517static stmt_ty
3518ast_for_if_stmt(struct compiling *c, const node *n)
3519{
3520 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3521 ['else' ':' suite]
3522 */
3523 char *s;
3524
3525 REQ(n, if_stmt);
3526
3527 if (NCH(n) == 4) {
3528 expr_ty expression;
3529 asdl_seq *suite_seq;
3530
3531 expression = ast_for_expr(c, CHILD(n, 1));
3532 if (!expression)
3533 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003535 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003536 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537
Guido van Rossumd8faa362007-04-27 19:54:29 +00003538 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3539 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003541
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 s = STR(CHILD(n, 4));
3543 /* s[2], the third character in the string, will be
3544 's' for el_s_e, or
3545 'i' for el_i_f
3546 */
3547 if (s[2] == 's') {
3548 expr_ty expression;
3549 asdl_seq *seq1, *seq2;
3550
3551 expression = ast_for_expr(c, CHILD(n, 1));
3552 if (!expression)
3553 return NULL;
3554 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003555 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 return NULL;
3557 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003558 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 return NULL;
3560
Guido van Rossumd8faa362007-04-27 19:54:29 +00003561 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3562 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 }
3564 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003565 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003566 expr_ty expression;
3567 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003568 asdl_seq *orelse = NULL;
3569 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 /* must reference the child n_elif+1 since 'else' token is third,
3571 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003572 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3573 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3574 has_else = 1;
3575 n_elif -= 3;
3576 }
3577 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578
Thomas Wouters89f507f2006-12-13 04:49:30 +00003579 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003580 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003582 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003583 if (!orelse)
3584 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003586 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003588 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3589 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003591 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3592 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 asdl_seq_SET(orelse, 0,
3596 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003597 LINENO(CHILD(n, NCH(n) - 6)),
3598 CHILD(n, NCH(n) - 6)->n_col_offset,
3599 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003600 /* the just-created orelse handled the last elif */
3601 n_elif--;
3602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603
Thomas Wouters89f507f2006-12-13 04:49:30 +00003604 for (i = 0; i < n_elif; i++) {
3605 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003606 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003607 if (!newobj)
3608 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003610 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003612 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003613 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615
Thomas Wouters89f507f2006-12-13 04:49:30 +00003616 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003618 LINENO(CHILD(n, off)),
3619 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003620 orelse = newobj;
3621 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003622 expression = ast_for_expr(c, CHILD(n, 1));
3623 if (!expression)
3624 return NULL;
3625 suite_seq = ast_for_suite(c, CHILD(n, 3));
3626 if (!suite_seq)
3627 return NULL;
3628 return If(expression, suite_seq, orelse,
3629 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003631
3632 PyErr_Format(PyExc_SystemError,
3633 "unexpected token in 'if' statement: %s", s);
3634 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635}
3636
3637static stmt_ty
3638ast_for_while_stmt(struct compiling *c, const node *n)
3639{
3640 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3641 REQ(n, while_stmt);
3642
3643 if (NCH(n) == 4) {
3644 expr_ty expression;
3645 asdl_seq *suite_seq;
3646
3647 expression = ast_for_expr(c, CHILD(n, 1));
3648 if (!expression)
3649 return NULL;
3650 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003651 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003653 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 }
3655 else if (NCH(n) == 7) {
3656 expr_ty expression;
3657 asdl_seq *seq1, *seq2;
3658
3659 expression = ast_for_expr(c, CHILD(n, 1));
3660 if (!expression)
3661 return NULL;
3662 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003663 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 return NULL;
3665 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003666 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 return NULL;
3668
Thomas Wouters89f507f2006-12-13 04:49:30 +00003669 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003671
3672 PyErr_Format(PyExc_SystemError,
3673 "wrong number of tokens for 'while' statement: %d",
3674 NCH(n));
3675 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676}
3677
3678static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003679ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003680{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003681 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003682 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003683 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003684 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3686 REQ(n, for_stmt);
3687
3688 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003689 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 if (!seq)
3691 return NULL;
3692 }
3693
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003694 node_target = CHILD(n, 1);
3695 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003696 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003698 /* Check the # of children rather than the length of _target, since
3699 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003700 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003701 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003702 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003704 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003706 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003707 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 return NULL;
3709 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003710 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 return NULL;
3712
Yury Selivanov75445082015-05-11 22:57:16 -04003713 if (is_async)
3714 return AsyncFor(target, expression, suite_seq, seq,
3715 LINENO(n), n->n_col_offset,
3716 c->c_arena);
3717 else
3718 return For(target, expression, suite_seq, seq,
3719 LINENO(n), n->n_col_offset,
3720 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721}
3722
3723static excepthandler_ty
3724ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3725{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003726 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 REQ(exc, except_clause);
3728 REQ(body, suite);
3729
3730 if (NCH(exc) == 1) {
3731 asdl_seq *suite_seq = ast_for_suite(c, body);
3732 if (!suite_seq)
3733 return NULL;
3734
Neal Norwitzad74aa82008-03-31 05:14:30 +00003735 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003736 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003737 }
3738 else if (NCH(exc) == 2) {
3739 expr_ty expression;
3740 asdl_seq *suite_seq;
3741
3742 expression = ast_for_expr(c, CHILD(exc, 1));
3743 if (!expression)
3744 return NULL;
3745 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003746 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 return NULL;
3748
Neal Norwitzad74aa82008-03-31 05:14:30 +00003749 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003750 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 }
3752 else if (NCH(exc) == 4) {
3753 asdl_seq *suite_seq;
3754 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003755 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003756 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003758 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003761 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 return NULL;
3763 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003764 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 return NULL;
3766
Neal Norwitzad74aa82008-03-31 05:14:30 +00003767 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003768 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003770
3771 PyErr_Format(PyExc_SystemError,
3772 "wrong number of children for 'except' clause: %d",
3773 NCH(exc));
3774 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775}
3776
3777static stmt_ty
3778ast_for_try_stmt(struct compiling *c, const node *n)
3779{
Neal Norwitzf599f422005-12-17 21:33:47 +00003780 const int nch = NCH(n);
3781 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003782 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003783
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 REQ(n, try_stmt);
3785
Neal Norwitzf599f422005-12-17 21:33:47 +00003786 body = ast_for_suite(c, CHILD(n, 2));
3787 if (body == NULL)
3788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789
Neal Norwitzf599f422005-12-17 21:33:47 +00003790 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3791 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3792 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3793 /* we can assume it's an "else",
3794 because nch >= 9 for try-else-finally and
3795 it would otherwise have a type of except_clause */
3796 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3797 if (orelse == NULL)
3798 return NULL;
3799 n_except--;
3800 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801
Neal Norwitzf599f422005-12-17 21:33:47 +00003802 finally = ast_for_suite(c, CHILD(n, nch - 1));
3803 if (finally == NULL)
3804 return NULL;
3805 n_except--;
3806 }
3807 else {
3808 /* we can assume it's an "else",
3809 otherwise it would have a type of except_clause */
3810 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3811 if (orelse == NULL)
3812 return NULL;
3813 n_except--;
3814 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003816 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003817 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 return NULL;
3819 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820
Neal Norwitzf599f422005-12-17 21:33:47 +00003821 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003822 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003823 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003824 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003825 if (handlers == NULL)
3826 return NULL;
3827
3828 for (i = 0; i < n_except; i++) {
3829 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3830 CHILD(n, 5 + i * 3));
3831 if (!e)
3832 return NULL;
3833 asdl_seq_SET(handlers, i, e);
3834 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003835 }
3836
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003837 assert(finally != NULL || asdl_seq_LEN(handlers));
3838 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003839}
3840
Georg Brandl0c315622009-05-25 21:10:36 +00003841/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003842static withitem_ty
3843ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003844{
3845 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003846
Georg Brandl0c315622009-05-25 21:10:36 +00003847 REQ(n, with_item);
3848 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003849 if (!context_expr)
3850 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003851 if (NCH(n) == 3) {
3852 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003853
3854 if (!optional_vars) {
3855 return NULL;
3856 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003857 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003858 return NULL;
3859 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003860 }
3861
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003862 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003863}
3864
Georg Brandl0c315622009-05-25 21:10:36 +00003865/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3866static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003867ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003868{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003869 int i, n_items;
3870 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003871
3872 REQ(n, with_stmt);
3873
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003874 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003875 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003876 if (!items)
3877 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003878 for (i = 1; i < NCH(n) - 2; i += 2) {
3879 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3880 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003881 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003882 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003883 }
3884
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003885 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3886 if (!body)
3887 return NULL;
3888
Yury Selivanov75445082015-05-11 22:57:16 -04003889 if (is_async)
3890 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3891 else
3892 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003893}
3894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003896ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003897{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003898 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003899 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003900 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003901 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003902
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 REQ(n, classdef);
3904
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003905 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 s = ast_for_suite(c, CHILD(n, 3));
3907 if (!s)
3908 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003909 classname = NEW_IDENTIFIER(CHILD(n, 1));
3910 if (!classname)
3911 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003912 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003913 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003914 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3915 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003917
3918 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003919 s = ast_for_suite(c, CHILD(n,5));
3920 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003921 return NULL;
3922 classname = NEW_IDENTIFIER(CHILD(n, 1));
3923 if (!classname)
3924 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003925 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003926 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003927 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3928 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929 }
3930
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003931 /* class NAME '(' arglist ')' ':' suite */
3932 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003933 {
3934 PyObject *dummy_name;
3935 expr_ty dummy;
3936 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3937 if (!dummy_name)
3938 return NULL;
3939 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3940 call = ast_for_call(c, CHILD(n, 3), dummy);
3941 if (!call)
3942 return NULL;
3943 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003945 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003947 classname = NEW_IDENTIFIER(CHILD(n, 1));
3948 if (!classname)
3949 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003950 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003951 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003952
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003953 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003954 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955}
3956
3957static stmt_ty
3958ast_for_stmt(struct compiling *c, const node *n)
3959{
3960 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003961 assert(NCH(n) == 1);
3962 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 }
3964 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003965 assert(num_stmts(n) == 1);
3966 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003967 }
3968 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003969 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003970 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3971 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003972 */
3973 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974 case expr_stmt:
3975 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 case del_stmt:
3977 return ast_for_del_stmt(c, n);
3978 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003979 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 case flow_stmt:
3981 return ast_for_flow_stmt(c, n);
3982 case import_stmt:
3983 return ast_for_import_stmt(c, n);
3984 case global_stmt:
3985 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003986 case nonlocal_stmt:
3987 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988 case assert_stmt:
3989 return ast_for_assert_stmt(c, n);
3990 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003991 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3993 TYPE(n), NCH(n));
3994 return NULL;
3995 }
3996 }
3997 else {
3998 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003999 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004000 */
4001 node *ch = CHILD(n, 0);
4002 REQ(n, compound_stmt);
4003 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 case if_stmt:
4005 return ast_for_if_stmt(c, ch);
4006 case while_stmt:
4007 return ast_for_while_stmt(c, ch);
4008 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004009 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 case try_stmt:
4011 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004012 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004013 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004015 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004017 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 case decorated:
4019 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004020 case async_stmt:
4021 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004023 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004024 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4025 TYPE(n), NCH(n));
4026 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004027 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028 }
4029}
4030
4031static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004032parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004034 const char *end;
4035 long x;
4036 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004037 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004038 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004040 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004041 errno = 0;
4042 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004043 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004044 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004045 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004046 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004047 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004048 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004049 }
4050 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004051 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004052 if (*end == '\0') {
4053 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004054 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004055 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004056 }
4057 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004058 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004059 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004060 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4061 if (compl.imag == -1.0 && PyErr_Occurred())
4062 return NULL;
4063 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004064 }
4065 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004066 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004067 dx = PyOS_string_to_double(s, NULL, NULL);
4068 if (dx == -1.0 && PyErr_Occurred())
4069 return NULL;
4070 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004071 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072}
4073
4074static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004075parsenumber(struct compiling *c, const char *s)
4076{
4077 char *dup, *end;
4078 PyObject *res = NULL;
4079
4080 assert(s != NULL);
4081
4082 if (strchr(s, '_') == NULL) {
4083 return parsenumber_raw(c, s);
4084 }
4085 /* Create a duplicate without underscores. */
4086 dup = PyMem_Malloc(strlen(s) + 1);
4087 end = dup;
4088 for (; *s; s++) {
4089 if (*s != '_') {
4090 *end++ = *s;
4091 }
4092 }
4093 *end = '\0';
4094 res = parsenumber_raw(c, dup);
4095 PyMem_Free(dup);
4096 return res;
4097}
4098
4099static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004100decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004101{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004102 const char *s, *t;
4103 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004104 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4105 while (s < end && (*s & 0x80)) s++;
4106 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004107 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108}
4109
Eric V. Smith56466482016-10-31 14:46:26 -04004110static int
4111warn_invalid_escape_sequence(struct compiling *c, const node *n,
4112 char first_invalid_escape_char)
4113{
4114 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4115 first_invalid_escape_char);
4116 if (msg == NULL) {
4117 return -1;
4118 }
4119 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4120 c->c_filename, LINENO(n),
4121 NULL, NULL) < 0 &&
4122 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4123 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004124 const char *s;
4125
4126 /* Replace the DeprecationWarning exception with a SyntaxError
4127 to get a more accurate error report */
4128 PyErr_Clear();
4129
4130 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004131 if (s != NULL) {
4132 ast_error(c, n, s);
4133 }
4134 Py_DECREF(msg);
4135 return -1;
4136 }
4137 Py_DECREF(msg);
4138 return 0;
4139}
4140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004142decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4143 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004144{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004145 PyObject *v, *u;
4146 char *buf;
4147 char *p;
4148 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004149
Benjamin Peterson202803a2016-02-25 22:34:45 -08004150 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004151 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004152 return NULL;
4153 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4154 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4155 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4156 if (u == NULL)
4157 return NULL;
4158 p = buf = PyBytes_AsString(u);
4159 end = s + len;
4160 while (s < end) {
4161 if (*s == '\\') {
4162 *p++ = *s++;
4163 if (*s & 0x80) {
4164 strcpy(p, "u005c");
4165 p += 5;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004166 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004167 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004168 if (*s & 0x80) { /* XXX inefficient */
4169 PyObject *w;
4170 int kind;
4171 void *data;
4172 Py_ssize_t len, i;
4173 w = decode_utf8(c, &s, end);
4174 if (w == NULL) {
4175 Py_DECREF(u);
4176 return NULL;
4177 }
4178 kind = PyUnicode_KIND(w);
4179 data = PyUnicode_DATA(w);
4180 len = PyUnicode_GET_LENGTH(w);
4181 for (i = 0; i < len; i++) {
4182 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4183 sprintf(p, "\\U%08x", chr);
4184 p += 10;
4185 }
4186 /* Should be impossible to overflow */
4187 assert(p - buf <= Py_SIZE(u));
4188 Py_DECREF(w);
4189 } else {
4190 *p++ = *s++;
4191 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004192 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004193 len = p - buf;
4194 s = buf;
4195
Eric V. Smith56466482016-10-31 14:46:26 -04004196 const char *first_invalid_escape;
4197 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4198
4199 if (v != NULL && first_invalid_escape != NULL) {
4200 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4201 /* We have not decref u before because first_invalid_escape points
4202 inside u. */
4203 Py_XDECREF(u);
4204 Py_DECREF(v);
4205 return NULL;
4206 }
4207 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004208 Py_XDECREF(u);
4209 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004210}
4211
Eric V. Smith56466482016-10-31 14:46:26 -04004212static PyObject *
4213decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4214 size_t len)
4215{
4216 const char *first_invalid_escape;
4217 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4218 &first_invalid_escape);
4219 if (result == NULL)
4220 return NULL;
4221
4222 if (first_invalid_escape != NULL) {
4223 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4224 Py_DECREF(result);
4225 return NULL;
4226 }
4227 }
4228 return result;
4229}
4230
Eric V. Smith451d0e32016-09-09 21:56:20 -04004231/* Compile this expression in to an expr_ty. Add parens around the
4232 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004233static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004234fstring_compile_expr(const char *expr_start, const char *expr_end,
4235 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004236
Eric V. Smith235a6f02015-09-19 14:51:32 -04004237{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004238 int all_whitespace = 1;
4239 int kind;
4240 void *data;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004241 PyCompilerFlags cf;
4242 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004243 char *str;
4244 PyObject *o;
4245 Py_ssize_t len;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004246 Py_ssize_t i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004247
Eric V. Smith1d44c412015-09-23 07:49:00 -04004248 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004249 assert(*(expr_start-1) == '{');
4250 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004251
Eric V. Smith451d0e32016-09-09 21:56:20 -04004252 /* We know there are no escapes here, because backslashes are not allowed,
4253 and we know it's utf-8 encoded (per PEP 263). But, in order to check
4254 that each char is not whitespace, we need to decode it to unicode.
4255 Which is unfortunate, but such is life. */
Eric V. Smith1d44c412015-09-23 07:49:00 -04004256
Eric V. Smith451d0e32016-09-09 21:56:20 -04004257 /* If the substring is all whitespace, it's an error. We need to catch
4258 this here, and not when we call PyParser_ASTFromString, because turning
4259 the expression '' in to '()' would go from being invalid to valid. */
4260 /* Note that this code says an empty string is all whitespace. That's
4261 important. There's a test for it: f'{}'. */
4262 o = PyUnicode_DecodeUTF8(expr_start, expr_end-expr_start, NULL);
4263 if (o == NULL)
4264 return NULL;
4265 len = PyUnicode_GET_LENGTH(o);
4266 kind = PyUnicode_KIND(o);
4267 data = PyUnicode_DATA(o);
4268 for (i = 0; i < len; i++) {
4269 if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004270 all_whitespace = 0;
4271 break;
4272 }
4273 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004274 Py_DECREF(o);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004275 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004276 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004277 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004278 }
4279
Eric V. Smith451d0e32016-09-09 21:56:20 -04004280 /* Reuse len to be the length of the utf-8 input string. */
4281 len = expr_end - expr_start;
4282 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4283 str = PyMem_RawMalloc(len + 3);
4284 if (str == NULL)
4285 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004286
Eric V. Smith451d0e32016-09-09 21:56:20 -04004287 str[0] = '(';
4288 memcpy(str+1, expr_start, len);
4289 str[len+1] = ')';
4290 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004291
4292 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004293 mod = PyParser_ASTFromString(str, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004294 Py_eval_input, &cf, c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004295 PyMem_RawFree(str);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004296 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004297 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004298 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004299}
4300
4301/* Return -1 on error.
4302
4303 Return 0 if we reached the end of the literal.
4304
4305 Return 1 if we haven't reached the end of the literal, but we want
4306 the caller to process the literal up to this point. Used for
4307 doubled braces.
4308*/
4309static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004310fstring_find_literal(const char **str, const char *end, int raw,
4311 PyObject **literal, int recurse_lvl,
4312 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004313{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004314 /* Get any literal string. It ends when we hit an un-doubled left
4315 brace (which isn't part of a unicode name escape such as
4316 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004317
Eric V. Smith451d0e32016-09-09 21:56:20 -04004318 const char *literal_start = *str;
4319 const char *literal_end;
4320 int in_named_escape = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004321 int result = 0;
4322
Eric V. Smith235a6f02015-09-19 14:51:32 -04004323 assert(*literal == NULL);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004324 for (; *str < end; (*str)++) {
4325 char ch = **str;
4326 if (!in_named_escape && ch == '{' && (*str)-literal_start >= 2 &&
4327 *(*str-2) == '\\' && *(*str-1) == 'N') {
4328 in_named_escape = 1;
4329 } else if (in_named_escape && ch == '}') {
4330 in_named_escape = 0;
4331 } else if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004332 /* Check for doubled braces, but only at the top level. If
4333 we checked at every level, then f'{0:{3}}' would fail
4334 with the two closing braces. */
4335 if (recurse_lvl == 0) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004336 if (*str+1 < end && *(*str+1) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004337 /* We're going to tell the caller that the literal ends
4338 here, but that they should continue scanning. But also
4339 skip over the second brace when we resume scanning. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004340 literal_end = *str+1;
4341 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004342 result = 1;
4343 goto done;
4344 }
4345
4346 /* Where a single '{' is the start of a new expression, a
4347 single '}' is not allowed. */
4348 if (ch == '}') {
4349 ast_error(c, n, "f-string: single '}' is not allowed");
4350 return -1;
4351 }
4352 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004353 /* We're either at a '{', which means we're starting another
4354 expression; or a '}', which means we're at the end of this
4355 f-string (for a nested format_spec). */
4356 break;
4357 }
4358 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004359 literal_end = *str;
4360 assert(*str <= end);
4361 assert(*str == end || **str == '{' || **str == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004362done:
4363 if (literal_start != literal_end) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004364 if (raw)
4365 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
4366 literal_end-literal_start,
4367 NULL, NULL);
4368 else
Eric V. Smith56466482016-10-31 14:46:26 -04004369 *literal = decode_unicode_with_escapes(c, n, literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004370 literal_end-literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004371 if (!*literal)
4372 return -1;
4373 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004374 return result;
4375}
4376
4377/* Forward declaration because parsing is recursive. */
4378static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004379fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004380 struct compiling *c, const node *n);
4381
Eric V. Smith451d0e32016-09-09 21:56:20 -04004382/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004383 expression (so it must be a '{'). Returns the FormattedValue node,
4384 which includes the expression, conversion character, and
4385 format_spec expression.
4386
4387 Note that I don't do a perfect job here: I don't make sure that a
4388 closing brace doesn't match an opening paren, for example. It
4389 doesn't need to error on all invalid expressions, just correctly
4390 find the end of all valid ones. Any errors inside the expression
4391 will be caught when we parse it later. */
4392static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004393fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004394 expr_ty *expression, struct compiling *c, const node *n)
4395{
4396 /* Return -1 on error, else 0. */
4397
Eric V. Smith451d0e32016-09-09 21:56:20 -04004398 const char *expr_start;
4399 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004400 expr_ty simple_expression;
4401 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004402 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004403
4404 /* 0 if we're not in a string, else the quote char we're trying to
4405 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004406 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004407
4408 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4409 int string_type = 0;
4410
4411 /* Keep track of nesting level for braces/parens/brackets in
4412 expressions. */
4413 Py_ssize_t nested_depth = 0;
4414
4415 /* Can only nest one level deep. */
4416 if (recurse_lvl >= 2) {
4417 ast_error(c, n, "f-string: expressions nested too deeply");
4418 return -1;
4419 }
4420
4421 /* The first char must be a left brace, or we wouldn't have gotten
4422 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004423 assert(**str == '{');
4424 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004425
Eric V. Smith451d0e32016-09-09 21:56:20 -04004426 expr_start = *str;
4427 for (; *str < end; (*str)++) {
4428 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004429
4430 /* Loop invariants. */
4431 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004432 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004433 if (quote_char)
4434 assert(string_type == 1 || string_type == 3);
4435 else
4436 assert(string_type == 0);
4437
Eric V. Smith451d0e32016-09-09 21:56:20 -04004438 ch = **str;
4439 /* Nowhere inside an expression is a backslash allowed. */
4440 if (ch == '\\') {
4441 /* Error: can't include a backslash character, inside
4442 parens or strings or not. */
4443 ast_error(c, n, "f-string expression part "
4444 "cannot include a backslash");
4445 return -1;
4446 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004447 if (quote_char) {
4448 /* We're inside a string. See if we're at the end. */
4449 /* This code needs to implement the same non-error logic
4450 as tok_get from tokenizer.c, at the letter_quote
4451 label. To actually share that code would be a
4452 nightmare. But, it's unlikely to change and is small,
4453 so duplicate it here. Note we don't need to catch all
4454 of the errors, since they'll be caught when parsing the
4455 expression. We just need to match the non-error
4456 cases. Thus we can ignore \n in single-quoted strings,
4457 for example. Or non-terminated strings. */
4458 if (ch == quote_char) {
4459 /* Does this match the string_type (single or triple
4460 quoted)? */
4461 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004462 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004463 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004464 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004465 string_type = 0;
4466 quote_char = 0;
4467 continue;
4468 }
4469 } else {
4470 /* We're at the end of a normal string. */
4471 quote_char = 0;
4472 string_type = 0;
4473 continue;
4474 }
4475 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004476 } else if (ch == '\'' || ch == '"') {
4477 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004478 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004479 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004480 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004481 } else {
4482 /* Start of a normal string. */
4483 string_type = 1;
4484 }
4485 /* Start looking for the end of the string. */
4486 quote_char = ch;
4487 } else if (ch == '[' || ch == '{' || ch == '(') {
4488 nested_depth++;
4489 } else if (nested_depth != 0 &&
4490 (ch == ']' || ch == '}' || ch == ')')) {
4491 nested_depth--;
4492 } else if (ch == '#') {
4493 /* Error: can't include a comment character, inside parens
4494 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004495 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004496 return -1;
4497 } else if (nested_depth == 0 &&
4498 (ch == '!' || ch == ':' || ch == '}')) {
4499 /* First, test for the special case of "!=". Since '=' is
4500 not an allowed conversion character, nothing is lost in
4501 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004502 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004503 /* This isn't a conversion character, just continue. */
4504 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004505 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004506 /* Normal way out of this loop. */
4507 break;
4508 } else {
4509 /* Just consume this char and loop around. */
4510 }
4511 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004512 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004513 /* If we leave this loop in a string or with mismatched parens, we
4514 don't care. We'll get a syntax error when compiling the
4515 expression. But, we can produce a better error message, so
4516 let's just do that.*/
4517 if (quote_char) {
4518 ast_error(c, n, "f-string: unterminated string");
4519 return -1;
4520 }
4521 if (nested_depth) {
4522 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4523 return -1;
4524 }
4525
Eric V. Smith451d0e32016-09-09 21:56:20 -04004526 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004527 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004528
4529 /* Compile the expression as soon as possible, so we show errors
4530 related to the expression before errors related to the
4531 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004532 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004533 if (!simple_expression)
4534 return -1;
4535
4536 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004537 if (**str == '!') {
4538 *str += 1;
4539 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004540 goto unexpected_end_of_string;
4541
Eric V. Smith451d0e32016-09-09 21:56:20 -04004542 conversion = **str;
4543 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004544
4545 /* Validate the conversion. */
4546 if (!(conversion == 's' || conversion == 'r'
4547 || conversion == 'a')) {
4548 ast_error(c, n, "f-string: invalid conversion character: "
4549 "expected 's', 'r', or 'a'");
4550 return -1;
4551 }
4552 }
4553
4554 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004555 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004556 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004557 if (**str == ':') {
4558 *str += 1;
4559 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004560 goto unexpected_end_of_string;
4561
4562 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004563 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004564 if (!format_spec)
4565 return -1;
4566 }
4567
Eric V. Smith451d0e32016-09-09 21:56:20 -04004568 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004569 goto unexpected_end_of_string;
4570
4571 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004572 assert(*str < end);
4573 assert(**str == '}');
4574 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004575
Eric V. Smith451d0e32016-09-09 21:56:20 -04004576 /* And now create the FormattedValue node that represents this
4577 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004578 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004579 format_spec, LINENO(n), n->n_col_offset,
4580 c->c_arena);
4581 if (!*expression)
4582 return -1;
4583
4584 return 0;
4585
4586unexpected_end_of_string:
4587 ast_error(c, n, "f-string: expecting '}'");
4588 return -1;
4589}
4590
4591/* Return -1 on error.
4592
4593 Return 0 if we have a literal (possible zero length) and an
4594 expression (zero length if at the end of the string.
4595
4596 Return 1 if we have a literal, but no expression, and we want the
4597 caller to call us again. This is used to deal with doubled
4598 braces.
4599
4600 When called multiple times on the string 'a{{b{0}c', this function
4601 will return:
4602
4603 1. the literal 'a{' with no expression, and a return value
4604 of 1. Despite the fact that there's no expression, the return
4605 value of 1 means we're not finished yet.
4606
4607 2. the literal 'b' and the expression '0', with a return value of
4608 0. The fact that there's an expression means we're not finished.
4609
4610 3. literal 'c' with no expression and a return value of 0. The
4611 combination of the return value of 0 with no expression means
4612 we're finished.
4613*/
4614static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004615fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4616 int recurse_lvl, PyObject **literal,
4617 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004618 struct compiling *c, const node *n)
4619{
4620 int result;
4621
4622 assert(*literal == NULL && *expression == NULL);
4623
4624 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004625 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004626 if (result < 0)
4627 goto error;
4628
4629 assert(result == 0 || result == 1);
4630
4631 if (result == 1)
4632 /* We have a literal, but don't look at the expression. */
4633 return 1;
4634
Eric V. Smith451d0e32016-09-09 21:56:20 -04004635 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004636 /* We're at the end of the string or the end of a nested
4637 f-string: no expression. The top-level error case where we
4638 expect to be at the end of the string but we're at a '}' is
4639 handled later. */
4640 return 0;
4641
4642 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004643 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004644
Eric V. Smith451d0e32016-09-09 21:56:20 -04004645 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004646 goto error;
4647
4648 return 0;
4649
4650error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004651 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004652 return -1;
4653}
4654
4655#define EXPRLIST_N_CACHED 64
4656
4657typedef struct {
4658 /* Incrementally build an array of expr_ty, so be used in an
4659 asdl_seq. Cache some small but reasonably sized number of
4660 expr_ty's, and then after that start dynamically allocating,
4661 doubling the number allocated each time. Note that the f-string
4662 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4663 Str for the literal 'a'. So you add expr_ty's about twice as
4664 fast as you add exressions in an f-string. */
4665
4666 Py_ssize_t allocated; /* Number we've allocated. */
4667 Py_ssize_t size; /* Number we've used. */
4668 expr_ty *p; /* Pointer to the memory we're actually
4669 using. Will point to 'data' until we
4670 start dynamically allocating. */
4671 expr_ty data[EXPRLIST_N_CACHED];
4672} ExprList;
4673
4674#ifdef NDEBUG
4675#define ExprList_check_invariants(l)
4676#else
4677static void
4678ExprList_check_invariants(ExprList *l)
4679{
4680 /* Check our invariants. Make sure this object is "live", and
4681 hasn't been deallocated. */
4682 assert(l->size >= 0);
4683 assert(l->p != NULL);
4684 if (l->size <= EXPRLIST_N_CACHED)
4685 assert(l->data == l->p);
4686}
4687#endif
4688
4689static void
4690ExprList_Init(ExprList *l)
4691{
4692 l->allocated = EXPRLIST_N_CACHED;
4693 l->size = 0;
4694
4695 /* Until we start allocating dynamically, p points to data. */
4696 l->p = l->data;
4697
4698 ExprList_check_invariants(l);
4699}
4700
4701static int
4702ExprList_Append(ExprList *l, expr_ty exp)
4703{
4704 ExprList_check_invariants(l);
4705 if (l->size >= l->allocated) {
4706 /* We need to alloc (or realloc) the memory. */
4707 Py_ssize_t new_size = l->allocated * 2;
4708
4709 /* See if we've ever allocated anything dynamically. */
4710 if (l->p == l->data) {
4711 Py_ssize_t i;
4712 /* We're still using the cached data. Switch to
4713 alloc-ing. */
4714 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4715 if (!l->p)
4716 return -1;
4717 /* Copy the cached data into the new buffer. */
4718 for (i = 0; i < l->size; i++)
4719 l->p[i] = l->data[i];
4720 } else {
4721 /* Just realloc. */
4722 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4723 if (!tmp) {
4724 PyMem_RawFree(l->p);
4725 l->p = NULL;
4726 return -1;
4727 }
4728 l->p = tmp;
4729 }
4730
4731 l->allocated = new_size;
4732 assert(l->allocated == 2 * l->size);
4733 }
4734
4735 l->p[l->size++] = exp;
4736
4737 ExprList_check_invariants(l);
4738 return 0;
4739}
4740
4741static void
4742ExprList_Dealloc(ExprList *l)
4743{
4744 ExprList_check_invariants(l);
4745
4746 /* If there's been an error, or we've never dynamically allocated,
4747 do nothing. */
4748 if (!l->p || l->p == l->data) {
4749 /* Do nothing. */
4750 } else {
4751 /* We have dynamically allocated. Free the memory. */
4752 PyMem_RawFree(l->p);
4753 }
4754 l->p = NULL;
4755 l->size = -1;
4756}
4757
4758static asdl_seq *
4759ExprList_Finish(ExprList *l, PyArena *arena)
4760{
4761 asdl_seq *seq;
4762
4763 ExprList_check_invariants(l);
4764
4765 /* Allocate the asdl_seq and copy the expressions in to it. */
4766 seq = _Py_asdl_seq_new(l->size, arena);
4767 if (seq) {
4768 Py_ssize_t i;
4769 for (i = 0; i < l->size; i++)
4770 asdl_seq_SET(seq, i, l->p[i]);
4771 }
4772 ExprList_Dealloc(l);
4773 return seq;
4774}
4775
4776/* The FstringParser is designed to add a mix of strings and
4777 f-strings, and concat them together as needed. Ultimately, it
4778 generates an expr_ty. */
4779typedef struct {
4780 PyObject *last_str;
4781 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004782 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004783} FstringParser;
4784
4785#ifdef NDEBUG
4786#define FstringParser_check_invariants(state)
4787#else
4788static void
4789FstringParser_check_invariants(FstringParser *state)
4790{
4791 if (state->last_str)
4792 assert(PyUnicode_CheckExact(state->last_str));
4793 ExprList_check_invariants(&state->expr_list);
4794}
4795#endif
4796
4797static void
4798FstringParser_Init(FstringParser *state)
4799{
4800 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004801 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004802 ExprList_Init(&state->expr_list);
4803 FstringParser_check_invariants(state);
4804}
4805
4806static void
4807FstringParser_Dealloc(FstringParser *state)
4808{
4809 FstringParser_check_invariants(state);
4810
4811 Py_XDECREF(state->last_str);
4812 ExprList_Dealloc(&state->expr_list);
4813}
4814
4815/* Make a Str node, but decref the PyUnicode object being added. */
4816static expr_ty
4817make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4818{
4819 PyObject *s = *str;
4820 *str = NULL;
4821 assert(PyUnicode_CheckExact(s));
4822 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4823 Py_DECREF(s);
4824 return NULL;
4825 }
4826 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4827}
4828
4829/* Add a non-f-string (that is, a regular literal string). str is
4830 decref'd. */
4831static int
4832FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4833{
4834 FstringParser_check_invariants(state);
4835
4836 assert(PyUnicode_CheckExact(str));
4837
4838 if (PyUnicode_GET_LENGTH(str) == 0) {
4839 Py_DECREF(str);
4840 return 0;
4841 }
4842
4843 if (!state->last_str) {
4844 /* We didn't have a string before, so just remember this one. */
4845 state->last_str = str;
4846 } else {
4847 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004848 PyUnicode_AppendAndDel(&state->last_str, str);
4849 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004850 return -1;
4851 }
4852 FstringParser_check_invariants(state);
4853 return 0;
4854}
4855
Eric V. Smith451d0e32016-09-09 21:56:20 -04004856/* Parse an f-string. The f-string is in *str to end, with no
4857 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004858static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004859FstringParser_ConcatFstring(FstringParser *state, const char **str,
4860 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004861 struct compiling *c, const node *n)
4862{
4863 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004864 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004865
4866 /* Parse the f-string. */
4867 while (1) {
4868 PyObject *literal = NULL;
4869 expr_ty expression = NULL;
4870
4871 /* If there's a zero length literal in front of the
4872 expression, literal will be NULL. If we're at the end of
4873 the f-string, expression will be NULL (unless result == 1,
4874 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004875 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004876 &literal, &expression,
4877 c, n);
4878 if (result < 0)
4879 return -1;
4880
4881 /* Add the literal, if any. */
4882 if (!literal) {
4883 /* Do nothing. Just leave last_str alone (and possibly
4884 NULL). */
4885 } else if (!state->last_str) {
4886 state->last_str = literal;
4887 literal = NULL;
4888 } else {
4889 /* We have a literal, concatenate it. */
4890 assert(PyUnicode_GET_LENGTH(literal) != 0);
4891 if (FstringParser_ConcatAndDel(state, literal) < 0)
4892 return -1;
4893 literal = NULL;
4894 }
4895 assert(!state->last_str ||
4896 PyUnicode_GET_LENGTH(state->last_str) != 0);
4897
4898 /* We've dealt with the literal now. It can't be leaked on further
4899 errors. */
4900 assert(literal == NULL);
4901
4902 /* See if we should just loop around to get the next literal
4903 and expression, while ignoring the expression this
4904 time. This is used for un-doubling braces, as an
4905 optimization. */
4906 if (result == 1)
4907 continue;
4908
4909 if (!expression)
4910 /* We're done with this f-string. */
4911 break;
4912
4913 /* We know we have an expression. Convert any existing string
4914 to a Str node. */
4915 if (!state->last_str) {
4916 /* Do nothing. No previous literal. */
4917 } else {
4918 /* Convert the existing last_str literal to a Str node. */
4919 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4920 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4921 return -1;
4922 }
4923
4924 if (ExprList_Append(&state->expr_list, expression) < 0)
4925 return -1;
4926 }
4927
Eric V. Smith235a6f02015-09-19 14:51:32 -04004928 /* If recurse_lvl is zero, then we must be at the end of the
4929 string. Otherwise, we must be at a right brace. */
4930
Eric V. Smith451d0e32016-09-09 21:56:20 -04004931 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004932 ast_error(c, n, "f-string: unexpected end of string");
4933 return -1;
4934 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004935 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004936 ast_error(c, n, "f-string: expecting '}'");
4937 return -1;
4938 }
4939
4940 FstringParser_check_invariants(state);
4941 return 0;
4942}
4943
4944/* Convert the partial state reflected in last_str and expr_list to an
4945 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4946static expr_ty
4947FstringParser_Finish(FstringParser *state, struct compiling *c,
4948 const node *n)
4949{
4950 asdl_seq *seq;
4951
4952 FstringParser_check_invariants(state);
4953
4954 /* If we're just a constant string with no expressions, return
4955 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004956 if (!state->fmode) {
4957 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004958 if (!state->last_str) {
4959 /* Create a zero length string. */
4960 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4961 if (!state->last_str)
4962 goto error;
4963 }
4964 return make_str_node_and_del(&state->last_str, c, n);
4965 }
4966
4967 /* Create a Str node out of last_str, if needed. It will be the
4968 last node in our expression list. */
4969 if (state->last_str) {
4970 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4971 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4972 goto error;
4973 }
4974 /* This has already been freed. */
4975 assert(state->last_str == NULL);
4976
4977 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4978 if (!seq)
4979 goto error;
4980
Eric V. Smith235a6f02015-09-19 14:51:32 -04004981 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4982
4983error:
4984 FstringParser_Dealloc(state);
4985 return NULL;
4986}
4987
Eric V. Smith451d0e32016-09-09 21:56:20 -04004988/* Given an f-string (with no 'f' or quotes) that's in *str and ends
4989 at end, parse it into an expr_ty. Return NULL on error. Adjust
4990 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004991static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004992fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 struct compiling *c, const node *n)
4994{
4995 FstringParser state;
4996
4997 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004998 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004999 c, n) < 0) {
5000 FstringParser_Dealloc(&state);
5001 return NULL;
5002 }
5003
5004 return FstringParser_Finish(&state, c, n);
5005}
5006
5007/* n is a Python string literal, including the bracketing quote
5008 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005009 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005010 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005011 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5012 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005014static int
5015parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5016 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005017{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005018 size_t len;
5019 const char *s = STR(n);
5020 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005021 int fmode = 0;
5022 *bytesmode = 0;
5023 *rawmode = 0;
5024 *result = NULL;
5025 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005026 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005027 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005028 if (quote == 'b' || quote == 'B') {
5029 quote = *++s;
5030 *bytesmode = 1;
5031 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005032 else if (quote == 'u' || quote == 'U') {
5033 quote = *++s;
5034 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005035 else if (quote == 'r' || quote == 'R') {
5036 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005037 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005038 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005039 else if (quote == 'f' || quote == 'F') {
5040 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005041 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005042 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005043 else {
5044 break;
5045 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005046 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005047 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005048 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005049 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005050 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005051 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005052 if (quote != '\'' && quote != '\"') {
5053 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005054 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005055 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005056 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005057 s++;
5058 len = strlen(s);
5059 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005060 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005061 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005062 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005063 }
5064 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005065 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005066 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005067 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005068 }
5069 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005070 /* A triple quoted string. We've already skipped one quote at
5071 the start and one at the end of the string. Now skip the
5072 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005073 s += 2;
5074 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005075 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005076 if (s[--len] != quote || s[--len] != quote) {
5077 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005078 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005079 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005080 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005081
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082 if (fmode) {
5083 /* Just return the bytes. The caller will parse the resulting
5084 string. */
5085 *fstr = s;
5086 *fstrlen = len;
5087 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005088 }
5089
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005091 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005092 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005093 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005094 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005095 const char *ch;
5096 for (ch = s; *ch; ch++) {
5097 if (Py_CHARMASK(*ch) >= 0x80) {
5098 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005099 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005100 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005101 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005102 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005103 if (*rawmode)
5104 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005105 else
Eric V. Smith56466482016-10-31 14:46:26 -04005106 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005107 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005108 if (*rawmode)
5109 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005110 else
Eric V. Smith56466482016-10-31 14:46:26 -04005111 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005112 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005113 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005114}
5115
Eric V. Smith235a6f02015-09-19 14:51:32 -04005116/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5117 each STRING atom, and process it as needed. For bytes, just
5118 concatenate them together, and the result will be a Bytes node. For
5119 normal strings and f-strings, concatenate them together. The result
5120 will be a Str node if there were no f-strings; a FormattedValue
5121 node if there's just an f-string (with no leading or trailing
5122 literals), or a JoinedStr node if there are multiple f-strings or
5123 any literals involved. */
5124static expr_ty
5125parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005126{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005127 int bytesmode = 0;
5128 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005129 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005130
5131 FstringParser state;
5132 FstringParser_Init(&state);
5133
5134 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005135 int this_bytesmode;
5136 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005137 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005138 const char *fstr;
5139 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005140
5141 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005142 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5143 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005144 goto error;
5145
5146 /* Check that we're not mixing bytes with unicode. */
5147 if (i != 0 && bytesmode != this_bytesmode) {
5148 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005149 /* s is NULL if the current string part is an f-string. */
5150 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005151 goto error;
5152 }
5153 bytesmode = this_bytesmode;
5154
Eric V. Smith451d0e32016-09-09 21:56:20 -04005155 if (fstr != NULL) {
5156 int result;
5157 assert(s == NULL && !bytesmode);
5158 /* This is an f-string. Parse and concatenate it. */
5159 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5160 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005161 if (result < 0)
5162 goto error;
5163 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005164 /* A string or byte string. */
5165 assert(s != NULL && fstr == NULL);
5166
Eric V. Smith451d0e32016-09-09 21:56:20 -04005167 assert(bytesmode ? PyBytes_CheckExact(s) :
5168 PyUnicode_CheckExact(s));
5169
Eric V. Smith451d0e32016-09-09 21:56:20 -04005170 if (bytesmode) {
5171 /* For bytes, concat as we go. */
5172 if (i == 0) {
5173 /* First time, just remember this value. */
5174 bytes_str = s;
5175 } else {
5176 PyBytes_ConcatAndDel(&bytes_str, s);
5177 if (!bytes_str)
5178 goto error;
5179 }
5180 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005181 /* This is a regular string. Concatenate it. */
5182 if (FstringParser_ConcatAndDel(&state, s) < 0)
5183 goto error;
5184 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005185 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005186 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005187 if (bytesmode) {
5188 /* Just return the bytes object and we're done. */
5189 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5190 goto error;
5191 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005193
Eric V. Smith235a6f02015-09-19 14:51:32 -04005194 /* We're not a bytes string, bytes_str should never have been set. */
5195 assert(bytes_str == NULL);
5196
5197 return FstringParser_Finish(&state, c, n);
5198
5199error:
5200 Py_XDECREF(bytes_str);
5201 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005203}