blob: 092031cc8c443ea226aafe5e4694e734fae6bf04 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100135validate_constant(PyObject *value)
136{
137 if (value == Py_None || value == Py_Ellipsis)
138 return 1;
139
140 if (PyLong_CheckExact(value)
141 || PyFloat_CheckExact(value)
142 || PyComplex_CheckExact(value)
143 || PyBool_Check(value)
144 || PyUnicode_CheckExact(value)
145 || PyBytes_CheckExact(value))
146 return 1;
147
148 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
149 PyObject *it;
150
151 it = PyObject_GetIter(value);
152 if (it == NULL)
153 return 0;
154
155 while (1) {
156 PyObject *item = PyIter_Next(it);
157 if (item == NULL) {
158 if (PyErr_Occurred()) {
159 Py_DECREF(it);
160 return 0;
161 }
162 break;
163 }
164
165 if (!validate_constant(item)) {
166 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100167 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100168 return 0;
169 }
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 }
172
173 Py_DECREF(it);
174 return 1;
175 }
176
177 return 0;
178}
179
180static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500181validate_expr(expr_ty exp, expr_context_ty ctx)
182{
183 int check_ctx = 1;
184 expr_context_ty actual_ctx;
185
186 /* First check expression context. */
187 switch (exp->kind) {
188 case Attribute_kind:
189 actual_ctx = exp->v.Attribute.ctx;
190 break;
191 case Subscript_kind:
192 actual_ctx = exp->v.Subscript.ctx;
193 break;
194 case Starred_kind:
195 actual_ctx = exp->v.Starred.ctx;
196 break;
197 case Name_kind:
198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100291 PyErr_Format(PyExc_TypeError,
292 "got an invalid type in Constant: %s",
293 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 return 0;
295 }
296 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 case Num_kind: {
298 PyObject *n = exp->v.Num.n;
299 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
300 !PyComplex_CheckExact(n)) {
301 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
302 return 0;
303 }
304 return 1;
305 }
306 case Str_kind: {
307 PyObject *s = exp->v.Str.s;
308 if (!PyUnicode_CheckExact(s)) {
309 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
310 return 0;
311 }
312 return 1;
313 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314 case JoinedStr_kind:
315 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
316 case FormattedValue_kind:
317 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
318 return 0;
319 if (exp->v.FormattedValue.format_spec)
320 return validate_expr(exp->v.FormattedValue.format_spec, Load);
321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Bytes_kind: {
323 PyObject *b = exp->v.Bytes.s;
324 if (!PyBytes_CheckExact(b)) {
325 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
326 return 0;
327 }
328 return 1;
329 }
330 case Attribute_kind:
331 return validate_expr(exp->v.Attribute.value, Load);
332 case Subscript_kind:
333 return validate_slice(exp->v.Subscript.slice) &&
334 validate_expr(exp->v.Subscript.value, Load);
335 case Starred_kind:
336 return validate_expr(exp->v.Starred.value, ctx);
337 case List_kind:
338 return validate_exprs(exp->v.List.elts, ctx, 0);
339 case Tuple_kind:
340 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
341 /* These last cases don't have any checking. */
342 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500343 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 case Ellipsis_kind:
345 return 1;
346 default:
347 PyErr_SetString(PyExc_SystemError, "unexpected expression");
348 return 0;
349 }
350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
369validate_body(asdl_seq *body, const char *owner)
370{
371 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
372}
373
374static int
375validate_stmt(stmt_ty stmt)
376{
377 int i;
378 switch (stmt->kind) {
379 case FunctionDef_kind:
380 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
381 validate_arguments(stmt->v.FunctionDef.args) &&
382 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
383 (!stmt->v.FunctionDef.returns ||
384 validate_expr(stmt->v.FunctionDef.returns, Load));
385 case ClassDef_kind:
386 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
387 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
388 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400389 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500390 case Return_kind:
391 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
392 case Delete_kind:
393 return validate_assignlist(stmt->v.Delete.targets, Del);
394 case Assign_kind:
395 return validate_assignlist(stmt->v.Assign.targets, Store) &&
396 validate_expr(stmt->v.Assign.value, Load);
397 case AugAssign_kind:
398 return validate_expr(stmt->v.AugAssign.target, Store) &&
399 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700400 case AnnAssign_kind:
401 if (stmt->v.AnnAssign.target->kind != Name_kind &&
402 stmt->v.AnnAssign.simple) {
403 PyErr_SetString(PyExc_TypeError,
404 "AnnAssign with simple non-Name target");
405 return 0;
406 }
407 return validate_expr(stmt->v.AnnAssign.target, Store) &&
408 (!stmt->v.AnnAssign.value ||
409 validate_expr(stmt->v.AnnAssign.value, Load)) &&
410 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500411 case For_kind:
412 return validate_expr(stmt->v.For.target, Store) &&
413 validate_expr(stmt->v.For.iter, Load) &&
414 validate_body(stmt->v.For.body, "For") &&
415 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400416 case AsyncFor_kind:
417 return validate_expr(stmt->v.AsyncFor.target, Store) &&
418 validate_expr(stmt->v.AsyncFor.iter, Load) &&
419 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
420 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500421 case While_kind:
422 return validate_expr(stmt->v.While.test, Load) &&
423 validate_body(stmt->v.While.body, "While") &&
424 validate_stmts(stmt->v.While.orelse);
425 case If_kind:
426 return validate_expr(stmt->v.If.test, Load) &&
427 validate_body(stmt->v.If.body, "If") &&
428 validate_stmts(stmt->v.If.orelse);
429 case With_kind:
430 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
431 return 0;
432 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
433 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
434 if (!validate_expr(item->context_expr, Load) ||
435 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
436 return 0;
437 }
438 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400439 case AsyncWith_kind:
440 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
441 return 0;
442 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
443 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
444 if (!validate_expr(item->context_expr, Load) ||
445 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
446 return 0;
447 }
448 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500449 case Raise_kind:
450 if (stmt->v.Raise.exc) {
451 return validate_expr(stmt->v.Raise.exc, Load) &&
452 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
453 }
454 if (stmt->v.Raise.cause) {
455 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
456 return 0;
457 }
458 return 1;
459 case Try_kind:
460 if (!validate_body(stmt->v.Try.body, "Try"))
461 return 0;
462 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
463 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
464 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
465 return 0;
466 }
467 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
468 asdl_seq_LEN(stmt->v.Try.orelse)) {
469 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
470 return 0;
471 }
472 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
473 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
474 if ((handler->v.ExceptHandler.type &&
475 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
476 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
477 return 0;
478 }
479 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
480 validate_stmts(stmt->v.Try.finalbody)) &&
481 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
482 validate_stmts(stmt->v.Try.orelse));
483 case Assert_kind:
484 return validate_expr(stmt->v.Assert.test, Load) &&
485 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
486 case Import_kind:
487 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
488 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300489 if (stmt->v.ImportFrom.level < 0) {
490 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500491 return 0;
492 }
493 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
494 case Global_kind:
495 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
496 case Nonlocal_kind:
497 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
498 case Expr_kind:
499 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400500 case AsyncFunctionDef_kind:
501 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
502 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
503 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
504 (!stmt->v.AsyncFunctionDef.returns ||
505 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500506 case Pass_kind:
507 case Break_kind:
508 case Continue_kind:
509 return 1;
510 default:
511 PyErr_SetString(PyExc_SystemError, "unexpected statement");
512 return 0;
513 }
514}
515
516static int
517validate_stmts(asdl_seq *seq)
518{
519 int i;
520 for (i = 0; i < asdl_seq_LEN(seq); i++) {
521 stmt_ty stmt = asdl_seq_GET(seq, i);
522 if (stmt) {
523 if (!validate_stmt(stmt))
524 return 0;
525 }
526 else {
527 PyErr_SetString(PyExc_ValueError,
528 "None disallowed in statement list");
529 return 0;
530 }
531 }
532 return 1;
533}
534
535static int
536validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
537{
538 int i;
539 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
540 expr_ty expr = asdl_seq_GET(exprs, i);
541 if (expr) {
542 if (!validate_expr(expr, ctx))
543 return 0;
544 }
545 else if (!null_ok) {
546 PyErr_SetString(PyExc_ValueError,
547 "None disallowed in expression list");
548 return 0;
549 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100550
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500551 }
552 return 1;
553}
554
555int
556PyAST_Validate(mod_ty mod)
557{
558 int res = 0;
559
560 switch (mod->kind) {
561 case Module_kind:
562 res = validate_stmts(mod->v.Module.body);
563 break;
564 case Interactive_kind:
565 res = validate_stmts(mod->v.Interactive.body);
566 break;
567 case Expression_kind:
568 res = validate_expr(mod->v.Expression.body, Load);
569 break;
570 case Suite_kind:
571 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
572 break;
573 default:
574 PyErr_SetString(PyExc_SystemError, "impossible module node");
575 res = 0;
576 break;
577 }
578 return res;
579}
580
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500581/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500582#include "grammar.h"
583#include "parsetok.h"
584#include "graminit.h"
585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586/* Data structure used internally */
587struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400588 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200589 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500590 PyObject *c_normalize; /* Normalization function from unicodedata. */
591 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592};
593
594static asdl_seq *seq_for_testlist(struct compiling *, const node *);
595static expr_ty ast_for_expr(struct compiling *, const node *);
596static stmt_ty ast_for_stmt(struct compiling *, const node *);
597static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000598static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
599 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000600static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000601static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Yury Selivanov75445082015-05-11 22:57:16 -0400603static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
604static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
605
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606/* Note different signature for ast_for_call */
607static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
608
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000609static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400610static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Nick Coghlan650f0d02007-04-15 12:05:43 +0000612#define COMP_GENEXP 0
613#define COMP_LISTCOMP 1
614#define COMP_SETCOMP 2
615
Benjamin Peterson55e00432012-01-16 17:22:31 -0500616static int
617init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000618{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500619 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
620 if (!m)
621 return 0;
622 c->c_normalize = PyObject_GetAttrString(m, "normalize");
623 Py_DECREF(m);
624 if (!c->c_normalize)
625 return 0;
626 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500627 if (!c->c_normalize_args) {
628 Py_CLEAR(c->c_normalize);
629 return 0;
630 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200631 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500632 return 1;
633}
634
635static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400636new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500637{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400638 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500639 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000640 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500641 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500642 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000643 /* Check whether there are non-ASCII characters in the
644 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500645 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200646 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500647 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500648 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200649 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500650 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500651 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
652 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500653 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 if (!id2)
655 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200656 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000657 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000658 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200659 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
660 Py_DECREF(id);
661 return NULL;
662 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000663 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664}
665
Benjamin Peterson55e00432012-01-16 17:22:31 -0500666#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400669ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400671 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
Victor Stinner14e461d2013-08-26 22:28:21 +0200673 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000675 Py_INCREF(Py_None);
676 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200678 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 if (!tmp)
680 return 0;
681 errstr = PyUnicode_FromString(errmsg);
682 if (!errstr) {
683 Py_DECREF(tmp);
684 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000685 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 Py_DECREF(errstr);
688 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400689 if (value) {
690 PyErr_SetObject(PyExc_SyntaxError, value);
691 Py_DECREF(value);
692 }
693 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694}
695
696/* num_stmts() returns number of contained statements.
697
698 Use this routine to determine how big a sequence is needed for
699 the statements in a parse tree. Its raison d'etre is this bit of
700 grammar:
701
702 stmt: simple_stmt | compound_stmt
703 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
704
705 A simple_stmt can contain multiple small_stmt elements joined
706 by semicolons. If the arg is a simple_stmt, the number of
707 small_stmt elements is returned.
708*/
709
710static int
711num_stmts(const node *n)
712{
713 int i, l;
714 node *ch;
715
716 switch (TYPE(n)) {
717 case single_input:
718 if (TYPE(CHILD(n, 0)) == NEWLINE)
719 return 0;
720 else
721 return num_stmts(CHILD(n, 0));
722 case file_input:
723 l = 0;
724 for (i = 0; i < NCH(n); i++) {
725 ch = CHILD(n, i);
726 if (TYPE(ch) == stmt)
727 l += num_stmts(ch);
728 }
729 return l;
730 case stmt:
731 return num_stmts(CHILD(n, 0));
732 case compound_stmt:
733 return 1;
734 case simple_stmt:
735 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
736 case suite:
737 if (NCH(n) == 1)
738 return num_stmts(CHILD(n, 0));
739 else {
740 l = 0;
741 for (i = 2; i < (NCH(n) - 1); i++)
742 l += num_stmts(CHILD(n, i));
743 return l;
744 }
745 default: {
746 char buf[128];
747
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000748 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 TYPE(n), NCH(n));
750 Py_FatalError(buf);
751 }
752 }
753 assert(0);
754 return 0;
755}
756
757/* Transform the CST rooted at node * to the appropriate AST
758*/
759
760mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200761PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
762 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000764 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 asdl_seq *stmts = NULL;
766 stmt_ty s;
767 node *ch;
768 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500769 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400771 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200772 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400773 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800774 c.c_normalize = NULL;
775 c.c_normalize_args = NULL;
776
777 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Jeremy Hyltona8293132006-02-28 17:58:27 +0000780 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 switch (TYPE(n)) {
782 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200783 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500785 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 for (i = 0; i < NCH(n) - 1; i++) {
787 ch = CHILD(n, i);
788 if (TYPE(ch) == NEWLINE)
789 continue;
790 REQ(ch, stmt);
791 num = num_stmts(ch);
792 if (num == 1) {
793 s = ast_for_stmt(&c, ch);
794 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500795 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000796 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 }
798 else {
799 ch = CHILD(ch, 0);
800 REQ(ch, simple_stmt);
801 for (j = 0; j < num; j++) {
802 s = ast_for_stmt(&c, CHILD(ch, j * 2));
803 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500804 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000805 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
807 }
808 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 res = Module(stmts, arena);
810 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 case eval_input: {
812 expr_ty testlist_ast;
813
Nick Coghlan650f0d02007-04-15 12:05:43 +0000814 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000815 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
818 res = Expression(testlist_ast, arena);
819 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 case single_input:
822 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200823 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500825 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
827 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000828 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500829 goto out;
830 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 }
832 else {
833 n = CHILD(n, 0);
834 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200835 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500837 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000839 s = ast_for_stmt(&c, n);
840 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500841 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 asdl_seq_SET(stmts, 0, s);
843 }
844 else {
845 /* Only a simple_stmt can contain multiple statements. */
846 REQ(n, simple_stmt);
847 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (TYPE(CHILD(n, i)) == NEWLINE)
849 break;
850 s = ast_for_stmt(&c, CHILD(n, i));
851 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500852 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 asdl_seq_SET(stmts, i / 2, s);
854 }
855 }
856
Benjamin Peterson55e00432012-01-16 17:22:31 -0500857 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500859 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000861 PyErr_Format(PyExc_SystemError,
862 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 out:
866 if (c.c_normalize) {
867 Py_DECREF(c.c_normalize);
868 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
869 Py_DECREF(c.c_normalize_args);
870 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872}
873
Victor Stinner14e461d2013-08-26 22:28:21 +0200874mod_ty
875PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
876 PyArena *arena)
877{
878 mod_ty mod;
879 PyObject *filename;
880 filename = PyUnicode_DecodeFSDefault(filename_str);
881 if (filename == NULL)
882 return NULL;
883 mod = PyAST_FromNodeObject(n, flags, filename, arena);
884 Py_DECREF(filename);
885 return mod;
886
887}
888
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
890*/
891
892static operator_ty
893get_operator(const node *n)
894{
895 switch (TYPE(n)) {
896 case VBAR:
897 return BitOr;
898 case CIRCUMFLEX:
899 return BitXor;
900 case AMPER:
901 return BitAnd;
902 case LEFTSHIFT:
903 return LShift;
904 case RIGHTSHIFT:
905 return RShift;
906 case PLUS:
907 return Add;
908 case MINUS:
909 return Sub;
910 case STAR:
911 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400912 case AT:
913 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 case SLASH:
915 return Div;
916 case DOUBLESLASH:
917 return FloorDiv;
918 case PERCENT:
919 return Mod;
920 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 }
923}
924
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200925static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000926 "None",
927 "True",
928 "False",
929 NULL,
930};
931
932static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400933forbidden_name(struct compiling *c, identifier name, const node *n,
934 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000935{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000936 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000937 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400938 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000939 return 1;
940 }
941 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200942 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000943 for (p = FORBIDDEN; *p; p++) {
944 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400945 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000946 return 1;
947 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000948 }
949 }
950 return 0;
951}
952
Jeremy Hyltona8293132006-02-28 17:58:27 +0000953/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
955 Only sets context for expr kinds that "can appear in assignment context"
956 (according to ../Parser/Python.asdl). For other expr kinds, it sets
957 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958*/
959
960static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000961set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962{
963 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 /* If a particular expression type can't be used for assign / delete,
965 set expr_name to its name and an error message will be generated.
966 */
967 const char* expr_name = NULL;
968
969 /* The ast defines augmented store and load contexts, but the
970 implementation here doesn't actually use them. The code may be
971 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000972 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000973 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000974 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 */
976 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977
978 switch (e->kind) {
979 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000980 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400981 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000982 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000983 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000985 e->v.Subscript.ctx = ctx;
986 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000987 case Starred_kind:
988 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000989 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000990 return 0;
991 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000993 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500994 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000995 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000996 }
997 e->v.Name.ctx = ctx;
998 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001000 e->v.List.ctx = ctx;
1001 s = e->v.List.elts;
1002 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001004 e->v.Tuple.ctx = ctx;
1005 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001006 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001007 case Lambda_kind:
1008 expr_name = "lambda";
1009 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001011 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001012 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001013 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001015 case UnaryOp_kind:
1016 expr_name = "operator";
1017 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001019 expr_name = "generator expression";
1020 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001021 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001022 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001023 expr_name = "yield expression";
1024 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001025 case Await_kind:
1026 expr_name = "await expression";
1027 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001028 case ListComp_kind:
1029 expr_name = "list comprehension";
1030 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001031 case SetComp_kind:
1032 expr_name = "set comprehension";
1033 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001034 case DictComp_kind:
1035 expr_name = "dict comprehension";
1036 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001037 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001038 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 case Num_kind:
1040 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001041 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001042 case JoinedStr_kind:
1043 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001044 expr_name = "literal";
1045 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001046 case NameConstant_kind:
1047 expr_name = "keyword";
1048 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001049 case Ellipsis_kind:
1050 expr_name = "Ellipsis";
1051 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001052 case Compare_kind:
1053 expr_name = "comparison";
1054 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055 case IfExp_kind:
1056 expr_name = "conditional expression";
1057 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001058 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 PyErr_Format(PyExc_SystemError,
1060 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001061 e->kind, e->lineno);
1062 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001064 /* Check for error string set by switch */
1065 if (expr_name) {
1066 char buf[300];
1067 PyOS_snprintf(buf, sizeof(buf),
1068 "can't %s %s",
1069 ctx == Store ? "assign to" : "delete",
1070 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001071 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001072 }
1073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 */
1077 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001078 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079
Thomas Wouters89f507f2006-12-13 04:49:30 +00001080 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001081 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001082 return 0;
1083 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 }
1085 return 1;
1086}
1087
1088static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001089ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090{
1091 REQ(n, augassign);
1092 n = CHILD(n, 0);
1093 switch (STR(n)[0]) {
1094 case '+':
1095 return Add;
1096 case '-':
1097 return Sub;
1098 case '/':
1099 if (STR(n)[1] == '/')
1100 return FloorDiv;
1101 else
1102 return Div;
1103 case '%':
1104 return Mod;
1105 case '<':
1106 return LShift;
1107 case '>':
1108 return RShift;
1109 case '&':
1110 return BitAnd;
1111 case '^':
1112 return BitXor;
1113 case '|':
1114 return BitOr;
1115 case '*':
1116 if (STR(n)[1] == '*')
1117 return Pow;
1118 else
1119 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001120 case '@':
1121 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001123 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001124 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 }
1126}
1127
1128static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001129ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001131 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 |'is' 'not'
1133 */
1134 REQ(n, comp_op);
1135 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001136 n = CHILD(n, 0);
1137 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 case LESS:
1139 return Lt;
1140 case GREATER:
1141 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001142 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 return Eq;
1144 case LESSEQUAL:
1145 return LtE;
1146 case GREATEREQUAL:
1147 return GtE;
1148 case NOTEQUAL:
1149 return NotEq;
1150 case NAME:
1151 if (strcmp(STR(n), "in") == 0)
1152 return In;
1153 if (strcmp(STR(n), "is") == 0)
1154 return Is;
1155 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001156 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 }
1161 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 /* handle "not in" and "is not" */
1163 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 case NAME:
1165 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1166 return NotIn;
1167 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1168 return IsNot;
1169 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001170 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001172 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001173 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 }
Neal Norwitz79792652005-11-14 04:25:03 +00001175 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001177 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178}
1179
1180static asdl_seq *
1181seq_for_testlist(struct compiling *c, const node *n)
1182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001184 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1185 */
Armin Rigo31441302005-10-21 12:57:31 +00001186 asdl_seq *seq;
1187 expr_ty expression;
1188 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001189 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001191 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 if (!seq)
1193 return NULL;
1194
1195 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001197 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198
Benjamin Peterson4905e802009-09-27 02:43:28 +00001199 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202
1203 assert(i / 2 < seq->size);
1204 asdl_seq_SET(seq, i / 2, expression);
1205 }
1206 return seq;
1207}
1208
Neal Norwitzc1505362006-12-28 06:47:50 +00001209static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001210ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001211{
1212 identifier name;
1213 expr_ty annotation = NULL;
1214 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001215 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001216
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001217 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001218 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 name = NEW_IDENTIFIER(ch);
1220 if (!name)
1221 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001222 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001223 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001224
1225 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1226 annotation = ast_for_expr(c, CHILD(n, 2));
1227 if (!annotation)
1228 return NULL;
1229 }
1230
Victor Stinnerc106c682015-11-06 17:01:48 +01001231 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001232 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001233 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001234 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235}
1236
Guido van Rossum4f72a782006-10-27 23:31:49 +00001237/* returns -1 if failed to handle keyword only arguments
1238 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001239 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240 ^^^
1241 start pointing here
1242 */
1243static int
1244handle_keywordonly_args(struct compiling *c, const node *n, int start,
1245 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1246{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001247 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001248 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001249 expr_ty expression, annotation;
1250 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001251 int i = start;
1252 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001253
1254 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001255 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001256 return -1;
1257 }
1258 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001259 while (i < NCH(n)) {
1260 ch = CHILD(n, i);
1261 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001262 case vfpdef:
1263 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001264 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001265 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001266 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001267 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001268 asdl_seq_SET(kwdefaults, j, expression);
1269 i += 2; /* '=' and test */
1270 }
1271 else { /* setting NULL if no default value exists */
1272 asdl_seq_SET(kwdefaults, j, NULL);
1273 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001274 if (NCH(ch) == 3) {
1275 /* ch is NAME ':' test */
1276 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001277 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001278 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001279 }
1280 else {
1281 annotation = NULL;
1282 }
1283 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001284 argname = NEW_IDENTIFIER(ch);
1285 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001287 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001288 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001289 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1290 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001291 if (!arg)
1292 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001294 i += 2; /* the name and the comma */
1295 break;
1296 case DOUBLESTAR:
1297 return i;
1298 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001299 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001300 goto error;
1301 }
1302 }
1303 return i;
1304 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307
Jeremy Hyltona8293132006-02-28 17:58:27 +00001308/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309
1310static arguments_ty
1311ast_for_arguments(struct compiling *c, const node *n)
1312{
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 /* This function handles both typedargslist (function definition)
1314 and varargslist (lambda definition).
1315
1316 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001317 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1318 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1319 | '**' tfpdef [',']]]
1320 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1321 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001322 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001323 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1324 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1325 | '**' vfpdef [',']]]
1326 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1327 | '**' vfpdef [',']
1328 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001329 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001330
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1333 int nposdefaults = 0, found_default = 0;
1334 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001335 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 node *ch;
1338
1339 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001340 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001341 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001342 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345
Jeremy Hyltone921e022008-07-17 16:37:17 +00001346 /* First count the number of positional args & defaults. The
1347 variable i is the loop index for this for loop and the next.
1348 The next loop picks up where the first leaves off.
1349 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351 ch = CHILD(n, i);
1352 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001353 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001354 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001355 if (i < NCH(n) && /* skip argument following star */
1356 (TYPE(CHILD(n, i)) == tfpdef ||
1357 TYPE(CHILD(n, i)) == vfpdef)) {
1358 i++;
1359 }
1360 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001362 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001363 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 defaults for keyword only args */
1368 for ( ; i < NCH(n); ++i) {
1369 ch = CHILD(n, i);
1370 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001371 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001373 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001375 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001377 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001379 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001381 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001383 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 since we set NULL as default for keyword only argument w/o default
1386 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001387 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001388 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001390 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391
1392 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001393 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001394 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001397 /* tfpdef: NAME [':' test]
1398 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 */
1400 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001401 j = 0; /* index for defaults */
1402 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 ch = CHILD(n, i);
1405 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001406 case tfpdef:
1407 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1409 anything other than EQUAL or a comma? */
1410 /* XXX Should NCH(n) check be made a separate check? */
1411 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001412 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1413 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001414 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 assert(posdefaults != NULL);
1416 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001418 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001421 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001422 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001423 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001425 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001426 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001427 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001428 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 i += 2; /* the name and the comma */
1430 break;
1431 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001432 if (i+1 >= NCH(n) ||
1433 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001434 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001435 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001436 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001437 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001438 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001439 if (TYPE(ch) == COMMA) {
1440 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 i += 2; /* now follows keyword only arguments */
1442 res = handle_keywordonly_args(c, n, i,
1443 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001444 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 i = res; /* res has new position to process */
1446 }
1447 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001448 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001449 if (!vararg)
1450 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001451
Guido van Rossum4f72a782006-10-27 23:31:49 +00001452 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001453 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1454 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001455 int res = 0;
1456 res = handle_keywordonly_args(c, n, i,
1457 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001458 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001459 i = res; /* res has new position to process */
1460 }
1461 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 break;
1463 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001464 ch = CHILD(n, i+1); /* tfpdef */
1465 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001466 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001467 if (!kwarg)
1468 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 i += 3;
1470 break;
1471 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001472 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 "unexpected node in varargslist: %d @ %d",
1474 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001475 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001476 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001478 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479}
1480
1481static expr_ty
1482ast_for_dotted_name(struct compiling *c, const node *n)
1483{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001484 expr_ty e;
1485 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001486 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 int i;
1488
1489 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001490
1491 lineno = LINENO(n);
1492 col_offset = n->n_col_offset;
1493
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 id = NEW_IDENTIFIER(CHILD(n, 0));
1495 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001496 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001497 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001499 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500
1501 for (i = 2; i < NCH(n); i+=2) {
1502 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001503 if (!id)
1504 return NULL;
1505 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1506 if (!e)
1507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 }
1509
1510 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511}
1512
1513static expr_ty
1514ast_for_decorator(struct compiling *c, const node *n)
1515{
1516 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1517 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001518 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001521 REQ(CHILD(n, 0), AT);
1522 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1525 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001526 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001529 d = name_expr;
1530 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 }
1532 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001533 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001534 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001535 if (!d)
1536 return NULL;
1537 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 }
1539 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 d = ast_for_call(c, CHILD(n, 3), name_expr);
1541 if (!d)
1542 return NULL;
1543 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 }
1545
1546 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547}
1548
1549static asdl_seq*
1550ast_for_decorators(struct compiling *c, const node *n)
1551{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001552 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001553 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001557 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 if (!decorator_seq)
1559 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001562 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001563 if (!d)
1564 return NULL;
1565 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 }
1567 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568}
1569
1570static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001571ast_for_funcdef_impl(struct compiling *c, const node *n,
1572 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001574 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001575 identifier name;
1576 arguments_ty args;
1577 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001578 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001579 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580
1581 REQ(n, funcdef);
1582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 name = NEW_IDENTIFIER(CHILD(n, name_i));
1584 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001585 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001586 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1589 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001590 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001591 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1592 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1593 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001594 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001595 name_i += 2;
1596 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 body = ast_for_suite(c, CHILD(n, name_i + 3));
1598 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001599 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600
Yury Selivanov75445082015-05-11 22:57:16 -04001601 if (is_async)
1602 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1603 LINENO(n),
1604 n->n_col_offset, c->c_arena);
1605 else
1606 return FunctionDef(name, args, body, decorator_seq, returns,
1607 LINENO(n),
1608 n->n_col_offset, c->c_arena);
1609}
1610
1611static stmt_ty
1612ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1613{
1614 /* async_funcdef: ASYNC funcdef */
1615 REQ(n, async_funcdef);
1616 REQ(CHILD(n, 0), ASYNC);
1617 REQ(CHILD(n, 1), funcdef);
1618
1619 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1620 1 /* is_async */);
1621}
1622
1623static stmt_ty
1624ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1625{
1626 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1627 return ast_for_funcdef_impl(c, n, decorator_seq,
1628 0 /* is_async */);
1629}
1630
1631
1632static stmt_ty
1633ast_for_async_stmt(struct compiling *c, const node *n)
1634{
1635 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1636 REQ(n, async_stmt);
1637 REQ(CHILD(n, 0), ASYNC);
1638
1639 switch (TYPE(CHILD(n, 1))) {
1640 case funcdef:
1641 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1642 1 /* is_async */);
1643 case with_stmt:
1644 return ast_for_with_stmt(c, CHILD(n, 1),
1645 1 /* is_async */);
1646
1647 case for_stmt:
1648 return ast_for_for_stmt(c, CHILD(n, 1),
1649 1 /* is_async */);
1650
1651 default:
1652 PyErr_Format(PyExc_SystemError,
1653 "invalid async stament: %s",
1654 STR(CHILD(n, 1)));
1655 return NULL;
1656 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657}
1658
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001659static stmt_ty
1660ast_for_decorated(struct compiling *c, const node *n)
1661{
Yury Selivanov75445082015-05-11 22:57:16 -04001662 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001663 stmt_ty thing = NULL;
1664 asdl_seq *decorator_seq = NULL;
1665
1666 REQ(n, decorated);
1667
1668 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1669 if (!decorator_seq)
1670 return NULL;
1671
1672 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001673 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001675
1676 if (TYPE(CHILD(n, 1)) == funcdef) {
1677 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1678 } else if (TYPE(CHILD(n, 1)) == classdef) {
1679 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001680 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1681 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001682 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001683 /* we count the decorators in when talking about the class' or
1684 * function's line number */
1685 if (thing) {
1686 thing->lineno = LINENO(n);
1687 thing->col_offset = n->n_col_offset;
1688 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001689 return thing;
1690}
1691
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692static expr_ty
1693ast_for_lambdef(struct compiling *c, const node *n)
1694{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001695 /* lambdef: 'lambda' [varargslist] ':' test
1696 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 arguments_ty args;
1698 expr_ty expression;
1699
1700 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001701 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 if (!args)
1703 return NULL;
1704 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001705 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 }
1708 else {
1709 args = ast_for_arguments(c, CHILD(n, 1));
1710 if (!args)
1711 return NULL;
1712 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001713 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 }
1716
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001717 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718}
1719
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001720static expr_ty
1721ast_for_ifexpr(struct compiling *c, const node *n)
1722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001724 expr_ty expression, body, orelse;
1725
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001726 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001727 body = ast_for_expr(c, CHILD(n, 0));
1728 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001729 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001730 expression = ast_for_expr(c, CHILD(n, 2));
1731 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001732 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001733 orelse = ast_for_expr(c, CHILD(n, 4));
1734 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001735 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001736 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1737 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001738}
1739
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001741 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742
Nick Coghlan650f0d02007-04-15 12:05:43 +00001743 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744*/
1745
1746static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001747count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001749 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001750 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751
Guido van Rossumd8faa362007-04-27 19:54:29 +00001752 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001753 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001754 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001755 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001756 if (TYPE(CHILD(n, 0)) == ASYNC) {
1757 is_async = 1;
1758 }
1759 if (NCH(n) == (5 + is_async)) {
1760 n = CHILD(n, 4 + is_async);
1761 }
1762 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001763 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001764 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001765 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001766 REQ(n, comp_iter);
1767 n = CHILD(n, 0);
1768 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001769 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001770 else if (TYPE(n) == comp_if) {
1771 if (NCH(n) == 3) {
1772 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001773 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001774 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001775 else
1776 return n_fors;
1777 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001778
Guido van Rossumd8faa362007-04-27 19:54:29 +00001779 /* Should never be reached */
1780 PyErr_SetString(PyExc_SystemError,
1781 "logic error in count_comp_fors");
1782 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783}
1784
Nick Coghlan650f0d02007-04-15 12:05:43 +00001785/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786
Nick Coghlan650f0d02007-04-15 12:05:43 +00001787 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788*/
1789
1790static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001791count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001793 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794
Guido van Rossumd8faa362007-04-27 19:54:29 +00001795 while (1) {
1796 REQ(n, comp_iter);
1797 if (TYPE(CHILD(n, 0)) == comp_for)
1798 return n_ifs;
1799 n = CHILD(n, 0);
1800 REQ(n, comp_if);
1801 n_ifs++;
1802 if (NCH(n) == 2)
1803 return n_ifs;
1804 n = CHILD(n, 2);
1805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806}
1807
Guido van Rossum992d4a32007-07-11 13:09:30 +00001808static asdl_seq *
1809ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001812 asdl_seq *comps;
1813
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001814 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 if (n_fors == -1)
1816 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001818 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001819 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001821
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001823 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001825 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001826 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001827 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828
Guido van Rossum992d4a32007-07-11 13:09:30 +00001829 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001831 if (TYPE(CHILD(n, 0)) == ASYNC) {
1832 is_async = 1;
1833 }
1834
1835 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001836 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001837 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001839 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001840 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001842
Thomas Wouters89f507f2006-12-13 04:49:30 +00001843 /* Check the # of children rather than the length of t, since
1844 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001845 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001846 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001847 comp = comprehension(first, expression, NULL,
1848 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001850 comp = comprehension(Tuple(t, Store, first->lineno,
1851 first->col_offset, c->c_arena),
1852 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001853 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001855
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001856 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 int j, n_ifs;
1858 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001860 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001861 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001862 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001864
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001865 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001866 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001870 REQ(n, comp_iter);
1871 n = CHILD(n, 0);
1872 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873
Guido van Rossum992d4a32007-07-11 13:09:30 +00001874 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001875 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001876 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001877 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001878 if (NCH(n) == 3)
1879 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001881 /* on exit, must guarantee that n is a comp_for */
1882 if (TYPE(n) == comp_iter)
1883 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001884 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001886 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001888 return comps;
1889}
1890
1891static expr_ty
1892ast_for_itercomp(struct compiling *c, const node *n, int type)
1893{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001894 /* testlist_comp: (test|star_expr)
1895 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001896 expr_ty elt;
1897 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001898 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899
Guido van Rossum992d4a32007-07-11 13:09:30 +00001900 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001902 ch = CHILD(n, 0);
1903 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001904 if (!elt)
1905 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001906 if (elt->kind == Starred_kind) {
1907 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1908 return NULL;
1909 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910
Guido van Rossum992d4a32007-07-11 13:09:30 +00001911 comps = ast_for_comprehension(c, CHILD(n, 1));
1912 if (!comps)
1913 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001914
1915 if (type == COMP_GENEXP)
1916 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1917 else if (type == COMP_LISTCOMP)
1918 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1919 else if (type == COMP_SETCOMP)
1920 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1921 else
1922 /* Should never happen */
1923 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924}
1925
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001926/* Fills in the key, value pair corresponding to the dict element. In case
1927 * of an unpacking, key is NULL. *i is advanced by the number of ast
1928 * elements. Iff successful, nonzero is returned.
1929 */
1930static int
1931ast_for_dictelement(struct compiling *c, const node *n, int *i,
1932 expr_ty *key, expr_ty *value)
1933{
1934 expr_ty expression;
1935 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1936 assert(NCH(n) - *i >= 2);
1937
1938 expression = ast_for_expr(c, CHILD(n, *i + 1));
1939 if (!expression)
1940 return 0;
1941 *key = NULL;
1942 *value = expression;
1943
1944 *i += 2;
1945 }
1946 else {
1947 assert(NCH(n) - *i >= 3);
1948
1949 expression = ast_for_expr(c, CHILD(n, *i));
1950 if (!expression)
1951 return 0;
1952 *key = expression;
1953
1954 REQ(CHILD(n, *i + 1), COLON);
1955
1956 expression = ast_for_expr(c, CHILD(n, *i + 2));
1957 if (!expression)
1958 return 0;
1959 *value = expression;
1960
1961 *i += 3;
1962 }
1963 return 1;
1964}
1965
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001967ast_for_dictcomp(struct compiling *c, const node *n)
1968{
1969 expr_ty key, value;
1970 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001971 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001973 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001974 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001975 assert(key);
1976 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001978 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001979 if (!comps)
1980 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981
Guido van Rossum992d4a32007-07-11 13:09:30 +00001982 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1983}
1984
1985static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001986ast_for_dictdisplay(struct compiling *c, const node *n)
1987{
1988 int i;
1989 int j;
1990 int size;
1991 asdl_seq *keys, *values;
1992
1993 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1994 keys = _Py_asdl_seq_new(size, c->c_arena);
1995 if (!keys)
1996 return NULL;
1997
1998 values = _Py_asdl_seq_new(size, c->c_arena);
1999 if (!values)
2000 return NULL;
2001
2002 j = 0;
2003 for (i = 0; i < NCH(n); i++) {
2004 expr_ty key, value;
2005
2006 if (!ast_for_dictelement(c, n, &i, &key, &value))
2007 return NULL;
2008 asdl_seq_SET(keys, j, key);
2009 asdl_seq_SET(values, j, value);
2010
2011 j++;
2012 }
2013 keys->size = j;
2014 values->size = j;
2015 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2016}
2017
2018static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002019ast_for_genexp(struct compiling *c, const node *n)
2020{
2021 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002022 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002023}
2024
2025static expr_ty
2026ast_for_listcomp(struct compiling *c, const node *n)
2027{
2028 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002029 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002030}
2031
2032static expr_ty
2033ast_for_setcomp(struct compiling *c, const node *n)
2034{
2035 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002036 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002037}
2038
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002039static expr_ty
2040ast_for_setdisplay(struct compiling *c, const node *n)
2041{
2042 int i;
2043 int size;
2044 asdl_seq *elts;
2045
2046 assert(TYPE(n) == (dictorsetmaker));
2047 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2048 elts = _Py_asdl_seq_new(size, c->c_arena);
2049 if (!elts)
2050 return NULL;
2051 for (i = 0; i < NCH(n); i += 2) {
2052 expr_ty expression;
2053 expression = ast_for_expr(c, CHILD(n, i));
2054 if (!expression)
2055 return NULL;
2056 asdl_seq_SET(elts, i / 2, expression);
2057 }
2058 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2059}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002060
2061static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062ast_for_atom(struct compiling *c, const node *n)
2063{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002064 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2065 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002066 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 */
2068 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002071 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002072 PyObject *name;
2073 const char *s = STR(ch);
2074 size_t len = strlen(s);
2075 if (len >= 4 && len <= 5) {
2076 if (!strcmp(s, "None"))
2077 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2078 if (!strcmp(s, "True"))
2079 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2080 if (!strcmp(s, "False"))
2081 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2082 }
2083 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002084 if (!name)
2085 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002086 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002087 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2088 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002090 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002091 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002092 const char *errtype = NULL;
2093 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2094 errtype = "unicode error";
2095 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2096 errtype = "value error";
2097 if (errtype) {
2098 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002099 PyObject *type, *value, *tback, *errstr;
2100 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002101 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002102 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002103 char *s = _PyUnicode_AsString(errstr);
2104 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002105 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002106 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002107 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002108 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002109 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002110 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002111 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002112 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002113 Py_XDECREF(tback);
2114 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002115 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002116 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002117 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 }
2119 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002120 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002121 if (!pynum)
2122 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002123
Victor Stinner43d81952013-07-17 00:57:58 +02002124 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2125 Py_DECREF(pynum);
2126 return NULL;
2127 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002128 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 }
Georg Brandldde00282007-03-18 19:01:53 +00002130 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002131 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002133 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134
Thomas Wouters89f507f2006-12-13 04:49:30 +00002135 if (TYPE(ch) == RPAR)
2136 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137
Thomas Wouters89f507f2006-12-13 04:49:30 +00002138 if (TYPE(ch) == yield_expr)
2139 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002142 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002144
Nick Coghlan650f0d02007-04-15 12:05:43 +00002145 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002147 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148
Thomas Wouters89f507f2006-12-13 04:49:30 +00002149 if (TYPE(ch) == RSQB)
2150 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151
Nick Coghlan650f0d02007-04-15 12:05:43 +00002152 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002153 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2154 asdl_seq *elts = seq_for_testlist(c, ch);
2155 if (!elts)
2156 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002157
Thomas Wouters89f507f2006-12-13 04:49:30 +00002158 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2159 }
2160 else
2161 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002163 /* dictorsetmaker: ( ((test ':' test | '**' test)
2164 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2165 * ((test | '*' test)
2166 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002167 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002168 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002169 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002170 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002171 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002172 }
2173 else {
2174 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2175 if (NCH(ch) == 1 ||
2176 (NCH(ch) > 1 &&
2177 TYPE(CHILD(ch, 1)) == COMMA)) {
2178 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002179 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002180 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002181 else if (NCH(ch) > 1 &&
2182 TYPE(CHILD(ch, 1)) == comp_for) {
2183 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002184 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002185 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002186 else if (NCH(ch) > 3 - is_dict &&
2187 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2188 /* It's a dictionary comprehension. */
2189 if (is_dict) {
2190 ast_error(c, n, "dict unpacking cannot be used in "
2191 "dict comprehension");
2192 return NULL;
2193 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002194 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002195 }
2196 else {
2197 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002198 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002199 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002200 if (res) {
2201 res->lineno = LINENO(n);
2202 res->col_offset = n->n_col_offset;
2203 }
2204 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002205 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002208 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2209 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 }
2211}
2212
2213static slice_ty
2214ast_for_slice(struct compiling *c, const node *n)
2215{
2216 node *ch;
2217 expr_ty lower = NULL, upper = NULL, step = NULL;
2218
2219 REQ(n, subscript);
2220
2221 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002222 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 sliceop: ':' [test]
2224 */
2225 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 if (NCH(n) == 1 && TYPE(ch) == test) {
2227 /* 'step' variable hold no significance in terms of being used over
2228 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 if (!step)
2231 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232
Thomas Wouters89f507f2006-12-13 04:49:30 +00002233 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 }
2235
2236 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002237 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 if (!lower)
2239 return NULL;
2240 }
2241
2242 /* If there's an upper bound it's in the second or third position. */
2243 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002244 if (NCH(n) > 1) {
2245 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246
Thomas Wouters89f507f2006-12-13 04:49:30 +00002247 if (TYPE(n2) == test) {
2248 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 if (!upper)
2250 return NULL;
2251 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002252 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002254 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255
Thomas Wouters89f507f2006-12-13 04:49:30 +00002256 if (TYPE(n2) == test) {
2257 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 if (!upper)
2259 return NULL;
2260 }
2261 }
2262
2263 ch = CHILD(n, NCH(n) - 1);
2264 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002265 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002266 ch = CHILD(ch, 1);
2267 if (TYPE(ch) == test) {
2268 step = ast_for_expr(c, ch);
2269 if (!step)
2270 return NULL;
2271 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 }
2273 }
2274
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002275 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276}
2277
2278static expr_ty
2279ast_for_binop(struct compiling *c, const node *n)
2280{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002281 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002283 BinOp(BinOp(A, op, B), op, C).
2284 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285
Guido van Rossumd8faa362007-04-27 19:54:29 +00002286 int i, nops;
2287 expr_ty expr1, expr2, result;
2288 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289
Guido van Rossumd8faa362007-04-27 19:54:29 +00002290 expr1 = ast_for_expr(c, CHILD(n, 0));
2291 if (!expr1)
2292 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293
Guido van Rossumd8faa362007-04-27 19:54:29 +00002294 expr2 = ast_for_expr(c, CHILD(n, 2));
2295 if (!expr2)
2296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297
Guido van Rossumd8faa362007-04-27 19:54:29 +00002298 newoperator = get_operator(CHILD(n, 1));
2299 if (!newoperator)
2300 return NULL;
2301
2302 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2303 c->c_arena);
2304 if (!result)
2305 return NULL;
2306
2307 nops = (NCH(n) - 1) / 2;
2308 for (i = 1; i < nops; i++) {
2309 expr_ty tmp_result, tmp;
2310 const node* next_oper = CHILD(n, i * 2 + 1);
2311
2312 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002313 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 return NULL;
2315
Guido van Rossumd8faa362007-04-27 19:54:29 +00002316 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2317 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318 return NULL;
2319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002321 LINENO(next_oper), next_oper->n_col_offset,
2322 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002324 return NULL;
2325 result = tmp_result;
2326 }
2327 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328}
2329
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002330static expr_ty
2331ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002334 subscriptlist: subscript (',' subscript)* [',']
2335 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2336 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002337 REQ(n, trailer);
2338 if (TYPE(CHILD(n, 0)) == LPAR) {
2339 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002340 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002342 else
2343 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002344 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002345 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002346 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2347 if (!attr_id)
2348 return NULL;
2349 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002350 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002351 }
2352 else {
2353 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002354 REQ(CHILD(n, 2), RSQB);
2355 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002356 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002357 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2358 if (!slc)
2359 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002360 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2361 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002362 }
2363 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002365 by treating the sequence as a tuple literal if there are
2366 no slice features.
2367 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002368 int j;
2369 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002370 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002371 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002372 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002373 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002374 if (!slices)
2375 return NULL;
2376 for (j = 0; j < NCH(n); j += 2) {
2377 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002378 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002379 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002380 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002381 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002382 asdl_seq_SET(slices, j / 2, slc);
2383 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002384 if (!simple) {
2385 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002386 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002387 }
2388 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002389 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002390 if (!elts)
2391 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002392 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2393 slc = (slice_ty)asdl_seq_GET(slices, j);
2394 assert(slc->kind == Index_kind && slc->v.Index.value);
2395 asdl_seq_SET(elts, j, slc->v.Index.value);
2396 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002397 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002398 if (!e)
2399 return NULL;
2400 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002401 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002402 }
2403 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002404}
2405
2406static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002407ast_for_factor(struct compiling *c, const node *n)
2408{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002409 expr_ty expression;
2410
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002411 expression = ast_for_expr(c, CHILD(n, 1));
2412 if (!expression)
2413 return NULL;
2414
2415 switch (TYPE(CHILD(n, 0))) {
2416 case PLUS:
2417 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2418 c->c_arena);
2419 case MINUS:
2420 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2421 c->c_arena);
2422 case TILDE:
2423 return UnaryOp(Invert, expression, LINENO(n),
2424 n->n_col_offset, c->c_arena);
2425 }
2426 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2427 TYPE(CHILD(n, 0)));
2428 return NULL;
2429}
2430
2431static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002432ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002433{
Yury Selivanov75445082015-05-11 22:57:16 -04002434 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002435 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002436
2437 REQ(n, atom_expr);
2438 nch = NCH(n);
2439
2440 if (TYPE(CHILD(n, 0)) == AWAIT) {
2441 start = 1;
2442 assert(nch > 1);
2443 }
2444
2445 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002446 if (!e)
2447 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002448 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002449 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002450 if (start && nch == 2) {
2451 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2452 }
2453
2454 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002455 node *ch = CHILD(n, i);
2456 if (TYPE(ch) != trailer)
2457 break;
2458 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002459 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002460 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002461 tmp->lineno = e->lineno;
2462 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002463 e = tmp;
2464 }
Yury Selivanov75445082015-05-11 22:57:16 -04002465
2466 if (start) {
2467 /* there was an AWAIT */
2468 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2469 }
2470 else {
2471 return e;
2472 }
2473}
2474
2475static expr_ty
2476ast_for_power(struct compiling *c, const node *n)
2477{
2478 /* power: atom trailer* ('**' factor)*
2479 */
2480 expr_ty e;
2481 REQ(n, power);
2482 e = ast_for_atom_expr(c, CHILD(n, 0));
2483 if (!e)
2484 return NULL;
2485 if (NCH(n) == 1)
2486 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002487 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2488 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002489 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002490 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002491 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002492 }
2493 return e;
2494}
2495
Guido van Rossum0368b722007-05-11 16:50:42 +00002496static expr_ty
2497ast_for_starred(struct compiling *c, const node *n)
2498{
2499 expr_ty tmp;
2500 REQ(n, star_expr);
2501
2502 tmp = ast_for_expr(c, CHILD(n, 1));
2503 if (!tmp)
2504 return NULL;
2505
2506 /* The Load context is changed later. */
2507 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2508}
2509
2510
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511/* Do not name a variable 'expr'! Will cause a compile error.
2512*/
2513
2514static expr_ty
2515ast_for_expr(struct compiling *c, const node *n)
2516{
2517 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002518 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002519 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 and_test: not_test ('and' not_test)*
2522 not_test: 'not' not_test | comparison
2523 comparison: expr (comp_op expr)*
2524 expr: xor_expr ('|' xor_expr)*
2525 xor_expr: and_expr ('^' and_expr)*
2526 and_expr: shift_expr ('&' shift_expr)*
2527 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2528 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002529 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002531 power: atom_expr ['**' factor]
2532 atom_expr: [AWAIT] atom trailer*
2533 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 */
2535
2536 asdl_seq *seq;
2537 int i;
2538
2539 loop:
2540 switch (TYPE(n)) {
2541 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002542 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002543 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002544 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002546 else if (NCH(n) > 1)
2547 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002548 /* Fallthrough */
2549 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 case and_test:
2551 if (NCH(n) == 1) {
2552 n = CHILD(n, 0);
2553 goto loop;
2554 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002555 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 if (!seq)
2557 return NULL;
2558 for (i = 0; i < NCH(n); i += 2) {
2559 expr_ty e = ast_for_expr(c, CHILD(n, i));
2560 if (!e)
2561 return NULL;
2562 asdl_seq_SET(seq, i / 2, e);
2563 }
2564 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002565 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2566 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002567 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002568 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 case not_test:
2570 if (NCH(n) == 1) {
2571 n = CHILD(n, 0);
2572 goto loop;
2573 }
2574 else {
2575 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2576 if (!expression)
2577 return NULL;
2578
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002579 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2580 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 }
2582 case comparison:
2583 if (NCH(n) == 1) {
2584 n = CHILD(n, 0);
2585 goto loop;
2586 }
2587 else {
2588 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002589 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002590 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002591 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 if (!ops)
2593 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002594 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 return NULL;
2597 }
2598 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002599 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002601 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002602 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002604 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605
2606 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002607 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002609 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002611 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 asdl_seq_SET(cmps, i / 2, expression);
2613 }
2614 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002615 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002617 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002619 return Compare(expression, ops, cmps, LINENO(n),
2620 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 }
2622 break;
2623
Guido van Rossum0368b722007-05-11 16:50:42 +00002624 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 /* The next five cases all handle BinOps. The main body of code
2627 is the same in each case, but the switch turned inside out to
2628 reuse the code for each type of operator.
2629 */
2630 case expr:
2631 case xor_expr:
2632 case and_expr:
2633 case shift_expr:
2634 case arith_expr:
2635 case term:
2636 if (NCH(n) == 1) {
2637 n = CHILD(n, 0);
2638 goto loop;
2639 }
2640 return ast_for_binop(c, n);
2641 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002642 node *an = NULL;
2643 node *en = NULL;
2644 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002645 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002646 if (NCH(n) > 1)
2647 an = CHILD(n, 1); /* yield_arg */
2648 if (an) {
2649 en = CHILD(an, NCH(an) - 1);
2650 if (NCH(an) == 2) {
2651 is_from = 1;
2652 exp = ast_for_expr(c, en);
2653 }
2654 else
2655 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002656 if (!exp)
2657 return NULL;
2658 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002659 if (is_from)
2660 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2661 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002662 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002663 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664 if (NCH(n) == 1) {
2665 n = CHILD(n, 0);
2666 goto loop;
2667 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002668 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002669 case power:
2670 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002672 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 return NULL;
2674 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002675 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 return NULL;
2677}
2678
2679static expr_ty
2680ast_for_call(struct compiling *c, const node *n, expr_ty func)
2681{
2682 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002683 arglist: argument (',' argument)* [',']
2684 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685 */
2686
2687 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002688 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002689 asdl_seq *args;
2690 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691
2692 REQ(n, arglist);
2693
2694 nargs = 0;
2695 nkeywords = 0;
2696 ngens = 0;
2697 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002698 node *ch = CHILD(n, i);
2699 if (TYPE(ch) == argument) {
2700 if (NCH(ch) == 1)
2701 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002702 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002703 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002704 else if (TYPE(CHILD(ch, 0)) == STAR)
2705 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002707 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002708 nkeywords++;
2709 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 }
2711 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002712 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002713 "if not sole argument");
2714 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 }
2716
2717 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002718 ast_error(c, n, "more than 255 arguments");
2719 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 }
2721
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002722 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002724 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002725 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002727 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002728
2729 nargs = 0; /* positional arguments + iterable argument unpackings */
2730 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2731 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002733 node *ch = CHILD(n, i);
2734 if (TYPE(ch) == argument) {
2735 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002736 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002737 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002738 /* a positional argument */
2739 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002740 if (ndoublestars) {
2741 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002742 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002745 else {
2746 ast_error(c, chch,
2747 "positional argument follows "
2748 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002749 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002750 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002751 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002752 e = ast_for_expr(c, chch);
2753 if (!e)
2754 return NULL;
2755 asdl_seq_SET(args, nargs++, e);
2756 }
2757 else if (TYPE(chch) == STAR) {
2758 /* an iterable argument unpacking */
2759 expr_ty starred;
2760 if (ndoublestars) {
2761 ast_error(c, chch,
2762 "iterable argument unpacking follows "
2763 "keyword argument unpacking");
2764 return NULL;
2765 }
2766 e = ast_for_expr(c, CHILD(ch, 1));
2767 if (!e)
2768 return NULL;
2769 starred = Starred(e, Load, LINENO(chch),
2770 chch->n_col_offset,
2771 c->c_arena);
2772 if (!starred)
2773 return NULL;
2774 asdl_seq_SET(args, nargs++, starred);
2775
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002776 }
2777 else if (TYPE(chch) == DOUBLESTAR) {
2778 /* a keyword argument unpacking */
2779 keyword_ty kw;
2780 i++;
2781 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002783 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002784 kw = keyword(NULL, e, c->c_arena);
2785 asdl_seq_SET(keywords, nkeywords++, kw);
2786 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002788 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002789 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002790 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002792 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002793 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002795 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002796 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002797 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002798 identifier key, tmp;
2799 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 /* chch is test, but must be an identifier? */
2802 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002804 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 /* f(lambda x: x[0] = 3) ends up getting parsed with
2806 * LHS test = lambda x: x[0], and RHS test = 3.
2807 * SF bug 132313 points out that complaining about a keyword
2808 * then is very confusing.
2809 */
2810 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002811 ast_error(c, chch,
2812 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002813 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002814 }
2815 else if (e->kind != Name_kind) {
2816 ast_error(c, chch,
2817 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002818 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002819 }
2820 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002821 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002823 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002824 for (k = 0; k < nkeywords; k++) {
2825 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002826 if (tmp && !PyUnicode_Compare(tmp, key)) {
2827 ast_error(c, chch,
2828 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002829 return NULL;
2830 }
2831 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002832 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002834 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002835 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002837 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002838 asdl_seq_SET(keywords, nkeywords++, kw);
2839 }
2840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 }
2842
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002843 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844}
2845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002847ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002849 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002850 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002852 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002853 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002854 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002855 }
2856 else {
2857 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002858 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002861 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 else {
2863 asdl_seq *tmp = seq_for_testlist(c, n);
2864 if (!tmp)
2865 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002866 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002868}
2869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870static stmt_ty
2871ast_for_expr_stmt(struct compiling *c, const node *n)
2872{
2873 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002874 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2875 ('=' (yield_expr|testlist_star_expr))*)
2876 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002877 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002878 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002879 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002880 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 */
2882
2883 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 if (!e)
2886 return NULL;
2887
Thomas Wouters89f507f2006-12-13 04:49:30 +00002888 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 }
2890 else if (TYPE(CHILD(n, 1)) == augassign) {
2891 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002892 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002893 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894
Thomas Wouters89f507f2006-12-13 04:49:30 +00002895 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 if (!expr1)
2897 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002898 if(!set_context(c, expr1, Store, ch))
2899 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002900 /* set_context checks that most expressions are not the left side.
2901 Augmented assignments can only have a name, a subscript, or an
2902 attribute on the left, though, so we have to explicitly check for
2903 those. */
2904 switch (expr1->kind) {
2905 case Name_kind:
2906 case Attribute_kind:
2907 case Subscript_kind:
2908 break;
2909 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002910 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002911 return NULL;
2912 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913
Thomas Wouters89f507f2006-12-13 04:49:30 +00002914 ch = CHILD(n, 2);
2915 if (TYPE(ch) == testlist)
2916 expr2 = ast_for_testlist(c, ch);
2917 else
2918 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002919 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 return NULL;
2921
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002922 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002923 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 return NULL;
2925
Thomas Wouters89f507f2006-12-13 04:49:30 +00002926 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002928 else if (TYPE(CHILD(n, 1)) == annassign) {
2929 expr_ty expr1, expr2, expr3;
2930 node *ch = CHILD(n, 0);
2931 node *deep, *ann = CHILD(n, 1);
2932 int simple = 1;
2933
2934 /* we keep track of parens to qualify (x) as expression not name */
2935 deep = ch;
2936 while (NCH(deep) == 1) {
2937 deep = CHILD(deep, 0);
2938 }
2939 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2940 simple = 0;
2941 }
2942 expr1 = ast_for_testlist(c, ch);
2943 if (!expr1) {
2944 return NULL;
2945 }
2946 switch (expr1->kind) {
2947 case Name_kind:
2948 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2949 return NULL;
2950 }
2951 expr1->v.Name.ctx = Store;
2952 break;
2953 case Attribute_kind:
2954 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2955 return NULL;
2956 }
2957 expr1->v.Attribute.ctx = Store;
2958 break;
2959 case Subscript_kind:
2960 expr1->v.Subscript.ctx = Store;
2961 break;
2962 case List_kind:
2963 ast_error(c, ch,
2964 "only single target (not list) can be annotated");
2965 return NULL;
2966 case Tuple_kind:
2967 ast_error(c, ch,
2968 "only single target (not tuple) can be annotated");
2969 return NULL;
2970 default:
2971 ast_error(c, ch,
2972 "illegal target for annotation");
2973 return NULL;
2974 }
2975
2976 if (expr1->kind != Name_kind) {
2977 simple = 0;
2978 }
2979 ch = CHILD(ann, 1);
2980 expr2 = ast_for_expr(c, ch);
2981 if (!expr2) {
2982 return NULL;
2983 }
2984 if (NCH(ann) == 2) {
2985 return AnnAssign(expr1, expr2, NULL, simple,
2986 LINENO(n), n->n_col_offset, c->c_arena);
2987 }
2988 else {
2989 ch = CHILD(ann, 3);
2990 expr3 = ast_for_expr(c, ch);
2991 if (!expr3) {
2992 return NULL;
2993 }
2994 return AnnAssign(expr1, expr2, expr3, simple,
2995 LINENO(n), n->n_col_offset, c->c_arena);
2996 }
2997 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002999 int i;
3000 asdl_seq *targets;
3001 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002 expr_ty expression;
3003
Thomas Wouters89f507f2006-12-13 04:49:30 +00003004 /* a normal assignment */
3005 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003006 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003007 if (!targets)
3008 return NULL;
3009 for (i = 0; i < NCH(n) - 2; i += 2) {
3010 expr_ty e;
3011 node *ch = CHILD(n, i);
3012 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003013 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003014 return NULL;
3015 }
3016 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003020 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003021 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003022 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023
Thomas Wouters89f507f2006-12-13 04:49:30 +00003024 asdl_seq_SET(targets, i / 2, e);
3025 }
3026 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003027 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 expression = ast_for_testlist(c, value);
3029 else
3030 expression = ast_for_expr(c, value);
3031 if (!expression)
3032 return NULL;
3033 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035}
3036
Benjamin Peterson78565b22009-06-28 19:19:51 +00003037
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003039ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040{
3041 asdl_seq *seq;
3042 int i;
3043 expr_ty e;
3044
3045 REQ(n, exprlist);
3046
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003047 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003051 e = ast_for_expr(c, CHILD(n, i));
3052 if (!e)
3053 return NULL;
3054 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003055 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003056 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 }
3058 return seq;
3059}
3060
3061static stmt_ty
3062ast_for_del_stmt(struct compiling *c, const node *n)
3063{
3064 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066 /* del_stmt: 'del' exprlist */
3067 REQ(n, del_stmt);
3068
3069 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3070 if (!expr_list)
3071 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003072 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073}
3074
3075static stmt_ty
3076ast_for_flow_stmt(struct compiling *c, const node *n)
3077{
3078 /*
3079 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3080 | yield_stmt
3081 break_stmt: 'break'
3082 continue_stmt: 'continue'
3083 return_stmt: 'return' [testlist]
3084 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003085 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 raise_stmt: 'raise' [test [',' test [',' test]]]
3087 */
3088 node *ch;
3089
3090 REQ(n, flow_stmt);
3091 ch = CHILD(n, 0);
3092 switch (TYPE(ch)) {
3093 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003094 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003096 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003098 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3099 if (!exp)
3100 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003101 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102 }
3103 case return_stmt:
3104 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003105 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003107 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 if (!expression)
3109 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003110 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 }
3112 case raise_stmt:
3113 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003114 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3115 else if (NCH(ch) >= 2) {
3116 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3118 if (!expression)
3119 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003120 if (NCH(ch) == 4) {
3121 cause = ast_for_expr(c, CHILD(ch, 3));
3122 if (!cause)
3123 return NULL;
3124 }
3125 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 }
3127 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003128 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 "unexpected flow_stmt: %d", TYPE(ch));
3130 return NULL;
3131 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003132
3133 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3134 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135}
3136
3137static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003138alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139{
3140 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003141 import_as_name: NAME ['as' NAME]
3142 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 dotted_name: NAME ('.' NAME)*
3144 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003145 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 loop:
3148 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003149 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003150 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003151 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003152 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003153 if (!name)
3154 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003155 if (NCH(n) == 3) {
3156 node *str_node = CHILD(n, 2);
3157 str = NEW_IDENTIFIER(str_node);
3158 if (!str)
3159 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003160 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003161 return NULL;
3162 }
3163 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003164 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003165 return NULL;
3166 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003167 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 case dotted_as_name:
3170 if (NCH(n) == 1) {
3171 n = CHILD(n, 0);
3172 goto loop;
3173 }
3174 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003175 node *asname_node = CHILD(n, 2);
3176 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003177 if (!a)
3178 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003180 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003181 if (!a->asname)
3182 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003183 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003184 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 return a;
3186 }
3187 break;
3188 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003189 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003190 node *name_node = CHILD(n, 0);
3191 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003192 if (!name)
3193 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003194 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003195 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003196 return alias(name, NULL, c->c_arena);
3197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 else {
3199 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003200 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003201 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204
3205 len = 0;
3206 for (i = 0; i < NCH(n); i += 2)
3207 /* length of string plus one for the dot */
3208 len += strlen(STR(CHILD(n, i))) + 1;
3209 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003210 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 if (!str)
3212 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003213 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 if (!s)
3215 return NULL;
3216 for (i = 0; i < NCH(n); i += 2) {
3217 char *sch = STR(CHILD(n, i));
3218 strcpy(s, STR(CHILD(n, i)));
3219 s += strlen(sch);
3220 *s++ = '.';
3221 }
3222 --s;
3223 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3225 PyBytes_GET_SIZE(str),
3226 NULL);
3227 Py_DECREF(str);
3228 if (!uni)
3229 return NULL;
3230 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003231 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003232 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3233 Py_DECREF(str);
3234 return NULL;
3235 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003236 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 }
3238 break;
3239 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003240 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003241 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3242 Py_DECREF(str);
3243 return NULL;
3244 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003245 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003247 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 "unexpected import name: %d", TYPE(n));
3249 return NULL;
3250 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003251
3252 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253 return NULL;
3254}
3255
3256static stmt_ty
3257ast_for_import_stmt(struct compiling *c, const node *n)
3258{
3259 /*
3260 import_stmt: import_name | import_from
3261 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003262 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3263 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003265 int lineno;
3266 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 int i;
3268 asdl_seq *aliases;
3269
3270 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003271 lineno = LINENO(n);
3272 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003274 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003276 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003277 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003278 if (!aliases)
3279 return NULL;
3280 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003281 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003282 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003284 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003286 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003288 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003290 int idx, ndots = 0;
3291 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003292 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003294 /* Count the number of dots (for relative imports) and check for the
3295 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 for (idx = 1; idx < NCH(n); idx++) {
3297 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003298 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3299 if (!mod)
3300 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003301 idx++;
3302 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003303 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003305 ndots += 3;
3306 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 } else if (TYPE(CHILD(n, idx)) != DOT) {
3308 break;
3309 }
3310 ndots++;
3311 }
3312 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003313 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003314 case STAR:
3315 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 n = CHILD(n, idx);
3317 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 break;
3319 case LPAR:
3320 /* from ... import (x, y, z) */
3321 n = CHILD(n, idx + 1);
3322 n_children = NCH(n);
3323 break;
3324 case import_as_names:
3325 /* from ... import x, y, z */
3326 n = CHILD(n, idx);
3327 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003328 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003329 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330 " surrounding parentheses");
3331 return NULL;
3332 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 break;
3334 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003335 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003336 return NULL;
3337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003339 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003342
3343 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003344 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003345 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003346 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003348 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003350 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003351 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003352 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003353 if (!import_alias)
3354 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003355 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003356 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003358 if (mod != NULL)
3359 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003360 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003361 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362 }
Neal Norwitz79792652005-11-14 04:25:03 +00003363 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 "unknown import statement: starts with command '%s'",
3365 STR(CHILD(n, 0)));
3366 return NULL;
3367}
3368
3369static stmt_ty
3370ast_for_global_stmt(struct compiling *c, const node *n)
3371{
3372 /* global_stmt: 'global' NAME (',' NAME)* */
3373 identifier name;
3374 asdl_seq *s;
3375 int i;
3376
3377 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003378 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003380 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003382 name = NEW_IDENTIFIER(CHILD(n, i));
3383 if (!name)
3384 return NULL;
3385 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003387 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388}
3389
3390static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003391ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3392{
3393 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3394 identifier name;
3395 asdl_seq *s;
3396 int i;
3397
3398 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003399 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003400 if (!s)
3401 return NULL;
3402 for (i = 1; i < NCH(n); i += 2) {
3403 name = NEW_IDENTIFIER(CHILD(n, i));
3404 if (!name)
3405 return NULL;
3406 asdl_seq_SET(s, i / 2, name);
3407 }
3408 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3409}
3410
3411static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412ast_for_assert_stmt(struct compiling *c, const node *n)
3413{
3414 /* assert_stmt: 'assert' test [',' test] */
3415 REQ(n, assert_stmt);
3416 if (NCH(n) == 2) {
3417 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3418 if (!expression)
3419 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003420 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 }
3422 else if (NCH(n) == 4) {
3423 expr_ty expr1, expr2;
3424
3425 expr1 = ast_for_expr(c, CHILD(n, 1));
3426 if (!expr1)
3427 return NULL;
3428 expr2 = ast_for_expr(c, CHILD(n, 3));
3429 if (!expr2)
3430 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431
Thomas Wouters89f507f2006-12-13 04:49:30 +00003432 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433 }
Neal Norwitz79792652005-11-14 04:25:03 +00003434 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435 "improper number of parts to 'assert' statement: %d",
3436 NCH(n));
3437 return NULL;
3438}
3439
3440static asdl_seq *
3441ast_for_suite(struct compiling *c, const node *n)
3442{
3443 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003444 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445 stmt_ty s;
3446 int i, total, num, end, pos = 0;
3447 node *ch;
3448
3449 REQ(n, suite);
3450
3451 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003452 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003454 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003456 n = CHILD(n, 0);
3457 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003459 */
3460 end = NCH(n) - 1;
3461 if (TYPE(CHILD(n, end - 1)) == SEMI)
3462 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003464 for (i = 0; i < end; i += 2) {
3465 ch = CHILD(n, i);
3466 s = ast_for_stmt(c, ch);
3467 if (!s)
3468 return NULL;
3469 asdl_seq_SET(seq, pos++, s);
3470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 }
3472 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003473 for (i = 2; i < (NCH(n) - 1); i++) {
3474 ch = CHILD(n, i);
3475 REQ(ch, stmt);
3476 num = num_stmts(ch);
3477 if (num == 1) {
3478 /* small_stmt or compound_stmt with only one child */
3479 s = ast_for_stmt(c, ch);
3480 if (!s)
3481 return NULL;
3482 asdl_seq_SET(seq, pos++, s);
3483 }
3484 else {
3485 int j;
3486 ch = CHILD(ch, 0);
3487 REQ(ch, simple_stmt);
3488 for (j = 0; j < NCH(ch); j += 2) {
3489 /* statement terminates with a semi-colon ';' */
3490 if (NCH(CHILD(ch, j)) == 0) {
3491 assert((j + 1) == NCH(ch));
3492 break;
3493 }
3494 s = ast_for_stmt(c, CHILD(ch, j));
3495 if (!s)
3496 return NULL;
3497 asdl_seq_SET(seq, pos++, s);
3498 }
3499 }
3500 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 }
3502 assert(pos == seq->size);
3503 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504}
3505
3506static stmt_ty
3507ast_for_if_stmt(struct compiling *c, const node *n)
3508{
3509 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3510 ['else' ':' suite]
3511 */
3512 char *s;
3513
3514 REQ(n, if_stmt);
3515
3516 if (NCH(n) == 4) {
3517 expr_ty expression;
3518 asdl_seq *suite_seq;
3519
3520 expression = ast_for_expr(c, CHILD(n, 1));
3521 if (!expression)
3522 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003524 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526
Guido van Rossumd8faa362007-04-27 19:54:29 +00003527 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3528 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003530
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531 s = STR(CHILD(n, 4));
3532 /* s[2], the third character in the string, will be
3533 's' for el_s_e, or
3534 'i' for el_i_f
3535 */
3536 if (s[2] == 's') {
3537 expr_ty expression;
3538 asdl_seq *seq1, *seq2;
3539
3540 expression = ast_for_expr(c, CHILD(n, 1));
3541 if (!expression)
3542 return NULL;
3543 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003544 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 return NULL;
3546 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003547 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 return NULL;
3549
Guido van Rossumd8faa362007-04-27 19:54:29 +00003550 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3551 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 }
3553 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003554 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003555 expr_ty expression;
3556 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003557 asdl_seq *orelse = NULL;
3558 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 /* must reference the child n_elif+1 since 'else' token is third,
3560 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003561 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3562 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3563 has_else = 1;
3564 n_elif -= 3;
3565 }
3566 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567
Thomas Wouters89f507f2006-12-13 04:49:30 +00003568 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003569 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003571 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003572 if (!orelse)
3573 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003575 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003577 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3578 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003580 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3581 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 asdl_seq_SET(orelse, 0,
3585 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003586 LINENO(CHILD(n, NCH(n) - 6)),
3587 CHILD(n, NCH(n) - 6)->n_col_offset,
3588 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003589 /* the just-created orelse handled the last elif */
3590 n_elif--;
3591 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592
Thomas Wouters89f507f2006-12-13 04:49:30 +00003593 for (i = 0; i < n_elif; i++) {
3594 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003595 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003596 if (!newobj)
3597 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003599 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003602 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604
Thomas Wouters89f507f2006-12-13 04:49:30 +00003605 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003607 LINENO(CHILD(n, off)),
3608 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003609 orelse = newobj;
3610 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003611 expression = ast_for_expr(c, CHILD(n, 1));
3612 if (!expression)
3613 return NULL;
3614 suite_seq = ast_for_suite(c, CHILD(n, 3));
3615 if (!suite_seq)
3616 return NULL;
3617 return If(expression, suite_seq, orelse,
3618 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003620
3621 PyErr_Format(PyExc_SystemError,
3622 "unexpected token in 'if' statement: %s", s);
3623 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624}
3625
3626static stmt_ty
3627ast_for_while_stmt(struct compiling *c, const node *n)
3628{
3629 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3630 REQ(n, while_stmt);
3631
3632 if (NCH(n) == 4) {
3633 expr_ty expression;
3634 asdl_seq *suite_seq;
3635
3636 expression = ast_for_expr(c, CHILD(n, 1));
3637 if (!expression)
3638 return NULL;
3639 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003640 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003642 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 }
3644 else if (NCH(n) == 7) {
3645 expr_ty expression;
3646 asdl_seq *seq1, *seq2;
3647
3648 expression = ast_for_expr(c, CHILD(n, 1));
3649 if (!expression)
3650 return NULL;
3651 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003652 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 return NULL;
3654 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003655 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 return NULL;
3657
Thomas Wouters89f507f2006-12-13 04:49:30 +00003658 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003660
3661 PyErr_Format(PyExc_SystemError,
3662 "wrong number of tokens for 'while' statement: %d",
3663 NCH(n));
3664 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665}
3666
3667static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003668ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003670 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003672 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003673 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3675 REQ(n, for_stmt);
3676
3677 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003678 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679 if (!seq)
3680 return NULL;
3681 }
3682
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003683 node_target = CHILD(n, 1);
3684 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003685 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003687 /* Check the # of children rather than the length of _target, since
3688 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003689 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003690 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003691 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003692 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003693 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003695 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003696 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697 return NULL;
3698 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003699 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 return NULL;
3701
Yury Selivanov75445082015-05-11 22:57:16 -04003702 if (is_async)
3703 return AsyncFor(target, expression, suite_seq, seq,
3704 LINENO(n), n->n_col_offset,
3705 c->c_arena);
3706 else
3707 return For(target, expression, suite_seq, seq,
3708 LINENO(n), n->n_col_offset,
3709 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710}
3711
3712static excepthandler_ty
3713ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3714{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003715 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716 REQ(exc, except_clause);
3717 REQ(body, suite);
3718
3719 if (NCH(exc) == 1) {
3720 asdl_seq *suite_seq = ast_for_suite(c, body);
3721 if (!suite_seq)
3722 return NULL;
3723
Neal Norwitzad74aa82008-03-31 05:14:30 +00003724 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003725 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 }
3727 else if (NCH(exc) == 2) {
3728 expr_ty expression;
3729 asdl_seq *suite_seq;
3730
3731 expression = ast_for_expr(c, CHILD(exc, 1));
3732 if (!expression)
3733 return NULL;
3734 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003735 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736 return NULL;
3737
Neal Norwitzad74aa82008-03-31 05:14:30 +00003738 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003739 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740 }
3741 else if (NCH(exc) == 4) {
3742 asdl_seq *suite_seq;
3743 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003744 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003745 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003747 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003750 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 return NULL;
3752 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003753 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 return NULL;
3755
Neal Norwitzad74aa82008-03-31 05:14:30 +00003756 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003757 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003759
3760 PyErr_Format(PyExc_SystemError,
3761 "wrong number of children for 'except' clause: %d",
3762 NCH(exc));
3763 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764}
3765
3766static stmt_ty
3767ast_for_try_stmt(struct compiling *c, const node *n)
3768{
Neal Norwitzf599f422005-12-17 21:33:47 +00003769 const int nch = NCH(n);
3770 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003771 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 REQ(n, try_stmt);
3774
Neal Norwitzf599f422005-12-17 21:33:47 +00003775 body = ast_for_suite(c, CHILD(n, 2));
3776 if (body == NULL)
3777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778
Neal Norwitzf599f422005-12-17 21:33:47 +00003779 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3780 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3781 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3782 /* we can assume it's an "else",
3783 because nch >= 9 for try-else-finally and
3784 it would otherwise have a type of except_clause */
3785 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3786 if (orelse == NULL)
3787 return NULL;
3788 n_except--;
3789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790
Neal Norwitzf599f422005-12-17 21:33:47 +00003791 finally = ast_for_suite(c, CHILD(n, nch - 1));
3792 if (finally == NULL)
3793 return NULL;
3794 n_except--;
3795 }
3796 else {
3797 /* we can assume it's an "else",
3798 otherwise it would have a type of except_clause */
3799 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3800 if (orelse == NULL)
3801 return NULL;
3802 n_except--;
3803 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003805 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003806 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003807 return NULL;
3808 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809
Neal Norwitzf599f422005-12-17 21:33:47 +00003810 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003811 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003812 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003813 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003814 if (handlers == NULL)
3815 return NULL;
3816
3817 for (i = 0; i < n_except; i++) {
3818 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3819 CHILD(n, 5 + i * 3));
3820 if (!e)
3821 return NULL;
3822 asdl_seq_SET(handlers, i, e);
3823 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003824 }
3825
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003826 assert(finally != NULL || asdl_seq_LEN(handlers));
3827 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828}
3829
Georg Brandl0c315622009-05-25 21:10:36 +00003830/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003831static withitem_ty
3832ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003833{
3834 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003835
Georg Brandl0c315622009-05-25 21:10:36 +00003836 REQ(n, with_item);
3837 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003838 if (!context_expr)
3839 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003840 if (NCH(n) == 3) {
3841 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003842
3843 if (!optional_vars) {
3844 return NULL;
3845 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003846 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003847 return NULL;
3848 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003849 }
3850
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003851 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003852}
3853
Georg Brandl0c315622009-05-25 21:10:36 +00003854/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3855static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003856ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003857{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003858 int i, n_items;
3859 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003860
3861 REQ(n, with_stmt);
3862
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003863 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003864 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003865 if (!items)
3866 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003867 for (i = 1; i < NCH(n) - 2; i += 2) {
3868 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3869 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003870 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003871 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003872 }
3873
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003874 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3875 if (!body)
3876 return NULL;
3877
Yury Selivanov75445082015-05-11 22:57:16 -04003878 if (is_async)
3879 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3880 else
3881 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003882}
3883
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003885ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003887 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003888 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003889 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003890 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003891
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003892 REQ(n, classdef);
3893
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003894 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895 s = ast_for_suite(c, CHILD(n, 3));
3896 if (!s)
3897 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003898 classname = NEW_IDENTIFIER(CHILD(n, 1));
3899 if (!classname)
3900 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003901 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003902 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003903 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3904 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003906
3907 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003908 s = ast_for_suite(c, CHILD(n,5));
3909 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003910 return NULL;
3911 classname = NEW_IDENTIFIER(CHILD(n, 1));
3912 if (!classname)
3913 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003914 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003915 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003916 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3917 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 }
3919
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003920 /* class NAME '(' arglist ')' ':' suite */
3921 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003922 {
3923 PyObject *dummy_name;
3924 expr_ty dummy;
3925 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3926 if (!dummy_name)
3927 return NULL;
3928 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3929 call = ast_for_call(c, CHILD(n, 3), dummy);
3930 if (!call)
3931 return NULL;
3932 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003934 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003936 classname = NEW_IDENTIFIER(CHILD(n, 1));
3937 if (!classname)
3938 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003939 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003940 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003941
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003942 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003943 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944}
3945
3946static stmt_ty
3947ast_for_stmt(struct compiling *c, const node *n)
3948{
3949 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003950 assert(NCH(n) == 1);
3951 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 }
3953 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003954 assert(num_stmts(n) == 1);
3955 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 }
3957 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003958 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003959 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3960 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003961 */
3962 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 case expr_stmt:
3964 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 case del_stmt:
3966 return ast_for_del_stmt(c, n);
3967 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003968 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969 case flow_stmt:
3970 return ast_for_flow_stmt(c, n);
3971 case import_stmt:
3972 return ast_for_import_stmt(c, n);
3973 case global_stmt:
3974 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003975 case nonlocal_stmt:
3976 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 case assert_stmt:
3978 return ast_for_assert_stmt(c, n);
3979 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003980 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3982 TYPE(n), NCH(n));
3983 return NULL;
3984 }
3985 }
3986 else {
3987 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003988 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003989 */
3990 node *ch = CHILD(n, 0);
3991 REQ(n, compound_stmt);
3992 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 case if_stmt:
3994 return ast_for_if_stmt(c, ch);
3995 case while_stmt:
3996 return ast_for_while_stmt(c, ch);
3997 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003998 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 case try_stmt:
4000 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004001 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004002 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004004 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004006 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 case decorated:
4008 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004009 case async_stmt:
4010 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004012 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4014 TYPE(n), NCH(n));
4015 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004016 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 }
4018}
4019
4020static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004021parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004023 const char *end;
4024 long x;
4025 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004026 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004027 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004029 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004030 errno = 0;
4031 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004032 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004033 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004034 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004035 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004036 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004037 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004038 }
4039 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004040 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004041 if (*end == '\0') {
4042 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004043 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004044 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004045 }
4046 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004048 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004049 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4050 if (compl.imag == -1.0 && PyErr_Occurred())
4051 return NULL;
4052 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004053 }
4054 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004056 dx = PyOS_string_to_double(s, NULL, NULL);
4057 if (dx == -1.0 && PyErr_Occurred())
4058 return NULL;
4059 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004060 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061}
4062
4063static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004064parsenumber(struct compiling *c, const char *s)
4065{
4066 char *dup, *end;
4067 PyObject *res = NULL;
4068
4069 assert(s != NULL);
4070
4071 if (strchr(s, '_') == NULL) {
4072 return parsenumber_raw(c, s);
4073 }
4074 /* Create a duplicate without underscores. */
4075 dup = PyMem_Malloc(strlen(s) + 1);
4076 end = dup;
4077 for (; *s; s++) {
4078 if (*s != '_') {
4079 *end++ = *s;
4080 }
4081 }
4082 *end = '\0';
4083 res = parsenumber_raw(c, dup);
4084 PyMem_Free(dup);
4085 return res;
4086}
4087
4088static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004089decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004090{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004091 const char *s, *t;
4092 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004093 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4094 while (s < end && (*s & 0x80)) s++;
4095 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004096 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004097}
4098
4099static PyObject *
Benjamin Peterson768921c2016-02-25 23:13:53 -08004100decode_unicode_with_escapes(struct compiling *c, const char *s, size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004101{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004102 PyObject *v, *u;
4103 char *buf;
4104 char *p;
4105 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004106
Benjamin Peterson202803a2016-02-25 22:34:45 -08004107 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004108 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004109 return NULL;
4110 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4111 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4112 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4113 if (u == NULL)
4114 return NULL;
4115 p = buf = PyBytes_AsString(u);
4116 end = s + len;
4117 while (s < end) {
4118 if (*s == '\\') {
4119 *p++ = *s++;
4120 if (*s & 0x80) {
4121 strcpy(p, "u005c");
4122 p += 5;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004123 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004124 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004125 if (*s & 0x80) { /* XXX inefficient */
4126 PyObject *w;
4127 int kind;
4128 void *data;
4129 Py_ssize_t len, i;
4130 w = decode_utf8(c, &s, end);
4131 if (w == NULL) {
4132 Py_DECREF(u);
4133 return NULL;
4134 }
4135 kind = PyUnicode_KIND(w);
4136 data = PyUnicode_DATA(w);
4137 len = PyUnicode_GET_LENGTH(w);
4138 for (i = 0; i < len; i++) {
4139 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4140 sprintf(p, "\\U%08x", chr);
4141 p += 10;
4142 }
4143 /* Should be impossible to overflow */
4144 assert(p - buf <= Py_SIZE(u));
4145 Py_DECREF(w);
4146 } else {
4147 *p++ = *s++;
4148 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004149 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004150 len = p - buf;
4151 s = buf;
4152
Eric V. Smith5567f892015-09-21 13:36:09 -04004153 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004154 Py_XDECREF(u);
4155 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004156}
4157
Eric V. Smith451d0e32016-09-09 21:56:20 -04004158/* Compile this expression in to an expr_ty. Add parens around the
4159 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004160static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004161fstring_compile_expr(const char *expr_start, const char *expr_end,
4162 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004163
Eric V. Smith235a6f02015-09-19 14:51:32 -04004164{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004165 int all_whitespace = 1;
4166 int kind;
4167 void *data;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004168 PyCompilerFlags cf;
4169 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004170 char *str;
4171 PyObject *o;
4172 Py_ssize_t len;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004173 Py_ssize_t i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004174
Eric V. Smith1d44c412015-09-23 07:49:00 -04004175 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004176 assert(*(expr_start-1) == '{');
4177 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004178
Eric V. Smith451d0e32016-09-09 21:56:20 -04004179 /* We know there are no escapes here, because backslashes are not allowed,
4180 and we know it's utf-8 encoded (per PEP 263). But, in order to check
4181 that each char is not whitespace, we need to decode it to unicode.
4182 Which is unfortunate, but such is life. */
Eric V. Smith1d44c412015-09-23 07:49:00 -04004183
Eric V. Smith451d0e32016-09-09 21:56:20 -04004184 /* If the substring is all whitespace, it's an error. We need to catch
4185 this here, and not when we call PyParser_ASTFromString, because turning
4186 the expression '' in to '()' would go from being invalid to valid. */
4187 /* Note that this code says an empty string is all whitespace. That's
4188 important. There's a test for it: f'{}'. */
4189 o = PyUnicode_DecodeUTF8(expr_start, expr_end-expr_start, NULL);
4190 if (o == NULL)
4191 return NULL;
4192 len = PyUnicode_GET_LENGTH(o);
4193 kind = PyUnicode_KIND(o);
4194 data = PyUnicode_DATA(o);
4195 for (i = 0; i < len; i++) {
4196 if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004197 all_whitespace = 0;
4198 break;
4199 }
4200 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004201 Py_DECREF(o);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004202 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004203 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004204 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004205 }
4206
Eric V. Smith451d0e32016-09-09 21:56:20 -04004207 /* Reuse len to be the length of the utf-8 input string. */
4208 len = expr_end - expr_start;
4209 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4210 str = PyMem_RawMalloc(len + 3);
4211 if (str == NULL)
4212 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004213
Eric V. Smith451d0e32016-09-09 21:56:20 -04004214 str[0] = '(';
4215 memcpy(str+1, expr_start, len);
4216 str[len+1] = ')';
4217 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004218
4219 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004220 mod = PyParser_ASTFromString(str, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004221 Py_eval_input, &cf, c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004222 PyMem_RawFree(str);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004223 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004224 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004225 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004226}
4227
4228/* Return -1 on error.
4229
4230 Return 0 if we reached the end of the literal.
4231
4232 Return 1 if we haven't reached the end of the literal, but we want
4233 the caller to process the literal up to this point. Used for
4234 doubled braces.
4235*/
4236static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004237fstring_find_literal(const char **str, const char *end, int raw,
4238 PyObject **literal, int recurse_lvl,
4239 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004240{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004241 /* Get any literal string. It ends when we hit an un-doubled left
4242 brace (which isn't part of a unicode name escape such as
4243 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004244
Eric V. Smith451d0e32016-09-09 21:56:20 -04004245 const char *literal_start = *str;
4246 const char *literal_end;
4247 int in_named_escape = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004248 int result = 0;
4249
Eric V. Smith235a6f02015-09-19 14:51:32 -04004250 assert(*literal == NULL);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004251 for (; *str < end; (*str)++) {
4252 char ch = **str;
4253 if (!in_named_escape && ch == '{' && (*str)-literal_start >= 2 &&
4254 *(*str-2) == '\\' && *(*str-1) == 'N') {
4255 in_named_escape = 1;
4256 } else if (in_named_escape && ch == '}') {
4257 in_named_escape = 0;
4258 } else if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004259 /* Check for doubled braces, but only at the top level. If
4260 we checked at every level, then f'{0:{3}}' would fail
4261 with the two closing braces. */
4262 if (recurse_lvl == 0) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004263 if (*str+1 < end && *(*str+1) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004264 /* We're going to tell the caller that the literal ends
4265 here, but that they should continue scanning. But also
4266 skip over the second brace when we resume scanning. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004267 literal_end = *str+1;
4268 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004269 result = 1;
4270 goto done;
4271 }
4272
4273 /* Where a single '{' is the start of a new expression, a
4274 single '}' is not allowed. */
4275 if (ch == '}') {
4276 ast_error(c, n, "f-string: single '}' is not allowed");
4277 return -1;
4278 }
4279 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004280 /* We're either at a '{', which means we're starting another
4281 expression; or a '}', which means we're at the end of this
4282 f-string (for a nested format_spec). */
4283 break;
4284 }
4285 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004286 literal_end = *str;
4287 assert(*str <= end);
4288 assert(*str == end || **str == '{' || **str == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004289done:
4290 if (literal_start != literal_end) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004291 if (raw)
4292 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
4293 literal_end-literal_start,
4294 NULL, NULL);
4295 else
4296 *literal = decode_unicode_with_escapes(c, literal_start,
4297 literal_end-literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004298 if (!*literal)
4299 return -1;
4300 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004301 return result;
4302}
4303
4304/* Forward declaration because parsing is recursive. */
4305static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004306fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004307 struct compiling *c, const node *n);
4308
Eric V. Smith451d0e32016-09-09 21:56:20 -04004309/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004310 expression (so it must be a '{'). Returns the FormattedValue node,
4311 which includes the expression, conversion character, and
4312 format_spec expression.
4313
4314 Note that I don't do a perfect job here: I don't make sure that a
4315 closing brace doesn't match an opening paren, for example. It
4316 doesn't need to error on all invalid expressions, just correctly
4317 find the end of all valid ones. Any errors inside the expression
4318 will be caught when we parse it later. */
4319static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004320fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004321 expr_ty *expression, struct compiling *c, const node *n)
4322{
4323 /* Return -1 on error, else 0. */
4324
Eric V. Smith451d0e32016-09-09 21:56:20 -04004325 const char *expr_start;
4326 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004327 expr_ty simple_expression;
4328 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004329 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004330
4331 /* 0 if we're not in a string, else the quote char we're trying to
4332 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004333 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004334
4335 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4336 int string_type = 0;
4337
4338 /* Keep track of nesting level for braces/parens/brackets in
4339 expressions. */
4340 Py_ssize_t nested_depth = 0;
4341
4342 /* Can only nest one level deep. */
4343 if (recurse_lvl >= 2) {
4344 ast_error(c, n, "f-string: expressions nested too deeply");
4345 return -1;
4346 }
4347
4348 /* The first char must be a left brace, or we wouldn't have gotten
4349 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004350 assert(**str == '{');
4351 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352
Eric V. Smith451d0e32016-09-09 21:56:20 -04004353 expr_start = *str;
4354 for (; *str < end; (*str)++) {
4355 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004356
4357 /* Loop invariants. */
4358 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004359 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004360 if (quote_char)
4361 assert(string_type == 1 || string_type == 3);
4362 else
4363 assert(string_type == 0);
4364
Eric V. Smith451d0e32016-09-09 21:56:20 -04004365 ch = **str;
4366 /* Nowhere inside an expression is a backslash allowed. */
4367 if (ch == '\\') {
4368 /* Error: can't include a backslash character, inside
4369 parens or strings or not. */
4370 ast_error(c, n, "f-string expression part "
4371 "cannot include a backslash");
4372 return -1;
4373 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004374 if (quote_char) {
4375 /* We're inside a string. See if we're at the end. */
4376 /* This code needs to implement the same non-error logic
4377 as tok_get from tokenizer.c, at the letter_quote
4378 label. To actually share that code would be a
4379 nightmare. But, it's unlikely to change and is small,
4380 so duplicate it here. Note we don't need to catch all
4381 of the errors, since they'll be caught when parsing the
4382 expression. We just need to match the non-error
4383 cases. Thus we can ignore \n in single-quoted strings,
4384 for example. Or non-terminated strings. */
4385 if (ch == quote_char) {
4386 /* Does this match the string_type (single or triple
4387 quoted)? */
4388 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004389 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004390 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004391 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004392 string_type = 0;
4393 quote_char = 0;
4394 continue;
4395 }
4396 } else {
4397 /* We're at the end of a normal string. */
4398 quote_char = 0;
4399 string_type = 0;
4400 continue;
4401 }
4402 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004403 } else if (ch == '\'' || ch == '"') {
4404 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004405 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004406 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004407 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004408 } else {
4409 /* Start of a normal string. */
4410 string_type = 1;
4411 }
4412 /* Start looking for the end of the string. */
4413 quote_char = ch;
4414 } else if (ch == '[' || ch == '{' || ch == '(') {
4415 nested_depth++;
4416 } else if (nested_depth != 0 &&
4417 (ch == ']' || ch == '}' || ch == ')')) {
4418 nested_depth--;
4419 } else if (ch == '#') {
4420 /* Error: can't include a comment character, inside parens
4421 or not. */
4422 ast_error(c, n, "f-string cannot include '#'");
4423 return -1;
4424 } else if (nested_depth == 0 &&
4425 (ch == '!' || ch == ':' || ch == '}')) {
4426 /* First, test for the special case of "!=". Since '=' is
4427 not an allowed conversion character, nothing is lost in
4428 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004429 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004430 /* This isn't a conversion character, just continue. */
4431 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004432 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004433 /* Normal way out of this loop. */
4434 break;
4435 } else {
4436 /* Just consume this char and loop around. */
4437 }
4438 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004439 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004440 /* If we leave this loop in a string or with mismatched parens, we
4441 don't care. We'll get a syntax error when compiling the
4442 expression. But, we can produce a better error message, so
4443 let's just do that.*/
4444 if (quote_char) {
4445 ast_error(c, n, "f-string: unterminated string");
4446 return -1;
4447 }
4448 if (nested_depth) {
4449 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4450 return -1;
4451 }
4452
Eric V. Smith451d0e32016-09-09 21:56:20 -04004453 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004454 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004455
4456 /* Compile the expression as soon as possible, so we show errors
4457 related to the expression before errors related to the
4458 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004459 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004460 if (!simple_expression)
4461 return -1;
4462
4463 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004464 if (**str == '!') {
4465 *str += 1;
4466 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004467 goto unexpected_end_of_string;
4468
Eric V. Smith451d0e32016-09-09 21:56:20 -04004469 conversion = **str;
4470 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004471
4472 /* Validate the conversion. */
4473 if (!(conversion == 's' || conversion == 'r'
4474 || conversion == 'a')) {
4475 ast_error(c, n, "f-string: invalid conversion character: "
4476 "expected 's', 'r', or 'a'");
4477 return -1;
4478 }
4479 }
4480
4481 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004482 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004483 goto unexpected_end_of_string;
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
4489 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004490 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004491 if (!format_spec)
4492 return -1;
4493 }
4494
Eric V. Smith451d0e32016-09-09 21:56:20 -04004495 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004496 goto unexpected_end_of_string;
4497
4498 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004499 assert(*str < end);
4500 assert(**str == '}');
4501 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004502
Eric V. Smith451d0e32016-09-09 21:56:20 -04004503 /* And now create the FormattedValue node that represents this
4504 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004505 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004506 format_spec, LINENO(n), n->n_col_offset,
4507 c->c_arena);
4508 if (!*expression)
4509 return -1;
4510
4511 return 0;
4512
4513unexpected_end_of_string:
4514 ast_error(c, n, "f-string: expecting '}'");
4515 return -1;
4516}
4517
4518/* Return -1 on error.
4519
4520 Return 0 if we have a literal (possible zero length) and an
4521 expression (zero length if at the end of the string.
4522
4523 Return 1 if we have a literal, but no expression, and we want the
4524 caller to call us again. This is used to deal with doubled
4525 braces.
4526
4527 When called multiple times on the string 'a{{b{0}c', this function
4528 will return:
4529
4530 1. the literal 'a{' with no expression, and a return value
4531 of 1. Despite the fact that there's no expression, the return
4532 value of 1 means we're not finished yet.
4533
4534 2. the literal 'b' and the expression '0', with a return value of
4535 0. The fact that there's an expression means we're not finished.
4536
4537 3. literal 'c' with no expression and a return value of 0. The
4538 combination of the return value of 0 with no expression means
4539 we're finished.
4540*/
4541static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004542fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4543 int recurse_lvl, PyObject **literal,
4544 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004545 struct compiling *c, const node *n)
4546{
4547 int result;
4548
4549 assert(*literal == NULL && *expression == NULL);
4550
4551 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004552 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004553 if (result < 0)
4554 goto error;
4555
4556 assert(result == 0 || result == 1);
4557
4558 if (result == 1)
4559 /* We have a literal, but don't look at the expression. */
4560 return 1;
4561
Eric V. Smith451d0e32016-09-09 21:56:20 -04004562 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004563 /* We're at the end of the string or the end of a nested
4564 f-string: no expression. The top-level error case where we
4565 expect to be at the end of the string but we're at a '}' is
4566 handled later. */
4567 return 0;
4568
4569 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004570 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004571
Eric V. Smith451d0e32016-09-09 21:56:20 -04004572 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004573 goto error;
4574
4575 return 0;
4576
4577error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004578 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004579 return -1;
4580}
4581
4582#define EXPRLIST_N_CACHED 64
4583
4584typedef struct {
4585 /* Incrementally build an array of expr_ty, so be used in an
4586 asdl_seq. Cache some small but reasonably sized number of
4587 expr_ty's, and then after that start dynamically allocating,
4588 doubling the number allocated each time. Note that the f-string
4589 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4590 Str for the literal 'a'. So you add expr_ty's about twice as
4591 fast as you add exressions in an f-string. */
4592
4593 Py_ssize_t allocated; /* Number we've allocated. */
4594 Py_ssize_t size; /* Number we've used. */
4595 expr_ty *p; /* Pointer to the memory we're actually
4596 using. Will point to 'data' until we
4597 start dynamically allocating. */
4598 expr_ty data[EXPRLIST_N_CACHED];
4599} ExprList;
4600
4601#ifdef NDEBUG
4602#define ExprList_check_invariants(l)
4603#else
4604static void
4605ExprList_check_invariants(ExprList *l)
4606{
4607 /* Check our invariants. Make sure this object is "live", and
4608 hasn't been deallocated. */
4609 assert(l->size >= 0);
4610 assert(l->p != NULL);
4611 if (l->size <= EXPRLIST_N_CACHED)
4612 assert(l->data == l->p);
4613}
4614#endif
4615
4616static void
4617ExprList_Init(ExprList *l)
4618{
4619 l->allocated = EXPRLIST_N_CACHED;
4620 l->size = 0;
4621
4622 /* Until we start allocating dynamically, p points to data. */
4623 l->p = l->data;
4624
4625 ExprList_check_invariants(l);
4626}
4627
4628static int
4629ExprList_Append(ExprList *l, expr_ty exp)
4630{
4631 ExprList_check_invariants(l);
4632 if (l->size >= l->allocated) {
4633 /* We need to alloc (or realloc) the memory. */
4634 Py_ssize_t new_size = l->allocated * 2;
4635
4636 /* See if we've ever allocated anything dynamically. */
4637 if (l->p == l->data) {
4638 Py_ssize_t i;
4639 /* We're still using the cached data. Switch to
4640 alloc-ing. */
4641 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4642 if (!l->p)
4643 return -1;
4644 /* Copy the cached data into the new buffer. */
4645 for (i = 0; i < l->size; i++)
4646 l->p[i] = l->data[i];
4647 } else {
4648 /* Just realloc. */
4649 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4650 if (!tmp) {
4651 PyMem_RawFree(l->p);
4652 l->p = NULL;
4653 return -1;
4654 }
4655 l->p = tmp;
4656 }
4657
4658 l->allocated = new_size;
4659 assert(l->allocated == 2 * l->size);
4660 }
4661
4662 l->p[l->size++] = exp;
4663
4664 ExprList_check_invariants(l);
4665 return 0;
4666}
4667
4668static void
4669ExprList_Dealloc(ExprList *l)
4670{
4671 ExprList_check_invariants(l);
4672
4673 /* If there's been an error, or we've never dynamically allocated,
4674 do nothing. */
4675 if (!l->p || l->p == l->data) {
4676 /* Do nothing. */
4677 } else {
4678 /* We have dynamically allocated. Free the memory. */
4679 PyMem_RawFree(l->p);
4680 }
4681 l->p = NULL;
4682 l->size = -1;
4683}
4684
4685static asdl_seq *
4686ExprList_Finish(ExprList *l, PyArena *arena)
4687{
4688 asdl_seq *seq;
4689
4690 ExprList_check_invariants(l);
4691
4692 /* Allocate the asdl_seq and copy the expressions in to it. */
4693 seq = _Py_asdl_seq_new(l->size, arena);
4694 if (seq) {
4695 Py_ssize_t i;
4696 for (i = 0; i < l->size; i++)
4697 asdl_seq_SET(seq, i, l->p[i]);
4698 }
4699 ExprList_Dealloc(l);
4700 return seq;
4701}
4702
4703/* The FstringParser is designed to add a mix of strings and
4704 f-strings, and concat them together as needed. Ultimately, it
4705 generates an expr_ty. */
4706typedef struct {
4707 PyObject *last_str;
4708 ExprList expr_list;
4709} FstringParser;
4710
4711#ifdef NDEBUG
4712#define FstringParser_check_invariants(state)
4713#else
4714static void
4715FstringParser_check_invariants(FstringParser *state)
4716{
4717 if (state->last_str)
4718 assert(PyUnicode_CheckExact(state->last_str));
4719 ExprList_check_invariants(&state->expr_list);
4720}
4721#endif
4722
4723static void
4724FstringParser_Init(FstringParser *state)
4725{
4726 state->last_str = NULL;
4727 ExprList_Init(&state->expr_list);
4728 FstringParser_check_invariants(state);
4729}
4730
4731static void
4732FstringParser_Dealloc(FstringParser *state)
4733{
4734 FstringParser_check_invariants(state);
4735
4736 Py_XDECREF(state->last_str);
4737 ExprList_Dealloc(&state->expr_list);
4738}
4739
4740/* Make a Str node, but decref the PyUnicode object being added. */
4741static expr_ty
4742make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4743{
4744 PyObject *s = *str;
4745 *str = NULL;
4746 assert(PyUnicode_CheckExact(s));
4747 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4748 Py_DECREF(s);
4749 return NULL;
4750 }
4751 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4752}
4753
4754/* Add a non-f-string (that is, a regular literal string). str is
4755 decref'd. */
4756static int
4757FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4758{
4759 FstringParser_check_invariants(state);
4760
4761 assert(PyUnicode_CheckExact(str));
4762
4763 if (PyUnicode_GET_LENGTH(str) == 0) {
4764 Py_DECREF(str);
4765 return 0;
4766 }
4767
4768 if (!state->last_str) {
4769 /* We didn't have a string before, so just remember this one. */
4770 state->last_str = str;
4771 } else {
4772 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004773 PyUnicode_AppendAndDel(&state->last_str, str);
4774 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004775 return -1;
4776 }
4777 FstringParser_check_invariants(state);
4778 return 0;
4779}
4780
Eric V. Smith451d0e32016-09-09 21:56:20 -04004781/* Parse an f-string. The f-string is in *str to end, with no
4782 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004783static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004784FstringParser_ConcatFstring(FstringParser *state, const char **str,
4785 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004786 struct compiling *c, const node *n)
4787{
4788 FstringParser_check_invariants(state);
4789
4790 /* Parse the f-string. */
4791 while (1) {
4792 PyObject *literal = NULL;
4793 expr_ty expression = NULL;
4794
4795 /* If there's a zero length literal in front of the
4796 expression, literal will be NULL. If we're at the end of
4797 the f-string, expression will be NULL (unless result == 1,
4798 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004799 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004800 &literal, &expression,
4801 c, n);
4802 if (result < 0)
4803 return -1;
4804
4805 /* Add the literal, if any. */
4806 if (!literal) {
4807 /* Do nothing. Just leave last_str alone (and possibly
4808 NULL). */
4809 } else if (!state->last_str) {
4810 state->last_str = literal;
4811 literal = NULL;
4812 } else {
4813 /* We have a literal, concatenate it. */
4814 assert(PyUnicode_GET_LENGTH(literal) != 0);
4815 if (FstringParser_ConcatAndDel(state, literal) < 0)
4816 return -1;
4817 literal = NULL;
4818 }
4819 assert(!state->last_str ||
4820 PyUnicode_GET_LENGTH(state->last_str) != 0);
4821
4822 /* We've dealt with the literal now. It can't be leaked on further
4823 errors. */
4824 assert(literal == NULL);
4825
4826 /* See if we should just loop around to get the next literal
4827 and expression, while ignoring the expression this
4828 time. This is used for un-doubling braces, as an
4829 optimization. */
4830 if (result == 1)
4831 continue;
4832
4833 if (!expression)
4834 /* We're done with this f-string. */
4835 break;
4836
4837 /* We know we have an expression. Convert any existing string
4838 to a Str node. */
4839 if (!state->last_str) {
4840 /* Do nothing. No previous literal. */
4841 } else {
4842 /* Convert the existing last_str literal to a Str node. */
4843 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4844 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4845 return -1;
4846 }
4847
4848 if (ExprList_Append(&state->expr_list, expression) < 0)
4849 return -1;
4850 }
4851
Eric V. Smith235a6f02015-09-19 14:51:32 -04004852 /* If recurse_lvl is zero, then we must be at the end of the
4853 string. Otherwise, we must be at a right brace. */
4854
Eric V. Smith451d0e32016-09-09 21:56:20 -04004855 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004856 ast_error(c, n, "f-string: unexpected end of string");
4857 return -1;
4858 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004859 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004860 ast_error(c, n, "f-string: expecting '}'");
4861 return -1;
4862 }
4863
4864 FstringParser_check_invariants(state);
4865 return 0;
4866}
4867
4868/* Convert the partial state reflected in last_str and expr_list to an
4869 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4870static expr_ty
4871FstringParser_Finish(FstringParser *state, struct compiling *c,
4872 const node *n)
4873{
4874 asdl_seq *seq;
4875
4876 FstringParser_check_invariants(state);
4877
4878 /* If we're just a constant string with no expressions, return
4879 that. */
4880 if(state->expr_list.size == 0) {
4881 if (!state->last_str) {
4882 /* Create a zero length string. */
4883 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4884 if (!state->last_str)
4885 goto error;
4886 }
4887 return make_str_node_and_del(&state->last_str, c, n);
4888 }
4889
4890 /* Create a Str node out of last_str, if needed. It will be the
4891 last node in our expression list. */
4892 if (state->last_str) {
4893 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4894 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4895 goto error;
4896 }
4897 /* This has already been freed. */
4898 assert(state->last_str == NULL);
4899
4900 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4901 if (!seq)
4902 goto error;
4903
4904 /* If there's only one expression, return it. Otherwise, we need
4905 to join them together. */
4906 if (seq->size == 1)
4907 return seq->elements[0];
4908
4909 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4910
4911error:
4912 FstringParser_Dealloc(state);
4913 return NULL;
4914}
4915
Eric V. Smith451d0e32016-09-09 21:56:20 -04004916/* Given an f-string (with no 'f' or quotes) that's in *str and ends
4917 at end, parse it into an expr_ty. Return NULL on error. Adjust
4918 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004920fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921 struct compiling *c, const node *n)
4922{
4923 FstringParser state;
4924
4925 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004926 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927 c, n) < 0) {
4928 FstringParser_Dealloc(&state);
4929 return NULL;
4930 }
4931
4932 return FstringParser_Finish(&state, c, n);
4933}
4934
4935/* n is a Python string literal, including the bracketing quote
4936 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04004937 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04004938 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04004939 *fstr and *fstrlen to the unparsed string object. Return 0 if no
4940 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004941*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04004942static int
4943parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
4944 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004945{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004946 size_t len;
4947 const char *s = STR(n);
4948 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004949 int fmode = 0;
4950 *bytesmode = 0;
4951 *rawmode = 0;
4952 *result = NULL;
4953 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004954 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004955 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004956 if (quote == 'b' || quote == 'B') {
4957 quote = *++s;
4958 *bytesmode = 1;
4959 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004960 else if (quote == 'u' || quote == 'U') {
4961 quote = *++s;
4962 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004963 else if (quote == 'r' || quote == 'R') {
4964 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004965 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004966 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004967 else if (quote == 'f' || quote == 'F') {
4968 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004969 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004970 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004971 else {
4972 break;
4973 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004974 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004975 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004976 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004977 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004978 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004980 if (quote != '\'' && quote != '\"') {
4981 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004982 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004983 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004985 s++;
4986 len = strlen(s);
4987 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004988 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004989 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004990 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004991 }
4992 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004994 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004995 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004996 }
4997 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004998 /* A triple quoted string. We've already skipped one quote at
4999 the start and one at the end of the string. Now skip the
5000 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005001 s += 2;
5002 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005003 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005004 if (s[--len] != quote || s[--len] != quote) {
5005 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005006 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005007 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005008 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005009
Eric V. Smith451d0e32016-09-09 21:56:20 -04005010 if (fmode) {
5011 /* Just return the bytes. The caller will parse the resulting
5012 string. */
5013 *fstr = s;
5014 *fstrlen = len;
5015 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005016 }
5017
Eric V. Smith451d0e32016-09-09 21:56:20 -04005018 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005019 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005020 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005021 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005022 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005023 const char *ch;
5024 for (ch = s; *ch; ch++) {
5025 if (Py_CHARMASK(*ch) >= 0x80) {
5026 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005027 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005028 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005029 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005030 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005031 if (*rawmode)
5032 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005033 else
Eric V. Smith451d0e32016-09-09 21:56:20 -04005034 *result = PyBytes_DecodeEscape(s, len, NULL, /* ignored */ 0, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005035 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005036 if (*rawmode)
5037 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005038 else
Eric V. Smith451d0e32016-09-09 21:56:20 -04005039 *result = decode_unicode_with_escapes(c, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005040 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005041 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005042}
5043
Eric V. Smith235a6f02015-09-19 14:51:32 -04005044/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5045 each STRING atom, and process it as needed. For bytes, just
5046 concatenate them together, and the result will be a Bytes node. For
5047 normal strings and f-strings, concatenate them together. The result
5048 will be a Str node if there were no f-strings; a FormattedValue
5049 node if there's just an f-string (with no leading or trailing
5050 literals), or a JoinedStr node if there are multiple f-strings or
5051 any literals involved. */
5052static expr_ty
5053parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005054{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005055 int bytesmode = 0;
5056 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005057 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005058
5059 FstringParser state;
5060 FstringParser_Init(&state);
5061
5062 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005063 int this_bytesmode;
5064 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005065 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005066 const char *fstr;
5067 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005068
5069 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005070 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5071 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005072 goto error;
5073
5074 /* Check that we're not mixing bytes with unicode. */
5075 if (i != 0 && bytesmode != this_bytesmode) {
5076 ast_error(c, n, "cannot mix bytes and nonbytes literals");
5077 Py_DECREF(s);
5078 goto error;
5079 }
5080 bytesmode = this_bytesmode;
5081
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082 if (fstr != NULL) {
5083 int result;
5084 assert(s == NULL && !bytesmode);
5085 /* This is an f-string. Parse and concatenate it. */
5086 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5087 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005088 if (result < 0)
5089 goto error;
5090 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005091 assert(bytesmode ? PyBytes_CheckExact(s) :
5092 PyUnicode_CheckExact(s));
5093
5094 /* A string or byte string. */
5095 assert(s != NULL && fstr == NULL);
5096 if (bytesmode) {
5097 /* For bytes, concat as we go. */
5098 if (i == 0) {
5099 /* First time, just remember this value. */
5100 bytes_str = s;
5101 } else {
5102 PyBytes_ConcatAndDel(&bytes_str, s);
5103 if (!bytes_str)
5104 goto error;
5105 }
5106 } else {
5107 assert(s != NULL && fstr == NULL);
5108 /* This is a regular string. Concatenate it. */
5109 if (FstringParser_ConcatAndDel(&state, s) < 0)
5110 goto error;
5111 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005112 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005113 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005114 if (bytesmode) {
5115 /* Just return the bytes object and we're done. */
5116 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5117 goto error;
5118 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5119 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005120
Eric V. Smith235a6f02015-09-19 14:51:32 -04005121 /* We're not a bytes string, bytes_str should never have been set. */
5122 assert(bytes_str == NULL);
5123
5124 return FstringParser_Finish(&state, c, n);
5125
5126error:
5127 Py_XDECREF(bytes_str);
5128 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005129 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005130}