blob: aa4acc9b2610a7886b806908403b38488b78b7d2 [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"
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
14
Benjamin Peterson832bfe22011-08-09 16:15:04 -050015static int validate_stmts(asdl_seq *);
16static int validate_exprs(asdl_seq *, expr_context_ty, int);
17static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
18static int validate_stmt(stmt_ty);
19static int validate_expr(expr_ty, expr_context_ty);
20
21static int
22validate_comprehension(asdl_seq *gens)
23{
24 int i;
25 if (!asdl_seq_LEN(gens)) {
26 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
27 return 0;
28 }
29 for (i = 0; i < asdl_seq_LEN(gens); i++) {
30 comprehension_ty comp = asdl_seq_GET(gens, i);
31 if (!validate_expr(comp->target, Store) ||
32 !validate_expr(comp->iter, Load) ||
33 !validate_exprs(comp->ifs, Load, 0))
34 return 0;
35 }
36 return 1;
37}
38
39static int
40validate_slice(slice_ty slice)
41{
42 switch (slice->kind) {
43 case Slice_kind:
44 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
45 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
46 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
47 case ExtSlice_kind: {
48 int i;
49 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
50 return 0;
51 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
52 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
53 return 0;
54 return 1;
55 }
56 case Index_kind:
57 return validate_expr(slice->v.Index.value, Load);
58 default:
59 PyErr_SetString(PyExc_SystemError, "unknown slice node");
60 return 0;
61 }
62}
63
64static int
65validate_keywords(asdl_seq *keywords)
66{
67 int i;
68 for (i = 0; i < asdl_seq_LEN(keywords); i++)
69 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
70 return 0;
71 return 1;
72}
73
74static int
75validate_args(asdl_seq *args)
76{
77 int i;
78 for (i = 0; i < asdl_seq_LEN(args); i++) {
79 arg_ty arg = asdl_seq_GET(args, i);
80 if (arg->annotation && !validate_expr(arg->annotation, Load))
81 return 0;
82 }
83 return 1;
84}
85
86static const char *
87expr_context_name(expr_context_ty ctx)
88{
89 switch (ctx) {
90 case Load:
91 return "Load";
92 case Store:
93 return "Store";
94 case Del:
95 return "Del";
96 case AugLoad:
97 return "AugLoad";
98 case AugStore:
99 return "AugStore";
100 case Param:
101 return "Param";
102 default:
103 assert(0);
104 return "(unknown)";
105 }
106}
107
108static int
109validate_arguments(arguments_ty args)
110{
111 if (!validate_args(args->args))
112 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700113 if (args->vararg && args->vararg->annotation
114 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500115 return 0;
116 }
117 if (!validate_args(args->kwonlyargs))
118 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100119 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700120 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500121 return 0;
122 }
123 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
124 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
125 return 0;
126 }
127 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
128 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
129 "kw_defaults on arguments");
130 return 0;
131 }
132 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
133}
134
135static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100136validate_constant(PyObject *value)
137{
138 if (value == Py_None || value == Py_Ellipsis)
139 return 1;
140
141 if (PyLong_CheckExact(value)
142 || PyFloat_CheckExact(value)
143 || PyComplex_CheckExact(value)
144 || PyBool_Check(value)
145 || PyUnicode_CheckExact(value)
146 || PyBytes_CheckExact(value))
147 return 1;
148
149 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
150 PyObject *it;
151
152 it = PyObject_GetIter(value);
153 if (it == NULL)
154 return 0;
155
156 while (1) {
157 PyObject *item = PyIter_Next(it);
158 if (item == NULL) {
159 if (PyErr_Occurred()) {
160 Py_DECREF(it);
161 return 0;
162 }
163 break;
164 }
165
166 if (!validate_constant(item)) {
167 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100168 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100169 return 0;
170 }
Victor Stinner726f6902016-01-27 00:11:47 +0100171 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100172 }
173
174 Py_DECREF(it);
175 return 1;
176 }
177
178 return 0;
179}
180
181static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500182validate_expr(expr_ty exp, expr_context_ty ctx)
183{
184 int check_ctx = 1;
185 expr_context_ty actual_ctx;
186
187 /* First check expression context. */
188 switch (exp->kind) {
189 case Attribute_kind:
190 actual_ctx = exp->v.Attribute.ctx;
191 break;
192 case Subscript_kind:
193 actual_ctx = exp->v.Subscript.ctx;
194 break;
195 case Starred_kind:
196 actual_ctx = exp->v.Starred.ctx;
197 break;
198 case Name_kind:
199 actual_ctx = exp->v.Name.ctx;
200 break;
201 case List_kind:
202 actual_ctx = exp->v.List.ctx;
203 break;
204 case Tuple_kind:
205 actual_ctx = exp->v.Tuple.ctx;
206 break;
207 default:
208 if (ctx != Load) {
209 PyErr_Format(PyExc_ValueError, "expression which can't be "
210 "assigned to in %s context", expr_context_name(ctx));
211 return 0;
212 }
213 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100214 /* set actual_ctx to prevent gcc warning */
215 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500216 }
217 if (check_ctx && actual_ctx != ctx) {
218 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
219 expr_context_name(ctx), expr_context_name(actual_ctx));
220 return 0;
221 }
222
223 /* Now validate expression. */
224 switch (exp->kind) {
225 case BoolOp_kind:
226 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
227 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
228 return 0;
229 }
230 return validate_exprs(exp->v.BoolOp.values, Load, 0);
231 case BinOp_kind:
232 return validate_expr(exp->v.BinOp.left, Load) &&
233 validate_expr(exp->v.BinOp.right, Load);
234 case UnaryOp_kind:
235 return validate_expr(exp->v.UnaryOp.operand, Load);
236 case Lambda_kind:
237 return validate_arguments(exp->v.Lambda.args) &&
238 validate_expr(exp->v.Lambda.body, Load);
239 case IfExp_kind:
240 return validate_expr(exp->v.IfExp.test, Load) &&
241 validate_expr(exp->v.IfExp.body, Load) &&
242 validate_expr(exp->v.IfExp.orelse, Load);
243 case Dict_kind:
244 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
245 PyErr_SetString(PyExc_ValueError,
246 "Dict doesn't have the same number of keys as values");
247 return 0;
248 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400249 /* null_ok=1 for keys expressions to allow dict unpacking to work in
250 dict literals, i.e. ``{**{a:b}}`` */
251 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
252 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500253 case Set_kind:
254 return validate_exprs(exp->v.Set.elts, Load, 0);
255#define COMP(NAME) \
256 case NAME ## _kind: \
257 return validate_comprehension(exp->v.NAME.generators) && \
258 validate_expr(exp->v.NAME.elt, Load);
259 COMP(ListComp)
260 COMP(SetComp)
261 COMP(GeneratorExp)
262#undef COMP
263 case DictComp_kind:
264 return validate_comprehension(exp->v.DictComp.generators) &&
265 validate_expr(exp->v.DictComp.key, Load) &&
266 validate_expr(exp->v.DictComp.value, Load);
267 case Yield_kind:
268 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500269 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000270 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400271 case Await_kind:
272 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500273 case Compare_kind:
274 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
275 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
276 return 0;
277 }
278 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
279 asdl_seq_LEN(exp->v.Compare.ops)) {
280 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
281 "of comparators and operands");
282 return 0;
283 }
284 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
285 validate_expr(exp->v.Compare.left, Load);
286 case Call_kind:
287 return validate_expr(exp->v.Call.func, Load) &&
288 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400289 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100290 case Constant_kind:
291 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100292 PyErr_Format(PyExc_TypeError,
293 "got an invalid type in Constant: %s",
294 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100295 return 0;
296 }
297 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500298 case Num_kind: {
299 PyObject *n = exp->v.Num.n;
300 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
301 !PyComplex_CheckExact(n)) {
302 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
303 return 0;
304 }
305 return 1;
306 }
307 case Str_kind: {
308 PyObject *s = exp->v.Str.s;
309 if (!PyUnicode_CheckExact(s)) {
310 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
311 return 0;
312 }
313 return 1;
314 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400315 case JoinedStr_kind:
316 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
317 case FormattedValue_kind:
318 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
319 return 0;
320 if (exp->v.FormattedValue.format_spec)
321 return validate_expr(exp->v.FormattedValue.format_spec, Load);
322 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 case Bytes_kind: {
324 PyObject *b = exp->v.Bytes.s;
325 if (!PyBytes_CheckExact(b)) {
326 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
327 return 0;
328 }
329 return 1;
330 }
331 case Attribute_kind:
332 return validate_expr(exp->v.Attribute.value, Load);
333 case Subscript_kind:
334 return validate_slice(exp->v.Subscript.slice) &&
335 validate_expr(exp->v.Subscript.value, Load);
336 case Starred_kind:
337 return validate_expr(exp->v.Starred.value, ctx);
338 case List_kind:
339 return validate_exprs(exp->v.List.elts, ctx, 0);
340 case Tuple_kind:
341 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
342 /* These last cases don't have any checking. */
343 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500344 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500345 case Ellipsis_kind:
346 return 1;
347 default:
348 PyErr_SetString(PyExc_SystemError, "unexpected expression");
349 return 0;
350 }
351}
352
353static int
354validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
355{
356 if (asdl_seq_LEN(seq))
357 return 1;
358 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
359 return 0;
360}
361
362static int
363validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
364{
365 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
366 validate_exprs(targets, ctx, 0);
367}
368
369static int
370validate_body(asdl_seq *body, const char *owner)
371{
372 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
373}
374
375static int
376validate_stmt(stmt_ty stmt)
377{
378 int i;
379 switch (stmt->kind) {
380 case FunctionDef_kind:
381 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
382 validate_arguments(stmt->v.FunctionDef.args) &&
383 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
384 (!stmt->v.FunctionDef.returns ||
385 validate_expr(stmt->v.FunctionDef.returns, Load));
386 case ClassDef_kind:
387 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
388 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
389 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400390 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500391 case Return_kind:
392 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
393 case Delete_kind:
394 return validate_assignlist(stmt->v.Delete.targets, Del);
395 case Assign_kind:
396 return validate_assignlist(stmt->v.Assign.targets, Store) &&
397 validate_expr(stmt->v.Assign.value, Load);
398 case AugAssign_kind:
399 return validate_expr(stmt->v.AugAssign.target, Store) &&
400 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700401 case AnnAssign_kind:
402 if (stmt->v.AnnAssign.target->kind != Name_kind &&
403 stmt->v.AnnAssign.simple) {
404 PyErr_SetString(PyExc_TypeError,
405 "AnnAssign with simple non-Name target");
406 return 0;
407 }
408 return validate_expr(stmt->v.AnnAssign.target, Store) &&
409 (!stmt->v.AnnAssign.value ||
410 validate_expr(stmt->v.AnnAssign.value, Load)) &&
411 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500412 case For_kind:
413 return validate_expr(stmt->v.For.target, Store) &&
414 validate_expr(stmt->v.For.iter, Load) &&
415 validate_body(stmt->v.For.body, "For") &&
416 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncFor_kind:
418 return validate_expr(stmt->v.AsyncFor.target, Store) &&
419 validate_expr(stmt->v.AsyncFor.iter, Load) &&
420 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
421 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500422 case While_kind:
423 return validate_expr(stmt->v.While.test, Load) &&
424 validate_body(stmt->v.While.body, "While") &&
425 validate_stmts(stmt->v.While.orelse);
426 case If_kind:
427 return validate_expr(stmt->v.If.test, Load) &&
428 validate_body(stmt->v.If.body, "If") &&
429 validate_stmts(stmt->v.If.orelse);
430 case With_kind:
431 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
432 return 0;
433 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
434 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
435 if (!validate_expr(item->context_expr, Load) ||
436 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
437 return 0;
438 }
439 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400440 case AsyncWith_kind:
441 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
442 return 0;
443 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
444 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
445 if (!validate_expr(item->context_expr, Load) ||
446 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
447 return 0;
448 }
449 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500450 case Raise_kind:
451 if (stmt->v.Raise.exc) {
452 return validate_expr(stmt->v.Raise.exc, Load) &&
453 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
454 }
455 if (stmt->v.Raise.cause) {
456 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
457 return 0;
458 }
459 return 1;
460 case Try_kind:
461 if (!validate_body(stmt->v.Try.body, "Try"))
462 return 0;
463 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
464 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
465 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
466 return 0;
467 }
468 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
469 asdl_seq_LEN(stmt->v.Try.orelse)) {
470 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
471 return 0;
472 }
473 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
474 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
475 if ((handler->v.ExceptHandler.type &&
476 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
477 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
478 return 0;
479 }
480 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
481 validate_stmts(stmt->v.Try.finalbody)) &&
482 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
483 validate_stmts(stmt->v.Try.orelse));
484 case Assert_kind:
485 return validate_expr(stmt->v.Assert.test, Load) &&
486 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
487 case Import_kind:
488 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
489 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300490 if (stmt->v.ImportFrom.level < 0) {
491 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500492 return 0;
493 }
494 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
495 case Global_kind:
496 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
497 case Nonlocal_kind:
498 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
499 case Expr_kind:
500 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400501 case AsyncFunctionDef_kind:
502 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
503 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
504 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
505 (!stmt->v.AsyncFunctionDef.returns ||
506 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500507 case Pass_kind:
508 case Break_kind:
509 case Continue_kind:
510 return 1;
511 default:
512 PyErr_SetString(PyExc_SystemError, "unexpected statement");
513 return 0;
514 }
515}
516
517static int
518validate_stmts(asdl_seq *seq)
519{
520 int i;
521 for (i = 0; i < asdl_seq_LEN(seq); i++) {
522 stmt_ty stmt = asdl_seq_GET(seq, i);
523 if (stmt) {
524 if (!validate_stmt(stmt))
525 return 0;
526 }
527 else {
528 PyErr_SetString(PyExc_ValueError,
529 "None disallowed in statement list");
530 return 0;
531 }
532 }
533 return 1;
534}
535
536static int
537validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
538{
539 int i;
540 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
541 expr_ty expr = asdl_seq_GET(exprs, i);
542 if (expr) {
543 if (!validate_expr(expr, ctx))
544 return 0;
545 }
546 else if (!null_ok) {
547 PyErr_SetString(PyExc_ValueError,
548 "None disallowed in expression list");
549 return 0;
550 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100551
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500552 }
553 return 1;
554}
555
556int
557PyAST_Validate(mod_ty mod)
558{
559 int res = 0;
560
561 switch (mod->kind) {
562 case Module_kind:
563 res = validate_stmts(mod->v.Module.body);
564 break;
565 case Interactive_kind:
566 res = validate_stmts(mod->v.Interactive.body);
567 break;
568 case Expression_kind:
569 res = validate_expr(mod->v.Expression.body, Load);
570 break;
571 case Suite_kind:
572 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
573 break;
574 default:
575 PyErr_SetString(PyExc_SystemError, "impossible module node");
576 res = 0;
577 break;
578 }
579 return res;
580}
581
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500582/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500583#include "grammar.h"
584#include "parsetok.h"
585#include "graminit.h"
586
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587/* Data structure used internally */
588struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400589 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200590 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500591 PyObject *c_normalize; /* Normalization function from unicodedata. */
592 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593};
594
595static asdl_seq *seq_for_testlist(struct compiling *, const node *);
596static expr_ty ast_for_expr(struct compiling *, const node *);
597static stmt_ty ast_for_stmt(struct compiling *, const node *);
598static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000599static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
600 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000601static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000602static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603
Yury Selivanov75445082015-05-11 22:57:16 -0400604static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
605static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
606
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607/* Note different signature for ast_for_call */
608static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
609
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000610static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400611static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612
Nick Coghlan650f0d02007-04-15 12:05:43 +0000613#define COMP_GENEXP 0
614#define COMP_LISTCOMP 1
615#define COMP_SETCOMP 2
616
Benjamin Peterson55e00432012-01-16 17:22:31 -0500617static int
618init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000619{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500620 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
621 if (!m)
622 return 0;
623 c->c_normalize = PyObject_GetAttrString(m, "normalize");
624 Py_DECREF(m);
625 if (!c->c_normalize)
626 return 0;
627 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500628 if (!c->c_normalize_args) {
629 Py_CLEAR(c->c_normalize);
630 return 0;
631 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200632 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500633 return 1;
634}
635
636static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400637new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500638{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400639 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500640 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000641 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500642 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500643 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 /* Check whether there are non-ASCII characters in the
645 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500646 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200647 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500648 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500649 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200650 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500651 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500652 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
653 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500654 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200655 if (!id2)
656 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000658 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000659 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200660 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
661 Py_DECREF(id);
662 return NULL;
663 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000664 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665}
666
Benjamin Peterson55e00432012-01-16 17:22:31 -0500667#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400670ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400672 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673
Victor Stinner14e461d2013-08-26 22:28:21 +0200674 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000676 Py_INCREF(Py_None);
677 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200679 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400680 if (!tmp)
681 return 0;
682 errstr = PyUnicode_FromString(errmsg);
683 if (!errstr) {
684 Py_DECREF(tmp);
685 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000686 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000687 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 Py_DECREF(errstr);
689 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400690 if (value) {
691 PyErr_SetObject(PyExc_SyntaxError, value);
692 Py_DECREF(value);
693 }
694 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695}
696
697/* num_stmts() returns number of contained statements.
698
699 Use this routine to determine how big a sequence is needed for
700 the statements in a parse tree. Its raison d'etre is this bit of
701 grammar:
702
703 stmt: simple_stmt | compound_stmt
704 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
705
706 A simple_stmt can contain multiple small_stmt elements joined
707 by semicolons. If the arg is a simple_stmt, the number of
708 small_stmt elements is returned.
709*/
710
711static int
712num_stmts(const node *n)
713{
714 int i, l;
715 node *ch;
716
717 switch (TYPE(n)) {
718 case single_input:
719 if (TYPE(CHILD(n, 0)) == NEWLINE)
720 return 0;
721 else
722 return num_stmts(CHILD(n, 0));
723 case file_input:
724 l = 0;
725 for (i = 0; i < NCH(n); i++) {
726 ch = CHILD(n, i);
727 if (TYPE(ch) == stmt)
728 l += num_stmts(ch);
729 }
730 return l;
731 case stmt:
732 return num_stmts(CHILD(n, 0));
733 case compound_stmt:
734 return 1;
735 case simple_stmt:
736 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
737 case suite:
738 if (NCH(n) == 1)
739 return num_stmts(CHILD(n, 0));
740 else {
741 l = 0;
742 for (i = 2; i < (NCH(n) - 1); i++)
743 l += num_stmts(CHILD(n, i));
744 return l;
745 }
746 default: {
747 char buf[128];
748
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000749 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 TYPE(n), NCH(n));
751 Py_FatalError(buf);
752 }
753 }
754 assert(0);
755 return 0;
756}
757
758/* Transform the CST rooted at node * to the appropriate AST
759*/
760
761mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200762PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
763 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000765 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 asdl_seq *stmts = NULL;
767 stmt_ty s;
768 node *ch;
769 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500770 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400772 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200773 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400774 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800775 c.c_normalize = NULL;
776 c.c_normalize_args = NULL;
777
778 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780
Jeremy Hyltona8293132006-02-28 17:58:27 +0000781 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 switch (TYPE(n)) {
783 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200784 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500786 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 for (i = 0; i < NCH(n) - 1; i++) {
788 ch = CHILD(n, i);
789 if (TYPE(ch) == NEWLINE)
790 continue;
791 REQ(ch, stmt);
792 num = num_stmts(ch);
793 if (num == 1) {
794 s = ast_for_stmt(&c, ch);
795 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500796 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000797 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
799 else {
800 ch = CHILD(ch, 0);
801 REQ(ch, simple_stmt);
802 for (j = 0; j < num; j++) {
803 s = ast_for_stmt(&c, CHILD(ch, j * 2));
804 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000806 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808 }
809 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500810 res = Module(stmts, arena);
811 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 case eval_input: {
813 expr_ty testlist_ast;
814
Nick Coghlan650f0d02007-04-15 12:05:43 +0000815 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000816 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500818 goto out;
819 res = Expression(testlist_ast, arena);
820 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 }
822 case single_input:
823 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200824 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500826 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
828 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000829 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500830 goto out;
831 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 }
833 else {
834 n = CHILD(n, 0);
835 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200836 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000840 s = ast_for_stmt(&c, n);
841 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 asdl_seq_SET(stmts, 0, s);
844 }
845 else {
846 /* Only a simple_stmt can contain multiple statements. */
847 REQ(n, simple_stmt);
848 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (TYPE(CHILD(n, i)) == NEWLINE)
850 break;
851 s = ast_for_stmt(&c, CHILD(n, i));
852 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500853 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 asdl_seq_SET(stmts, i / 2, s);
855 }
856 }
857
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500860 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000862 PyErr_Format(PyExc_SystemError,
863 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500864 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500866 out:
867 if (c.c_normalize) {
868 Py_DECREF(c.c_normalize);
869 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
870 Py_DECREF(c.c_normalize_args);
871 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500872 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873}
874
Victor Stinner14e461d2013-08-26 22:28:21 +0200875mod_ty
876PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
877 PyArena *arena)
878{
879 mod_ty mod;
880 PyObject *filename;
881 filename = PyUnicode_DecodeFSDefault(filename_str);
882 if (filename == NULL)
883 return NULL;
884 mod = PyAST_FromNodeObject(n, flags, filename, arena);
885 Py_DECREF(filename);
886 return mod;
887
888}
889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
891*/
892
893static operator_ty
894get_operator(const node *n)
895{
896 switch (TYPE(n)) {
897 case VBAR:
898 return BitOr;
899 case CIRCUMFLEX:
900 return BitXor;
901 case AMPER:
902 return BitAnd;
903 case LEFTSHIFT:
904 return LShift;
905 case RIGHTSHIFT:
906 return RShift;
907 case PLUS:
908 return Add;
909 case MINUS:
910 return Sub;
911 case STAR:
912 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400913 case AT:
914 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 case SLASH:
916 return Div;
917 case DOUBLESLASH:
918 return FloorDiv;
919 case PERCENT:
920 return Mod;
921 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000922 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923 }
924}
925
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200926static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000927 "None",
928 "True",
929 "False",
930 NULL,
931};
932
933static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400934forbidden_name(struct compiling *c, identifier name, const node *n,
935 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000936{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000937 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200938 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400939 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000940 return 1;
941 }
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +0200942 if (_PyUnicode_EqualToASCIIString(name, "async") ||
943 _PyUnicode_EqualToASCIIString(name, "await"))
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400944 {
945 PyObject *message = PyUnicode_FromString(
946 "'async' and 'await' will become reserved keywords"
947 " in Python 3.7");
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500948 int ret;
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400949 if (message == NULL) {
950 return 1;
951 }
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500952 ret = PyErr_WarnExplicitObject(
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400953 PyExc_DeprecationWarning,
954 message,
955 c->c_filename,
956 LINENO(n),
957 NULL,
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500958 NULL);
959 Py_DECREF(message);
960 if (ret < 0) {
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400961 return 1;
962 }
963 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000964 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200965 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000966 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200967 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400968 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000969 return 1;
970 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000971 }
972 }
973 return 0;
974}
975
Jeremy Hyltona8293132006-02-28 17:58:27 +0000976/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977
978 Only sets context for expr kinds that "can appear in assignment context"
979 (according to ../Parser/Python.asdl). For other expr kinds, it sets
980 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981*/
982
983static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000984set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985{
986 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000987 /* If a particular expression type can't be used for assign / delete,
988 set expr_name to its name and an error message will be generated.
989 */
990 const char* expr_name = NULL;
991
992 /* The ast defines augmented store and load contexts, but the
993 implementation here doesn't actually use them. The code may be
994 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000995 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000996 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000997 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000998 */
999 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
1001 switch (e->kind) {
1002 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001003 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001004 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001005 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001006 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001008 e->v.Subscript.ctx = ctx;
1009 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001010 case Starred_kind:
1011 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001012 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001013 return 0;
1014 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001016 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001017 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001018 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001019 }
1020 e->v.Name.ctx = ctx;
1021 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 e->v.List.ctx = ctx;
1024 s = e->v.List.elts;
1025 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001027 e->v.Tuple.ctx = ctx;
1028 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001029 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001030 case Lambda_kind:
1031 expr_name = "lambda";
1032 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001034 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001035 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001036 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001038 case UnaryOp_kind:
1039 expr_name = "operator";
1040 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001042 expr_name = "generator expression";
1043 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001044 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001045 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001046 expr_name = "yield expression";
1047 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001048 case Await_kind:
1049 expr_name = "await expression";
1050 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001051 case ListComp_kind:
1052 expr_name = "list comprehension";
1053 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001054 case SetComp_kind:
1055 expr_name = "set comprehension";
1056 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001057 case DictComp_kind:
1058 expr_name = "dict comprehension";
1059 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001060 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001061 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 case Num_kind:
1063 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001064 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001065 case JoinedStr_kind:
1066 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001067 expr_name = "literal";
1068 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001069 case NameConstant_kind:
1070 expr_name = "keyword";
1071 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001072 case Ellipsis_kind:
1073 expr_name = "Ellipsis";
1074 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001075 case Compare_kind:
1076 expr_name = "comparison";
1077 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001078 case IfExp_kind:
1079 expr_name = "conditional expression";
1080 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001081 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 PyErr_Format(PyExc_SystemError,
1083 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001084 e->kind, e->lineno);
1085 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001087 /* Check for error string set by switch */
1088 if (expr_name) {
1089 char buf[300];
1090 PyOS_snprintf(buf, sizeof(buf),
1091 "can't %s %s",
1092 ctx == Store ? "assign to" : "delete",
1093 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001094 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001095 }
1096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 */
1100 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001101 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102
Thomas Wouters89f507f2006-12-13 04:49:30 +00001103 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001104 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001105 return 0;
1106 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 }
1108 return 1;
1109}
1110
1111static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001112ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113{
1114 REQ(n, augassign);
1115 n = CHILD(n, 0);
1116 switch (STR(n)[0]) {
1117 case '+':
1118 return Add;
1119 case '-':
1120 return Sub;
1121 case '/':
1122 if (STR(n)[1] == '/')
1123 return FloorDiv;
1124 else
1125 return Div;
1126 case '%':
1127 return Mod;
1128 case '<':
1129 return LShift;
1130 case '>':
1131 return RShift;
1132 case '&':
1133 return BitAnd;
1134 case '^':
1135 return BitXor;
1136 case '|':
1137 return BitOr;
1138 case '*':
1139 if (STR(n)[1] == '*')
1140 return Pow;
1141 else
1142 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001143 case '@':
1144 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001146 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 }
1149}
1150
1151static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001152ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001154 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 |'is' 'not'
1156 */
1157 REQ(n, comp_op);
1158 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 n = CHILD(n, 0);
1160 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 case LESS:
1162 return Lt;
1163 case GREATER:
1164 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 return Eq;
1167 case LESSEQUAL:
1168 return LtE;
1169 case GREATEREQUAL:
1170 return GtE;
1171 case NOTEQUAL:
1172 return NotEq;
1173 case NAME:
1174 if (strcmp(STR(n), "in") == 0)
1175 return In;
1176 if (strcmp(STR(n), "is") == 0)
1177 return Is;
Victor Stinnerc0e77362017-09-12 16:09:44 -07001178 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001180 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001182 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001183 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 }
1185 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001186 /* handle "not in" and "is not" */
1187 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 case NAME:
1189 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1190 return NotIn;
1191 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1192 return IsNot;
Victor Stinnerc0e77362017-09-12 16:09:44 -07001193 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001195 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001198 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 }
Neal Norwitz79792652005-11-14 04:25:03 +00001200 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001202 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203}
1204
1205static asdl_seq *
1206seq_for_testlist(struct compiling *c, const node *n)
1207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001209 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1210 */
Armin Rigo31441302005-10-21 12:57:31 +00001211 asdl_seq *seq;
1212 expr_ty expression;
1213 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001214 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001216 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 if (!seq)
1218 return NULL;
1219
1220 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001222 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223
Benjamin Peterson4905e802009-09-27 02:43:28 +00001224 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001225 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227
1228 assert(i / 2 < seq->size);
1229 asdl_seq_SET(seq, i / 2, expression);
1230 }
1231 return seq;
1232}
1233
Neal Norwitzc1505362006-12-28 06:47:50 +00001234static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001235ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001236{
1237 identifier name;
1238 expr_ty annotation = NULL;
1239 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001240 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001241
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001242 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001243 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001244 name = NEW_IDENTIFIER(ch);
1245 if (!name)
1246 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001247 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001248 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001249
1250 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1251 annotation = ast_for_expr(c, CHILD(n, 2));
1252 if (!annotation)
1253 return NULL;
1254 }
1255
Victor Stinnerc106c682015-11-06 17:01:48 +01001256 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001257 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001258 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001259 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260}
1261
Guido van Rossum4f72a782006-10-27 23:31:49 +00001262/* returns -1 if failed to handle keyword only arguments
1263 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001264 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001265 ^^^
1266 start pointing here
1267 */
1268static int
1269handle_keywordonly_args(struct compiling *c, const node *n, int start,
1270 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1271{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001272 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001273 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001274 expr_ty expression, annotation;
1275 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001276 int i = start;
1277 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001278
1279 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001280 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001281 return -1;
1282 }
1283 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 while (i < NCH(n)) {
1285 ch = CHILD(n, i);
1286 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001287 case vfpdef:
1288 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001289 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001290 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001291 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001292 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001293 asdl_seq_SET(kwdefaults, j, expression);
1294 i += 2; /* '=' and test */
1295 }
1296 else { /* setting NULL if no default value exists */
1297 asdl_seq_SET(kwdefaults, j, NULL);
1298 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001299 if (NCH(ch) == 3) {
1300 /* ch is NAME ':' test */
1301 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001302 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001303 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001304 }
1305 else {
1306 annotation = NULL;
1307 }
1308 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001309 argname = NEW_IDENTIFIER(ch);
1310 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001311 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001312 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001313 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001314 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1315 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001316 if (!arg)
1317 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001318 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001319 i += 2; /* the name and the comma */
1320 break;
1321 case DOUBLESTAR:
1322 return i;
1323 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001324 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001325 goto error;
1326 }
1327 }
1328 return i;
1329 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332
Jeremy Hyltona8293132006-02-28 17:58:27 +00001333/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334
1335static arguments_ty
1336ast_for_arguments(struct compiling *c, const node *n)
1337{
Neal Norwitzc1505362006-12-28 06:47:50 +00001338 /* This function handles both typedargslist (function definition)
1339 and varargslist (lambda definition).
1340
1341 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001342 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1343 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1344 | '**' tfpdef [',']]]
1345 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1346 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001347 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001348 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1349 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1350 | '**' vfpdef [',']]]
1351 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1352 | '**' vfpdef [',']
1353 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001354 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001355
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001357 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1358 int nposdefaults = 0, found_default = 0;
1359 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001360 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001361 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 node *ch;
1363
1364 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001366 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001369 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370
Jeremy Hyltone921e022008-07-17 16:37:17 +00001371 /* First count the number of positional args & defaults. The
1372 variable i is the loop index for this for loop and the next.
1373 The next loop picks up where the first leaves off.
1374 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 ch = CHILD(n, i);
1377 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001378 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001379 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001380 if (i < NCH(n) && /* skip argument following star */
1381 (TYPE(CHILD(n, i)) == tfpdef ||
1382 TYPE(CHILD(n, i)) == vfpdef)) {
1383 i++;
1384 }
1385 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001386 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001387 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001388 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 defaults for keyword only args */
1393 for ( ; i < NCH(n); ++i) {
1394 ch = CHILD(n, i);
1395 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001396 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001398 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001399 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001400 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001401 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001402 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001403 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001404 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001406 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001408 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001410 since we set NULL as default for keyword only argument w/o default
1411 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001412 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001413 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001414 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001415 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416
1417 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001418 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001419 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001422 /* tfpdef: NAME [':' test]
1423 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 */
1425 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001426 j = 0; /* index for defaults */
1427 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001429 ch = CHILD(n, i);
1430 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001431 case tfpdef:
1432 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1434 anything other than EQUAL or a comma? */
1435 /* XXX Should NCH(n) check be made a separate check? */
1436 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001437 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1438 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001439 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001440 assert(posdefaults != NULL);
1441 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001443 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001446 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001447 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001448 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001450 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001451 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001452 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001453 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 i += 2; /* the name and the comma */
1455 break;
1456 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001457 if (i+1 >= NCH(n) ||
1458 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001459 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001460 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001461 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001462 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001463 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001464 if (TYPE(ch) == COMMA) {
1465 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001466 i += 2; /* now follows keyword only arguments */
1467 res = handle_keywordonly_args(c, n, i,
1468 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001469 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001470 i = res; /* res has new position to process */
1471 }
1472 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001473 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001474 if (!vararg)
1475 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001476
Guido van Rossum4f72a782006-10-27 23:31:49 +00001477 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001478 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1479 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001480 int res = 0;
1481 res = handle_keywordonly_args(c, n, i,
1482 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001483 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001484 i = res; /* res has new position to process */
1485 }
1486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 break;
1488 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001489 ch = CHILD(n, i+1); /* tfpdef */
1490 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001491 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001492 if (!kwarg)
1493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 i += 3;
1495 break;
1496 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001497 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 "unexpected node in varargslist: %d @ %d",
1499 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001500 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001501 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001503 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504}
1505
1506static expr_ty
1507ast_for_dotted_name(struct compiling *c, const node *n)
1508{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001509 expr_ty e;
1510 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001511 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 int i;
1513
1514 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001515
1516 lineno = LINENO(n);
1517 col_offset = n->n_col_offset;
1518
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 id = NEW_IDENTIFIER(CHILD(n, 0));
1520 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001521 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001522 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525
1526 for (i = 2; i < NCH(n); i+=2) {
1527 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001528 if (!id)
1529 return NULL;
1530 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1531 if (!e)
1532 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 }
1534
1535 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536}
1537
1538static expr_ty
1539ast_for_decorator(struct compiling *c, const node *n)
1540{
1541 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1542 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001543 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001546 REQ(CHILD(n, 0), AT);
1547 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1550 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001551 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001554 d = name_expr;
1555 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 }
1557 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001558 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001559 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001560 if (!d)
1561 return NULL;
1562 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 }
1564 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001565 d = ast_for_call(c, CHILD(n, 3), name_expr);
1566 if (!d)
1567 return NULL;
1568 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 }
1570
1571 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572}
1573
1574static asdl_seq*
1575ast_for_decorators(struct compiling *c, const node *n)
1576{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001577 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001578 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001582 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 if (!decorator_seq)
1584 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001587 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001588 if (!d)
1589 return NULL;
1590 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 }
1592 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593}
1594
1595static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001596ast_for_funcdef_impl(struct compiling *c, const node *n,
1597 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001599 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001600 identifier name;
1601 arguments_ty args;
1602 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001603 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001604 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605
1606 REQ(n, funcdef);
1607
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 name = NEW_IDENTIFIER(CHILD(n, name_i));
1609 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001610 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001611 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001612 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1614 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001615 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001616 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1617 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1618 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001619 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001620 name_i += 2;
1621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 body = ast_for_suite(c, CHILD(n, name_i + 3));
1623 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001624 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625
Yury Selivanov75445082015-05-11 22:57:16 -04001626 if (is_async)
1627 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1628 LINENO(n),
1629 n->n_col_offset, c->c_arena);
1630 else
1631 return FunctionDef(name, args, body, decorator_seq, returns,
1632 LINENO(n),
1633 n->n_col_offset, c->c_arena);
1634}
1635
1636static stmt_ty
1637ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1638{
1639 /* async_funcdef: ASYNC funcdef */
1640 REQ(n, async_funcdef);
1641 REQ(CHILD(n, 0), ASYNC);
1642 REQ(CHILD(n, 1), funcdef);
1643
1644 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1645 1 /* is_async */);
1646}
1647
1648static stmt_ty
1649ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1650{
1651 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1652 return ast_for_funcdef_impl(c, n, decorator_seq,
1653 0 /* is_async */);
1654}
1655
1656
1657static stmt_ty
1658ast_for_async_stmt(struct compiling *c, const node *n)
1659{
1660 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1661 REQ(n, async_stmt);
1662 REQ(CHILD(n, 0), ASYNC);
1663
1664 switch (TYPE(CHILD(n, 1))) {
1665 case funcdef:
1666 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1667 1 /* is_async */);
1668 case with_stmt:
1669 return ast_for_with_stmt(c, CHILD(n, 1),
1670 1 /* is_async */);
1671
1672 case for_stmt:
1673 return ast_for_for_stmt(c, CHILD(n, 1),
1674 1 /* is_async */);
1675
1676 default:
1677 PyErr_Format(PyExc_SystemError,
1678 "invalid async stament: %s",
1679 STR(CHILD(n, 1)));
1680 return NULL;
1681 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682}
1683
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001684static stmt_ty
1685ast_for_decorated(struct compiling *c, const node *n)
1686{
Yury Selivanov75445082015-05-11 22:57:16 -04001687 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001688 stmt_ty thing = NULL;
1689 asdl_seq *decorator_seq = NULL;
1690
1691 REQ(n, decorated);
1692
1693 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1694 if (!decorator_seq)
1695 return NULL;
1696
1697 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001698 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001700
1701 if (TYPE(CHILD(n, 1)) == funcdef) {
1702 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1703 } else if (TYPE(CHILD(n, 1)) == classdef) {
1704 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001705 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1706 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001707 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001708 /* we count the decorators in when talking about the class' or
1709 * function's line number */
1710 if (thing) {
1711 thing->lineno = LINENO(n);
1712 thing->col_offset = n->n_col_offset;
1713 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001714 return thing;
1715}
1716
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717static expr_ty
1718ast_for_lambdef(struct compiling *c, const node *n)
1719{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001720 /* lambdef: 'lambda' [varargslist] ':' test
1721 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 arguments_ty args;
1723 expr_ty expression;
1724
1725 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001726 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727 if (!args)
1728 return NULL;
1729 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001730 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 }
1733 else {
1734 args = ast_for_arguments(c, CHILD(n, 1));
1735 if (!args)
1736 return NULL;
1737 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001738 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 }
1741
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001742 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743}
1744
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001745static expr_ty
1746ast_for_ifexpr(struct compiling *c, const node *n)
1747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001749 expr_ty expression, body, orelse;
1750
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001751 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001752 body = ast_for_expr(c, CHILD(n, 0));
1753 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001754 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001755 expression = ast_for_expr(c, CHILD(n, 2));
1756 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001757 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001758 orelse = ast_for_expr(c, CHILD(n, 4));
1759 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001760 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001761 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1762 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001763}
1764
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001766 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767
Nick Coghlan650f0d02007-04-15 12:05:43 +00001768 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769*/
1770
1771static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001772count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001774 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001775 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776
Guido van Rossumd8faa362007-04-27 19:54:29 +00001777 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001778 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001779 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001780 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001781 if (TYPE(CHILD(n, 0)) == ASYNC) {
1782 is_async = 1;
1783 }
1784 if (NCH(n) == (5 + is_async)) {
1785 n = CHILD(n, 4 + is_async);
1786 }
1787 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001788 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001789 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001791 REQ(n, comp_iter);
1792 n = CHILD(n, 0);
1793 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001794 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001795 else if (TYPE(n) == comp_if) {
1796 if (NCH(n) == 3) {
1797 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001798 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001799 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001800 else
1801 return n_fors;
1802 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001803
Guido van Rossumd8faa362007-04-27 19:54:29 +00001804 /* Should never be reached */
1805 PyErr_SetString(PyExc_SystemError,
1806 "logic error in count_comp_fors");
1807 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808}
1809
Nick Coghlan650f0d02007-04-15 12:05:43 +00001810/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811
Nick Coghlan650f0d02007-04-15 12:05:43 +00001812 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813*/
1814
1815static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001816count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001818 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819
Guido van Rossumd8faa362007-04-27 19:54:29 +00001820 while (1) {
1821 REQ(n, comp_iter);
1822 if (TYPE(CHILD(n, 0)) == comp_for)
1823 return n_ifs;
1824 n = CHILD(n, 0);
1825 REQ(n, comp_if);
1826 n_ifs++;
1827 if (NCH(n) == 2)
1828 return n_ifs;
1829 n = CHILD(n, 2);
1830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831}
1832
Guido van Rossum992d4a32007-07-11 13:09:30 +00001833static asdl_seq *
1834ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001837 asdl_seq *comps;
1838
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001839 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 if (n_fors == -1)
1841 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001842
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001843 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001844 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001848 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001850 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001851 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001852 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853
Guido van Rossum992d4a32007-07-11 13:09:30 +00001854 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001856 if (TYPE(CHILD(n, 0)) == ASYNC) {
1857 is_async = 1;
1858 }
1859
1860 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001861 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001862 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001864 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001865 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001867
Thomas Wouters89f507f2006-12-13 04:49:30 +00001868 /* Check the # of children rather than the length of t, since
1869 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001870 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001871 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001872 comp = comprehension(first, expression, NULL,
1873 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001875 comp = comprehension(Tuple(t, Store, first->lineno,
1876 first->col_offset, c->c_arena),
1877 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001878 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001880
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001881 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 int j, n_ifs;
1883 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001885 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001886 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001887 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001889
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001890 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001893
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001895 REQ(n, comp_iter);
1896 n = CHILD(n, 0);
1897 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898
Guido van Rossum992d4a32007-07-11 13:09:30 +00001899 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001900 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001901 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001902 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001903 if (NCH(n) == 3)
1904 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001906 /* on exit, must guarantee that n is a comp_for */
1907 if (TYPE(n) == comp_iter)
1908 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001909 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001911 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001913 return comps;
1914}
1915
1916static expr_ty
1917ast_for_itercomp(struct compiling *c, const node *n, int type)
1918{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001919 /* testlist_comp: (test|star_expr)
1920 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001921 expr_ty elt;
1922 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001923 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924
Guido van Rossum992d4a32007-07-11 13:09:30 +00001925 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001927 ch = CHILD(n, 0);
1928 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001929 if (!elt)
1930 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001931 if (elt->kind == Starred_kind) {
1932 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1933 return NULL;
1934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935
Guido van Rossum992d4a32007-07-11 13:09:30 +00001936 comps = ast_for_comprehension(c, CHILD(n, 1));
1937 if (!comps)
1938 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001939
1940 if (type == COMP_GENEXP)
1941 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1942 else if (type == COMP_LISTCOMP)
1943 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1944 else if (type == COMP_SETCOMP)
1945 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1946 else
1947 /* Should never happen */
1948 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949}
1950
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001951/* Fills in the key, value pair corresponding to the dict element. In case
1952 * of an unpacking, key is NULL. *i is advanced by the number of ast
1953 * elements. Iff successful, nonzero is returned.
1954 */
1955static int
1956ast_for_dictelement(struct compiling *c, const node *n, int *i,
1957 expr_ty *key, expr_ty *value)
1958{
1959 expr_ty expression;
1960 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1961 assert(NCH(n) - *i >= 2);
1962
1963 expression = ast_for_expr(c, CHILD(n, *i + 1));
1964 if (!expression)
1965 return 0;
1966 *key = NULL;
1967 *value = expression;
1968
1969 *i += 2;
1970 }
1971 else {
1972 assert(NCH(n) - *i >= 3);
1973
1974 expression = ast_for_expr(c, CHILD(n, *i));
1975 if (!expression)
1976 return 0;
1977 *key = expression;
1978
1979 REQ(CHILD(n, *i + 1), COLON);
1980
1981 expression = ast_for_expr(c, CHILD(n, *i + 2));
1982 if (!expression)
1983 return 0;
1984 *value = expression;
1985
1986 *i += 3;
1987 }
1988 return 1;
1989}
1990
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001992ast_for_dictcomp(struct compiling *c, const node *n)
1993{
1994 expr_ty key, value;
1995 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001996 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001998 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001999 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002000 assert(key);
2001 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002003 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002004 if (!comps)
2005 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006
Guido van Rossum992d4a32007-07-11 13:09:30 +00002007 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2008}
2009
2010static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002011ast_for_dictdisplay(struct compiling *c, const node *n)
2012{
2013 int i;
2014 int j;
2015 int size;
2016 asdl_seq *keys, *values;
2017
2018 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2019 keys = _Py_asdl_seq_new(size, c->c_arena);
2020 if (!keys)
2021 return NULL;
2022
2023 values = _Py_asdl_seq_new(size, c->c_arena);
2024 if (!values)
2025 return NULL;
2026
2027 j = 0;
2028 for (i = 0; i < NCH(n); i++) {
2029 expr_ty key, value;
2030
2031 if (!ast_for_dictelement(c, n, &i, &key, &value))
2032 return NULL;
2033 asdl_seq_SET(keys, j, key);
2034 asdl_seq_SET(values, j, value);
2035
2036 j++;
2037 }
2038 keys->size = j;
2039 values->size = j;
2040 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2041}
2042
2043static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002044ast_for_genexp(struct compiling *c, const node *n)
2045{
2046 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002047 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002048}
2049
2050static expr_ty
2051ast_for_listcomp(struct compiling *c, const node *n)
2052{
2053 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002054 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002055}
2056
2057static expr_ty
2058ast_for_setcomp(struct compiling *c, const node *n)
2059{
2060 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002061 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002062}
2063
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002064static expr_ty
2065ast_for_setdisplay(struct compiling *c, const node *n)
2066{
2067 int i;
2068 int size;
2069 asdl_seq *elts;
2070
2071 assert(TYPE(n) == (dictorsetmaker));
2072 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2073 elts = _Py_asdl_seq_new(size, c->c_arena);
2074 if (!elts)
2075 return NULL;
2076 for (i = 0; i < NCH(n); i += 2) {
2077 expr_ty expression;
2078 expression = ast_for_expr(c, CHILD(n, i));
2079 if (!expression)
2080 return NULL;
2081 asdl_seq_SET(elts, i / 2, expression);
2082 }
2083 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2084}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002085
2086static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087ast_for_atom(struct compiling *c, const node *n)
2088{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002089 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2090 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002091 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 */
2093 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002096 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002097 PyObject *name;
2098 const char *s = STR(ch);
2099 size_t len = strlen(s);
2100 if (len >= 4 && len <= 5) {
2101 if (!strcmp(s, "None"))
2102 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2103 if (!strcmp(s, "True"))
2104 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2105 if (!strcmp(s, "False"))
2106 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2107 }
2108 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002109 if (!name)
2110 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002111 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002112 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2113 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002115 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002116 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002117 const char *errtype = NULL;
2118 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2119 errtype = "unicode error";
2120 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2121 errtype = "value error";
2122 if (errtype) {
2123 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002124 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002125 PyObject *type, *value, *tback, *errstr;
2126 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002127 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002128 if (errstr)
2129 s = PyUnicode_AsUTF8(errstr);
2130 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002131 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002132 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002133 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002134 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002135 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002136 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002137 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002138 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002139 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002140 Py_XDECREF(tback);
2141 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002142 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002143 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002144 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 }
2146 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002147 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002148 if (!pynum)
2149 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002150
Victor Stinner43d81952013-07-17 00:57:58 +02002151 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2152 Py_DECREF(pynum);
2153 return NULL;
2154 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002155 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 }
Georg Brandldde00282007-03-18 19:01:53 +00002157 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002158 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002160 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161
Thomas Wouters89f507f2006-12-13 04:49:30 +00002162 if (TYPE(ch) == RPAR)
2163 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164
Thomas Wouters89f507f2006-12-13 04:49:30 +00002165 if (TYPE(ch) == yield_expr)
2166 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002169 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002170 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002171
Nick Coghlan650f0d02007-04-15 12:05:43 +00002172 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002174 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175
Thomas Wouters89f507f2006-12-13 04:49:30 +00002176 if (TYPE(ch) == RSQB)
2177 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178
Nick Coghlan650f0d02007-04-15 12:05:43 +00002179 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002180 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2181 asdl_seq *elts = seq_for_testlist(c, ch);
2182 if (!elts)
2183 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002184
Thomas Wouters89f507f2006-12-13 04:49:30 +00002185 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2186 }
2187 else
2188 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002190 /* dictorsetmaker: ( ((test ':' test | '**' test)
2191 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2192 * ((test | '*' test)
2193 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002194 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002195 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002196 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002197 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002198 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002199 }
2200 else {
2201 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2202 if (NCH(ch) == 1 ||
2203 (NCH(ch) > 1 &&
2204 TYPE(CHILD(ch, 1)) == COMMA)) {
2205 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002206 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002207 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002208 else if (NCH(ch) > 1 &&
2209 TYPE(CHILD(ch, 1)) == comp_for) {
2210 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002211 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002212 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002213 else if (NCH(ch) > 3 - is_dict &&
2214 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2215 /* It's a dictionary comprehension. */
2216 if (is_dict) {
2217 ast_error(c, n, "dict unpacking cannot be used in "
2218 "dict comprehension");
2219 return NULL;
2220 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002221 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002222 }
2223 else {
2224 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002225 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002226 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002227 if (res) {
2228 res->lineno = LINENO(n);
2229 res->col_offset = n->n_col_offset;
2230 }
2231 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002235 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2236 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 }
2238}
2239
2240static slice_ty
2241ast_for_slice(struct compiling *c, const node *n)
2242{
2243 node *ch;
2244 expr_ty lower = NULL, upper = NULL, step = NULL;
2245
2246 REQ(n, subscript);
2247
2248 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002249 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 sliceop: ':' [test]
2251 */
2252 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 if (NCH(n) == 1 && TYPE(ch) == test) {
2254 /* 'step' variable hold no significance in terms of being used over
2255 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 if (!step)
2258 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259
Thomas Wouters89f507f2006-12-13 04:49:30 +00002260 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 }
2262
2263 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002264 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 if (!lower)
2266 return NULL;
2267 }
2268
2269 /* If there's an upper bound it's in the second or third position. */
2270 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002271 if (NCH(n) > 1) {
2272 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273
Thomas Wouters89f507f2006-12-13 04:49:30 +00002274 if (TYPE(n2) == test) {
2275 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 if (!upper)
2277 return NULL;
2278 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002279 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002281 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282
Thomas Wouters89f507f2006-12-13 04:49:30 +00002283 if (TYPE(n2) == test) {
2284 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 if (!upper)
2286 return NULL;
2287 }
2288 }
2289
2290 ch = CHILD(n, NCH(n) - 1);
2291 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002292 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002293 ch = CHILD(ch, 1);
2294 if (TYPE(ch) == test) {
2295 step = ast_for_expr(c, ch);
2296 if (!step)
2297 return NULL;
2298 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 }
2300 }
2301
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002302 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303}
2304
2305static expr_ty
2306ast_for_binop(struct compiling *c, const node *n)
2307{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002308 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002310 BinOp(BinOp(A, op, B), op, C).
2311 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312
Guido van Rossumd8faa362007-04-27 19:54:29 +00002313 int i, nops;
2314 expr_ty expr1, expr2, result;
2315 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316
Guido van Rossumd8faa362007-04-27 19:54:29 +00002317 expr1 = ast_for_expr(c, CHILD(n, 0));
2318 if (!expr1)
2319 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320
Guido van Rossumd8faa362007-04-27 19:54:29 +00002321 expr2 = ast_for_expr(c, CHILD(n, 2));
2322 if (!expr2)
2323 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324
Guido van Rossumd8faa362007-04-27 19:54:29 +00002325 newoperator = get_operator(CHILD(n, 1));
2326 if (!newoperator)
2327 return NULL;
2328
2329 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2330 c->c_arena);
2331 if (!result)
2332 return NULL;
2333
2334 nops = (NCH(n) - 1) / 2;
2335 for (i = 1; i < nops; i++) {
2336 expr_ty tmp_result, tmp;
2337 const node* next_oper = CHILD(n, i * 2 + 1);
2338
2339 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002340 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 return NULL;
2342
Guido van Rossumd8faa362007-04-27 19:54:29 +00002343 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2344 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 return NULL;
2346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002348 LINENO(next_oper), next_oper->n_col_offset,
2349 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002351 return NULL;
2352 result = tmp_result;
2353 }
2354 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355}
2356
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002357static expr_ty
2358ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002361 subscriptlist: subscript (',' subscript)* [',']
2362 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2363 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364 REQ(n, trailer);
2365 if (TYPE(CHILD(n, 0)) == LPAR) {
2366 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002367 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002368 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002369 else
2370 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002371 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002372 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002373 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2374 if (!attr_id)
2375 return NULL;
2376 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002377 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002378 }
2379 else {
2380 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002381 REQ(CHILD(n, 2), RSQB);
2382 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002383 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002384 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2385 if (!slc)
2386 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002387 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2388 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002389 }
2390 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002392 by treating the sequence as a tuple literal if there are
2393 no slice features.
2394 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002395 int j;
2396 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002397 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002398 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002399 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002400 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002401 if (!slices)
2402 return NULL;
2403 for (j = 0; j < NCH(n); j += 2) {
2404 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002405 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002406 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002407 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002408 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002409 asdl_seq_SET(slices, j / 2, slc);
2410 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002411 if (!simple) {
2412 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002413 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002414 }
2415 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002416 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002417 if (!elts)
2418 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002419 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2420 slc = (slice_ty)asdl_seq_GET(slices, j);
2421 assert(slc->kind == Index_kind && slc->v.Index.value);
2422 asdl_seq_SET(elts, j, slc->v.Index.value);
2423 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002424 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002425 if (!e)
2426 return NULL;
2427 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002428 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002429 }
2430 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002431}
2432
2433static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002434ast_for_factor(struct compiling *c, const node *n)
2435{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002436 expr_ty expression;
2437
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002438 expression = ast_for_expr(c, CHILD(n, 1));
2439 if (!expression)
2440 return NULL;
2441
2442 switch (TYPE(CHILD(n, 0))) {
2443 case PLUS:
2444 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2445 c->c_arena);
2446 case MINUS:
2447 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2448 c->c_arena);
2449 case TILDE:
2450 return UnaryOp(Invert, expression, LINENO(n),
2451 n->n_col_offset, c->c_arena);
2452 }
2453 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2454 TYPE(CHILD(n, 0)));
2455 return NULL;
2456}
2457
2458static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002459ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002460{
Yury Selivanov75445082015-05-11 22:57:16 -04002461 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002462 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002463
2464 REQ(n, atom_expr);
2465 nch = NCH(n);
2466
2467 if (TYPE(CHILD(n, 0)) == AWAIT) {
2468 start = 1;
2469 assert(nch > 1);
2470 }
2471
2472 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002473 if (!e)
2474 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002475 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002476 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002477 if (start && nch == 2) {
2478 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2479 }
2480
2481 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002482 node *ch = CHILD(n, i);
2483 if (TYPE(ch) != trailer)
2484 break;
2485 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002486 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002487 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002488 tmp->lineno = e->lineno;
2489 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002490 e = tmp;
2491 }
Yury Selivanov75445082015-05-11 22:57:16 -04002492
2493 if (start) {
2494 /* there was an AWAIT */
2495 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2496 }
2497 else {
2498 return e;
2499 }
2500}
2501
2502static expr_ty
2503ast_for_power(struct compiling *c, const node *n)
2504{
2505 /* power: atom trailer* ('**' factor)*
2506 */
2507 expr_ty e;
2508 REQ(n, power);
2509 e = ast_for_atom_expr(c, CHILD(n, 0));
2510 if (!e)
2511 return NULL;
2512 if (NCH(n) == 1)
2513 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002514 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2515 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002516 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002517 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002518 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002519 }
2520 return e;
2521}
2522
Guido van Rossum0368b722007-05-11 16:50:42 +00002523static expr_ty
2524ast_for_starred(struct compiling *c, const node *n)
2525{
2526 expr_ty tmp;
2527 REQ(n, star_expr);
2528
2529 tmp = ast_for_expr(c, CHILD(n, 1));
2530 if (!tmp)
2531 return NULL;
2532
2533 /* The Load context is changed later. */
2534 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2535}
2536
2537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538/* Do not name a variable 'expr'! Will cause a compile error.
2539*/
2540
2541static expr_ty
2542ast_for_expr(struct compiling *c, const node *n)
2543{
2544 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002545 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002546 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 and_test: not_test ('and' not_test)*
2549 not_test: 'not' not_test | comparison
2550 comparison: expr (comp_op expr)*
2551 expr: xor_expr ('|' xor_expr)*
2552 xor_expr: and_expr ('^' and_expr)*
2553 and_expr: shift_expr ('&' shift_expr)*
2554 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2555 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002556 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002558 power: atom_expr ['**' factor]
2559 atom_expr: [AWAIT] atom trailer*
2560 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 */
2562
2563 asdl_seq *seq;
2564 int i;
2565
2566 loop:
2567 switch (TYPE(n)) {
2568 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002569 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002570 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002571 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002573 else if (NCH(n) > 1)
2574 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002575 /* Fallthrough */
2576 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 case and_test:
2578 if (NCH(n) == 1) {
2579 n = CHILD(n, 0);
2580 goto loop;
2581 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002582 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 if (!seq)
2584 return NULL;
2585 for (i = 0; i < NCH(n); i += 2) {
2586 expr_ty e = ast_for_expr(c, CHILD(n, i));
2587 if (!e)
2588 return NULL;
2589 asdl_seq_SET(seq, i / 2, e);
2590 }
2591 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002592 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2593 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002594 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002595 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 case not_test:
2597 if (NCH(n) == 1) {
2598 n = CHILD(n, 0);
2599 goto loop;
2600 }
2601 else {
2602 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2603 if (!expression)
2604 return NULL;
2605
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002606 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2607 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 }
2609 case comparison:
2610 if (NCH(n) == 1) {
2611 n = CHILD(n, 0);
2612 goto loop;
2613 }
2614 else {
2615 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002616 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002617 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002618 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 if (!ops)
2620 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002621 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 return NULL;
2624 }
2625 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002626 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002628 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002629 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002631 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632
2633 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002634 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002636 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002638 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 asdl_seq_SET(cmps, i / 2, expression);
2640 }
2641 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002642 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002644 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002646 return Compare(expression, ops, cmps, LINENO(n),
2647 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 }
2649 break;
2650
Guido van Rossum0368b722007-05-11 16:50:42 +00002651 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 /* The next five cases all handle BinOps. The main body of code
2654 is the same in each case, but the switch turned inside out to
2655 reuse the code for each type of operator.
2656 */
2657 case expr:
2658 case xor_expr:
2659 case and_expr:
2660 case shift_expr:
2661 case arith_expr:
2662 case term:
2663 if (NCH(n) == 1) {
2664 n = CHILD(n, 0);
2665 goto loop;
2666 }
2667 return ast_for_binop(c, n);
2668 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002669 node *an = NULL;
2670 node *en = NULL;
2671 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002672 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002673 if (NCH(n) > 1)
2674 an = CHILD(n, 1); /* yield_arg */
2675 if (an) {
2676 en = CHILD(an, NCH(an) - 1);
2677 if (NCH(an) == 2) {
2678 is_from = 1;
2679 exp = ast_for_expr(c, en);
2680 }
2681 else
2682 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002683 if (!exp)
2684 return NULL;
2685 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002686 if (is_from)
2687 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2688 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002689 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002690 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 if (NCH(n) == 1) {
2692 n = CHILD(n, 0);
2693 goto loop;
2694 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002695 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002696 case power:
2697 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002699 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 return NULL;
2701 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002702 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 return NULL;
2704}
2705
2706static expr_ty
2707ast_for_call(struct compiling *c, const node *n, expr_ty func)
2708{
2709 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002710 arglist: argument (',' argument)* [',']
2711 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712 */
2713
2714 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002715 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002716 asdl_seq *args;
2717 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718
2719 REQ(n, arglist);
2720
2721 nargs = 0;
2722 nkeywords = 0;
2723 ngens = 0;
2724 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002725 node *ch = CHILD(n, i);
2726 if (TYPE(ch) == argument) {
2727 if (NCH(ch) == 1)
2728 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002729 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002730 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731 else if (TYPE(CHILD(ch, 0)) == STAR)
2732 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002734 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002735 nkeywords++;
2736 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 }
2738 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002739 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002740 "if not sole argument");
2741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 }
2743
2744 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002745 ast_error(c, n, "more than 255 arguments");
2746 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 }
2748
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002749 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002751 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002752 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002754 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002755
2756 nargs = 0; /* positional arguments + iterable argument unpackings */
2757 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2758 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002760 node *ch = CHILD(n, i);
2761 if (TYPE(ch) == argument) {
2762 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002764 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002765 /* a positional argument */
2766 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002767 if (ndoublestars) {
2768 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002769 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002771 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002772 else {
2773 ast_error(c, chch,
2774 "positional argument follows "
2775 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002776 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002777 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002778 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002779 e = ast_for_expr(c, chch);
2780 if (!e)
2781 return NULL;
2782 asdl_seq_SET(args, nargs++, e);
2783 }
2784 else if (TYPE(chch) == STAR) {
2785 /* an iterable argument unpacking */
2786 expr_ty starred;
2787 if (ndoublestars) {
2788 ast_error(c, chch,
2789 "iterable argument unpacking follows "
2790 "keyword argument unpacking");
2791 return NULL;
2792 }
2793 e = ast_for_expr(c, CHILD(ch, 1));
2794 if (!e)
2795 return NULL;
2796 starred = Starred(e, Load, LINENO(chch),
2797 chch->n_col_offset,
2798 c->c_arena);
2799 if (!starred)
2800 return NULL;
2801 asdl_seq_SET(args, nargs++, starred);
2802
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002803 }
2804 else if (TYPE(chch) == DOUBLESTAR) {
2805 /* a keyword argument unpacking */
2806 keyword_ty kw;
2807 i++;
2808 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002810 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002811 kw = keyword(NULL, e, c->c_arena);
2812 asdl_seq_SET(keywords, nkeywords++, kw);
2813 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002815 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002816 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002819 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002820 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002822 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002823 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002824 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002825 identifier key, tmp;
2826 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002828 /* chch is test, but must be an identifier? */
2829 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002831 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 /* f(lambda x: x[0] = 3) ends up getting parsed with
2833 * LHS test = lambda x: x[0], and RHS test = 3.
2834 * SF bug 132313 points out that complaining about a keyword
2835 * then is very confusing.
2836 */
2837 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002838 ast_error(c, chch,
2839 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002840 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002841 }
2842 else if (e->kind != Name_kind) {
2843 ast_error(c, chch,
2844 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002845 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002846 }
2847 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002848 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002850 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002851 for (k = 0; k < nkeywords; k++) {
2852 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002853 if (tmp && !PyUnicode_Compare(tmp, key)) {
2854 ast_error(c, chch,
2855 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002856 return NULL;
2857 }
2858 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002861 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002862 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002864 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002865 asdl_seq_SET(keywords, nkeywords++, kw);
2866 }
2867 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 }
2869
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002870 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871}
2872
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002874ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002876 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002877 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002879 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002880 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002881 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002882 }
2883 else {
2884 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002885 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002886 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002888 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 else {
2890 asdl_seq *tmp = seq_for_testlist(c, n);
2891 if (!tmp)
2892 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002893 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002895}
2896
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897static stmt_ty
2898ast_for_expr_stmt(struct compiling *c, const node *n)
2899{
2900 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002901 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2902 ('=' (yield_expr|testlist_star_expr))*)
2903 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002904 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002905 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002906 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002907 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 */
2909
2910 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002911 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 if (!e)
2913 return NULL;
2914
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 }
2917 else if (TYPE(CHILD(n, 1)) == augassign) {
2918 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002919 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002920 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921
Thomas Wouters89f507f2006-12-13 04:49:30 +00002922 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923 if (!expr1)
2924 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002925 if(!set_context(c, expr1, Store, ch))
2926 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002927 /* set_context checks that most expressions are not the left side.
2928 Augmented assignments can only have a name, a subscript, or an
2929 attribute on the left, though, so we have to explicitly check for
2930 those. */
2931 switch (expr1->kind) {
2932 case Name_kind:
2933 case Attribute_kind:
2934 case Subscript_kind:
2935 break;
2936 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002937 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002938 return NULL;
2939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940
Thomas Wouters89f507f2006-12-13 04:49:30 +00002941 ch = CHILD(n, 2);
2942 if (TYPE(ch) == testlist)
2943 expr2 = ast_for_testlist(c, ch);
2944 else
2945 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002946 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 return NULL;
2948
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002949 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002950 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 return NULL;
2952
Thomas Wouters89f507f2006-12-13 04:49:30 +00002953 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002955 else if (TYPE(CHILD(n, 1)) == annassign) {
2956 expr_ty expr1, expr2, expr3;
2957 node *ch = CHILD(n, 0);
2958 node *deep, *ann = CHILD(n, 1);
2959 int simple = 1;
2960
2961 /* we keep track of parens to qualify (x) as expression not name */
2962 deep = ch;
2963 while (NCH(deep) == 1) {
2964 deep = CHILD(deep, 0);
2965 }
2966 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2967 simple = 0;
2968 }
2969 expr1 = ast_for_testlist(c, ch);
2970 if (!expr1) {
2971 return NULL;
2972 }
2973 switch (expr1->kind) {
2974 case Name_kind:
2975 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2976 return NULL;
2977 }
2978 expr1->v.Name.ctx = Store;
2979 break;
2980 case Attribute_kind:
2981 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2982 return NULL;
2983 }
2984 expr1->v.Attribute.ctx = Store;
2985 break;
2986 case Subscript_kind:
2987 expr1->v.Subscript.ctx = Store;
2988 break;
2989 case List_kind:
2990 ast_error(c, ch,
2991 "only single target (not list) can be annotated");
2992 return NULL;
2993 case Tuple_kind:
2994 ast_error(c, ch,
2995 "only single target (not tuple) can be annotated");
2996 return NULL;
2997 default:
2998 ast_error(c, ch,
2999 "illegal target for annotation");
3000 return NULL;
3001 }
3002
3003 if (expr1->kind != Name_kind) {
3004 simple = 0;
3005 }
3006 ch = CHILD(ann, 1);
3007 expr2 = ast_for_expr(c, ch);
3008 if (!expr2) {
3009 return NULL;
3010 }
3011 if (NCH(ann) == 2) {
3012 return AnnAssign(expr1, expr2, NULL, simple,
3013 LINENO(n), n->n_col_offset, c->c_arena);
3014 }
3015 else {
3016 ch = CHILD(ann, 3);
3017 expr3 = ast_for_expr(c, ch);
3018 if (!expr3) {
3019 return NULL;
3020 }
3021 return AnnAssign(expr1, expr2, expr3, simple,
3022 LINENO(n), n->n_col_offset, c->c_arena);
3023 }
3024 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003026 int i;
3027 asdl_seq *targets;
3028 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 expr_ty expression;
3030
Thomas Wouters89f507f2006-12-13 04:49:30 +00003031 /* a normal assignment */
3032 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003033 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003034 if (!targets)
3035 return NULL;
3036 for (i = 0; i < NCH(n) - 2; i += 2) {
3037 expr_ty e;
3038 node *ch = CHILD(n, i);
3039 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003040 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003041 return NULL;
3042 }
3043 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003047 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003048 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050
Thomas Wouters89f507f2006-12-13 04:49:30 +00003051 asdl_seq_SET(targets, i / 2, e);
3052 }
3053 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003054 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003055 expression = ast_for_testlist(c, value);
3056 else
3057 expression = ast_for_expr(c, value);
3058 if (!expression)
3059 return NULL;
3060 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062}
3063
Benjamin Peterson78565b22009-06-28 19:19:51 +00003064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003066ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003067{
3068 asdl_seq *seq;
3069 int i;
3070 expr_ty e;
3071
3072 REQ(n, exprlist);
3073
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003074 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003076 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003078 e = ast_for_expr(c, CHILD(n, i));
3079 if (!e)
3080 return NULL;
3081 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003082 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003083 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 }
3085 return seq;
3086}
3087
3088static stmt_ty
3089ast_for_del_stmt(struct compiling *c, const node *n)
3090{
3091 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093 /* del_stmt: 'del' exprlist */
3094 REQ(n, del_stmt);
3095
3096 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3097 if (!expr_list)
3098 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003099 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100}
3101
3102static stmt_ty
3103ast_for_flow_stmt(struct compiling *c, const node *n)
3104{
3105 /*
3106 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3107 | yield_stmt
3108 break_stmt: 'break'
3109 continue_stmt: 'continue'
3110 return_stmt: 'return' [testlist]
3111 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003112 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 raise_stmt: 'raise' [test [',' test [',' test]]]
3114 */
3115 node *ch;
3116
3117 REQ(n, flow_stmt);
3118 ch = CHILD(n, 0);
3119 switch (TYPE(ch)) {
3120 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003121 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003123 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003125 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3126 if (!exp)
3127 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003128 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 }
3130 case return_stmt:
3131 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003132 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003134 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 if (!expression)
3136 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003137 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 }
3139 case raise_stmt:
3140 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003141 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3142 else if (NCH(ch) >= 2) {
3143 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3145 if (!expression)
3146 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003147 if (NCH(ch) == 4) {
3148 cause = ast_for_expr(c, CHILD(ch, 3));
3149 if (!cause)
3150 return NULL;
3151 }
3152 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 }
Victor Stinnerc0e77362017-09-12 16:09:44 -07003154 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003156 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 "unexpected flow_stmt: %d", TYPE(ch));
3158 return NULL;
3159 }
3160}
3161
3162static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003163alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164{
3165 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003166 import_as_name: NAME ['as' NAME]
3167 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 dotted_name: NAME ('.' NAME)*
3169 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003170 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003171
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 loop:
3173 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003174 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003175 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003176 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003177 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003178 if (!name)
3179 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003180 if (NCH(n) == 3) {
3181 node *str_node = CHILD(n, 2);
3182 str = NEW_IDENTIFIER(str_node);
3183 if (!str)
3184 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003185 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003186 return NULL;
3187 }
3188 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003189 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003190 return NULL;
3191 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003192 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194 case dotted_as_name:
3195 if (NCH(n) == 1) {
3196 n = CHILD(n, 0);
3197 goto loop;
3198 }
3199 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003200 node *asname_node = CHILD(n, 2);
3201 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003202 if (!a)
3203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003205 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003206 if (!a->asname)
3207 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003208 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003209 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210 return a;
3211 }
3212 break;
3213 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003214 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003215 node *name_node = CHILD(n, 0);
3216 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003217 if (!name)
3218 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003219 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003220 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003221 return alias(name, NULL, c->c_arena);
3222 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223 else {
3224 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003225 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003226 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229
3230 len = 0;
3231 for (i = 0; i < NCH(n); i += 2)
3232 /* length of string plus one for the dot */
3233 len += strlen(STR(CHILD(n, i))) + 1;
3234 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003235 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 if (!str)
3237 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003238 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239 if (!s)
3240 return NULL;
3241 for (i = 0; i < NCH(n); i += 2) {
3242 char *sch = STR(CHILD(n, i));
3243 strcpy(s, STR(CHILD(n, i)));
3244 s += strlen(sch);
3245 *s++ = '.';
3246 }
3247 --s;
3248 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3250 PyBytes_GET_SIZE(str),
3251 NULL);
3252 Py_DECREF(str);
3253 if (!uni)
3254 return NULL;
3255 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003256 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003257 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3258 Py_DECREF(str);
3259 return NULL;
3260 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003261 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 }
3263 break;
3264 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003265 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003266 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3267 Py_DECREF(str);
3268 return NULL;
3269 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003270 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003272 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 "unexpected import name: %d", TYPE(n));
3274 return NULL;
3275 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003276
3277 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278 return NULL;
3279}
3280
3281static stmt_ty
3282ast_for_import_stmt(struct compiling *c, const node *n)
3283{
3284 /*
3285 import_stmt: import_name | import_from
3286 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003287 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3288 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003290 int lineno;
3291 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 int i;
3293 asdl_seq *aliases;
3294
3295 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003296 lineno = LINENO(n);
3297 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003299 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003301 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003302 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 if (!aliases)
3304 return NULL;
3305 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003306 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003307 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003309 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003311 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003313 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003315 int idx, ndots = 0;
3316 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003317 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003319 /* Count the number of dots (for relative imports) and check for the
3320 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 for (idx = 1; idx < NCH(n); idx++) {
3322 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003323 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3324 if (!mod)
3325 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003326 idx++;
3327 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003328 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003330 ndots += 3;
3331 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003332 } else if (TYPE(CHILD(n, idx)) != DOT) {
3333 break;
3334 }
3335 ndots++;
3336 }
3337 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003338 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003339 case STAR:
3340 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003341 n = CHILD(n, idx);
3342 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003343 break;
3344 case LPAR:
3345 /* from ... import (x, y, z) */
3346 n = CHILD(n, idx + 1);
3347 n_children = NCH(n);
3348 break;
3349 case import_as_names:
3350 /* from ... import x, y, z */
3351 n = CHILD(n, idx);
3352 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003353 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003354 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355 " surrounding parentheses");
3356 return NULL;
3357 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003358 break;
3359 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003360 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003361 return NULL;
3362 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003364 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003365 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367
3368 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003369 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003370 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003371 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003373 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003375 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003376 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003377 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003378 if (!import_alias)
3379 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003380 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003381 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003383 if (mod != NULL)
3384 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003385 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003386 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387 }
Neal Norwitz79792652005-11-14 04:25:03 +00003388 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389 "unknown import statement: starts with command '%s'",
3390 STR(CHILD(n, 0)));
3391 return NULL;
3392}
3393
3394static stmt_ty
3395ast_for_global_stmt(struct compiling *c, const node *n)
3396{
3397 /* global_stmt: 'global' NAME (',' NAME)* */
3398 identifier name;
3399 asdl_seq *s;
3400 int i;
3401
3402 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003403 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003405 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003407 name = NEW_IDENTIFIER(CHILD(n, i));
3408 if (!name)
3409 return NULL;
3410 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003412 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413}
3414
3415static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003416ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3417{
3418 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3419 identifier name;
3420 asdl_seq *s;
3421 int i;
3422
3423 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003424 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003425 if (!s)
3426 return NULL;
3427 for (i = 1; i < NCH(n); i += 2) {
3428 name = NEW_IDENTIFIER(CHILD(n, i));
3429 if (!name)
3430 return NULL;
3431 asdl_seq_SET(s, i / 2, name);
3432 }
3433 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3434}
3435
3436static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437ast_for_assert_stmt(struct compiling *c, const node *n)
3438{
3439 /* assert_stmt: 'assert' test [',' test] */
3440 REQ(n, assert_stmt);
3441 if (NCH(n) == 2) {
3442 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3443 if (!expression)
3444 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003445 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 }
3447 else if (NCH(n) == 4) {
3448 expr_ty expr1, expr2;
3449
3450 expr1 = ast_for_expr(c, CHILD(n, 1));
3451 if (!expr1)
3452 return NULL;
3453 expr2 = ast_for_expr(c, CHILD(n, 3));
3454 if (!expr2)
3455 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456
Thomas Wouters89f507f2006-12-13 04:49:30 +00003457 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 }
Neal Norwitz79792652005-11-14 04:25:03 +00003459 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 "improper number of parts to 'assert' statement: %d",
3461 NCH(n));
3462 return NULL;
3463}
3464
3465static asdl_seq *
3466ast_for_suite(struct compiling *c, const node *n)
3467{
3468 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003469 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 stmt_ty s;
3471 int i, total, num, end, pos = 0;
3472 node *ch;
3473
3474 REQ(n, suite);
3475
3476 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003477 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003479 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003481 n = CHILD(n, 0);
3482 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003484 */
3485 end = NCH(n) - 1;
3486 if (TYPE(CHILD(n, end - 1)) == SEMI)
3487 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003489 for (i = 0; i < end; i += 2) {
3490 ch = CHILD(n, i);
3491 s = ast_for_stmt(c, ch);
3492 if (!s)
3493 return NULL;
3494 asdl_seq_SET(seq, pos++, s);
3495 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496 }
3497 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003498 for (i = 2; i < (NCH(n) - 1); i++) {
3499 ch = CHILD(n, i);
3500 REQ(ch, stmt);
3501 num = num_stmts(ch);
3502 if (num == 1) {
3503 /* small_stmt or compound_stmt with only one child */
3504 s = ast_for_stmt(c, ch);
3505 if (!s)
3506 return NULL;
3507 asdl_seq_SET(seq, pos++, s);
3508 }
3509 else {
3510 int j;
3511 ch = CHILD(ch, 0);
3512 REQ(ch, simple_stmt);
3513 for (j = 0; j < NCH(ch); j += 2) {
3514 /* statement terminates with a semi-colon ';' */
3515 if (NCH(CHILD(ch, j)) == 0) {
3516 assert((j + 1) == NCH(ch));
3517 break;
3518 }
3519 s = ast_for_stmt(c, CHILD(ch, j));
3520 if (!s)
3521 return NULL;
3522 asdl_seq_SET(seq, pos++, s);
3523 }
3524 }
3525 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526 }
3527 assert(pos == seq->size);
3528 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529}
3530
3531static stmt_ty
3532ast_for_if_stmt(struct compiling *c, const node *n)
3533{
3534 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3535 ['else' ':' suite]
3536 */
3537 char *s;
3538
3539 REQ(n, if_stmt);
3540
3541 if (NCH(n) == 4) {
3542 expr_ty expression;
3543 asdl_seq *suite_seq;
3544
3545 expression = ast_for_expr(c, CHILD(n, 1));
3546 if (!expression)
3547 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003549 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551
Guido van Rossumd8faa362007-04-27 19:54:29 +00003552 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3553 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 s = STR(CHILD(n, 4));
3557 /* s[2], the third character in the string, will be
3558 's' for el_s_e, or
3559 'i' for el_i_f
3560 */
3561 if (s[2] == 's') {
3562 expr_ty expression;
3563 asdl_seq *seq1, *seq2;
3564
3565 expression = ast_for_expr(c, CHILD(n, 1));
3566 if (!expression)
3567 return NULL;
3568 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003569 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 return NULL;
3571 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003572 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 return NULL;
3574
Guido van Rossumd8faa362007-04-27 19:54:29 +00003575 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3576 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 }
3578 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003579 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003580 expr_ty expression;
3581 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003582 asdl_seq *orelse = NULL;
3583 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 /* must reference the child n_elif+1 since 'else' token is third,
3585 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003586 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3587 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3588 has_else = 1;
3589 n_elif -= 3;
3590 }
3591 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592
Thomas Wouters89f507f2006-12-13 04:49:30 +00003593 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003594 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003596 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003597 if (!orelse)
3598 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003600 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003602 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3603 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003605 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3606 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 asdl_seq_SET(orelse, 0,
3610 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003611 LINENO(CHILD(n, NCH(n) - 6)),
3612 CHILD(n, NCH(n) - 6)->n_col_offset,
3613 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003614 /* the just-created orelse handled the last elif */
3615 n_elif--;
3616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617
Thomas Wouters89f507f2006-12-13 04:49:30 +00003618 for (i = 0; i < n_elif; i++) {
3619 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003620 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003621 if (!newobj)
3622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003624 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003627 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003629
Thomas Wouters89f507f2006-12-13 04:49:30 +00003630 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003632 LINENO(CHILD(n, off)),
3633 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003634 orelse = newobj;
3635 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003636 expression = ast_for_expr(c, CHILD(n, 1));
3637 if (!expression)
3638 return NULL;
3639 suite_seq = ast_for_suite(c, CHILD(n, 3));
3640 if (!suite_seq)
3641 return NULL;
3642 return If(expression, suite_seq, orelse,
3643 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003645
3646 PyErr_Format(PyExc_SystemError,
3647 "unexpected token in 'if' statement: %s", s);
3648 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649}
3650
3651static stmt_ty
3652ast_for_while_stmt(struct compiling *c, const node *n)
3653{
3654 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3655 REQ(n, while_stmt);
3656
3657 if (NCH(n) == 4) {
3658 expr_ty expression;
3659 asdl_seq *suite_seq;
3660
3661 expression = ast_for_expr(c, CHILD(n, 1));
3662 if (!expression)
3663 return NULL;
3664 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003665 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003667 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668 }
3669 else if (NCH(n) == 7) {
3670 expr_ty expression;
3671 asdl_seq *seq1, *seq2;
3672
3673 expression = ast_for_expr(c, CHILD(n, 1));
3674 if (!expression)
3675 return NULL;
3676 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003677 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678 return NULL;
3679 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003680 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 return NULL;
3682
Thomas Wouters89f507f2006-12-13 04:49:30 +00003683 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003685
3686 PyErr_Format(PyExc_SystemError,
3687 "wrong number of tokens for 'while' statement: %d",
3688 NCH(n));
3689 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690}
3691
3692static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003693ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003695 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003697 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003698 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3700 REQ(n, for_stmt);
3701
3702 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003703 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 if (!seq)
3705 return NULL;
3706 }
3707
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003708 node_target = CHILD(n, 1);
3709 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003710 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003712 /* Check the # of children rather than the length of _target, since
3713 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003714 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003715 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003716 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003718 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003720 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003721 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 return NULL;
3723 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003724 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 return NULL;
3726
Yury Selivanov75445082015-05-11 22:57:16 -04003727 if (is_async)
3728 return AsyncFor(target, expression, suite_seq, seq,
3729 LINENO(n), n->n_col_offset,
3730 c->c_arena);
3731 else
3732 return For(target, expression, suite_seq, seq,
3733 LINENO(n), n->n_col_offset,
3734 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735}
3736
3737static excepthandler_ty
3738ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3739{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003740 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 REQ(exc, except_clause);
3742 REQ(body, suite);
3743
3744 if (NCH(exc) == 1) {
3745 asdl_seq *suite_seq = ast_for_suite(c, body);
3746 if (!suite_seq)
3747 return NULL;
3748
Neal Norwitzad74aa82008-03-31 05:14:30 +00003749 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003750 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 }
3752 else if (NCH(exc) == 2) {
3753 expr_ty expression;
3754 asdl_seq *suite_seq;
3755
3756 expression = ast_for_expr(c, CHILD(exc, 1));
3757 if (!expression)
3758 return NULL;
3759 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003760 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761 return NULL;
3762
Neal Norwitzad74aa82008-03-31 05:14:30 +00003763 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003764 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 }
3766 else if (NCH(exc) == 4) {
3767 asdl_seq *suite_seq;
3768 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003769 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003770 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003772 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003773 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003775 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 return NULL;
3777 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003778 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779 return NULL;
3780
Neal Norwitzad74aa82008-03-31 05:14:30 +00003781 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003782 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003784
3785 PyErr_Format(PyExc_SystemError,
3786 "wrong number of children for 'except' clause: %d",
3787 NCH(exc));
3788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789}
3790
3791static stmt_ty
3792ast_for_try_stmt(struct compiling *c, const node *n)
3793{
Neal Norwitzf599f422005-12-17 21:33:47 +00003794 const int nch = NCH(n);
3795 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003796 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003797
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 REQ(n, try_stmt);
3799
Neal Norwitzf599f422005-12-17 21:33:47 +00003800 body = ast_for_suite(c, CHILD(n, 2));
3801 if (body == NULL)
3802 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803
Neal Norwitzf599f422005-12-17 21:33:47 +00003804 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3805 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3806 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3807 /* we can assume it's an "else",
3808 because nch >= 9 for try-else-finally and
3809 it would otherwise have a type of except_clause */
3810 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3811 if (orelse == NULL)
3812 return NULL;
3813 n_except--;
3814 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815
Neal Norwitzf599f422005-12-17 21:33:47 +00003816 finally = ast_for_suite(c, CHILD(n, nch - 1));
3817 if (finally == NULL)
3818 return NULL;
3819 n_except--;
3820 }
3821 else {
3822 /* we can assume it's an "else",
3823 otherwise it would have a type of except_clause */
3824 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3825 if (orelse == NULL)
3826 return NULL;
3827 n_except--;
3828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003830 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003831 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832 return NULL;
3833 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834
Neal Norwitzf599f422005-12-17 21:33:47 +00003835 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003836 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003837 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003838 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003839 if (handlers == NULL)
3840 return NULL;
3841
3842 for (i = 0; i < n_except; i++) {
3843 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3844 CHILD(n, 5 + i * 3));
3845 if (!e)
3846 return NULL;
3847 asdl_seq_SET(handlers, i, e);
3848 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003849 }
3850
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003851 assert(finally != NULL || asdl_seq_LEN(handlers));
3852 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853}
3854
Georg Brandl0c315622009-05-25 21:10:36 +00003855/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003856static withitem_ty
3857ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003858{
3859 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003860
Georg Brandl0c315622009-05-25 21:10:36 +00003861 REQ(n, with_item);
3862 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003863 if (!context_expr)
3864 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003865 if (NCH(n) == 3) {
3866 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003867
3868 if (!optional_vars) {
3869 return NULL;
3870 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003871 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003872 return NULL;
3873 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003874 }
3875
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003876 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003877}
3878
Georg Brandl0c315622009-05-25 21:10:36 +00003879/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3880static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003881ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003882{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003883 int i, n_items;
3884 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003885
3886 REQ(n, with_stmt);
3887
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003888 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003889 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003890 if (!items)
3891 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003892 for (i = 1; i < NCH(n) - 2; i += 2) {
3893 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3894 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003895 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003896 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003897 }
3898
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003899 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3900 if (!body)
3901 return NULL;
3902
Yury Selivanov75445082015-05-11 22:57:16 -04003903 if (is_async)
3904 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3905 else
3906 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003907}
3908
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003910ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003912 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003913 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003914 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003915 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003916
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917 REQ(n, classdef);
3918
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003919 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920 s = ast_for_suite(c, CHILD(n, 3));
3921 if (!s)
3922 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003923 classname = NEW_IDENTIFIER(CHILD(n, 1));
3924 if (!classname)
3925 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003926 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003927 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003928 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3929 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003931
3932 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003933 s = ast_for_suite(c, CHILD(n,5));
3934 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003935 return NULL;
3936 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, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003940 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003941 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3942 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 }
3944
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003945 /* class NAME '(' arglist ')' ':' suite */
3946 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003947 {
3948 PyObject *dummy_name;
3949 expr_ty dummy;
3950 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3951 if (!dummy_name)
3952 return NULL;
3953 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3954 call = ast_for_call(c, CHILD(n, 3), dummy);
3955 if (!call)
3956 return NULL;
3957 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003959 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003961 classname = NEW_IDENTIFIER(CHILD(n, 1));
3962 if (!classname)
3963 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003964 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003965 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003966
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003967 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003968 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969}
3970
3971static stmt_ty
3972ast_for_stmt(struct compiling *c, const node *n)
3973{
3974 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003975 assert(NCH(n) == 1);
3976 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 }
3978 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003979 assert(num_stmts(n) == 1);
3980 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 }
3982 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003983 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003984 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3985 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003986 */
3987 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988 case expr_stmt:
3989 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 case del_stmt:
3991 return ast_for_del_stmt(c, n);
3992 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003993 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003994 case flow_stmt:
3995 return ast_for_flow_stmt(c, n);
3996 case import_stmt:
3997 return ast_for_import_stmt(c, n);
3998 case global_stmt:
3999 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004000 case nonlocal_stmt:
4001 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 case assert_stmt:
4003 return ast_for_assert_stmt(c, n);
4004 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004005 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4007 TYPE(n), NCH(n));
4008 return NULL;
4009 }
4010 }
4011 else {
4012 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004013 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004014 */
4015 node *ch = CHILD(n, 0);
4016 REQ(n, compound_stmt);
4017 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004018 case if_stmt:
4019 return ast_for_if_stmt(c, ch);
4020 case while_stmt:
4021 return ast_for_while_stmt(c, ch);
4022 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004023 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004024 case try_stmt:
4025 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004026 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004027 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004029 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004031 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 case decorated:
4033 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004034 case async_stmt:
4035 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004037 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4039 TYPE(n), NCH(n));
4040 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004041 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 }
4043}
4044
4045static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004046parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004048 const char *end;
4049 long x;
4050 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004051 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004052 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004054 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 errno = 0;
4056 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004057 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004058 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004059 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004060 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004061 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004062 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004063 }
4064 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004065 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004066 if (*end == '\0') {
4067 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004068 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004069 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004070 }
4071 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004072 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004073 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004074 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4075 if (compl.imag == -1.0 && PyErr_Occurred())
4076 return NULL;
4077 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004078 }
4079 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004080 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004081 dx = PyOS_string_to_double(s, NULL, NULL);
4082 if (dx == -1.0 && PyErr_Occurred())
4083 return NULL;
4084 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004085 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086}
4087
4088static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004089parsenumber(struct compiling *c, const char *s)
4090{
4091 char *dup, *end;
4092 PyObject *res = NULL;
4093
4094 assert(s != NULL);
4095
4096 if (strchr(s, '_') == NULL) {
4097 return parsenumber_raw(c, s);
4098 }
4099 /* Create a duplicate without underscores. */
4100 dup = PyMem_Malloc(strlen(s) + 1);
4101 end = dup;
4102 for (; *s; s++) {
4103 if (*s != '_') {
4104 *end++ = *s;
4105 }
4106 }
4107 *end = '\0';
4108 res = parsenumber_raw(c, dup);
4109 PyMem_Free(dup);
4110 return res;
4111}
4112
4113static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004114decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004116 const char *s, *t;
4117 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004118 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4119 while (s < end && (*s & 0x80)) s++;
4120 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004121 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122}
4123
Eric V. Smith56466482016-10-31 14:46:26 -04004124static int
4125warn_invalid_escape_sequence(struct compiling *c, const node *n,
4126 char first_invalid_escape_char)
4127{
4128 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4129 first_invalid_escape_char);
4130 if (msg == NULL) {
4131 return -1;
4132 }
4133 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4134 c->c_filename, LINENO(n),
4135 NULL, NULL) < 0 &&
4136 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4137 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004138 const char *s;
4139
4140 /* Replace the DeprecationWarning exception with a SyntaxError
4141 to get a more accurate error report */
4142 PyErr_Clear();
4143
4144 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004145 if (s != NULL) {
4146 ast_error(c, n, s);
4147 }
4148 Py_DECREF(msg);
4149 return -1;
4150 }
4151 Py_DECREF(msg);
4152 return 0;
4153}
4154
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004156decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4157 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004158{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004159 PyObject *v, *u;
4160 char *buf;
4161 char *p;
4162 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004163
Benjamin Peterson202803a2016-02-25 22:34:45 -08004164 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004165 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004166 return NULL;
4167 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4168 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4169 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4170 if (u == NULL)
4171 return NULL;
4172 p = buf = PyBytes_AsString(u);
4173 end = s + len;
4174 while (s < end) {
4175 if (*s == '\\') {
4176 *p++ = *s++;
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004177 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004178 strcpy(p, "u005c");
4179 p += 5;
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004180 if (s >= end)
4181 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004182 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004183 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004184 if (*s & 0x80) { /* XXX inefficient */
4185 PyObject *w;
4186 int kind;
4187 void *data;
4188 Py_ssize_t len, i;
4189 w = decode_utf8(c, &s, end);
4190 if (w == NULL) {
4191 Py_DECREF(u);
4192 return NULL;
4193 }
4194 kind = PyUnicode_KIND(w);
4195 data = PyUnicode_DATA(w);
4196 len = PyUnicode_GET_LENGTH(w);
4197 for (i = 0; i < len; i++) {
4198 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4199 sprintf(p, "\\U%08x", chr);
4200 p += 10;
4201 }
4202 /* Should be impossible to overflow */
4203 assert(p - buf <= Py_SIZE(u));
4204 Py_DECREF(w);
4205 } else {
4206 *p++ = *s++;
4207 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004208 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004209 len = p - buf;
4210 s = buf;
4211
Eric V. Smith56466482016-10-31 14:46:26 -04004212 const char *first_invalid_escape;
4213 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4214
4215 if (v != NULL && first_invalid_escape != NULL) {
4216 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4217 /* We have not decref u before because first_invalid_escape points
4218 inside u. */
4219 Py_XDECREF(u);
4220 Py_DECREF(v);
4221 return NULL;
4222 }
4223 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004224 Py_XDECREF(u);
4225 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004226}
4227
Eric V. Smith56466482016-10-31 14:46:26 -04004228static PyObject *
4229decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4230 size_t len)
4231{
4232 const char *first_invalid_escape;
4233 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4234 &first_invalid_escape);
4235 if (result == NULL)
4236 return NULL;
4237
4238 if (first_invalid_escape != NULL) {
4239 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4240 Py_DECREF(result);
4241 return NULL;
4242 }
4243 }
4244 return result;
4245}
4246
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004247/* Shift locations for the given node and all its children by adding `lineno`
4248 and `col_offset` to existing locations. */
4249static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4250{
4251 n->n_col_offset = n->n_col_offset + col_offset;
4252 for (int i = 0; i < NCH(n); ++i) {
4253 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4254 /* Shifting column offsets unnecessary if there's been newlines. */
4255 col_offset = 0;
4256 }
4257 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4258 }
4259 n->n_lineno = n->n_lineno + lineno;
4260}
4261
4262/* Fix locations for the given node and its children.
4263
4264 `parent` is the enclosing node.
4265 `n` is the node which locations are going to be fixed relative to parent.
4266 `expr_str` is the child node's string representation, incuding braces.
4267*/
4268static void
4269fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4270{
4271 char *substr = NULL;
4272 char *start;
4273 int lines = LINENO(parent) - 1;
4274 int cols = parent->n_col_offset;
4275 /* Find the full fstring to fix location information in `n`. */
4276 while (parent && parent->n_type != STRING)
4277 parent = parent->n_child;
4278 if (parent && parent->n_str) {
4279 substr = strstr(parent->n_str, expr_str);
4280 if (substr) {
4281 start = substr;
4282 while (start > parent->n_str) {
4283 if (start[0] == '\n')
4284 break;
4285 start--;
4286 }
4287 cols += substr - start;
4288 /* Fix lineno in mulitline strings. */
4289 while ((substr = strchr(substr + 1, '\n')))
4290 lines--;
4291 }
4292 }
4293 fstring_shift_node_locations(n, lines, cols);
4294}
4295
Eric V. Smith451d0e32016-09-09 21:56:20 -04004296/* Compile this expression in to an expr_ty. Add parens around the
4297 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004298static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004299fstring_compile_expr(const char *expr_start, const char *expr_end,
4300 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004301
Eric V. Smith235a6f02015-09-19 14:51:32 -04004302{
4303 PyCompilerFlags cf;
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004304 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004305 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004306 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004307 Py_ssize_t len;
Serhiy Storchaka570b1c92017-06-09 00:38:06 +03004308 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004309
Eric V. Smith1d44c412015-09-23 07:49:00 -04004310 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004311 assert(*(expr_start-1) == '{');
4312 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004313
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004314 /* If the substring is all whitespace, it's an error. We need to catch this
4315 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4316 because turning the expression '' in to '()' would go from being invalid
4317 to valid. */
Serhiy Storchaka570b1c92017-06-09 00:38:06 +03004318 for (s = expr_start; s != expr_end; s++) {
4319 char c = *s;
4320 /* The Python parser ignores only the following whitespace
4321 characters (\r already is converted to \n). */
4322 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004323 break;
4324 }
4325 }
Serhiy Storchaka570b1c92017-06-09 00:38:06 +03004326 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004327 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004328 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004329 }
4330
Eric V. Smith451d0e32016-09-09 21:56:20 -04004331 len = expr_end - expr_start;
4332 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4333 str = PyMem_RawMalloc(len + 3);
4334 if (str == NULL)
4335 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004336
Eric V. Smith451d0e32016-09-09 21:56:20 -04004337 str[0] = '(';
4338 memcpy(str+1, expr_start, len);
4339 str[len+1] = ')';
4340 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004341
4342 cf.cf_flags = PyCF_ONLY_AST;
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004343 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4344 Py_eval_input, 0);
4345 if (!mod_n) {
4346 PyMem_RawFree(str);
4347 return NULL;
4348 }
4349 /* Reuse str to find the correct column offset. */
4350 str[0] = '{';
4351 str[len+1] = '}';
4352 fstring_fix_node_location(n, mod_n, str);
4353 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004354 PyMem_RawFree(str);
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004355 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004356 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004357 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004358 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004359}
4360
4361/* Return -1 on error.
4362
4363 Return 0 if we reached the end of the literal.
4364
4365 Return 1 if we haven't reached the end of the literal, but we want
4366 the caller to process the literal up to this point. Used for
4367 doubled braces.
4368*/
4369static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004370fstring_find_literal(const char **str, const char *end, int raw,
4371 PyObject **literal, int recurse_lvl,
4372 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004373{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004374 /* Get any literal string. It ends when we hit an un-doubled left
4375 brace (which isn't part of a unicode name escape such as
4376 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004377
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004378 const char *s = *str;
4379 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004380 int result = 0;
4381
Eric V. Smith235a6f02015-09-19 14:51:32 -04004382 assert(*literal == NULL);
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004383 while (s < end) {
4384 char ch = *s++;
4385 if (!raw && ch == '\\' && s < end) {
4386 ch = *s++;
4387 if (ch == 'N') {
4388 if (s < end && *s++ == '{') {
4389 while (s < end && *s++ != '}') {
4390 }
4391 continue;
4392 }
4393 break;
4394 }
4395 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4396 return -1;
4397 }
4398 }
4399 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004400 /* Check for doubled braces, but only at the top level. If
4401 we checked at every level, then f'{0:{3}}' would fail
4402 with the two closing braces. */
4403 if (recurse_lvl == 0) {
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004404 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004405 /* We're going to tell the caller that the literal ends
4406 here, but that they should continue scanning. But also
4407 skip over the second brace when we resume scanning. */
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004408 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004409 result = 1;
4410 goto done;
4411 }
4412
4413 /* Where a single '{' is the start of a new expression, a
4414 single '}' is not allowed. */
4415 if (ch == '}') {
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004416 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004417 ast_error(c, n, "f-string: single '}' is not allowed");
4418 return -1;
4419 }
4420 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004421 /* We're either at a '{', which means we're starting another
4422 expression; or a '}', which means we're at the end of this
4423 f-string (for a nested format_spec). */
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004424 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004425 break;
4426 }
4427 }
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004428 *str = s;
4429 assert(s <= end);
4430 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004431done:
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004432 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004433 if (raw)
4434 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004435 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004436 NULL, NULL);
4437 else
Eric V. Smith56466482016-10-31 14:46:26 -04004438 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004439 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004440 if (!*literal)
4441 return -1;
4442 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004443 return result;
4444}
4445
4446/* Forward declaration because parsing is recursive. */
4447static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004448fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004449 struct compiling *c, const node *n);
4450
Eric V. Smith451d0e32016-09-09 21:56:20 -04004451/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004452 expression (so it must be a '{'). Returns the FormattedValue node,
4453 which includes the expression, conversion character, and
4454 format_spec expression.
4455
4456 Note that I don't do a perfect job here: I don't make sure that a
4457 closing brace doesn't match an opening paren, for example. It
4458 doesn't need to error on all invalid expressions, just correctly
4459 find the end of all valid ones. Any errors inside the expression
4460 will be caught when we parse it later. */
4461static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004462fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004463 expr_ty *expression, struct compiling *c, const node *n)
4464{
4465 /* Return -1 on error, else 0. */
4466
Eric V. Smith451d0e32016-09-09 21:56:20 -04004467 const char *expr_start;
4468 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004469 expr_ty simple_expression;
4470 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004471 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004472
4473 /* 0 if we're not in a string, else the quote char we're trying to
4474 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004475 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004476
4477 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4478 int string_type = 0;
4479
4480 /* Keep track of nesting level for braces/parens/brackets in
4481 expressions. */
4482 Py_ssize_t nested_depth = 0;
4483
4484 /* Can only nest one level deep. */
4485 if (recurse_lvl >= 2) {
4486 ast_error(c, n, "f-string: expressions nested too deeply");
4487 return -1;
4488 }
4489
4490 /* The first char must be a left brace, or we wouldn't have gotten
4491 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004492 assert(**str == '{');
4493 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004494
Eric V. Smith451d0e32016-09-09 21:56:20 -04004495 expr_start = *str;
4496 for (; *str < end; (*str)++) {
4497 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004498
4499 /* Loop invariants. */
4500 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004501 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004502 if (quote_char)
4503 assert(string_type == 1 || string_type == 3);
4504 else
4505 assert(string_type == 0);
4506
Eric V. Smith451d0e32016-09-09 21:56:20 -04004507 ch = **str;
4508 /* Nowhere inside an expression is a backslash allowed. */
4509 if (ch == '\\') {
4510 /* Error: can't include a backslash character, inside
4511 parens or strings or not. */
4512 ast_error(c, n, "f-string expression part "
4513 "cannot include a backslash");
4514 return -1;
4515 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004516 if (quote_char) {
4517 /* We're inside a string. See if we're at the end. */
4518 /* This code needs to implement the same non-error logic
4519 as tok_get from tokenizer.c, at the letter_quote
4520 label. To actually share that code would be a
4521 nightmare. But, it's unlikely to change and is small,
4522 so duplicate it here. Note we don't need to catch all
4523 of the errors, since they'll be caught when parsing the
4524 expression. We just need to match the non-error
4525 cases. Thus we can ignore \n in single-quoted strings,
4526 for example. Or non-terminated strings. */
4527 if (ch == quote_char) {
4528 /* Does this match the string_type (single or triple
4529 quoted)? */
4530 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004531 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004532 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004533 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004534 string_type = 0;
4535 quote_char = 0;
4536 continue;
4537 }
4538 } else {
4539 /* We're at the end of a normal string. */
4540 quote_char = 0;
4541 string_type = 0;
4542 continue;
4543 }
4544 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004545 } else if (ch == '\'' || ch == '"') {
4546 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004547 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004548 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004549 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004550 } else {
4551 /* Start of a normal string. */
4552 string_type = 1;
4553 }
4554 /* Start looking for the end of the string. */
4555 quote_char = ch;
4556 } else if (ch == '[' || ch == '{' || ch == '(') {
4557 nested_depth++;
4558 } else if (nested_depth != 0 &&
4559 (ch == ']' || ch == '}' || ch == ')')) {
4560 nested_depth--;
4561 } else if (ch == '#') {
4562 /* Error: can't include a comment character, inside parens
4563 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004564 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004565 return -1;
4566 } else if (nested_depth == 0 &&
4567 (ch == '!' || ch == ':' || ch == '}')) {
4568 /* First, test for the special case of "!=". Since '=' is
4569 not an allowed conversion character, nothing is lost in
4570 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004571 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004572 /* This isn't a conversion character, just continue. */
4573 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004574 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004575 /* Normal way out of this loop. */
4576 break;
4577 } else {
4578 /* Just consume this char and loop around. */
4579 }
4580 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004581 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004582 /* If we leave this loop in a string or with mismatched parens, we
4583 don't care. We'll get a syntax error when compiling the
4584 expression. But, we can produce a better error message, so
4585 let's just do that.*/
4586 if (quote_char) {
4587 ast_error(c, n, "f-string: unterminated string");
4588 return -1;
4589 }
4590 if (nested_depth) {
4591 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4592 return -1;
4593 }
4594
Eric V. Smith451d0e32016-09-09 21:56:20 -04004595 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004596 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004597
4598 /* Compile the expression as soon as possible, so we show errors
4599 related to the expression before errors related to the
4600 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004601 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004602 if (!simple_expression)
4603 return -1;
4604
4605 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004606 if (**str == '!') {
4607 *str += 1;
4608 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004609 goto unexpected_end_of_string;
4610
Eric V. Smith451d0e32016-09-09 21:56:20 -04004611 conversion = **str;
4612 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004613
4614 /* Validate the conversion. */
4615 if (!(conversion == 's' || conversion == 'r'
4616 || conversion == 'a')) {
4617 ast_error(c, n, "f-string: invalid conversion character: "
4618 "expected 's', 'r', or 'a'");
4619 return -1;
4620 }
4621 }
4622
4623 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004624 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004625 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004626 if (**str == ':') {
4627 *str += 1;
4628 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004629 goto unexpected_end_of_string;
4630
4631 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004632 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004633 if (!format_spec)
4634 return -1;
4635 }
4636
Eric V. Smith451d0e32016-09-09 21:56:20 -04004637 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004638 goto unexpected_end_of_string;
4639
4640 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004641 assert(*str < end);
4642 assert(**str == '}');
4643 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004644
Eric V. Smith451d0e32016-09-09 21:56:20 -04004645 /* And now create the FormattedValue node that represents this
4646 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004647 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004648 format_spec, LINENO(n), n->n_col_offset,
4649 c->c_arena);
4650 if (!*expression)
4651 return -1;
4652
4653 return 0;
4654
4655unexpected_end_of_string:
4656 ast_error(c, n, "f-string: expecting '}'");
4657 return -1;
4658}
4659
4660/* Return -1 on error.
4661
4662 Return 0 if we have a literal (possible zero length) and an
4663 expression (zero length if at the end of the string.
4664
4665 Return 1 if we have a literal, but no expression, and we want the
4666 caller to call us again. This is used to deal with doubled
4667 braces.
4668
4669 When called multiple times on the string 'a{{b{0}c', this function
4670 will return:
4671
4672 1. the literal 'a{' with no expression, and a return value
4673 of 1. Despite the fact that there's no expression, the return
4674 value of 1 means we're not finished yet.
4675
4676 2. the literal 'b' and the expression '0', with a return value of
4677 0. The fact that there's an expression means we're not finished.
4678
4679 3. literal 'c' with no expression and a return value of 0. The
4680 combination of the return value of 0 with no expression means
4681 we're finished.
4682*/
4683static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004684fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4685 int recurse_lvl, PyObject **literal,
4686 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004687 struct compiling *c, const node *n)
4688{
4689 int result;
4690
4691 assert(*literal == NULL && *expression == NULL);
4692
4693 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004694 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004695 if (result < 0)
4696 goto error;
4697
4698 assert(result == 0 || result == 1);
4699
4700 if (result == 1)
4701 /* We have a literal, but don't look at the expression. */
4702 return 1;
4703
Eric V. Smith451d0e32016-09-09 21:56:20 -04004704 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004705 /* We're at the end of the string or the end of a nested
4706 f-string: no expression. The top-level error case where we
4707 expect to be at the end of the string but we're at a '}' is
4708 handled later. */
4709 return 0;
4710
4711 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004712 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004713
Eric V. Smith451d0e32016-09-09 21:56:20 -04004714 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004715 goto error;
4716
4717 return 0;
4718
4719error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004720 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004721 return -1;
4722}
4723
4724#define EXPRLIST_N_CACHED 64
4725
4726typedef struct {
4727 /* Incrementally build an array of expr_ty, so be used in an
4728 asdl_seq. Cache some small but reasonably sized number of
4729 expr_ty's, and then after that start dynamically allocating,
4730 doubling the number allocated each time. Note that the f-string
4731 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4732 Str for the literal 'a'. So you add expr_ty's about twice as
4733 fast as you add exressions in an f-string. */
4734
4735 Py_ssize_t allocated; /* Number we've allocated. */
4736 Py_ssize_t size; /* Number we've used. */
4737 expr_ty *p; /* Pointer to the memory we're actually
4738 using. Will point to 'data' until we
4739 start dynamically allocating. */
4740 expr_ty data[EXPRLIST_N_CACHED];
4741} ExprList;
4742
4743#ifdef NDEBUG
4744#define ExprList_check_invariants(l)
4745#else
4746static void
4747ExprList_check_invariants(ExprList *l)
4748{
4749 /* Check our invariants. Make sure this object is "live", and
4750 hasn't been deallocated. */
4751 assert(l->size >= 0);
4752 assert(l->p != NULL);
4753 if (l->size <= EXPRLIST_N_CACHED)
4754 assert(l->data == l->p);
4755}
4756#endif
4757
4758static void
4759ExprList_Init(ExprList *l)
4760{
4761 l->allocated = EXPRLIST_N_CACHED;
4762 l->size = 0;
4763
4764 /* Until we start allocating dynamically, p points to data. */
4765 l->p = l->data;
4766
4767 ExprList_check_invariants(l);
4768}
4769
4770static int
4771ExprList_Append(ExprList *l, expr_ty exp)
4772{
4773 ExprList_check_invariants(l);
4774 if (l->size >= l->allocated) {
4775 /* We need to alloc (or realloc) the memory. */
4776 Py_ssize_t new_size = l->allocated * 2;
4777
4778 /* See if we've ever allocated anything dynamically. */
4779 if (l->p == l->data) {
4780 Py_ssize_t i;
4781 /* We're still using the cached data. Switch to
4782 alloc-ing. */
4783 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4784 if (!l->p)
4785 return -1;
4786 /* Copy the cached data into the new buffer. */
4787 for (i = 0; i < l->size; i++)
4788 l->p[i] = l->data[i];
4789 } else {
4790 /* Just realloc. */
4791 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4792 if (!tmp) {
4793 PyMem_RawFree(l->p);
4794 l->p = NULL;
4795 return -1;
4796 }
4797 l->p = tmp;
4798 }
4799
4800 l->allocated = new_size;
4801 assert(l->allocated == 2 * l->size);
4802 }
4803
4804 l->p[l->size++] = exp;
4805
4806 ExprList_check_invariants(l);
4807 return 0;
4808}
4809
4810static void
4811ExprList_Dealloc(ExprList *l)
4812{
4813 ExprList_check_invariants(l);
4814
4815 /* If there's been an error, or we've never dynamically allocated,
4816 do nothing. */
4817 if (!l->p || l->p == l->data) {
4818 /* Do nothing. */
4819 } else {
4820 /* We have dynamically allocated. Free the memory. */
4821 PyMem_RawFree(l->p);
4822 }
4823 l->p = NULL;
4824 l->size = -1;
4825}
4826
4827static asdl_seq *
4828ExprList_Finish(ExprList *l, PyArena *arena)
4829{
4830 asdl_seq *seq;
4831
4832 ExprList_check_invariants(l);
4833
4834 /* Allocate the asdl_seq and copy the expressions in to it. */
4835 seq = _Py_asdl_seq_new(l->size, arena);
4836 if (seq) {
4837 Py_ssize_t i;
4838 for (i = 0; i < l->size; i++)
4839 asdl_seq_SET(seq, i, l->p[i]);
4840 }
4841 ExprList_Dealloc(l);
4842 return seq;
4843}
4844
4845/* The FstringParser is designed to add a mix of strings and
4846 f-strings, and concat them together as needed. Ultimately, it
4847 generates an expr_ty. */
4848typedef struct {
4849 PyObject *last_str;
4850 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004851 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004852} FstringParser;
4853
4854#ifdef NDEBUG
4855#define FstringParser_check_invariants(state)
4856#else
4857static void
4858FstringParser_check_invariants(FstringParser *state)
4859{
4860 if (state->last_str)
4861 assert(PyUnicode_CheckExact(state->last_str));
4862 ExprList_check_invariants(&state->expr_list);
4863}
4864#endif
4865
4866static void
4867FstringParser_Init(FstringParser *state)
4868{
4869 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004870 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004871 ExprList_Init(&state->expr_list);
4872 FstringParser_check_invariants(state);
4873}
4874
4875static void
4876FstringParser_Dealloc(FstringParser *state)
4877{
4878 FstringParser_check_invariants(state);
4879
4880 Py_XDECREF(state->last_str);
4881 ExprList_Dealloc(&state->expr_list);
4882}
4883
4884/* Make a Str node, but decref the PyUnicode object being added. */
4885static expr_ty
4886make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4887{
4888 PyObject *s = *str;
4889 *str = NULL;
4890 assert(PyUnicode_CheckExact(s));
4891 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4892 Py_DECREF(s);
4893 return NULL;
4894 }
4895 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4896}
4897
4898/* Add a non-f-string (that is, a regular literal string). str is
4899 decref'd. */
4900static int
4901FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4902{
4903 FstringParser_check_invariants(state);
4904
4905 assert(PyUnicode_CheckExact(str));
4906
4907 if (PyUnicode_GET_LENGTH(str) == 0) {
4908 Py_DECREF(str);
4909 return 0;
4910 }
4911
4912 if (!state->last_str) {
4913 /* We didn't have a string before, so just remember this one. */
4914 state->last_str = str;
4915 } else {
4916 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004917 PyUnicode_AppendAndDel(&state->last_str, str);
4918 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919 return -1;
4920 }
4921 FstringParser_check_invariants(state);
4922 return 0;
4923}
4924
Eric V. Smith451d0e32016-09-09 21:56:20 -04004925/* Parse an f-string. The f-string is in *str to end, with no
4926 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004928FstringParser_ConcatFstring(FstringParser *state, const char **str,
4929 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004930 struct compiling *c, const node *n)
4931{
4932 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004933 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004934
4935 /* Parse the f-string. */
4936 while (1) {
4937 PyObject *literal = NULL;
4938 expr_ty expression = NULL;
4939
4940 /* If there's a zero length literal in front of the
4941 expression, literal will be NULL. If we're at the end of
4942 the f-string, expression will be NULL (unless result == 1,
4943 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004944 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004945 &literal, &expression,
4946 c, n);
4947 if (result < 0)
4948 return -1;
4949
4950 /* Add the literal, if any. */
4951 if (!literal) {
4952 /* Do nothing. Just leave last_str alone (and possibly
4953 NULL). */
4954 } else if (!state->last_str) {
Serhiy Storchaka2eca5b42017-06-16 16:29:42 +03004955 /* Note that the literal can be zero length, if the
4956 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004957 state->last_str = literal;
4958 literal = NULL;
4959 } else {
4960 /* We have a literal, concatenate it. */
4961 assert(PyUnicode_GET_LENGTH(literal) != 0);
4962 if (FstringParser_ConcatAndDel(state, literal) < 0)
4963 return -1;
4964 literal = NULL;
4965 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966
4967 /* We've dealt with the literal now. It can't be leaked on further
4968 errors. */
4969 assert(literal == NULL);
4970
4971 /* See if we should just loop around to get the next literal
4972 and expression, while ignoring the expression this
4973 time. This is used for un-doubling braces, as an
4974 optimization. */
4975 if (result == 1)
4976 continue;
4977
4978 if (!expression)
4979 /* We're done with this f-string. */
4980 break;
4981
4982 /* We know we have an expression. Convert any existing string
4983 to a Str node. */
4984 if (!state->last_str) {
4985 /* Do nothing. No previous literal. */
4986 } else {
4987 /* Convert the existing last_str literal to a Str node. */
4988 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4989 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4990 return -1;
4991 }
4992
4993 if (ExprList_Append(&state->expr_list, expression) < 0)
4994 return -1;
4995 }
4996
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 /* If recurse_lvl is zero, then we must be at the end of the
4998 string. Otherwise, we must be at a right brace. */
4999
Eric V. Smith451d0e32016-09-09 21:56:20 -04005000 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005001 ast_error(c, n, "f-string: unexpected end of string");
5002 return -1;
5003 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005004 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005005 ast_error(c, n, "f-string: expecting '}'");
5006 return -1;
5007 }
5008
5009 FstringParser_check_invariants(state);
5010 return 0;
5011}
5012
5013/* Convert the partial state reflected in last_str and expr_list to an
5014 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5015static expr_ty
5016FstringParser_Finish(FstringParser *state, struct compiling *c,
5017 const node *n)
5018{
5019 asdl_seq *seq;
5020
5021 FstringParser_check_invariants(state);
5022
5023 /* If we're just a constant string with no expressions, return
5024 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005025 if (!state->fmode) {
5026 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027 if (!state->last_str) {
5028 /* Create a zero length string. */
5029 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5030 if (!state->last_str)
5031 goto error;
5032 }
5033 return make_str_node_and_del(&state->last_str, c, n);
5034 }
5035
5036 /* Create a Str node out of last_str, if needed. It will be the
5037 last node in our expression list. */
5038 if (state->last_str) {
5039 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5040 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5041 goto error;
5042 }
5043 /* This has already been freed. */
5044 assert(state->last_str == NULL);
5045
5046 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5047 if (!seq)
5048 goto error;
5049
Eric V. Smith235a6f02015-09-19 14:51:32 -04005050 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5051
5052error:
5053 FstringParser_Dealloc(state);
5054 return NULL;
5055}
5056
Eric V. Smith451d0e32016-09-09 21:56:20 -04005057/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5058 at end, parse it into an expr_ty. Return NULL on error. Adjust
5059 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005060static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005061fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005062 struct compiling *c, const node *n)
5063{
5064 FstringParser state;
5065
5066 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005067 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005068 c, n) < 0) {
5069 FstringParser_Dealloc(&state);
5070 return NULL;
5071 }
5072
5073 return FstringParser_Finish(&state, c, n);
5074}
5075
5076/* n is a Python string literal, including the bracketing quote
5077 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005078 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005079 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005080 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5081 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005082*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005083static int
5084parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5085 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005086{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005087 size_t len;
5088 const char *s = STR(n);
5089 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090 int fmode = 0;
5091 *bytesmode = 0;
5092 *rawmode = 0;
5093 *result = NULL;
5094 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005095 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005097 if (quote == 'b' || quote == 'B') {
5098 quote = *++s;
5099 *bytesmode = 1;
5100 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005101 else if (quote == 'u' || quote == 'U') {
5102 quote = *++s;
5103 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005104 else if (quote == 'r' || quote == 'R') {
5105 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005106 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005107 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005108 else if (quote == 'f' || quote == 'F') {
5109 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005110 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005111 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005112 else {
5113 break;
5114 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005115 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005116 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005117 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005119 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005120 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005121 if (quote != '\'' && quote != '\"') {
5122 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005123 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005124 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005125 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005126 s++;
5127 len = strlen(s);
5128 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005130 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005131 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005132 }
5133 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005135 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005136 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005137 }
5138 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005139 /* A triple quoted string. We've already skipped one quote at
5140 the start and one at the end of the string. Now skip the
5141 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005142 s += 2;
5143 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005144 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005145 if (s[--len] != quote || s[--len] != quote) {
5146 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005147 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005148 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005149 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005150
Eric V. Smith451d0e32016-09-09 21:56:20 -04005151 if (fmode) {
5152 /* Just return the bytes. The caller will parse the resulting
5153 string. */
5154 *fstr = s;
5155 *fstrlen = len;
5156 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005157 }
5158
Eric V. Smith451d0e32016-09-09 21:56:20 -04005159 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005160 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005161 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005162 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005163 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005164 const char *ch;
5165 for (ch = s; *ch; ch++) {
5166 if (Py_CHARMASK(*ch) >= 0x80) {
5167 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005168 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005169 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005170 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005171 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005172 if (*rawmode)
5173 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005174 else
Eric V. Smith56466482016-10-31 14:46:26 -04005175 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005176 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005177 if (*rawmode)
5178 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005179 else
Eric V. Smith56466482016-10-31 14:46:26 -04005180 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005181 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005182 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005183}
5184
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5186 each STRING atom, and process it as needed. For bytes, just
5187 concatenate them together, and the result will be a Bytes node. For
5188 normal strings and f-strings, concatenate them together. The result
5189 will be a Str node if there were no f-strings; a FormattedValue
5190 node if there's just an f-string (with no leading or trailing
5191 literals), or a JoinedStr node if there are multiple f-strings or
5192 any literals involved. */
5193static expr_ty
5194parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005195{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005196 int bytesmode = 0;
5197 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005198 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005199
5200 FstringParser state;
5201 FstringParser_Init(&state);
5202
5203 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005204 int this_bytesmode;
5205 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005206 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005207 const char *fstr;
5208 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005209
5210 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005211 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5212 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005213 goto error;
5214
5215 /* Check that we're not mixing bytes with unicode. */
5216 if (i != 0 && bytesmode != this_bytesmode) {
5217 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005218 /* s is NULL if the current string part is an f-string. */
5219 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005220 goto error;
5221 }
5222 bytesmode = this_bytesmode;
5223
Eric V. Smith451d0e32016-09-09 21:56:20 -04005224 if (fstr != NULL) {
5225 int result;
5226 assert(s == NULL && !bytesmode);
5227 /* This is an f-string. Parse and concatenate it. */
5228 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5229 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005230 if (result < 0)
5231 goto error;
5232 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005233 /* A string or byte string. */
5234 assert(s != NULL && fstr == NULL);
5235
Eric V. Smith451d0e32016-09-09 21:56:20 -04005236 assert(bytesmode ? PyBytes_CheckExact(s) :
5237 PyUnicode_CheckExact(s));
5238
Eric V. Smith451d0e32016-09-09 21:56:20 -04005239 if (bytesmode) {
5240 /* For bytes, concat as we go. */
5241 if (i == 0) {
5242 /* First time, just remember this value. */
5243 bytes_str = s;
5244 } else {
5245 PyBytes_ConcatAndDel(&bytes_str, s);
5246 if (!bytes_str)
5247 goto error;
5248 }
5249 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005250 /* This is a regular string. Concatenate it. */
5251 if (FstringParser_ConcatAndDel(&state, s) < 0)
5252 goto error;
5253 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005254 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005255 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005256 if (bytesmode) {
5257 /* Just return the bytes object and we're done. */
5258 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5259 goto error;
5260 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005262
Eric V. Smith235a6f02015-09-19 14:51:32 -04005263 /* We're not a bytes string, bytes_str should never have been set. */
5264 assert(bytes_str == NULL);
5265
5266 return FstringParser_Finish(&state, c, n);
5267
5268error:
5269 Py_XDECREF(bytes_str);
5270 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005271 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005272}