blob: 01d1f7aac063416121881947c94389a893855458 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
8#include "grammar.h"
9#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "ast.h"
12#include "token.h"
13#include "parsetok.h"
14#include "graminit.h"
15
16#include <assert.h>
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018/* Data structure used internally */
19struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000020 char *c_encoding; /* source encoding */
Christian Heimes3c608332008-03-26 22:01:37 +000021 int c_future_unicode; /* __future__ unicode literals flag */
Neal Norwitzadb69fc2005-12-17 20:54:49 +000022 PyArena *c_arena; /* arena for allocating memeory */
Christian Heimesffcd1e12007-11-24 01:36:02 +000023 const char *c_filename; /* filename */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024};
25
26static asdl_seq *seq_for_testlist(struct compiling *, const node *);
27static expr_ty ast_for_expr(struct compiling *, const node *);
28static stmt_ty ast_for_stmt(struct compiling *, const node *);
29static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000030static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
31 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000032static expr_ty ast_for_testlist(struct compiling *, const node *);
Christian Heimes5224d282008-02-23 15:01:05 +000033static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000034static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035
36/* Note different signature for ast_for_call */
37static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
38
Benjamin Peterson2b30ea02008-05-03 15:56:42 +000039static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes3c608332008-03-26 22:01:37 +000040static PyObject *parsestr(struct compiling *, const char *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041static PyObject *parsestrplus(struct compiling *, const node *n);
42
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000044#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#endif
46
Neal Norwitzadb69fc2005-12-17 20:54:49 +000047static identifier
48new_identifier(const char* n, PyArena *arena) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000049 PyObject* id = PyString_InternFromString(n);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +000050 if (id != NULL)
51 PyArena_AddPyObject(arena, id);
Neal Norwitzadb69fc2005-12-17 20:54:49 +000052 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053}
54
Neal Norwitzadb69fc2005-12-17 20:54:49 +000055#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056
57/* This routine provides an invalid object for the syntax error.
58 The outermost routine must unpack this error and create the
59 proper object. We do this so that we don't have to pass
60 the filename to everything function.
61
62 XXX Maybe we should just pass the filename...
63*/
64
65static int
66ast_error(const node *n, const char *errstr)
67{
68 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
69 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000070 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000071 PyErr_SetObject(PyExc_SyntaxError, u);
72 Py_DECREF(u);
73 return 0;
74}
75
76static void
77ast_error_finish(const char *filename)
78{
79 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000080 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
82 assert(PyErr_Occurred());
83 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000084 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085
86 PyErr_Fetch(&type, &value, &tback);
87 errstr = PyTuple_GetItem(value, 0);
88 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000089 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090 Py_INCREF(errstr);
91 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000092 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000093 Py_DECREF(errstr);
94 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000095 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096 Py_DECREF(value);
97
98 loc = PyErr_ProgramText(filename, lineno);
99 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000100 Py_INCREF(Py_None);
101 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000103 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000105 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000106 Py_DECREF(errstr);
107 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000108 }
Georg Brandl7784f122006-05-26 20:04:44 +0000109 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 Py_DECREF(errstr);
111 Py_DECREF(tmp);
112 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000113 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114 PyErr_Restore(type, value, tback);
115}
116
Benjamin Petersoncbd78132008-06-08 15:45:23 +0000117static int
118ast_warn(struct compiling *c, const node *n, char *msg)
119{
120 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
121 NULL, NULL) < 0) {
122 /* if -Werr, change it to a SyntaxError */
123 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
124 ast_error(n, msg);
125 return 0;
126 }
127 return 1;
128}
129
Benjamin Petersond5efd202008-06-08 22:52:37 +0000130static int
131forbidden_check(struct compiling *c, const node *n, const char *x)
132{
133 if (!strcmp(x, "None"))
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000134 return ast_error(n, "cannot assign to None");
135 if (!strcmp(x, "__debug__"))
136 return ast_error(n, "cannot assign to __debug__");
Benjamin Peterson399b1fe2008-10-25 02:53:28 +0000137 if (Py_Py3kWarningFlag) {
138 if (!(strcmp(x, "True") && strcmp(x, "False")) &&
139 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
140 return 0;
141 if (!strcmp(x, "nonlocal") &&
142 !ast_warn(c, n, "nonlocal is a keyword in 3.x"))
143 return 0;
144 }
Benjamin Petersond5efd202008-06-08 22:52:37 +0000145 return 1;
146}
147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148/* num_stmts() returns number of contained statements.
149
150 Use this routine to determine how big a sequence is needed for
151 the statements in a parse tree. Its raison d'etre is this bit of
152 grammar:
153
154 stmt: simple_stmt | compound_stmt
155 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
156
157 A simple_stmt can contain multiple small_stmt elements joined
158 by semicolons. If the arg is a simple_stmt, the number of
159 small_stmt elements is returned.
160*/
161
162static int
163num_stmts(const node *n)
164{
165 int i, l;
166 node *ch;
167
168 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000169 case single_input:
170 if (TYPE(CHILD(n, 0)) == NEWLINE)
171 return 0;
172 else
173 return num_stmts(CHILD(n, 0));
174 case file_input:
175 l = 0;
176 for (i = 0; i < NCH(n); i++) {
177 ch = CHILD(n, i);
178 if (TYPE(ch) == stmt)
179 l += num_stmts(ch);
180 }
181 return l;
182 case stmt:
183 return num_stmts(CHILD(n, 0));
184 case compound_stmt:
185 return 1;
186 case simple_stmt:
187 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
188 case suite:
189 if (NCH(n) == 1)
190 return num_stmts(CHILD(n, 0));
191 else {
192 l = 0;
193 for (i = 2; i < (NCH(n) - 1); i++)
194 l += num_stmts(CHILD(n, i));
195 return l;
196 }
197 default: {
198 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +0000200 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000201 TYPE(n), NCH(n));
202 Py_FatalError(buf);
203 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 }
205 assert(0);
206 return 0;
207}
208
209/* Transform the CST rooted at node * to the appropriate AST
210*/
211
212mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000213PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000214 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000216 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 asdl_seq *stmts = NULL;
218 stmt_ty s;
219 node *ch;
220 struct compiling c;
221
222 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000223 c.c_encoding = "utf-8";
224 if (TYPE(n) == encoding_decl) {
225 ast_error(n, "encoding declaration in Unicode string");
226 goto error;
227 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000229 c.c_encoding = STR(n);
230 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000232 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 }
Christian Heimes3c608332008-03-26 22:01:37 +0000234 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000236 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237
Jeremy Hyltona8293132006-02-28 17:58:27 +0000238 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000240 case file_input:
241 stmts = asdl_seq_new(num_stmts(n), arena);
242 if (!stmts)
243 return NULL;
244 for (i = 0; i < NCH(n) - 1; i++) {
245 ch = CHILD(n, i);
246 if (TYPE(ch) == NEWLINE)
247 continue;
248 REQ(ch, stmt);
249 num = num_stmts(ch);
250 if (num == 1) {
251 s = ast_for_stmt(&c, ch);
252 if (!s)
253 goto error;
254 asdl_seq_SET(stmts, k++, s);
255 }
256 else {
257 ch = CHILD(ch, 0);
258 REQ(ch, simple_stmt);
259 for (j = 0; j < num; j++) {
260 s = ast_for_stmt(&c, CHILD(ch, j * 2));
261 if (!s)
262 goto error;
263 asdl_seq_SET(stmts, k++, s);
264 }
265 }
266 }
267 return Module(stmts, arena);
268 case eval_input: {
269 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000271 /* XXX Why not gen_for here? */
272 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
273 if (!testlist_ast)
274 goto error;
275 return Expression(testlist_ast, arena);
276 }
277 case single_input:
278 if (TYPE(CHILD(n, 0)) == NEWLINE) {
279 stmts = asdl_seq_new(1, arena);
280 if (!stmts)
281 goto error;
282 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
283 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000284 if (!asdl_seq_GET(stmts, 0))
285 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000286 return Interactive(stmts, arena);
287 }
288 else {
289 n = CHILD(n, 0);
290 num = num_stmts(n);
291 stmts = asdl_seq_new(num, arena);
292 if (!stmts)
293 goto error;
294 if (num == 1) {
295 s = ast_for_stmt(&c, n);
296 if (!s)
297 goto error;
298 asdl_seq_SET(stmts, 0, s);
299 }
300 else {
301 /* Only a simple_stmt can contain multiple statements. */
302 REQ(n, simple_stmt);
303 for (i = 0; i < NCH(n); i += 2) {
304 if (TYPE(CHILD(n, i)) == NEWLINE)
305 break;
306 s = ast_for_stmt(&c, CHILD(n, i));
307 if (!s)
308 goto error;
309 asdl_seq_SET(stmts, i / 2, s);
310 }
311 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000313 return Interactive(stmts, arena);
314 }
315 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000316 PyErr_Format(PyExc_SystemError,
317 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000318 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319 }
320 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 ast_error_finish(filename);
322 return NULL;
323}
324
325/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
326*/
327
328static operator_ty
329get_operator(const node *n)
330{
331 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000332 case VBAR:
333 return BitOr;
334 case CIRCUMFLEX:
335 return BitXor;
336 case AMPER:
337 return BitAnd;
338 case LEFTSHIFT:
339 return LShift;
340 case RIGHTSHIFT:
341 return RShift;
342 case PLUS:
343 return Add;
344 case MINUS:
345 return Sub;
346 case STAR:
347 return Mult;
348 case SLASH:
349 return Div;
350 case DOUBLESLASH:
351 return FloorDiv;
352 case PERCENT:
353 return Mod;
354 default:
355 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356 }
357}
358
Jeremy Hyltona8293132006-02-28 17:58:27 +0000359/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360
361 Only sets context for expr kinds that "can appear in assignment context"
362 (according to ../Parser/Python.asdl). For other expr kinds, it sets
363 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364*/
365
366static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000367set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368{
369 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000370 /* If a particular expression type can't be used for assign / delete,
371 set expr_name to its name and an error message will be generated.
372 */
373 const char* expr_name = NULL;
374
375 /* The ast defines augmented store and load contexts, but the
376 implementation here doesn't actually use them. The code may be
377 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000378 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000380 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000381 */
382 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
384 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000385 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000386 if (ctx == Store && !forbidden_check(c, n,
387 PyBytes_AS_STRING(e->v.Attribute.attr)))
388 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000389 e->v.Attribute.ctx = ctx;
390 break;
391 case Subscript_kind:
392 e->v.Subscript.ctx = ctx;
393 break;
394 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000395 if (ctx == Store && !forbidden_check(c, n,
396 PyBytes_AS_STRING(e->v.Name.id)))
397 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000398 e->v.Name.ctx = ctx;
399 break;
400 case List_kind:
401 e->v.List.ctx = ctx;
402 s = e->v.List.elts;
403 break;
404 case Tuple_kind:
Benjamin Peterson52c4bec2009-06-13 17:08:53 +0000405 if (asdl_seq_LEN(e->v.Tuple.elts)) {
406 e->v.Tuple.ctx = ctx;
407 s = e->v.Tuple.elts;
408 }
409 else {
410 expr_name = "()";
411 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000412 break;
413 case Lambda_kind:
414 expr_name = "lambda";
415 break;
416 case Call_kind:
417 expr_name = "function call";
418 break;
419 case BoolOp_kind:
420 case BinOp_kind:
421 case UnaryOp_kind:
422 expr_name = "operator";
423 break;
424 case GeneratorExp_kind:
425 expr_name = "generator expression";
426 break;
427 case Yield_kind:
428 expr_name = "yield expression";
429 break;
430 case ListComp_kind:
431 expr_name = "list comprehension";
432 break;
433 case Dict_kind:
434 case Num_kind:
435 case Str_kind:
436 expr_name = "literal";
437 break;
438 case Compare_kind:
439 expr_name = "comparison";
440 break;
441 case Repr_kind:
442 expr_name = "repr";
443 break;
444 case IfExp_kind:
445 expr_name = "conditional expression";
446 break;
447 default:
448 PyErr_Format(PyExc_SystemError,
449 "unexpected expression in assignment %d (line %d)",
450 e->kind, e->lineno);
451 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000453 /* Check for error string set by switch */
454 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000455 char buf[300];
456 PyOS_snprintf(buf, sizeof(buf),
457 "can't %s %s",
458 ctx == Store ? "assign to" : "delete",
459 expr_name);
460 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000461 }
462
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000464 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465 */
466 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000467 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000469 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000470 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000471 return 0;
472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473 }
474 return 1;
475}
476
477static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000478ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479{
480 REQ(n, augassign);
481 n = CHILD(n, 0);
482 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000483 case '+':
484 return Add;
485 case '-':
486 return Sub;
487 case '/':
488 if (STR(n)[1] == '/')
489 return FloorDiv;
490 else
491 return Div;
492 case '%':
493 return Mod;
494 case '<':
495 return LShift;
496 case '>':
497 return RShift;
498 case '&':
499 return BitAnd;
500 case '^':
501 return BitXor;
502 case '|':
503 return BitOr;
504 case '*':
505 if (STR(n)[1] == '*')
506 return Pow;
507 else
508 return Mult;
509 default:
510 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
511 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 }
513}
514
515static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000516ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517{
518 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000519 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 */
521 REQ(n, comp_op);
522 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000523 n = CHILD(n, 0);
524 switch (TYPE(n)) {
525 case LESS:
526 return Lt;
527 case GREATER:
528 return Gt;
529 case EQEQUAL: /* == */
530 return Eq;
531 case LESSEQUAL:
532 return LtE;
533 case GREATEREQUAL:
534 return GtE;
535 case NOTEQUAL:
536 return NotEq;
537 case NAME:
538 if (strcmp(STR(n), "in") == 0)
539 return In;
540 if (strcmp(STR(n), "is") == 0)
541 return Is;
542 default:
543 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
544 STR(n));
545 return (cmpop_ty)0;
546 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 }
548 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000549 /* handle "not in" and "is not" */
550 switch (TYPE(CHILD(n, 0))) {
551 case NAME:
552 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
553 return NotIn;
554 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
555 return IsNot;
556 default:
557 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
558 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
559 return (cmpop_ty)0;
560 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 }
Neal Norwitz79792652005-11-14 04:25:03 +0000562 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000563 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000564 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565}
566
567static asdl_seq *
568seq_for_testlist(struct compiling *c, const node *n)
569{
570 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000571 asdl_seq *seq;
572 expr_ty expression;
573 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000574 assert(TYPE(n) == testlist ||
575 TYPE(n) == listmaker ||
576 TYPE(n) == testlist_gexp ||
577 TYPE(n) == testlist_safe ||
578 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000580 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
584 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000585 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000587 expression = ast_for_expr(c, CHILD(n, i));
588 if (!expression)
589 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000591 assert(i / 2 < seq->size);
592 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 }
594 return seq;
595}
596
597static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000598compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599{
600 int i, len = (NCH(n) + 1) / 2;
601 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000602 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000604 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
Neal Norwitz3a230172006-09-22 08:18:10 +0000606 /* fpdef: NAME | '(' fplist ')'
607 fplist: fpdef (',' fpdef)* [',']
608 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 for (i = 0; i < len; i++) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000611 PyObject *arg_id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000612 const node *fpdef_node = CHILD(n, 2*i);
613 const node *child;
614 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000615set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000616 /* fpdef_node is either a NAME or an fplist */
617 child = CHILD(fpdef_node, 0);
618 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000619 if (!forbidden_check(c, n, STR(child)))
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000620 return NULL;
621 arg_id = NEW_IDENTIFIER(child);
622 if (!arg_id)
623 return NULL;
624 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
625 c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000626 }
627 else {
628 assert(TYPE(fpdef_node) == fpdef);
629 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
630 child = CHILD(fpdef_node, 1);
631 assert(TYPE(child) == fplist);
632 /* NCH == 1 means we have (x), we need to elide the extra parens */
633 if (NCH(child) == 1) {
634 fpdef_node = CHILD(child, 0);
635 assert(TYPE(fpdef_node) == fpdef);
636 goto set_name;
637 }
638 arg = compiler_complex_args(c, child);
639 }
640 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 }
642
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000643 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000644 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 return result;
647}
648
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649
Jeremy Hyltona8293132006-02-28 17:58:27 +0000650/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651
652static arguments_ty
653ast_for_arguments(struct compiling *c, const node *n)
654{
655 /* parameters: '(' [varargslist] ')'
656 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000657 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000659 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660 asdl_seq *args, *defaults;
661 identifier vararg = NULL, kwarg = NULL;
662 node *ch;
663
664 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000665 if (NCH(n) == 2) /* () as argument list */
666 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
667 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 }
669 REQ(n, varargslist);
670
671 /* first count the number of normal args & defaults */
672 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000673 ch = CHILD(n, i);
674 if (TYPE(ch) == fpdef)
675 n_args++;
676 if (TYPE(ch) == EQUAL)
677 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000679 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000681 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000682 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000684 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685
686 /* fpdef: NAME | '(' fplist ')'
687 fplist: fpdef (',' fpdef)* [',']
688 */
689 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000690 j = 0; /* index for defaults */
691 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000693 ch = CHILD(n, i);
694 switch (TYPE(ch)) {
Benjamin Peterson99a50232009-11-19 22:54:57 +0000695 case fpdef: {
696 int complex_args = 0, parenthesized = 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000697 handle_fpdef:
698 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
699 anything other than EQUAL or a comma? */
700 /* XXX Should NCH(n) check be made a separate check? */
701 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
702 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
703 if (!expression)
704 goto error;
705 assert(defaults != NULL);
706 asdl_seq_SET(defaults, j++, expression);
707 i += 2;
708 found_default = 1;
709 }
710 else if (found_default) {
Benjamin Peterson99a50232009-11-19 22:54:57 +0000711 /* def f((x)=4): pass should raise an error.
712 def f((x, (y))): pass will just incur the tuple unpacking warning. */
713 if (parenthesized && !complex_args) {
714 ast_error(n, "parenthesized arg with default");
715 goto error;
716 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000717 ast_error(n,
718 "non-default argument follows default argument");
719 goto error;
720 }
721 if (NCH(ch) == 3) {
722 ch = CHILD(ch, 1);
723 /* def foo((x)): is not complex, special case. */
724 if (NCH(ch) != 1) {
725 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000726 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
727 "tuple parameter unpacking has been removed in 3.x"))
728 goto error;
Benjamin Peterson99a50232009-11-19 22:54:57 +0000729 complex_args = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000730 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000731 if (!asdl_seq_GET(args, k-1))
732 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000733 } else {
734 /* def foo((x)): setup for checking NAME below. */
735 /* Loop because there can be many parens and tuple
736 unpacking mixed in. */
Benjamin Peterson99a50232009-11-19 22:54:57 +0000737 parenthesized = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000738 ch = CHILD(ch, 0);
739 assert(TYPE(ch) == fpdef);
740 goto handle_fpdef;
741 }
742 }
743 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000744 PyObject *id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000745 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000746 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000747 goto error;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000748 id = NEW_IDENTIFIER(CHILD(ch, 0));
749 if (!id)
750 goto error;
751 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000752 c->c_arena);
753 if (!name)
754 goto error;
755 asdl_seq_SET(args, k++, name);
756
757 }
758 i += 2; /* the name and the comma */
Benjamin Peterson99a50232009-11-19 22:54:57 +0000759 if (parenthesized && Py_Py3kWarningFlag &&
760 !ast_warn(c, ch, "parenthesized argument names "
761 "are invalid in 3.x"))
762 goto error;
763
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000764 break;
Benjamin Peterson99a50232009-11-19 22:54:57 +0000765 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000766 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000767 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000768 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000769 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000770 if (!vararg)
771 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000772 i += 3;
773 break;
774 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000775 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000776 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000777 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000778 if (!kwarg)
779 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000780 i += 3;
781 break;
782 default:
783 PyErr_Format(PyExc_SystemError,
784 "unexpected node in varargslist: %d @ %d",
785 TYPE(ch), i);
786 goto error;
787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 }
789
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000790 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
792 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000793 Py_XDECREF(vararg);
794 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 return NULL;
796}
797
798static expr_ty
799ast_for_dotted_name(struct compiling *c, const node *n)
800{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000801 expr_ty e;
802 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000803 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 int i;
805
806 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000807
808 lineno = LINENO(n);
809 col_offset = n->n_col_offset;
810
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 id = NEW_IDENTIFIER(CHILD(n, 0));
812 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000813 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000814 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817
818 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000819 id = NEW_IDENTIFIER(CHILD(n, i));
820 if (!id)
821 return NULL;
822 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
823 if (!e)
824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 }
826
827 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828}
829
830static expr_ty
831ast_for_decorator(struct compiling *c, const node *n)
832{
833 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
834 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000835 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836
837 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000838 REQ(CHILD(n, 0), AT);
839 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840
841 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
842 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000843 return NULL;
844
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000846 d = name_expr;
847 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 }
849 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000850 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
851 n->n_col_offset, c->c_arena);
852 if (!d)
853 return NULL;
854 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 }
856 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000857 d = ast_for_call(c, CHILD(n, 3), name_expr);
858 if (!d)
859 return NULL;
860 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 }
862
863 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864}
865
866static asdl_seq*
867ast_for_decorators(struct compiling *c, const node *n)
868{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000869 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000870 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 int i;
872
873 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000874 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000876 return NULL;
877
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000879 d = ast_for_decorator(c, CHILD(n, i));
880 if (!d)
881 return NULL;
882 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 }
884 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885}
886
887static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000888ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889{
Christian Heimes5224d282008-02-23 15:01:05 +0000890 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000891 identifier name;
892 arguments_ty args;
893 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000894 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895
896 REQ(n, funcdef);
897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898 name = NEW_IDENTIFIER(CHILD(n, name_i));
899 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000900 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000901 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000902 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 args = ast_for_arguments(c, CHILD(n, name_i + 1));
904 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000905 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 body = ast_for_suite(c, CHILD(n, name_i + 3));
907 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000910 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000911 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912}
913
Christian Heimes5224d282008-02-23 15:01:05 +0000914static stmt_ty
915ast_for_decorated(struct compiling *c, const node *n)
916{
917 /* decorated: decorators (classdef | funcdef) */
918 stmt_ty thing = NULL;
919 asdl_seq *decorator_seq = NULL;
920
921 REQ(n, decorated);
922
923 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
924 if (!decorator_seq)
925 return NULL;
926
927 assert(TYPE(CHILD(n, 1)) == funcdef ||
928 TYPE(CHILD(n, 1)) == classdef);
929
930 if (TYPE(CHILD(n, 1)) == funcdef) {
931 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
932 } else if (TYPE(CHILD(n, 1)) == classdef) {
933 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
934 }
935 /* we count the decorators in when talking about the class' or
936 function's line number */
937 if (thing) {
938 thing->lineno = LINENO(n);
939 thing->col_offset = n->n_col_offset;
940 }
941 return thing;
942}
943
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944static expr_ty
945ast_for_lambdef(struct compiling *c, const node *n)
946{
947 /* lambdef: 'lambda' [varargslist] ':' test */
948 arguments_ty args;
949 expr_ty expression;
950
951 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000952 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
953 if (!args)
954 return NULL;
955 expression = ast_for_expr(c, CHILD(n, 2));
956 if (!expression)
957 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 }
959 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000960 args = ast_for_arguments(c, CHILD(n, 1));
961 if (!args)
962 return NULL;
963 expression = ast_for_expr(c, CHILD(n, 3));
964 if (!expression)
965 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 }
967
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000968 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969}
970
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000971static expr_ty
972ast_for_ifexpr(struct compiling *c, const node *n)
973{
974 /* test: or_test 'if' or_test 'else' test */
975 expr_ty expression, body, orelse;
976
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000977 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000978 body = ast_for_expr(c, CHILD(n, 0));
979 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000980 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000981 expression = ast_for_expr(c, CHILD(n, 2));
982 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000983 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000984 orelse = ast_for_expr(c, CHILD(n, 4));
985 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000986 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000987 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000988 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000989}
990
Neal Norwitze4d4f002006-09-05 03:58:26 +0000991/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
992 so there is only a single version. Possibly for loops can also re-use
993 the code.
994*/
995
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996/* Count the number of 'for' loop in a list comprehension.
997
998 Helper for ast_for_listcomp().
999*/
1000
1001static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001002count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003{
1004 int n_fors = 0;
1005 node *ch = CHILD(n, 1);
1006
1007 count_list_for:
1008 n_fors++;
1009 REQ(ch, list_for);
1010 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001011 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001013 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 count_list_iter:
1015 REQ(ch, list_iter);
1016 ch = CHILD(ch, 0);
1017 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001018 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001020 if (NCH(ch) == 3) {
1021 ch = CHILD(ch, 2);
1022 goto count_list_iter;
1023 }
1024 else
1025 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001027
1028 /* Should never be reached */
1029 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1030 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031}
1032
1033/* Count the number of 'if' statements in a list comprehension.
1034
1035 Helper for ast_for_listcomp().
1036*/
1037
1038static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001039count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040{
1041 int n_ifs = 0;
1042
1043 count_list_iter:
1044 REQ(n, list_iter);
1045 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001046 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 n = CHILD(n, 0);
1048 REQ(n, list_if);
1049 n_ifs++;
1050 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001051 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 n = CHILD(n, 2);
1053 goto count_list_iter;
1054}
1055
1056static expr_ty
1057ast_for_listcomp(struct compiling *c, const node *n)
1058{
1059 /* listmaker: test ( list_for | (',' test)* [','] )
1060 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1061 list_iter: list_for | list_if
1062 list_if: 'if' test [list_iter]
1063 testlist_safe: test [(',' test)+ [',']]
1064 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001065 expr_ty elt, first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 asdl_seq *listcomps;
1067 int i, n_fors;
1068 node *ch;
1069
1070 REQ(n, listmaker);
1071 assert(NCH(n) > 1);
1072
1073 elt = ast_for_expr(c, CHILD(n, 0));
1074 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001075 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001077 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001079 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001081 listcomps = asdl_seq_new(n_fors, c->c_arena);
1082 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001083 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 ch = CHILD(n, 1);
1086 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001087 comprehension_ty lc;
1088 asdl_seq *t;
1089 expr_ty expression;
1090 node *for_ch;
1091
1092 REQ(ch, list_for);
1093
1094 for_ch = CHILD(ch, 1);
1095 t = ast_for_exprlist(c, for_ch, Store);
1096 if (!t)
1097 return NULL;
1098 expression = ast_for_testlist(c, CHILD(ch, 3));
1099 if (!expression)
1100 return NULL;
1101
1102 /* Check the # of children rather than the length of t, since
1103 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1104 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001105 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001106 if (NCH(for_ch) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001107 lc = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001108 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001109 lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001110 c->c_arena),
1111 expression, NULL, c->c_arena);
1112 if (!lc)
1113 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001115 if (NCH(ch) == 5) {
1116 int j, n_ifs;
1117 asdl_seq *ifs;
1118 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001120 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001121 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001122 if (n_ifs == -1)
1123 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001125 ifs = asdl_seq_new(n_ifs, c->c_arena);
1126 if (!ifs)
1127 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001129 for (j = 0; j < n_ifs; j++) {
1130 REQ(ch, list_iter);
1131 ch = CHILD(ch, 0);
1132 REQ(ch, list_if);
1133
1134 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1135 if (!list_for_expr)
1136 return NULL;
1137
1138 asdl_seq_SET(ifs, j, list_for_expr);
1139 if (NCH(ch) == 3)
1140 ch = CHILD(ch, 2);
1141 }
1142 /* on exit, must guarantee that ch is a list_for */
1143 if (TYPE(ch) == list_iter)
1144 ch = CHILD(ch, 0);
1145 lc->ifs = ifs;
1146 }
1147 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 }
1149
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001150 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151}
1152
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001153/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154
1155 Helper for ast_for_genexp().
1156*/
1157
1158static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001159count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001161 int n_fors = 0;
1162 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163
1164 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001165 n_fors++;
1166 REQ(ch, gen_for);
1167 if (NCH(ch) == 5)
1168 ch = CHILD(ch, 4);
1169 else
1170 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001172 REQ(ch, gen_iter);
1173 ch = CHILD(ch, 0);
1174 if (TYPE(ch) == gen_for)
1175 goto count_gen_for;
1176 else if (TYPE(ch) == gen_if) {
1177 if (NCH(ch) == 3) {
1178 ch = CHILD(ch, 2);
1179 goto count_gen_iter;
1180 }
1181 else
1182 return n_fors;
1183 }
1184
1185 /* Should never be reached */
1186 PyErr_SetString(PyExc_SystemError,
1187 "logic error in count_gen_fors");
1188 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189}
1190
1191/* Count the number of 'if' statements in a generator expression.
1192
1193 Helper for ast_for_genexp().
1194*/
1195
1196static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001197count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001199 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001201 while (1) {
1202 REQ(n, gen_iter);
1203 if (TYPE(CHILD(n, 0)) == gen_for)
1204 return n_ifs;
1205 n = CHILD(n, 0);
1206 REQ(n, gen_if);
1207 n_ifs++;
1208 if (NCH(n) == 2)
1209 return n_ifs;
1210 n = CHILD(n, 2);
1211 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212}
1213
Jeremy Hyltona8293132006-02-28 17:58:27 +00001214/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215static expr_ty
1216ast_for_genexp(struct compiling *c, const node *n)
1217{
1218 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001219 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 expr_ty elt;
1221 asdl_seq *genexps;
1222 int i, n_fors;
1223 node *ch;
1224
1225 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1226 assert(NCH(n) > 1);
1227
1228 elt = ast_for_expr(c, CHILD(n, 0));
1229 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001230 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001232 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001234 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001235
1236 genexps = asdl_seq_new(n_fors, c->c_arena);
1237 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001238 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 ch = CHILD(n, 1);
1241 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001242 comprehension_ty ge;
1243 asdl_seq *t;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001244 expr_ty expression, first;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001245 node *for_ch;
1246
1247 REQ(ch, gen_for);
1248
1249 for_ch = CHILD(ch, 1);
1250 t = ast_for_exprlist(c, for_ch, Store);
1251 if (!t)
1252 return NULL;
1253 expression = ast_for_expr(c, CHILD(ch, 3));
1254 if (!expression)
1255 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001256
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001257 /* Check the # of children rather than the length of t, since
1258 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001259 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001260 if (NCH(for_ch) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001261 ge = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001262 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001263 ge = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001264 c->c_arena),
1265 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001266
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001267 if (!ge)
1268 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001269
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001270 if (NCH(ch) == 5) {
1271 int j, n_ifs;
1272 asdl_seq *ifs;
1273
1274 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001275 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001276 if (n_ifs == -1)
1277 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001278
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001279 ifs = asdl_seq_new(n_ifs, c->c_arena);
1280 if (!ifs)
1281 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001282
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001283 for (j = 0; j < n_ifs; j++) {
1284 REQ(ch, gen_iter);
1285 ch = CHILD(ch, 0);
1286 REQ(ch, gen_if);
1287
1288 expression = ast_for_expr(c, CHILD(ch, 1));
1289 if (!expression)
1290 return NULL;
1291 asdl_seq_SET(ifs, j, expression);
1292 if (NCH(ch) == 3)
1293 ch = CHILD(ch, 2);
1294 }
1295 /* on exit, must guarantee that ch is a gen_for */
1296 if (TYPE(ch) == gen_iter)
1297 ch = CHILD(ch, 0);
1298 ge->ifs = ifs;
1299 }
1300 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 }
1302
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001303 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304}
1305
1306static expr_ty
1307ast_for_atom(struct compiling *c, const node *n)
1308{
1309 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1310 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1311 */
1312 node *ch = CHILD(n, 0);
1313
1314 switch (TYPE(ch)) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001315 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001316 /* All names start in Load context, but may later be
1317 changed. */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001318 PyObject *name = NEW_IDENTIFIER(ch);
1319 if (!name)
1320 return NULL;
1321 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1322 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001324 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001325 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001326#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001327 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1328 PyObject *type, *value, *tback, *errstr;
1329 PyErr_Fetch(&type, &value, &tback);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001330 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001331 if (errstr) {
1332 char *s = "";
1333 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001334 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001335 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1336 ast_error(n, buf);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001337 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001338 } else {
1339 ast_error(n, "(unicode error) unknown error");
1340 }
1341 Py_DECREF(type);
1342 Py_DECREF(value);
1343 Py_XDECREF(tback);
1344 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001345#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001346 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001347 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001348 PyArena_AddPyObject(c->c_arena, str);
1349 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 }
1351 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001352 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001353 if (!pynum)
1354 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001355
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001356 PyArena_AddPyObject(c->c_arena, pynum);
1357 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 }
1359 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001360 ch = CHILD(n, 1);
1361
1362 if (TYPE(ch) == RPAR)
1363 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1364
1365 if (TYPE(ch) == yield_expr)
1366 return ast_for_expr(c, ch);
1367
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001368 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001370 ch = CHILD(n, 1);
1371
1372 if (TYPE(ch) == RSQB)
1373 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1374
1375 REQ(ch, listmaker);
1376 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1377 asdl_seq *elts = seq_for_testlist(c, ch);
1378 if (!elts)
1379 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001380
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001381 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1382 }
1383 else
1384 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 case LBRACE: {
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001386 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
1387 * test (',' test)* [','])
1388 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001389 int i, size;
1390 asdl_seq *keys, *values;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001391
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001392 ch = CHILD(n, 1);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001393 if (TYPE(ch) == RBRACE) {
1394 /* it's an empty dict */
1395 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
1396 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1397 /* it's a simple set */
1398 asdl_seq *elts;
1399 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
1400 elts = asdl_seq_new(size, c->c_arena);
1401 if (!elts)
1402 return NULL;
1403 for (i = 0; i < NCH(ch); i += 2) {
1404 expr_ty expression;
1405 expression = ast_for_expr(c, CHILD(ch, i));
1406 if (!expression)
1407 return NULL;
1408 asdl_seq_SET(elts, i / 2, expression);
1409 }
1410 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1411 } else {
1412 /* it's a dict */
1413 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1414 keys = asdl_seq_new(size, c->c_arena);
1415 if (!keys)
1416 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001417
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001418 values = asdl_seq_new(size, c->c_arena);
1419 if (!values)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001420 return NULL;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001421
1422 for (i = 0; i < NCH(ch); i += 4) {
1423 expr_ty expression;
1424
1425 expression = ast_for_expr(c, CHILD(ch, i));
1426 if (!expression)
1427 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001428
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001429 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001430
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001431 expression = ast_for_expr(c, CHILD(ch, i + 2));
1432 if (!expression)
1433 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001434
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001435 asdl_seq_SET(values, i / 4, expression);
1436 }
1437 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001438 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 }
1440 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001441 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001442 if (Py_Py3kWarningFlag &&
1443 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001444 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001445 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001446 if (!expression)
1447 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001448
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001449 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 }
1451 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001452 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1453 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 }
1455}
1456
1457static slice_ty
1458ast_for_slice(struct compiling *c, const node *n)
1459{
1460 node *ch;
1461 expr_ty lower = NULL, upper = NULL, step = NULL;
1462
1463 REQ(n, subscript);
1464
1465 /*
1466 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1467 sliceop: ':' [test]
1468 */
1469 ch = CHILD(n, 0);
1470 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001471 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472
1473 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001474 /* 'step' variable hold no significance in terms of being used over
1475 other vars */
1476 step = ast_for_expr(c, ch);
1477 if (!step)
1478 return NULL;
1479
1480 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 }
1482
1483 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001484 lower = ast_for_expr(c, ch);
1485 if (!lower)
1486 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 }
1488
1489 /* If there's an upper bound it's in the second or third position. */
1490 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001491 if (NCH(n) > 1) {
1492 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001494 if (TYPE(n2) == test) {
1495 upper = ast_for_expr(c, n2);
1496 if (!upper)
1497 return NULL;
1498 }
1499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001501 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001503 if (TYPE(n2) == test) {
1504 upper = ast_for_expr(c, n2);
1505 if (!upper)
1506 return NULL;
1507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 }
1509
1510 ch = CHILD(n, NCH(n) - 1);
1511 if (TYPE(ch) == sliceop) {
Benjamin Peterson4879c902009-07-20 20:28:08 +00001512 if (NCH(ch) == 1) {
1513 /*
1514 This is an extended slice (ie "x[::]") with no expression in the
1515 step field. We set this literally to "None" in order to
1516 disambiguate it from x[:]. (The interpreter might have to call
1517 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1518 */
1519 identifier none = new_identifier("None", c->c_arena);
1520 if (!none)
1521 return NULL;
1522 ch = CHILD(ch, 0);
1523 step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);
1524 if (!step)
1525 return NULL;
1526 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001527 ch = CHILD(ch, 1);
1528 if (TYPE(ch) == test) {
1529 step = ast_for_expr(c, ch);
1530 if (!step)
1531 return NULL;
1532 }
1533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 }
1535
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001536 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537}
1538
1539static expr_ty
1540ast_for_binop(struct compiling *c, const node *n)
1541{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001542 /* Must account for a sequence of expressions.
1543 How should A op B op C by represented?
1544 BinOp(BinOp(A, op, B), op, C).
1545 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001547 int i, nops;
1548 expr_ty expr1, expr2, result;
1549 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001551 expr1 = ast_for_expr(c, CHILD(n, 0));
1552 if (!expr1)
1553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001555 expr2 = ast_for_expr(c, CHILD(n, 2));
1556 if (!expr2)
1557 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001559 newoperator = get_operator(CHILD(n, 1));
1560 if (!newoperator)
1561 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001563 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1564 c->c_arena);
1565 if (!result)
1566 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001568 nops = (NCH(n) - 1) / 2;
1569 for (i = 1; i < nops; i++) {
1570 expr_ty tmp_result, tmp;
1571 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001573 newoperator = get_operator(next_oper);
1574 if (!newoperator)
1575 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001577 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1578 if (!tmp)
1579 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001581 tmp_result = BinOp(result, newoperator, tmp,
1582 LINENO(next_oper), next_oper->n_col_offset,
1583 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001584 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001585 return NULL;
1586 result = tmp_result;
1587 }
1588 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589}
1590
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001591static expr_ty
1592ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1593{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001594 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1595 subscriptlist: subscript (',' subscript)* [',']
1596 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1597 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001598 REQ(n, trailer);
1599 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001600 if (NCH(n) == 2)
1601 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1602 n->n_col_offset, c->c_arena);
1603 else
1604 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001605 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001606 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001607 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1608 if (!attr_id)
1609 return NULL;
1610 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001611 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001612 }
1613 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001614 REQ(CHILD(n, 0), LSQB);
1615 REQ(CHILD(n, 2), RSQB);
1616 n = CHILD(n, 1);
1617 if (NCH(n) == 1) {
1618 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1619 if (!slc)
1620 return NULL;
1621 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1622 c->c_arena);
1623 }
1624 else {
1625 /* The grammar is ambiguous here. The ambiguity is resolved
1626 by treating the sequence as a tuple literal if there are
1627 no slice features.
1628 */
1629 int j;
1630 slice_ty slc;
1631 expr_ty e;
1632 bool simple = true;
1633 asdl_seq *slices, *elts;
1634 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1635 if (!slices)
1636 return NULL;
1637 for (j = 0; j < NCH(n); j += 2) {
1638 slc = ast_for_slice(c, CHILD(n, j));
1639 if (!slc)
1640 return NULL;
1641 if (slc->kind != Index_kind)
1642 simple = false;
1643 asdl_seq_SET(slices, j / 2, slc);
1644 }
1645 if (!simple) {
1646 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1647 Load, LINENO(n), n->n_col_offset, c->c_arena);
1648 }
1649 /* extract Index values and put them in a Tuple */
1650 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1651 if (!elts)
1652 return NULL;
1653 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1654 slc = (slice_ty)asdl_seq_GET(slices, j);
1655 assert(slc->kind == Index_kind && slc->v.Index.value);
1656 asdl_seq_SET(elts, j, slc->v.Index.value);
1657 }
1658 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1659 if (!e)
1660 return NULL;
1661 return Subscript(left_expr, Index(e, c->c_arena),
1662 Load, LINENO(n), n->n_col_offset, c->c_arena);
1663 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001664 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001665}
1666
1667static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001668ast_for_factor(struct compiling *c, const node *n)
1669{
1670 node *pfactor, *ppower, *patom, *pnum;
1671 expr_ty expression;
1672
1673 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001674 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001675 constant. The peephole optimizer already does something like
1676 this but it doesn't handle the case where the constant is
1677 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1678 PyLongObject.
1679 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001680 if (TYPE(CHILD(n, 0)) == MINUS &&
1681 NCH(n) == 2 &&
1682 TYPE((pfactor = CHILD(n, 1))) == factor &&
1683 NCH(pfactor) == 1 &&
1684 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1685 NCH(ppower) == 1 &&
1686 TYPE((patom = CHILD(ppower, 0))) == atom &&
1687 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1688 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1689 if (s == NULL)
1690 return NULL;
1691 s[0] = '-';
1692 strcpy(s + 1, STR(pnum));
1693 PyObject_FREE(STR(pnum));
1694 STR(pnum) = s;
1695 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001696 }
1697
1698 expression = ast_for_expr(c, CHILD(n, 1));
1699 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001700 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001701
1702 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001703 case PLUS:
1704 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1705 c->c_arena);
1706 case MINUS:
1707 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1708 c->c_arena);
1709 case TILDE:
1710 return UnaryOp(Invert, expression, LINENO(n),
1711 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001712 }
1713 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001714 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001715 return NULL;
1716}
1717
1718static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001719ast_for_power(struct compiling *c, const node *n)
1720{
1721 /* power: atom trailer* ('**' factor)*
1722 */
1723 int i;
1724 expr_ty e, tmp;
1725 REQ(n, power);
1726 e = ast_for_atom(c, CHILD(n, 0));
1727 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001728 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001729 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001730 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001731 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001732 node *ch = CHILD(n, i);
1733 if (TYPE(ch) != trailer)
1734 break;
1735 tmp = ast_for_trailer(c, ch, e);
1736 if (!tmp)
1737 return NULL;
1738 tmp->lineno = e->lineno;
1739 tmp->col_offset = e->col_offset;
1740 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001741 }
1742 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001743 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1744 if (!f)
1745 return NULL;
1746 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1747 if (!tmp)
1748 return NULL;
1749 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001750 }
1751 return e;
1752}
1753
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754/* Do not name a variable 'expr'! Will cause a compile error.
1755*/
1756
1757static expr_ty
1758ast_for_expr(struct compiling *c, const node *n)
1759{
1760 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001761 test: or_test ['if' or_test 'else' test] | lambdef
1762 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 and_test: not_test ('and' not_test)*
1764 not_test: 'not' not_test | comparison
1765 comparison: expr (comp_op expr)*
1766 expr: xor_expr ('|' xor_expr)*
1767 xor_expr: and_expr ('^' and_expr)*
1768 and_expr: shift_expr ('&' shift_expr)*
1769 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1770 arith_expr: term (('+'|'-') term)*
1771 term: factor (('*'|'/'|'%'|'//') factor)*
1772 factor: ('+'|'-'|'~') factor | power
1773 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001774
1775 As well as modified versions that exist for backward compatibility,
1776 to explicitly allow:
1777 [ x for x in lambda: 0, lambda: 1 ]
1778 (which would be ambiguous without these extra rules)
1779
1780 old_test: or_test | old_lambdef
1781 old_lambdef: 'lambda' [vararglist] ':' old_test
1782
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783 */
1784
1785 asdl_seq *seq;
1786 int i;
1787
1788 loop:
1789 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001790 case test:
1791 case old_test:
1792 if (TYPE(CHILD(n, 0)) == lambdef ||
1793 TYPE(CHILD(n, 0)) == old_lambdef)
1794 return ast_for_lambdef(c, CHILD(n, 0));
1795 else if (NCH(n) > 1)
1796 return ast_for_ifexpr(c, n);
1797 /* Fallthrough */
1798 case or_test:
1799 case and_test:
1800 if (NCH(n) == 1) {
1801 n = CHILD(n, 0);
1802 goto loop;
1803 }
1804 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1805 if (!seq)
1806 return NULL;
1807 for (i = 0; i < NCH(n); i += 2) {
1808 expr_ty e = ast_for_expr(c, CHILD(n, i));
1809 if (!e)
1810 return NULL;
1811 asdl_seq_SET(seq, i / 2, e);
1812 }
1813 if (!strcmp(STR(CHILD(n, 1)), "and"))
1814 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1815 c->c_arena);
1816 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1817 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1818 case not_test:
1819 if (NCH(n) == 1) {
1820 n = CHILD(n, 0);
1821 goto loop;
1822 }
1823 else {
1824 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1825 if (!expression)
1826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001828 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1829 c->c_arena);
1830 }
1831 case comparison:
1832 if (NCH(n) == 1) {
1833 n = CHILD(n, 0);
1834 goto loop;
1835 }
1836 else {
1837 expr_ty expression;
1838 asdl_int_seq *ops;
1839 asdl_seq *cmps;
1840 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1841 if (!ops)
1842 return NULL;
1843 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1844 if (!cmps) {
1845 return NULL;
1846 }
1847 for (i = 1; i < NCH(n); i += 2) {
1848 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001850 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001851 if (!newoperator) {
1852 return NULL;
1853 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001855 expression = ast_for_expr(c, CHILD(n, i + 1));
1856 if (!expression) {
1857 return NULL;
1858 }
1859
1860 asdl_seq_SET(ops, i / 2, newoperator);
1861 asdl_seq_SET(cmps, i / 2, expression);
1862 }
1863 expression = ast_for_expr(c, CHILD(n, 0));
1864 if (!expression) {
1865 return NULL;
1866 }
1867
1868 return Compare(expression, ops, cmps, LINENO(n),
1869 n->n_col_offset, c->c_arena);
1870 }
1871 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001873 /* The next five cases all handle BinOps. The main body of code
1874 is the same in each case, but the switch turned inside out to
1875 reuse the code for each type of operator.
1876 */
1877 case expr:
1878 case xor_expr:
1879 case and_expr:
1880 case shift_expr:
1881 case arith_expr:
1882 case term:
1883 if (NCH(n) == 1) {
1884 n = CHILD(n, 0);
1885 goto loop;
1886 }
1887 return ast_for_binop(c, n);
1888 case yield_expr: {
1889 expr_ty exp = NULL;
1890 if (NCH(n) == 2) {
1891 exp = ast_for_testlist(c, CHILD(n, 1));
1892 if (!exp)
1893 return NULL;
1894 }
1895 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1896 }
1897 case factor:
1898 if (NCH(n) == 1) {
1899 n = CHILD(n, 0);
1900 goto loop;
1901 }
1902 return ast_for_factor(c, n);
1903 case power:
1904 return ast_for_power(c, n);
1905 default:
1906 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1907 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001909 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 return NULL;
1911}
1912
1913static expr_ty
1914ast_for_call(struct compiling *c, const node *n, expr_ty func)
1915{
1916 /*
1917 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001918 | '**' test)
1919 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 */
1921
1922 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001923 asdl_seq *args;
1924 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 expr_ty vararg = NULL, kwarg = NULL;
1926
1927 REQ(n, arglist);
1928
1929 nargs = 0;
1930 nkeywords = 0;
1931 ngens = 0;
1932 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001933 node *ch = CHILD(n, i);
1934 if (TYPE(ch) == argument) {
1935 if (NCH(ch) == 1)
1936 nargs++;
1937 else if (TYPE(CHILD(ch, 1)) == gen_for)
1938 ngens++;
1939 else
1940 nkeywords++;
1941 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 }
1943 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001944 ast_error(n, "Generator expression must be parenthesized "
1945 "if not sole argument");
1946 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947 }
1948
1949 if (nargs + nkeywords + ngens > 255) {
1950 ast_error(n, "more than 255 arguments");
1951 return NULL;
1952 }
1953
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001954 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001956 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001957 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001959 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 nargs = 0;
1961 nkeywords = 0;
1962 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001963 node *ch = CHILD(n, i);
1964 if (TYPE(ch) == argument) {
1965 expr_ty e;
1966 if (NCH(ch) == 1) {
1967 if (nkeywords) {
1968 ast_error(CHILD(ch, 0),
1969 "non-keyword arg after keyword arg");
1970 return NULL;
1971 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001972 if (vararg) {
1973 ast_error(CHILD(ch, 0),
1974 "only named arguments may follow *expression");
1975 return NULL;
1976 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001977 e = ast_for_expr(c, CHILD(ch, 0));
1978 if (!e)
1979 return NULL;
1980 asdl_seq_SET(args, nargs++, e);
1981 }
1982 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1983 e = ast_for_genexp(c, ch);
1984 if (!e)
1985 return NULL;
1986 asdl_seq_SET(args, nargs++, e);
1987 }
1988 else {
1989 keyword_ty kw;
1990 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001991 int k;
1992 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001994 /* CHILD(ch, 0) is test, but must be an identifier? */
1995 e = ast_for_expr(c, CHILD(ch, 0));
1996 if (!e)
1997 return NULL;
1998 /* f(lambda x: x[0] = 3) ends up getting parsed with
1999 * LHS test = lambda x: x[0], and RHS test = 3.
2000 * SF bug 132313 points out that complaining about a keyword
2001 * then is very confusing.
2002 */
2003 if (e->kind == Lambda_kind) {
2004 ast_error(CHILD(ch, 0),
2005 "lambda cannot contain assignment");
2006 return NULL;
2007 } else if (e->kind != Name_kind) {
2008 ast_error(CHILD(ch, 0), "keyword can't be an expression");
2009 return NULL;
2010 }
2011 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00002012 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00002013 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00002014 for (k = 0; k < nkeywords; k++) {
2015 tmp = PyString_AS_STRING(
2016 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
2017 if (!strcmp(tmp, PyString_AS_STRING(key))) {
2018 ast_error(CHILD(ch, 0), "keyword argument repeated");
2019 return NULL;
2020 }
2021 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002022 e = ast_for_expr(c, CHILD(ch, 2));
2023 if (!e)
2024 return NULL;
2025 kw = keyword(key, e, c->c_arena);
2026 if (!kw)
2027 return NULL;
2028 asdl_seq_SET(keywords, nkeywords++, kw);
2029 }
2030 }
2031 else if (TYPE(ch) == STAR) {
2032 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002033 if (!vararg)
2034 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002035 i++;
2036 }
2037 else if (TYPE(ch) == DOUBLESTAR) {
2038 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002039 if (!kwarg)
2040 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002041 i++;
2042 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 }
2044
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002045 return Call(func, args, keywords, vararg, kwarg, func->lineno,
2046 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047}
2048
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002050ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002052 /* testlist_gexp: test (',' test)* [','] */
2053 /* testlist: test (',' test)* [','] */
2054 /* testlist_safe: test (',' test)+ [','] */
2055 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002057 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002058 if (NCH(n) > 1)
2059 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002060 }
2061 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002062 assert(TYPE(n) == testlist ||
2063 TYPE(n) == testlist_safe ||
2064 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002065 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002067 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002069 asdl_seq *tmp = seq_for_testlist(c, n);
2070 if (!tmp)
2071 return NULL;
2072 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002074}
2075
2076static expr_ty
2077ast_for_testlist_gexp(struct compiling *c, const node* n)
2078{
2079 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2080 /* argument: test [ gen_for ] */
2081 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002082 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002083 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002084 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002085}
2086
2087/* like ast_for_testlist() but returns a sequence */
2088static asdl_seq*
2089ast_for_class_bases(struct compiling *c, const node* n)
2090{
2091 /* testlist: test (',' test)* [','] */
2092 assert(NCH(n) > 0);
2093 REQ(n, testlist);
2094 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002095 expr_ty base;
2096 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2097 if (!bases)
2098 return NULL;
2099 base = ast_for_expr(c, CHILD(n, 0));
2100 if (!base)
2101 return NULL;
2102 asdl_seq_SET(bases, 0, base);
2103 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002104 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002105
2106 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107}
2108
2109static stmt_ty
2110ast_for_expr_stmt(struct compiling *c, const node *n)
2111{
2112 REQ(n, expr_stmt);
2113 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002114 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 testlist: test (',' test)* [',']
2116 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002117 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 test: ... here starts the operator precendence dance
2119 */
2120
2121 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002122 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2123 if (!e)
2124 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002126 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 }
2128 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002129 expr_ty expr1, expr2;
2130 operator_ty newoperator;
2131 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002133 expr1 = ast_for_testlist(c, ch);
2134 if (!expr1)
2135 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002136 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002137 return NULL;
Benjamin Peterson7adbb5a2009-10-03 20:23:24 +00002138 /* set_context checks that most expressions are not the left side.
2139 Augmented assignments can only have a name, a subscript, or an
2140 attribute on the left, though, so we have to explicitly check for
2141 those. */
2142 switch (expr1->kind) {
2143 case Name_kind:
2144 case Attribute_kind:
2145 case Subscript_kind:
2146 break;
2147 default:
2148 ast_error(ch, "illegal expression for augmented assignment");
2149 return NULL;
2150 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002152 ch = CHILD(n, 2);
2153 if (TYPE(ch) == testlist)
2154 expr2 = ast_for_testlist(c, ch);
2155 else
2156 expr2 = ast_for_expr(c, ch);
2157 if (!expr2)
2158 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002160 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002161 if (!newoperator)
2162 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002164 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2165 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 }
2167 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002168 int i;
2169 asdl_seq *targets;
2170 node *value;
2171 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002173 /* a normal assignment */
2174 REQ(CHILD(n, 1), EQUAL);
2175 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2176 if (!targets)
2177 return NULL;
2178 for (i = 0; i < NCH(n) - 2; i += 2) {
2179 expr_ty e;
2180 node *ch = CHILD(n, i);
Benjamin Petersonb2664812009-06-11 17:49:38 +00002181 if (TYPE(ch) == yield_expr) {
2182 ast_error(ch, "assignment to yield expression not possible");
2183 return NULL;
2184 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002185 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002187 /* set context to assign */
2188 if (!e)
2189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002191 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002192 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002194 asdl_seq_SET(targets, i / 2, e);
2195 }
2196 value = CHILD(n, NCH(n) - 1);
2197 if (TYPE(value) == testlist)
2198 expression = ast_for_testlist(c, value);
2199 else
2200 expression = ast_for_expr(c, value);
2201 if (!expression)
2202 return NULL;
2203 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2204 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206}
2207
2208static stmt_ty
2209ast_for_print_stmt(struct compiling *c, const node *n)
2210{
2211 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002212 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 */
2214 expr_ty dest = NULL, expression;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002215 asdl_seq *seq = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 bool nl;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002217 int i, j, values_count, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218
2219 REQ(n, print_stmt);
2220 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002221 dest = ast_for_expr(c, CHILD(n, 2));
2222 if (!dest)
2223 return NULL;
2224 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 }
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002226 values_count = (NCH(n) + 1 - start) / 2;
2227 if (values_count) {
2228 seq = asdl_seq_new(values_count, c->c_arena);
2229 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002230 return NULL;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002231 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
2232 expression = ast_for_expr(c, CHILD(n, i));
2233 if (!expression)
2234 return NULL;
2235 asdl_seq_SET(seq, j, expression);
2236 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 }
2238 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002239 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240}
2241
2242static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002243ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244{
2245 asdl_seq *seq;
2246 int i;
2247 expr_ty e;
2248
2249 REQ(n, exprlist);
2250
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002251 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002253 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002255 e = ast_for_expr(c, CHILD(n, i));
2256 if (!e)
2257 return NULL;
2258 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002259 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002260 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 }
2262 return seq;
2263}
2264
2265static stmt_ty
2266ast_for_del_stmt(struct compiling *c, const node *n)
2267{
2268 asdl_seq *expr_list;
2269
2270 /* del_stmt: 'del' exprlist */
2271 REQ(n, del_stmt);
2272
2273 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2274 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002275 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002276 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277}
2278
2279static stmt_ty
2280ast_for_flow_stmt(struct compiling *c, const node *n)
2281{
2282 /*
2283 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002284 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 break_stmt: 'break'
2286 continue_stmt: 'continue'
2287 return_stmt: 'return' [testlist]
2288 yield_stmt: yield_expr
2289 yield_expr: 'yield' testlist
2290 raise_stmt: 'raise' [test [',' test [',' test]]]
2291 */
2292 node *ch;
2293
2294 REQ(n, flow_stmt);
2295 ch = CHILD(n, 0);
2296 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002297 case break_stmt:
2298 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2299 case continue_stmt:
2300 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2301 case yield_stmt: { /* will reduce to yield_expr */
2302 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2303 if (!exp)
2304 return NULL;
2305 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2306 }
2307 case return_stmt:
2308 if (NCH(ch) == 1)
2309 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2310 else {
2311 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2312 if (!expression)
2313 return NULL;
2314 return Return(expression, LINENO(n), n->n_col_offset,
2315 c->c_arena);
2316 }
2317 case raise_stmt:
2318 if (NCH(ch) == 1)
2319 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2320 c->c_arena);
2321 else if (NCH(ch) == 2) {
2322 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2323 if (!expression)
2324 return NULL;
2325 return Raise(expression, NULL, NULL, LINENO(n),
2326 n->n_col_offset, c->c_arena);
2327 }
2328 else if (NCH(ch) == 4) {
2329 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002331 expr1 = ast_for_expr(c, CHILD(ch, 1));
2332 if (!expr1)
2333 return NULL;
2334 expr2 = ast_for_expr(c, CHILD(ch, 3));
2335 if (!expr2)
2336 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002338 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2339 c->c_arena);
2340 }
2341 else if (NCH(ch) == 6) {
2342 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002344 expr1 = ast_for_expr(c, CHILD(ch, 1));
2345 if (!expr1)
2346 return NULL;
2347 expr2 = ast_for_expr(c, CHILD(ch, 3));
2348 if (!expr2)
2349 return NULL;
2350 expr3 = ast_for_expr(c, CHILD(ch, 5));
2351 if (!expr3)
2352 return NULL;
2353
2354 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2355 c->c_arena);
2356 }
2357 default:
2358 PyErr_Format(PyExc_SystemError,
2359 "unexpected flow_stmt: %d", TYPE(ch));
2360 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002362
2363 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2364 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365}
2366
2367static alias_ty
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002368alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002369{
2370 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002371 import_as_name: NAME ['as' NAME]
2372 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 dotted_name: NAME ('.' NAME)*
2374 */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002375 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002376
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 loop:
2378 switch (TYPE(n)) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002379 case import_as_name: {
2380 node *name_node = CHILD(n, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002381 str = NULL;
2382 if (NCH(n) == 3) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002383 node *str_node = CHILD(n, 2);
2384 if (store && !forbidden_check(c, str_node, STR(str_node)))
2385 return NULL;
2386 str = NEW_IDENTIFIER(str_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002387 if (!str)
2388 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002389 }
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002390 else {
2391 if (!forbidden_check(c, name_node, STR(name_node)))
2392 return NULL;
2393 }
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002394 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002395 if (!name)
2396 return NULL;
2397 return alias(name, str, c->c_arena);
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002398 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002399 case dotted_as_name:
2400 if (NCH(n) == 1) {
2401 n = CHILD(n, 0);
2402 goto loop;
2403 }
2404 else {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002405 node *asname_node = CHILD(n, 2);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002406 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002407 if (!a)
2408 return NULL;
2409 assert(!a->asname);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002410 if (!forbidden_check(c, asname_node, STR(asname_node)))
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002411 return NULL;
2412 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002413 if (!a->asname)
2414 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002415 return a;
2416 }
2417 break;
2418 case dotted_name:
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002419 if (NCH(n) == 1) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002420 node *name_node = CHILD(n, 0);
2421 if (store && !forbidden_check(c, name_node, STR(name_node)))
2422 return NULL;
2423 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002424 if (!name)
2425 return NULL;
2426 return alias(name, NULL, c->c_arena);
2427 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002428 else {
2429 /* Create a string of the form "a.b.c" */
2430 int i;
2431 size_t len;
2432 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002434 len = 0;
2435 for (i = 0; i < NCH(n); i += 2)
2436 /* length of string plus one for the dot */
2437 len += strlen(STR(CHILD(n, i))) + 1;
2438 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002439 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002440 if (!str)
2441 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002442 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002443 if (!s)
2444 return NULL;
2445 for (i = 0; i < NCH(n); i += 2) {
2446 char *sch = STR(CHILD(n, i));
2447 strcpy(s, STR(CHILD(n, i)));
2448 s += strlen(sch);
2449 *s++ = '.';
2450 }
2451 --s;
2452 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002453 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002454 PyArena_AddPyObject(c->c_arena, str);
2455 return alias(str, NULL, c->c_arena);
2456 }
2457 break;
2458 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002459 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002460 PyArena_AddPyObject(c->c_arena, str);
2461 return alias(str, NULL, c->c_arena);
2462 default:
2463 PyErr_Format(PyExc_SystemError,
2464 "unexpected import name: %d", TYPE(n));
2465 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002467
2468 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 return NULL;
2470}
2471
2472static stmt_ty
2473ast_for_import_stmt(struct compiling *c, const node *n)
2474{
2475 /*
2476 import_stmt: import_name | import_from
2477 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002478 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002479 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002481 int lineno;
2482 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 int i;
2484 asdl_seq *aliases;
2485
2486 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002487 lineno = LINENO(n);
2488 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002490 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002491 n = CHILD(n, 1);
2492 REQ(n, dotted_as_names);
2493 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2494 if (!aliases)
2495 return NULL;
2496 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002497 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002498 if (!import_alias)
2499 return NULL;
2500 asdl_seq_SET(aliases, i / 2, import_alias);
2501 }
2502 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002504 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002505 int n_children;
2506 int idx, ndots = 0;
2507 alias_ty mod = NULL;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002508 identifier modname = NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002509
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002510 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002511 optional module name */
2512 for (idx = 1; idx < NCH(n); idx++) {
2513 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002514 mod = alias_for_import_name(c, CHILD(n, idx), 0);
2515 if (!mod)
2516 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002517 idx++;
2518 break;
2519 } else if (TYPE(CHILD(n, idx)) != DOT) {
2520 break;
2521 }
2522 ndots++;
2523 }
2524 idx++; /* skip over the 'import' keyword */
2525 switch (TYPE(CHILD(n, idx))) {
2526 case STAR:
2527 /* from ... import * */
2528 n = CHILD(n, idx);
2529 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002530 break;
2531 case LPAR:
2532 /* from ... import (x, y, z) */
2533 n = CHILD(n, idx + 1);
2534 n_children = NCH(n);
2535 break;
2536 case import_as_names:
2537 /* from ... import x, y, z */
2538 n = CHILD(n, idx);
2539 n_children = NCH(n);
2540 if (n_children % 2 == 0) {
2541 ast_error(n, "trailing comma not allowed without"
2542 " surrounding parentheses");
2543 return NULL;
2544 }
2545 break;
2546 default:
2547 ast_error(n, "Unexpected node-type in from-import");
2548 return NULL;
2549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002551 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2552 if (!aliases)
2553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002555 /* handle "from ... import *" special b/c there's no children */
2556 if (TYPE(n) == STAR) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002557 alias_ty import_alias = alias_for_import_name(c, n, 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002558 if (!import_alias)
2559 return NULL;
2560 asdl_seq_SET(aliases, 0, import_alias);
2561 }
2562 else {
2563 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002564 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002565 if (!import_alias)
2566 return NULL;
2567 asdl_seq_SET(aliases, i / 2, import_alias);
2568 }
2569 }
2570 if (mod != NULL)
2571 modname = mod->name;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002572 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2573 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 }
Neal Norwitz79792652005-11-14 04:25:03 +00002575 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002576 "unknown import statement: starts with command '%s'",
2577 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 return NULL;
2579}
2580
2581static stmt_ty
2582ast_for_global_stmt(struct compiling *c, const node *n)
2583{
2584 /* global_stmt: 'global' NAME (',' NAME)* */
2585 identifier name;
2586 asdl_seq *s;
2587 int i;
2588
2589 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002590 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002592 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002594 name = NEW_IDENTIFIER(CHILD(n, i));
2595 if (!name)
2596 return NULL;
2597 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002599 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600}
2601
2602static stmt_ty
2603ast_for_exec_stmt(struct compiling *c, const node *n)
2604{
2605 expr_ty expr1, globals = NULL, locals = NULL;
2606 int n_children = NCH(n);
2607 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002608 PyErr_Format(PyExc_SystemError,
2609 "poorly formed 'exec' statement: %d parts to statement",
2610 n_children);
2611 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 }
2613
2614 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2615 REQ(n, exec_stmt);
2616 expr1 = ast_for_expr(c, CHILD(n, 1));
2617 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002618 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002620 globals = ast_for_expr(c, CHILD(n, 3));
2621 if (!globals)
2622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 }
2624 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002625 locals = ast_for_expr(c, CHILD(n, 5));
2626 if (!locals)
2627 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 }
2629
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002630 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2631 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632}
2633
2634static stmt_ty
2635ast_for_assert_stmt(struct compiling *c, const node *n)
2636{
2637 /* assert_stmt: 'assert' test [',' test] */
2638 REQ(n, assert_stmt);
2639 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002640 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2641 if (!expression)
2642 return NULL;
2643 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2644 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 }
2646 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002647 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002649 expr1 = ast_for_expr(c, CHILD(n, 1));
2650 if (!expr1)
2651 return NULL;
2652 expr2 = ast_for_expr(c, CHILD(n, 3));
2653 if (!expr2)
2654 return NULL;
2655
2656 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 }
Neal Norwitz79792652005-11-14 04:25:03 +00002658 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002659 "improper number of parts to 'assert' statement: %d",
2660 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 return NULL;
2662}
2663
2664static asdl_seq *
2665ast_for_suite(struct compiling *c, const node *n)
2666{
2667 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002668 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 stmt_ty s;
2670 int i, total, num, end, pos = 0;
2671 node *ch;
2672
2673 REQ(n, suite);
2674
2675 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002676 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002678 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002680 n = CHILD(n, 0);
2681 /* simple_stmt always ends with a NEWLINE,
2682 and may have a trailing SEMI
2683 */
2684 end = NCH(n) - 1;
2685 if (TYPE(CHILD(n, end - 1)) == SEMI)
2686 end--;
2687 /* loop by 2 to skip semi-colons */
2688 for (i = 0; i < end; i += 2) {
2689 ch = CHILD(n, i);
2690 s = ast_for_stmt(c, ch);
2691 if (!s)
2692 return NULL;
2693 asdl_seq_SET(seq, pos++, s);
2694 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 }
2696 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002697 for (i = 2; i < (NCH(n) - 1); i++) {
2698 ch = CHILD(n, i);
2699 REQ(ch, stmt);
2700 num = num_stmts(ch);
2701 if (num == 1) {
2702 /* small_stmt or compound_stmt with only one child */
2703 s = ast_for_stmt(c, ch);
2704 if (!s)
2705 return NULL;
2706 asdl_seq_SET(seq, pos++, s);
2707 }
2708 else {
2709 int j;
2710 ch = CHILD(ch, 0);
2711 REQ(ch, simple_stmt);
2712 for (j = 0; j < NCH(ch); j += 2) {
2713 /* statement terminates with a semi-colon ';' */
2714 if (NCH(CHILD(ch, j)) == 0) {
2715 assert((j + 1) == NCH(ch));
2716 break;
2717 }
2718 s = ast_for_stmt(c, CHILD(ch, j));
2719 if (!s)
2720 return NULL;
2721 asdl_seq_SET(seq, pos++, s);
2722 }
2723 }
2724 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 }
2726 assert(pos == seq->size);
2727 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728}
2729
2730static stmt_ty
2731ast_for_if_stmt(struct compiling *c, const node *n)
2732{
2733 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2734 ['else' ':' suite]
2735 */
2736 char *s;
2737
2738 REQ(n, if_stmt);
2739
2740 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002741 expr_ty expression;
2742 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002744 expression = ast_for_expr(c, CHILD(n, 1));
2745 if (!expression)
2746 return NULL;
2747 suite_seq = ast_for_suite(c, CHILD(n, 3));
2748 if (!suite_seq)
2749 return NULL;
2750
2751 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2752 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002754
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 s = STR(CHILD(n, 4));
2756 /* s[2], the third character in the string, will be
2757 's' for el_s_e, or
2758 'i' for el_i_f
2759 */
2760 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002761 expr_ty expression;
2762 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002764 expression = ast_for_expr(c, CHILD(n, 1));
2765 if (!expression)
2766 return NULL;
2767 seq1 = ast_for_suite(c, CHILD(n, 3));
2768 if (!seq1)
2769 return NULL;
2770 seq2 = ast_for_suite(c, CHILD(n, 6));
2771 if (!seq2)
2772 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002774 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2775 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 }
2777 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002778 int i, n_elif, has_else = 0;
2779 expr_ty expression;
2780 asdl_seq *suite_seq;
2781 asdl_seq *orelse = NULL;
2782 n_elif = NCH(n) - 4;
2783 /* must reference the child n_elif+1 since 'else' token is third,
2784 not fourth, child from the end. */
2785 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2786 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2787 has_else = 1;
2788 n_elif -= 3;
2789 }
2790 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002792 if (has_else) {
2793 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002795 orelse = asdl_seq_new(1, c->c_arena);
2796 if (!orelse)
2797 return NULL;
2798 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2799 if (!expression)
2800 return NULL;
2801 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2802 if (!suite_seq)
2803 return NULL;
2804 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2805 if (!suite_seq2)
2806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002808 asdl_seq_SET(orelse, 0,
2809 If(expression, suite_seq, suite_seq2,
2810 LINENO(CHILD(n, NCH(n) - 6)),
2811 CHILD(n, NCH(n) - 6)->n_col_offset,
2812 c->c_arena));
2813 /* the just-created orelse handled the last elif */
2814 n_elif--;
2815 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002817 for (i = 0; i < n_elif; i++) {
2818 int off = 5 + (n_elif - i - 1) * 4;
2819 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2820 if (!newobj)
2821 return NULL;
2822 expression = ast_for_expr(c, CHILD(n, off));
2823 if (!expression)
2824 return NULL;
2825 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2826 if (!suite_seq)
2827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002829 asdl_seq_SET(newobj, 0,
2830 If(expression, suite_seq, orelse,
2831 LINENO(CHILD(n, off)),
2832 CHILD(n, off)->n_col_offset, c->c_arena));
2833 orelse = newobj;
2834 }
2835 expression = ast_for_expr(c, CHILD(n, 1));
2836 if (!expression)
2837 return NULL;
2838 suite_seq = ast_for_suite(c, CHILD(n, 3));
2839 if (!suite_seq)
2840 return NULL;
2841 return If(expression, suite_seq, orelse,
2842 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002844
2845 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002846 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002847 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848}
2849
2850static stmt_ty
2851ast_for_while_stmt(struct compiling *c, const node *n)
2852{
2853 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2854 REQ(n, while_stmt);
2855
2856 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002857 expr_ty expression;
2858 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002860 expression = ast_for_expr(c, CHILD(n, 1));
2861 if (!expression)
2862 return NULL;
2863 suite_seq = ast_for_suite(c, CHILD(n, 3));
2864 if (!suite_seq)
2865 return NULL;
2866 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2867 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 }
2869 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002870 expr_ty expression;
2871 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002873 expression = ast_for_expr(c, CHILD(n, 1));
2874 if (!expression)
2875 return NULL;
2876 seq1 = ast_for_suite(c, CHILD(n, 3));
2877 if (!seq1)
2878 return NULL;
2879 seq2 = ast_for_suite(c, CHILD(n, 6));
2880 if (!seq2)
2881 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002883 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2884 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002886
2887 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002888 "wrong number of tokens for 'while' statement: %d",
2889 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891}
2892
2893static stmt_ty
2894ast_for_for_stmt(struct compiling *c, const node *n)
2895{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002896 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 expr_ty expression;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002898 expr_ty target, first;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002899 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2901 REQ(n, for_stmt);
2902
2903 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002904 seq = ast_for_suite(c, CHILD(n, 8));
2905 if (!seq)
2906 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 }
2908
Neal Norwitzedef2be2006-07-12 05:26:17 +00002909 node_target = CHILD(n, 1);
2910 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002911 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002912 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002913 /* Check the # of children rather than the length of _target, since
2914 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002915 first = (expr_ty)asdl_seq_GET(_target, 0);
Neal Norwitzedef2be2006-07-12 05:26:17 +00002916 if (NCH(node_target) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002917 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002919 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002921 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002922 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002923 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002925 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002926 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002928 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002929 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930}
2931
2932static excepthandler_ty
2933ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2934{
Collin Winter62903052007-05-18 23:11:24 +00002935 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 REQ(exc, except_clause);
2937 REQ(body, suite);
2938
2939 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002940 asdl_seq *suite_seq = ast_for_suite(c, body);
2941 if (!suite_seq)
2942 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943
Georg Brandla48f3ab2008-03-30 06:40:17 +00002944 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002945 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 }
2947 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002948 expr_ty expression;
2949 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002951 expression = ast_for_expr(c, CHILD(exc, 1));
2952 if (!expression)
2953 return NULL;
2954 suite_seq = ast_for_suite(c, body);
2955 if (!suite_seq)
2956 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957
Georg Brandla48f3ab2008-03-30 06:40:17 +00002958 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002959 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 }
2961 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002962 asdl_seq *suite_seq;
2963 expr_ty expression;
2964 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2965 if (!e)
2966 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002967 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002968 return NULL;
2969 expression = ast_for_expr(c, CHILD(exc, 1));
2970 if (!expression)
2971 return NULL;
2972 suite_seq = ast_for_suite(c, body);
2973 if (!suite_seq)
2974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975
Georg Brandla48f3ab2008-03-30 06:40:17 +00002976 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002977 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002979
2980 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002981 "wrong number of children for 'except' clause: %d",
2982 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984}
2985
2986static stmt_ty
2987ast_for_try_stmt(struct compiling *c, const node *n)
2988{
Neal Norwitzf599f422005-12-17 21:33:47 +00002989 const int nch = NCH(n);
2990 int n_except = (nch - 3)/3;
2991 asdl_seq *body, *orelse = NULL, *finally = NULL;
2992
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 REQ(n, try_stmt);
2994
Neal Norwitzf599f422005-12-17 21:33:47 +00002995 body = ast_for_suite(c, CHILD(n, 2));
2996 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002997 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998
Neal Norwitzf599f422005-12-17 21:33:47 +00002999 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003000 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3001 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3002 /* we can assume it's an "else",
3003 because nch >= 9 for try-else-finally and
3004 it would otherwise have a type of except_clause */
3005 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3006 if (orelse == NULL)
3007 return NULL;
3008 n_except--;
3009 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003011 finally = ast_for_suite(c, CHILD(n, nch - 1));
3012 if (finally == NULL)
3013 return NULL;
3014 n_except--;
3015 }
3016 else {
3017 /* we can assume it's an "else",
3018 otherwise it would have a type of except_clause */
3019 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3020 if (orelse == NULL)
3021 return NULL;
3022 n_except--;
3023 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003025 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003026 ast_error(n, "malformed 'try' statement");
3027 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003029
3030 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003031 int i;
3032 stmt_ty except_st;
3033 /* process except statements to create a try ... except */
3034 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
3035 if (handlers == NULL)
3036 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003037
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003038 for (i = 0; i < n_except; i++) {
3039 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3040 CHILD(n, 5 + i * 3));
3041 if (!e)
3042 return NULL;
3043 asdl_seq_SET(handlers, i, e);
3044 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003045
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003046 except_st = TryExcept(body, handlers, orelse, LINENO(n),
3047 n->n_col_offset, c->c_arena);
3048 if (!finally)
3049 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00003050
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003051 /* if a 'finally' is present too, we nest the TryExcept within a
3052 TryFinally to emulate try ... except ... finally */
3053 body = asdl_seq_new(1, c->c_arena);
3054 if (body == NULL)
3055 return NULL;
3056 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00003057 }
3058
3059 /* must be a try ... finally (except clauses are in body, if any exist) */
3060 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003061 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062}
3063
Georg Brandl944f6842009-05-25 21:02:56 +00003064/* with_item: test ['as' expr] */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003065static stmt_ty
Georg Brandl944f6842009-05-25 21:02:56 +00003066ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003067{
3068 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003069
Georg Brandl944f6842009-05-25 21:02:56 +00003070 REQ(n, with_item);
3071 context_expr = ast_for_expr(c, CHILD(n, 0));
Collin Winter77c67bd2007-03-16 04:11:30 +00003072 if (!context_expr)
3073 return NULL;
Georg Brandl944f6842009-05-25 21:02:56 +00003074 if (NCH(n) == 3) {
3075 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003076
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003077 if (!optional_vars) {
3078 return NULL;
3079 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003080 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003081 return NULL;
3082 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003083 }
3084
Georg Brandl944f6842009-05-25 21:02:56 +00003085 return With(context_expr, optional_vars, content, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003086 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003087}
3088
Georg Brandl944f6842009-05-25 21:02:56 +00003089/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3090static stmt_ty
3091ast_for_with_stmt(struct compiling *c, const node *n)
3092{
3093 int i;
3094 stmt_ty ret;
3095 asdl_seq *inner;
3096
3097 REQ(n, with_stmt);
3098
3099 /* process the with items inside-out */
3100 i = NCH(n) - 1;
3101 /* the suite of the innermost with item is the suite of the with stmt */
3102 inner = ast_for_suite(c, CHILD(n, i));
3103 if (!inner)
3104 return NULL;
3105
3106 for (;;) {
3107 i -= 2;
3108 ret = ast_for_with_item(c, CHILD(n, i), inner);
3109 if (!ret)
3110 return NULL;
3111 /* was this the last item? */
3112 if (i == 1)
3113 break;
3114 /* if not, wrap the result so far in a new sequence */
3115 inner = asdl_seq_new(1, c->c_arena);
3116 if (!inner)
3117 return NULL;
3118 asdl_seq_SET(inner, 0, ret);
3119 }
3120
3121 return ret;
3122}
3123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003125ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126{
3127 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003128 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 asdl_seq *bases, *s;
3130
3131 REQ(n, classdef);
3132
Benjamin Petersond5efd202008-06-08 22:52:37 +00003133 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003134 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135
3136 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003137 s = ast_for_suite(c, CHILD(n, 3));
3138 if (!s)
3139 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003140 classname = NEW_IDENTIFIER(CHILD(n, 1));
3141 if (!classname)
3142 return NULL;
3143 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3144 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 }
3146 /* check for empty base list */
3147 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003148 s = ast_for_suite(c, CHILD(n,5));
3149 if (!s)
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003150 return NULL;
3151 classname = NEW_IDENTIFIER(CHILD(n, 1));
3152 if (!classname)
3153 return NULL;
3154 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3155 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 }
3157
3158 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003159 bases = ast_for_class_bases(c, CHILD(n, 3));
3160 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003161 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162
3163 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003164 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003165 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003166 classname = NEW_IDENTIFIER(CHILD(n, 1));
3167 if (!classname)
3168 return NULL;
3169 return ClassDef(classname, bases, s, decorator_seq,
Christian Heimes5224d282008-02-23 15:01:05 +00003170 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171}
3172
3173static stmt_ty
3174ast_for_stmt(struct compiling *c, const node *n)
3175{
3176 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003177 assert(NCH(n) == 1);
3178 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179 }
3180 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003181 assert(num_stmts(n) == 1);
3182 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 }
3184 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003185 n = CHILD(n, 0);
3186 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3187 | flow_stmt | import_stmt | global_stmt | exec_stmt
3188 | assert_stmt
3189 */
3190 switch (TYPE(n)) {
3191 case expr_stmt:
3192 return ast_for_expr_stmt(c, n);
3193 case print_stmt:
3194 return ast_for_print_stmt(c, n);
3195 case del_stmt:
3196 return ast_for_del_stmt(c, n);
3197 case pass_stmt:
3198 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3199 case flow_stmt:
3200 return ast_for_flow_stmt(c, n);
3201 case import_stmt:
3202 return ast_for_import_stmt(c, n);
3203 case global_stmt:
3204 return ast_for_global_stmt(c, n);
3205 case exec_stmt:
3206 return ast_for_exec_stmt(c, n);
3207 case assert_stmt:
3208 return ast_for_assert_stmt(c, n);
3209 default:
3210 PyErr_Format(PyExc_SystemError,
3211 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3212 TYPE(n), NCH(n));
3213 return NULL;
3214 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 }
3216 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003217 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003218 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003219 */
3220 node *ch = CHILD(n, 0);
3221 REQ(n, compound_stmt);
3222 switch (TYPE(ch)) {
3223 case if_stmt:
3224 return ast_for_if_stmt(c, ch);
3225 case while_stmt:
3226 return ast_for_while_stmt(c, ch);
3227 case for_stmt:
3228 return ast_for_for_stmt(c, ch);
3229 case try_stmt:
3230 return ast_for_try_stmt(c, ch);
3231 case with_stmt:
3232 return ast_for_with_stmt(c, ch);
3233 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003234 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003235 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003236 return ast_for_classdef(c, ch, NULL);
3237 case decorated:
3238 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003239 default:
3240 PyErr_Format(PyExc_SystemError,
3241 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3242 TYPE(n), NCH(n));
3243 return NULL;
3244 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245 }
3246}
3247
3248static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003249parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003250{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003251 const char *end;
3252 long x;
3253 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003255 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003256 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257#endif
3258
Mark Dickinson422ce062008-12-05 17:59:46 +00003259 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003260 errno = 0;
3261 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003263 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003265 if (*end == 'l' || *end == 'L')
3266 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003267 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003268 if (*end == '\0') {
3269 if (errno != 0)
3270 return PyLong_FromString((char *)s, (char **)0, 0);
3271 return PyInt_FromLong(x);
3272 }
3273 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003275 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003276 complex.real = 0.;
Eric Smithabc9f702009-10-27 18:33:14 +00003277 complex.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3278 if (complex.imag == -1.0 && PyErr_Occurred())
3279 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003280 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003281 }
3282 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003284 {
Eric Smithabc9f702009-10-27 18:33:14 +00003285 dx = PyOS_string_to_double(s, NULL, NULL);
3286 if (dx == -1.0 && PyErr_Occurred())
3287 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003288 return PyFloat_FromDouble(dx);
3289 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290}
3291
3292static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003293decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294{
3295#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003296 Py_FatalError("decode_utf8 should not be called in this build.");
3297 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003299 PyObject *u, *v;
3300 char *s, *t;
3301 t = s = (char *)*sPtr;
3302 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3303 while (s < end && (*s & 0x80)) s++;
3304 *sPtr = s;
3305 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3306 if (u == NULL)
3307 return NULL;
3308 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3309 Py_DECREF(u);
3310 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311#endif
3312}
3313
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003314#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003316decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003318 PyObject *v, *u;
3319 char *buf;
3320 char *p;
3321 const char *end;
3322 if (encoding == NULL) {
3323 buf = (char *)s;
3324 u = NULL;
3325 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3326 buf = (char *)s;
3327 u = NULL;
3328 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003329 /* check for integer overflow */
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003330 if (len > PY_SIZE_MAX / 6)
Gregory P. Smith9d534572008-06-11 07:41:16 +00003331 return NULL;
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003332 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3333 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3334 u = PyString_FromStringAndSize((char *)NULL, len * 6);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003335 if (u == NULL)
3336 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003337 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003338 end = s + len;
3339 while (s < end) {
3340 if (*s == '\\') {
3341 *p++ = *s++;
3342 if (*s & 0x80) {
3343 strcpy(p, "u005c");
3344 p += 5;
3345 }
3346 }
3347 if (*s & 0x80) { /* XXX inefficient */
3348 PyObject *w;
3349 char *r;
3350 Py_ssize_t rn, i;
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003351 w = decode_utf8(c, &s, end, "utf-32-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003352 if (w == NULL) {
3353 Py_DECREF(u);
3354 return NULL;
3355 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003356 r = PyString_AsString(w);
3357 rn = PyString_Size(w);
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003358 assert(rn % 4 == 0);
3359 for (i = 0; i < rn; i += 4) {
3360 sprintf(p, "\\U%02x%02x%02x%02x",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003361 r[i + 0] & 0xFF,
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003362 r[i + 1] & 0xFF,
3363 r[i + 2] & 0xFF,
3364 r[i + 3] & 0xFF);
3365 p += 10;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003366 }
3367 Py_DECREF(w);
3368 } else {
3369 *p++ = *s++;
3370 }
3371 }
3372 len = p - buf;
3373 s = buf;
3374 }
3375 if (rawmode)
3376 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3377 else
3378 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3379 Py_XDECREF(u);
3380 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003382#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383
3384/* s is a Python string literal, including the bracketing quote characters,
3385 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3386 * parsestr parses it, and returns the decoded Python string object.
3387 */
3388static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003389parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003391 size_t len;
3392 int quote = Py_CHARMASK(*s);
3393 int rawmode = 0;
3394 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003395 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003397 if (isalpha(quote) || quote == '_') {
3398 if (quote == 'u' || quote == 'U') {
3399 quote = *++s;
3400 unicode = 1;
3401 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003402 if (quote == 'b' || quote == 'B') {
3403 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003404 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003405 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003406 if (quote == 'r' || quote == 'R') {
3407 quote = *++s;
3408 rawmode = 1;
3409 }
3410 }
3411 if (quote != '\'' && quote != '\"') {
3412 PyErr_BadInternalCall();
3413 return NULL;
3414 }
3415 s++;
3416 len = strlen(s);
3417 if (len > INT_MAX) {
3418 PyErr_SetString(PyExc_OverflowError,
3419 "string to parse is too long");
3420 return NULL;
3421 }
3422 if (s[--len] != quote) {
3423 PyErr_BadInternalCall();
3424 return NULL;
3425 }
3426 if (len >= 4 && s[0] == quote && s[1] == quote) {
3427 s += 2;
3428 len -= 2;
3429 if (s[--len] != quote || s[--len] != quote) {
3430 PyErr_BadInternalCall();
3431 return NULL;
3432 }
3433 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003435 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003436 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003437 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003439 need_encoding = (c->c_encoding != NULL &&
3440 strcmp(c->c_encoding, "utf-8") != 0 &&
3441 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003442 if (rawmode || strchr(s, '\\') == NULL) {
3443 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003445 /* This should not happen - we never see any other
3446 encoding. */
3447 Py_FatalError(
3448 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003450 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3451 if (u == NULL)
3452 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003453 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003454 Py_DECREF(u);
3455 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003457 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003458 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003459 }
3460 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003462 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003463 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464}
3465
3466/* Build a Python string object out of a STRING atom. This takes care of
3467 * compile-time literal catenation, calling parsestr() on each piece, and
3468 * pasting the intermediate results together.
3469 */
3470static PyObject *
3471parsestrplus(struct compiling *c, const node *n)
3472{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003473 PyObject *v;
3474 int i;
3475 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003476 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003477 /* String literal concatenation */
3478 for (i = 1; i < NCH(n); i++) {
3479 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003480 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003481 if (s == NULL)
3482 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003483 if (PyString_Check(v) && PyString_Check(s)) {
3484 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003485 if (v == NULL)
3486 goto onError;
3487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003489 else {
3490 PyObject *temp = PyUnicode_Concat(v, s);
3491 Py_DECREF(s);
3492 Py_DECREF(v);
3493 v = temp;
3494 if (v == NULL)
3495 goto onError;
3496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003498 }
3499 }
3500 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501
3502 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003503 Py_XDECREF(v);
3504 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003505}