blob: a5d8dbaee1bb76fde3542fea139ae93a4d30d8bf [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
135validate_expr(expr_ty exp, expr_context_ty ctx)
136{
137 int check_ctx = 1;
138 expr_context_ty actual_ctx;
139
140 /* First check expression context. */
141 switch (exp->kind) {
142 case Attribute_kind:
143 actual_ctx = exp->v.Attribute.ctx;
144 break;
145 case Subscript_kind:
146 actual_ctx = exp->v.Subscript.ctx;
147 break;
148 case Starred_kind:
149 actual_ctx = exp->v.Starred.ctx;
150 break;
151 case Name_kind:
152 actual_ctx = exp->v.Name.ctx;
153 break;
154 case List_kind:
155 actual_ctx = exp->v.List.ctx;
156 break;
157 case Tuple_kind:
158 actual_ctx = exp->v.Tuple.ctx;
159 break;
160 default:
161 if (ctx != Load) {
162 PyErr_Format(PyExc_ValueError, "expression which can't be "
163 "assigned to in %s context", expr_context_name(ctx));
164 return 0;
165 }
166 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100167 /* set actual_ctx to prevent gcc warning */
168 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500169 }
170 if (check_ctx && actual_ctx != ctx) {
171 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
172 expr_context_name(ctx), expr_context_name(actual_ctx));
173 return 0;
174 }
175
176 /* Now validate expression. */
177 switch (exp->kind) {
178 case BoolOp_kind:
179 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
180 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
181 return 0;
182 }
183 return validate_exprs(exp->v.BoolOp.values, Load, 0);
184 case BinOp_kind:
185 return validate_expr(exp->v.BinOp.left, Load) &&
186 validate_expr(exp->v.BinOp.right, Load);
187 case UnaryOp_kind:
188 return validate_expr(exp->v.UnaryOp.operand, Load);
189 case Lambda_kind:
190 return validate_arguments(exp->v.Lambda.args) &&
191 validate_expr(exp->v.Lambda.body, Load);
192 case IfExp_kind:
193 return validate_expr(exp->v.IfExp.test, Load) &&
194 validate_expr(exp->v.IfExp.body, Load) &&
195 validate_expr(exp->v.IfExp.orelse, Load);
196 case Dict_kind:
197 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
198 PyErr_SetString(PyExc_ValueError,
199 "Dict doesn't have the same number of keys as values");
200 return 0;
201 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400202 /* null_ok=1 for keys expressions to allow dict unpacking to work in
203 dict literals, i.e. ``{**{a:b}}`` */
204 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
205 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500206 case Set_kind:
207 return validate_exprs(exp->v.Set.elts, Load, 0);
208#define COMP(NAME) \
209 case NAME ## _kind: \
210 return validate_comprehension(exp->v.NAME.generators) && \
211 validate_expr(exp->v.NAME.elt, Load);
212 COMP(ListComp)
213 COMP(SetComp)
214 COMP(GeneratorExp)
215#undef COMP
216 case DictComp_kind:
217 return validate_comprehension(exp->v.DictComp.generators) &&
218 validate_expr(exp->v.DictComp.key, Load) &&
219 validate_expr(exp->v.DictComp.value, Load);
220 case Yield_kind:
221 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500222 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000223 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400224 case Await_kind:
225 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500226 case Compare_kind:
227 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
228 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
229 return 0;
230 }
231 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
232 asdl_seq_LEN(exp->v.Compare.ops)) {
233 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
234 "of comparators and operands");
235 return 0;
236 }
237 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
238 validate_expr(exp->v.Compare.left, Load);
239 case Call_kind:
240 return validate_expr(exp->v.Call.func, Load) &&
241 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400242 validate_keywords(exp->v.Call.keywords);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500243 case Num_kind: {
244 PyObject *n = exp->v.Num.n;
245 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
246 !PyComplex_CheckExact(n)) {
247 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
248 return 0;
249 }
250 return 1;
251 }
252 case Str_kind: {
253 PyObject *s = exp->v.Str.s;
254 if (!PyUnicode_CheckExact(s)) {
255 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
256 return 0;
257 }
258 return 1;
259 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400260 case JoinedStr_kind:
261 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
262 case FormattedValue_kind:
263 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
264 return 0;
265 if (exp->v.FormattedValue.format_spec)
266 return validate_expr(exp->v.FormattedValue.format_spec, Load);
267 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500268 case Bytes_kind: {
269 PyObject *b = exp->v.Bytes.s;
270 if (!PyBytes_CheckExact(b)) {
271 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
272 return 0;
273 }
274 return 1;
275 }
276 case Attribute_kind:
277 return validate_expr(exp->v.Attribute.value, Load);
278 case Subscript_kind:
279 return validate_slice(exp->v.Subscript.slice) &&
280 validate_expr(exp->v.Subscript.value, Load);
281 case Starred_kind:
282 return validate_expr(exp->v.Starred.value, ctx);
283 case List_kind:
284 return validate_exprs(exp->v.List.elts, ctx, 0);
285 case Tuple_kind:
286 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
287 /* These last cases don't have any checking. */
288 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500289 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500290 case Ellipsis_kind:
291 return 1;
292 default:
293 PyErr_SetString(PyExc_SystemError, "unexpected expression");
294 return 0;
295 }
296}
297
298static int
299validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
300{
301 if (asdl_seq_LEN(seq))
302 return 1;
303 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
304 return 0;
305}
306
307static int
308validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
309{
310 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
311 validate_exprs(targets, ctx, 0);
312}
313
314static int
315validate_body(asdl_seq *body, const char *owner)
316{
317 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
318}
319
320static int
321validate_stmt(stmt_ty stmt)
322{
323 int i;
324 switch (stmt->kind) {
325 case FunctionDef_kind:
326 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
327 validate_arguments(stmt->v.FunctionDef.args) &&
328 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
329 (!stmt->v.FunctionDef.returns ||
330 validate_expr(stmt->v.FunctionDef.returns, Load));
331 case ClassDef_kind:
332 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
333 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
334 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400335 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500336 case Return_kind:
337 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
338 case Delete_kind:
339 return validate_assignlist(stmt->v.Delete.targets, Del);
340 case Assign_kind:
341 return validate_assignlist(stmt->v.Assign.targets, Store) &&
342 validate_expr(stmt->v.Assign.value, Load);
343 case AugAssign_kind:
344 return validate_expr(stmt->v.AugAssign.target, Store) &&
345 validate_expr(stmt->v.AugAssign.value, Load);
346 case For_kind:
347 return validate_expr(stmt->v.For.target, Store) &&
348 validate_expr(stmt->v.For.iter, Load) &&
349 validate_body(stmt->v.For.body, "For") &&
350 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400351 case AsyncFor_kind:
352 return validate_expr(stmt->v.AsyncFor.target, Store) &&
353 validate_expr(stmt->v.AsyncFor.iter, Load) &&
354 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
355 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 case While_kind:
357 return validate_expr(stmt->v.While.test, Load) &&
358 validate_body(stmt->v.While.body, "While") &&
359 validate_stmts(stmt->v.While.orelse);
360 case If_kind:
361 return validate_expr(stmt->v.If.test, Load) &&
362 validate_body(stmt->v.If.body, "If") &&
363 validate_stmts(stmt->v.If.orelse);
364 case With_kind:
365 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
366 return 0;
367 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
368 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
369 if (!validate_expr(item->context_expr, Load) ||
370 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
371 return 0;
372 }
373 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400374 case AsyncWith_kind:
375 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
376 return 0;
377 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
378 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
379 if (!validate_expr(item->context_expr, Load) ||
380 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
381 return 0;
382 }
383 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500384 case Raise_kind:
385 if (stmt->v.Raise.exc) {
386 return validate_expr(stmt->v.Raise.exc, Load) &&
387 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
388 }
389 if (stmt->v.Raise.cause) {
390 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
391 return 0;
392 }
393 return 1;
394 case Try_kind:
395 if (!validate_body(stmt->v.Try.body, "Try"))
396 return 0;
397 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
398 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
399 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
400 return 0;
401 }
402 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
403 asdl_seq_LEN(stmt->v.Try.orelse)) {
404 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
405 return 0;
406 }
407 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
408 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
409 if ((handler->v.ExceptHandler.type &&
410 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
411 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
412 return 0;
413 }
414 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
415 validate_stmts(stmt->v.Try.finalbody)) &&
416 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
417 validate_stmts(stmt->v.Try.orelse));
418 case Assert_kind:
419 return validate_expr(stmt->v.Assert.test, Load) &&
420 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
421 case Import_kind:
422 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
423 case ImportFrom_kind:
424 if (stmt->v.ImportFrom.level < -1) {
425 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
426 return 0;
427 }
428 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
429 case Global_kind:
430 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
431 case Nonlocal_kind:
432 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
433 case Expr_kind:
434 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400435 case AsyncFunctionDef_kind:
436 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
437 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
438 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
439 (!stmt->v.AsyncFunctionDef.returns ||
440 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500441 case Pass_kind:
442 case Break_kind:
443 case Continue_kind:
444 return 1;
445 default:
446 PyErr_SetString(PyExc_SystemError, "unexpected statement");
447 return 0;
448 }
449}
450
451static int
452validate_stmts(asdl_seq *seq)
453{
454 int i;
455 for (i = 0; i < asdl_seq_LEN(seq); i++) {
456 stmt_ty stmt = asdl_seq_GET(seq, i);
457 if (stmt) {
458 if (!validate_stmt(stmt))
459 return 0;
460 }
461 else {
462 PyErr_SetString(PyExc_ValueError,
463 "None disallowed in statement list");
464 return 0;
465 }
466 }
467 return 1;
468}
469
470static int
471validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
472{
473 int i;
474 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
475 expr_ty expr = asdl_seq_GET(exprs, i);
476 if (expr) {
477 if (!validate_expr(expr, ctx))
478 return 0;
479 }
480 else if (!null_ok) {
481 PyErr_SetString(PyExc_ValueError,
482 "None disallowed in expression list");
483 return 0;
484 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100485
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500486 }
487 return 1;
488}
489
490int
491PyAST_Validate(mod_ty mod)
492{
493 int res = 0;
494
495 switch (mod->kind) {
496 case Module_kind:
497 res = validate_stmts(mod->v.Module.body);
498 break;
499 case Interactive_kind:
500 res = validate_stmts(mod->v.Interactive.body);
501 break;
502 case Expression_kind:
503 res = validate_expr(mod->v.Expression.body, Load);
504 break;
505 case Suite_kind:
506 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
507 break;
508 default:
509 PyErr_SetString(PyExc_SystemError, "impossible module node");
510 res = 0;
511 break;
512 }
513 return res;
514}
515
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500516/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517#include "grammar.h"
518#include "parsetok.h"
519#include "graminit.h"
520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521/* Data structure used internally */
522struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000523 char *c_encoding; /* source encoding */
Eric V. Smith163b5c62015-08-21 09:40:38 -0400524 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200525 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500526 PyObject *c_normalize; /* Normalization function from unicodedata. */
527 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528};
529
530static asdl_seq *seq_for_testlist(struct compiling *, const node *);
531static expr_ty ast_for_expr(struct compiling *, const node *);
532static stmt_ty ast_for_stmt(struct compiling *, const node *);
533static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000534static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
535 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000536static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000537static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
Yury Selivanov75445082015-05-11 22:57:16 -0400539static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
540static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
541
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542/* Note different signature for ast_for_call */
543static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
544
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000545static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400546static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
Nick Coghlan650f0d02007-04-15 12:05:43 +0000548#define COMP_GENEXP 0
549#define COMP_LISTCOMP 1
550#define COMP_SETCOMP 2
551
Benjamin Peterson55e00432012-01-16 17:22:31 -0500552static int
553init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000554{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500555 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
556 if (!m)
557 return 0;
558 c->c_normalize = PyObject_GetAttrString(m, "normalize");
559 Py_DECREF(m);
560 if (!c->c_normalize)
561 return 0;
562 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500563 if (!c->c_normalize_args) {
564 Py_CLEAR(c->c_normalize);
565 return 0;
566 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200567 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 return 1;
569}
570
571static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400572new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500573{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400574 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500575 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000576 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500577 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500578 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000579 /* Check whether there are non-ASCII characters in the
580 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500581 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500583 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500584 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200585 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500586 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500587 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
588 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500589 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200590 if (!id2)
591 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000593 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000594 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200595 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
596 Py_DECREF(id);
597 return NULL;
598 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000599 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600}
601
Benjamin Peterson55e00432012-01-16 17:22:31 -0500602#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400605ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400607 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608
Victor Stinner14e461d2013-08-26 22:28:21 +0200609 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000611 Py_INCREF(Py_None);
612 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200614 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400615 if (!tmp)
616 return 0;
617 errstr = PyUnicode_FromString(errmsg);
618 if (!errstr) {
619 Py_DECREF(tmp);
620 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000621 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000622 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 Py_DECREF(errstr);
624 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400625 if (value) {
626 PyErr_SetObject(PyExc_SyntaxError, value);
627 Py_DECREF(value);
628 }
629 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630}
631
632/* num_stmts() returns number of contained statements.
633
634 Use this routine to determine how big a sequence is needed for
635 the statements in a parse tree. Its raison d'etre is this bit of
636 grammar:
637
638 stmt: simple_stmt | compound_stmt
639 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
640
641 A simple_stmt can contain multiple small_stmt elements joined
642 by semicolons. If the arg is a simple_stmt, the number of
643 small_stmt elements is returned.
644*/
645
646static int
647num_stmts(const node *n)
648{
649 int i, l;
650 node *ch;
651
652 switch (TYPE(n)) {
653 case single_input:
654 if (TYPE(CHILD(n, 0)) == NEWLINE)
655 return 0;
656 else
657 return num_stmts(CHILD(n, 0));
658 case file_input:
659 l = 0;
660 for (i = 0; i < NCH(n); i++) {
661 ch = CHILD(n, i);
662 if (TYPE(ch) == stmt)
663 l += num_stmts(ch);
664 }
665 return l;
666 case stmt:
667 return num_stmts(CHILD(n, 0));
668 case compound_stmt:
669 return 1;
670 case simple_stmt:
671 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
672 case suite:
673 if (NCH(n) == 1)
674 return num_stmts(CHILD(n, 0));
675 else {
676 l = 0;
677 for (i = 2; i < (NCH(n) - 1); i++)
678 l += num_stmts(CHILD(n, i));
679 return l;
680 }
681 default: {
682 char buf[128];
683
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000684 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 TYPE(n), NCH(n));
686 Py_FatalError(buf);
687 }
688 }
689 assert(0);
690 return 0;
691}
692
693/* Transform the CST rooted at node * to the appropriate AST
694*/
695
696mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200697PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
698 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000700 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 asdl_seq *stmts = NULL;
702 stmt_ty s;
703 node *ch;
704 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500705 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400707 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200708 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400709 c.c_filename = filename;
710 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000712 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000714#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400715 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500716 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000717#endif
718 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 } else if (TYPE(n) == encoding_decl) {
721 c.c_encoding = STR(n);
722 n = CHILD(n, 0);
723 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000725 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 }
727
Jeremy Hyltona8293132006-02-28 17:58:27 +0000728 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 switch (TYPE(n)) {
730 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200731 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500733 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 for (i = 0; i < NCH(n) - 1; i++) {
735 ch = CHILD(n, i);
736 if (TYPE(ch) == NEWLINE)
737 continue;
738 REQ(ch, stmt);
739 num = num_stmts(ch);
740 if (num == 1) {
741 s = ast_for_stmt(&c, ch);
742 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500743 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000744 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 }
746 else {
747 ch = CHILD(ch, 0);
748 REQ(ch, simple_stmt);
749 for (j = 0; j < num; j++) {
750 s = ast_for_stmt(&c, CHILD(ch, j * 2));
751 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500752 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000753 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 }
755 }
756 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500757 res = Module(stmts, arena);
758 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 case eval_input: {
760 expr_ty testlist_ast;
761
Nick Coghlan650f0d02007-04-15 12:05:43 +0000762 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000763 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500765 goto out;
766 res = Expression(testlist_ast, arena);
767 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 }
769 case single_input:
770 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200771 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500773 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000774 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
775 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000776 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500777 goto out;
778 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 }
780 else {
781 n = CHILD(n, 0);
782 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200783 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500785 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000787 s = ast_for_stmt(&c, n);
788 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500789 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 asdl_seq_SET(stmts, 0, s);
791 }
792 else {
793 /* Only a simple_stmt can contain multiple statements. */
794 REQ(n, simple_stmt);
795 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 if (TYPE(CHILD(n, i)) == NEWLINE)
797 break;
798 s = ast_for_stmt(&c, CHILD(n, i));
799 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500800 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 asdl_seq_SET(stmts, i / 2, s);
802 }
803 }
804
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500807 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000809 PyErr_Format(PyExc_SystemError,
810 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500811 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500813 out:
814 if (c.c_normalize) {
815 Py_DECREF(c.c_normalize);
816 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
817 Py_DECREF(c.c_normalize_args);
818 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500819 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820}
821
Victor Stinner14e461d2013-08-26 22:28:21 +0200822mod_ty
823PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
824 PyArena *arena)
825{
826 mod_ty mod;
827 PyObject *filename;
828 filename = PyUnicode_DecodeFSDefault(filename_str);
829 if (filename == NULL)
830 return NULL;
831 mod = PyAST_FromNodeObject(n, flags, filename, arena);
832 Py_DECREF(filename);
833 return mod;
834
835}
836
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
838*/
839
840static operator_ty
841get_operator(const node *n)
842{
843 switch (TYPE(n)) {
844 case VBAR:
845 return BitOr;
846 case CIRCUMFLEX:
847 return BitXor;
848 case AMPER:
849 return BitAnd;
850 case LEFTSHIFT:
851 return LShift;
852 case RIGHTSHIFT:
853 return RShift;
854 case PLUS:
855 return Add;
856 case MINUS:
857 return Sub;
858 case STAR:
859 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400860 case AT:
861 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 case SLASH:
863 return Div;
864 case DOUBLESLASH:
865 return FloorDiv;
866 case PERCENT:
867 return Mod;
868 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000869 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 }
871}
872
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200873static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000874 "None",
875 "True",
876 "False",
877 NULL,
878};
879
880static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400881forbidden_name(struct compiling *c, identifier name, const node *n,
882 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000883{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000884 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000885 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400886 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000887 return 1;
888 }
889 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200890 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000891 for (p = FORBIDDEN; *p; p++) {
892 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400893 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000894 return 1;
895 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000896 }
897 }
898 return 0;
899}
900
Jeremy Hyltona8293132006-02-28 17:58:27 +0000901/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902
903 Only sets context for expr kinds that "can appear in assignment context"
904 (according to ../Parser/Python.asdl). For other expr kinds, it sets
905 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906*/
907
908static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000909set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910{
911 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000912 /* If a particular expression type can't be used for assign / delete,
913 set expr_name to its name and an error message will be generated.
914 */
915 const char* expr_name = NULL;
916
917 /* The ast defines augmented store and load contexts, but the
918 implementation here doesn't actually use them. The code may be
919 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000920 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000921 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000922 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000923 */
924 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925
926 switch (e->kind) {
927 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000928 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400929 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000930 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000931 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000933 e->v.Subscript.ctx = ctx;
934 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000935 case Starred_kind:
936 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000937 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000938 return 0;
939 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000941 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500942 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000943 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000944 }
945 e->v.Name.ctx = ctx;
946 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000948 e->v.List.ctx = ctx;
949 s = e->v.List.elts;
950 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000952 if (asdl_seq_LEN(e->v.Tuple.elts)) {
953 e->v.Tuple.ctx = ctx;
954 s = e->v.Tuple.elts;
955 }
956 else {
957 expr_name = "()";
958 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000959 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000960 case Lambda_kind:
961 expr_name = "lambda";
962 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000965 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000966 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000968 case UnaryOp_kind:
969 expr_name = "operator";
970 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000972 expr_name = "generator expression";
973 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000974 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500975 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000976 expr_name = "yield expression";
977 break;
Yury Selivanov75445082015-05-11 22:57:16 -0400978 case Await_kind:
979 expr_name = "await expression";
980 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000981 case ListComp_kind:
982 expr_name = "list comprehension";
983 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000984 case SetComp_kind:
985 expr_name = "set comprehension";
986 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000987 case DictComp_kind:
988 expr_name = "dict comprehension";
989 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000990 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000991 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 case Num_kind:
993 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500994 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -0400995 case JoinedStr_kind:
996 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000997 expr_name = "literal";
998 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -0500999 case NameConstant_kind:
1000 expr_name = "keyword";
1001 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001002 case Ellipsis_kind:
1003 expr_name = "Ellipsis";
1004 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001005 case Compare_kind:
1006 expr_name = "comparison";
1007 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008 case IfExp_kind:
1009 expr_name = "conditional expression";
1010 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001011 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 PyErr_Format(PyExc_SystemError,
1013 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001014 e->kind, e->lineno);
1015 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001017 /* Check for error string set by switch */
1018 if (expr_name) {
1019 char buf[300];
1020 PyOS_snprintf(buf, sizeof(buf),
1021 "can't %s %s",
1022 ctx == Store ? "assign to" : "delete",
1023 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001024 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001025 }
1026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 */
1030 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001031 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032
Thomas Wouters89f507f2006-12-13 04:49:30 +00001033 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001034 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001035 return 0;
1036 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 }
1038 return 1;
1039}
1040
1041static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001042ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043{
1044 REQ(n, augassign);
1045 n = CHILD(n, 0);
1046 switch (STR(n)[0]) {
1047 case '+':
1048 return Add;
1049 case '-':
1050 return Sub;
1051 case '/':
1052 if (STR(n)[1] == '/')
1053 return FloorDiv;
1054 else
1055 return Div;
1056 case '%':
1057 return Mod;
1058 case '<':
1059 return LShift;
1060 case '>':
1061 return RShift;
1062 case '&':
1063 return BitAnd;
1064 case '^':
1065 return BitXor;
1066 case '|':
1067 return BitOr;
1068 case '*':
1069 if (STR(n)[1] == '*')
1070 return Pow;
1071 else
1072 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001073 case '@':
1074 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001076 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001077 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 }
1079}
1080
1081static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001082ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001084 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 |'is' 'not'
1086 */
1087 REQ(n, comp_op);
1088 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001089 n = CHILD(n, 0);
1090 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 case LESS:
1092 return Lt;
1093 case GREATER:
1094 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001095 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 return Eq;
1097 case LESSEQUAL:
1098 return LtE;
1099 case GREATEREQUAL:
1100 return GtE;
1101 case NOTEQUAL:
1102 return NotEq;
1103 case NAME:
1104 if (strcmp(STR(n), "in") == 0)
1105 return In;
1106 if (strcmp(STR(n), "is") == 0)
1107 return Is;
1108 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001109 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001111 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001112 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 }
1114 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001115 /* handle "not in" and "is not" */
1116 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 case NAME:
1118 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1119 return NotIn;
1120 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1121 return IsNot;
1122 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001123 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001126 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 }
Neal Norwitz79792652005-11-14 04:25:03 +00001128 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001130 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131}
1132
1133static asdl_seq *
1134seq_for_testlist(struct compiling *c, const node *n)
1135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001137 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1138 */
Armin Rigo31441302005-10-21 12:57:31 +00001139 asdl_seq *seq;
1140 expr_ty expression;
1141 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001142 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001144 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 if (!seq)
1146 return NULL;
1147
1148 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001150 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151
Benjamin Peterson4905e802009-09-27 02:43:28 +00001152 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001153 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155
1156 assert(i / 2 < seq->size);
1157 asdl_seq_SET(seq, i / 2, expression);
1158 }
1159 return seq;
1160}
1161
Neal Norwitzc1505362006-12-28 06:47:50 +00001162static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001163ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001164{
1165 identifier name;
1166 expr_ty annotation = NULL;
1167 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001168 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001169
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001170 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001171 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001172 name = NEW_IDENTIFIER(ch);
1173 if (!name)
1174 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001175 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001176 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001177
1178 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1179 annotation = ast_for_expr(c, CHILD(n, 2));
1180 if (!annotation)
1181 return NULL;
1182 }
1183
Victor Stinnerc106c682015-11-06 17:01:48 +01001184 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001185 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001186 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001187 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188}
1189
Guido van Rossum4f72a782006-10-27 23:31:49 +00001190/* returns -1 if failed to handle keyword only arguments
1191 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001192 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001193 ^^^
1194 start pointing here
1195 */
1196static int
1197handle_keywordonly_args(struct compiling *c, const node *n, int start,
1198 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1199{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001200 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001201 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001202 expr_ty expression, annotation;
1203 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001204 int i = start;
1205 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001206
1207 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001208 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001209 return -1;
1210 }
1211 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001212 while (i < NCH(n)) {
1213 ch = CHILD(n, i);
1214 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001215 case vfpdef:
1216 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001217 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001218 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001219 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001220 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001221 asdl_seq_SET(kwdefaults, j, expression);
1222 i += 2; /* '=' and test */
1223 }
1224 else { /* setting NULL if no default value exists */
1225 asdl_seq_SET(kwdefaults, j, NULL);
1226 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001227 if (NCH(ch) == 3) {
1228 /* ch is NAME ':' test */
1229 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001230 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001231 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001232 }
1233 else {
1234 annotation = NULL;
1235 }
1236 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001237 argname = NEW_IDENTIFIER(ch);
1238 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001239 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001240 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001241 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001242 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1243 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001244 if (!arg)
1245 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001246 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001247 i += 2; /* the name and the comma */
1248 break;
1249 case DOUBLESTAR:
1250 return i;
1251 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001252 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001253 goto error;
1254 }
1255 }
1256 return i;
1257 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001259}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260
Jeremy Hyltona8293132006-02-28 17:58:27 +00001261/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262
1263static arguments_ty
1264ast_for_arguments(struct compiling *c, const node *n)
1265{
Neal Norwitzc1505362006-12-28 06:47:50 +00001266 /* This function handles both typedargslist (function definition)
1267 and varargslist (lambda definition).
1268
1269 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001270 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1271 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1272 | '**' tfpdef [',']]]
1273 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1274 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001275 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001276 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1277 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1278 | '**' vfpdef [',']]]
1279 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1280 | '**' vfpdef [',']
1281 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001282 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001283
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001285 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1286 int nposdefaults = 0, found_default = 0;
1287 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001288 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001289 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 node *ch;
1291
1292 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001293 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001294 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001295 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001297 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298
Jeremy Hyltone921e022008-07-17 16:37:17 +00001299 /* First count the number of positional args & defaults. The
1300 variable i is the loop index for this for loop and the next.
1301 The next loop picks up where the first leaves off.
1302 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001304 ch = CHILD(n, i);
1305 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001306 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001307 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001308 if (i < NCH(n) && /* skip argument following star */
1309 (TYPE(CHILD(n, i)) == tfpdef ||
1310 TYPE(CHILD(n, i)) == vfpdef)) {
1311 i++;
1312 }
1313 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001314 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001315 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001316 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001317 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 defaults for keyword only args */
1321 for ( ; i < NCH(n); ++i) {
1322 ch = CHILD(n, i);
1323 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001324 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001325 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001326 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001328 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001329 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001330 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001332 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001334 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001336 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 since we set NULL as default for keyword only argument w/o default
1339 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001340 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001341 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001342 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001343 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001344
1345 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001346 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001347 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001350 /* tfpdef: NAME [':' test]
1351 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 */
1353 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001354 j = 0; /* index for defaults */
1355 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001357 ch = CHILD(n, i);
1358 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001359 case tfpdef:
1360 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1362 anything other than EQUAL or a comma? */
1363 /* XXX Should NCH(n) check be made a separate check? */
1364 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001365 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1366 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001367 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001368 assert(posdefaults != NULL);
1369 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001374 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001376 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001377 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001378 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001379 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001380 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001381 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 i += 2; /* the name and the comma */
1383 break;
1384 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001385 if (i+1 >= NCH(n) ||
1386 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001387 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001388 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001389 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001390 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001391 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001392 if (TYPE(ch) == COMMA) {
1393 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 i += 2; /* now follows keyword only arguments */
1395 res = handle_keywordonly_args(c, n, i,
1396 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001397 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 i = res; /* res has new position to process */
1399 }
1400 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001401 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001402 if (!vararg)
1403 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001404
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001406 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1407 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001408 int res = 0;
1409 res = handle_keywordonly_args(c, n, i,
1410 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001411 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001412 i = res; /* res has new position to process */
1413 }
1414 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 break;
1416 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001417 ch = CHILD(n, i+1); /* tfpdef */
1418 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001419 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001420 if (!kwarg)
1421 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 i += 3;
1423 break;
1424 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001425 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 "unexpected node in varargslist: %d @ %d",
1427 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001428 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001429 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001431 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432}
1433
1434static expr_ty
1435ast_for_dotted_name(struct compiling *c, const node *n)
1436{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001437 expr_ty e;
1438 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001439 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 int i;
1441
1442 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001443
1444 lineno = LINENO(n);
1445 col_offset = n->n_col_offset;
1446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 id = NEW_IDENTIFIER(CHILD(n, 0));
1448 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001449 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001450 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001452 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453
1454 for (i = 2; i < NCH(n); i+=2) {
1455 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001456 if (!id)
1457 return NULL;
1458 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1459 if (!e)
1460 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 }
1462
1463 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464}
1465
1466static expr_ty
1467ast_for_decorator(struct compiling *c, const node *n)
1468{
1469 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1470 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001471 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001474 REQ(CHILD(n, 0), AT);
1475 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1478 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001479 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001482 d = name_expr;
1483 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 }
1485 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001486 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001487 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001488 if (!d)
1489 return NULL;
1490 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 }
1492 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001493 d = ast_for_call(c, CHILD(n, 3), name_expr);
1494 if (!d)
1495 return NULL;
1496 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 }
1498
1499 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
1502static asdl_seq*
1503ast_for_decorators(struct compiling *c, const node *n)
1504{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001505 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001506 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001510 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 if (!decorator_seq)
1512 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001515 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001516 if (!d)
1517 return NULL;
1518 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 }
1520 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521}
1522
1523static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001524ast_for_funcdef_impl(struct compiling *c, const node *n,
1525 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001527 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001528 identifier name;
1529 arguments_ty args;
1530 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001531 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001532 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533
1534 REQ(n, funcdef);
1535
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 name = NEW_IDENTIFIER(CHILD(n, name_i));
1537 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001538 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001539 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001540 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1542 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001543 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001544 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1545 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1546 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001547 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001548 name_i += 2;
1549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 body = ast_for_suite(c, CHILD(n, name_i + 3));
1551 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001552 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553
Yury Selivanov75445082015-05-11 22:57:16 -04001554 if (is_async)
1555 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1556 LINENO(n),
1557 n->n_col_offset, c->c_arena);
1558 else
1559 return FunctionDef(name, args, body, decorator_seq, returns,
1560 LINENO(n),
1561 n->n_col_offset, c->c_arena);
1562}
1563
1564static stmt_ty
1565ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1566{
1567 /* async_funcdef: ASYNC funcdef */
1568 REQ(n, async_funcdef);
1569 REQ(CHILD(n, 0), ASYNC);
1570 REQ(CHILD(n, 1), funcdef);
1571
1572 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1573 1 /* is_async */);
1574}
1575
1576static stmt_ty
1577ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1578{
1579 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1580 return ast_for_funcdef_impl(c, n, decorator_seq,
1581 0 /* is_async */);
1582}
1583
1584
1585static stmt_ty
1586ast_for_async_stmt(struct compiling *c, const node *n)
1587{
1588 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1589 REQ(n, async_stmt);
1590 REQ(CHILD(n, 0), ASYNC);
1591
1592 switch (TYPE(CHILD(n, 1))) {
1593 case funcdef:
1594 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1595 1 /* is_async */);
1596 case with_stmt:
1597 return ast_for_with_stmt(c, CHILD(n, 1),
1598 1 /* is_async */);
1599
1600 case for_stmt:
1601 return ast_for_for_stmt(c, CHILD(n, 1),
1602 1 /* is_async */);
1603
1604 default:
1605 PyErr_Format(PyExc_SystemError,
1606 "invalid async stament: %s",
1607 STR(CHILD(n, 1)));
1608 return NULL;
1609 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610}
1611
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001612static stmt_ty
1613ast_for_decorated(struct compiling *c, const node *n)
1614{
Yury Selivanov75445082015-05-11 22:57:16 -04001615 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001616 stmt_ty thing = NULL;
1617 asdl_seq *decorator_seq = NULL;
1618
1619 REQ(n, decorated);
1620
1621 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1622 if (!decorator_seq)
1623 return NULL;
1624
1625 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001626 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001628
1629 if (TYPE(CHILD(n, 1)) == funcdef) {
1630 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1631 } else if (TYPE(CHILD(n, 1)) == classdef) {
1632 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001633 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1634 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001635 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001636 /* we count the decorators in when talking about the class' or
1637 * function's line number */
1638 if (thing) {
1639 thing->lineno = LINENO(n);
1640 thing->col_offset = n->n_col_offset;
1641 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001642 return thing;
1643}
1644
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645static expr_ty
1646ast_for_lambdef(struct compiling *c, const node *n)
1647{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001648 /* lambdef: 'lambda' [varargslist] ':' test
1649 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 arguments_ty args;
1651 expr_ty expression;
1652
1653 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001654 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 if (!args)
1656 return NULL;
1657 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001658 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 }
1661 else {
1662 args = ast_for_arguments(c, CHILD(n, 1));
1663 if (!args)
1664 return NULL;
1665 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001666 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 }
1669
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001670 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671}
1672
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001673static expr_ty
1674ast_for_ifexpr(struct compiling *c, const node *n)
1675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001677 expr_ty expression, body, orelse;
1678
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001679 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001680 body = ast_for_expr(c, CHILD(n, 0));
1681 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001682 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001683 expression = ast_for_expr(c, CHILD(n, 2));
1684 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001685 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001686 orelse = ast_for_expr(c, CHILD(n, 4));
1687 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001688 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001689 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1690 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001691}
1692
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001694 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695
Nick Coghlan650f0d02007-04-15 12:05:43 +00001696 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697*/
1698
1699static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001700count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001702 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703
Guido van Rossumd8faa362007-04-27 19:54:29 +00001704 count_comp_for:
1705 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001706 REQ(n, comp_for);
1707 if (NCH(n) == 5)
1708 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001709 else
1710 return n_fors;
1711 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001712 REQ(n, comp_iter);
1713 n = CHILD(n, 0);
1714 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001715 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001716 else if (TYPE(n) == comp_if) {
1717 if (NCH(n) == 3) {
1718 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001719 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001720 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001721 else
1722 return n_fors;
1723 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001724
Guido van Rossumd8faa362007-04-27 19:54:29 +00001725 /* Should never be reached */
1726 PyErr_SetString(PyExc_SystemError,
1727 "logic error in count_comp_fors");
1728 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729}
1730
Nick Coghlan650f0d02007-04-15 12:05:43 +00001731/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732
Nick Coghlan650f0d02007-04-15 12:05:43 +00001733 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734*/
1735
1736static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001737count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001739 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740
Guido van Rossumd8faa362007-04-27 19:54:29 +00001741 while (1) {
1742 REQ(n, comp_iter);
1743 if (TYPE(CHILD(n, 0)) == comp_for)
1744 return n_ifs;
1745 n = CHILD(n, 0);
1746 REQ(n, comp_if);
1747 n_ifs++;
1748 if (NCH(n) == 2)
1749 return n_ifs;
1750 n = CHILD(n, 2);
1751 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752}
1753
Guido van Rossum992d4a32007-07-11 13:09:30 +00001754static asdl_seq *
1755ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001758 asdl_seq *comps;
1759
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001760 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 if (n_fors == -1)
1762 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001763
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001764 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001765 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001769 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001771 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001772 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773
Guido van Rossum992d4a32007-07-11 13:09:30 +00001774 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775
Guido van Rossum992d4a32007-07-11 13:09:30 +00001776 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001777 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001778 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001780 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001781 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001783
Thomas Wouters89f507f2006-12-13 04:49:30 +00001784 /* Check the # of children rather than the length of t, since
1785 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001786 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001787 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001788 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001790 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1791 c->c_arena),
1792 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001793 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001795
Guido van Rossum992d4a32007-07-11 13:09:30 +00001796 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 int j, n_ifs;
1798 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799
Guido van Rossum992d4a32007-07-11 13:09:30 +00001800 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001801 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001802 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001804
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001805 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001806 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001808
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001810 REQ(n, comp_iter);
1811 n = CHILD(n, 0);
1812 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813
Guido van Rossum992d4a32007-07-11 13:09:30 +00001814 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001815 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001816 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001817 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001818 if (NCH(n) == 3)
1819 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001821 /* on exit, must guarantee that n is a comp_for */
1822 if (TYPE(n) == comp_iter)
1823 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001824 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001826 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001828 return comps;
1829}
1830
1831static expr_ty
1832ast_for_itercomp(struct compiling *c, const node *n, int type)
1833{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001834 /* testlist_comp: (test|star_expr)
1835 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001836 expr_ty elt;
1837 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001838 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839
Guido van Rossum992d4a32007-07-11 13:09:30 +00001840 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001842 ch = CHILD(n, 0);
1843 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001844 if (!elt)
1845 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001846 if (elt->kind == Starred_kind) {
1847 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1848 return NULL;
1849 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850
Guido van Rossum992d4a32007-07-11 13:09:30 +00001851 comps = ast_for_comprehension(c, CHILD(n, 1));
1852 if (!comps)
1853 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001854
1855 if (type == COMP_GENEXP)
1856 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1857 else if (type == COMP_LISTCOMP)
1858 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1859 else if (type == COMP_SETCOMP)
1860 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1861 else
1862 /* Should never happen */
1863 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864}
1865
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001866/* Fills in the key, value pair corresponding to the dict element. In case
1867 * of an unpacking, key is NULL. *i is advanced by the number of ast
1868 * elements. Iff successful, nonzero is returned.
1869 */
1870static int
1871ast_for_dictelement(struct compiling *c, const node *n, int *i,
1872 expr_ty *key, expr_ty *value)
1873{
1874 expr_ty expression;
1875 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1876 assert(NCH(n) - *i >= 2);
1877
1878 expression = ast_for_expr(c, CHILD(n, *i + 1));
1879 if (!expression)
1880 return 0;
1881 *key = NULL;
1882 *value = expression;
1883
1884 *i += 2;
1885 }
1886 else {
1887 assert(NCH(n) - *i >= 3);
1888
1889 expression = ast_for_expr(c, CHILD(n, *i));
1890 if (!expression)
1891 return 0;
1892 *key = expression;
1893
1894 REQ(CHILD(n, *i + 1), COLON);
1895
1896 expression = ast_for_expr(c, CHILD(n, *i + 2));
1897 if (!expression)
1898 return 0;
1899 *value = expression;
1900
1901 *i += 3;
1902 }
1903 return 1;
1904}
1905
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907ast_for_dictcomp(struct compiling *c, const node *n)
1908{
1909 expr_ty key, value;
1910 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001911 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001913 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001914 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001915 assert(key);
1916 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001918 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001919 if (!comps)
1920 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921
Guido van Rossum992d4a32007-07-11 13:09:30 +00001922 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1923}
1924
1925static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001926ast_for_dictdisplay(struct compiling *c, const node *n)
1927{
1928 int i;
1929 int j;
1930 int size;
1931 asdl_seq *keys, *values;
1932
1933 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1934 keys = _Py_asdl_seq_new(size, c->c_arena);
1935 if (!keys)
1936 return NULL;
1937
1938 values = _Py_asdl_seq_new(size, c->c_arena);
1939 if (!values)
1940 return NULL;
1941
1942 j = 0;
1943 for (i = 0; i < NCH(n); i++) {
1944 expr_ty key, value;
1945
1946 if (!ast_for_dictelement(c, n, &i, &key, &value))
1947 return NULL;
1948 asdl_seq_SET(keys, j, key);
1949 asdl_seq_SET(values, j, value);
1950
1951 j++;
1952 }
1953 keys->size = j;
1954 values->size = j;
1955 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1956}
1957
1958static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001959ast_for_genexp(struct compiling *c, const node *n)
1960{
1961 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001962 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001963}
1964
1965static expr_ty
1966ast_for_listcomp(struct compiling *c, const node *n)
1967{
1968 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001969 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001970}
1971
1972static expr_ty
1973ast_for_setcomp(struct compiling *c, const node *n)
1974{
1975 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001976 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001977}
1978
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001979static expr_ty
1980ast_for_setdisplay(struct compiling *c, const node *n)
1981{
1982 int i;
1983 int size;
1984 asdl_seq *elts;
1985
1986 assert(TYPE(n) == (dictorsetmaker));
1987 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
1988 elts = _Py_asdl_seq_new(size, c->c_arena);
1989 if (!elts)
1990 return NULL;
1991 for (i = 0; i < NCH(n); i += 2) {
1992 expr_ty expression;
1993 expression = ast_for_expr(c, CHILD(n, i));
1994 if (!expression)
1995 return NULL;
1996 asdl_seq_SET(elts, i / 2, expression);
1997 }
1998 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1999}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002000
2001static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002ast_for_atom(struct compiling *c, const node *n)
2003{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002004 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2005 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002006 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 */
2008 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002011 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002012 PyObject *name;
2013 const char *s = STR(ch);
2014 size_t len = strlen(s);
2015 if (len >= 4 && len <= 5) {
2016 if (!strcmp(s, "None"))
2017 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2018 if (!strcmp(s, "True"))
2019 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2020 if (!strcmp(s, "False"))
2021 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2022 }
2023 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002024 if (!name)
2025 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002026 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002027 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2028 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002030 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002031 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002032 const char *errtype = NULL;
2033 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2034 errtype = "unicode error";
2035 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2036 errtype = "value error";
2037 if (errtype) {
2038 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002039 PyObject *type, *value, *tback, *errstr;
2040 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002041 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002042 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002043 char *s = _PyUnicode_AsString(errstr);
2044 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002045 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002046 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002047 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002048 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002049 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002050 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002051 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002052 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002053 Py_XDECREF(tback);
2054 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002055 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002056 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002057 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 }
2059 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002060 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002061 if (!pynum)
2062 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002063
Victor Stinner43d81952013-07-17 00:57:58 +02002064 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2065 Py_DECREF(pynum);
2066 return NULL;
2067 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002068 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 }
Georg Brandldde00282007-03-18 19:01:53 +00002070 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002071 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002073 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074
Thomas Wouters89f507f2006-12-13 04:49:30 +00002075 if (TYPE(ch) == RPAR)
2076 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077
Thomas Wouters89f507f2006-12-13 04:49:30 +00002078 if (TYPE(ch) == yield_expr)
2079 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002082 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002083 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002084
Nick Coghlan650f0d02007-04-15 12:05:43 +00002085 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002087 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088
Thomas Wouters89f507f2006-12-13 04:49:30 +00002089 if (TYPE(ch) == RSQB)
2090 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091
Nick Coghlan650f0d02007-04-15 12:05:43 +00002092 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002093 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2094 asdl_seq *elts = seq_for_testlist(c, ch);
2095 if (!elts)
2096 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097
Thomas Wouters89f507f2006-12-13 04:49:30 +00002098 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2099 }
2100 else
2101 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002103 /* dictorsetmaker: ( ((test ':' test | '**' test)
2104 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2105 * ((test | '*' test)
2106 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002107 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002108 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002109 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002110 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002111 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002112 }
2113 else {
2114 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2115 if (NCH(ch) == 1 ||
2116 (NCH(ch) > 1 &&
2117 TYPE(CHILD(ch, 1)) == COMMA)) {
2118 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002119 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002120 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002121 else if (NCH(ch) > 1 &&
2122 TYPE(CHILD(ch, 1)) == comp_for) {
2123 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002124 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002125 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002126 else if (NCH(ch) > 3 - is_dict &&
2127 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2128 /* It's a dictionary comprehension. */
2129 if (is_dict) {
2130 ast_error(c, n, "dict unpacking cannot be used in "
2131 "dict comprehension");
2132 return NULL;
2133 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002134 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002135 }
2136 else {
2137 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002138 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002139 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002140 if (res) {
2141 res->lineno = LINENO(n);
2142 res->col_offset = n->n_col_offset;
2143 }
2144 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002145 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002148 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2149 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 }
2151}
2152
2153static slice_ty
2154ast_for_slice(struct compiling *c, const node *n)
2155{
2156 node *ch;
2157 expr_ty lower = NULL, upper = NULL, step = NULL;
2158
2159 REQ(n, subscript);
2160
2161 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002162 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 sliceop: ':' [test]
2164 */
2165 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 if (NCH(n) == 1 && TYPE(ch) == test) {
2167 /* 'step' variable hold no significance in terms of being used over
2168 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 if (!step)
2171 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172
Thomas Wouters89f507f2006-12-13 04:49:30 +00002173 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 }
2175
2176 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002177 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 if (!lower)
2179 return NULL;
2180 }
2181
2182 /* If there's an upper bound it's in the second or third position. */
2183 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002184 if (NCH(n) > 1) {
2185 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186
Thomas Wouters89f507f2006-12-13 04:49:30 +00002187 if (TYPE(n2) == test) {
2188 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 if (!upper)
2190 return NULL;
2191 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002194 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195
Thomas Wouters89f507f2006-12-13 04:49:30 +00002196 if (TYPE(n2) == test) {
2197 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 if (!upper)
2199 return NULL;
2200 }
2201 }
2202
2203 ch = CHILD(n, NCH(n) - 1);
2204 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002205 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002206 ch = CHILD(ch, 1);
2207 if (TYPE(ch) == test) {
2208 step = ast_for_expr(c, ch);
2209 if (!step)
2210 return NULL;
2211 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 }
2213 }
2214
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002215 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216}
2217
2218static expr_ty
2219ast_for_binop(struct compiling *c, const node *n)
2220{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002221 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002223 BinOp(BinOp(A, op, B), op, C).
2224 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225
Guido van Rossumd8faa362007-04-27 19:54:29 +00002226 int i, nops;
2227 expr_ty expr1, expr2, result;
2228 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229
Guido van Rossumd8faa362007-04-27 19:54:29 +00002230 expr1 = ast_for_expr(c, CHILD(n, 0));
2231 if (!expr1)
2232 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233
Guido van Rossumd8faa362007-04-27 19:54:29 +00002234 expr2 = ast_for_expr(c, CHILD(n, 2));
2235 if (!expr2)
2236 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237
Guido van Rossumd8faa362007-04-27 19:54:29 +00002238 newoperator = get_operator(CHILD(n, 1));
2239 if (!newoperator)
2240 return NULL;
2241
2242 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2243 c->c_arena);
2244 if (!result)
2245 return NULL;
2246
2247 nops = (NCH(n) - 1) / 2;
2248 for (i = 1; i < nops; i++) {
2249 expr_ty tmp_result, tmp;
2250 const node* next_oper = CHILD(n, i * 2 + 1);
2251
2252 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002253 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 return NULL;
2255
Guido van Rossumd8faa362007-04-27 19:54:29 +00002256 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2257 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 return NULL;
2259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002261 LINENO(next_oper), next_oper->n_col_offset,
2262 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002264 return NULL;
2265 result = tmp_result;
2266 }
2267 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268}
2269
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002270static expr_ty
2271ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002274 subscriptlist: subscript (',' subscript)* [',']
2275 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2276 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002277 REQ(n, trailer);
2278 if (TYPE(CHILD(n, 0)) == LPAR) {
2279 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002280 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002281 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002282 else
2283 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002284 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002285 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002286 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2287 if (!attr_id)
2288 return NULL;
2289 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002290 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002291 }
2292 else {
2293 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002294 REQ(CHILD(n, 2), RSQB);
2295 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002296 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002297 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2298 if (!slc)
2299 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002300 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2301 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002302 }
2303 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002305 by treating the sequence as a tuple literal if there are
2306 no slice features.
2307 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002308 int j;
2309 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002310 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002311 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002312 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002313 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002314 if (!slices)
2315 return NULL;
2316 for (j = 0; j < NCH(n); j += 2) {
2317 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002318 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002319 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002320 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002321 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002322 asdl_seq_SET(slices, j / 2, slc);
2323 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002324 if (!simple) {
2325 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002326 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002327 }
2328 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002329 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002330 if (!elts)
2331 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002332 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2333 slc = (slice_ty)asdl_seq_GET(slices, j);
2334 assert(slc->kind == Index_kind && slc->v.Index.value);
2335 asdl_seq_SET(elts, j, slc->v.Index.value);
2336 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002337 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002338 if (!e)
2339 return NULL;
2340 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002341 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002342 }
2343 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002344}
2345
2346static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002347ast_for_factor(struct compiling *c, const node *n)
2348{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002349 expr_ty expression;
2350
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002351 expression = ast_for_expr(c, CHILD(n, 1));
2352 if (!expression)
2353 return NULL;
2354
2355 switch (TYPE(CHILD(n, 0))) {
2356 case PLUS:
2357 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2358 c->c_arena);
2359 case MINUS:
2360 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2361 c->c_arena);
2362 case TILDE:
2363 return UnaryOp(Invert, expression, LINENO(n),
2364 n->n_col_offset, c->c_arena);
2365 }
2366 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2367 TYPE(CHILD(n, 0)));
2368 return NULL;
2369}
2370
2371static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002372ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002373{
Yury Selivanov75445082015-05-11 22:57:16 -04002374 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002375 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002376
2377 REQ(n, atom_expr);
2378 nch = NCH(n);
2379
2380 if (TYPE(CHILD(n, 0)) == AWAIT) {
2381 start = 1;
2382 assert(nch > 1);
2383 }
2384
2385 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002386 if (!e)
2387 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002388 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002389 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002390 if (start && nch == 2) {
2391 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2392 }
2393
2394 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002395 node *ch = CHILD(n, i);
2396 if (TYPE(ch) != trailer)
2397 break;
2398 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002399 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002400 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002401 tmp->lineno = e->lineno;
2402 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002403 e = tmp;
2404 }
Yury Selivanov75445082015-05-11 22:57:16 -04002405
2406 if (start) {
2407 /* there was an AWAIT */
2408 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2409 }
2410 else {
2411 return e;
2412 }
2413}
2414
2415static expr_ty
2416ast_for_power(struct compiling *c, const node *n)
2417{
2418 /* power: atom trailer* ('**' factor)*
2419 */
2420 expr_ty e;
2421 REQ(n, power);
2422 e = ast_for_atom_expr(c, CHILD(n, 0));
2423 if (!e)
2424 return NULL;
2425 if (NCH(n) == 1)
2426 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002427 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2428 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002429 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002430 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002431 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002432 }
2433 return e;
2434}
2435
Guido van Rossum0368b722007-05-11 16:50:42 +00002436static expr_ty
2437ast_for_starred(struct compiling *c, const node *n)
2438{
2439 expr_ty tmp;
2440 REQ(n, star_expr);
2441
2442 tmp = ast_for_expr(c, CHILD(n, 1));
2443 if (!tmp)
2444 return NULL;
2445
2446 /* The Load context is changed later. */
2447 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2448}
2449
2450
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451/* Do not name a variable 'expr'! Will cause a compile error.
2452*/
2453
2454static expr_ty
2455ast_for_expr(struct compiling *c, const node *n)
2456{
2457 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002458 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002459 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 and_test: not_test ('and' not_test)*
2462 not_test: 'not' not_test | comparison
2463 comparison: expr (comp_op expr)*
2464 expr: xor_expr ('|' xor_expr)*
2465 xor_expr: and_expr ('^' and_expr)*
2466 and_expr: shift_expr ('&' shift_expr)*
2467 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2468 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002469 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002471 power: atom_expr ['**' factor]
2472 atom_expr: [AWAIT] atom trailer*
2473 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 */
2475
2476 asdl_seq *seq;
2477 int i;
2478
2479 loop:
2480 switch (TYPE(n)) {
2481 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002482 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002483 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002484 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002486 else if (NCH(n) > 1)
2487 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002488 /* Fallthrough */
2489 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 case and_test:
2491 if (NCH(n) == 1) {
2492 n = CHILD(n, 0);
2493 goto loop;
2494 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002495 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 if (!seq)
2497 return NULL;
2498 for (i = 0; i < NCH(n); i += 2) {
2499 expr_ty e = ast_for_expr(c, CHILD(n, i));
2500 if (!e)
2501 return NULL;
2502 asdl_seq_SET(seq, i / 2, e);
2503 }
2504 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002505 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2506 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002507 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002508 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 case not_test:
2510 if (NCH(n) == 1) {
2511 n = CHILD(n, 0);
2512 goto loop;
2513 }
2514 else {
2515 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2516 if (!expression)
2517 return NULL;
2518
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002519 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2520 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 }
2522 case comparison:
2523 if (NCH(n) == 1) {
2524 n = CHILD(n, 0);
2525 goto loop;
2526 }
2527 else {
2528 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002529 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002530 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002531 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 if (!ops)
2533 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002534 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 return NULL;
2537 }
2538 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002539 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002541 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002542 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002544 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545
2546 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002547 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002551 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 asdl_seq_SET(cmps, i / 2, expression);
2553 }
2554 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002555 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002557 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002559 return Compare(expression, ops, cmps, LINENO(n),
2560 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
2562 break;
2563
Guido van Rossum0368b722007-05-11 16:50:42 +00002564 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 /* The next five cases all handle BinOps. The main body of code
2567 is the same in each case, but the switch turned inside out to
2568 reuse the code for each type of operator.
2569 */
2570 case expr:
2571 case xor_expr:
2572 case and_expr:
2573 case shift_expr:
2574 case arith_expr:
2575 case term:
2576 if (NCH(n) == 1) {
2577 n = CHILD(n, 0);
2578 goto loop;
2579 }
2580 return ast_for_binop(c, n);
2581 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002582 node *an = NULL;
2583 node *en = NULL;
2584 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002585 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002586 if (NCH(n) > 1)
2587 an = CHILD(n, 1); /* yield_arg */
2588 if (an) {
2589 en = CHILD(an, NCH(an) - 1);
2590 if (NCH(an) == 2) {
2591 is_from = 1;
2592 exp = ast_for_expr(c, en);
2593 }
2594 else
2595 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002596 if (!exp)
2597 return NULL;
2598 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002599 if (is_from)
2600 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2601 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002602 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002603 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 if (NCH(n) == 1) {
2605 n = CHILD(n, 0);
2606 goto loop;
2607 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002608 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002609 case power:
2610 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002612 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 return NULL;
2614 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002615 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 return NULL;
2617}
2618
2619static expr_ty
2620ast_for_call(struct compiling *c, const node *n, expr_ty func)
2621{
2622 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002623 arglist: argument (',' argument)* [',']
2624 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 */
2626
2627 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002628 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002629 asdl_seq *args;
2630 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631
2632 REQ(n, arglist);
2633
2634 nargs = 0;
2635 nkeywords = 0;
2636 ngens = 0;
2637 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002638 node *ch = CHILD(n, i);
2639 if (TYPE(ch) == argument) {
2640 if (NCH(ch) == 1)
2641 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002642 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002643 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002644 else if (TYPE(CHILD(ch, 0)) == STAR)
2645 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002647 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002648 nkeywords++;
2649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 }
2651 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002652 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002653 "if not sole argument");
2654 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 }
2656
2657 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002658 ast_error(c, n, "more than 255 arguments");
2659 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 }
2661
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002662 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002664 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002665 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002667 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002668
2669 nargs = 0; /* positional arguments + iterable argument unpackings */
2670 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2671 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002673 node *ch = CHILD(n, i);
2674 if (TYPE(ch) == argument) {
2675 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002676 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002677 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002678 /* a positional argument */
2679 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002680 if (ndoublestars) {
2681 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002682 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002683 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002684 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002685 else {
2686 ast_error(c, chch,
2687 "positional argument follows "
2688 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002689 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002690 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002691 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002692 e = ast_for_expr(c, chch);
2693 if (!e)
2694 return NULL;
2695 asdl_seq_SET(args, nargs++, e);
2696 }
2697 else if (TYPE(chch) == STAR) {
2698 /* an iterable argument unpacking */
2699 expr_ty starred;
2700 if (ndoublestars) {
2701 ast_error(c, chch,
2702 "iterable argument unpacking follows "
2703 "keyword argument unpacking");
2704 return NULL;
2705 }
2706 e = ast_for_expr(c, CHILD(ch, 1));
2707 if (!e)
2708 return NULL;
2709 starred = Starred(e, Load, LINENO(chch),
2710 chch->n_col_offset,
2711 c->c_arena);
2712 if (!starred)
2713 return NULL;
2714 asdl_seq_SET(args, nargs++, starred);
2715
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 }
2717 else if (TYPE(chch) == DOUBLESTAR) {
2718 /* a keyword argument unpacking */
2719 keyword_ty kw;
2720 i++;
2721 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002723 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002724 kw = keyword(NULL, e, c->c_arena);
2725 asdl_seq_SET(keywords, nkeywords++, kw);
2726 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002728 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002729 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002730 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002732 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002733 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002735 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002736 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002737 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002738 identifier key, tmp;
2739 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002741 /* chch is test, but must be an identifier? */
2742 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 /* f(lambda x: x[0] = 3) ends up getting parsed with
2746 * LHS test = lambda x: x[0], and RHS test = 3.
2747 * SF bug 132313 points out that complaining about a keyword
2748 * then is very confusing.
2749 */
2750 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002751 ast_error(c, chch,
2752 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002753 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002754 }
2755 else if (e->kind != Name_kind) {
2756 ast_error(c, chch,
2757 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002758 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002759 }
2760 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002761 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002763 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002764 for (k = 0; k < nkeywords; k++) {
2765 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002766 if (tmp && !PyUnicode_Compare(tmp, key)) {
2767 ast_error(c, chch,
2768 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002769 return NULL;
2770 }
2771 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002772 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002774 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002775 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002777 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002778 asdl_seq_SET(keywords, nkeywords++, kw);
2779 }
2780 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 }
2782
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002783 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784}
2785
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002787ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002789 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002790 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002792 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002793 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002794 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002795 }
2796 else {
2797 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002798 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002801 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 else {
2803 asdl_seq *tmp = seq_for_testlist(c, n);
2804 if (!tmp)
2805 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002806 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002808}
2809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810static stmt_ty
2811ast_for_expr_stmt(struct compiling *c, const node *n)
2812{
2813 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002816 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002817 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002818 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 test: ... here starts the operator precendence dance
2820 */
2821
2822 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002823 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 if (!e)
2825 return NULL;
2826
Thomas Wouters89f507f2006-12-13 04:49:30 +00002827 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 }
2829 else if (TYPE(CHILD(n, 1)) == augassign) {
2830 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002831 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002832 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833
Thomas Wouters89f507f2006-12-13 04:49:30 +00002834 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 if (!expr1)
2836 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002837 if(!set_context(c, expr1, Store, ch))
2838 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002839 /* set_context checks that most expressions are not the left side.
2840 Augmented assignments can only have a name, a subscript, or an
2841 attribute on the left, though, so we have to explicitly check for
2842 those. */
2843 switch (expr1->kind) {
2844 case Name_kind:
2845 case Attribute_kind:
2846 case Subscript_kind:
2847 break;
2848 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002849 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002850 return NULL;
2851 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852
Thomas Wouters89f507f2006-12-13 04:49:30 +00002853 ch = CHILD(n, 2);
2854 if (TYPE(ch) == testlist)
2855 expr2 = ast_for_testlist(c, ch);
2856 else
2857 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002858 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 return NULL;
2860
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002861 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002862 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 return NULL;
2864
Thomas Wouters89f507f2006-12-13 04:49:30 +00002865 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 }
2867 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002868 int i;
2869 asdl_seq *targets;
2870 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 expr_ty expression;
2872
Thomas Wouters89f507f2006-12-13 04:49:30 +00002873 /* a normal assignment */
2874 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002875 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002876 if (!targets)
2877 return NULL;
2878 for (i = 0; i < NCH(n) - 2; i += 2) {
2879 expr_ty e;
2880 node *ch = CHILD(n, i);
2881 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002882 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002883 return NULL;
2884 }
2885 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002889 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002890 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002891 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892
Thomas Wouters89f507f2006-12-13 04:49:30 +00002893 asdl_seq_SET(targets, i / 2, e);
2894 }
2895 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002896 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002897 expression = ast_for_testlist(c, value);
2898 else
2899 expression = ast_for_expr(c, value);
2900 if (!expression)
2901 return NULL;
2902 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904}
2905
Benjamin Peterson78565b22009-06-28 19:19:51 +00002906
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002908ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909{
2910 asdl_seq *seq;
2911 int i;
2912 expr_ty e;
2913
2914 REQ(n, exprlist);
2915
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002916 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002920 e = ast_for_expr(c, CHILD(n, i));
2921 if (!e)
2922 return NULL;
2923 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002924 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002925 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926 }
2927 return seq;
2928}
2929
2930static stmt_ty
2931ast_for_del_stmt(struct compiling *c, const node *n)
2932{
2933 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935 /* del_stmt: 'del' exprlist */
2936 REQ(n, del_stmt);
2937
2938 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2939 if (!expr_list)
2940 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002941 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942}
2943
2944static stmt_ty
2945ast_for_flow_stmt(struct compiling *c, const node *n)
2946{
2947 /*
2948 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2949 | yield_stmt
2950 break_stmt: 'break'
2951 continue_stmt: 'continue'
2952 return_stmt: 'return' [testlist]
2953 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002954 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 raise_stmt: 'raise' [test [',' test [',' test]]]
2956 */
2957 node *ch;
2958
2959 REQ(n, flow_stmt);
2960 ch = CHILD(n, 0);
2961 switch (TYPE(ch)) {
2962 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002963 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002965 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002967 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2968 if (!exp)
2969 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002970 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 }
2972 case return_stmt:
2973 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002974 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002976 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 if (!expression)
2978 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002979 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 }
2981 case raise_stmt:
2982 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002983 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2984 else if (NCH(ch) >= 2) {
2985 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2987 if (!expression)
2988 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002989 if (NCH(ch) == 4) {
2990 cause = ast_for_expr(c, CHILD(ch, 3));
2991 if (!cause)
2992 return NULL;
2993 }
2994 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 }
2996 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002997 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998 "unexpected flow_stmt: %d", TYPE(ch));
2999 return NULL;
3000 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003001
3002 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004}
3005
3006static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003007alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008{
3009 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003010 import_as_name: NAME ['as' NAME]
3011 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 dotted_name: NAME ('.' NAME)*
3013 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003014 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003015
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 loop:
3017 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003018 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003019 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003020 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003021 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003022 if (!name)
3023 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003024 if (NCH(n) == 3) {
3025 node *str_node = CHILD(n, 2);
3026 str = NEW_IDENTIFIER(str_node);
3027 if (!str)
3028 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003029 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003030 return NULL;
3031 }
3032 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003033 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003034 return NULL;
3035 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003036 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003037 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038 case dotted_as_name:
3039 if (NCH(n) == 1) {
3040 n = CHILD(n, 0);
3041 goto loop;
3042 }
3043 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003044 node *asname_node = CHILD(n, 2);
3045 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003046 if (!a)
3047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003049 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003050 if (!a->asname)
3051 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003052 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003053 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 return a;
3055 }
3056 break;
3057 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003058 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003059 node *name_node = CHILD(n, 0);
3060 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003061 if (!name)
3062 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003063 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003064 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003065 return alias(name, NULL, c->c_arena);
3066 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003067 else {
3068 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003069 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003070 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073
3074 len = 0;
3075 for (i = 0; i < NCH(n); i += 2)
3076 /* length of string plus one for the dot */
3077 len += strlen(STR(CHILD(n, i))) + 1;
3078 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003079 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 if (!str)
3081 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003082 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 if (!s)
3084 return NULL;
3085 for (i = 0; i < NCH(n); i += 2) {
3086 char *sch = STR(CHILD(n, i));
3087 strcpy(s, STR(CHILD(n, i)));
3088 s += strlen(sch);
3089 *s++ = '.';
3090 }
3091 --s;
3092 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3094 PyBytes_GET_SIZE(str),
3095 NULL);
3096 Py_DECREF(str);
3097 if (!uni)
3098 return NULL;
3099 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003100 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003101 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3102 Py_DECREF(str);
3103 return NULL;
3104 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003105 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 }
3107 break;
3108 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003109 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003110 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3111 Py_DECREF(str);
3112 return NULL;
3113 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003114 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003116 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 "unexpected import name: %d", TYPE(n));
3118 return NULL;
3119 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003120
3121 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 return NULL;
3123}
3124
3125static stmt_ty
3126ast_for_import_stmt(struct compiling *c, const node *n)
3127{
3128 /*
3129 import_stmt: import_name | import_from
3130 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003131 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3132 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003134 int lineno;
3135 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 int i;
3137 asdl_seq *aliases;
3138
3139 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003140 lineno = LINENO(n);
3141 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003143 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003145 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003146 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003147 if (!aliases)
3148 return NULL;
3149 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003150 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003151 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003153 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003155 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003157 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003159 int idx, ndots = 0;
3160 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003161 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003163 /* Count the number of dots (for relative imports) and check for the
3164 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003165 for (idx = 1; idx < NCH(n); idx++) {
3166 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003167 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3168 if (!mod)
3169 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003170 idx++;
3171 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003172 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003174 ndots += 3;
3175 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003176 } else if (TYPE(CHILD(n, idx)) != DOT) {
3177 break;
3178 }
3179 ndots++;
3180 }
3181 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003182 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003183 case STAR:
3184 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003185 n = CHILD(n, idx);
3186 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003187 break;
3188 case LPAR:
3189 /* from ... import (x, y, z) */
3190 n = CHILD(n, idx + 1);
3191 n_children = NCH(n);
3192 break;
3193 case import_as_names:
3194 /* from ... import x, y, z */
3195 n = CHILD(n, idx);
3196 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003197 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003198 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 " surrounding parentheses");
3200 return NULL;
3201 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003202 break;
3203 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003204 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003205 return NULL;
3206 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003208 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003209 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211
3212 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003213 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003214 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003215 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003217 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003219 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003220 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003221 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003222 if (!import_alias)
3223 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003224 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003225 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003227 if (mod != NULL)
3228 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003229 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003230 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 }
Neal Norwitz79792652005-11-14 04:25:03 +00003232 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 "unknown import statement: starts with command '%s'",
3234 STR(CHILD(n, 0)));
3235 return NULL;
3236}
3237
3238static stmt_ty
3239ast_for_global_stmt(struct compiling *c, const node *n)
3240{
3241 /* global_stmt: 'global' NAME (',' NAME)* */
3242 identifier name;
3243 asdl_seq *s;
3244 int i;
3245
3246 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003247 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003249 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003250 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003251 name = NEW_IDENTIFIER(CHILD(n, i));
3252 if (!name)
3253 return NULL;
3254 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003256 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257}
3258
3259static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003260ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3261{
3262 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3263 identifier name;
3264 asdl_seq *s;
3265 int i;
3266
3267 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003268 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003269 if (!s)
3270 return NULL;
3271 for (i = 1; i < NCH(n); i += 2) {
3272 name = NEW_IDENTIFIER(CHILD(n, i));
3273 if (!name)
3274 return NULL;
3275 asdl_seq_SET(s, i / 2, name);
3276 }
3277 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3278}
3279
3280static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281ast_for_assert_stmt(struct compiling *c, const node *n)
3282{
3283 /* assert_stmt: 'assert' test [',' test] */
3284 REQ(n, assert_stmt);
3285 if (NCH(n) == 2) {
3286 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3287 if (!expression)
3288 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003289 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 }
3291 else if (NCH(n) == 4) {
3292 expr_ty expr1, expr2;
3293
3294 expr1 = ast_for_expr(c, CHILD(n, 1));
3295 if (!expr1)
3296 return NULL;
3297 expr2 = ast_for_expr(c, CHILD(n, 3));
3298 if (!expr2)
3299 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300
Thomas Wouters89f507f2006-12-13 04:49:30 +00003301 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 }
Neal Norwitz79792652005-11-14 04:25:03 +00003303 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 "improper number of parts to 'assert' statement: %d",
3305 NCH(n));
3306 return NULL;
3307}
3308
3309static asdl_seq *
3310ast_for_suite(struct compiling *c, const node *n)
3311{
3312 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003313 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314 stmt_ty s;
3315 int i, total, num, end, pos = 0;
3316 node *ch;
3317
3318 REQ(n, suite);
3319
3320 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003321 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 n = CHILD(n, 0);
3326 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003328 */
3329 end = NCH(n) - 1;
3330 if (TYPE(CHILD(n, end - 1)) == SEMI)
3331 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 for (i = 0; i < end; i += 2) {
3334 ch = CHILD(n, i);
3335 s = ast_for_stmt(c, ch);
3336 if (!s)
3337 return NULL;
3338 asdl_seq_SET(seq, pos++, s);
3339 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340 }
3341 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003342 for (i = 2; i < (NCH(n) - 1); i++) {
3343 ch = CHILD(n, i);
3344 REQ(ch, stmt);
3345 num = num_stmts(ch);
3346 if (num == 1) {
3347 /* small_stmt or compound_stmt with only one child */
3348 s = ast_for_stmt(c, ch);
3349 if (!s)
3350 return NULL;
3351 asdl_seq_SET(seq, pos++, s);
3352 }
3353 else {
3354 int j;
3355 ch = CHILD(ch, 0);
3356 REQ(ch, simple_stmt);
3357 for (j = 0; j < NCH(ch); j += 2) {
3358 /* statement terminates with a semi-colon ';' */
3359 if (NCH(CHILD(ch, j)) == 0) {
3360 assert((j + 1) == NCH(ch));
3361 break;
3362 }
3363 s = ast_for_stmt(c, CHILD(ch, j));
3364 if (!s)
3365 return NULL;
3366 asdl_seq_SET(seq, pos++, s);
3367 }
3368 }
3369 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370 }
3371 assert(pos == seq->size);
3372 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373}
3374
3375static stmt_ty
3376ast_for_if_stmt(struct compiling *c, const node *n)
3377{
3378 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3379 ['else' ':' suite]
3380 */
3381 char *s;
3382
3383 REQ(n, if_stmt);
3384
3385 if (NCH(n) == 4) {
3386 expr_ty expression;
3387 asdl_seq *suite_seq;
3388
3389 expression = ast_for_expr(c, CHILD(n, 1));
3390 if (!expression)
3391 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003393 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003394 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395
Guido van Rossumd8faa362007-04-27 19:54:29 +00003396 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3397 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003399
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 s = STR(CHILD(n, 4));
3401 /* s[2], the third character in the string, will be
3402 's' for el_s_e, or
3403 'i' for el_i_f
3404 */
3405 if (s[2] == 's') {
3406 expr_ty expression;
3407 asdl_seq *seq1, *seq2;
3408
3409 expression = ast_for_expr(c, CHILD(n, 1));
3410 if (!expression)
3411 return NULL;
3412 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003413 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414 return NULL;
3415 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003416 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 return NULL;
3418
Guido van Rossumd8faa362007-04-27 19:54:29 +00003419 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3420 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 }
3422 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003423 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003424 expr_ty expression;
3425 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003426 asdl_seq *orelse = NULL;
3427 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428 /* must reference the child n_elif+1 since 'else' token is third,
3429 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003430 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3431 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3432 has_else = 1;
3433 n_elif -= 3;
3434 }
3435 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436
Thomas Wouters89f507f2006-12-13 04:49:30 +00003437 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003438 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003440 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003441 if (!orelse)
3442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003444 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003446 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3447 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003449 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3450 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 asdl_seq_SET(orelse, 0,
3454 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003455 LINENO(CHILD(n, NCH(n) - 6)),
3456 CHILD(n, NCH(n) - 6)->n_col_offset,
3457 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003458 /* the just-created orelse handled the last elif */
3459 n_elif--;
3460 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461
Thomas Wouters89f507f2006-12-13 04:49:30 +00003462 for (i = 0; i < n_elif; i++) {
3463 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003464 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003465 if (!newobj)
3466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003468 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003471 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473
Thomas Wouters89f507f2006-12-13 04:49:30 +00003474 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003476 LINENO(CHILD(n, off)),
3477 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 orelse = newobj;
3479 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003480 expression = ast_for_expr(c, CHILD(n, 1));
3481 if (!expression)
3482 return NULL;
3483 suite_seq = ast_for_suite(c, CHILD(n, 3));
3484 if (!suite_seq)
3485 return NULL;
3486 return If(expression, suite_seq, orelse,
3487 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003489
3490 PyErr_Format(PyExc_SystemError,
3491 "unexpected token in 'if' statement: %s", s);
3492 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493}
3494
3495static stmt_ty
3496ast_for_while_stmt(struct compiling *c, const node *n)
3497{
3498 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3499 REQ(n, while_stmt);
3500
3501 if (NCH(n) == 4) {
3502 expr_ty expression;
3503 asdl_seq *suite_seq;
3504
3505 expression = ast_for_expr(c, CHILD(n, 1));
3506 if (!expression)
3507 return NULL;
3508 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003509 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003511 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512 }
3513 else if (NCH(n) == 7) {
3514 expr_ty expression;
3515 asdl_seq *seq1, *seq2;
3516
3517 expression = ast_for_expr(c, CHILD(n, 1));
3518 if (!expression)
3519 return NULL;
3520 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003521 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 return NULL;
3523 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003524 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 return NULL;
3526
Thomas Wouters89f507f2006-12-13 04:49:30 +00003527 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003529
3530 PyErr_Format(PyExc_SystemError,
3531 "wrong number of tokens for 'while' statement: %d",
3532 NCH(n));
3533 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534}
3535
3536static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003537ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003539 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003541 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003542 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3544 REQ(n, for_stmt);
3545
3546 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003547 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 if (!seq)
3549 return NULL;
3550 }
3551
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003552 node_target = CHILD(n, 1);
3553 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003554 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003556 /* Check the # of children rather than the length of _target, since
3557 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003558 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003559 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003560 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003562 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003564 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003565 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 return NULL;
3567 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003568 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 return NULL;
3570
Yury Selivanov75445082015-05-11 22:57:16 -04003571 if (is_async)
3572 return AsyncFor(target, expression, suite_seq, seq,
3573 LINENO(n), n->n_col_offset,
3574 c->c_arena);
3575 else
3576 return For(target, expression, suite_seq, seq,
3577 LINENO(n), n->n_col_offset,
3578 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579}
3580
3581static excepthandler_ty
3582ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3583{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003584 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585 REQ(exc, except_clause);
3586 REQ(body, suite);
3587
3588 if (NCH(exc) == 1) {
3589 asdl_seq *suite_seq = ast_for_suite(c, body);
3590 if (!suite_seq)
3591 return NULL;
3592
Neal Norwitzad74aa82008-03-31 05:14:30 +00003593 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003594 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 }
3596 else if (NCH(exc) == 2) {
3597 expr_ty expression;
3598 asdl_seq *suite_seq;
3599
3600 expression = ast_for_expr(c, CHILD(exc, 1));
3601 if (!expression)
3602 return NULL;
3603 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003604 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 return NULL;
3606
Neal Norwitzad74aa82008-03-31 05:14:30 +00003607 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003608 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 }
3610 else if (NCH(exc) == 4) {
3611 asdl_seq *suite_seq;
3612 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003613 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003614 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003616 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003617 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003619 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 return NULL;
3621 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003622 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 return NULL;
3624
Neal Norwitzad74aa82008-03-31 05:14:30 +00003625 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003626 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003628
3629 PyErr_Format(PyExc_SystemError,
3630 "wrong number of children for 'except' clause: %d",
3631 NCH(exc));
3632 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633}
3634
3635static stmt_ty
3636ast_for_try_stmt(struct compiling *c, const node *n)
3637{
Neal Norwitzf599f422005-12-17 21:33:47 +00003638 const int nch = NCH(n);
3639 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003640 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003641
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003642 REQ(n, try_stmt);
3643
Neal Norwitzf599f422005-12-17 21:33:47 +00003644 body = ast_for_suite(c, CHILD(n, 2));
3645 if (body == NULL)
3646 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647
Neal Norwitzf599f422005-12-17 21:33:47 +00003648 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3649 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3650 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3651 /* we can assume it's an "else",
3652 because nch >= 9 for try-else-finally and
3653 it would otherwise have a type of except_clause */
3654 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3655 if (orelse == NULL)
3656 return NULL;
3657 n_except--;
3658 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659
Neal Norwitzf599f422005-12-17 21:33:47 +00003660 finally = ast_for_suite(c, CHILD(n, nch - 1));
3661 if (finally == NULL)
3662 return NULL;
3663 n_except--;
3664 }
3665 else {
3666 /* we can assume it's an "else",
3667 otherwise it would have a type of except_clause */
3668 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3669 if (orelse == NULL)
3670 return NULL;
3671 n_except--;
3672 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003674 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003675 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 return NULL;
3677 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678
Neal Norwitzf599f422005-12-17 21:33:47 +00003679 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003680 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003681 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003682 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003683 if (handlers == NULL)
3684 return NULL;
3685
3686 for (i = 0; i < n_except; i++) {
3687 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3688 CHILD(n, 5 + i * 3));
3689 if (!e)
3690 return NULL;
3691 asdl_seq_SET(handlers, i, e);
3692 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003693 }
3694
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003695 assert(finally != NULL || asdl_seq_LEN(handlers));
3696 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697}
3698
Georg Brandl0c315622009-05-25 21:10:36 +00003699/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003700static withitem_ty
3701ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003702{
3703 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003704
Georg Brandl0c315622009-05-25 21:10:36 +00003705 REQ(n, with_item);
3706 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003707 if (!context_expr)
3708 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003709 if (NCH(n) == 3) {
3710 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003711
3712 if (!optional_vars) {
3713 return NULL;
3714 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003715 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003716 return NULL;
3717 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003718 }
3719
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003720 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003721}
3722
Georg Brandl0c315622009-05-25 21:10:36 +00003723/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3724static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003725ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003726{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003727 int i, n_items;
3728 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003729
3730 REQ(n, with_stmt);
3731
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003732 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003733 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003734 if (!items)
3735 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003736 for (i = 1; i < NCH(n) - 2; i += 2) {
3737 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3738 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003739 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003740 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003741 }
3742
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003743 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3744 if (!body)
3745 return NULL;
3746
Yury Selivanov75445082015-05-11 22:57:16 -04003747 if (is_async)
3748 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3749 else
3750 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003751}
3752
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003753static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003754ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003756 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003757 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003758 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003759 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003760
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761 REQ(n, classdef);
3762
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003763 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764 s = ast_for_suite(c, CHILD(n, 3));
3765 if (!s)
3766 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003767 classname = NEW_IDENTIFIER(CHILD(n, 1));
3768 if (!classname)
3769 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003770 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003771 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003772 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3773 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003775
3776 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003777 s = ast_for_suite(c, CHILD(n,5));
3778 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003779 return NULL;
3780 classname = NEW_IDENTIFIER(CHILD(n, 1));
3781 if (!classname)
3782 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003783 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003784 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003785 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3786 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787 }
3788
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003789 /* class NAME '(' arglist ')' ':' suite */
3790 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003791 {
3792 PyObject *dummy_name;
3793 expr_ty dummy;
3794 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3795 if (!dummy_name)
3796 return NULL;
3797 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3798 call = ast_for_call(c, CHILD(n, 3), dummy);
3799 if (!call)
3800 return NULL;
3801 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003803 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003805 classname = NEW_IDENTIFIER(CHILD(n, 1));
3806 if (!classname)
3807 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003808 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003809 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003810
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003811 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003812 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813}
3814
3815static stmt_ty
3816ast_for_stmt(struct compiling *c, const node *n)
3817{
3818 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003819 assert(NCH(n) == 1);
3820 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 }
3822 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003823 assert(num_stmts(n) == 1);
3824 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 }
3826 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003827 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003828 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3829 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003830 */
3831 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832 case expr_stmt:
3833 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003834 case del_stmt:
3835 return ast_for_del_stmt(c, n);
3836 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003837 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838 case flow_stmt:
3839 return ast_for_flow_stmt(c, n);
3840 case import_stmt:
3841 return ast_for_import_stmt(c, n);
3842 case global_stmt:
3843 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003844 case nonlocal_stmt:
3845 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846 case assert_stmt:
3847 return ast_for_assert_stmt(c, n);
3848 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003849 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3851 TYPE(n), NCH(n));
3852 return NULL;
3853 }
3854 }
3855 else {
3856 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003857 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003858 */
3859 node *ch = CHILD(n, 0);
3860 REQ(n, compound_stmt);
3861 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 case if_stmt:
3863 return ast_for_if_stmt(c, ch);
3864 case while_stmt:
3865 return ast_for_while_stmt(c, ch);
3866 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003867 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 case try_stmt:
3869 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003870 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003871 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003872 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003873 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003875 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 case decorated:
3877 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003878 case async_stmt:
3879 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003881 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3883 TYPE(n), NCH(n));
3884 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003885 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886 }
3887}
3888
3889static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003890parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003892 const char *end;
3893 long x;
3894 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003895 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003896 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003897
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003898 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003899 errno = 0;
3900 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003901 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003902 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003903 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003904 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003905 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003906 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003907 }
3908 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003909 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003910 if (*end == '\0') {
3911 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003912 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003913 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003914 }
3915 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003916 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003917 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003918 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3919 if (compl.imag == -1.0 && PyErr_Occurred())
3920 return NULL;
3921 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003922 }
3923 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003924 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003925 dx = PyOS_string_to_double(s, NULL, NULL);
3926 if (dx == -1.0 && PyErr_Occurred())
3927 return NULL;
3928 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003929 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930}
3931
3932static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003933decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003935 const char *s, *t;
3936 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003937 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3938 while (s < end && (*s & 0x80)) s++;
3939 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003940 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941}
3942
3943static PyObject *
Eric V. Smith5567f892015-09-21 13:36:09 -04003944decode_unicode(struct compiling *c, const char *s, size_t len, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003946 PyObject *v, *u;
3947 char *buf;
3948 char *p;
3949 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003950
Guido van Rossumd8faa362007-04-27 19:54:29 +00003951 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003952 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003953 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003954 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003955 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003956 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003957 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3958 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3959 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003960 if (u == NULL)
3961 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003962 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003963 end = s + len;
3964 while (s < end) {
3965 if (*s == '\\') {
3966 *p++ = *s++;
3967 if (*s & 0x80) {
3968 strcpy(p, "u005c");
3969 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003970 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003971 }
3972 if (*s & 0x80) { /* XXX inefficient */
3973 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003974 int kind;
3975 void *data;
3976 Py_ssize_t len, i;
3977 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003978 if (w == NULL) {
3979 Py_DECREF(u);
3980 return NULL;
3981 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003982 kind = PyUnicode_KIND(w);
3983 data = PyUnicode_DATA(w);
3984 len = PyUnicode_GET_LENGTH(w);
3985 for (i = 0; i < len; i++) {
3986 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3987 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003988 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003989 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003990 /* Should be impossible to overflow */
3991 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003992 Py_DECREF(w);
3993 } else {
3994 *p++ = *s++;
3995 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003996 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003997 len = p - buf;
3998 s = buf;
3999 }
Eric V. Smith5567f892015-09-21 13:36:09 -04004000 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004001 Py_XDECREF(u);
4002 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003}
4004
Eric V. Smith235a6f02015-09-19 14:51:32 -04004005/* Compile this expression in to an expr_ty. We know that we can
4006 temporarily modify the character before the start of this string
4007 (it's '{'), and we know we can temporarily modify the character
4008 after this string (it is a '}'). Leverage this to create a
4009 sub-string with enough room for us to add parens around the
4010 expression. This is to allow strings with embedded newlines, for
4011 example. */
4012static expr_ty
Eric V. Smith1d44c412015-09-23 07:49:00 -04004013fstring_compile_expr(PyObject *str, Py_ssize_t expr_start,
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004014 Py_ssize_t expr_end, struct compiling *c, const node *n)
4015
Eric V. Smith235a6f02015-09-19 14:51:32 -04004016{
4017 PyCompilerFlags cf;
4018 mod_ty mod;
4019 char *utf_expr;
4020 Py_ssize_t i;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004021 Py_UCS4 end_ch = -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004022 int all_whitespace;
4023 PyObject *sub = NULL;
4024
4025 /* We only decref sub if we allocated it with a PyUnicode_Substring.
4026 decref_sub records that. */
4027 int decref_sub = 0;
4028
4029 assert(str);
4030
Eric V. Smith1d44c412015-09-23 07:49:00 -04004031 assert(expr_start >= 0 && expr_start < PyUnicode_GET_LENGTH(str));
4032 assert(expr_end >= 0 && expr_end < PyUnicode_GET_LENGTH(str));
4033 assert(expr_end >= expr_start);
4034
Martin Panterc2432f62015-10-07 11:15:15 +00004035 /* There has to be at least one character on each side of the
Eric V. Smith1d44c412015-09-23 07:49:00 -04004036 expression inside this str. This will have been caught before
4037 we're called. */
4038 assert(expr_start >= 1);
4039 assert(expr_end <= PyUnicode_GET_LENGTH(str)-1);
4040
Eric V. Smith235a6f02015-09-19 14:51:32 -04004041 /* If the substring is all whitespace, it's an error. We need to
4042 catch this here, and not when we call PyParser_ASTFromString,
4043 because turning the expression '' in to '()' would go from
4044 being invalid to valid. */
4045 /* Note that this code says an empty string is all
4046 whitespace. That's important. There's a test for it: f'{}'. */
4047 all_whitespace = 1;
4048 for (i = expr_start; i < expr_end; i++) {
4049 if (!Py_UNICODE_ISSPACE(PyUnicode_READ_CHAR(str, i))) {
4050 all_whitespace = 0;
4051 break;
4052 }
4053 }
4054 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004055 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004056 goto error;
4057 }
4058
4059 /* If the substring will be the entire source string, we can't use
4060 PyUnicode_Substring, since it will return another reference to
4061 our original string. Because we're modifying the string in
4062 place, that's a no-no. So, detect that case and just use our
4063 string directly. */
4064
4065 if (expr_start-1 == 0 && expr_end+1 == PyUnicode_GET_LENGTH(str)) {
Eric V. Smith1d44c412015-09-23 07:49:00 -04004066 /* If str is well formed, then the first and last chars must
4067 be '{' and '}', respectively. But, if there's a syntax
4068 error, for example f'{3!', then the last char won't be a
4069 closing brace. So, remember the last character we read in
4070 order for us to restore it. */
4071 end_ch = PyUnicode_ReadChar(str, expr_end-expr_start+1);
4072 assert(end_ch != (Py_UCS4)-1);
4073
4074 /* In all cases, however, start_ch must be '{'. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004075 assert(PyUnicode_ReadChar(str, 0) == '{');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004076
Eric V. Smith235a6f02015-09-19 14:51:32 -04004077 sub = str;
4078 } else {
4079 /* Create a substring object. It must be a new object, with
4080 refcount==1, so that we can modify it. */
4081 sub = PyUnicode_Substring(str, expr_start-1, expr_end+1);
4082 if (!sub)
4083 goto error;
4084 assert(sub != str); /* Make sure it's a new string. */
4085 decref_sub = 1; /* Remember to deallocate it on error. */
4086 }
4087
Eric V. Smith1d44c412015-09-23 07:49:00 -04004088 /* Put () around the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004089 if (PyUnicode_WriteChar(sub, 0, '(') < 0 ||
4090 PyUnicode_WriteChar(sub, expr_end-expr_start+1, ')') < 0)
4091 goto error;
4092
Eric V. Smith235a6f02015-09-19 14:51:32 -04004093 /* No need to free the memory returned here: it's managed by the
4094 string. */
4095 utf_expr = PyUnicode_AsUTF8(sub);
4096 if (!utf_expr)
4097 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004098
4099 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004100 mod = PyParser_ASTFromString(utf_expr, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004101 Py_eval_input, &cf, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004102 if (!mod)
4103 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004104
Eric V. Smith235a6f02015-09-19 14:51:32 -04004105 if (sub != str)
4106 /* Clear instead of decref in case we ever modify this code to change
4107 the error handling: this is safest because the XDECREF won't try
4108 and decref it when it's NULL. */
4109 /* No need to restore the chars in sub, since we know it's getting
4110 ready to get deleted (refcount must be 1, since we got a new string
4111 in PyUnicode_Substring). */
4112 Py_CLEAR(sub);
4113 else {
4114 assert(!decref_sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004115 assert(end_ch != (Py_UCS4)-1);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004116 /* Restore str, which we earlier modified directly. */
4117 if (PyUnicode_WriteChar(str, 0, '{') < 0 ||
Eric V. Smith1d44c412015-09-23 07:49:00 -04004118 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004119 goto error;
4120 }
4121 return mod->v.Expression.body;
4122
4123error:
4124 /* Only decref sub if it was the result of a call to SubString. */
4125 if (decref_sub)
4126 Py_XDECREF(sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004127
4128 if (end_ch != (Py_UCS4)-1) {
4129 /* We only get here if we modified str. Make sure that's the
4130 case: str will be equal to sub. */
4131 if (str == sub) {
4132 /* Don't check the error, because we've already set the
4133 error state (that's why we're in 'error', after
4134 all). */
4135 PyUnicode_WriteChar(str, 0, '{');
4136 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch);
4137 }
4138 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004139 return NULL;
4140}
4141
4142/* Return -1 on error.
4143
4144 Return 0 if we reached the end of the literal.
4145
4146 Return 1 if we haven't reached the end of the literal, but we want
4147 the caller to process the literal up to this point. Used for
4148 doubled braces.
4149*/
4150static int
4151fstring_find_literal(PyObject *str, Py_ssize_t *ofs, PyObject **literal,
4152 int recurse_lvl, struct compiling *c, const node *n)
4153{
4154 /* Get any literal string. It ends when we hit an un-doubled brace, or the
4155 end of the string. */
4156
4157 Py_ssize_t literal_start, literal_end;
4158 int result = 0;
4159
4160 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4161 void *data = PyUnicode_DATA(str);
4162
4163 assert(*literal == NULL);
4164
4165 literal_start = *ofs;
4166 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4167 Py_UCS4 ch = PyUnicode_READ(kind, data, *ofs);
4168 if (ch == '{' || ch == '}') {
4169 /* Check for doubled braces, but only at the top level. If
4170 we checked at every level, then f'{0:{3}}' would fail
4171 with the two closing braces. */
4172 if (recurse_lvl == 0) {
4173 if (*ofs + 1 < PyUnicode_GET_LENGTH(str) &&
4174 PyUnicode_READ(kind, data, *ofs + 1) == ch) {
4175 /* We're going to tell the caller that the literal ends
4176 here, but that they should continue scanning. But also
4177 skip over the second brace when we resume scanning. */
4178 literal_end = *ofs + 1;
4179 *ofs += 2;
4180 result = 1;
4181 goto done;
4182 }
4183
4184 /* Where a single '{' is the start of a new expression, a
4185 single '}' is not allowed. */
4186 if (ch == '}') {
4187 ast_error(c, n, "f-string: single '}' is not allowed");
4188 return -1;
4189 }
4190 }
4191
4192 /* We're either at a '{', which means we're starting another
4193 expression; or a '}', which means we're at the end of this
4194 f-string (for a nested format_spec). */
4195 break;
4196 }
4197 }
4198 literal_end = *ofs;
4199
4200 assert(*ofs == PyUnicode_GET_LENGTH(str) ||
4201 PyUnicode_READ(kind, data, *ofs) == '{' ||
4202 PyUnicode_READ(kind, data, *ofs) == '}');
4203done:
4204 if (literal_start != literal_end) {
4205 *literal = PyUnicode_Substring(str, literal_start, literal_end);
4206 if (!*literal)
4207 return -1;
4208 }
4209
4210 return result;
4211}
4212
4213/* Forward declaration because parsing is recursive. */
4214static expr_ty
4215fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4216 struct compiling *c, const node *n);
4217
4218/* Parse the f-string str, starting at ofs. We know *ofs starts an
4219 expression (so it must be a '{'). Returns the FormattedValue node,
4220 which includes the expression, conversion character, and
4221 format_spec expression.
4222
4223 Note that I don't do a perfect job here: I don't make sure that a
4224 closing brace doesn't match an opening paren, for example. It
4225 doesn't need to error on all invalid expressions, just correctly
4226 find the end of all valid ones. Any errors inside the expression
4227 will be caught when we parse it later. */
4228static int
4229fstring_find_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4230 expr_ty *expression, struct compiling *c, const node *n)
4231{
4232 /* Return -1 on error, else 0. */
4233
4234 Py_ssize_t expr_start;
4235 Py_ssize_t expr_end;
4236 expr_ty simple_expression;
4237 expr_ty format_spec = NULL; /* Optional format specifier. */
4238 Py_UCS4 conversion = -1; /* The conversion char. -1 if not specified. */
4239
4240 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4241 void *data = PyUnicode_DATA(str);
4242
4243 /* 0 if we're not in a string, else the quote char we're trying to
4244 match (single or double quote). */
4245 Py_UCS4 quote_char = 0;
4246
4247 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4248 int string_type = 0;
4249
4250 /* Keep track of nesting level for braces/parens/brackets in
4251 expressions. */
4252 Py_ssize_t nested_depth = 0;
4253
4254 /* Can only nest one level deep. */
4255 if (recurse_lvl >= 2) {
4256 ast_error(c, n, "f-string: expressions nested too deeply");
4257 return -1;
4258 }
4259
4260 /* The first char must be a left brace, or we wouldn't have gotten
4261 here. Skip over it. */
4262 assert(PyUnicode_READ(kind, data, *ofs) == '{');
4263 *ofs += 1;
4264
4265 expr_start = *ofs;
4266 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4267 Py_UCS4 ch;
4268
4269 /* Loop invariants. */
4270 assert(nested_depth >= 0);
4271 assert(*ofs >= expr_start);
4272 if (quote_char)
4273 assert(string_type == 1 || string_type == 3);
4274 else
4275 assert(string_type == 0);
4276
4277 ch = PyUnicode_READ(kind, data, *ofs);
4278 if (quote_char) {
4279 /* We're inside a string. See if we're at the end. */
4280 /* This code needs to implement the same non-error logic
4281 as tok_get from tokenizer.c, at the letter_quote
4282 label. To actually share that code would be a
4283 nightmare. But, it's unlikely to change and is small,
4284 so duplicate it here. Note we don't need to catch all
4285 of the errors, since they'll be caught when parsing the
4286 expression. We just need to match the non-error
4287 cases. Thus we can ignore \n in single-quoted strings,
4288 for example. Or non-terminated strings. */
4289 if (ch == quote_char) {
4290 /* Does this match the string_type (single or triple
4291 quoted)? */
4292 if (string_type == 3) {
4293 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4294 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4295 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4296 /* We're at the end of a triple quoted string. */
4297 *ofs += 2;
4298 string_type = 0;
4299 quote_char = 0;
4300 continue;
4301 }
4302 } else {
4303 /* We're at the end of a normal string. */
4304 quote_char = 0;
4305 string_type = 0;
4306 continue;
4307 }
4308 }
4309 /* We're inside a string, and not finished with the
4310 string. If this is a backslash, skip the next char (it
4311 might be an end quote that needs skipping). Otherwise,
4312 just consume this character normally. */
4313 if (ch == '\\' && *ofs+1 < PyUnicode_GET_LENGTH(str)) {
4314 /* Just skip the next char, whatever it is. */
4315 *ofs += 1;
4316 }
4317 } else if (ch == '\'' || ch == '"') {
4318 /* Is this a triple quoted string? */
4319 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4320 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4321 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4322 string_type = 3;
4323 *ofs += 2;
4324 } else {
4325 /* Start of a normal string. */
4326 string_type = 1;
4327 }
4328 /* Start looking for the end of the string. */
4329 quote_char = ch;
4330 } else if (ch == '[' || ch == '{' || ch == '(') {
4331 nested_depth++;
4332 } else if (nested_depth != 0 &&
4333 (ch == ']' || ch == '}' || ch == ')')) {
4334 nested_depth--;
4335 } else if (ch == '#') {
4336 /* Error: can't include a comment character, inside parens
4337 or not. */
4338 ast_error(c, n, "f-string cannot include '#'");
4339 return -1;
4340 } else if (nested_depth == 0 &&
4341 (ch == '!' || ch == ':' || ch == '}')) {
4342 /* First, test for the special case of "!=". Since '=' is
4343 not an allowed conversion character, nothing is lost in
4344 this test. */
4345 if (ch == '!' && *ofs+1 < PyUnicode_GET_LENGTH(str) &&
4346 PyUnicode_READ(kind, data, *ofs+1) == '=')
4347 /* This isn't a conversion character, just continue. */
4348 continue;
4349
4350 /* Normal way out of this loop. */
4351 break;
4352 } else {
4353 /* Just consume this char and loop around. */
4354 }
4355 }
4356 expr_end = *ofs;
4357 /* If we leave this loop in a string or with mismatched parens, we
4358 don't care. We'll get a syntax error when compiling the
4359 expression. But, we can produce a better error message, so
4360 let's just do that.*/
4361 if (quote_char) {
4362 ast_error(c, n, "f-string: unterminated string");
4363 return -1;
4364 }
4365 if (nested_depth) {
4366 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4367 return -1;
4368 }
4369
Eric V. Smith235a6f02015-09-19 14:51:32 -04004370 if (*ofs >= PyUnicode_GET_LENGTH(str))
4371 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004372
4373 /* Compile the expression as soon as possible, so we show errors
4374 related to the expression before errors related to the
4375 conversion or format_spec. */
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004376 simple_expression = fstring_compile_expr(str, expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004377 if (!simple_expression)
4378 return -1;
4379
4380 /* Check for a conversion char, if present. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004381 if (PyUnicode_READ(kind, data, *ofs) == '!') {
4382 *ofs += 1;
4383 if (*ofs >= PyUnicode_GET_LENGTH(str))
4384 goto unexpected_end_of_string;
4385
4386 conversion = PyUnicode_READ(kind, data, *ofs);
4387 *ofs += 1;
4388
4389 /* Validate the conversion. */
4390 if (!(conversion == 's' || conversion == 'r'
4391 || conversion == 'a')) {
4392 ast_error(c, n, "f-string: invalid conversion character: "
4393 "expected 's', 'r', or 'a'");
4394 return -1;
4395 }
4396 }
4397
4398 /* Check for the format spec, if present. */
4399 if (*ofs >= PyUnicode_GET_LENGTH(str))
4400 goto unexpected_end_of_string;
4401 if (PyUnicode_READ(kind, data, *ofs) == ':') {
4402 *ofs += 1;
4403 if (*ofs >= PyUnicode_GET_LENGTH(str))
4404 goto unexpected_end_of_string;
4405
4406 /* Parse the format spec. */
4407 format_spec = fstring_parse(str, ofs, recurse_lvl+1, c, n);
4408 if (!format_spec)
4409 return -1;
4410 }
4411
4412 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4413 PyUnicode_READ(kind, data, *ofs) != '}')
4414 goto unexpected_end_of_string;
4415
4416 /* We're at a right brace. Consume it. */
4417 assert(*ofs < PyUnicode_GET_LENGTH(str));
4418 assert(PyUnicode_READ(kind, data, *ofs) == '}');
4419 *ofs += 1;
4420
Eric V. Smith235a6f02015-09-19 14:51:32 -04004421 /* And now create the FormattedValue node that represents this entire
4422 expression with the conversion and format spec. */
4423 *expression = FormattedValue(simple_expression, (int)conversion,
4424 format_spec, LINENO(n), n->n_col_offset,
4425 c->c_arena);
4426 if (!*expression)
4427 return -1;
4428
4429 return 0;
4430
4431unexpected_end_of_string:
4432 ast_error(c, n, "f-string: expecting '}'");
4433 return -1;
4434}
4435
4436/* Return -1 on error.
4437
4438 Return 0 if we have a literal (possible zero length) and an
4439 expression (zero length if at the end of the string.
4440
4441 Return 1 if we have a literal, but no expression, and we want the
4442 caller to call us again. This is used to deal with doubled
4443 braces.
4444
4445 When called multiple times on the string 'a{{b{0}c', this function
4446 will return:
4447
4448 1. the literal 'a{' with no expression, and a return value
4449 of 1. Despite the fact that there's no expression, the return
4450 value of 1 means we're not finished yet.
4451
4452 2. the literal 'b' and the expression '0', with a return value of
4453 0. The fact that there's an expression means we're not finished.
4454
4455 3. literal 'c' with no expression and a return value of 0. The
4456 combination of the return value of 0 with no expression means
4457 we're finished.
4458*/
4459static int
4460fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4461 PyObject **literal, expr_ty *expression,
4462 struct compiling *c, const node *n)
4463{
4464 int result;
4465
4466 assert(*literal == NULL && *expression == NULL);
4467
4468 /* Get any literal string. */
4469 result = fstring_find_literal(str, ofs, literal, recurse_lvl, c, n);
4470 if (result < 0)
4471 goto error;
4472
4473 assert(result == 0 || result == 1);
4474
4475 if (result == 1)
4476 /* We have a literal, but don't look at the expression. */
4477 return 1;
4478
4479 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4480
4481 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4482 PyUnicode_READ_CHAR(str, *ofs) == '}')
4483 /* We're at the end of the string or the end of a nested
4484 f-string: no expression. The top-level error case where we
4485 expect to be at the end of the string but we're at a '}' is
4486 handled later. */
4487 return 0;
4488
4489 /* We must now be the start of an expression, on a '{'. */
4490 assert(*ofs < PyUnicode_GET_LENGTH(str) &&
4491 PyUnicode_READ_CHAR(str, *ofs) == '{');
4492
4493 if (fstring_find_expr(str, ofs, recurse_lvl, expression, c, n) < 0)
4494 goto error;
4495
4496 return 0;
4497
4498error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004499 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004500 return -1;
4501}
4502
4503#define EXPRLIST_N_CACHED 64
4504
4505typedef struct {
4506 /* Incrementally build an array of expr_ty, so be used in an
4507 asdl_seq. Cache some small but reasonably sized number of
4508 expr_ty's, and then after that start dynamically allocating,
4509 doubling the number allocated each time. Note that the f-string
4510 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4511 Str for the literal 'a'. So you add expr_ty's about twice as
4512 fast as you add exressions in an f-string. */
4513
4514 Py_ssize_t allocated; /* Number we've allocated. */
4515 Py_ssize_t size; /* Number we've used. */
4516 expr_ty *p; /* Pointer to the memory we're actually
4517 using. Will point to 'data' until we
4518 start dynamically allocating. */
4519 expr_ty data[EXPRLIST_N_CACHED];
4520} ExprList;
4521
4522#ifdef NDEBUG
4523#define ExprList_check_invariants(l)
4524#else
4525static void
4526ExprList_check_invariants(ExprList *l)
4527{
4528 /* Check our invariants. Make sure this object is "live", and
4529 hasn't been deallocated. */
4530 assert(l->size >= 0);
4531 assert(l->p != NULL);
4532 if (l->size <= EXPRLIST_N_CACHED)
4533 assert(l->data == l->p);
4534}
4535#endif
4536
4537static void
4538ExprList_Init(ExprList *l)
4539{
4540 l->allocated = EXPRLIST_N_CACHED;
4541 l->size = 0;
4542
4543 /* Until we start allocating dynamically, p points to data. */
4544 l->p = l->data;
4545
4546 ExprList_check_invariants(l);
4547}
4548
4549static int
4550ExprList_Append(ExprList *l, expr_ty exp)
4551{
4552 ExprList_check_invariants(l);
4553 if (l->size >= l->allocated) {
4554 /* We need to alloc (or realloc) the memory. */
4555 Py_ssize_t new_size = l->allocated * 2;
4556
4557 /* See if we've ever allocated anything dynamically. */
4558 if (l->p == l->data) {
4559 Py_ssize_t i;
4560 /* We're still using the cached data. Switch to
4561 alloc-ing. */
4562 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4563 if (!l->p)
4564 return -1;
4565 /* Copy the cached data into the new buffer. */
4566 for (i = 0; i < l->size; i++)
4567 l->p[i] = l->data[i];
4568 } else {
4569 /* Just realloc. */
4570 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4571 if (!tmp) {
4572 PyMem_RawFree(l->p);
4573 l->p = NULL;
4574 return -1;
4575 }
4576 l->p = tmp;
4577 }
4578
4579 l->allocated = new_size;
4580 assert(l->allocated == 2 * l->size);
4581 }
4582
4583 l->p[l->size++] = exp;
4584
4585 ExprList_check_invariants(l);
4586 return 0;
4587}
4588
4589static void
4590ExprList_Dealloc(ExprList *l)
4591{
4592 ExprList_check_invariants(l);
4593
4594 /* If there's been an error, or we've never dynamically allocated,
4595 do nothing. */
4596 if (!l->p || l->p == l->data) {
4597 /* Do nothing. */
4598 } else {
4599 /* We have dynamically allocated. Free the memory. */
4600 PyMem_RawFree(l->p);
4601 }
4602 l->p = NULL;
4603 l->size = -1;
4604}
4605
4606static asdl_seq *
4607ExprList_Finish(ExprList *l, PyArena *arena)
4608{
4609 asdl_seq *seq;
4610
4611 ExprList_check_invariants(l);
4612
4613 /* Allocate the asdl_seq and copy the expressions in to it. */
4614 seq = _Py_asdl_seq_new(l->size, arena);
4615 if (seq) {
4616 Py_ssize_t i;
4617 for (i = 0; i < l->size; i++)
4618 asdl_seq_SET(seq, i, l->p[i]);
4619 }
4620 ExprList_Dealloc(l);
4621 return seq;
4622}
4623
4624/* The FstringParser is designed to add a mix of strings and
4625 f-strings, and concat them together as needed. Ultimately, it
4626 generates an expr_ty. */
4627typedef struct {
4628 PyObject *last_str;
4629 ExprList expr_list;
4630} FstringParser;
4631
4632#ifdef NDEBUG
4633#define FstringParser_check_invariants(state)
4634#else
4635static void
4636FstringParser_check_invariants(FstringParser *state)
4637{
4638 if (state->last_str)
4639 assert(PyUnicode_CheckExact(state->last_str));
4640 ExprList_check_invariants(&state->expr_list);
4641}
4642#endif
4643
4644static void
4645FstringParser_Init(FstringParser *state)
4646{
4647 state->last_str = NULL;
4648 ExprList_Init(&state->expr_list);
4649 FstringParser_check_invariants(state);
4650}
4651
4652static void
4653FstringParser_Dealloc(FstringParser *state)
4654{
4655 FstringParser_check_invariants(state);
4656
4657 Py_XDECREF(state->last_str);
4658 ExprList_Dealloc(&state->expr_list);
4659}
4660
4661/* Make a Str node, but decref the PyUnicode object being added. */
4662static expr_ty
4663make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4664{
4665 PyObject *s = *str;
4666 *str = NULL;
4667 assert(PyUnicode_CheckExact(s));
4668 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4669 Py_DECREF(s);
4670 return NULL;
4671 }
4672 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4673}
4674
4675/* Add a non-f-string (that is, a regular literal string). str is
4676 decref'd. */
4677static int
4678FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4679{
4680 FstringParser_check_invariants(state);
4681
4682 assert(PyUnicode_CheckExact(str));
4683
4684 if (PyUnicode_GET_LENGTH(str) == 0) {
4685 Py_DECREF(str);
4686 return 0;
4687 }
4688
4689 if (!state->last_str) {
4690 /* We didn't have a string before, so just remember this one. */
4691 state->last_str = str;
4692 } else {
4693 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004694 PyUnicode_AppendAndDel(&state->last_str, str);
4695 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004696 return -1;
4697 }
4698 FstringParser_check_invariants(state);
4699 return 0;
4700}
4701
4702/* Parse an f-string. The f-string is in str, starting at ofs, with no 'f'
4703 or quotes. str is not decref'd, since we don't know if it's used elsewhere.
4704 And if we're only looking at a part of a string, then decref'ing is
4705 definitely not the right thing to do! */
4706static int
4707FstringParser_ConcatFstring(FstringParser *state, PyObject *str,
4708 Py_ssize_t *ofs, int recurse_lvl,
4709 struct compiling *c, const node *n)
4710{
4711 FstringParser_check_invariants(state);
4712
4713 /* Parse the f-string. */
4714 while (1) {
4715 PyObject *literal = NULL;
4716 expr_ty expression = NULL;
4717
4718 /* If there's a zero length literal in front of the
4719 expression, literal will be NULL. If we're at the end of
4720 the f-string, expression will be NULL (unless result == 1,
4721 see below). */
4722 int result = fstring_find_literal_and_expr(str, ofs, recurse_lvl,
4723 &literal, &expression,
4724 c, n);
4725 if (result < 0)
4726 return -1;
4727
4728 /* Add the literal, if any. */
4729 if (!literal) {
4730 /* Do nothing. Just leave last_str alone (and possibly
4731 NULL). */
4732 } else if (!state->last_str) {
4733 state->last_str = literal;
4734 literal = NULL;
4735 } else {
4736 /* We have a literal, concatenate it. */
4737 assert(PyUnicode_GET_LENGTH(literal) != 0);
4738 if (FstringParser_ConcatAndDel(state, literal) < 0)
4739 return -1;
4740 literal = NULL;
4741 }
4742 assert(!state->last_str ||
4743 PyUnicode_GET_LENGTH(state->last_str) != 0);
4744
4745 /* We've dealt with the literal now. It can't be leaked on further
4746 errors. */
4747 assert(literal == NULL);
4748
4749 /* See if we should just loop around to get the next literal
4750 and expression, while ignoring the expression this
4751 time. This is used for un-doubling braces, as an
4752 optimization. */
4753 if (result == 1)
4754 continue;
4755
4756 if (!expression)
4757 /* We're done with this f-string. */
4758 break;
4759
4760 /* We know we have an expression. Convert any existing string
4761 to a Str node. */
4762 if (!state->last_str) {
4763 /* Do nothing. No previous literal. */
4764 } else {
4765 /* Convert the existing last_str literal to a Str node. */
4766 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4767 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4768 return -1;
4769 }
4770
4771 if (ExprList_Append(&state->expr_list, expression) < 0)
4772 return -1;
4773 }
4774
4775 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4776
4777 /* If recurse_lvl is zero, then we must be at the end of the
4778 string. Otherwise, we must be at a right brace. */
4779
4780 if (recurse_lvl == 0 && *ofs < PyUnicode_GET_LENGTH(str)) {
4781 ast_error(c, n, "f-string: unexpected end of string");
4782 return -1;
4783 }
4784 if (recurse_lvl != 0 && PyUnicode_READ_CHAR(str, *ofs) != '}') {
4785 ast_error(c, n, "f-string: expecting '}'");
4786 return -1;
4787 }
4788
4789 FstringParser_check_invariants(state);
4790 return 0;
4791}
4792
4793/* Convert the partial state reflected in last_str and expr_list to an
4794 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4795static expr_ty
4796FstringParser_Finish(FstringParser *state, struct compiling *c,
4797 const node *n)
4798{
4799 asdl_seq *seq;
4800
4801 FstringParser_check_invariants(state);
4802
4803 /* If we're just a constant string with no expressions, return
4804 that. */
4805 if(state->expr_list.size == 0) {
4806 if (!state->last_str) {
4807 /* Create a zero length string. */
4808 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4809 if (!state->last_str)
4810 goto error;
4811 }
4812 return make_str_node_and_del(&state->last_str, c, n);
4813 }
4814
4815 /* Create a Str node out of last_str, if needed. It will be the
4816 last node in our expression list. */
4817 if (state->last_str) {
4818 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4819 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4820 goto error;
4821 }
4822 /* This has already been freed. */
4823 assert(state->last_str == NULL);
4824
4825 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4826 if (!seq)
4827 goto error;
4828
4829 /* If there's only one expression, return it. Otherwise, we need
4830 to join them together. */
4831 if (seq->size == 1)
4832 return seq->elements[0];
4833
4834 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4835
4836error:
4837 FstringParser_Dealloc(state);
4838 return NULL;
4839}
4840
4841/* Given an f-string (with no 'f' or quotes) that's in str starting at
4842 ofs, parse it into an expr_ty. Return NULL on error. Does not
4843 decref str. */
4844static expr_ty
4845fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4846 struct compiling *c, const node *n)
4847{
4848 FstringParser state;
4849
4850 FstringParser_Init(&state);
4851 if (FstringParser_ConcatFstring(&state, str, ofs, recurse_lvl,
4852 c, n) < 0) {
4853 FstringParser_Dealloc(&state);
4854 return NULL;
4855 }
4856
4857 return FstringParser_Finish(&state, c, n);
4858}
4859
4860/* n is a Python string literal, including the bracketing quote
4861 characters, and r, b, u, &/or f prefixes (if any), and embedded
4862 escape sequences (if any). parsestr parses it, and returns the
4863 decoded Python string object. If the string is an f-string, set
4864 *fmode and return the unparsed string object.
4865*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004866static PyObject *
Eric V. Smith235a6f02015-09-19 14:51:32 -04004867parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004868{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004869 size_t len;
4870 const char *s = STR(n);
4871 int quote = Py_CHARMASK(*s);
4872 int rawmode = 0;
4873 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004874 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004875 while (!*bytesmode || !rawmode) {
4876 if (quote == 'b' || quote == 'B') {
4877 quote = *++s;
4878 *bytesmode = 1;
4879 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004880 else if (quote == 'u' || quote == 'U') {
4881 quote = *++s;
4882 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004883 else if (quote == 'r' || quote == 'R') {
4884 quote = *++s;
4885 rawmode = 1;
4886 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004887 else if (quote == 'f' || quote == 'F') {
4888 quote = *++s;
4889 *fmode = 1;
4890 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004891 else {
4892 break;
4893 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004894 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004895 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004896 if (*fmode && *bytesmode) {
4897 PyErr_BadInternalCall();
4898 return NULL;
4899 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004900 if (quote != '\'' && quote != '\"') {
4901 PyErr_BadInternalCall();
4902 return NULL;
4903 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004904 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004905 s++;
4906 len = strlen(s);
4907 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004909 "string to parse is too long");
4910 return NULL;
4911 }
4912 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004913 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004914 PyErr_BadInternalCall();
4915 return NULL;
4916 }
4917 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004918 /* A triple quoted string. We've already skipped one quote at
4919 the start and one at the end of the string. Now skip the
4920 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004921 s += 2;
4922 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004923 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004924 if (s[--len] != quote || s[--len] != quote) {
4925 PyErr_BadInternalCall();
4926 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004927 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004928 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00004929 if (!*bytesmode && !rawmode) {
Eric V. Smith5567f892015-09-21 13:36:09 -04004930 return decode_unicode(c, s, len, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004931 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004932 if (*bytesmode) {
4933 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004934 const char *ch;
4935 for (ch = s; *ch; ch++) {
4936 if (Py_CHARMASK(*ch) >= 0x80) {
4937 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004938 "literal characters.");
4939 return NULL;
4940 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004941 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004942 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00004943 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00004944 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004945 if (rawmode || strchr(s, '\\') == NULL) {
4946 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004947 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00004948 if (u == NULL || !*bytesmode)
4949 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004950 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004951 Py_DECREF(u);
4952 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004953 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00004954 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004955 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004956 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004958 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004959 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004960 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004961 return PyBytes_DecodeEscape(s, len, NULL, 1,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004962 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004963}
4964
Eric V. Smith235a6f02015-09-19 14:51:32 -04004965/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
4966 each STRING atom, and process it as needed. For bytes, just
4967 concatenate them together, and the result will be a Bytes node. For
4968 normal strings and f-strings, concatenate them together. The result
4969 will be a Str node if there were no f-strings; a FormattedValue
4970 node if there's just an f-string (with no leading or trailing
4971 literals), or a JoinedStr node if there are multiple f-strings or
4972 any literals involved. */
4973static expr_ty
4974parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004975{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004976 int bytesmode = 0;
4977 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004978 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979
4980 FstringParser state;
4981 FstringParser_Init(&state);
4982
4983 for (i = 0; i < NCH(n); i++) {
4984 int this_bytesmode = 0;
4985 int this_fmode = 0;
4986 PyObject *s;
4987
4988 REQ(CHILD(n, i), STRING);
4989 s = parsestr(c, CHILD(n, i), &this_bytesmode, &this_fmode);
4990 if (!s)
4991 goto error;
4992
4993 /* Check that we're not mixing bytes with unicode. */
4994 if (i != 0 && bytesmode != this_bytesmode) {
4995 ast_error(c, n, "cannot mix bytes and nonbytes literals");
4996 Py_DECREF(s);
4997 goto error;
4998 }
4999 bytesmode = this_bytesmode;
5000
5001 assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
5002
5003 if (bytesmode) {
5004 /* For bytes, concat as we go. */
5005 if (i == 0) {
5006 /* First time, just remember this value. */
5007 bytes_str = s;
5008 } else {
5009 PyBytes_ConcatAndDel(&bytes_str, s);
5010 if (!bytes_str)
5011 goto error;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005012 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013 } else if (this_fmode) {
5014 /* This is an f-string. Concatenate and decref it. */
5015 Py_ssize_t ofs = 0;
5016 int result = FstringParser_ConcatFstring(&state, s, &ofs, 0, c, n);
5017 Py_DECREF(s);
5018 if (result < 0)
5019 goto error;
5020 } else {
5021 /* This is a regular string. Concatenate it. */
5022 if (FstringParser_ConcatAndDel(&state, s) < 0)
5023 goto error;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005024 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005025 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 if (bytesmode) {
5027 /* Just return the bytes object and we're done. */
5028 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5029 goto error;
5030 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5031 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005032
Eric V. Smith235a6f02015-09-19 14:51:32 -04005033 /* We're not a bytes string, bytes_str should never have been set. */
5034 assert(bytes_str == NULL);
5035
5036 return FstringParser_Finish(&state, c, n);
5037
5038error:
5039 Py_XDECREF(bytes_str);
5040 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005042}