blob: e2092f0f85433e95cc690b03cc5399b448f61d81 [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"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
Serhiy Storchakaddbce132017-11-15 17:39:37 +020014#include <stdbool.h>
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Benjamin Peterson832bfe22011-08-09 16:15:04 -050016static int validate_stmts(asdl_seq *);
17static int validate_exprs(asdl_seq *, expr_context_ty, int);
18static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
19static int validate_stmt(stmt_ty);
20static int validate_expr(expr_ty, expr_context_ty);
21
22static int
23validate_comprehension(asdl_seq *gens)
24{
25 int i;
26 if (!asdl_seq_LEN(gens)) {
27 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
28 return 0;
29 }
30 for (i = 0; i < asdl_seq_LEN(gens); i++) {
31 comprehension_ty comp = asdl_seq_GET(gens, i);
32 if (!validate_expr(comp->target, Store) ||
33 !validate_expr(comp->iter, Load) ||
34 !validate_exprs(comp->ifs, Load, 0))
35 return 0;
36 }
37 return 1;
38}
39
40static int
41validate_slice(slice_ty slice)
42{
43 switch (slice->kind) {
44 case Slice_kind:
45 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
46 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
47 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
48 case ExtSlice_kind: {
49 int i;
50 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
51 return 0;
52 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
53 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
54 return 0;
55 return 1;
56 }
57 case Index_kind:
58 return validate_expr(slice->v.Index.value, Load);
59 default:
60 PyErr_SetString(PyExc_SystemError, "unknown slice node");
61 return 0;
62 }
63}
64
65static int
66validate_keywords(asdl_seq *keywords)
67{
68 int i;
69 for (i = 0; i < asdl_seq_LEN(keywords); i++)
70 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
71 return 0;
72 return 1;
73}
74
75static int
76validate_args(asdl_seq *args)
77{
78 int i;
79 for (i = 0; i < asdl_seq_LEN(args); i++) {
80 arg_ty arg = asdl_seq_GET(args, i);
81 if (arg->annotation && !validate_expr(arg->annotation, Load))
82 return 0;
83 }
84 return 1;
85}
86
87static const char *
88expr_context_name(expr_context_ty ctx)
89{
90 switch (ctx) {
91 case Load:
92 return "Load";
93 case Store:
94 return "Store";
95 case Del:
96 return "Del";
97 case AugLoad:
98 return "AugLoad";
99 case AugStore:
100 return "AugStore";
101 case Param:
102 return "Param";
103 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700104 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500105 }
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
INADA Naokicb41b272017-02-23 00:31:59 +0900370validate_body(asdl_seq *body, const char *owner, int allowempty)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500371{
INADA Naokicb41b272017-02-23 00:31:59 +0900372 if (!allowempty && !validate_nonempty_seq(body, "body", owner)) {
373 return 0;
374 }
375 return validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500376}
377
378static int
379validate_stmt(stmt_ty stmt)
380{
381 int i;
382 switch (stmt->kind) {
383 case FunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900384 return validate_body(stmt->v.FunctionDef.body, "FunctionDef",
385 stmt->v.FunctionDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500386 validate_arguments(stmt->v.FunctionDef.args) &&
387 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
388 (!stmt->v.FunctionDef.returns ||
389 validate_expr(stmt->v.FunctionDef.returns, Load));
390 case ClassDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900391 return validate_body(stmt->v.ClassDef.body, "ClassDef",
392 stmt->v.ClassDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
394 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400395 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500396 case Return_kind:
397 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
398 case Delete_kind:
399 return validate_assignlist(stmt->v.Delete.targets, Del);
400 case Assign_kind:
401 return validate_assignlist(stmt->v.Assign.targets, Store) &&
402 validate_expr(stmt->v.Assign.value, Load);
403 case AugAssign_kind:
404 return validate_expr(stmt->v.AugAssign.target, Store) &&
405 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700406 case AnnAssign_kind:
407 if (stmt->v.AnnAssign.target->kind != Name_kind &&
408 stmt->v.AnnAssign.simple) {
409 PyErr_SetString(PyExc_TypeError,
410 "AnnAssign with simple non-Name target");
411 return 0;
412 }
413 return validate_expr(stmt->v.AnnAssign.target, Store) &&
414 (!stmt->v.AnnAssign.value ||
415 validate_expr(stmt->v.AnnAssign.value, Load)) &&
416 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500417 case For_kind:
418 return validate_expr(stmt->v.For.target, Store) &&
419 validate_expr(stmt->v.For.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900420 validate_body(stmt->v.For.body, "For", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500421 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400422 case AsyncFor_kind:
423 return validate_expr(stmt->v.AsyncFor.target, Store) &&
424 validate_expr(stmt->v.AsyncFor.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900425 validate_body(stmt->v.AsyncFor.body, "AsyncFor", 0) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400426 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case While_kind:
428 return validate_expr(stmt->v.While.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900429 validate_body(stmt->v.While.body, "While", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500430 validate_stmts(stmt->v.While.orelse);
431 case If_kind:
432 return validate_expr(stmt->v.If.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900433 validate_body(stmt->v.If.body, "If", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500434 validate_stmts(stmt->v.If.orelse);
435 case With_kind:
436 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
437 return 0;
438 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
439 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
440 if (!validate_expr(item->context_expr, Load) ||
441 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
442 return 0;
443 }
INADA Naokicb41b272017-02-23 00:31:59 +0900444 return validate_body(stmt->v.With.body, "With", 0);
Yury Selivanov75445082015-05-11 22:57:16 -0400445 case AsyncWith_kind:
446 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
447 return 0;
448 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
449 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
450 if (!validate_expr(item->context_expr, Load) ||
451 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
452 return 0;
453 }
INADA Naokicb41b272017-02-23 00:31:59 +0900454 return validate_body(stmt->v.AsyncWith.body, "AsyncWith", 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 case Raise_kind:
456 if (stmt->v.Raise.exc) {
457 return validate_expr(stmt->v.Raise.exc, Load) &&
458 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
459 }
460 if (stmt->v.Raise.cause) {
461 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
462 return 0;
463 }
464 return 1;
465 case Try_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900466 if (!validate_body(stmt->v.Try.body, "Try", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500467 return 0;
468 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
469 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
470 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
471 return 0;
472 }
473 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
474 asdl_seq_LEN(stmt->v.Try.orelse)) {
475 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
476 return 0;
477 }
478 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
479 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
480 if ((handler->v.ExceptHandler.type &&
481 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
INADA Naokicb41b272017-02-23 00:31:59 +0900482 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500483 return 0;
484 }
485 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
486 validate_stmts(stmt->v.Try.finalbody)) &&
487 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
488 validate_stmts(stmt->v.Try.orelse));
489 case Assert_kind:
490 return validate_expr(stmt->v.Assert.test, Load) &&
491 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
492 case Import_kind:
493 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
494 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300495 if (stmt->v.ImportFrom.level < 0) {
496 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500497 return 0;
498 }
499 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
500 case Global_kind:
501 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
502 case Nonlocal_kind:
503 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
504 case Expr_kind:
505 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400506 case AsyncFunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900507 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef",
508 stmt->v.AsyncFunctionDef.docstring != NULL) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400509 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
510 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
511 (!stmt->v.AsyncFunctionDef.returns ||
512 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500513 case Pass_kind:
514 case Break_kind:
515 case Continue_kind:
516 return 1;
517 default:
518 PyErr_SetString(PyExc_SystemError, "unexpected statement");
519 return 0;
520 }
521}
522
523static int
524validate_stmts(asdl_seq *seq)
525{
526 int i;
527 for (i = 0; i < asdl_seq_LEN(seq); i++) {
528 stmt_ty stmt = asdl_seq_GET(seq, i);
529 if (stmt) {
530 if (!validate_stmt(stmt))
531 return 0;
532 }
533 else {
534 PyErr_SetString(PyExc_ValueError,
535 "None disallowed in statement list");
536 return 0;
537 }
538 }
539 return 1;
540}
541
542static int
543validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
544{
545 int i;
546 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
547 expr_ty expr = asdl_seq_GET(exprs, i);
548 if (expr) {
549 if (!validate_expr(expr, ctx))
550 return 0;
551 }
552 else if (!null_ok) {
553 PyErr_SetString(PyExc_ValueError,
554 "None disallowed in expression list");
555 return 0;
556 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100557
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500558 }
559 return 1;
560}
561
562int
563PyAST_Validate(mod_ty mod)
564{
565 int res = 0;
566
567 switch (mod->kind) {
568 case Module_kind:
569 res = validate_stmts(mod->v.Module.body);
570 break;
571 case Interactive_kind:
572 res = validate_stmts(mod->v.Interactive.body);
573 break;
574 case Expression_kind:
575 res = validate_expr(mod->v.Expression.body, Load);
576 break;
577 case Suite_kind:
578 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
579 break;
580 default:
581 PyErr_SetString(PyExc_SystemError, "impossible module node");
582 res = 0;
583 break;
584 }
585 return res;
586}
587
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500588/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500589#include "grammar.h"
590#include "parsetok.h"
591#include "graminit.h"
592
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593/* Data structure used internally */
594struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400595 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200596 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500597 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598};
599
600static asdl_seq *seq_for_testlist(struct compiling *, const node *);
601static expr_ty ast_for_expr(struct compiling *, const node *);
602static stmt_ty ast_for_stmt(struct compiling *, const node *);
INADA Naokicb41b272017-02-23 00:31:59 +0900603static asdl_seq *ast_for_body(struct compiling *c, const node *n,
604 string *docstring);
605static string docstring_from_stmts(asdl_seq *stmts);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000606static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
607 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000608static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000609static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610
Yury Selivanov75445082015-05-11 22:57:16 -0400611static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
612static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614/* Note different signature for ast_for_call */
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200615static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, bool);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000617static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400618static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619
Nick Coghlan650f0d02007-04-15 12:05:43 +0000620#define COMP_GENEXP 0
621#define COMP_LISTCOMP 1
622#define COMP_SETCOMP 2
623
Benjamin Peterson55e00432012-01-16 17:22:31 -0500624static int
625init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000626{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500627 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
628 if (!m)
629 return 0;
630 c->c_normalize = PyObject_GetAttrString(m, "normalize");
631 Py_DECREF(m);
632 if (!c->c_normalize)
633 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500634 return 1;
635}
636
637static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400638new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500639{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400640 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500641 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000642 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500643 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500644 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000645 /* Check whether there are non-ASCII characters in the
646 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500647 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200648 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300649 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500650 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500651 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200652 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500653 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300654 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
655 if (form == NULL) {
656 Py_DECREF(id);
657 return NULL;
658 }
659 PyObject *args[2] = {form, id};
660 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500661 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200662 if (!id2)
663 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300664 if (!PyUnicode_Check(id2)) {
665 PyErr_Format(PyExc_TypeError,
666 "unicodedata.normalize() must return a string, not "
667 "%.200s",
668 Py_TYPE(id2)->tp_name);
669 Py_DECREF(id2);
670 return NULL;
671 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200672 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000673 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000674 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200675 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
676 Py_DECREF(id);
677 return NULL;
678 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000679 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680}
681
Benjamin Peterson55e00432012-01-16 17:22:31 -0500682#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400685ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400687 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688
Victor Stinner14e461d2013-08-26 22:28:21 +0200689 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000691 Py_INCREF(Py_None);
692 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200694 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400695 if (!tmp)
696 return 0;
697 errstr = PyUnicode_FromString(errmsg);
698 if (!errstr) {
699 Py_DECREF(tmp);
700 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000701 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000702 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 Py_DECREF(errstr);
704 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400705 if (value) {
706 PyErr_SetObject(PyExc_SyntaxError, value);
707 Py_DECREF(value);
708 }
709 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710}
711
712/* num_stmts() returns number of contained statements.
713
714 Use this routine to determine how big a sequence is needed for
715 the statements in a parse tree. Its raison d'etre is this bit of
716 grammar:
717
718 stmt: simple_stmt | compound_stmt
719 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
720
721 A simple_stmt can contain multiple small_stmt elements joined
722 by semicolons. If the arg is a simple_stmt, the number of
723 small_stmt elements is returned.
724*/
725
726static int
727num_stmts(const node *n)
728{
729 int i, l;
730 node *ch;
731
732 switch (TYPE(n)) {
733 case single_input:
734 if (TYPE(CHILD(n, 0)) == NEWLINE)
735 return 0;
736 else
737 return num_stmts(CHILD(n, 0));
738 case file_input:
739 l = 0;
740 for (i = 0; i < NCH(n); i++) {
741 ch = CHILD(n, i);
742 if (TYPE(ch) == stmt)
743 l += num_stmts(ch);
744 }
745 return l;
746 case stmt:
747 return num_stmts(CHILD(n, 0));
748 case compound_stmt:
749 return 1;
750 case simple_stmt:
751 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
752 case suite:
753 if (NCH(n) == 1)
754 return num_stmts(CHILD(n, 0));
755 else {
756 l = 0;
757 for (i = 2; i < (NCH(n) - 1); i++)
758 l += num_stmts(CHILD(n, i));
759 return l;
760 }
761 default: {
762 char buf[128];
763
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000764 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 TYPE(n), NCH(n));
766 Py_FatalError(buf);
767 }
768 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700769 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770}
771
772/* Transform the CST rooted at node * to the appropriate AST
773*/
774
775mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200776PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
777 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000779 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 asdl_seq *stmts = NULL;
781 stmt_ty s;
782 node *ch;
783 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500784 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400786 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200787 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400788 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800789 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800790
791 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Jeremy Hyltona8293132006-02-28 17:58:27 +0000794 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 switch (TYPE(n)) {
796 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200797 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500799 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 for (i = 0; i < NCH(n) - 1; i++) {
801 ch = CHILD(n, i);
802 if (TYPE(ch) == NEWLINE)
803 continue;
804 REQ(ch, stmt);
805 num = num_stmts(ch);
806 if (num == 1) {
807 s = ast_for_stmt(&c, ch);
808 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000810 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 }
812 else {
813 ch = CHILD(ch, 0);
814 REQ(ch, simple_stmt);
815 for (j = 0; j < num; j++) {
816 s = ast_for_stmt(&c, CHILD(ch, j * 2));
817 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500818 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000819 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 }
822 }
INADA Naokicb41b272017-02-23 00:31:59 +0900823 res = Module(stmts, docstring_from_stmts(stmts), arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500824 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 case eval_input: {
826 expr_ty testlist_ast;
827
Nick Coghlan650f0d02007-04-15 12:05:43 +0000828 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000829 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500831 goto out;
832 res = Expression(testlist_ast, arena);
833 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 }
835 case single_input:
836 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200837 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500839 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
841 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000842 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 goto out;
844 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 }
846 else {
847 n = CHILD(n, 0);
848 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200849 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500851 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000853 s = ast_for_stmt(&c, n);
854 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500855 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 asdl_seq_SET(stmts, 0, s);
857 }
858 else {
859 /* Only a simple_stmt can contain multiple statements. */
860 REQ(n, simple_stmt);
861 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 if (TYPE(CHILD(n, i)) == NEWLINE)
863 break;
864 s = ast_for_stmt(&c, CHILD(n, i));
865 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500866 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 asdl_seq_SET(stmts, i / 2, s);
868 }
869 }
870
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500873 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000875 PyErr_Format(PyExc_SystemError,
876 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500877 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500879 out:
880 if (c.c_normalize) {
881 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500882 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500883 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884}
885
Victor Stinner14e461d2013-08-26 22:28:21 +0200886mod_ty
887PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
888 PyArena *arena)
889{
890 mod_ty mod;
891 PyObject *filename;
892 filename = PyUnicode_DecodeFSDefault(filename_str);
893 if (filename == NULL)
894 return NULL;
895 mod = PyAST_FromNodeObject(n, flags, filename, arena);
896 Py_DECREF(filename);
897 return mod;
898
899}
900
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
902*/
903
904static operator_ty
905get_operator(const node *n)
906{
907 switch (TYPE(n)) {
908 case VBAR:
909 return BitOr;
910 case CIRCUMFLEX:
911 return BitXor;
912 case AMPER:
913 return BitAnd;
914 case LEFTSHIFT:
915 return LShift;
916 case RIGHTSHIFT:
917 return RShift;
918 case PLUS:
919 return Add;
920 case MINUS:
921 return Sub;
922 case STAR:
923 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400924 case AT:
925 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 case SLASH:
927 return Div;
928 case DOUBLESLASH:
929 return FloorDiv;
930 case PERCENT:
931 return Mod;
932 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 }
935}
936
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200937static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000938 "None",
939 "True",
940 "False",
941 NULL,
942};
943
944static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400945forbidden_name(struct compiling *c, identifier name, const node *n,
946 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000947{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000948 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200949 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400950 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000951 return 1;
952 }
953 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200954 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000955 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200956 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400957 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000958 return 1;
959 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000960 }
961 }
962 return 0;
963}
964
Jeremy Hyltona8293132006-02-28 17:58:27 +0000965/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966
967 Only sets context for expr kinds that "can appear in assignment context"
968 (according to ../Parser/Python.asdl). For other expr kinds, it sets
969 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970*/
971
972static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000973set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974{
975 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000976 /* If a particular expression type can't be used for assign / delete,
977 set expr_name to its name and an error message will be generated.
978 */
979 const char* expr_name = NULL;
980
981 /* The ast defines augmented store and load contexts, but the
982 implementation here doesn't actually use them. The code may be
983 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000984 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000985 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000986 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000987 */
988 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989
990 switch (e->kind) {
991 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000992 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400993 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000994 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000995 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000997 e->v.Subscript.ctx = ctx;
998 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000999 case Starred_kind:
1000 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001001 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001002 return 0;
1003 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001005 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001006 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001007 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001008 }
1009 e->v.Name.ctx = ctx;
1010 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001012 e->v.List.ctx = ctx;
1013 s = e->v.List.elts;
1014 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001016 e->v.Tuple.ctx = ctx;
1017 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001018 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001019 case Lambda_kind:
1020 expr_name = "lambda";
1021 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001023 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001024 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001025 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001027 case UnaryOp_kind:
1028 expr_name = "operator";
1029 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001031 expr_name = "generator expression";
1032 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001033 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001034 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001035 expr_name = "yield expression";
1036 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001037 case Await_kind:
1038 expr_name = "await expression";
1039 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001040 case ListComp_kind:
1041 expr_name = "list comprehension";
1042 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001043 case SetComp_kind:
1044 expr_name = "set comprehension";
1045 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001046 case DictComp_kind:
1047 expr_name = "dict comprehension";
1048 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001049 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001050 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051 case Num_kind:
1052 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001053 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001054 case JoinedStr_kind:
1055 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001056 expr_name = "literal";
1057 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001058 case NameConstant_kind:
1059 expr_name = "keyword";
1060 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001061 case Ellipsis_kind:
1062 expr_name = "Ellipsis";
1063 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001064 case Compare_kind:
1065 expr_name = "comparison";
1066 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 case IfExp_kind:
1068 expr_name = "conditional expression";
1069 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001070 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 PyErr_Format(PyExc_SystemError,
1072 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001073 e->kind, e->lineno);
1074 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001076 /* Check for error string set by switch */
1077 if (expr_name) {
1078 char buf[300];
1079 PyOS_snprintf(buf, sizeof(buf),
1080 "can't %s %s",
1081 ctx == Store ? "assign to" : "delete",
1082 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001083 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001084 }
1085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 */
1089 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001090 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091
Thomas Wouters89f507f2006-12-13 04:49:30 +00001092 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001093 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001094 return 0;
1095 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 }
1097 return 1;
1098}
1099
1100static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001101ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102{
1103 REQ(n, augassign);
1104 n = CHILD(n, 0);
1105 switch (STR(n)[0]) {
1106 case '+':
1107 return Add;
1108 case '-':
1109 return Sub;
1110 case '/':
1111 if (STR(n)[1] == '/')
1112 return FloorDiv;
1113 else
1114 return Div;
1115 case '%':
1116 return Mod;
1117 case '<':
1118 return LShift;
1119 case '>':
1120 return RShift;
1121 case '&':
1122 return BitAnd;
1123 case '^':
1124 return BitXor;
1125 case '|':
1126 return BitOr;
1127 case '*':
1128 if (STR(n)[1] == '*')
1129 return Pow;
1130 else
1131 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001132 case '@':
1133 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001135 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001136 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 }
1138}
1139
1140static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001141ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001143 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 |'is' 'not'
1145 */
1146 REQ(n, comp_op);
1147 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001148 n = CHILD(n, 0);
1149 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 case LESS:
1151 return Lt;
1152 case GREATER:
1153 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001154 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 return Eq;
1156 case LESSEQUAL:
1157 return LtE;
1158 case GREATEREQUAL:
1159 return GtE;
1160 case NOTEQUAL:
1161 return NotEq;
1162 case NAME:
1163 if (strcmp(STR(n), "in") == 0)
1164 return In;
1165 if (strcmp(STR(n), "is") == 0)
1166 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001167 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001169 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001171 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 }
1174 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001175 /* handle "not in" and "is not" */
1176 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 case NAME:
1178 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1179 return NotIn;
1180 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1181 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001182 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001184 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001186 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001187 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 }
Neal Norwitz79792652005-11-14 04:25:03 +00001189 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001191 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192}
1193
1194static asdl_seq *
1195seq_for_testlist(struct compiling *c, const node *n)
1196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001198 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1199 */
Armin Rigo31441302005-10-21 12:57:31 +00001200 asdl_seq *seq;
1201 expr_ty expression;
1202 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001203 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001205 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 if (!seq)
1207 return NULL;
1208
1209 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001211 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212
Benjamin Peterson4905e802009-09-27 02:43:28 +00001213 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001214 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216
1217 assert(i / 2 < seq->size);
1218 asdl_seq_SET(seq, i / 2, expression);
1219 }
1220 return seq;
1221}
1222
Neal Norwitzc1505362006-12-28 06:47:50 +00001223static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001224ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001225{
1226 identifier name;
1227 expr_ty annotation = NULL;
1228 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001229 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001230
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001231 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001232 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001233 name = NEW_IDENTIFIER(ch);
1234 if (!name)
1235 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001236 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001237 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001238
1239 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1240 annotation = ast_for_expr(c, CHILD(n, 2));
1241 if (!annotation)
1242 return NULL;
1243 }
1244
Victor Stinnerc106c682015-11-06 17:01:48 +01001245 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001246 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001247 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001248 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249}
1250
Guido van Rossum4f72a782006-10-27 23:31:49 +00001251/* returns -1 if failed to handle keyword only arguments
1252 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001253 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001254 ^^^
1255 start pointing here
1256 */
1257static int
1258handle_keywordonly_args(struct compiling *c, const node *n, int start,
1259 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1260{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001261 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001262 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001263 expr_ty expression, annotation;
1264 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001265 int i = start;
1266 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001267
1268 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001269 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001270 return -1;
1271 }
1272 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001273 while (i < NCH(n)) {
1274 ch = CHILD(n, i);
1275 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001276 case vfpdef:
1277 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001278 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001279 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001280 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001281 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 asdl_seq_SET(kwdefaults, j, expression);
1283 i += 2; /* '=' and test */
1284 }
1285 else { /* setting NULL if no default value exists */
1286 asdl_seq_SET(kwdefaults, j, NULL);
1287 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001288 if (NCH(ch) == 3) {
1289 /* ch is NAME ':' test */
1290 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001291 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001292 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 }
1294 else {
1295 annotation = NULL;
1296 }
1297 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001298 argname = NEW_IDENTIFIER(ch);
1299 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001300 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001301 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001302 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001303 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1304 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001305 if (!arg)
1306 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001307 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001308 i += 2; /* the name and the comma */
1309 break;
1310 case DOUBLESTAR:
1311 return i;
1312 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001313 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001314 goto error;
1315 }
1316 }
1317 return i;
1318 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321
Jeremy Hyltona8293132006-02-28 17:58:27 +00001322/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323
1324static arguments_ty
1325ast_for_arguments(struct compiling *c, const node *n)
1326{
Neal Norwitzc1505362006-12-28 06:47:50 +00001327 /* This function handles both typedargslist (function definition)
1328 and varargslist (lambda definition).
1329
1330 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001331 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1332 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1333 | '**' tfpdef [',']]]
1334 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1335 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001336 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001337 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1338 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1339 | '**' vfpdef [',']]]
1340 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1341 | '**' vfpdef [',']
1342 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001343 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001344
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1347 int nposdefaults = 0, found_default = 0;
1348 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001349 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 node *ch;
1352
1353 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001354 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001355 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001356 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001358 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359
Jeremy Hyltone921e022008-07-17 16:37:17 +00001360 /* First count the number of positional args & defaults. The
1361 variable i is the loop index for this for loop and the next.
1362 The next loop picks up where the first leaves off.
1363 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 ch = CHILD(n, i);
1366 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001367 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001368 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001369 if (i < NCH(n) && /* skip argument following star */
1370 (TYPE(CHILD(n, i)) == tfpdef ||
1371 TYPE(CHILD(n, i)) == vfpdef)) {
1372 i++;
1373 }
1374 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001376 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001377 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 defaults for keyword only args */
1382 for ( ; i < NCH(n); ++i) {
1383 ch = CHILD(n, i);
1384 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001385 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001386 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001387 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001388 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001389 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001390 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001391 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001393 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001395 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001397 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001399 since we set NULL as default for keyword only argument w/o default
1400 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001401 kwdefaults = (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 (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001404 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001406 /* tfpdef: NAME [':' test]
1407 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 */
1409 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001410 j = 0; /* index for defaults */
1411 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 ch = CHILD(n, i);
1414 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001415 case tfpdef:
1416 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1418 anything other than EQUAL or a comma? */
1419 /* XXX Should NCH(n) check be made a separate check? */
1420 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001421 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1422 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001423 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 assert(posdefaults != NULL);
1425 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001427 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001429 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001430 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001431 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001432 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001433 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001434 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001435 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001436 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001437 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 i += 2; /* the name and the comma */
1439 break;
1440 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001441 if (i+1 >= NCH(n) ||
1442 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001443 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001444 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001445 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001446 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001447 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001448 if (TYPE(ch) == COMMA) {
1449 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001450 i += 2; /* now follows keyword only arguments */
1451 res = handle_keywordonly_args(c, n, i,
1452 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001453 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001454 i = res; /* res has new position to process */
1455 }
1456 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001457 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001458 if (!vararg)
1459 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001460
Guido van Rossum4f72a782006-10-27 23:31:49 +00001461 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001462 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1463 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001464 int res = 0;
1465 res = handle_keywordonly_args(c, n, i,
1466 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001467 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001468 i = res; /* res has new position to process */
1469 }
1470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 break;
1472 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001473 ch = CHILD(n, i+1); /* tfpdef */
1474 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001475 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001476 if (!kwarg)
1477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 i += 3;
1479 break;
1480 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001481 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 "unexpected node in varargslist: %d @ %d",
1483 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001484 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001485 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001487 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
1490static expr_ty
1491ast_for_dotted_name(struct compiling *c, const node *n)
1492{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001493 expr_ty e;
1494 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001495 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 int i;
1497
1498 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001499
1500 lineno = LINENO(n);
1501 col_offset = n->n_col_offset;
1502
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 id = NEW_IDENTIFIER(CHILD(n, 0));
1504 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001505 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001506 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509
1510 for (i = 2; i < NCH(n); i+=2) {
1511 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001512 if (!id)
1513 return NULL;
1514 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1515 if (!e)
1516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 }
1518
1519 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520}
1521
1522static expr_ty
1523ast_for_decorator(struct compiling *c, const node *n)
1524{
1525 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1526 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001527 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001530 REQ(CHILD(n, 0), AT);
1531 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1534 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001535 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001538 d = name_expr;
1539 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 }
1541 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001542 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001543 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001544 if (!d)
1545 return NULL;
1546 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 }
1548 else {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02001549 d = ast_for_call(c, CHILD(n, 3), name_expr, true);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001550 if (!d)
1551 return NULL;
1552 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 }
1554
1555 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556}
1557
1558static asdl_seq*
1559ast_for_decorators(struct compiling *c, const node *n)
1560{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001561 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001562 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001566 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 if (!decorator_seq)
1568 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001571 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001572 if (!d)
1573 return NULL;
1574 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 }
1576 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577}
1578
1579static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001580ast_for_funcdef_impl(struct compiling *c, const node *n,
1581 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001583 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001584 identifier name;
1585 arguments_ty args;
1586 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001587 expr_ty returns = NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09001588 string docstring;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001589 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590
1591 REQ(n, funcdef);
1592
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 name = NEW_IDENTIFIER(CHILD(n, name_i));
1594 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001595 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001596 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001597 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1599 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001600 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001601 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1602 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1603 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001604 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001605 name_i += 2;
1606 }
INADA Naokicb41b272017-02-23 00:31:59 +09001607 body = ast_for_body(c, CHILD(n, name_i + 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001609 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610
Yury Selivanov75445082015-05-11 22:57:16 -04001611 if (is_async)
1612 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001613 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001614 n->n_col_offset, c->c_arena);
1615 else
1616 return FunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001617 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001618 n->n_col_offset, c->c_arena);
1619}
1620
1621static stmt_ty
1622ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1623{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001624 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001625 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001626 REQ(CHILD(n, 0), NAME);
1627 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001628 REQ(CHILD(n, 1), funcdef);
1629
1630 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1631 1 /* is_async */);
1632}
1633
1634static stmt_ty
1635ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1636{
1637 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1638 return ast_for_funcdef_impl(c, n, decorator_seq,
1639 0 /* is_async */);
1640}
1641
1642
1643static stmt_ty
1644ast_for_async_stmt(struct compiling *c, const node *n)
1645{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001646 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001647 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001648 REQ(CHILD(n, 0), NAME);
1649 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001650
1651 switch (TYPE(CHILD(n, 1))) {
1652 case funcdef:
1653 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1654 1 /* is_async */);
1655 case with_stmt:
1656 return ast_for_with_stmt(c, CHILD(n, 1),
1657 1 /* is_async */);
1658
1659 case for_stmt:
1660 return ast_for_for_stmt(c, CHILD(n, 1),
1661 1 /* is_async */);
1662
1663 default:
1664 PyErr_Format(PyExc_SystemError,
1665 "invalid async stament: %s",
1666 STR(CHILD(n, 1)));
1667 return NULL;
1668 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669}
1670
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001671static stmt_ty
1672ast_for_decorated(struct compiling *c, const node *n)
1673{
Yury Selivanov75445082015-05-11 22:57:16 -04001674 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001675 stmt_ty thing = NULL;
1676 asdl_seq *decorator_seq = NULL;
1677
1678 REQ(n, decorated);
1679
1680 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1681 if (!decorator_seq)
1682 return NULL;
1683
1684 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001685 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001687
1688 if (TYPE(CHILD(n, 1)) == funcdef) {
1689 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1690 } else if (TYPE(CHILD(n, 1)) == classdef) {
1691 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001692 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1693 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001694 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001695 /* we count the decorators in when talking about the class' or
1696 * function's line number */
1697 if (thing) {
1698 thing->lineno = LINENO(n);
1699 thing->col_offset = n->n_col_offset;
1700 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001701 return thing;
1702}
1703
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704static expr_ty
1705ast_for_lambdef(struct compiling *c, const node *n)
1706{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001707 /* lambdef: 'lambda' [varargslist] ':' test
1708 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 arguments_ty args;
1710 expr_ty expression;
1711
1712 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001713 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 if (!args)
1715 return NULL;
1716 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001717 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 }
1720 else {
1721 args = ast_for_arguments(c, CHILD(n, 1));
1722 if (!args)
1723 return NULL;
1724 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001725 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727 }
1728
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001729 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730}
1731
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001732static expr_ty
1733ast_for_ifexpr(struct compiling *c, const node *n)
1734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001736 expr_ty expression, body, orelse;
1737
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001738 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001739 body = ast_for_expr(c, CHILD(n, 0));
1740 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001741 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001742 expression = ast_for_expr(c, CHILD(n, 2));
1743 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001744 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001745 orelse = ast_for_expr(c, CHILD(n, 4));
1746 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001747 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001748 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1749 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001750}
1751
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001753 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754
Nick Coghlan650f0d02007-04-15 12:05:43 +00001755 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756*/
1757
1758static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001759count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001761 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762
Guido van Rossumd8faa362007-04-27 19:54:29 +00001763 count_comp_for:
1764 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001765 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001766 if (NCH(n) == 2) {
1767 REQ(CHILD(n, 0), NAME);
1768 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1769 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001770 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001771 else if (NCH(n) == 1) {
1772 n = CHILD(n, 0);
1773 }
1774 else {
1775 goto error;
1776 }
1777 if (NCH(n) == (5)) {
1778 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001779 }
1780 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001781 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001782 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001783 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001784 REQ(n, comp_iter);
1785 n = CHILD(n, 0);
1786 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001787 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001788 else if (TYPE(n) == comp_if) {
1789 if (NCH(n) == 3) {
1790 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001791 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001792 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001793 else
1794 return n_fors;
1795 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001796
Jelle Zijlstraac317702017-10-05 20:24:46 -07001797 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001798 /* Should never be reached */
1799 PyErr_SetString(PyExc_SystemError,
1800 "logic error in count_comp_fors");
1801 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802}
1803
Nick Coghlan650f0d02007-04-15 12:05:43 +00001804/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805
Nick Coghlan650f0d02007-04-15 12:05:43 +00001806 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807*/
1808
1809static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001810count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001812 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813
Guido van Rossumd8faa362007-04-27 19:54:29 +00001814 while (1) {
1815 REQ(n, comp_iter);
1816 if (TYPE(CHILD(n, 0)) == comp_for)
1817 return n_ifs;
1818 n = CHILD(n, 0);
1819 REQ(n, comp_if);
1820 n_ifs++;
1821 if (NCH(n) == 2)
1822 return n_ifs;
1823 n = CHILD(n, 2);
1824 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825}
1826
Guido van Rossum992d4a32007-07-11 13:09:30 +00001827static asdl_seq *
1828ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001831 asdl_seq *comps;
1832
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001833 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 if (n_fors == -1)
1835 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001836
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001837 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001838 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001840
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001842 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001844 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001845 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001846 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001847 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848
Guido van Rossum992d4a32007-07-11 13:09:30 +00001849 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850
Jelle Zijlstraac317702017-10-05 20:24:46 -07001851 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001852 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001853 REQ(CHILD(n, 0), NAME);
1854 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1855 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001856 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001857 else {
1858 sync_n = CHILD(n, 0);
1859 }
1860 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001861
Jelle Zijlstraac317702017-10-05 20:24:46 -07001862 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001863 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001864 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001866 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001867 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869
Thomas Wouters89f507f2006-12-13 04:49:30 +00001870 /* Check the # of children rather than the length of t, since
1871 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001872 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001873 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001874 comp = comprehension(first, expression, NULL,
1875 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001877 comp = comprehension(Tuple(t, Store, first->lineno,
1878 first->col_offset, c->c_arena),
1879 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001880 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001882
Jelle Zijlstraac317702017-10-05 20:24:46 -07001883 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 int j, n_ifs;
1885 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886
Jelle Zijlstraac317702017-10-05 20:24:46 -07001887 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001888 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001889 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001892 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001893 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001897 REQ(n, comp_iter);
1898 n = CHILD(n, 0);
1899 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001902 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001903 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001904 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001905 if (NCH(n) == 3)
1906 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001908 /* on exit, must guarantee that n is a comp_for */
1909 if (TYPE(n) == comp_iter)
1910 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001911 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001913 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001915 return comps;
1916}
1917
1918static expr_ty
1919ast_for_itercomp(struct compiling *c, const node *n, int type)
1920{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001921 /* testlist_comp: (test|star_expr)
1922 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001923 expr_ty elt;
1924 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001925 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926
Guido van Rossum992d4a32007-07-11 13:09:30 +00001927 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001929 ch = CHILD(n, 0);
1930 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001931 if (!elt)
1932 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001933 if (elt->kind == Starred_kind) {
1934 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1935 return NULL;
1936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937
Guido van Rossum992d4a32007-07-11 13:09:30 +00001938 comps = ast_for_comprehension(c, CHILD(n, 1));
1939 if (!comps)
1940 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001941
1942 if (type == COMP_GENEXP)
1943 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1944 else if (type == COMP_LISTCOMP)
1945 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1946 else if (type == COMP_SETCOMP)
1947 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1948 else
1949 /* Should never happen */
1950 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951}
1952
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001953/* Fills in the key, value pair corresponding to the dict element. In case
1954 * of an unpacking, key is NULL. *i is advanced by the number of ast
1955 * elements. Iff successful, nonzero is returned.
1956 */
1957static int
1958ast_for_dictelement(struct compiling *c, const node *n, int *i,
1959 expr_ty *key, expr_ty *value)
1960{
1961 expr_ty expression;
1962 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1963 assert(NCH(n) - *i >= 2);
1964
1965 expression = ast_for_expr(c, CHILD(n, *i + 1));
1966 if (!expression)
1967 return 0;
1968 *key = NULL;
1969 *value = expression;
1970
1971 *i += 2;
1972 }
1973 else {
1974 assert(NCH(n) - *i >= 3);
1975
1976 expression = ast_for_expr(c, CHILD(n, *i));
1977 if (!expression)
1978 return 0;
1979 *key = expression;
1980
1981 REQ(CHILD(n, *i + 1), COLON);
1982
1983 expression = ast_for_expr(c, CHILD(n, *i + 2));
1984 if (!expression)
1985 return 0;
1986 *value = expression;
1987
1988 *i += 3;
1989 }
1990 return 1;
1991}
1992
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001994ast_for_dictcomp(struct compiling *c, const node *n)
1995{
1996 expr_ty key, value;
1997 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001998 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002000 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002001 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002002 assert(key);
2003 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002005 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002006 if (!comps)
2007 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008
Guido van Rossum992d4a32007-07-11 13:09:30 +00002009 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2010}
2011
2012static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002013ast_for_dictdisplay(struct compiling *c, const node *n)
2014{
2015 int i;
2016 int j;
2017 int size;
2018 asdl_seq *keys, *values;
2019
2020 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2021 keys = _Py_asdl_seq_new(size, c->c_arena);
2022 if (!keys)
2023 return NULL;
2024
2025 values = _Py_asdl_seq_new(size, c->c_arena);
2026 if (!values)
2027 return NULL;
2028
2029 j = 0;
2030 for (i = 0; i < NCH(n); i++) {
2031 expr_ty key, value;
2032
2033 if (!ast_for_dictelement(c, n, &i, &key, &value))
2034 return NULL;
2035 asdl_seq_SET(keys, j, key);
2036 asdl_seq_SET(values, j, value);
2037
2038 j++;
2039 }
2040 keys->size = j;
2041 values->size = j;
2042 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2043}
2044
2045static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002046ast_for_genexp(struct compiling *c, const node *n)
2047{
2048 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002049 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002050}
2051
2052static expr_ty
2053ast_for_listcomp(struct compiling *c, const node *n)
2054{
2055 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002056 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002057}
2058
2059static expr_ty
2060ast_for_setcomp(struct compiling *c, const node *n)
2061{
2062 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002063 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002064}
2065
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002066static expr_ty
2067ast_for_setdisplay(struct compiling *c, const node *n)
2068{
2069 int i;
2070 int size;
2071 asdl_seq *elts;
2072
2073 assert(TYPE(n) == (dictorsetmaker));
2074 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2075 elts = _Py_asdl_seq_new(size, c->c_arena);
2076 if (!elts)
2077 return NULL;
2078 for (i = 0; i < NCH(n); i += 2) {
2079 expr_ty expression;
2080 expression = ast_for_expr(c, CHILD(n, i));
2081 if (!expression)
2082 return NULL;
2083 asdl_seq_SET(elts, i / 2, expression);
2084 }
2085 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2086}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002087
2088static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089ast_for_atom(struct compiling *c, const node *n)
2090{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002091 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2092 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002093 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 */
2095 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002098 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002099 PyObject *name;
2100 const char *s = STR(ch);
2101 size_t len = strlen(s);
2102 if (len >= 4 && len <= 5) {
2103 if (!strcmp(s, "None"))
2104 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2105 if (!strcmp(s, "True"))
2106 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2107 if (!strcmp(s, "False"))
2108 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2109 }
2110 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002111 if (!name)
2112 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002113 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002114 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2115 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002117 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002118 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002119 const char *errtype = NULL;
2120 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2121 errtype = "unicode error";
2122 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2123 errtype = "value error";
2124 if (errtype) {
2125 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002126 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002127 PyObject *type, *value, *tback, *errstr;
2128 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002129 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002130 if (errstr)
2131 s = PyUnicode_AsUTF8(errstr);
2132 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002133 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002134 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002135 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002136 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002137 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002138 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002139 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002140 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002141 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002142 Py_XDECREF(tback);
2143 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002144 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002145 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002146 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 }
2148 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002149 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002150 if (!pynum)
2151 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002152
Victor Stinner43d81952013-07-17 00:57:58 +02002153 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2154 Py_DECREF(pynum);
2155 return NULL;
2156 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002157 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 }
Georg Brandldde00282007-03-18 19:01:53 +00002159 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002160 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002162 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163
Thomas Wouters89f507f2006-12-13 04:49:30 +00002164 if (TYPE(ch) == RPAR)
2165 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 if (TYPE(ch) == yield_expr)
2168 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002171 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002172 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002173
Nick Coghlan650f0d02007-04-15 12:05:43 +00002174 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002176 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177
Thomas Wouters89f507f2006-12-13 04:49:30 +00002178 if (TYPE(ch) == RSQB)
2179 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180
Nick Coghlan650f0d02007-04-15 12:05:43 +00002181 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002182 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2183 asdl_seq *elts = seq_for_testlist(c, ch);
2184 if (!elts)
2185 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002186
Thomas Wouters89f507f2006-12-13 04:49:30 +00002187 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2188 }
2189 else
2190 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002192 /* dictorsetmaker: ( ((test ':' test | '**' test)
2193 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2194 * ((test | '*' test)
2195 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002196 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002197 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002198 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002199 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002200 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002201 }
2202 else {
2203 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2204 if (NCH(ch) == 1 ||
2205 (NCH(ch) > 1 &&
2206 TYPE(CHILD(ch, 1)) == COMMA)) {
2207 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002208 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002209 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002210 else if (NCH(ch) > 1 &&
2211 TYPE(CHILD(ch, 1)) == comp_for) {
2212 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002213 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002214 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002215 else if (NCH(ch) > 3 - is_dict &&
2216 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2217 /* It's a dictionary comprehension. */
2218 if (is_dict) {
2219 ast_error(c, n, "dict unpacking cannot be used in "
2220 "dict comprehension");
2221 return NULL;
2222 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002223 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002224 }
2225 else {
2226 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002227 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002228 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002229 if (res) {
2230 res->lineno = LINENO(n);
2231 res->col_offset = n->n_col_offset;
2232 }
2233 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002237 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2238 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 }
2240}
2241
2242static slice_ty
2243ast_for_slice(struct compiling *c, const node *n)
2244{
2245 node *ch;
2246 expr_ty lower = NULL, upper = NULL, step = NULL;
2247
2248 REQ(n, subscript);
2249
2250 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002251 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 sliceop: ':' [test]
2253 */
2254 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 if (NCH(n) == 1 && TYPE(ch) == test) {
2256 /* 'step' variable hold no significance in terms of being used over
2257 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 if (!step)
2260 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261
Thomas Wouters89f507f2006-12-13 04:49:30 +00002262 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 }
2264
2265 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002266 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 if (!lower)
2268 return NULL;
2269 }
2270
2271 /* If there's an upper bound it's in the second or third position. */
2272 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002273 if (NCH(n) > 1) {
2274 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Thomas Wouters89f507f2006-12-13 04:49:30 +00002276 if (TYPE(n2) == test) {
2277 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 if (!upper)
2279 return NULL;
2280 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002281 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002283 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284
Thomas Wouters89f507f2006-12-13 04:49:30 +00002285 if (TYPE(n2) == test) {
2286 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 if (!upper)
2288 return NULL;
2289 }
2290 }
2291
2292 ch = CHILD(n, NCH(n) - 1);
2293 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002294 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002295 ch = CHILD(ch, 1);
2296 if (TYPE(ch) == test) {
2297 step = ast_for_expr(c, ch);
2298 if (!step)
2299 return NULL;
2300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 }
2302 }
2303
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002304 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305}
2306
2307static expr_ty
2308ast_for_binop(struct compiling *c, const node *n)
2309{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002310 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002312 BinOp(BinOp(A, op, B), op, C).
2313 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314
Guido van Rossumd8faa362007-04-27 19:54:29 +00002315 int i, nops;
2316 expr_ty expr1, expr2, result;
2317 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318
Guido van Rossumd8faa362007-04-27 19:54:29 +00002319 expr1 = ast_for_expr(c, CHILD(n, 0));
2320 if (!expr1)
2321 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322
Guido van Rossumd8faa362007-04-27 19:54:29 +00002323 expr2 = ast_for_expr(c, CHILD(n, 2));
2324 if (!expr2)
2325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326
Guido van Rossumd8faa362007-04-27 19:54:29 +00002327 newoperator = get_operator(CHILD(n, 1));
2328 if (!newoperator)
2329 return NULL;
2330
2331 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2332 c->c_arena);
2333 if (!result)
2334 return NULL;
2335
2336 nops = (NCH(n) - 1) / 2;
2337 for (i = 1; i < nops; i++) {
2338 expr_ty tmp_result, tmp;
2339 const node* next_oper = CHILD(n, i * 2 + 1);
2340
2341 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002342 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 return NULL;
2344
Guido van Rossumd8faa362007-04-27 19:54:29 +00002345 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2346 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 return NULL;
2348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002350 LINENO(next_oper), next_oper->n_col_offset,
2351 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002353 return NULL;
2354 result = tmp_result;
2355 }
2356 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357}
2358
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002359static expr_ty
2360ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002363 subscriptlist: subscript (',' subscript)* [',']
2364 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2365 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002366 REQ(n, trailer);
2367 if (TYPE(CHILD(n, 0)) == LPAR) {
2368 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002369 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002370 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002371 else
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002372 return ast_for_call(c, CHILD(n, 1), left_expr, true);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002373 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002374 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002375 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2376 if (!attr_id)
2377 return NULL;
2378 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002379 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002380 }
2381 else {
2382 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002383 REQ(CHILD(n, 2), RSQB);
2384 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002385 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002386 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2387 if (!slc)
2388 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002389 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2390 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002391 }
2392 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002394 by treating the sequence as a tuple literal if there are
2395 no slice features.
2396 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002397 int j;
2398 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002399 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002400 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002401 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002402 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002403 if (!slices)
2404 return NULL;
2405 for (j = 0; j < NCH(n); j += 2) {
2406 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002407 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002408 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002409 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002410 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002411 asdl_seq_SET(slices, j / 2, slc);
2412 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002413 if (!simple) {
2414 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002415 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002416 }
2417 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002418 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002419 if (!elts)
2420 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002421 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2422 slc = (slice_ty)asdl_seq_GET(slices, j);
2423 assert(slc->kind == Index_kind && slc->v.Index.value);
2424 asdl_seq_SET(elts, j, slc->v.Index.value);
2425 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002426 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002427 if (!e)
2428 return NULL;
2429 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002430 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002431 }
2432 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002433}
2434
2435static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002436ast_for_factor(struct compiling *c, const node *n)
2437{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002438 expr_ty expression;
2439
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002440 expression = ast_for_expr(c, CHILD(n, 1));
2441 if (!expression)
2442 return NULL;
2443
2444 switch (TYPE(CHILD(n, 0))) {
2445 case PLUS:
2446 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2447 c->c_arena);
2448 case MINUS:
2449 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2450 c->c_arena);
2451 case TILDE:
2452 return UnaryOp(Invert, expression, LINENO(n),
2453 n->n_col_offset, c->c_arena);
2454 }
2455 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2456 TYPE(CHILD(n, 0)));
2457 return NULL;
2458}
2459
2460static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002461ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002462{
Yury Selivanov75445082015-05-11 22:57:16 -04002463 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002464 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002465
2466 REQ(n, atom_expr);
2467 nch = NCH(n);
2468
Jelle Zijlstraac317702017-10-05 20:24:46 -07002469 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002470 start = 1;
2471 assert(nch > 1);
2472 }
2473
2474 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002475 if (!e)
2476 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002477 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002478 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002479 if (start && nch == 2) {
2480 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2481 }
2482
2483 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002484 node *ch = CHILD(n, i);
2485 if (TYPE(ch) != trailer)
2486 break;
2487 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002488 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002489 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002490 tmp->lineno = e->lineno;
2491 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002492 e = tmp;
2493 }
Yury Selivanov75445082015-05-11 22:57:16 -04002494
2495 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002496 /* there was an 'await' */
Yury Selivanov75445082015-05-11 22:57:16 -04002497 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2498 }
2499 else {
2500 return e;
2501 }
2502}
2503
2504static expr_ty
2505ast_for_power(struct compiling *c, const node *n)
2506{
2507 /* power: atom trailer* ('**' factor)*
2508 */
2509 expr_ty e;
2510 REQ(n, power);
2511 e = ast_for_atom_expr(c, CHILD(n, 0));
2512 if (!e)
2513 return NULL;
2514 if (NCH(n) == 1)
2515 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002516 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2517 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002518 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002519 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002520 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002521 }
2522 return e;
2523}
2524
Guido van Rossum0368b722007-05-11 16:50:42 +00002525static expr_ty
2526ast_for_starred(struct compiling *c, const node *n)
2527{
2528 expr_ty tmp;
2529 REQ(n, star_expr);
2530
2531 tmp = ast_for_expr(c, CHILD(n, 1));
2532 if (!tmp)
2533 return NULL;
2534
2535 /* The Load context is changed later. */
2536 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2537}
2538
2539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540/* Do not name a variable 'expr'! Will cause a compile error.
2541*/
2542
2543static expr_ty
2544ast_for_expr(struct compiling *c, const node *n)
2545{
2546 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002547 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002548 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 and_test: not_test ('and' not_test)*
2551 not_test: 'not' not_test | comparison
2552 comparison: expr (comp_op expr)*
2553 expr: xor_expr ('|' xor_expr)*
2554 xor_expr: and_expr ('^' and_expr)*
2555 and_expr: shift_expr ('&' shift_expr)*
2556 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2557 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002558 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002560 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002561 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002562 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 */
2564
2565 asdl_seq *seq;
2566 int i;
2567
2568 loop:
2569 switch (TYPE(n)) {
2570 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002571 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002572 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002573 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002575 else if (NCH(n) > 1)
2576 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002577 /* Fallthrough */
2578 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579 case and_test:
2580 if (NCH(n) == 1) {
2581 n = CHILD(n, 0);
2582 goto loop;
2583 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002584 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 if (!seq)
2586 return NULL;
2587 for (i = 0; i < NCH(n); i += 2) {
2588 expr_ty e = ast_for_expr(c, CHILD(n, i));
2589 if (!e)
2590 return NULL;
2591 asdl_seq_SET(seq, i / 2, e);
2592 }
2593 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002594 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2595 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002596 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002597 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 case not_test:
2599 if (NCH(n) == 1) {
2600 n = CHILD(n, 0);
2601 goto loop;
2602 }
2603 else {
2604 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2605 if (!expression)
2606 return NULL;
2607
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002608 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2609 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 }
2611 case comparison:
2612 if (NCH(n) == 1) {
2613 n = CHILD(n, 0);
2614 goto loop;
2615 }
2616 else {
2617 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002618 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002619 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002620 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 if (!ops)
2622 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002623 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 return NULL;
2626 }
2627 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002628 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002630 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002631 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002633 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634
2635 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002636 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002638 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002640 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 asdl_seq_SET(cmps, i / 2, expression);
2642 }
2643 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002644 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002646 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002648 return Compare(expression, ops, cmps, LINENO(n),
2649 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 }
2651 break;
2652
Guido van Rossum0368b722007-05-11 16:50:42 +00002653 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 /* The next five cases all handle BinOps. The main body of code
2656 is the same in each case, but the switch turned inside out to
2657 reuse the code for each type of operator.
2658 */
2659 case expr:
2660 case xor_expr:
2661 case and_expr:
2662 case shift_expr:
2663 case arith_expr:
2664 case term:
2665 if (NCH(n) == 1) {
2666 n = CHILD(n, 0);
2667 goto loop;
2668 }
2669 return ast_for_binop(c, n);
2670 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002671 node *an = NULL;
2672 node *en = NULL;
2673 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002674 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002675 if (NCH(n) > 1)
2676 an = CHILD(n, 1); /* yield_arg */
2677 if (an) {
2678 en = CHILD(an, NCH(an) - 1);
2679 if (NCH(an) == 2) {
2680 is_from = 1;
2681 exp = ast_for_expr(c, en);
2682 }
2683 else
2684 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002685 if (!exp)
2686 return NULL;
2687 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002688 if (is_from)
2689 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2690 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002691 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002692 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 if (NCH(n) == 1) {
2694 n = CHILD(n, 0);
2695 goto loop;
2696 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002697 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002698 case power:
2699 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002701 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 return NULL;
2703 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002704 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 return NULL;
2706}
2707
2708static expr_ty
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002709ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710{
2711 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002712 arglist: argument (',' argument)* [',']
2713 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 */
2715
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002716 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002717 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002718 asdl_seq *args;
2719 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720
2721 REQ(n, arglist);
2722
2723 nargs = 0;
2724 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002726 node *ch = CHILD(n, i);
2727 if (TYPE(ch) == argument) {
2728 if (NCH(ch) == 1)
2729 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002730 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2731 nargs++;
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002732 if (!allowgen) {
2733 ast_error(c, ch, "invalid syntax");
2734 return NULL;
2735 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002736 if (NCH(n) > 1) {
2737 ast_error(c, ch, "Generator expression must be parenthesized");
2738 return NULL;
2739 }
2740 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002741 else if (TYPE(CHILD(ch, 0)) == STAR)
2742 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002745 nkeywords++;
2746 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002749 args = _Py_asdl_seq_new(nargs, 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 }
Stefan Krahf432a322017-08-21 13:09:59 +02003154 /* 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
INADA Naokicb41b272017-02-23 00:31:59 +09003531static string
3532docstring_from_stmts(asdl_seq *stmts)
3533{
3534 if (stmts && stmts->size) {
3535 stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0);
3536 /* If first statement is a literal string, it's the doc string. */
3537 if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) {
3538 string doc = s->v.Expr.value->v.Str.s;
3539 /* not very efficient, but simple */
3540 memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1),
3541 (stmts->size - 1) * sizeof(void*));
3542 stmts->size--;
3543 return doc;
3544 }
3545 }
3546 return NULL;
3547}
3548
3549static asdl_seq *
3550ast_for_body(struct compiling *c, const node *n, string *docstring)
3551{
3552 asdl_seq *stmts = ast_for_suite(c, n);
3553 *docstring = docstring_from_stmts(stmts);
3554 return stmts;
3555}
3556
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557static stmt_ty
3558ast_for_if_stmt(struct compiling *c, const node *n)
3559{
3560 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3561 ['else' ':' suite]
3562 */
3563 char *s;
3564
3565 REQ(n, if_stmt);
3566
3567 if (NCH(n) == 4) {
3568 expr_ty expression;
3569 asdl_seq *suite_seq;
3570
3571 expression = ast_for_expr(c, CHILD(n, 1));
3572 if (!expression)
3573 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003575 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577
Guido van Rossumd8faa362007-04-27 19:54:29 +00003578 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3579 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 s = STR(CHILD(n, 4));
3583 /* s[2], the third character in the string, will be
3584 's' for el_s_e, or
3585 'i' for el_i_f
3586 */
3587 if (s[2] == 's') {
3588 expr_ty expression;
3589 asdl_seq *seq1, *seq2;
3590
3591 expression = ast_for_expr(c, CHILD(n, 1));
3592 if (!expression)
3593 return NULL;
3594 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003595 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 return NULL;
3597 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003598 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 return NULL;
3600
Guido van Rossumd8faa362007-04-27 19:54:29 +00003601 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3602 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 }
3604 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003605 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003606 expr_ty expression;
3607 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003608 asdl_seq *orelse = NULL;
3609 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 /* must reference the child n_elif+1 since 'else' token is third,
3611 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003612 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3613 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3614 has_else = 1;
3615 n_elif -= 3;
3616 }
3617 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618
Thomas Wouters89f507f2006-12-13 04:49:30 +00003619 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003620 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003622 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003623 if (!orelse)
3624 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003626 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003628 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3629 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003631 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3632 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 asdl_seq_SET(orelse, 0,
3636 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003637 LINENO(CHILD(n, NCH(n) - 6)),
3638 CHILD(n, NCH(n) - 6)->n_col_offset,
3639 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003640 /* the just-created orelse handled the last elif */
3641 n_elif--;
3642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643
Thomas Wouters89f507f2006-12-13 04:49:30 +00003644 for (i = 0; i < n_elif; i++) {
3645 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003646 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003647 if (!newobj)
3648 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003650 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003653 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655
Thomas Wouters89f507f2006-12-13 04:49:30 +00003656 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003658 LINENO(CHILD(n, off)),
3659 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003660 orelse = newobj;
3661 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003662 expression = ast_for_expr(c, CHILD(n, 1));
3663 if (!expression)
3664 return NULL;
3665 suite_seq = ast_for_suite(c, CHILD(n, 3));
3666 if (!suite_seq)
3667 return NULL;
3668 return If(expression, suite_seq, orelse,
3669 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003671
3672 PyErr_Format(PyExc_SystemError,
3673 "unexpected token in 'if' statement: %s", s);
3674 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675}
3676
3677static stmt_ty
3678ast_for_while_stmt(struct compiling *c, const node *n)
3679{
3680 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3681 REQ(n, while_stmt);
3682
3683 if (NCH(n) == 4) {
3684 expr_ty expression;
3685 asdl_seq *suite_seq;
3686
3687 expression = ast_for_expr(c, CHILD(n, 1));
3688 if (!expression)
3689 return NULL;
3690 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003691 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003692 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003693 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694 }
3695 else if (NCH(n) == 7) {
3696 expr_ty expression;
3697 asdl_seq *seq1, *seq2;
3698
3699 expression = ast_for_expr(c, CHILD(n, 1));
3700 if (!expression)
3701 return NULL;
3702 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003703 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 return NULL;
3705 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003706 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707 return NULL;
3708
Thomas Wouters89f507f2006-12-13 04:49:30 +00003709 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003711
3712 PyErr_Format(PyExc_SystemError,
3713 "wrong number of tokens for 'while' statement: %d",
3714 NCH(n));
3715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716}
3717
3718static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003719ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003721 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003723 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003724 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3726 REQ(n, for_stmt);
3727
3728 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003729 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730 if (!seq)
3731 return NULL;
3732 }
3733
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003734 node_target = CHILD(n, 1);
3735 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003736 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003737 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003738 /* Check the # of children rather than the length of _target, since
3739 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003740 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003741 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003742 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003744 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003746 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003747 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 return NULL;
3749 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003750 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 return NULL;
3752
Yury Selivanov75445082015-05-11 22:57:16 -04003753 if (is_async)
3754 return AsyncFor(target, expression, suite_seq, seq,
3755 LINENO(n), n->n_col_offset,
3756 c->c_arena);
3757 else
3758 return For(target, expression, suite_seq, seq,
3759 LINENO(n), n->n_col_offset,
3760 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761}
3762
3763static excepthandler_ty
3764ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3765{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003766 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 REQ(exc, except_clause);
3768 REQ(body, suite);
3769
3770 if (NCH(exc) == 1) {
3771 asdl_seq *suite_seq = ast_for_suite(c, body);
3772 if (!suite_seq)
3773 return NULL;
3774
Neal Norwitzad74aa82008-03-31 05:14:30 +00003775 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003776 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 }
3778 else if (NCH(exc) == 2) {
3779 expr_ty expression;
3780 asdl_seq *suite_seq;
3781
3782 expression = ast_for_expr(c, CHILD(exc, 1));
3783 if (!expression)
3784 return NULL;
3785 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003786 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787 return NULL;
3788
Neal Norwitzad74aa82008-03-31 05:14:30 +00003789 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003790 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 }
3792 else if (NCH(exc) == 4) {
3793 asdl_seq *suite_seq;
3794 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003795 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003796 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003798 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003799 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003801 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 return NULL;
3803 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003804 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805 return NULL;
3806
Neal Norwitzad74aa82008-03-31 05:14:30 +00003807 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003808 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003810
3811 PyErr_Format(PyExc_SystemError,
3812 "wrong number of children for 'except' clause: %d",
3813 NCH(exc));
3814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815}
3816
3817static stmt_ty
3818ast_for_try_stmt(struct compiling *c, const node *n)
3819{
Neal Norwitzf599f422005-12-17 21:33:47 +00003820 const int nch = NCH(n);
3821 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003822 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003823
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 REQ(n, try_stmt);
3825
Neal Norwitzf599f422005-12-17 21:33:47 +00003826 body = ast_for_suite(c, CHILD(n, 2));
3827 if (body == NULL)
3828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829
Neal Norwitzf599f422005-12-17 21:33:47 +00003830 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3831 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3832 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3833 /* we can assume it's an "else",
3834 because nch >= 9 for try-else-finally and
3835 it would otherwise have a type of except_clause */
3836 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3837 if (orelse == NULL)
3838 return NULL;
3839 n_except--;
3840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841
Neal Norwitzf599f422005-12-17 21:33:47 +00003842 finally = ast_for_suite(c, CHILD(n, nch - 1));
3843 if (finally == NULL)
3844 return NULL;
3845 n_except--;
3846 }
3847 else {
3848 /* we can assume it's an "else",
3849 otherwise it would have a type of except_clause */
3850 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3851 if (orelse == NULL)
3852 return NULL;
3853 n_except--;
3854 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003855 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003856 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003857 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858 return NULL;
3859 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860
Neal Norwitzf599f422005-12-17 21:33:47 +00003861 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003862 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003863 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003864 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003865 if (handlers == NULL)
3866 return NULL;
3867
3868 for (i = 0; i < n_except; i++) {
3869 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3870 CHILD(n, 5 + i * 3));
3871 if (!e)
3872 return NULL;
3873 asdl_seq_SET(handlers, i, e);
3874 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003875 }
3876
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003877 assert(finally != NULL || asdl_seq_LEN(handlers));
3878 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879}
3880
Georg Brandl0c315622009-05-25 21:10:36 +00003881/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003882static withitem_ty
3883ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003884{
3885 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003886
Georg Brandl0c315622009-05-25 21:10:36 +00003887 REQ(n, with_item);
3888 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003889 if (!context_expr)
3890 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003891 if (NCH(n) == 3) {
3892 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003893
3894 if (!optional_vars) {
3895 return NULL;
3896 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003897 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003898 return NULL;
3899 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003900 }
3901
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003902 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003903}
3904
Georg Brandl0c315622009-05-25 21:10:36 +00003905/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3906static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003907ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003908{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003909 int i, n_items;
3910 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003911
3912 REQ(n, with_stmt);
3913
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003914 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003915 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003916 if (!items)
3917 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003918 for (i = 1; i < NCH(n) - 2; i += 2) {
3919 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3920 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003921 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003922 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003923 }
3924
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003925 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3926 if (!body)
3927 return NULL;
3928
Yury Selivanov75445082015-05-11 22:57:16 -04003929 if (is_async)
3930 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3931 else
3932 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003933}
3934
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003936ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003938 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003939 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003940 asdl_seq *s;
INADA Naokicb41b272017-02-23 00:31:59 +09003941 string docstring;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003942 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003943
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 REQ(n, classdef);
3945
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003946 if (NCH(n) == 4) { /* class NAME ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003947 s = ast_for_body(c, CHILD(n, 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003948 if (!s)
3949 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003950 classname = NEW_IDENTIFIER(CHILD(n, 1));
3951 if (!classname)
3952 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003953 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003954 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003955 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3956 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003958
3959 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003960 s = ast_for_body(c, CHILD(n, 5), &docstring);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003961 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003962 return NULL;
3963 classname = NEW_IDENTIFIER(CHILD(n, 1));
3964 if (!classname)
3965 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003966 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003967 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003968 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3969 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970 }
3971
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003972 /* class NAME '(' arglist ')' ':' suite */
3973 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003974 {
3975 PyObject *dummy_name;
3976 expr_ty dummy;
3977 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3978 if (!dummy_name)
3979 return NULL;
3980 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003981 call = ast_for_call(c, CHILD(n, 3), dummy, false);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003982 if (!call)
3983 return NULL;
3984 }
INADA Naokicb41b272017-02-23 00:31:59 +09003985 s = ast_for_body(c, CHILD(n, 6), &docstring);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003986 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003987 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003988 classname = NEW_IDENTIFIER(CHILD(n, 1));
3989 if (!classname)
3990 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003991 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003992 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003993
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003994 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
INADA Naokicb41b272017-02-23 00:31:59 +09003995 decorator_seq, docstring, LINENO(n), n->n_col_offset,
3996 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003997}
3998
3999static stmt_ty
4000ast_for_stmt(struct compiling *c, const node *n)
4001{
4002 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004003 assert(NCH(n) == 1);
4004 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 }
4006 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004007 assert(num_stmts(n) == 1);
4008 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004009 }
4010 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004011 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004012 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4013 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004014 */
4015 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 case expr_stmt:
4017 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004018 case del_stmt:
4019 return ast_for_del_stmt(c, n);
4020 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004021 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 case flow_stmt:
4023 return ast_for_flow_stmt(c, n);
4024 case import_stmt:
4025 return ast_for_import_stmt(c, n);
4026 case global_stmt:
4027 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004028 case nonlocal_stmt:
4029 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 case assert_stmt:
4031 return ast_for_assert_stmt(c, n);
4032 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004033 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4035 TYPE(n), NCH(n));
4036 return NULL;
4037 }
4038 }
4039 else {
4040 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004041 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004042 */
4043 node *ch = CHILD(n, 0);
4044 REQ(n, compound_stmt);
4045 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046 case if_stmt:
4047 return ast_for_if_stmt(c, ch);
4048 case while_stmt:
4049 return ast_for_while_stmt(c, ch);
4050 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004051 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052 case try_stmt:
4053 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004054 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004055 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004057 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004058 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004059 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 case decorated:
4061 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004062 case async_stmt:
4063 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004065 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4067 TYPE(n), NCH(n));
4068 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004069 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 }
4071}
4072
4073static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004074parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004076 const char *end;
4077 long x;
4078 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004079 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004080 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004081
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004082 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004083 errno = 0;
4084 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004085 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004086 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004087 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004088 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004089 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004090 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004091 }
4092 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004093 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004094 if (*end == '\0') {
4095 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004096 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004097 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004098 }
4099 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004100 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004101 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004102 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4103 if (compl.imag == -1.0 && PyErr_Occurred())
4104 return NULL;
4105 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004106 }
4107 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004108 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004109 dx = PyOS_string_to_double(s, NULL, NULL);
4110 if (dx == -1.0 && PyErr_Occurred())
4111 return NULL;
4112 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004113 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004114}
4115
4116static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004117parsenumber(struct compiling *c, const char *s)
4118{
4119 char *dup, *end;
4120 PyObject *res = NULL;
4121
4122 assert(s != NULL);
4123
4124 if (strchr(s, '_') == NULL) {
4125 return parsenumber_raw(c, s);
4126 }
4127 /* Create a duplicate without underscores. */
4128 dup = PyMem_Malloc(strlen(s) + 1);
4129 end = dup;
4130 for (; *s; s++) {
4131 if (*s != '_') {
4132 *end++ = *s;
4133 }
4134 }
4135 *end = '\0';
4136 res = parsenumber_raw(c, dup);
4137 PyMem_Free(dup);
4138 return res;
4139}
4140
4141static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004142decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004143{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004144 const char *s, *t;
4145 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004146 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4147 while (s < end && (*s & 0x80)) s++;
4148 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004149 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004150}
4151
Eric V. Smith56466482016-10-31 14:46:26 -04004152static int
4153warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004154 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004155{
4156 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4157 first_invalid_escape_char);
4158 if (msg == NULL) {
4159 return -1;
4160 }
4161 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4162 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004163 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004164 {
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004165 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4166 const char *s;
Victor Stinnerf9cca362016-11-15 09:12:10 +01004167
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004168 /* Replace the DeprecationWarning exception with a SyntaxError
4169 to get a more accurate error report */
4170 PyErr_Clear();
Victor Stinnerf9cca362016-11-15 09:12:10 +01004171
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004172 s = PyUnicode_AsUTF8(msg);
4173 if (s != NULL) {
4174 ast_error(c, n, s);
4175 }
Eric V. Smith56466482016-10-31 14:46:26 -04004176 }
4177 Py_DECREF(msg);
4178 return -1;
4179 }
4180 Py_DECREF(msg);
4181 return 0;
4182}
4183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004184static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004185decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4186 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004187{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004188 PyObject *v, *u;
4189 char *buf;
4190 char *p;
4191 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004192
Benjamin Peterson202803a2016-02-25 22:34:45 -08004193 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004194 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004195 return NULL;
4196 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4197 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4198 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4199 if (u == NULL)
4200 return NULL;
4201 p = buf = PyBytes_AsString(u);
4202 end = s + len;
4203 while (s < end) {
4204 if (*s == '\\') {
4205 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004206 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004207 strcpy(p, "u005c");
4208 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004209 if (s >= end)
4210 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004211 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004212 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004213 if (*s & 0x80) { /* XXX inefficient */
4214 PyObject *w;
4215 int kind;
4216 void *data;
4217 Py_ssize_t len, i;
4218 w = decode_utf8(c, &s, end);
4219 if (w == NULL) {
4220 Py_DECREF(u);
4221 return NULL;
4222 }
4223 kind = PyUnicode_KIND(w);
4224 data = PyUnicode_DATA(w);
4225 len = PyUnicode_GET_LENGTH(w);
4226 for (i = 0; i < len; i++) {
4227 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4228 sprintf(p, "\\U%08x", chr);
4229 p += 10;
4230 }
4231 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004232 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004233 Py_DECREF(w);
4234 } else {
4235 *p++ = *s++;
4236 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004237 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004238 len = p - buf;
4239 s = buf;
4240
Eric V. Smith56466482016-10-31 14:46:26 -04004241 const char *first_invalid_escape;
4242 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4243
4244 if (v != NULL && first_invalid_escape != NULL) {
4245 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4246 /* We have not decref u before because first_invalid_escape points
4247 inside u. */
4248 Py_XDECREF(u);
4249 Py_DECREF(v);
4250 return NULL;
4251 }
4252 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004253 Py_XDECREF(u);
4254 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004255}
4256
Eric V. Smith56466482016-10-31 14:46:26 -04004257static PyObject *
4258decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4259 size_t len)
4260{
4261 const char *first_invalid_escape;
4262 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4263 &first_invalid_escape);
4264 if (result == NULL)
4265 return NULL;
4266
4267 if (first_invalid_escape != NULL) {
4268 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4269 Py_DECREF(result);
4270 return NULL;
4271 }
4272 }
4273 return result;
4274}
4275
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004276/* Shift locations for the given node and all its children by adding `lineno`
4277 and `col_offset` to existing locations. */
4278static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4279{
4280 n->n_col_offset = n->n_col_offset + col_offset;
4281 for (int i = 0; i < NCH(n); ++i) {
4282 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4283 /* Shifting column offsets unnecessary if there's been newlines. */
4284 col_offset = 0;
4285 }
4286 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4287 }
4288 n->n_lineno = n->n_lineno + lineno;
4289}
4290
4291/* Fix locations for the given node and its children.
4292
4293 `parent` is the enclosing node.
4294 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004295 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004296*/
4297static void
4298fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4299{
4300 char *substr = NULL;
4301 char *start;
4302 int lines = LINENO(parent) - 1;
4303 int cols = parent->n_col_offset;
4304 /* Find the full fstring to fix location information in `n`. */
4305 while (parent && parent->n_type != STRING)
4306 parent = parent->n_child;
4307 if (parent && parent->n_str) {
4308 substr = strstr(parent->n_str, expr_str);
4309 if (substr) {
4310 start = substr;
4311 while (start > parent->n_str) {
4312 if (start[0] == '\n')
4313 break;
4314 start--;
4315 }
4316 cols += substr - start;
4317 /* Fix lineno in mulitline strings. */
4318 while ((substr = strchr(substr + 1, '\n')))
4319 lines--;
4320 }
4321 }
4322 fstring_shift_node_locations(n, lines, cols);
4323}
4324
Eric V. Smith451d0e32016-09-09 21:56:20 -04004325/* Compile this expression in to an expr_ty. Add parens around the
4326 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004327static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004328fstring_compile_expr(const char *expr_start, const char *expr_end,
4329 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004330
Eric V. Smith235a6f02015-09-19 14:51:32 -04004331{
4332 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004333 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004334 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004335 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004336 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004337 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004338
Eric V. Smith1d44c412015-09-23 07:49:00 -04004339 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004340 assert(*(expr_start-1) == '{');
4341 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004342
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004343 /* If the substring is all whitespace, it's an error. We need to catch this
4344 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4345 because turning the expression '' in to '()' would go from being invalid
4346 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004347 for (s = expr_start; s != expr_end; s++) {
4348 char c = *s;
4349 /* The Python parser ignores only the following whitespace
4350 characters (\r already is converted to \n). */
4351 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352 break;
4353 }
4354 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004355 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004356 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004357 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004358 }
4359
Eric V. Smith451d0e32016-09-09 21:56:20 -04004360 len = expr_end - expr_start;
4361 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4362 str = PyMem_RawMalloc(len + 3);
4363 if (str == NULL)
4364 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004365
Eric V. Smith451d0e32016-09-09 21:56:20 -04004366 str[0] = '(';
4367 memcpy(str+1, expr_start, len);
4368 str[len+1] = ')';
4369 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004370
4371 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004372 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4373 Py_eval_input, 0);
4374 if (!mod_n) {
4375 PyMem_RawFree(str);
4376 return NULL;
4377 }
4378 /* Reuse str to find the correct column offset. */
4379 str[0] = '{';
4380 str[len+1] = '}';
4381 fstring_fix_node_location(n, mod_n, str);
4382 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004383 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004384 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004385 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004386 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004387 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004388}
4389
4390/* Return -1 on error.
4391
4392 Return 0 if we reached the end of the literal.
4393
4394 Return 1 if we haven't reached the end of the literal, but we want
4395 the caller to process the literal up to this point. Used for
4396 doubled braces.
4397*/
4398static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004399fstring_find_literal(const char **str, const char *end, int raw,
4400 PyObject **literal, int recurse_lvl,
4401 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004402{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004403 /* Get any literal string. It ends when we hit an un-doubled left
4404 brace (which isn't part of a unicode name escape such as
4405 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004406
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004407 const char *s = *str;
4408 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004409 int result = 0;
4410
Eric V. Smith235a6f02015-09-19 14:51:32 -04004411 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004412 while (s < end) {
4413 char ch = *s++;
4414 if (!raw && ch == '\\' && s < end) {
4415 ch = *s++;
4416 if (ch == 'N') {
4417 if (s < end && *s++ == '{') {
4418 while (s < end && *s++ != '}') {
4419 }
4420 continue;
4421 }
4422 break;
4423 }
4424 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4425 return -1;
4426 }
4427 }
4428 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004429 /* Check for doubled braces, but only at the top level. If
4430 we checked at every level, then f'{0:{3}}' would fail
4431 with the two closing braces. */
4432 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004433 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004434 /* We're going to tell the caller that the literal ends
4435 here, but that they should continue scanning. But also
4436 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004437 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004438 result = 1;
4439 goto done;
4440 }
4441
4442 /* Where a single '{' is the start of a new expression, a
4443 single '}' is not allowed. */
4444 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004445 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004446 ast_error(c, n, "f-string: single '}' is not allowed");
4447 return -1;
4448 }
4449 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004450 /* We're either at a '{', which means we're starting another
4451 expression; or a '}', which means we're at the end of this
4452 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004453 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004454 break;
4455 }
4456 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004457 *str = s;
4458 assert(s <= end);
4459 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004460done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004461 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004462 if (raw)
4463 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004464 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004465 NULL, NULL);
4466 else
Eric V. Smith56466482016-10-31 14:46:26 -04004467 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004468 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004469 if (!*literal)
4470 return -1;
4471 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004472 return result;
4473}
4474
4475/* Forward declaration because parsing is recursive. */
4476static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004477fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004478 struct compiling *c, const node *n);
4479
Eric V. Smith451d0e32016-09-09 21:56:20 -04004480/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004481 expression (so it must be a '{'). Returns the FormattedValue node,
4482 which includes the expression, conversion character, and
4483 format_spec expression.
4484
4485 Note that I don't do a perfect job here: I don't make sure that a
4486 closing brace doesn't match an opening paren, for example. It
4487 doesn't need to error on all invalid expressions, just correctly
4488 find the end of all valid ones. Any errors inside the expression
4489 will be caught when we parse it later. */
4490static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004491fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004492 expr_ty *expression, struct compiling *c, const node *n)
4493{
4494 /* Return -1 on error, else 0. */
4495
Eric V. Smith451d0e32016-09-09 21:56:20 -04004496 const char *expr_start;
4497 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004498 expr_ty simple_expression;
4499 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004500 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004501
4502 /* 0 if we're not in a string, else the quote char we're trying to
4503 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004504 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004505
4506 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4507 int string_type = 0;
4508
4509 /* Keep track of nesting level for braces/parens/brackets in
4510 expressions. */
4511 Py_ssize_t nested_depth = 0;
4512
4513 /* Can only nest one level deep. */
4514 if (recurse_lvl >= 2) {
4515 ast_error(c, n, "f-string: expressions nested too deeply");
4516 return -1;
4517 }
4518
4519 /* The first char must be a left brace, or we wouldn't have gotten
4520 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004521 assert(**str == '{');
4522 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004523
Eric V. Smith451d0e32016-09-09 21:56:20 -04004524 expr_start = *str;
4525 for (; *str < end; (*str)++) {
4526 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004527
4528 /* Loop invariants. */
4529 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004530 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004531 if (quote_char)
4532 assert(string_type == 1 || string_type == 3);
4533 else
4534 assert(string_type == 0);
4535
Eric V. Smith451d0e32016-09-09 21:56:20 -04004536 ch = **str;
4537 /* Nowhere inside an expression is a backslash allowed. */
4538 if (ch == '\\') {
4539 /* Error: can't include a backslash character, inside
4540 parens or strings or not. */
4541 ast_error(c, n, "f-string expression part "
4542 "cannot include a backslash");
4543 return -1;
4544 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004545 if (quote_char) {
4546 /* We're inside a string. See if we're at the end. */
4547 /* This code needs to implement the same non-error logic
4548 as tok_get from tokenizer.c, at the letter_quote
4549 label. To actually share that code would be a
4550 nightmare. But, it's unlikely to change and is small,
4551 so duplicate it here. Note we don't need to catch all
4552 of the errors, since they'll be caught when parsing the
4553 expression. We just need to match the non-error
4554 cases. Thus we can ignore \n in single-quoted strings,
4555 for example. Or non-terminated strings. */
4556 if (ch == quote_char) {
4557 /* Does this match the string_type (single or triple
4558 quoted)? */
4559 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004560 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004561 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004562 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004563 string_type = 0;
4564 quote_char = 0;
4565 continue;
4566 }
4567 } else {
4568 /* We're at the end of a normal string. */
4569 quote_char = 0;
4570 string_type = 0;
4571 continue;
4572 }
4573 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004574 } else if (ch == '\'' || ch == '"') {
4575 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004576 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004577 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004578 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004579 } else {
4580 /* Start of a normal string. */
4581 string_type = 1;
4582 }
4583 /* Start looking for the end of the string. */
4584 quote_char = ch;
4585 } else if (ch == '[' || ch == '{' || ch == '(') {
4586 nested_depth++;
4587 } else if (nested_depth != 0 &&
4588 (ch == ']' || ch == '}' || ch == ')')) {
4589 nested_depth--;
4590 } else if (ch == '#') {
4591 /* Error: can't include a comment character, inside parens
4592 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004593 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004594 return -1;
4595 } else if (nested_depth == 0 &&
4596 (ch == '!' || ch == ':' || ch == '}')) {
4597 /* First, test for the special case of "!=". Since '=' is
4598 not an allowed conversion character, nothing is lost in
4599 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004600 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004601 /* This isn't a conversion character, just continue. */
4602 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004603 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004604 /* Normal way out of this loop. */
4605 break;
4606 } else {
4607 /* Just consume this char and loop around. */
4608 }
4609 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004610 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004611 /* If we leave this loop in a string or with mismatched parens, we
4612 don't care. We'll get a syntax error when compiling the
4613 expression. But, we can produce a better error message, so
4614 let's just do that.*/
4615 if (quote_char) {
4616 ast_error(c, n, "f-string: unterminated string");
4617 return -1;
4618 }
4619 if (nested_depth) {
4620 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4621 return -1;
4622 }
4623
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. Smith1d44c412015-09-23 07:49:00 -04004626
4627 /* Compile the expression as soon as possible, so we show errors
4628 related to the expression before errors related to the
4629 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004630 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004631 if (!simple_expression)
4632 return -1;
4633
4634 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004635 if (**str == '!') {
4636 *str += 1;
4637 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004638 goto unexpected_end_of_string;
4639
Eric V. Smith451d0e32016-09-09 21:56:20 -04004640 conversion = **str;
4641 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004642
4643 /* Validate the conversion. */
4644 if (!(conversion == 's' || conversion == 'r'
4645 || conversion == 'a')) {
4646 ast_error(c, n, "f-string: invalid conversion character: "
4647 "expected 's', 'r', or 'a'");
4648 return -1;
4649 }
4650 }
4651
4652 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004653 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004654 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004655 if (**str == ':') {
4656 *str += 1;
4657 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004658 goto unexpected_end_of_string;
4659
4660 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004661 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004662 if (!format_spec)
4663 return -1;
4664 }
4665
Eric V. Smith451d0e32016-09-09 21:56:20 -04004666 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004667 goto unexpected_end_of_string;
4668
4669 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004670 assert(*str < end);
4671 assert(**str == '}');
4672 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004673
Eric V. Smith451d0e32016-09-09 21:56:20 -04004674 /* And now create the FormattedValue node that represents this
4675 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004676 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004677 format_spec, LINENO(n), n->n_col_offset,
4678 c->c_arena);
4679 if (!*expression)
4680 return -1;
4681
4682 return 0;
4683
4684unexpected_end_of_string:
4685 ast_error(c, n, "f-string: expecting '}'");
4686 return -1;
4687}
4688
4689/* Return -1 on error.
4690
4691 Return 0 if we have a literal (possible zero length) and an
4692 expression (zero length if at the end of the string.
4693
4694 Return 1 if we have a literal, but no expression, and we want the
4695 caller to call us again. This is used to deal with doubled
4696 braces.
4697
4698 When called multiple times on the string 'a{{b{0}c', this function
4699 will return:
4700
4701 1. the literal 'a{' with no expression, and a return value
4702 of 1. Despite the fact that there's no expression, the return
4703 value of 1 means we're not finished yet.
4704
4705 2. the literal 'b' and the expression '0', with a return value of
4706 0. The fact that there's an expression means we're not finished.
4707
4708 3. literal 'c' with no expression and a return value of 0. The
4709 combination of the return value of 0 with no expression means
4710 we're finished.
4711*/
4712static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004713fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4714 int recurse_lvl, PyObject **literal,
4715 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004716 struct compiling *c, const node *n)
4717{
4718 int result;
4719
4720 assert(*literal == NULL && *expression == NULL);
4721
4722 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004723 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004724 if (result < 0)
4725 goto error;
4726
4727 assert(result == 0 || result == 1);
4728
4729 if (result == 1)
4730 /* We have a literal, but don't look at the expression. */
4731 return 1;
4732
Eric V. Smith451d0e32016-09-09 21:56:20 -04004733 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004734 /* We're at the end of the string or the end of a nested
4735 f-string: no expression. The top-level error case where we
4736 expect to be at the end of the string but we're at a '}' is
4737 handled later. */
4738 return 0;
4739
4740 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004741 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004742
Eric V. Smith451d0e32016-09-09 21:56:20 -04004743 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004744 goto error;
4745
4746 return 0;
4747
4748error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004749 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004750 return -1;
4751}
4752
4753#define EXPRLIST_N_CACHED 64
4754
4755typedef struct {
4756 /* Incrementally build an array of expr_ty, so be used in an
4757 asdl_seq. Cache some small but reasonably sized number of
4758 expr_ty's, and then after that start dynamically allocating,
4759 doubling the number allocated each time. Note that the f-string
4760 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4761 Str for the literal 'a'. So you add expr_ty's about twice as
4762 fast as you add exressions in an f-string. */
4763
4764 Py_ssize_t allocated; /* Number we've allocated. */
4765 Py_ssize_t size; /* Number we've used. */
4766 expr_ty *p; /* Pointer to the memory we're actually
4767 using. Will point to 'data' until we
4768 start dynamically allocating. */
4769 expr_ty data[EXPRLIST_N_CACHED];
4770} ExprList;
4771
4772#ifdef NDEBUG
4773#define ExprList_check_invariants(l)
4774#else
4775static void
4776ExprList_check_invariants(ExprList *l)
4777{
4778 /* Check our invariants. Make sure this object is "live", and
4779 hasn't been deallocated. */
4780 assert(l->size >= 0);
4781 assert(l->p != NULL);
4782 if (l->size <= EXPRLIST_N_CACHED)
4783 assert(l->data == l->p);
4784}
4785#endif
4786
4787static void
4788ExprList_Init(ExprList *l)
4789{
4790 l->allocated = EXPRLIST_N_CACHED;
4791 l->size = 0;
4792
4793 /* Until we start allocating dynamically, p points to data. */
4794 l->p = l->data;
4795
4796 ExprList_check_invariants(l);
4797}
4798
4799static int
4800ExprList_Append(ExprList *l, expr_ty exp)
4801{
4802 ExprList_check_invariants(l);
4803 if (l->size >= l->allocated) {
4804 /* We need to alloc (or realloc) the memory. */
4805 Py_ssize_t new_size = l->allocated * 2;
4806
4807 /* See if we've ever allocated anything dynamically. */
4808 if (l->p == l->data) {
4809 Py_ssize_t i;
4810 /* We're still using the cached data. Switch to
4811 alloc-ing. */
4812 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4813 if (!l->p)
4814 return -1;
4815 /* Copy the cached data into the new buffer. */
4816 for (i = 0; i < l->size; i++)
4817 l->p[i] = l->data[i];
4818 } else {
4819 /* Just realloc. */
4820 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4821 if (!tmp) {
4822 PyMem_RawFree(l->p);
4823 l->p = NULL;
4824 return -1;
4825 }
4826 l->p = tmp;
4827 }
4828
4829 l->allocated = new_size;
4830 assert(l->allocated == 2 * l->size);
4831 }
4832
4833 l->p[l->size++] = exp;
4834
4835 ExprList_check_invariants(l);
4836 return 0;
4837}
4838
4839static void
4840ExprList_Dealloc(ExprList *l)
4841{
4842 ExprList_check_invariants(l);
4843
4844 /* If there's been an error, or we've never dynamically allocated,
4845 do nothing. */
4846 if (!l->p || l->p == l->data) {
4847 /* Do nothing. */
4848 } else {
4849 /* We have dynamically allocated. Free the memory. */
4850 PyMem_RawFree(l->p);
4851 }
4852 l->p = NULL;
4853 l->size = -1;
4854}
4855
4856static asdl_seq *
4857ExprList_Finish(ExprList *l, PyArena *arena)
4858{
4859 asdl_seq *seq;
4860
4861 ExprList_check_invariants(l);
4862
4863 /* Allocate the asdl_seq and copy the expressions in to it. */
4864 seq = _Py_asdl_seq_new(l->size, arena);
4865 if (seq) {
4866 Py_ssize_t i;
4867 for (i = 0; i < l->size; i++)
4868 asdl_seq_SET(seq, i, l->p[i]);
4869 }
4870 ExprList_Dealloc(l);
4871 return seq;
4872}
4873
4874/* The FstringParser is designed to add a mix of strings and
4875 f-strings, and concat them together as needed. Ultimately, it
4876 generates an expr_ty. */
4877typedef struct {
4878 PyObject *last_str;
4879 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004880 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004881} FstringParser;
4882
4883#ifdef NDEBUG
4884#define FstringParser_check_invariants(state)
4885#else
4886static void
4887FstringParser_check_invariants(FstringParser *state)
4888{
4889 if (state->last_str)
4890 assert(PyUnicode_CheckExact(state->last_str));
4891 ExprList_check_invariants(&state->expr_list);
4892}
4893#endif
4894
4895static void
4896FstringParser_Init(FstringParser *state)
4897{
4898 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004899 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900 ExprList_Init(&state->expr_list);
4901 FstringParser_check_invariants(state);
4902}
4903
4904static void
4905FstringParser_Dealloc(FstringParser *state)
4906{
4907 FstringParser_check_invariants(state);
4908
4909 Py_XDECREF(state->last_str);
4910 ExprList_Dealloc(&state->expr_list);
4911}
4912
4913/* Make a Str node, but decref the PyUnicode object being added. */
4914static expr_ty
4915make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4916{
4917 PyObject *s = *str;
4918 *str = NULL;
4919 assert(PyUnicode_CheckExact(s));
4920 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4921 Py_DECREF(s);
4922 return NULL;
4923 }
4924 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4925}
4926
4927/* Add a non-f-string (that is, a regular literal string). str is
4928 decref'd. */
4929static int
4930FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4931{
4932 FstringParser_check_invariants(state);
4933
4934 assert(PyUnicode_CheckExact(str));
4935
4936 if (PyUnicode_GET_LENGTH(str) == 0) {
4937 Py_DECREF(str);
4938 return 0;
4939 }
4940
4941 if (!state->last_str) {
4942 /* We didn't have a string before, so just remember this one. */
4943 state->last_str = str;
4944 } else {
4945 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004946 PyUnicode_AppendAndDel(&state->last_str, str);
4947 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004948 return -1;
4949 }
4950 FstringParser_check_invariants(state);
4951 return 0;
4952}
4953
Eric V. Smith451d0e32016-09-09 21:56:20 -04004954/* Parse an f-string. The f-string is in *str to end, with no
4955 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004956static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004957FstringParser_ConcatFstring(FstringParser *state, const char **str,
4958 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004959 struct compiling *c, const node *n)
4960{
4961 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004962 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963
4964 /* Parse the f-string. */
4965 while (1) {
4966 PyObject *literal = NULL;
4967 expr_ty expression = NULL;
4968
4969 /* If there's a zero length literal in front of the
4970 expression, literal will be NULL. If we're at the end of
4971 the f-string, expression will be NULL (unless result == 1,
4972 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004973 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004974 &literal, &expression,
4975 c, n);
4976 if (result < 0)
4977 return -1;
4978
4979 /* Add the literal, if any. */
4980 if (!literal) {
4981 /* Do nothing. Just leave last_str alone (and possibly
4982 NULL). */
4983 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004984 /* Note that the literal can be zero length, if the
4985 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004986 state->last_str = literal;
4987 literal = NULL;
4988 } else {
4989 /* We have a literal, concatenate it. */
4990 assert(PyUnicode_GET_LENGTH(literal) != 0);
4991 if (FstringParser_ConcatAndDel(state, literal) < 0)
4992 return -1;
4993 literal = NULL;
4994 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995
4996 /* We've dealt with the literal now. It can't be leaked on further
4997 errors. */
4998 assert(literal == NULL);
4999
5000 /* See if we should just loop around to get the next literal
5001 and expression, while ignoring the expression this
5002 time. This is used for un-doubling braces, as an
5003 optimization. */
5004 if (result == 1)
5005 continue;
5006
5007 if (!expression)
5008 /* We're done with this f-string. */
5009 break;
5010
5011 /* We know we have an expression. Convert any existing string
5012 to a Str node. */
5013 if (!state->last_str) {
5014 /* Do nothing. No previous literal. */
5015 } else {
5016 /* Convert the existing last_str literal to a Str node. */
5017 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5018 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5019 return -1;
5020 }
5021
5022 if (ExprList_Append(&state->expr_list, expression) < 0)
5023 return -1;
5024 }
5025
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 /* If recurse_lvl is zero, then we must be at the end of the
5027 string. Otherwise, we must be at a right brace. */
5028
Eric V. Smith451d0e32016-09-09 21:56:20 -04005029 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005030 ast_error(c, n, "f-string: unexpected end of string");
5031 return -1;
5032 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005033 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005034 ast_error(c, n, "f-string: expecting '}'");
5035 return -1;
5036 }
5037
5038 FstringParser_check_invariants(state);
5039 return 0;
5040}
5041
5042/* Convert the partial state reflected in last_str and expr_list to an
5043 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5044static expr_ty
5045FstringParser_Finish(FstringParser *state, struct compiling *c,
5046 const node *n)
5047{
5048 asdl_seq *seq;
5049
5050 FstringParser_check_invariants(state);
5051
5052 /* If we're just a constant string with no expressions, return
5053 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005054 if (!state->fmode) {
5055 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005056 if (!state->last_str) {
5057 /* Create a zero length string. */
5058 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5059 if (!state->last_str)
5060 goto error;
5061 }
5062 return make_str_node_and_del(&state->last_str, c, n);
5063 }
5064
5065 /* Create a Str node out of last_str, if needed. It will be the
5066 last node in our expression list. */
5067 if (state->last_str) {
5068 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5069 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5070 goto error;
5071 }
5072 /* This has already been freed. */
5073 assert(state->last_str == NULL);
5074
5075 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5076 if (!seq)
5077 goto error;
5078
Eric V. Smith235a6f02015-09-19 14:51:32 -04005079 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5080
5081error:
5082 FstringParser_Dealloc(state);
5083 return NULL;
5084}
5085
Eric V. Smith451d0e32016-09-09 21:56:20 -04005086/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5087 at end, parse it into an expr_ty. Return NULL on error. Adjust
5088 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005089static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005091 struct compiling *c, const node *n)
5092{
5093 FstringParser state;
5094
5095 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005097 c, n) < 0) {
5098 FstringParser_Dealloc(&state);
5099 return NULL;
5100 }
5101
5102 return FstringParser_Finish(&state, c, n);
5103}
5104
5105/* n is a Python string literal, including the bracketing quote
5106 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005108 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005109 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5110 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005111*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005112static int
5113parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5114 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005115{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005116 size_t len;
5117 const char *s = STR(n);
5118 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005119 int fmode = 0;
5120 *bytesmode = 0;
5121 *rawmode = 0;
5122 *result = NULL;
5123 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005124 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005125 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005126 if (quote == 'b' || quote == 'B') {
5127 quote = *++s;
5128 *bytesmode = 1;
5129 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005130 else if (quote == 'u' || quote == 'U') {
5131 quote = *++s;
5132 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005133 else if (quote == 'r' || quote == 'R') {
5134 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005135 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005136 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005137 else if (quote == 'f' || quote == 'F') {
5138 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005139 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005140 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005141 else {
5142 break;
5143 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005144 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005145 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005146 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005148 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005149 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005150 if (quote != '\'' && quote != '\"') {
5151 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005152 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005153 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005154 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005155 s++;
5156 len = strlen(s);
5157 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005159 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005160 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005161 }
5162 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005163 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005164 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005165 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005166 }
5167 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005168 /* A triple quoted string. We've already skipped one quote at
5169 the start and one at the end of the string. Now skip the
5170 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005171 s += 2;
5172 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005173 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005174 if (s[--len] != quote || s[--len] != quote) {
5175 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005177 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005178 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005179
Eric V. Smith451d0e32016-09-09 21:56:20 -04005180 if (fmode) {
5181 /* Just return the bytes. The caller will parse the resulting
5182 string. */
5183 *fstr = s;
5184 *fstrlen = len;
5185 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005186 }
5187
Eric V. Smith451d0e32016-09-09 21:56:20 -04005188 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005189 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005190 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005191 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005192 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005193 const char *ch;
5194 for (ch = s; *ch; ch++) {
5195 if (Py_CHARMASK(*ch) >= 0x80) {
5196 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005197 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005198 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005199 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005200 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005201 if (*rawmode)
5202 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005203 else
Eric V. Smith56466482016-10-31 14:46:26 -04005204 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005205 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005206 if (*rawmode)
5207 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005208 else
Eric V. Smith56466482016-10-31 14:46:26 -04005209 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005210 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005211 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005212}
5213
Eric V. Smith235a6f02015-09-19 14:51:32 -04005214/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5215 each STRING atom, and process it as needed. For bytes, just
5216 concatenate them together, and the result will be a Bytes node. For
5217 normal strings and f-strings, concatenate them together. The result
5218 will be a Str node if there were no f-strings; a FormattedValue
5219 node if there's just an f-string (with no leading or trailing
5220 literals), or a JoinedStr node if there are multiple f-strings or
5221 any literals involved. */
5222static expr_ty
5223parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005224{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005225 int bytesmode = 0;
5226 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005227 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005228
5229 FstringParser state;
5230 FstringParser_Init(&state);
5231
5232 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005233 int this_bytesmode;
5234 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005235 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005236 const char *fstr;
5237 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005238
5239 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005240 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5241 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005242 goto error;
5243
5244 /* Check that we're not mixing bytes with unicode. */
5245 if (i != 0 && bytesmode != this_bytesmode) {
5246 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005247 /* s is NULL if the current string part is an f-string. */
5248 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005249 goto error;
5250 }
5251 bytesmode = this_bytesmode;
5252
Eric V. Smith451d0e32016-09-09 21:56:20 -04005253 if (fstr != NULL) {
5254 int result;
5255 assert(s == NULL && !bytesmode);
5256 /* This is an f-string. Parse and concatenate it. */
5257 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5258 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005259 if (result < 0)
5260 goto error;
5261 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005262 /* A string or byte string. */
5263 assert(s != NULL && fstr == NULL);
5264
Eric V. Smith451d0e32016-09-09 21:56:20 -04005265 assert(bytesmode ? PyBytes_CheckExact(s) :
5266 PyUnicode_CheckExact(s));
5267
Eric V. Smith451d0e32016-09-09 21:56:20 -04005268 if (bytesmode) {
5269 /* For bytes, concat as we go. */
5270 if (i == 0) {
5271 /* First time, just remember this value. */
5272 bytes_str = s;
5273 } else {
5274 PyBytes_ConcatAndDel(&bytes_str, s);
5275 if (!bytes_str)
5276 goto error;
5277 }
5278 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005279 /* This is a regular string. Concatenate it. */
5280 if (FstringParser_ConcatAndDel(&state, s) < 0)
5281 goto error;
5282 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005283 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005284 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005285 if (bytesmode) {
5286 /* Just return the bytes object and we're done. */
5287 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5288 goto error;
5289 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005291
Eric V. Smith235a6f02015-09-19 14:51:32 -04005292 /* We're not a bytes string, bytes_str should never have been set. */
5293 assert(bytes_str == NULL);
5294
5295 return FstringParser_Finish(&state, c, n);
5296
5297error:
5298 Py_XDECREF(bytes_str);
5299 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005300 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005301}