blob: deea579c0ddfd05372818228031793b7afe3b8f0 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100135validate_constant(PyObject *value)
136{
137 if (value == Py_None || value == Py_Ellipsis)
138 return 1;
139
140 if (PyLong_CheckExact(value)
141 || PyFloat_CheckExact(value)
142 || PyComplex_CheckExact(value)
143 || PyBool_Check(value)
144 || PyUnicode_CheckExact(value)
145 || PyBytes_CheckExact(value))
146 return 1;
147
148 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
149 PyObject *it;
150
151 it = PyObject_GetIter(value);
152 if (it == NULL)
153 return 0;
154
155 while (1) {
156 PyObject *item = PyIter_Next(it);
157 if (item == NULL) {
158 if (PyErr_Occurred()) {
159 Py_DECREF(it);
160 return 0;
161 }
162 break;
163 }
164
165 if (!validate_constant(item)) {
166 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100167 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100168 return 0;
169 }
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 }
172
173 Py_DECREF(it);
174 return 1;
175 }
176
177 return 0;
178}
179
180static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500181validate_expr(expr_ty exp, expr_context_ty ctx)
182{
183 int check_ctx = 1;
184 expr_context_ty actual_ctx;
185
186 /* First check expression context. */
187 switch (exp->kind) {
188 case Attribute_kind:
189 actual_ctx = exp->v.Attribute.ctx;
190 break;
191 case Subscript_kind:
192 actual_ctx = exp->v.Subscript.ctx;
193 break;
194 case Starred_kind:
195 actual_ctx = exp->v.Starred.ctx;
196 break;
197 case Name_kind:
198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100291 PyErr_Format(PyExc_TypeError,
292 "got an invalid type in Constant: %s",
293 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 return 0;
295 }
296 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 case Num_kind: {
298 PyObject *n = exp->v.Num.n;
299 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
300 !PyComplex_CheckExact(n)) {
301 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
302 return 0;
303 }
304 return 1;
305 }
306 case Str_kind: {
307 PyObject *s = exp->v.Str.s;
308 if (!PyUnicode_CheckExact(s)) {
309 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
310 return 0;
311 }
312 return 1;
313 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314 case JoinedStr_kind:
315 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
316 case FormattedValue_kind:
317 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
318 return 0;
319 if (exp->v.FormattedValue.format_spec)
320 return validate_expr(exp->v.FormattedValue.format_spec, Load);
321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Bytes_kind: {
323 PyObject *b = exp->v.Bytes.s;
324 if (!PyBytes_CheckExact(b)) {
325 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
326 return 0;
327 }
328 return 1;
329 }
330 case Attribute_kind:
331 return validate_expr(exp->v.Attribute.value, Load);
332 case Subscript_kind:
333 return validate_slice(exp->v.Subscript.slice) &&
334 validate_expr(exp->v.Subscript.value, Load);
335 case Starred_kind:
336 return validate_expr(exp->v.Starred.value, ctx);
337 case List_kind:
338 return validate_exprs(exp->v.List.elts, ctx, 0);
339 case Tuple_kind:
340 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
341 /* These last cases don't have any checking. */
342 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500343 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 case Ellipsis_kind:
345 return 1;
346 default:
347 PyErr_SetString(PyExc_SystemError, "unexpected expression");
348 return 0;
349 }
350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
369validate_body(asdl_seq *body, const char *owner)
370{
371 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
372}
373
374static int
375validate_stmt(stmt_ty stmt)
376{
377 int i;
378 switch (stmt->kind) {
379 case FunctionDef_kind:
380 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
381 validate_arguments(stmt->v.FunctionDef.args) &&
382 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
383 (!stmt->v.FunctionDef.returns ||
384 validate_expr(stmt->v.FunctionDef.returns, Load));
385 case ClassDef_kind:
386 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
387 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
388 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400389 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500390 case Return_kind:
391 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
392 case Delete_kind:
393 return validate_assignlist(stmt->v.Delete.targets, Del);
394 case Assign_kind:
395 return validate_assignlist(stmt->v.Assign.targets, Store) &&
396 validate_expr(stmt->v.Assign.value, Load);
397 case AugAssign_kind:
398 return validate_expr(stmt->v.AugAssign.target, Store) &&
399 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700400 case AnnAssign_kind:
401 if (stmt->v.AnnAssign.target->kind != Name_kind &&
402 stmt->v.AnnAssign.simple) {
403 PyErr_SetString(PyExc_TypeError,
404 "AnnAssign with simple non-Name target");
405 return 0;
406 }
407 return validate_expr(stmt->v.AnnAssign.target, Store) &&
408 (!stmt->v.AnnAssign.value ||
409 validate_expr(stmt->v.AnnAssign.value, Load)) &&
410 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500411 case For_kind:
412 return validate_expr(stmt->v.For.target, Store) &&
413 validate_expr(stmt->v.For.iter, Load) &&
414 validate_body(stmt->v.For.body, "For") &&
415 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400416 case AsyncFor_kind:
417 return validate_expr(stmt->v.AsyncFor.target, Store) &&
418 validate_expr(stmt->v.AsyncFor.iter, Load) &&
419 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
420 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500421 case While_kind:
422 return validate_expr(stmt->v.While.test, Load) &&
423 validate_body(stmt->v.While.body, "While") &&
424 validate_stmts(stmt->v.While.orelse);
425 case If_kind:
426 return validate_expr(stmt->v.If.test, Load) &&
427 validate_body(stmt->v.If.body, "If") &&
428 validate_stmts(stmt->v.If.orelse);
429 case With_kind:
430 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
431 return 0;
432 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
433 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
434 if (!validate_expr(item->context_expr, Load) ||
435 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
436 return 0;
437 }
438 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400439 case AsyncWith_kind:
440 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
441 return 0;
442 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
443 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
444 if (!validate_expr(item->context_expr, Load) ||
445 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
446 return 0;
447 }
448 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500449 case Raise_kind:
450 if (stmt->v.Raise.exc) {
451 return validate_expr(stmt->v.Raise.exc, Load) &&
452 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
453 }
454 if (stmt->v.Raise.cause) {
455 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
456 return 0;
457 }
458 return 1;
459 case Try_kind:
460 if (!validate_body(stmt->v.Try.body, "Try"))
461 return 0;
462 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
463 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
464 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
465 return 0;
466 }
467 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
468 asdl_seq_LEN(stmt->v.Try.orelse)) {
469 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
470 return 0;
471 }
472 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
473 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
474 if ((handler->v.ExceptHandler.type &&
475 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
476 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
477 return 0;
478 }
479 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
480 validate_stmts(stmt->v.Try.finalbody)) &&
481 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
482 validate_stmts(stmt->v.Try.orelse));
483 case Assert_kind:
484 return validate_expr(stmt->v.Assert.test, Load) &&
485 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
486 case Import_kind:
487 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
488 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300489 if (stmt->v.ImportFrom.level < 0) {
490 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500491 return 0;
492 }
493 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
494 case Global_kind:
495 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
496 case Nonlocal_kind:
497 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
498 case Expr_kind:
499 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400500 case AsyncFunctionDef_kind:
501 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
502 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
503 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
504 (!stmt->v.AsyncFunctionDef.returns ||
505 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500506 case Pass_kind:
507 case Break_kind:
508 case Continue_kind:
509 return 1;
510 default:
511 PyErr_SetString(PyExc_SystemError, "unexpected statement");
512 return 0;
513 }
514}
515
516static int
517validate_stmts(asdl_seq *seq)
518{
519 int i;
520 for (i = 0; i < asdl_seq_LEN(seq); i++) {
521 stmt_ty stmt = asdl_seq_GET(seq, i);
522 if (stmt) {
523 if (!validate_stmt(stmt))
524 return 0;
525 }
526 else {
527 PyErr_SetString(PyExc_ValueError,
528 "None disallowed in statement list");
529 return 0;
530 }
531 }
532 return 1;
533}
534
535static int
536validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
537{
538 int i;
539 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
540 expr_ty expr = asdl_seq_GET(exprs, i);
541 if (expr) {
542 if (!validate_expr(expr, ctx))
543 return 0;
544 }
545 else if (!null_ok) {
546 PyErr_SetString(PyExc_ValueError,
547 "None disallowed in expression list");
548 return 0;
549 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100550
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500551 }
552 return 1;
553}
554
555int
556PyAST_Validate(mod_ty mod)
557{
558 int res = 0;
559
560 switch (mod->kind) {
561 case Module_kind:
562 res = validate_stmts(mod->v.Module.body);
563 break;
564 case Interactive_kind:
565 res = validate_stmts(mod->v.Interactive.body);
566 break;
567 case Expression_kind:
568 res = validate_expr(mod->v.Expression.body, Load);
569 break;
570 case Suite_kind:
571 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
572 break;
573 default:
574 PyErr_SetString(PyExc_SystemError, "impossible module node");
575 res = 0;
576 break;
577 }
578 return res;
579}
580
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500581/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500582#include "grammar.h"
583#include "parsetok.h"
584#include "graminit.h"
585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586/* Data structure used internally */
587struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400588 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200589 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500590 PyObject *c_normalize; /* Normalization function from unicodedata. */
591 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592};
593
594static asdl_seq *seq_for_testlist(struct compiling *, const node *);
595static expr_ty ast_for_expr(struct compiling *, const node *);
596static stmt_ty ast_for_stmt(struct compiling *, const node *);
597static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000598static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
599 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000600static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000601static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Yury Selivanov75445082015-05-11 22:57:16 -0400603static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
604static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
605
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606/* Note different signature for ast_for_call */
607static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
608
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000609static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400610static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Nick Coghlan650f0d02007-04-15 12:05:43 +0000612#define COMP_GENEXP 0
613#define COMP_LISTCOMP 1
614#define COMP_SETCOMP 2
615
Benjamin Peterson55e00432012-01-16 17:22:31 -0500616static int
617init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000618{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500619 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
620 if (!m)
621 return 0;
622 c->c_normalize = PyObject_GetAttrString(m, "normalize");
623 Py_DECREF(m);
624 if (!c->c_normalize)
625 return 0;
626 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500627 if (!c->c_normalize_args) {
628 Py_CLEAR(c->c_normalize);
629 return 0;
630 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200631 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500632 return 1;
633}
634
635static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400636new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500637{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400638 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500639 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000640 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500641 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500642 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000643 /* Check whether there are non-ASCII characters in the
644 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500645 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200646 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500647 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500648 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200649 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500650 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500651 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
652 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500653 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 if (!id2)
655 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200656 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000657 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000658 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200659 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
660 Py_DECREF(id);
661 return NULL;
662 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000663 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664}
665
Benjamin Peterson55e00432012-01-16 17:22:31 -0500666#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400669ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400671 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
Victor Stinner14e461d2013-08-26 22:28:21 +0200673 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000675 Py_INCREF(Py_None);
676 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200678 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 if (!tmp)
680 return 0;
681 errstr = PyUnicode_FromString(errmsg);
682 if (!errstr) {
683 Py_DECREF(tmp);
684 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000685 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 Py_DECREF(errstr);
688 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400689 if (value) {
690 PyErr_SetObject(PyExc_SyntaxError, value);
691 Py_DECREF(value);
692 }
693 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694}
695
696/* num_stmts() returns number of contained statements.
697
698 Use this routine to determine how big a sequence is needed for
699 the statements in a parse tree. Its raison d'etre is this bit of
700 grammar:
701
702 stmt: simple_stmt | compound_stmt
703 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
704
705 A simple_stmt can contain multiple small_stmt elements joined
706 by semicolons. If the arg is a simple_stmt, the number of
707 small_stmt elements is returned.
708*/
709
710static int
711num_stmts(const node *n)
712{
713 int i, l;
714 node *ch;
715
716 switch (TYPE(n)) {
717 case single_input:
718 if (TYPE(CHILD(n, 0)) == NEWLINE)
719 return 0;
720 else
721 return num_stmts(CHILD(n, 0));
722 case file_input:
723 l = 0;
724 for (i = 0; i < NCH(n); i++) {
725 ch = CHILD(n, i);
726 if (TYPE(ch) == stmt)
727 l += num_stmts(ch);
728 }
729 return l;
730 case stmt:
731 return num_stmts(CHILD(n, 0));
732 case compound_stmt:
733 return 1;
734 case simple_stmt:
735 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
736 case suite:
737 if (NCH(n) == 1)
738 return num_stmts(CHILD(n, 0));
739 else {
740 l = 0;
741 for (i = 2; i < (NCH(n) - 1); i++)
742 l += num_stmts(CHILD(n, i));
743 return l;
744 }
745 default: {
746 char buf[128];
747
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000748 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 TYPE(n), NCH(n));
750 Py_FatalError(buf);
751 }
752 }
753 assert(0);
754 return 0;
755}
756
757/* Transform the CST rooted at node * to the appropriate AST
758*/
759
760mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200761PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
762 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000764 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 asdl_seq *stmts = NULL;
766 stmt_ty s;
767 node *ch;
768 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500769 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400771 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200772 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400773 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800774 c.c_normalize = NULL;
775 c.c_normalize_args = NULL;
776
777 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Jeremy Hyltona8293132006-02-28 17:58:27 +0000780 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 switch (TYPE(n)) {
782 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200783 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500785 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 for (i = 0; i < NCH(n) - 1; i++) {
787 ch = CHILD(n, i);
788 if (TYPE(ch) == NEWLINE)
789 continue;
790 REQ(ch, stmt);
791 num = num_stmts(ch);
792 if (num == 1) {
793 s = ast_for_stmt(&c, ch);
794 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500795 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000796 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 }
798 else {
799 ch = CHILD(ch, 0);
800 REQ(ch, simple_stmt);
801 for (j = 0; j < num; j++) {
802 s = ast_for_stmt(&c, CHILD(ch, j * 2));
803 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500804 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000805 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
807 }
808 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 res = Module(stmts, arena);
810 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 case eval_input: {
812 expr_ty testlist_ast;
813
Nick Coghlan650f0d02007-04-15 12:05:43 +0000814 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000815 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
818 res = Expression(testlist_ast, arena);
819 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 case single_input:
822 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200823 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500825 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
827 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000828 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500829 goto out;
830 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 }
832 else {
833 n = CHILD(n, 0);
834 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200835 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500837 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000839 s = ast_for_stmt(&c, n);
840 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500841 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 asdl_seq_SET(stmts, 0, s);
843 }
844 else {
845 /* Only a simple_stmt can contain multiple statements. */
846 REQ(n, simple_stmt);
847 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (TYPE(CHILD(n, i)) == NEWLINE)
849 break;
850 s = ast_for_stmt(&c, CHILD(n, i));
851 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500852 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 asdl_seq_SET(stmts, i / 2, s);
854 }
855 }
856
Benjamin Peterson55e00432012-01-16 17:22:31 -0500857 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500859 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000861 PyErr_Format(PyExc_SystemError,
862 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 out:
866 if (c.c_normalize) {
867 Py_DECREF(c.c_normalize);
868 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
869 Py_DECREF(c.c_normalize_args);
870 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872}
873
Victor Stinner14e461d2013-08-26 22:28:21 +0200874mod_ty
875PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
876 PyArena *arena)
877{
878 mod_ty mod;
879 PyObject *filename;
880 filename = PyUnicode_DecodeFSDefault(filename_str);
881 if (filename == NULL)
882 return NULL;
883 mod = PyAST_FromNodeObject(n, flags, filename, arena);
884 Py_DECREF(filename);
885 return mod;
886
887}
888
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
890*/
891
892static operator_ty
893get_operator(const node *n)
894{
895 switch (TYPE(n)) {
896 case VBAR:
897 return BitOr;
898 case CIRCUMFLEX:
899 return BitXor;
900 case AMPER:
901 return BitAnd;
902 case LEFTSHIFT:
903 return LShift;
904 case RIGHTSHIFT:
905 return RShift;
906 case PLUS:
907 return Add;
908 case MINUS:
909 return Sub;
910 case STAR:
911 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400912 case AT:
913 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 case SLASH:
915 return Div;
916 case DOUBLESLASH:
917 return FloorDiv;
918 case PERCENT:
919 return Mod;
920 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 }
923}
924
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200925static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000926 "None",
927 "True",
928 "False",
929 NULL,
930};
931
932static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400933forbidden_name(struct compiling *c, identifier name, const node *n,
934 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000935{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000936 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000937 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400938 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000939 return 1;
940 }
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400941 if (PyUnicode_CompareWithASCIIString(name, "async") == 0 ||
942 PyUnicode_CompareWithASCIIString(name, "await") == 0)
943 {
944 PyObject *message = PyUnicode_FromString(
945 "'async' and 'await' will become reserved keywords"
946 " in Python 3.7");
947 if (message == NULL) {
948 return 1;
949 }
950 if (PyErr_WarnExplicitObject(
951 PyExc_DeprecationWarning,
952 message,
953 c->c_filename,
954 LINENO(n),
955 NULL,
956 NULL) < 0)
957 {
958 return 1;
959 }
960 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000961 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200962 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000963 for (p = FORBIDDEN; *p; p++) {
964 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400965 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000966 return 1;
967 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000968 }
969 }
970 return 0;
971}
972
Jeremy Hyltona8293132006-02-28 17:58:27 +0000973/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974
975 Only sets context for expr kinds that "can appear in assignment context"
976 (according to ../Parser/Python.asdl). For other expr kinds, it sets
977 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978*/
979
980static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000981set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982{
983 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000984 /* If a particular expression type can't be used for assign / delete,
985 set expr_name to its name and an error message will be generated.
986 */
987 const char* expr_name = NULL;
988
989 /* The ast defines augmented store and load contexts, but the
990 implementation here doesn't actually use them. The code may be
991 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000992 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000993 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000994 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000995 */
996 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
998 switch (e->kind) {
999 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001000 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001001 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001002 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001003 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001005 e->v.Subscript.ctx = ctx;
1006 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001007 case Starred_kind:
1008 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001009 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001010 return 0;
1011 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001013 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001014 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001015 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001016 }
1017 e->v.Name.ctx = ctx;
1018 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001020 e->v.List.ctx = ctx;
1021 s = e->v.List.elts;
1022 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001024 e->v.Tuple.ctx = ctx;
1025 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001026 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001027 case Lambda_kind:
1028 expr_name = "lambda";
1029 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001031 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001032 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001033 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001035 case UnaryOp_kind:
1036 expr_name = "operator";
1037 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001039 expr_name = "generator expression";
1040 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001041 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001042 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001043 expr_name = "yield expression";
1044 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001045 case Await_kind:
1046 expr_name = "await expression";
1047 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001048 case ListComp_kind:
1049 expr_name = "list comprehension";
1050 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001051 case SetComp_kind:
1052 expr_name = "set comprehension";
1053 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001054 case DictComp_kind:
1055 expr_name = "dict comprehension";
1056 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001057 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001058 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 case Num_kind:
1060 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001061 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001062 case JoinedStr_kind:
1063 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001064 expr_name = "literal";
1065 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001066 case NameConstant_kind:
1067 expr_name = "keyword";
1068 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001069 case Ellipsis_kind:
1070 expr_name = "Ellipsis";
1071 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001072 case Compare_kind:
1073 expr_name = "comparison";
1074 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001075 case IfExp_kind:
1076 expr_name = "conditional expression";
1077 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001078 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 PyErr_Format(PyExc_SystemError,
1080 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001081 e->kind, e->lineno);
1082 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001084 /* Check for error string set by switch */
1085 if (expr_name) {
1086 char buf[300];
1087 PyOS_snprintf(buf, sizeof(buf),
1088 "can't %s %s",
1089 ctx == Store ? "assign to" : "delete",
1090 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001091 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001092 }
1093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 */
1097 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001098 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099
Thomas Wouters89f507f2006-12-13 04:49:30 +00001100 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001101 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001102 return 0;
1103 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 }
1105 return 1;
1106}
1107
1108static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001109ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110{
1111 REQ(n, augassign);
1112 n = CHILD(n, 0);
1113 switch (STR(n)[0]) {
1114 case '+':
1115 return Add;
1116 case '-':
1117 return Sub;
1118 case '/':
1119 if (STR(n)[1] == '/')
1120 return FloorDiv;
1121 else
1122 return Div;
1123 case '%':
1124 return Mod;
1125 case '<':
1126 return LShift;
1127 case '>':
1128 return RShift;
1129 case '&':
1130 return BitAnd;
1131 case '^':
1132 return BitXor;
1133 case '|':
1134 return BitOr;
1135 case '*':
1136 if (STR(n)[1] == '*')
1137 return Pow;
1138 else
1139 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001140 case '@':
1141 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001143 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 }
1146}
1147
1148static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001149ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001151 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 |'is' 'not'
1153 */
1154 REQ(n, comp_op);
1155 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001156 n = CHILD(n, 0);
1157 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 case LESS:
1159 return Lt;
1160 case GREATER:
1161 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 return Eq;
1164 case LESSEQUAL:
1165 return LtE;
1166 case GREATEREQUAL:
1167 return GtE;
1168 case NOTEQUAL:
1169 return NotEq;
1170 case NAME:
1171 if (strcmp(STR(n), "in") == 0)
1172 return In;
1173 if (strcmp(STR(n), "is") == 0)
1174 return Is;
1175 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001176 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001178 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001179 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 }
1181 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001182 /* handle "not in" and "is not" */
1183 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 case NAME:
1185 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1186 return NotIn;
1187 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1188 return IsNot;
1189 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001190 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001192 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 }
Neal Norwitz79792652005-11-14 04:25:03 +00001195 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198}
1199
1200static asdl_seq *
1201seq_for_testlist(struct compiling *c, const node *n)
1202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001204 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1205 */
Armin Rigo31441302005-10-21 12:57:31 +00001206 asdl_seq *seq;
1207 expr_ty expression;
1208 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001209 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001211 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 if (!seq)
1213 return NULL;
1214
1215 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001217 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218
Benjamin Peterson4905e802009-09-27 02:43:28 +00001219 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001220 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222
1223 assert(i / 2 < seq->size);
1224 asdl_seq_SET(seq, i / 2, expression);
1225 }
1226 return seq;
1227}
1228
Neal Norwitzc1505362006-12-28 06:47:50 +00001229static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001230ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001231{
1232 identifier name;
1233 expr_ty annotation = NULL;
1234 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001235 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001236
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001237 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001238 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001239 name = NEW_IDENTIFIER(ch);
1240 if (!name)
1241 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001242 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001243 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001244
1245 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1246 annotation = ast_for_expr(c, CHILD(n, 2));
1247 if (!annotation)
1248 return NULL;
1249 }
1250
Victor Stinnerc106c682015-11-06 17:01:48 +01001251 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001252 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001253 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001254 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255}
1256
Guido van Rossum4f72a782006-10-27 23:31:49 +00001257/* returns -1 if failed to handle keyword only arguments
1258 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001259 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001260 ^^^
1261 start pointing here
1262 */
1263static int
1264handle_keywordonly_args(struct compiling *c, const node *n, int start,
1265 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1266{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001267 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001268 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001269 expr_ty expression, annotation;
1270 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001271 int i = start;
1272 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001273
1274 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001275 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001276 return -1;
1277 }
1278 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001279 while (i < NCH(n)) {
1280 ch = CHILD(n, i);
1281 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001282 case vfpdef:
1283 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001285 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001286 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001287 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001288 asdl_seq_SET(kwdefaults, j, expression);
1289 i += 2; /* '=' and test */
1290 }
1291 else { /* setting NULL if no default value exists */
1292 asdl_seq_SET(kwdefaults, j, NULL);
1293 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001294 if (NCH(ch) == 3) {
1295 /* ch is NAME ':' test */
1296 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001297 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001298 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001299 }
1300 else {
1301 annotation = NULL;
1302 }
1303 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001304 argname = NEW_IDENTIFIER(ch);
1305 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001307 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001308 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001309 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1310 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001311 if (!arg)
1312 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001314 i += 2; /* the name and the comma */
1315 break;
1316 case DOUBLESTAR:
1317 return i;
1318 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001319 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 goto error;
1321 }
1322 }
1323 return i;
1324 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001326}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327
Jeremy Hyltona8293132006-02-28 17:58:27 +00001328/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329
1330static arguments_ty
1331ast_for_arguments(struct compiling *c, const node *n)
1332{
Neal Norwitzc1505362006-12-28 06:47:50 +00001333 /* This function handles both typedargslist (function definition)
1334 and varargslist (lambda definition).
1335
1336 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001337 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1338 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1339 | '**' tfpdef [',']]]
1340 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1341 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001342 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001343 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1344 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1345 | '**' vfpdef [',']]]
1346 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1347 | '**' vfpdef [',']
1348 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001350
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001352 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1353 int nposdefaults = 0, found_default = 0;
1354 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001355 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001356 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 node *ch;
1358
1359 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001360 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001361 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001364 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365
Jeremy Hyltone921e022008-07-17 16:37:17 +00001366 /* First count the number of positional args & defaults. The
1367 variable i is the loop index for this for loop and the next.
1368 The next loop picks up where the first leaves off.
1369 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 ch = CHILD(n, i);
1372 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001373 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001374 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001375 if (i < NCH(n) && /* skip argument following star */
1376 (TYPE(CHILD(n, i)) == tfpdef ||
1377 TYPE(CHILD(n, i)) == vfpdef)) {
1378 i++;
1379 }
1380 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001382 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001383 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001387 defaults for keyword only args */
1388 for ( ; i < NCH(n); ++i) {
1389 ch = CHILD(n, i);
1390 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001391 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001393 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001395 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001397 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001399 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001401 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001403 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 since we set NULL as default for keyword only argument w/o default
1406 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001407 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001408 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001409 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001410 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411
1412 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001413 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001414 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001417 /* tfpdef: NAME [':' test]
1418 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 */
1420 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001421 j = 0; /* index for defaults */
1422 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 ch = CHILD(n, i);
1425 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001426 case tfpdef:
1427 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1429 anything other than EQUAL or a comma? */
1430 /* XXX Should NCH(n) check be made a separate check? */
1431 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001432 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1433 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001434 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001435 assert(posdefaults != NULL);
1436 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001438 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001440 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001441 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001442 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001443 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001444 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001445 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001446 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001447 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001448 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 i += 2; /* the name and the comma */
1450 break;
1451 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001452 if (i+1 >= NCH(n) ||
1453 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001454 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001455 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001456 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001457 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001458 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001459 if (TYPE(ch) == COMMA) {
1460 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001461 i += 2; /* now follows keyword only arguments */
1462 res = handle_keywordonly_args(c, n, i,
1463 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001464 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001465 i = res; /* res has new position to process */
1466 }
1467 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001468 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001469 if (!vararg)
1470 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001471
Guido van Rossum4f72a782006-10-27 23:31:49 +00001472 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001473 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1474 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001475 int res = 0;
1476 res = handle_keywordonly_args(c, n, i,
1477 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001478 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001479 i = res; /* res has new position to process */
1480 }
1481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 break;
1483 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001484 ch = CHILD(n, i+1); /* tfpdef */
1485 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001486 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001487 if (!kwarg)
1488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 i += 3;
1490 break;
1491 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001492 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 "unexpected node in varargslist: %d @ %d",
1494 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001495 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001498 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499}
1500
1501static expr_ty
1502ast_for_dotted_name(struct compiling *c, const node *n)
1503{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001504 expr_ty e;
1505 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001506 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 int i;
1508
1509 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001510
1511 lineno = LINENO(n);
1512 col_offset = n->n_col_offset;
1513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 id = NEW_IDENTIFIER(CHILD(n, 0));
1515 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001516 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001517 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001519 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
1521 for (i = 2; i < NCH(n); i+=2) {
1522 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001523 if (!id)
1524 return NULL;
1525 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1526 if (!e)
1527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 }
1529
1530 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531}
1532
1533static expr_ty
1534ast_for_decorator(struct compiling *c, const node *n)
1535{
1536 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1537 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001538 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001541 REQ(CHILD(n, 0), AT);
1542 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1545 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001546 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001549 d = name_expr;
1550 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 }
1552 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001553 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001554 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001555 if (!d)
1556 return NULL;
1557 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 }
1559 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001560 d = ast_for_call(c, CHILD(n, 3), name_expr);
1561 if (!d)
1562 return NULL;
1563 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 }
1565
1566 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567}
1568
1569static asdl_seq*
1570ast_for_decorators(struct compiling *c, const node *n)
1571{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001572 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001573 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001577 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 if (!decorator_seq)
1579 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001582 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001583 if (!d)
1584 return NULL;
1585 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 }
1587 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588}
1589
1590static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001591ast_for_funcdef_impl(struct compiling *c, const node *n,
1592 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001594 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001595 identifier name;
1596 arguments_ty args;
1597 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001598 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001599 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600
1601 REQ(n, funcdef);
1602
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 name = NEW_IDENTIFIER(CHILD(n, name_i));
1604 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001605 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001606 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1609 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001610 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001611 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1612 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1613 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001614 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001615 name_i += 2;
1616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 body = ast_for_suite(c, CHILD(n, name_i + 3));
1618 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620
Yury Selivanov75445082015-05-11 22:57:16 -04001621 if (is_async)
1622 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1623 LINENO(n),
1624 n->n_col_offset, c->c_arena);
1625 else
1626 return FunctionDef(name, args, body, decorator_seq, returns,
1627 LINENO(n),
1628 n->n_col_offset, c->c_arena);
1629}
1630
1631static stmt_ty
1632ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1633{
1634 /* async_funcdef: ASYNC funcdef */
1635 REQ(n, async_funcdef);
1636 REQ(CHILD(n, 0), ASYNC);
1637 REQ(CHILD(n, 1), funcdef);
1638
1639 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1640 1 /* is_async */);
1641}
1642
1643static stmt_ty
1644ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1645{
1646 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1647 return ast_for_funcdef_impl(c, n, decorator_seq,
1648 0 /* is_async */);
1649}
1650
1651
1652static stmt_ty
1653ast_for_async_stmt(struct compiling *c, const node *n)
1654{
1655 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1656 REQ(n, async_stmt);
1657 REQ(CHILD(n, 0), ASYNC);
1658
1659 switch (TYPE(CHILD(n, 1))) {
1660 case funcdef:
1661 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1662 1 /* is_async */);
1663 case with_stmt:
1664 return ast_for_with_stmt(c, CHILD(n, 1),
1665 1 /* is_async */);
1666
1667 case for_stmt:
1668 return ast_for_for_stmt(c, CHILD(n, 1),
1669 1 /* is_async */);
1670
1671 default:
1672 PyErr_Format(PyExc_SystemError,
1673 "invalid async stament: %s",
1674 STR(CHILD(n, 1)));
1675 return NULL;
1676 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677}
1678
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001679static stmt_ty
1680ast_for_decorated(struct compiling *c, const node *n)
1681{
Yury Selivanov75445082015-05-11 22:57:16 -04001682 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001683 stmt_ty thing = NULL;
1684 asdl_seq *decorator_seq = NULL;
1685
1686 REQ(n, decorated);
1687
1688 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1689 if (!decorator_seq)
1690 return NULL;
1691
1692 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001693 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001695
1696 if (TYPE(CHILD(n, 1)) == funcdef) {
1697 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1698 } else if (TYPE(CHILD(n, 1)) == classdef) {
1699 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001700 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1701 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001702 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001703 /* we count the decorators in when talking about the class' or
1704 * function's line number */
1705 if (thing) {
1706 thing->lineno = LINENO(n);
1707 thing->col_offset = n->n_col_offset;
1708 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001709 return thing;
1710}
1711
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712static expr_ty
1713ast_for_lambdef(struct compiling *c, const node *n)
1714{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001715 /* lambdef: 'lambda' [varargslist] ':' test
1716 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 arguments_ty args;
1718 expr_ty expression;
1719
1720 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001721 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 if (!args)
1723 return NULL;
1724 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001725 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727 }
1728 else {
1729 args = ast_for_arguments(c, CHILD(n, 1));
1730 if (!args)
1731 return NULL;
1732 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001733 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 }
1736
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001737 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738}
1739
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001740static expr_ty
1741ast_for_ifexpr(struct compiling *c, const node *n)
1742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001744 expr_ty expression, body, orelse;
1745
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001746 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001747 body = ast_for_expr(c, CHILD(n, 0));
1748 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001749 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001750 expression = ast_for_expr(c, CHILD(n, 2));
1751 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001752 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001753 orelse = ast_for_expr(c, CHILD(n, 4));
1754 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001755 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001756 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1757 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001758}
1759
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001761 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762
Nick Coghlan650f0d02007-04-15 12:05:43 +00001763 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764*/
1765
1766static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001767count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001769 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001770 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771
Guido van Rossumd8faa362007-04-27 19:54:29 +00001772 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001773 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001774 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001775 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001776 if (TYPE(CHILD(n, 0)) == ASYNC) {
1777 is_async = 1;
1778 }
1779 if (NCH(n) == (5 + is_async)) {
1780 n = CHILD(n, 4 + is_async);
1781 }
1782 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001783 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001784 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001785 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001786 REQ(n, comp_iter);
1787 n = CHILD(n, 0);
1788 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001789 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001790 else if (TYPE(n) == comp_if) {
1791 if (NCH(n) == 3) {
1792 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001793 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001794 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001795 else
1796 return n_fors;
1797 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001798
Guido van Rossumd8faa362007-04-27 19:54:29 +00001799 /* Should never be reached */
1800 PyErr_SetString(PyExc_SystemError,
1801 "logic error in count_comp_fors");
1802 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803}
1804
Nick Coghlan650f0d02007-04-15 12:05:43 +00001805/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806
Nick Coghlan650f0d02007-04-15 12:05:43 +00001807 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808*/
1809
1810static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001811count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001813 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814
Guido van Rossumd8faa362007-04-27 19:54:29 +00001815 while (1) {
1816 REQ(n, comp_iter);
1817 if (TYPE(CHILD(n, 0)) == comp_for)
1818 return n_ifs;
1819 n = CHILD(n, 0);
1820 REQ(n, comp_if);
1821 n_ifs++;
1822 if (NCH(n) == 2)
1823 return n_ifs;
1824 n = CHILD(n, 2);
1825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826}
1827
Guido van Rossum992d4a32007-07-11 13:09:30 +00001828static asdl_seq *
1829ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001832 asdl_seq *comps;
1833
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001834 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 if (n_fors == -1)
1836 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001837
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001838 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001839 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001841
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001843 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001845 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001846 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001847 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848
Guido van Rossum992d4a32007-07-11 13:09:30 +00001849 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001851 if (TYPE(CHILD(n, 0)) == ASYNC) {
1852 is_async = 1;
1853 }
1854
1855 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001856 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001857 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001859 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001862
Thomas Wouters89f507f2006-12-13 04:49:30 +00001863 /* Check the # of children rather than the length of t, since
1864 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001865 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001866 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001867 comp = comprehension(first, expression, NULL,
1868 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001870 comp = comprehension(Tuple(t, Store, first->lineno,
1871 first->col_offset, c->c_arena),
1872 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001873 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001875
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001876 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 int j, n_ifs;
1878 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001880 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001881 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001882 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001885 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001890 REQ(n, comp_iter);
1891 n = CHILD(n, 0);
1892 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893
Guido van Rossum992d4a32007-07-11 13:09:30 +00001894 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001896 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001897 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001898 if (NCH(n) == 3)
1899 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 /* on exit, must guarantee that n is a comp_for */
1902 if (TYPE(n) == comp_iter)
1903 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001904 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001906 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001908 return comps;
1909}
1910
1911static expr_ty
1912ast_for_itercomp(struct compiling *c, const node *n, int type)
1913{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001914 /* testlist_comp: (test|star_expr)
1915 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001916 expr_ty elt;
1917 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001918 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919
Guido van Rossum992d4a32007-07-11 13:09:30 +00001920 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001922 ch = CHILD(n, 0);
1923 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001924 if (!elt)
1925 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001926 if (elt->kind == Starred_kind) {
1927 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1928 return NULL;
1929 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930
Guido van Rossum992d4a32007-07-11 13:09:30 +00001931 comps = ast_for_comprehension(c, CHILD(n, 1));
1932 if (!comps)
1933 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001934
1935 if (type == COMP_GENEXP)
1936 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1937 else if (type == COMP_LISTCOMP)
1938 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1939 else if (type == COMP_SETCOMP)
1940 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1941 else
1942 /* Should never happen */
1943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944}
1945
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001946/* Fills in the key, value pair corresponding to the dict element. In case
1947 * of an unpacking, key is NULL. *i is advanced by the number of ast
1948 * elements. Iff successful, nonzero is returned.
1949 */
1950static int
1951ast_for_dictelement(struct compiling *c, const node *n, int *i,
1952 expr_ty *key, expr_ty *value)
1953{
1954 expr_ty expression;
1955 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1956 assert(NCH(n) - *i >= 2);
1957
1958 expression = ast_for_expr(c, CHILD(n, *i + 1));
1959 if (!expression)
1960 return 0;
1961 *key = NULL;
1962 *value = expression;
1963
1964 *i += 2;
1965 }
1966 else {
1967 assert(NCH(n) - *i >= 3);
1968
1969 expression = ast_for_expr(c, CHILD(n, *i));
1970 if (!expression)
1971 return 0;
1972 *key = expression;
1973
1974 REQ(CHILD(n, *i + 1), COLON);
1975
1976 expression = ast_for_expr(c, CHILD(n, *i + 2));
1977 if (!expression)
1978 return 0;
1979 *value = expression;
1980
1981 *i += 3;
1982 }
1983 return 1;
1984}
1985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001987ast_for_dictcomp(struct compiling *c, const node *n)
1988{
1989 expr_ty key, value;
1990 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001991 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001993 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001994 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001995 assert(key);
1996 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001998 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001999 if (!comps)
2000 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001
Guido van Rossum992d4a32007-07-11 13:09:30 +00002002 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2003}
2004
2005static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002006ast_for_dictdisplay(struct compiling *c, const node *n)
2007{
2008 int i;
2009 int j;
2010 int size;
2011 asdl_seq *keys, *values;
2012
2013 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2014 keys = _Py_asdl_seq_new(size, c->c_arena);
2015 if (!keys)
2016 return NULL;
2017
2018 values = _Py_asdl_seq_new(size, c->c_arena);
2019 if (!values)
2020 return NULL;
2021
2022 j = 0;
2023 for (i = 0; i < NCH(n); i++) {
2024 expr_ty key, value;
2025
2026 if (!ast_for_dictelement(c, n, &i, &key, &value))
2027 return NULL;
2028 asdl_seq_SET(keys, j, key);
2029 asdl_seq_SET(values, j, value);
2030
2031 j++;
2032 }
2033 keys->size = j;
2034 values->size = j;
2035 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2036}
2037
2038static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002039ast_for_genexp(struct compiling *c, const node *n)
2040{
2041 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002042 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002043}
2044
2045static expr_ty
2046ast_for_listcomp(struct compiling *c, const node *n)
2047{
2048 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002049 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002050}
2051
2052static expr_ty
2053ast_for_setcomp(struct compiling *c, const node *n)
2054{
2055 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002056 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002057}
2058
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002059static expr_ty
2060ast_for_setdisplay(struct compiling *c, const node *n)
2061{
2062 int i;
2063 int size;
2064 asdl_seq *elts;
2065
2066 assert(TYPE(n) == (dictorsetmaker));
2067 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2068 elts = _Py_asdl_seq_new(size, c->c_arena);
2069 if (!elts)
2070 return NULL;
2071 for (i = 0; i < NCH(n); i += 2) {
2072 expr_ty expression;
2073 expression = ast_for_expr(c, CHILD(n, i));
2074 if (!expression)
2075 return NULL;
2076 asdl_seq_SET(elts, i / 2, expression);
2077 }
2078 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2079}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002080
2081static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082ast_for_atom(struct compiling *c, const node *n)
2083{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002084 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2085 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002086 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 */
2088 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002091 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002092 PyObject *name;
2093 const char *s = STR(ch);
2094 size_t len = strlen(s);
2095 if (len >= 4 && len <= 5) {
2096 if (!strcmp(s, "None"))
2097 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2098 if (!strcmp(s, "True"))
2099 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2100 if (!strcmp(s, "False"))
2101 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2102 }
2103 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002104 if (!name)
2105 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002106 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002107 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2108 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002110 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002111 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002112 const char *errtype = NULL;
2113 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2114 errtype = "unicode error";
2115 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2116 errtype = "value error";
2117 if (errtype) {
2118 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002119 PyObject *type, *value, *tback, *errstr;
2120 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002121 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002122 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002123 char *s = _PyUnicode_AsString(errstr);
2124 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002125 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002126 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002127 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002128 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002129 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002130 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002131 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002132 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002133 Py_XDECREF(tback);
2134 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002135 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002136 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002137 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 }
2139 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002140 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002141 if (!pynum)
2142 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002143
Victor Stinner43d81952013-07-17 00:57:58 +02002144 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2145 Py_DECREF(pynum);
2146 return NULL;
2147 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002148 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 }
Georg Brandldde00282007-03-18 19:01:53 +00002150 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002151 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002153 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154
Thomas Wouters89f507f2006-12-13 04:49:30 +00002155 if (TYPE(ch) == RPAR)
2156 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157
Thomas Wouters89f507f2006-12-13 04:49:30 +00002158 if (TYPE(ch) == yield_expr)
2159 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002162 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002163 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002164
Nick Coghlan650f0d02007-04-15 12:05:43 +00002165 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168
Thomas Wouters89f507f2006-12-13 04:49:30 +00002169 if (TYPE(ch) == RSQB)
2170 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171
Nick Coghlan650f0d02007-04-15 12:05:43 +00002172 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002173 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2174 asdl_seq *elts = seq_for_testlist(c, ch);
2175 if (!elts)
2176 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002177
Thomas Wouters89f507f2006-12-13 04:49:30 +00002178 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2179 }
2180 else
2181 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002183 /* dictorsetmaker: ( ((test ':' test | '**' test)
2184 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2185 * ((test | '*' test)
2186 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002187 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002188 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002189 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002190 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002191 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002192 }
2193 else {
2194 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2195 if (NCH(ch) == 1 ||
2196 (NCH(ch) > 1 &&
2197 TYPE(CHILD(ch, 1)) == COMMA)) {
2198 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002199 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002200 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002201 else if (NCH(ch) > 1 &&
2202 TYPE(CHILD(ch, 1)) == comp_for) {
2203 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002204 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002205 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002206 else if (NCH(ch) > 3 - is_dict &&
2207 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2208 /* It's a dictionary comprehension. */
2209 if (is_dict) {
2210 ast_error(c, n, "dict unpacking cannot be used in "
2211 "dict comprehension");
2212 return NULL;
2213 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002214 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002215 }
2216 else {
2217 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002218 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002219 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002220 if (res) {
2221 res->lineno = LINENO(n);
2222 res->col_offset = n->n_col_offset;
2223 }
2224 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002225 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002228 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2229 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 }
2231}
2232
2233static slice_ty
2234ast_for_slice(struct compiling *c, const node *n)
2235{
2236 node *ch;
2237 expr_ty lower = NULL, upper = NULL, step = NULL;
2238
2239 REQ(n, subscript);
2240
2241 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002242 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 sliceop: ':' [test]
2244 */
2245 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 if (NCH(n) == 1 && TYPE(ch) == test) {
2247 /* 'step' variable hold no significance in terms of being used over
2248 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 if (!step)
2251 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252
Thomas Wouters89f507f2006-12-13 04:49:30 +00002253 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 }
2255
2256 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002257 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 if (!lower)
2259 return NULL;
2260 }
2261
2262 /* If there's an upper bound it's in the second or third position. */
2263 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002264 if (NCH(n) > 1) {
2265 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266
Thomas Wouters89f507f2006-12-13 04:49:30 +00002267 if (TYPE(n2) == test) {
2268 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 if (!upper)
2270 return NULL;
2271 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002274 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Thomas Wouters89f507f2006-12-13 04:49:30 +00002276 if (TYPE(n2) == test) {
2277 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 if (!upper)
2279 return NULL;
2280 }
2281 }
2282
2283 ch = CHILD(n, NCH(n) - 1);
2284 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002285 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002286 ch = CHILD(ch, 1);
2287 if (TYPE(ch) == test) {
2288 step = ast_for_expr(c, ch);
2289 if (!step)
2290 return NULL;
2291 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 }
2293 }
2294
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002295 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296}
2297
2298static expr_ty
2299ast_for_binop(struct compiling *c, const node *n)
2300{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002301 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002303 BinOp(BinOp(A, op, B), op, C).
2304 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305
Guido van Rossumd8faa362007-04-27 19:54:29 +00002306 int i, nops;
2307 expr_ty expr1, expr2, result;
2308 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309
Guido van Rossumd8faa362007-04-27 19:54:29 +00002310 expr1 = ast_for_expr(c, CHILD(n, 0));
2311 if (!expr1)
2312 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 expr2 = ast_for_expr(c, CHILD(n, 2));
2315 if (!expr2)
2316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317
Guido van Rossumd8faa362007-04-27 19:54:29 +00002318 newoperator = get_operator(CHILD(n, 1));
2319 if (!newoperator)
2320 return NULL;
2321
2322 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2323 c->c_arena);
2324 if (!result)
2325 return NULL;
2326
2327 nops = (NCH(n) - 1) / 2;
2328 for (i = 1; i < nops; i++) {
2329 expr_ty tmp_result, tmp;
2330 const node* next_oper = CHILD(n, i * 2 + 1);
2331
2332 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002333 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 return NULL;
2335
Guido van Rossumd8faa362007-04-27 19:54:29 +00002336 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2337 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338 return NULL;
2339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002341 LINENO(next_oper), next_oper->n_col_offset,
2342 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002344 return NULL;
2345 result = tmp_result;
2346 }
2347 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348}
2349
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002350static expr_ty
2351ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002354 subscriptlist: subscript (',' subscript)* [',']
2355 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2356 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002357 REQ(n, trailer);
2358 if (TYPE(CHILD(n, 0)) == LPAR) {
2359 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002360 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002361 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002362 else
2363 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002365 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002366 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2367 if (!attr_id)
2368 return NULL;
2369 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002370 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002371 }
2372 else {
2373 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002374 REQ(CHILD(n, 2), RSQB);
2375 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002376 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002377 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2378 if (!slc)
2379 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002380 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2381 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002382 }
2383 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002385 by treating the sequence as a tuple literal if there are
2386 no slice features.
2387 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002388 int j;
2389 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002390 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002391 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002392 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002393 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002394 if (!slices)
2395 return NULL;
2396 for (j = 0; j < NCH(n); j += 2) {
2397 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002398 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002399 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002400 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002401 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002402 asdl_seq_SET(slices, j / 2, slc);
2403 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002404 if (!simple) {
2405 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002406 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002407 }
2408 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002409 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002410 if (!elts)
2411 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002412 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2413 slc = (slice_ty)asdl_seq_GET(slices, j);
2414 assert(slc->kind == Index_kind && slc->v.Index.value);
2415 asdl_seq_SET(elts, j, slc->v.Index.value);
2416 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002417 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002418 if (!e)
2419 return NULL;
2420 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002421 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002422 }
2423 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002424}
2425
2426static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002427ast_for_factor(struct compiling *c, const node *n)
2428{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002429 expr_ty expression;
2430
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002431 expression = ast_for_expr(c, CHILD(n, 1));
2432 if (!expression)
2433 return NULL;
2434
2435 switch (TYPE(CHILD(n, 0))) {
2436 case PLUS:
2437 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2438 c->c_arena);
2439 case MINUS:
2440 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2441 c->c_arena);
2442 case TILDE:
2443 return UnaryOp(Invert, expression, LINENO(n),
2444 n->n_col_offset, c->c_arena);
2445 }
2446 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2447 TYPE(CHILD(n, 0)));
2448 return NULL;
2449}
2450
2451static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002452ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002453{
Yury Selivanov75445082015-05-11 22:57:16 -04002454 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002455 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002456
2457 REQ(n, atom_expr);
2458 nch = NCH(n);
2459
2460 if (TYPE(CHILD(n, 0)) == AWAIT) {
2461 start = 1;
2462 assert(nch > 1);
2463 }
2464
2465 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002466 if (!e)
2467 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002468 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002469 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002470 if (start && nch == 2) {
2471 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2472 }
2473
2474 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002475 node *ch = CHILD(n, i);
2476 if (TYPE(ch) != trailer)
2477 break;
2478 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002479 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002480 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002481 tmp->lineno = e->lineno;
2482 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002483 e = tmp;
2484 }
Yury Selivanov75445082015-05-11 22:57:16 -04002485
2486 if (start) {
2487 /* there was an AWAIT */
2488 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2489 }
2490 else {
2491 return e;
2492 }
2493}
2494
2495static expr_ty
2496ast_for_power(struct compiling *c, const node *n)
2497{
2498 /* power: atom trailer* ('**' factor)*
2499 */
2500 expr_ty e;
2501 REQ(n, power);
2502 e = ast_for_atom_expr(c, CHILD(n, 0));
2503 if (!e)
2504 return NULL;
2505 if (NCH(n) == 1)
2506 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002507 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2508 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002509 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002510 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002511 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002512 }
2513 return e;
2514}
2515
Guido van Rossum0368b722007-05-11 16:50:42 +00002516static expr_ty
2517ast_for_starred(struct compiling *c, const node *n)
2518{
2519 expr_ty tmp;
2520 REQ(n, star_expr);
2521
2522 tmp = ast_for_expr(c, CHILD(n, 1));
2523 if (!tmp)
2524 return NULL;
2525
2526 /* The Load context is changed later. */
2527 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2528}
2529
2530
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531/* Do not name a variable 'expr'! Will cause a compile error.
2532*/
2533
2534static expr_ty
2535ast_for_expr(struct compiling *c, const node *n)
2536{
2537 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002538 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002539 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 and_test: not_test ('and' not_test)*
2542 not_test: 'not' not_test | comparison
2543 comparison: expr (comp_op expr)*
2544 expr: xor_expr ('|' xor_expr)*
2545 xor_expr: and_expr ('^' and_expr)*
2546 and_expr: shift_expr ('&' shift_expr)*
2547 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2548 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002549 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002551 power: atom_expr ['**' factor]
2552 atom_expr: [AWAIT] atom trailer*
2553 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 */
2555
2556 asdl_seq *seq;
2557 int i;
2558
2559 loop:
2560 switch (TYPE(n)) {
2561 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002562 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002563 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002564 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002566 else if (NCH(n) > 1)
2567 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002568 /* Fallthrough */
2569 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 case and_test:
2571 if (NCH(n) == 1) {
2572 n = CHILD(n, 0);
2573 goto loop;
2574 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002575 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 if (!seq)
2577 return NULL;
2578 for (i = 0; i < NCH(n); i += 2) {
2579 expr_ty e = ast_for_expr(c, CHILD(n, i));
2580 if (!e)
2581 return NULL;
2582 asdl_seq_SET(seq, i / 2, e);
2583 }
2584 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002585 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2586 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002587 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002588 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 case not_test:
2590 if (NCH(n) == 1) {
2591 n = CHILD(n, 0);
2592 goto loop;
2593 }
2594 else {
2595 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2596 if (!expression)
2597 return NULL;
2598
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002599 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2600 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 }
2602 case comparison:
2603 if (NCH(n) == 1) {
2604 n = CHILD(n, 0);
2605 goto loop;
2606 }
2607 else {
2608 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002609 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002610 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002611 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 if (!ops)
2613 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002614 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 return NULL;
2617 }
2618 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002619 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002621 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002622 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002624 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625
2626 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002627 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002629 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002631 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 asdl_seq_SET(cmps, i / 2, expression);
2633 }
2634 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002635 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639 return Compare(expression, ops, cmps, LINENO(n),
2640 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 }
2642 break;
2643
Guido van Rossum0368b722007-05-11 16:50:42 +00002644 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 /* The next five cases all handle BinOps. The main body of code
2647 is the same in each case, but the switch turned inside out to
2648 reuse the code for each type of operator.
2649 */
2650 case expr:
2651 case xor_expr:
2652 case and_expr:
2653 case shift_expr:
2654 case arith_expr:
2655 case term:
2656 if (NCH(n) == 1) {
2657 n = CHILD(n, 0);
2658 goto loop;
2659 }
2660 return ast_for_binop(c, n);
2661 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002662 node *an = NULL;
2663 node *en = NULL;
2664 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002665 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002666 if (NCH(n) > 1)
2667 an = CHILD(n, 1); /* yield_arg */
2668 if (an) {
2669 en = CHILD(an, NCH(an) - 1);
2670 if (NCH(an) == 2) {
2671 is_from = 1;
2672 exp = ast_for_expr(c, en);
2673 }
2674 else
2675 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002676 if (!exp)
2677 return NULL;
2678 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002679 if (is_from)
2680 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2681 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002682 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002683 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 if (NCH(n) == 1) {
2685 n = CHILD(n, 0);
2686 goto loop;
2687 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002688 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002689 case power:
2690 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002692 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 return NULL;
2694 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002695 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 return NULL;
2697}
2698
2699static expr_ty
2700ast_for_call(struct compiling *c, const node *n, expr_ty func)
2701{
2702 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002703 arglist: argument (',' argument)* [',']
2704 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 */
2706
2707 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002708 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002709 asdl_seq *args;
2710 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711
2712 REQ(n, arglist);
2713
2714 nargs = 0;
2715 nkeywords = 0;
2716 ngens = 0;
2717 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002718 node *ch = CHILD(n, i);
2719 if (TYPE(ch) == argument) {
2720 if (NCH(ch) == 1)
2721 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002722 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002723 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002724 else if (TYPE(CHILD(ch, 0)) == STAR)
2725 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002727 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002728 nkeywords++;
2729 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 }
2731 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002732 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002733 "if not sole argument");
2734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 }
2736
2737 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002738 ast_error(c, n, "more than 255 arguments");
2739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 }
2741
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002742 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002744 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002745 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002747 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002748
2749 nargs = 0; /* positional arguments + iterable argument unpackings */
2750 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2751 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002753 node *ch = CHILD(n, i);
2754 if (TYPE(ch) == argument) {
2755 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002756 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002757 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002758 /* a positional argument */
2759 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 if (ndoublestars) {
2761 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002762 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002764 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002765 else {
2766 ast_error(c, chch,
2767 "positional argument follows "
2768 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002769 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002770 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002771 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002772 e = ast_for_expr(c, chch);
2773 if (!e)
2774 return NULL;
2775 asdl_seq_SET(args, nargs++, e);
2776 }
2777 else if (TYPE(chch) == STAR) {
2778 /* an iterable argument unpacking */
2779 expr_ty starred;
2780 if (ndoublestars) {
2781 ast_error(c, chch,
2782 "iterable argument unpacking follows "
2783 "keyword argument unpacking");
2784 return NULL;
2785 }
2786 e = ast_for_expr(c, CHILD(ch, 1));
2787 if (!e)
2788 return NULL;
2789 starred = Starred(e, Load, LINENO(chch),
2790 chch->n_col_offset,
2791 c->c_arena);
2792 if (!starred)
2793 return NULL;
2794 asdl_seq_SET(args, nargs++, starred);
2795
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002796 }
2797 else if (TYPE(chch) == DOUBLESTAR) {
2798 /* a keyword argument unpacking */
2799 keyword_ty kw;
2800 i++;
2801 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002803 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002804 kw = keyword(NULL, e, c->c_arena);
2805 asdl_seq_SET(keywords, nkeywords++, kw);
2806 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002808 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002809 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002810 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002812 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002813 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002816 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002818 identifier key, tmp;
2819 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002821 /* chch is test, but must be an identifier? */
2822 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 /* f(lambda x: x[0] = 3) ends up getting parsed with
2826 * LHS test = lambda x: x[0], and RHS test = 3.
2827 * SF bug 132313 points out that complaining about a keyword
2828 * then is very confusing.
2829 */
2830 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002831 ast_error(c, chch,
2832 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002833 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 }
2835 else if (e->kind != Name_kind) {
2836 ast_error(c, chch,
2837 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002838 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002839 }
2840 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002841 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002843 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002844 for (k = 0; k < nkeywords; k++) {
2845 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002846 if (tmp && !PyUnicode_Compare(tmp, key)) {
2847 ast_error(c, chch,
2848 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002849 return NULL;
2850 }
2851 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002852 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002854 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002857 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002858 asdl_seq_SET(keywords, nkeywords++, kw);
2859 }
2860 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 }
2862
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002863 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864}
2865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002867ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002869 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002870 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002872 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002873 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002874 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875 }
2876 else {
2877 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002878 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002879 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002881 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 else {
2883 asdl_seq *tmp = seq_for_testlist(c, n);
2884 if (!tmp)
2885 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002888}
2889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890static stmt_ty
2891ast_for_expr_stmt(struct compiling *c, const node *n)
2892{
2893 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002894 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2895 ('=' (yield_expr|testlist_star_expr))*)
2896 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002897 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002898 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002899 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002900 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 */
2902
2903 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002904 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 if (!e)
2906 return NULL;
2907
Thomas Wouters89f507f2006-12-13 04:49:30 +00002908 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 }
2910 else if (TYPE(CHILD(n, 1)) == augassign) {
2911 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002912 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002913 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 if (!expr1)
2917 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002918 if(!set_context(c, expr1, Store, ch))
2919 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002920 /* set_context checks that most expressions are not the left side.
2921 Augmented assignments can only have a name, a subscript, or an
2922 attribute on the left, though, so we have to explicitly check for
2923 those. */
2924 switch (expr1->kind) {
2925 case Name_kind:
2926 case Attribute_kind:
2927 case Subscript_kind:
2928 break;
2929 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002930 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002931 return NULL;
2932 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933
Thomas Wouters89f507f2006-12-13 04:49:30 +00002934 ch = CHILD(n, 2);
2935 if (TYPE(ch) == testlist)
2936 expr2 = ast_for_testlist(c, ch);
2937 else
2938 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002939 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 return NULL;
2941
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002942 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002943 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 return NULL;
2945
Thomas Wouters89f507f2006-12-13 04:49:30 +00002946 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002948 else if (TYPE(CHILD(n, 1)) == annassign) {
2949 expr_ty expr1, expr2, expr3;
2950 node *ch = CHILD(n, 0);
2951 node *deep, *ann = CHILD(n, 1);
2952 int simple = 1;
2953
2954 /* we keep track of parens to qualify (x) as expression not name */
2955 deep = ch;
2956 while (NCH(deep) == 1) {
2957 deep = CHILD(deep, 0);
2958 }
2959 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2960 simple = 0;
2961 }
2962 expr1 = ast_for_testlist(c, ch);
2963 if (!expr1) {
2964 return NULL;
2965 }
2966 switch (expr1->kind) {
2967 case Name_kind:
2968 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2969 return NULL;
2970 }
2971 expr1->v.Name.ctx = Store;
2972 break;
2973 case Attribute_kind:
2974 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2975 return NULL;
2976 }
2977 expr1->v.Attribute.ctx = Store;
2978 break;
2979 case Subscript_kind:
2980 expr1->v.Subscript.ctx = Store;
2981 break;
2982 case List_kind:
2983 ast_error(c, ch,
2984 "only single target (not list) can be annotated");
2985 return NULL;
2986 case Tuple_kind:
2987 ast_error(c, ch,
2988 "only single target (not tuple) can be annotated");
2989 return NULL;
2990 default:
2991 ast_error(c, ch,
2992 "illegal target for annotation");
2993 return NULL;
2994 }
2995
2996 if (expr1->kind != Name_kind) {
2997 simple = 0;
2998 }
2999 ch = CHILD(ann, 1);
3000 expr2 = ast_for_expr(c, ch);
3001 if (!expr2) {
3002 return NULL;
3003 }
3004 if (NCH(ann) == 2) {
3005 return AnnAssign(expr1, expr2, NULL, simple,
3006 LINENO(n), n->n_col_offset, c->c_arena);
3007 }
3008 else {
3009 ch = CHILD(ann, 3);
3010 expr3 = ast_for_expr(c, ch);
3011 if (!expr3) {
3012 return NULL;
3013 }
3014 return AnnAssign(expr1, expr2, expr3, simple,
3015 LINENO(n), n->n_col_offset, c->c_arena);
3016 }
3017 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003019 int i;
3020 asdl_seq *targets;
3021 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 expr_ty expression;
3023
Thomas Wouters89f507f2006-12-13 04:49:30 +00003024 /* a normal assignment */
3025 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003026 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003027 if (!targets)
3028 return NULL;
3029 for (i = 0; i < NCH(n) - 2; i += 2) {
3030 expr_ty e;
3031 node *ch = CHILD(n, i);
3032 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003033 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003034 return NULL;
3035 }
3036 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003040 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003041 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043
Thomas Wouters89f507f2006-12-13 04:49:30 +00003044 asdl_seq_SET(targets, i / 2, e);
3045 }
3046 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003047 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003048 expression = ast_for_testlist(c, value);
3049 else
3050 expression = ast_for_expr(c, value);
3051 if (!expression)
3052 return NULL;
3053 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055}
3056
Benjamin Peterson78565b22009-06-28 19:19:51 +00003057
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003059ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060{
3061 asdl_seq *seq;
3062 int i;
3063 expr_ty e;
3064
3065 REQ(n, exprlist);
3066
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003067 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003069 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003071 e = ast_for_expr(c, CHILD(n, i));
3072 if (!e)
3073 return NULL;
3074 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003075 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003076 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 }
3078 return seq;
3079}
3080
3081static stmt_ty
3082ast_for_del_stmt(struct compiling *c, const node *n)
3083{
3084 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 /* del_stmt: 'del' exprlist */
3087 REQ(n, del_stmt);
3088
3089 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3090 if (!expr_list)
3091 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003092 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093}
3094
3095static stmt_ty
3096ast_for_flow_stmt(struct compiling *c, const node *n)
3097{
3098 /*
3099 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3100 | yield_stmt
3101 break_stmt: 'break'
3102 continue_stmt: 'continue'
3103 return_stmt: 'return' [testlist]
3104 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003105 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 raise_stmt: 'raise' [test [',' test [',' test]]]
3107 */
3108 node *ch;
3109
3110 REQ(n, flow_stmt);
3111 ch = CHILD(n, 0);
3112 switch (TYPE(ch)) {
3113 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003114 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003116 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003118 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3119 if (!exp)
3120 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003121 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 }
3123 case return_stmt:
3124 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003125 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003127 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 if (!expression)
3129 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003130 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 }
3132 case raise_stmt:
3133 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003134 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3135 else if (NCH(ch) >= 2) {
3136 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3138 if (!expression)
3139 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003140 if (NCH(ch) == 4) {
3141 cause = ast_for_expr(c, CHILD(ch, 3));
3142 if (!cause)
3143 return NULL;
3144 }
3145 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 }
3147 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003148 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 "unexpected flow_stmt: %d", TYPE(ch));
3150 return NULL;
3151 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003152
3153 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3154 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155}
3156
3157static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003158alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159{
3160 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003161 import_as_name: NAME ['as' NAME]
3162 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 dotted_name: NAME ('.' NAME)*
3164 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003165 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003166
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 loop:
3168 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003169 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003170 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003171 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003173 if (!name)
3174 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003175 if (NCH(n) == 3) {
3176 node *str_node = CHILD(n, 2);
3177 str = NEW_IDENTIFIER(str_node);
3178 if (!str)
3179 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003180 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003181 return NULL;
3182 }
3183 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003184 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003185 return NULL;
3186 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003187 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003188 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 case dotted_as_name:
3190 if (NCH(n) == 1) {
3191 n = CHILD(n, 0);
3192 goto loop;
3193 }
3194 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003195 node *asname_node = CHILD(n, 2);
3196 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003197 if (!a)
3198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003200 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003201 if (!a->asname)
3202 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003203 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 return a;
3206 }
3207 break;
3208 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003209 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003210 node *name_node = CHILD(n, 0);
3211 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003212 if (!name)
3213 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003214 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003215 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003216 return alias(name, NULL, c->c_arena);
3217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218 else {
3219 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003220 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003221 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224
3225 len = 0;
3226 for (i = 0; i < NCH(n); i += 2)
3227 /* length of string plus one for the dot */
3228 len += strlen(STR(CHILD(n, i))) + 1;
3229 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003230 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 if (!str)
3232 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003233 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 if (!s)
3235 return NULL;
3236 for (i = 0; i < NCH(n); i += 2) {
3237 char *sch = STR(CHILD(n, i));
3238 strcpy(s, STR(CHILD(n, i)));
3239 s += strlen(sch);
3240 *s++ = '.';
3241 }
3242 --s;
3243 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3245 PyBytes_GET_SIZE(str),
3246 NULL);
3247 Py_DECREF(str);
3248 if (!uni)
3249 return NULL;
3250 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003251 PyUnicode_InternInPlace(&str);
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 }
3258 break;
3259 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003260 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003261 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3262 Py_DECREF(str);
3263 return NULL;
3264 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003265 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003267 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 "unexpected import name: %d", TYPE(n));
3269 return NULL;
3270 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003271
3272 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 return NULL;
3274}
3275
3276static stmt_ty
3277ast_for_import_stmt(struct compiling *c, const node *n)
3278{
3279 /*
3280 import_stmt: import_name | import_from
3281 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003282 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3283 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003285 int lineno;
3286 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 int i;
3288 asdl_seq *aliases;
3289
3290 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003291 lineno = LINENO(n);
3292 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003294 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003297 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 if (!aliases)
3299 return NULL;
3300 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003301 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003302 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003304 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003306 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003308 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 int idx, ndots = 0;
3311 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003312 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003314 /* Count the number of dots (for relative imports) and check for the
3315 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 for (idx = 1; idx < NCH(n); idx++) {
3317 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003318 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3319 if (!mod)
3320 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 idx++;
3322 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003323 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003325 ndots += 3;
3326 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003327 } else if (TYPE(CHILD(n, idx)) != DOT) {
3328 break;
3329 }
3330 ndots++;
3331 }
3332 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003333 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003334 case STAR:
3335 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003336 n = CHILD(n, idx);
3337 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 break;
3339 case LPAR:
3340 /* from ... import (x, y, z) */
3341 n = CHILD(n, idx + 1);
3342 n_children = NCH(n);
3343 break;
3344 case import_as_names:
3345 /* from ... import x, y, z */
3346 n = CHILD(n, idx);
3347 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003348 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003349 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350 " surrounding parentheses");
3351 return NULL;
3352 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353 break;
3354 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003355 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003356 return NULL;
3357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003359 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003360 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
3363 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003364 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003365 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003366 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003368 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003370 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003371 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003372 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003373 if (!import_alias)
3374 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003375 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003378 if (mod != NULL)
3379 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003380 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003381 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382 }
Neal Norwitz79792652005-11-14 04:25:03 +00003383 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 "unknown import statement: starts with command '%s'",
3385 STR(CHILD(n, 0)));
3386 return NULL;
3387}
3388
3389static stmt_ty
3390ast_for_global_stmt(struct compiling *c, const node *n)
3391{
3392 /* global_stmt: 'global' NAME (',' NAME)* */
3393 identifier name;
3394 asdl_seq *s;
3395 int i;
3396
3397 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003398 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003400 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402 name = NEW_IDENTIFIER(CHILD(n, i));
3403 if (!name)
3404 return NULL;
3405 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003407 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408}
3409
3410static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003411ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3412{
3413 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3414 identifier name;
3415 asdl_seq *s;
3416 int i;
3417
3418 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003419 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003420 if (!s)
3421 return NULL;
3422 for (i = 1; i < NCH(n); i += 2) {
3423 name = NEW_IDENTIFIER(CHILD(n, i));
3424 if (!name)
3425 return NULL;
3426 asdl_seq_SET(s, i / 2, name);
3427 }
3428 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3429}
3430
3431static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432ast_for_assert_stmt(struct compiling *c, const node *n)
3433{
3434 /* assert_stmt: 'assert' test [',' test] */
3435 REQ(n, assert_stmt);
3436 if (NCH(n) == 2) {
3437 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3438 if (!expression)
3439 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003440 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 }
3442 else if (NCH(n) == 4) {
3443 expr_ty expr1, expr2;
3444
3445 expr1 = ast_for_expr(c, CHILD(n, 1));
3446 if (!expr1)
3447 return NULL;
3448 expr2 = ast_for_expr(c, CHILD(n, 3));
3449 if (!expr2)
3450 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451
Thomas Wouters89f507f2006-12-13 04:49:30 +00003452 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 }
Neal Norwitz79792652005-11-14 04:25:03 +00003454 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 "improper number of parts to 'assert' statement: %d",
3456 NCH(n));
3457 return NULL;
3458}
3459
3460static asdl_seq *
3461ast_for_suite(struct compiling *c, const node *n)
3462{
3463 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003464 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465 stmt_ty s;
3466 int i, total, num, end, pos = 0;
3467 node *ch;
3468
3469 REQ(n, suite);
3470
3471 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003472 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003474 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 n = CHILD(n, 0);
3477 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003479 */
3480 end = NCH(n) - 1;
3481 if (TYPE(CHILD(n, end - 1)) == SEMI)
3482 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003484 for (i = 0; i < end; i += 2) {
3485 ch = CHILD(n, i);
3486 s = ast_for_stmt(c, ch);
3487 if (!s)
3488 return NULL;
3489 asdl_seq_SET(seq, pos++, s);
3490 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491 }
3492 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003493 for (i = 2; i < (NCH(n) - 1); i++) {
3494 ch = CHILD(n, i);
3495 REQ(ch, stmt);
3496 num = num_stmts(ch);
3497 if (num == 1) {
3498 /* small_stmt or compound_stmt with only one child */
3499 s = ast_for_stmt(c, ch);
3500 if (!s)
3501 return NULL;
3502 asdl_seq_SET(seq, pos++, s);
3503 }
3504 else {
3505 int j;
3506 ch = CHILD(ch, 0);
3507 REQ(ch, simple_stmt);
3508 for (j = 0; j < NCH(ch); j += 2) {
3509 /* statement terminates with a semi-colon ';' */
3510 if (NCH(CHILD(ch, j)) == 0) {
3511 assert((j + 1) == NCH(ch));
3512 break;
3513 }
3514 s = ast_for_stmt(c, CHILD(ch, j));
3515 if (!s)
3516 return NULL;
3517 asdl_seq_SET(seq, pos++, s);
3518 }
3519 }
3520 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521 }
3522 assert(pos == seq->size);
3523 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524}
3525
3526static stmt_ty
3527ast_for_if_stmt(struct compiling *c, const node *n)
3528{
3529 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3530 ['else' ':' suite]
3531 */
3532 char *s;
3533
3534 REQ(n, if_stmt);
3535
3536 if (NCH(n) == 4) {
3537 expr_ty expression;
3538 asdl_seq *suite_seq;
3539
3540 expression = ast_for_expr(c, CHILD(n, 1));
3541 if (!expression)
3542 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003544 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546
Guido van Rossumd8faa362007-04-27 19:54:29 +00003547 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3548 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 s = STR(CHILD(n, 4));
3552 /* s[2], the third character in the string, will be
3553 's' for el_s_e, or
3554 'i' for el_i_f
3555 */
3556 if (s[2] == 's') {
3557 expr_ty expression;
3558 asdl_seq *seq1, *seq2;
3559
3560 expression = ast_for_expr(c, CHILD(n, 1));
3561 if (!expression)
3562 return NULL;
3563 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003564 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565 return NULL;
3566 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003567 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568 return NULL;
3569
Guido van Rossumd8faa362007-04-27 19:54:29 +00003570 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3571 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572 }
3573 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003574 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003575 expr_ty expression;
3576 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003577 asdl_seq *orelse = NULL;
3578 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 /* must reference the child n_elif+1 since 'else' token is third,
3580 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003581 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3582 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3583 has_else = 1;
3584 n_elif -= 3;
3585 }
3586 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587
Thomas Wouters89f507f2006-12-13 04:49:30 +00003588 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003589 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003591 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003592 if (!orelse)
3593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003595 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003597 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3598 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003600 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3601 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 asdl_seq_SET(orelse, 0,
3605 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003606 LINENO(CHILD(n, NCH(n) - 6)),
3607 CHILD(n, NCH(n) - 6)->n_col_offset,
3608 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003609 /* the just-created orelse handled the last elif */
3610 n_elif--;
3611 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003612
Thomas Wouters89f507f2006-12-13 04:49:30 +00003613 for (i = 0; i < n_elif; i++) {
3614 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003615 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003616 if (!newobj)
3617 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003619 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003622 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624
Thomas Wouters89f507f2006-12-13 04:49:30 +00003625 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003627 LINENO(CHILD(n, off)),
3628 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003629 orelse = newobj;
3630 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003631 expression = ast_for_expr(c, CHILD(n, 1));
3632 if (!expression)
3633 return NULL;
3634 suite_seq = ast_for_suite(c, CHILD(n, 3));
3635 if (!suite_seq)
3636 return NULL;
3637 return If(expression, suite_seq, orelse,
3638 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003640
3641 PyErr_Format(PyExc_SystemError,
3642 "unexpected token in 'if' statement: %s", s);
3643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644}
3645
3646static stmt_ty
3647ast_for_while_stmt(struct compiling *c, const node *n)
3648{
3649 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3650 REQ(n, while_stmt);
3651
3652 if (NCH(n) == 4) {
3653 expr_ty expression;
3654 asdl_seq *suite_seq;
3655
3656 expression = ast_for_expr(c, CHILD(n, 1));
3657 if (!expression)
3658 return NULL;
3659 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003660 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003662 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 }
3664 else if (NCH(n) == 7) {
3665 expr_ty expression;
3666 asdl_seq *seq1, *seq2;
3667
3668 expression = ast_for_expr(c, CHILD(n, 1));
3669 if (!expression)
3670 return NULL;
3671 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003672 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 return NULL;
3674 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003675 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 return NULL;
3677
Thomas Wouters89f507f2006-12-13 04:49:30 +00003678 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003680
3681 PyErr_Format(PyExc_SystemError,
3682 "wrong number of tokens for 'while' statement: %d",
3683 NCH(n));
3684 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685}
3686
3687static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003688ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003690 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003692 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003693 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3695 REQ(n, for_stmt);
3696
3697 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003698 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 if (!seq)
3700 return NULL;
3701 }
3702
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003703 node_target = CHILD(n, 1);
3704 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003705 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003707 /* Check the # of children rather than the length of _target, since
3708 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003709 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003710 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003711 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003713 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003715 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003716 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 return NULL;
3718 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003719 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720 return NULL;
3721
Yury Selivanov75445082015-05-11 22:57:16 -04003722 if (is_async)
3723 return AsyncFor(target, expression, suite_seq, seq,
3724 LINENO(n), n->n_col_offset,
3725 c->c_arena);
3726 else
3727 return For(target, expression, suite_seq, seq,
3728 LINENO(n), n->n_col_offset,
3729 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730}
3731
3732static excepthandler_ty
3733ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3734{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003735 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736 REQ(exc, except_clause);
3737 REQ(body, suite);
3738
3739 if (NCH(exc) == 1) {
3740 asdl_seq *suite_seq = ast_for_suite(c, body);
3741 if (!suite_seq)
3742 return NULL;
3743
Neal Norwitzad74aa82008-03-31 05:14:30 +00003744 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003745 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746 }
3747 else if (NCH(exc) == 2) {
3748 expr_ty expression;
3749 asdl_seq *suite_seq;
3750
3751 expression = ast_for_expr(c, CHILD(exc, 1));
3752 if (!expression)
3753 return NULL;
3754 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003755 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756 return NULL;
3757
Neal Norwitzad74aa82008-03-31 05:14:30 +00003758 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003759 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 }
3761 else if (NCH(exc) == 4) {
3762 asdl_seq *suite_seq;
3763 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003764 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003765 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003767 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003770 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 return NULL;
3772 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003773 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 return NULL;
3775
Neal Norwitzad74aa82008-03-31 05:14:30 +00003776 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003777 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003779
3780 PyErr_Format(PyExc_SystemError,
3781 "wrong number of children for 'except' clause: %d",
3782 NCH(exc));
3783 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784}
3785
3786static stmt_ty
3787ast_for_try_stmt(struct compiling *c, const node *n)
3788{
Neal Norwitzf599f422005-12-17 21:33:47 +00003789 const int nch = NCH(n);
3790 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003791 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003792
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 REQ(n, try_stmt);
3794
Neal Norwitzf599f422005-12-17 21:33:47 +00003795 body = ast_for_suite(c, CHILD(n, 2));
3796 if (body == NULL)
3797 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798
Neal Norwitzf599f422005-12-17 21:33:47 +00003799 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3800 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3801 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3802 /* we can assume it's an "else",
3803 because nch >= 9 for try-else-finally and
3804 it would otherwise have a type of except_clause */
3805 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3806 if (orelse == NULL)
3807 return NULL;
3808 n_except--;
3809 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810
Neal Norwitzf599f422005-12-17 21:33:47 +00003811 finally = ast_for_suite(c, CHILD(n, nch - 1));
3812 if (finally == NULL)
3813 return NULL;
3814 n_except--;
3815 }
3816 else {
3817 /* we can assume it's an "else",
3818 otherwise it would have a type of except_clause */
3819 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3820 if (orelse == NULL)
3821 return NULL;
3822 n_except--;
3823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003825 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003826 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827 return NULL;
3828 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829
Neal Norwitzf599f422005-12-17 21:33:47 +00003830 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003831 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003832 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003833 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003834 if (handlers == NULL)
3835 return NULL;
3836
3837 for (i = 0; i < n_except; i++) {
3838 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3839 CHILD(n, 5 + i * 3));
3840 if (!e)
3841 return NULL;
3842 asdl_seq_SET(handlers, i, e);
3843 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003844 }
3845
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003846 assert(finally != NULL || asdl_seq_LEN(handlers));
3847 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848}
3849
Georg Brandl0c315622009-05-25 21:10:36 +00003850/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003851static withitem_ty
3852ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003853{
3854 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003855
Georg Brandl0c315622009-05-25 21:10:36 +00003856 REQ(n, with_item);
3857 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003858 if (!context_expr)
3859 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003860 if (NCH(n) == 3) {
3861 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003862
3863 if (!optional_vars) {
3864 return NULL;
3865 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003866 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003867 return NULL;
3868 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003869 }
3870
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003871 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003872}
3873
Georg Brandl0c315622009-05-25 21:10:36 +00003874/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3875static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003876ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003877{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003878 int i, n_items;
3879 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003880
3881 REQ(n, with_stmt);
3882
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003883 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003884 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003885 if (!items)
3886 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003887 for (i = 1; i < NCH(n) - 2; i += 2) {
3888 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3889 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003890 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003891 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003892 }
3893
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003894 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3895 if (!body)
3896 return NULL;
3897
Yury Selivanov75445082015-05-11 22:57:16 -04003898 if (is_async)
3899 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3900 else
3901 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003902}
3903
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003905ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003907 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003908 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003909 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003910 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003911
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003912 REQ(n, classdef);
3913
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003914 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003915 s = ast_for_suite(c, CHILD(n, 3));
3916 if (!s)
3917 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003918 classname = NEW_IDENTIFIER(CHILD(n, 1));
3919 if (!classname)
3920 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003921 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003922 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003923 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3924 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003926
3927 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003928 s = ast_for_suite(c, CHILD(n,5));
3929 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003930 return NULL;
3931 classname = NEW_IDENTIFIER(CHILD(n, 1));
3932 if (!classname)
3933 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003934 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003935 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003936 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3937 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938 }
3939
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003940 /* class NAME '(' arglist ')' ':' suite */
3941 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003942 {
3943 PyObject *dummy_name;
3944 expr_ty dummy;
3945 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3946 if (!dummy_name)
3947 return NULL;
3948 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3949 call = ast_for_call(c, CHILD(n, 3), dummy);
3950 if (!call)
3951 return NULL;
3952 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003954 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003956 classname = NEW_IDENTIFIER(CHILD(n, 1));
3957 if (!classname)
3958 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003959 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003960 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003961
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003962 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003963 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964}
3965
3966static stmt_ty
3967ast_for_stmt(struct compiling *c, const node *n)
3968{
3969 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003970 assert(NCH(n) == 1);
3971 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972 }
3973 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003974 assert(num_stmts(n) == 1);
3975 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 }
3977 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003978 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003979 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3980 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003981 */
3982 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983 case expr_stmt:
3984 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 case del_stmt:
3986 return ast_for_del_stmt(c, n);
3987 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003988 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989 case flow_stmt:
3990 return ast_for_flow_stmt(c, n);
3991 case import_stmt:
3992 return ast_for_import_stmt(c, n);
3993 case global_stmt:
3994 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003995 case nonlocal_stmt:
3996 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003997 case assert_stmt:
3998 return ast_for_assert_stmt(c, n);
3999 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004000 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4002 TYPE(n), NCH(n));
4003 return NULL;
4004 }
4005 }
4006 else {
4007 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004008 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004009 */
4010 node *ch = CHILD(n, 0);
4011 REQ(n, compound_stmt);
4012 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 case if_stmt:
4014 return ast_for_if_stmt(c, ch);
4015 case while_stmt:
4016 return ast_for_while_stmt(c, ch);
4017 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004018 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 case try_stmt:
4020 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004021 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004022 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004024 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004026 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 case decorated:
4028 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004029 case async_stmt:
4030 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004032 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4034 TYPE(n), NCH(n));
4035 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004036 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 }
4038}
4039
4040static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004041parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004043 const char *end;
4044 long x;
4045 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004046 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004049 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004050 errno = 0;
4051 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004052 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004053 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004054 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004056 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004057 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004058 }
4059 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004060 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004061 if (*end == '\0') {
4062 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004063 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004064 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004065 }
4066 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004067 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004068 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004069 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4070 if (compl.imag == -1.0 && PyErr_Occurred())
4071 return NULL;
4072 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004073 }
4074 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004075 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004076 dx = PyOS_string_to_double(s, NULL, NULL);
4077 if (dx == -1.0 && PyErr_Occurred())
4078 return NULL;
4079 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004080 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004081}
4082
4083static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004084parsenumber(struct compiling *c, const char *s)
4085{
4086 char *dup, *end;
4087 PyObject *res = NULL;
4088
4089 assert(s != NULL);
4090
4091 if (strchr(s, '_') == NULL) {
4092 return parsenumber_raw(c, s);
4093 }
4094 /* Create a duplicate without underscores. */
4095 dup = PyMem_Malloc(strlen(s) + 1);
4096 end = dup;
4097 for (; *s; s++) {
4098 if (*s != '_') {
4099 *end++ = *s;
4100 }
4101 }
4102 *end = '\0';
4103 res = parsenumber_raw(c, dup);
4104 PyMem_Free(dup);
4105 return res;
4106}
4107
4108static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004109decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004110{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004111 const char *s, *t;
4112 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004113 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4114 while (s < end && (*s & 0x80)) s++;
4115 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004116 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117}
4118
4119static PyObject *
Benjamin Peterson768921c2016-02-25 23:13:53 -08004120decode_unicode_with_escapes(struct compiling *c, const char *s, size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004121{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004122 PyObject *v, *u;
4123 char *buf;
4124 char *p;
4125 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004126
Benjamin Peterson202803a2016-02-25 22:34:45 -08004127 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004128 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004129 return NULL;
4130 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4131 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4132 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4133 if (u == NULL)
4134 return NULL;
4135 p = buf = PyBytes_AsString(u);
4136 end = s + len;
4137 while (s < end) {
4138 if (*s == '\\') {
4139 *p++ = *s++;
4140 if (*s & 0x80) {
4141 strcpy(p, "u005c");
4142 p += 5;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004143 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004144 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004145 if (*s & 0x80) { /* XXX inefficient */
4146 PyObject *w;
4147 int kind;
4148 void *data;
4149 Py_ssize_t len, i;
4150 w = decode_utf8(c, &s, end);
4151 if (w == NULL) {
4152 Py_DECREF(u);
4153 return NULL;
4154 }
4155 kind = PyUnicode_KIND(w);
4156 data = PyUnicode_DATA(w);
4157 len = PyUnicode_GET_LENGTH(w);
4158 for (i = 0; i < len; i++) {
4159 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4160 sprintf(p, "\\U%08x", chr);
4161 p += 10;
4162 }
4163 /* Should be impossible to overflow */
4164 assert(p - buf <= Py_SIZE(u));
4165 Py_DECREF(w);
4166 } else {
4167 *p++ = *s++;
4168 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004169 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004170 len = p - buf;
4171 s = buf;
4172
Eric V. Smith5567f892015-09-21 13:36:09 -04004173 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004174 Py_XDECREF(u);
4175 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004176}
4177
Eric V. Smith451d0e32016-09-09 21:56:20 -04004178/* Compile this expression in to an expr_ty. Add parens around the
4179 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004180static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004181fstring_compile_expr(const char *expr_start, const char *expr_end,
4182 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004183
Eric V. Smith235a6f02015-09-19 14:51:32 -04004184{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004185 int all_whitespace = 1;
4186 int kind;
4187 void *data;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004188 PyCompilerFlags cf;
4189 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004190 char *str;
4191 PyObject *o;
4192 Py_ssize_t len;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004193 Py_ssize_t i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004194
Eric V. Smith1d44c412015-09-23 07:49:00 -04004195 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004196 assert(*(expr_start-1) == '{');
4197 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004198
Eric V. Smith451d0e32016-09-09 21:56:20 -04004199 /* We know there are no escapes here, because backslashes are not allowed,
4200 and we know it's utf-8 encoded (per PEP 263). But, in order to check
4201 that each char is not whitespace, we need to decode it to unicode.
4202 Which is unfortunate, but such is life. */
Eric V. Smith1d44c412015-09-23 07:49:00 -04004203
Eric V. Smith451d0e32016-09-09 21:56:20 -04004204 /* If the substring is all whitespace, it's an error. We need to catch
4205 this here, and not when we call PyParser_ASTFromString, because turning
4206 the expression '' in to '()' would go from being invalid to valid. */
4207 /* Note that this code says an empty string is all whitespace. That's
4208 important. There's a test for it: f'{}'. */
4209 o = PyUnicode_DecodeUTF8(expr_start, expr_end-expr_start, NULL);
4210 if (o == NULL)
4211 return NULL;
4212 len = PyUnicode_GET_LENGTH(o);
4213 kind = PyUnicode_KIND(o);
4214 data = PyUnicode_DATA(o);
4215 for (i = 0; i < len; i++) {
4216 if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004217 all_whitespace = 0;
4218 break;
4219 }
4220 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004221 Py_DECREF(o);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004222 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004223 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004224 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004225 }
4226
Eric V. Smith451d0e32016-09-09 21:56:20 -04004227 /* Reuse len to be the length of the utf-8 input string. */
4228 len = expr_end - expr_start;
4229 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4230 str = PyMem_RawMalloc(len + 3);
4231 if (str == NULL)
4232 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004233
Eric V. Smith451d0e32016-09-09 21:56:20 -04004234 str[0] = '(';
4235 memcpy(str+1, expr_start, len);
4236 str[len+1] = ')';
4237 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004238
4239 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004240 mod = PyParser_ASTFromString(str, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004241 Py_eval_input, &cf, c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004242 PyMem_RawFree(str);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004243 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004244 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004245 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004246}
4247
4248/* Return -1 on error.
4249
4250 Return 0 if we reached the end of the literal.
4251
4252 Return 1 if we haven't reached the end of the literal, but we want
4253 the caller to process the literal up to this point. Used for
4254 doubled braces.
4255*/
4256static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004257fstring_find_literal(const char **str, const char *end, int raw,
4258 PyObject **literal, int recurse_lvl,
4259 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004260{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004261 /* Get any literal string. It ends when we hit an un-doubled left
4262 brace (which isn't part of a unicode name escape such as
4263 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004264
Eric V. Smith451d0e32016-09-09 21:56:20 -04004265 const char *literal_start = *str;
4266 const char *literal_end;
4267 int in_named_escape = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004268 int result = 0;
4269
Eric V. Smith235a6f02015-09-19 14:51:32 -04004270 assert(*literal == NULL);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004271 for (; *str < end; (*str)++) {
4272 char ch = **str;
4273 if (!in_named_escape && ch == '{' && (*str)-literal_start >= 2 &&
4274 *(*str-2) == '\\' && *(*str-1) == 'N') {
4275 in_named_escape = 1;
4276 } else if (in_named_escape && ch == '}') {
4277 in_named_escape = 0;
4278 } else if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004279 /* Check for doubled braces, but only at the top level. If
4280 we checked at every level, then f'{0:{3}}' would fail
4281 with the two closing braces. */
4282 if (recurse_lvl == 0) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004283 if (*str+1 < end && *(*str+1) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004284 /* We're going to tell the caller that the literal ends
4285 here, but that they should continue scanning. But also
4286 skip over the second brace when we resume scanning. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004287 literal_end = *str+1;
4288 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004289 result = 1;
4290 goto done;
4291 }
4292
4293 /* Where a single '{' is the start of a new expression, a
4294 single '}' is not allowed. */
4295 if (ch == '}') {
4296 ast_error(c, n, "f-string: single '}' is not allowed");
4297 return -1;
4298 }
4299 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004300 /* We're either at a '{', which means we're starting another
4301 expression; or a '}', which means we're at the end of this
4302 f-string (for a nested format_spec). */
4303 break;
4304 }
4305 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004306 literal_end = *str;
4307 assert(*str <= end);
4308 assert(*str == end || **str == '{' || **str == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004309done:
4310 if (literal_start != literal_end) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004311 if (raw)
4312 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
4313 literal_end-literal_start,
4314 NULL, NULL);
4315 else
4316 *literal = decode_unicode_with_escapes(c, literal_start,
4317 literal_end-literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004318 if (!*literal)
4319 return -1;
4320 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004321 return result;
4322}
4323
4324/* Forward declaration because parsing is recursive. */
4325static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004326fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004327 struct compiling *c, const node *n);
4328
Eric V. Smith451d0e32016-09-09 21:56:20 -04004329/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004330 expression (so it must be a '{'). Returns the FormattedValue node,
4331 which includes the expression, conversion character, and
4332 format_spec expression.
4333
4334 Note that I don't do a perfect job here: I don't make sure that a
4335 closing brace doesn't match an opening paren, for example. It
4336 doesn't need to error on all invalid expressions, just correctly
4337 find the end of all valid ones. Any errors inside the expression
4338 will be caught when we parse it later. */
4339static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004340fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004341 expr_ty *expression, struct compiling *c, const node *n)
4342{
4343 /* Return -1 on error, else 0. */
4344
Eric V. Smith451d0e32016-09-09 21:56:20 -04004345 const char *expr_start;
4346 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004347 expr_ty simple_expression;
4348 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004349 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004350
4351 /* 0 if we're not in a string, else the quote char we're trying to
4352 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004353 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004354
4355 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4356 int string_type = 0;
4357
4358 /* Keep track of nesting level for braces/parens/brackets in
4359 expressions. */
4360 Py_ssize_t nested_depth = 0;
4361
4362 /* Can only nest one level deep. */
4363 if (recurse_lvl >= 2) {
4364 ast_error(c, n, "f-string: expressions nested too deeply");
4365 return -1;
4366 }
4367
4368 /* The first char must be a left brace, or we wouldn't have gotten
4369 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004370 assert(**str == '{');
4371 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004372
Eric V. Smith451d0e32016-09-09 21:56:20 -04004373 expr_start = *str;
4374 for (; *str < end; (*str)++) {
4375 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004376
4377 /* Loop invariants. */
4378 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004379 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004380 if (quote_char)
4381 assert(string_type == 1 || string_type == 3);
4382 else
4383 assert(string_type == 0);
4384
Eric V. Smith451d0e32016-09-09 21:56:20 -04004385 ch = **str;
4386 /* Nowhere inside an expression is a backslash allowed. */
4387 if (ch == '\\') {
4388 /* Error: can't include a backslash character, inside
4389 parens or strings or not. */
4390 ast_error(c, n, "f-string expression part "
4391 "cannot include a backslash");
4392 return -1;
4393 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004394 if (quote_char) {
4395 /* We're inside a string. See if we're at the end. */
4396 /* This code needs to implement the same non-error logic
4397 as tok_get from tokenizer.c, at the letter_quote
4398 label. To actually share that code would be a
4399 nightmare. But, it's unlikely to change and is small,
4400 so duplicate it here. Note we don't need to catch all
4401 of the errors, since they'll be caught when parsing the
4402 expression. We just need to match the non-error
4403 cases. Thus we can ignore \n in single-quoted strings,
4404 for example. Or non-terminated strings. */
4405 if (ch == quote_char) {
4406 /* Does this match the string_type (single or triple
4407 quoted)? */
4408 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004409 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004410 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004411 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004412 string_type = 0;
4413 quote_char = 0;
4414 continue;
4415 }
4416 } else {
4417 /* We're at the end of a normal string. */
4418 quote_char = 0;
4419 string_type = 0;
4420 continue;
4421 }
4422 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004423 } else if (ch == '\'' || ch == '"') {
4424 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004425 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004426 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004427 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004428 } else {
4429 /* Start of a normal string. */
4430 string_type = 1;
4431 }
4432 /* Start looking for the end of the string. */
4433 quote_char = ch;
4434 } else if (ch == '[' || ch == '{' || ch == '(') {
4435 nested_depth++;
4436 } else if (nested_depth != 0 &&
4437 (ch == ']' || ch == '}' || ch == ')')) {
4438 nested_depth--;
4439 } else if (ch == '#') {
4440 /* Error: can't include a comment character, inside parens
4441 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004442 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004443 return -1;
4444 } else if (nested_depth == 0 &&
4445 (ch == '!' || ch == ':' || ch == '}')) {
4446 /* First, test for the special case of "!=". Since '=' is
4447 not an allowed conversion character, nothing is lost in
4448 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004449 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004450 /* This isn't a conversion character, just continue. */
4451 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004452 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004453 /* Normal way out of this loop. */
4454 break;
4455 } else {
4456 /* Just consume this char and loop around. */
4457 }
4458 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004459 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004460 /* If we leave this loop in a string or with mismatched parens, we
4461 don't care. We'll get a syntax error when compiling the
4462 expression. But, we can produce a better error message, so
4463 let's just do that.*/
4464 if (quote_char) {
4465 ast_error(c, n, "f-string: unterminated string");
4466 return -1;
4467 }
4468 if (nested_depth) {
4469 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4470 return -1;
4471 }
4472
Eric V. Smith451d0e32016-09-09 21:56:20 -04004473 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004474 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004475
4476 /* Compile the expression as soon as possible, so we show errors
4477 related to the expression before errors related to the
4478 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004479 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004480 if (!simple_expression)
4481 return -1;
4482
4483 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004484 if (**str == '!') {
4485 *str += 1;
4486 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004487 goto unexpected_end_of_string;
4488
Eric V. Smith451d0e32016-09-09 21:56:20 -04004489 conversion = **str;
4490 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004491
4492 /* Validate the conversion. */
4493 if (!(conversion == 's' || conversion == 'r'
4494 || conversion == 'a')) {
4495 ast_error(c, n, "f-string: invalid conversion character: "
4496 "expected 's', 'r', or 'a'");
4497 return -1;
4498 }
4499 }
4500
4501 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004502 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004503 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004504 if (**str == ':') {
4505 *str += 1;
4506 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004507 goto unexpected_end_of_string;
4508
4509 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004510 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004511 if (!format_spec)
4512 return -1;
4513 }
4514
Eric V. Smith451d0e32016-09-09 21:56:20 -04004515 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004516 goto unexpected_end_of_string;
4517
4518 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004519 assert(*str < end);
4520 assert(**str == '}');
4521 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004522
Eric V. Smith451d0e32016-09-09 21:56:20 -04004523 /* And now create the FormattedValue node that represents this
4524 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004525 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004526 format_spec, LINENO(n), n->n_col_offset,
4527 c->c_arena);
4528 if (!*expression)
4529 return -1;
4530
4531 return 0;
4532
4533unexpected_end_of_string:
4534 ast_error(c, n, "f-string: expecting '}'");
4535 return -1;
4536}
4537
4538/* Return -1 on error.
4539
4540 Return 0 if we have a literal (possible zero length) and an
4541 expression (zero length if at the end of the string.
4542
4543 Return 1 if we have a literal, but no expression, and we want the
4544 caller to call us again. This is used to deal with doubled
4545 braces.
4546
4547 When called multiple times on the string 'a{{b{0}c', this function
4548 will return:
4549
4550 1. the literal 'a{' with no expression, and a return value
4551 of 1. Despite the fact that there's no expression, the return
4552 value of 1 means we're not finished yet.
4553
4554 2. the literal 'b' and the expression '0', with a return value of
4555 0. The fact that there's an expression means we're not finished.
4556
4557 3. literal 'c' with no expression and a return value of 0. The
4558 combination of the return value of 0 with no expression means
4559 we're finished.
4560*/
4561static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004562fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4563 int recurse_lvl, PyObject **literal,
4564 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004565 struct compiling *c, const node *n)
4566{
4567 int result;
4568
4569 assert(*literal == NULL && *expression == NULL);
4570
4571 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004572 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004573 if (result < 0)
4574 goto error;
4575
4576 assert(result == 0 || result == 1);
4577
4578 if (result == 1)
4579 /* We have a literal, but don't look at the expression. */
4580 return 1;
4581
Eric V. Smith451d0e32016-09-09 21:56:20 -04004582 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004583 /* We're at the end of the string or the end of a nested
4584 f-string: no expression. The top-level error case where we
4585 expect to be at the end of the string but we're at a '}' is
4586 handled later. */
4587 return 0;
4588
4589 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004590 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004591
Eric V. Smith451d0e32016-09-09 21:56:20 -04004592 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004593 goto error;
4594
4595 return 0;
4596
4597error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004598 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004599 return -1;
4600}
4601
4602#define EXPRLIST_N_CACHED 64
4603
4604typedef struct {
4605 /* Incrementally build an array of expr_ty, so be used in an
4606 asdl_seq. Cache some small but reasonably sized number of
4607 expr_ty's, and then after that start dynamically allocating,
4608 doubling the number allocated each time. Note that the f-string
4609 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4610 Str for the literal 'a'. So you add expr_ty's about twice as
4611 fast as you add exressions in an f-string. */
4612
4613 Py_ssize_t allocated; /* Number we've allocated. */
4614 Py_ssize_t size; /* Number we've used. */
4615 expr_ty *p; /* Pointer to the memory we're actually
4616 using. Will point to 'data' until we
4617 start dynamically allocating. */
4618 expr_ty data[EXPRLIST_N_CACHED];
4619} ExprList;
4620
4621#ifdef NDEBUG
4622#define ExprList_check_invariants(l)
4623#else
4624static void
4625ExprList_check_invariants(ExprList *l)
4626{
4627 /* Check our invariants. Make sure this object is "live", and
4628 hasn't been deallocated. */
4629 assert(l->size >= 0);
4630 assert(l->p != NULL);
4631 if (l->size <= EXPRLIST_N_CACHED)
4632 assert(l->data == l->p);
4633}
4634#endif
4635
4636static void
4637ExprList_Init(ExprList *l)
4638{
4639 l->allocated = EXPRLIST_N_CACHED;
4640 l->size = 0;
4641
4642 /* Until we start allocating dynamically, p points to data. */
4643 l->p = l->data;
4644
4645 ExprList_check_invariants(l);
4646}
4647
4648static int
4649ExprList_Append(ExprList *l, expr_ty exp)
4650{
4651 ExprList_check_invariants(l);
4652 if (l->size >= l->allocated) {
4653 /* We need to alloc (or realloc) the memory. */
4654 Py_ssize_t new_size = l->allocated * 2;
4655
4656 /* See if we've ever allocated anything dynamically. */
4657 if (l->p == l->data) {
4658 Py_ssize_t i;
4659 /* We're still using the cached data. Switch to
4660 alloc-ing. */
4661 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4662 if (!l->p)
4663 return -1;
4664 /* Copy the cached data into the new buffer. */
4665 for (i = 0; i < l->size; i++)
4666 l->p[i] = l->data[i];
4667 } else {
4668 /* Just realloc. */
4669 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4670 if (!tmp) {
4671 PyMem_RawFree(l->p);
4672 l->p = NULL;
4673 return -1;
4674 }
4675 l->p = tmp;
4676 }
4677
4678 l->allocated = new_size;
4679 assert(l->allocated == 2 * l->size);
4680 }
4681
4682 l->p[l->size++] = exp;
4683
4684 ExprList_check_invariants(l);
4685 return 0;
4686}
4687
4688static void
4689ExprList_Dealloc(ExprList *l)
4690{
4691 ExprList_check_invariants(l);
4692
4693 /* If there's been an error, or we've never dynamically allocated,
4694 do nothing. */
4695 if (!l->p || l->p == l->data) {
4696 /* Do nothing. */
4697 } else {
4698 /* We have dynamically allocated. Free the memory. */
4699 PyMem_RawFree(l->p);
4700 }
4701 l->p = NULL;
4702 l->size = -1;
4703}
4704
4705static asdl_seq *
4706ExprList_Finish(ExprList *l, PyArena *arena)
4707{
4708 asdl_seq *seq;
4709
4710 ExprList_check_invariants(l);
4711
4712 /* Allocate the asdl_seq and copy the expressions in to it. */
4713 seq = _Py_asdl_seq_new(l->size, arena);
4714 if (seq) {
4715 Py_ssize_t i;
4716 for (i = 0; i < l->size; i++)
4717 asdl_seq_SET(seq, i, l->p[i]);
4718 }
4719 ExprList_Dealloc(l);
4720 return seq;
4721}
4722
4723/* The FstringParser is designed to add a mix of strings and
4724 f-strings, and concat them together as needed. Ultimately, it
4725 generates an expr_ty. */
4726typedef struct {
4727 PyObject *last_str;
4728 ExprList expr_list;
4729} FstringParser;
4730
4731#ifdef NDEBUG
4732#define FstringParser_check_invariants(state)
4733#else
4734static void
4735FstringParser_check_invariants(FstringParser *state)
4736{
4737 if (state->last_str)
4738 assert(PyUnicode_CheckExact(state->last_str));
4739 ExprList_check_invariants(&state->expr_list);
4740}
4741#endif
4742
4743static void
4744FstringParser_Init(FstringParser *state)
4745{
4746 state->last_str = NULL;
4747 ExprList_Init(&state->expr_list);
4748 FstringParser_check_invariants(state);
4749}
4750
4751static void
4752FstringParser_Dealloc(FstringParser *state)
4753{
4754 FstringParser_check_invariants(state);
4755
4756 Py_XDECREF(state->last_str);
4757 ExprList_Dealloc(&state->expr_list);
4758}
4759
4760/* Make a Str node, but decref the PyUnicode object being added. */
4761static expr_ty
4762make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4763{
4764 PyObject *s = *str;
4765 *str = NULL;
4766 assert(PyUnicode_CheckExact(s));
4767 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4768 Py_DECREF(s);
4769 return NULL;
4770 }
4771 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4772}
4773
4774/* Add a non-f-string (that is, a regular literal string). str is
4775 decref'd. */
4776static int
4777FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4778{
4779 FstringParser_check_invariants(state);
4780
4781 assert(PyUnicode_CheckExact(str));
4782
4783 if (PyUnicode_GET_LENGTH(str) == 0) {
4784 Py_DECREF(str);
4785 return 0;
4786 }
4787
4788 if (!state->last_str) {
4789 /* We didn't have a string before, so just remember this one. */
4790 state->last_str = str;
4791 } else {
4792 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004793 PyUnicode_AppendAndDel(&state->last_str, str);
4794 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004795 return -1;
4796 }
4797 FstringParser_check_invariants(state);
4798 return 0;
4799}
4800
Eric V. Smith451d0e32016-09-09 21:56:20 -04004801/* Parse an f-string. The f-string is in *str to end, with no
4802 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004803static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004804FstringParser_ConcatFstring(FstringParser *state, const char **str,
4805 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004806 struct compiling *c, const node *n)
4807{
4808 FstringParser_check_invariants(state);
4809
4810 /* Parse the f-string. */
4811 while (1) {
4812 PyObject *literal = NULL;
4813 expr_ty expression = NULL;
4814
4815 /* If there's a zero length literal in front of the
4816 expression, literal will be NULL. If we're at the end of
4817 the f-string, expression will be NULL (unless result == 1,
4818 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004819 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004820 &literal, &expression,
4821 c, n);
4822 if (result < 0)
4823 return -1;
4824
4825 /* Add the literal, if any. */
4826 if (!literal) {
4827 /* Do nothing. Just leave last_str alone (and possibly
4828 NULL). */
4829 } else if (!state->last_str) {
4830 state->last_str = literal;
4831 literal = NULL;
4832 } else {
4833 /* We have a literal, concatenate it. */
4834 assert(PyUnicode_GET_LENGTH(literal) != 0);
4835 if (FstringParser_ConcatAndDel(state, literal) < 0)
4836 return -1;
4837 literal = NULL;
4838 }
4839 assert(!state->last_str ||
4840 PyUnicode_GET_LENGTH(state->last_str) != 0);
4841
4842 /* We've dealt with the literal now. It can't be leaked on further
4843 errors. */
4844 assert(literal == NULL);
4845
4846 /* See if we should just loop around to get the next literal
4847 and expression, while ignoring the expression this
4848 time. This is used for un-doubling braces, as an
4849 optimization. */
4850 if (result == 1)
4851 continue;
4852
4853 if (!expression)
4854 /* We're done with this f-string. */
4855 break;
4856
4857 /* We know we have an expression. Convert any existing string
4858 to a Str node. */
4859 if (!state->last_str) {
4860 /* Do nothing. No previous literal. */
4861 } else {
4862 /* Convert the existing last_str literal to a Str node. */
4863 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4864 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4865 return -1;
4866 }
4867
4868 if (ExprList_Append(&state->expr_list, expression) < 0)
4869 return -1;
4870 }
4871
Eric V. Smith235a6f02015-09-19 14:51:32 -04004872 /* If recurse_lvl is zero, then we must be at the end of the
4873 string. Otherwise, we must be at a right brace. */
4874
Eric V. Smith451d0e32016-09-09 21:56:20 -04004875 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004876 ast_error(c, n, "f-string: unexpected end of string");
4877 return -1;
4878 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004879 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004880 ast_error(c, n, "f-string: expecting '}'");
4881 return -1;
4882 }
4883
4884 FstringParser_check_invariants(state);
4885 return 0;
4886}
4887
4888/* Convert the partial state reflected in last_str and expr_list to an
4889 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4890static expr_ty
4891FstringParser_Finish(FstringParser *state, struct compiling *c,
4892 const node *n)
4893{
4894 asdl_seq *seq;
4895
4896 FstringParser_check_invariants(state);
4897
4898 /* If we're just a constant string with no expressions, return
4899 that. */
4900 if(state->expr_list.size == 0) {
4901 if (!state->last_str) {
4902 /* Create a zero length string. */
4903 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4904 if (!state->last_str)
4905 goto error;
4906 }
4907 return make_str_node_and_del(&state->last_str, c, n);
4908 }
4909
4910 /* Create a Str node out of last_str, if needed. It will be the
4911 last node in our expression list. */
4912 if (state->last_str) {
4913 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4914 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4915 goto error;
4916 }
4917 /* This has already been freed. */
4918 assert(state->last_str == NULL);
4919
4920 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4921 if (!seq)
4922 goto error;
4923
4924 /* If there's only one expression, return it. Otherwise, we need
4925 to join them together. */
4926 if (seq->size == 1)
4927 return seq->elements[0];
4928
4929 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4930
4931error:
4932 FstringParser_Dealloc(state);
4933 return NULL;
4934}
4935
Eric V. Smith451d0e32016-09-09 21:56:20 -04004936/* Given an f-string (with no 'f' or quotes) that's in *str and ends
4937 at end, parse it into an expr_ty. Return NULL on error. Adjust
4938 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004939static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004940fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004941 struct compiling *c, const node *n)
4942{
4943 FstringParser state;
4944
4945 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004946 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004947 c, n) < 0) {
4948 FstringParser_Dealloc(&state);
4949 return NULL;
4950 }
4951
4952 return FstringParser_Finish(&state, c, n);
4953}
4954
4955/* n is a Python string literal, including the bracketing quote
4956 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04004957 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04004958 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04004959 *fstr and *fstrlen to the unparsed string object. Return 0 if no
4960 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004961*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04004962static int
4963parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
4964 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004965{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004966 size_t len;
4967 const char *s = STR(n);
4968 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004969 int fmode = 0;
4970 *bytesmode = 0;
4971 *rawmode = 0;
4972 *result = NULL;
4973 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004974 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004975 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004976 if (quote == 'b' || quote == 'B') {
4977 quote = *++s;
4978 *bytesmode = 1;
4979 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004980 else if (quote == 'u' || quote == 'U') {
4981 quote = *++s;
4982 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004983 else if (quote == 'r' || quote == 'R') {
4984 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004985 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004986 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 else if (quote == 'f' || quote == 'F') {
4988 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004989 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004990 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004991 else {
4992 break;
4993 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004994 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004995 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004996 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004998 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004999 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005000 if (quote != '\'' && quote != '\"') {
5001 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005002 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005003 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005004 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005005 s++;
5006 len = strlen(s);
5007 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005009 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005010 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005011 }
5012 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005014 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005015 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005016 }
5017 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005018 /* A triple quoted string. We've already skipped one quote at
5019 the start and one at the end of the string. Now skip the
5020 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005021 s += 2;
5022 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005023 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005024 if (s[--len] != quote || s[--len] != quote) {
5025 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005026 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005027 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005028 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005029
Eric V. Smith451d0e32016-09-09 21:56:20 -04005030 if (fmode) {
5031 /* Just return the bytes. The caller will parse the resulting
5032 string. */
5033 *fstr = s;
5034 *fstrlen = len;
5035 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005036 }
5037
Eric V. Smith451d0e32016-09-09 21:56:20 -04005038 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005039 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005040 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005041 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005042 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005043 const char *ch;
5044 for (ch = s; *ch; ch++) {
5045 if (Py_CHARMASK(*ch) >= 0x80) {
5046 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005047 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005048 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005049 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005050 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005051 if (*rawmode)
5052 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005053 else
Eric V. Smith451d0e32016-09-09 21:56:20 -04005054 *result = PyBytes_DecodeEscape(s, len, NULL, /* ignored */ 0, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005055 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005056 if (*rawmode)
5057 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005058 else
Eric V. Smith451d0e32016-09-09 21:56:20 -04005059 *result = decode_unicode_with_escapes(c, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005060 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005061 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005062}
5063
Eric V. Smith235a6f02015-09-19 14:51:32 -04005064/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5065 each STRING atom, and process it as needed. For bytes, just
5066 concatenate them together, and the result will be a Bytes node. For
5067 normal strings and f-strings, concatenate them together. The result
5068 will be a Str node if there were no f-strings; a FormattedValue
5069 node if there's just an f-string (with no leading or trailing
5070 literals), or a JoinedStr node if there are multiple f-strings or
5071 any literals involved. */
5072static expr_ty
5073parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005074{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005075 int bytesmode = 0;
5076 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005077 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005078
5079 FstringParser state;
5080 FstringParser_Init(&state);
5081
5082 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005083 int this_bytesmode;
5084 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005085 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005086 const char *fstr;
5087 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005088
5089 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5091 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005092 goto error;
5093
5094 /* Check that we're not mixing bytes with unicode. */
5095 if (i != 0 && bytesmode != this_bytesmode) {
5096 ast_error(c, n, "cannot mix bytes and nonbytes literals");
5097 Py_DECREF(s);
5098 goto error;
5099 }
5100 bytesmode = this_bytesmode;
5101
Eric V. Smith451d0e32016-09-09 21:56:20 -04005102 if (fstr != NULL) {
5103 int result;
5104 assert(s == NULL && !bytesmode);
5105 /* This is an f-string. Parse and concatenate it. */
5106 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5107 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005108 if (result < 0)
5109 goto error;
5110 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005111 assert(bytesmode ? PyBytes_CheckExact(s) :
5112 PyUnicode_CheckExact(s));
5113
5114 /* A string or byte string. */
5115 assert(s != NULL && fstr == NULL);
5116 if (bytesmode) {
5117 /* For bytes, concat as we go. */
5118 if (i == 0) {
5119 /* First time, just remember this value. */
5120 bytes_str = s;
5121 } else {
5122 PyBytes_ConcatAndDel(&bytes_str, s);
5123 if (!bytes_str)
5124 goto error;
5125 }
5126 } else {
5127 assert(s != NULL && fstr == NULL);
5128 /* This is a regular string. Concatenate it. */
5129 if (FstringParser_ConcatAndDel(&state, s) < 0)
5130 goto error;
5131 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005132 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005133 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134 if (bytesmode) {
5135 /* Just return the bytes object and we're done. */
5136 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5137 goto error;
5138 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5139 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005140
Eric V. Smith235a6f02015-09-19 14:51:32 -04005141 /* We're not a bytes string, bytes_str should never have been set. */
5142 assert(bytes_str == NULL);
5143
5144 return FstringParser_Finish(&state, c, n);
5145
5146error:
5147 Py_XDECREF(bytes_str);
5148 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005149 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005150}