blob: 574a3d817cebedbc6285e476e28c2d7ad82ef8b0 [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 Peterson5cb67632008-11-25 04:00:37 +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"))
134 return ast_error(n, "assignment to None");
135 if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) &&
136 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
137 return 0;
138 return 1;
139}
140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141/* num_stmts() returns number of contained statements.
142
143 Use this routine to determine how big a sequence is needed for
144 the statements in a parse tree. Its raison d'etre is this bit of
145 grammar:
146
147 stmt: simple_stmt | compound_stmt
148 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
149
150 A simple_stmt can contain multiple small_stmt elements joined
151 by semicolons. If the arg is a simple_stmt, the number of
152 small_stmt elements is returned.
153*/
154
155static int
156num_stmts(const node *n)
157{
158 int i, l;
159 node *ch;
160
161 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000162 case single_input:
163 if (TYPE(CHILD(n, 0)) == NEWLINE)
164 return 0;
165 else
166 return num_stmts(CHILD(n, 0));
167 case file_input:
168 l = 0;
169 for (i = 0; i < NCH(n); i++) {
170 ch = CHILD(n, i);
171 if (TYPE(ch) == stmt)
172 l += num_stmts(ch);
173 }
174 return l;
175 case stmt:
176 return num_stmts(CHILD(n, 0));
177 case compound_stmt:
178 return 1;
179 case simple_stmt:
180 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
181 case suite:
182 if (NCH(n) == 1)
183 return num_stmts(CHILD(n, 0));
184 else {
185 l = 0;
186 for (i = 2; i < (NCH(n) - 1); i++)
187 l += num_stmts(CHILD(n, i));
188 return l;
189 }
190 default: {
191 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000193 sprintf(buf, "Non-statement found: %d %d\n",
194 TYPE(n), NCH(n));
195 Py_FatalError(buf);
196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 }
198 assert(0);
199 return 0;
200}
201
202/* Transform the CST rooted at node * to the appropriate AST
203*/
204
205mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000206PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000207 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000209 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 asdl_seq *stmts = NULL;
211 stmt_ty s;
212 node *ch;
213 struct compiling c;
214
215 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000216 c.c_encoding = "utf-8";
217 if (TYPE(n) == encoding_decl) {
218 ast_error(n, "encoding declaration in Unicode string");
219 goto error;
220 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000222 c.c_encoding = STR(n);
223 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000225 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 }
Christian Heimes3c608332008-03-26 22:01:37 +0000227 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000228 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000229 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230
Jeremy Hyltona8293132006-02-28 17:58:27 +0000231 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000233 case file_input:
234 stmts = asdl_seq_new(num_stmts(n), arena);
235 if (!stmts)
236 return NULL;
237 for (i = 0; i < NCH(n) - 1; i++) {
238 ch = CHILD(n, i);
239 if (TYPE(ch) == NEWLINE)
240 continue;
241 REQ(ch, stmt);
242 num = num_stmts(ch);
243 if (num == 1) {
244 s = ast_for_stmt(&c, ch);
245 if (!s)
246 goto error;
247 asdl_seq_SET(stmts, k++, s);
248 }
249 else {
250 ch = CHILD(ch, 0);
251 REQ(ch, simple_stmt);
252 for (j = 0; j < num; j++) {
253 s = ast_for_stmt(&c, CHILD(ch, j * 2));
254 if (!s)
255 goto error;
256 asdl_seq_SET(stmts, k++, s);
257 }
258 }
259 }
260 return Module(stmts, arena);
261 case eval_input: {
262 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000264 /* XXX Why not gen_for here? */
265 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
266 if (!testlist_ast)
267 goto error;
268 return Expression(testlist_ast, arena);
269 }
270 case single_input:
271 if (TYPE(CHILD(n, 0)) == NEWLINE) {
272 stmts = asdl_seq_new(1, arena);
273 if (!stmts)
274 goto error;
275 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
276 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000277 if (!asdl_seq_GET(stmts, 0))
278 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000279 return Interactive(stmts, arena);
280 }
281 else {
282 n = CHILD(n, 0);
283 num = num_stmts(n);
284 stmts = asdl_seq_new(num, arena);
285 if (!stmts)
286 goto error;
287 if (num == 1) {
288 s = ast_for_stmt(&c, n);
289 if (!s)
290 goto error;
291 asdl_seq_SET(stmts, 0, s);
292 }
293 else {
294 /* Only a simple_stmt can contain multiple statements. */
295 REQ(n, simple_stmt);
296 for (i = 0; i < NCH(n); i += 2) {
297 if (TYPE(CHILD(n, i)) == NEWLINE)
298 break;
299 s = ast_for_stmt(&c, CHILD(n, i));
300 if (!s)
301 goto error;
302 asdl_seq_SET(stmts, i / 2, s);
303 }
304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000306 return Interactive(stmts, arena);
307 }
308 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000309 PyErr_Format(PyExc_SystemError,
310 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000311 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312 }
313 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 ast_error_finish(filename);
315 return NULL;
316}
317
318/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
319*/
320
321static operator_ty
322get_operator(const node *n)
323{
324 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000325 case VBAR:
326 return BitOr;
327 case CIRCUMFLEX:
328 return BitXor;
329 case AMPER:
330 return BitAnd;
331 case LEFTSHIFT:
332 return LShift;
333 case RIGHTSHIFT:
334 return RShift;
335 case PLUS:
336 return Add;
337 case MINUS:
338 return Sub;
339 case STAR:
340 return Mult;
341 case SLASH:
342 return Div;
343 case DOUBLESLASH:
344 return FloorDiv;
345 case PERCENT:
346 return Mod;
347 default:
348 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349 }
350}
351
Jeremy Hyltona8293132006-02-28 17:58:27 +0000352/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353
354 Only sets context for expr kinds that "can appear in assignment context"
355 (according to ../Parser/Python.asdl). For other expr kinds, it sets
356 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357*/
358
359static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000360set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361{
362 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000363 /* If a particular expression type can't be used for assign / delete,
364 set expr_name to its name and an error message will be generated.
365 */
366 const char* expr_name = NULL;
367
368 /* The ast defines augmented store and load contexts, but the
369 implementation here doesn't actually use them. The code may be
370 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000371 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000372 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000373 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000374 */
375 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376
377 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000378 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000379 if (ctx == Store && !forbidden_check(c, n,
380 PyBytes_AS_STRING(e->v.Attribute.attr)))
381 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000382 e->v.Attribute.ctx = ctx;
383 break;
384 case Subscript_kind:
385 e->v.Subscript.ctx = ctx;
386 break;
387 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000388 if (ctx == Store && !forbidden_check(c, n,
389 PyBytes_AS_STRING(e->v.Name.id)))
390 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000391 e->v.Name.ctx = ctx;
392 break;
393 case List_kind:
394 e->v.List.ctx = ctx;
395 s = e->v.List.elts;
396 break;
397 case Tuple_kind:
398 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
399 return ast_error(n, "can't assign to ()");
400 e->v.Tuple.ctx = ctx;
401 s = e->v.Tuple.elts;
402 break;
403 case Lambda_kind:
404 expr_name = "lambda";
405 break;
406 case Call_kind:
407 expr_name = "function call";
408 break;
409 case BoolOp_kind:
410 case BinOp_kind:
411 case UnaryOp_kind:
412 expr_name = "operator";
413 break;
414 case GeneratorExp_kind:
415 expr_name = "generator expression";
416 break;
417 case Yield_kind:
418 expr_name = "yield expression";
419 break;
420 case ListComp_kind:
421 expr_name = "list comprehension";
422 break;
423 case Dict_kind:
424 case Num_kind:
425 case Str_kind:
426 expr_name = "literal";
427 break;
428 case Compare_kind:
429 expr_name = "comparison";
430 break;
431 case Repr_kind:
432 expr_name = "repr";
433 break;
434 case IfExp_kind:
435 expr_name = "conditional expression";
436 break;
437 default:
438 PyErr_Format(PyExc_SystemError,
439 "unexpected expression in assignment %d (line %d)",
440 e->kind, e->lineno);
441 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000443 /* Check for error string set by switch */
444 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000445 char buf[300];
446 PyOS_snprintf(buf, sizeof(buf),
447 "can't %s %s",
448 ctx == Store ? "assign to" : "delete",
449 expr_name);
450 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000451 }
452
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000454 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455 */
456 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000457 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000459 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000460 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000461 return 0;
462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 }
464 return 1;
465}
466
467static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000468ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469{
470 REQ(n, augassign);
471 n = CHILD(n, 0);
472 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000473 case '+':
474 return Add;
475 case '-':
476 return Sub;
477 case '/':
478 if (STR(n)[1] == '/')
479 return FloorDiv;
480 else
481 return Div;
482 case '%':
483 return Mod;
484 case '<':
485 return LShift;
486 case '>':
487 return RShift;
488 case '&':
489 return BitAnd;
490 case '^':
491 return BitXor;
492 case '|':
493 return BitOr;
494 case '*':
495 if (STR(n)[1] == '*')
496 return Pow;
497 else
498 return Mult;
499 default:
500 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
501 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502 }
503}
504
505static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000506ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507{
508 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000509 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 */
511 REQ(n, comp_op);
512 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000513 n = CHILD(n, 0);
514 switch (TYPE(n)) {
515 case LESS:
516 return Lt;
517 case GREATER:
518 return Gt;
519 case EQEQUAL: /* == */
520 return Eq;
521 case LESSEQUAL:
522 return LtE;
523 case GREATEREQUAL:
524 return GtE;
525 case NOTEQUAL:
526 return NotEq;
527 case NAME:
528 if (strcmp(STR(n), "in") == 0)
529 return In;
530 if (strcmp(STR(n), "is") == 0)
531 return Is;
532 default:
533 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
534 STR(n));
535 return (cmpop_ty)0;
536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 }
538 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000539 /* handle "not in" and "is not" */
540 switch (TYPE(CHILD(n, 0))) {
541 case NAME:
542 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
543 return NotIn;
544 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
545 return IsNot;
546 default:
547 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
548 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
549 return (cmpop_ty)0;
550 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551 }
Neal Norwitz79792652005-11-14 04:25:03 +0000552 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000553 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000554 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555}
556
557static asdl_seq *
558seq_for_testlist(struct compiling *c, const node *n)
559{
560 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000561 asdl_seq *seq;
562 expr_ty expression;
563 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000564 assert(TYPE(n) == testlist ||
565 TYPE(n) == listmaker ||
566 TYPE(n) == testlist_gexp ||
567 TYPE(n) == testlist_safe ||
568 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000570 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000572 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573
574 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000575 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000577 expression = ast_for_expr(c, CHILD(n, i));
578 if (!expression)
579 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000581 assert(i / 2 < seq->size);
582 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 }
584 return seq;
585}
586
587static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000588compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589{
590 int i, len = (NCH(n) + 1) / 2;
591 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000592 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000594 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
Neal Norwitz3a230172006-09-22 08:18:10 +0000596 /* fpdef: NAME | '(' fplist ')'
597 fplist: fpdef (',' fpdef)* [',']
598 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 for (i = 0; i < len; i++) {
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000601 PyObject *arg_id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000602 const node *fpdef_node = CHILD(n, 2*i);
603 const node *child;
604 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000605set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000606 /* fpdef_node is either a NAME or an fplist */
607 child = CHILD(fpdef_node, 0);
608 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000609 if (!forbidden_check(c, n, STR(child)))
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000610 return NULL;
611 arg_id = NEW_IDENTIFIER(child);
612 if (!arg_id)
613 return NULL;
614 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
615 c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000616 }
617 else {
618 assert(TYPE(fpdef_node) == fpdef);
619 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
620 child = CHILD(fpdef_node, 1);
621 assert(TYPE(child) == fplist);
622 /* NCH == 1 means we have (x), we need to elide the extra parens */
623 if (NCH(child) == 1) {
624 fpdef_node = CHILD(child, 0);
625 assert(TYPE(fpdef_node) == fpdef);
626 goto set_name;
627 }
628 arg = compiler_complex_args(c, child);
629 }
630 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 }
632
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000633 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000634 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000635 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 return result;
637}
638
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639
Jeremy Hyltona8293132006-02-28 17:58:27 +0000640/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641
642static arguments_ty
643ast_for_arguments(struct compiling *c, const node *n)
644{
645 /* parameters: '(' [varargslist] ')'
646 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000647 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000649 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 asdl_seq *args, *defaults;
651 identifier vararg = NULL, kwarg = NULL;
652 node *ch;
653
654 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000655 if (NCH(n) == 2) /* () as argument list */
656 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
657 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 }
659 REQ(n, varargslist);
660
661 /* first count the number of normal args & defaults */
662 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000663 ch = CHILD(n, i);
664 if (TYPE(ch) == fpdef)
665 n_args++;
666 if (TYPE(ch) == EQUAL)
667 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000669 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000671 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000672 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000674 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
676 /* fpdef: NAME | '(' fplist ')'
677 fplist: fpdef (',' fpdef)* [',']
678 */
679 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000680 j = 0; /* index for defaults */
681 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000683 ch = CHILD(n, i);
684 switch (TYPE(ch)) {
Benjamin Peterson440847c2009-11-19 23:01:36 +0000685 case fpdef: {
686 int complex_args = 0, parenthesized = 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000687 handle_fpdef:
688 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
689 anything other than EQUAL or a comma? */
690 /* XXX Should NCH(n) check be made a separate check? */
691 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
692 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
693 if (!expression)
694 goto error;
695 assert(defaults != NULL);
696 asdl_seq_SET(defaults, j++, expression);
697 i += 2;
698 found_default = 1;
699 }
700 else if (found_default) {
Benjamin Peterson440847c2009-11-19 23:01:36 +0000701 /* def f((x)=4): pass should raise an error.
702 def f((x, (y))): pass will just incur the tuple unpacking warning. */
703 if (parenthesized && !complex_args) {
704 ast_error(n, "parenthesized arg with default");
705 goto error;
706 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000707 ast_error(n,
708 "non-default argument follows default argument");
709 goto error;
710 }
711 if (NCH(ch) == 3) {
712 ch = CHILD(ch, 1);
713 /* def foo((x)): is not complex, special case. */
714 if (NCH(ch) != 1) {
715 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000716 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
717 "tuple parameter unpacking has been removed in 3.x"))
718 goto error;
Benjamin Peterson440847c2009-11-19 23:01:36 +0000719 complex_args = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000720 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000721 if (!asdl_seq_GET(args, k-1))
722 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000723 } else {
724 /* def foo((x)): setup for checking NAME below. */
725 /* Loop because there can be many parens and tuple
726 unpacking mixed in. */
Benjamin Peterson440847c2009-11-19 23:01:36 +0000727 parenthesized = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000728 ch = CHILD(ch, 0);
729 assert(TYPE(ch) == fpdef);
730 goto handle_fpdef;
731 }
732 }
733 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000734 PyObject *id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000735 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000736 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000737 goto error;
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000738 id = NEW_IDENTIFIER(CHILD(ch, 0));
739 if (!id)
740 goto error;
741 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000742 c->c_arena);
743 if (!name)
744 goto error;
745 asdl_seq_SET(args, k++, name);
746
747 }
748 i += 2; /* the name and the comma */
Benjamin Peterson440847c2009-11-19 23:01:36 +0000749 if (parenthesized && Py_Py3kWarningFlag &&
750 !ast_warn(c, ch, "parenthesized argument names "
751 "are invalid in 3.x"))
752 goto error;
753
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000754 break;
Benjamin Peterson440847c2009-11-19 23:01:36 +0000755 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000756 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000757 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000758 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000759 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000760 if (!vararg)
761 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000762 i += 3;
763 break;
764 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000765 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000766 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000767 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson5cb67632008-11-25 04:00:37 +0000768 if (!kwarg)
769 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000770 i += 3;
771 break;
772 default:
773 PyErr_Format(PyExc_SystemError,
774 "unexpected node in varargslist: %d @ %d",
775 TYPE(ch), i);
776 goto error;
777 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 }
779
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000780 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
782 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000783 Py_XDECREF(vararg);
784 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 return NULL;
786}
787
788static expr_ty
789ast_for_dotted_name(struct compiling *c, const node *n)
790{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000791 expr_ty e;
792 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000793 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 int i;
795
796 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000797
798 lineno = LINENO(n);
799 col_offset = n->n_col_offset;
800
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 id = NEW_IDENTIFIER(CHILD(n, 0));
802 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000803 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000804 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807
808 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000809 id = NEW_IDENTIFIER(CHILD(n, i));
810 if (!id)
811 return NULL;
812 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
813 if (!e)
814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 }
816
817 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818}
819
820static expr_ty
821ast_for_decorator(struct compiling *c, const node *n)
822{
823 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
824 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000825 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826
827 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000828 REQ(CHILD(n, 0), AT);
829 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830
831 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
832 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000833 return NULL;
834
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000836 d = name_expr;
837 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 }
839 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000840 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
841 n->n_col_offset, c->c_arena);
842 if (!d)
843 return NULL;
844 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 }
846 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000847 d = ast_for_call(c, CHILD(n, 3), name_expr);
848 if (!d)
849 return NULL;
850 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 }
852
853 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854}
855
856static asdl_seq*
857ast_for_decorators(struct compiling *c, const node *n)
858{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000859 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000860 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 int i;
862
863 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000864 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000866 return NULL;
867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000869 d = ast_for_decorator(c, CHILD(n, i));
870 if (!d)
871 return NULL;
872 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 }
874 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875}
876
877static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000878ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879{
Christian Heimes5224d282008-02-23 15:01:05 +0000880 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000881 identifier name;
882 arguments_ty args;
883 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000884 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885
886 REQ(n, funcdef);
887
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 name = NEW_IDENTIFIER(CHILD(n, name_i));
889 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000890 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000891 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893 args = ast_for_arguments(c, CHILD(n, name_i + 1));
894 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000895 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896 body = ast_for_suite(c, CHILD(n, name_i + 3));
897 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000898 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000900 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000901 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902}
903
Christian Heimes5224d282008-02-23 15:01:05 +0000904static stmt_ty
905ast_for_decorated(struct compiling *c, const node *n)
906{
907 /* decorated: decorators (classdef | funcdef) */
908 stmt_ty thing = NULL;
909 asdl_seq *decorator_seq = NULL;
910
911 REQ(n, decorated);
912
913 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
914 if (!decorator_seq)
915 return NULL;
916
917 assert(TYPE(CHILD(n, 1)) == funcdef ||
918 TYPE(CHILD(n, 1)) == classdef);
919
920 if (TYPE(CHILD(n, 1)) == funcdef) {
921 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
922 } else if (TYPE(CHILD(n, 1)) == classdef) {
923 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
924 }
925 /* we count the decorators in when talking about the class' or
926 function's line number */
927 if (thing) {
928 thing->lineno = LINENO(n);
929 thing->col_offset = n->n_col_offset;
930 }
931 return thing;
932}
933
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934static expr_ty
935ast_for_lambdef(struct compiling *c, const node *n)
936{
937 /* lambdef: 'lambda' [varargslist] ':' test */
938 arguments_ty args;
939 expr_ty expression;
940
941 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000942 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
943 if (!args)
944 return NULL;
945 expression = ast_for_expr(c, CHILD(n, 2));
946 if (!expression)
947 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 }
949 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000950 args = ast_for_arguments(c, CHILD(n, 1));
951 if (!args)
952 return NULL;
953 expression = ast_for_expr(c, CHILD(n, 3));
954 if (!expression)
955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 }
957
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000958 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959}
960
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000961static expr_ty
962ast_for_ifexpr(struct compiling *c, const node *n)
963{
964 /* test: or_test 'if' or_test 'else' test */
965 expr_ty expression, body, orelse;
966
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000967 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000968 body = ast_for_expr(c, CHILD(n, 0));
969 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000970 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000971 expression = ast_for_expr(c, CHILD(n, 2));
972 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000973 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000974 orelse = ast_for_expr(c, CHILD(n, 4));
975 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000976 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000977 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000978 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000979}
980
Neal Norwitze4d4f002006-09-05 03:58:26 +0000981/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
982 so there is only a single version. Possibly for loops can also re-use
983 the code.
984*/
985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986/* Count the number of 'for' loop in a list comprehension.
987
988 Helper for ast_for_listcomp().
989*/
990
991static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000992count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993{
994 int n_fors = 0;
995 node *ch = CHILD(n, 1);
996
997 count_list_for:
998 n_fors++;
999 REQ(ch, list_for);
1000 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001001 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001003 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 count_list_iter:
1005 REQ(ch, list_iter);
1006 ch = CHILD(ch, 0);
1007 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001008 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001010 if (NCH(ch) == 3) {
1011 ch = CHILD(ch, 2);
1012 goto count_list_iter;
1013 }
1014 else
1015 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001017
1018 /* Should never be reached */
1019 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1020 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021}
1022
1023/* Count the number of 'if' statements in a list comprehension.
1024
1025 Helper for ast_for_listcomp().
1026*/
1027
1028static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001029count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030{
1031 int n_ifs = 0;
1032
1033 count_list_iter:
1034 REQ(n, list_iter);
1035 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001036 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 n = CHILD(n, 0);
1038 REQ(n, list_if);
1039 n_ifs++;
1040 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001041 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 n = CHILD(n, 2);
1043 goto count_list_iter;
1044}
1045
1046static expr_ty
1047ast_for_listcomp(struct compiling *c, const node *n)
1048{
1049 /* listmaker: test ( list_for | (',' test)* [','] )
1050 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1051 list_iter: list_for | list_if
1052 list_if: 'if' test [list_iter]
1053 testlist_safe: test [(',' test)+ [',']]
1054 */
1055 expr_ty elt;
1056 asdl_seq *listcomps;
1057 int i, n_fors;
1058 node *ch;
1059
1060 REQ(n, listmaker);
1061 assert(NCH(n) > 1);
1062
1063 elt = ast_for_expr(c, CHILD(n, 0));
1064 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001065 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001067 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001069 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001071 listcomps = asdl_seq_new(n_fors, c->c_arena);
1072 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001073 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 ch = CHILD(n, 1);
1076 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001077 comprehension_ty lc;
1078 asdl_seq *t;
1079 expr_ty expression;
1080 node *for_ch;
1081
1082 REQ(ch, list_for);
1083
1084 for_ch = CHILD(ch, 1);
1085 t = ast_for_exprlist(c, for_ch, Store);
1086 if (!t)
1087 return NULL;
1088 expression = ast_for_testlist(c, CHILD(ch, 3));
1089 if (!expression)
1090 return NULL;
1091
1092 /* Check the # of children rather than the length of t, since
1093 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1094 */
1095 if (NCH(for_ch) == 1)
1096 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1097 c->c_arena);
1098 else
1099 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1100 c->c_arena),
1101 expression, NULL, c->c_arena);
1102 if (!lc)
1103 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001105 if (NCH(ch) == 5) {
1106 int j, n_ifs;
1107 asdl_seq *ifs;
1108 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001110 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001111 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001112 if (n_ifs == -1)
1113 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001115 ifs = asdl_seq_new(n_ifs, c->c_arena);
1116 if (!ifs)
1117 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001119 for (j = 0; j < n_ifs; j++) {
1120 REQ(ch, list_iter);
1121 ch = CHILD(ch, 0);
1122 REQ(ch, list_if);
1123
1124 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1125 if (!list_for_expr)
1126 return NULL;
1127
1128 asdl_seq_SET(ifs, j, list_for_expr);
1129 if (NCH(ch) == 3)
1130 ch = CHILD(ch, 2);
1131 }
1132 /* on exit, must guarantee that ch is a list_for */
1133 if (TYPE(ch) == list_iter)
1134 ch = CHILD(ch, 0);
1135 lc->ifs = ifs;
1136 }
1137 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 }
1139
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001140 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141}
1142
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001143/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144
1145 Helper for ast_for_genexp().
1146*/
1147
1148static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001149count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001151 int n_fors = 0;
1152 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153
1154 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001155 n_fors++;
1156 REQ(ch, gen_for);
1157 if (NCH(ch) == 5)
1158 ch = CHILD(ch, 4);
1159 else
1160 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001162 REQ(ch, gen_iter);
1163 ch = CHILD(ch, 0);
1164 if (TYPE(ch) == gen_for)
1165 goto count_gen_for;
1166 else if (TYPE(ch) == gen_if) {
1167 if (NCH(ch) == 3) {
1168 ch = CHILD(ch, 2);
1169 goto count_gen_iter;
1170 }
1171 else
1172 return n_fors;
1173 }
1174
1175 /* Should never be reached */
1176 PyErr_SetString(PyExc_SystemError,
1177 "logic error in count_gen_fors");
1178 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179}
1180
1181/* Count the number of 'if' statements in a generator expression.
1182
1183 Helper for ast_for_genexp().
1184*/
1185
1186static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001187count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001189 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001191 while (1) {
1192 REQ(n, gen_iter);
1193 if (TYPE(CHILD(n, 0)) == gen_for)
1194 return n_ifs;
1195 n = CHILD(n, 0);
1196 REQ(n, gen_if);
1197 n_ifs++;
1198 if (NCH(n) == 2)
1199 return n_ifs;
1200 n = CHILD(n, 2);
1201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202}
1203
Jeremy Hyltona8293132006-02-28 17:58:27 +00001204/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205static expr_ty
1206ast_for_genexp(struct compiling *c, const node *n)
1207{
1208 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001209 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 expr_ty elt;
1211 asdl_seq *genexps;
1212 int i, n_fors;
1213 node *ch;
1214
1215 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1216 assert(NCH(n) > 1);
1217
1218 elt = ast_for_expr(c, CHILD(n, 0));
1219 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001222 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001224 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001225
1226 genexps = asdl_seq_new(n_fors, c->c_arena);
1227 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001228 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 ch = CHILD(n, 1);
1231 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001232 comprehension_ty ge;
1233 asdl_seq *t;
1234 expr_ty expression;
1235 node *for_ch;
1236
1237 REQ(ch, gen_for);
1238
1239 for_ch = CHILD(ch, 1);
1240 t = ast_for_exprlist(c, for_ch, Store);
1241 if (!t)
1242 return NULL;
1243 expression = ast_for_expr(c, CHILD(ch, 3));
1244 if (!expression)
1245 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001246
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001247 /* Check the # of children rather than the length of t, since
1248 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1249 if (NCH(for_ch) == 1)
1250 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1251 NULL, c->c_arena);
1252 else
1253 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1254 c->c_arena),
1255 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001256
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001257 if (!ge)
1258 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001259
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001260 if (NCH(ch) == 5) {
1261 int j, n_ifs;
1262 asdl_seq *ifs;
1263
1264 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001265 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001266 if (n_ifs == -1)
1267 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001268
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001269 ifs = asdl_seq_new(n_ifs, c->c_arena);
1270 if (!ifs)
1271 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001272
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001273 for (j = 0; j < n_ifs; j++) {
1274 REQ(ch, gen_iter);
1275 ch = CHILD(ch, 0);
1276 REQ(ch, gen_if);
1277
1278 expression = ast_for_expr(c, CHILD(ch, 1));
1279 if (!expression)
1280 return NULL;
1281 asdl_seq_SET(ifs, j, expression);
1282 if (NCH(ch) == 3)
1283 ch = CHILD(ch, 2);
1284 }
1285 /* on exit, must guarantee that ch is a gen_for */
1286 if (TYPE(ch) == gen_iter)
1287 ch = CHILD(ch, 0);
1288 ge->ifs = ifs;
1289 }
1290 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 }
1292
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001293 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294}
1295
1296static expr_ty
1297ast_for_atom(struct compiling *c, const node *n)
1298{
1299 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1300 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1301 */
1302 node *ch = CHILD(n, 0);
1303
1304 switch (TYPE(ch)) {
Benjamin Peterson5cb67632008-11-25 04:00:37 +00001305 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001306 /* All names start in Load context, but may later be
1307 changed. */
Benjamin Peterson5cb67632008-11-25 04:00:37 +00001308 PyObject *name = NEW_IDENTIFIER(ch);
1309 if (!name)
1310 return NULL;
1311 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1312 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001314 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001315 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001316#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001317 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1318 PyObject *type, *value, *tback, *errstr;
1319 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson71ce9e72008-11-21 22:52:21 +00001320 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001321 if (errstr) {
1322 char *s = "";
1323 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001324 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001325 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1326 ast_error(n, buf);
Benjamin Peterson71ce9e72008-11-21 22:52:21 +00001327 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001328 } else {
1329 ast_error(n, "(unicode error) unknown error");
1330 }
1331 Py_DECREF(type);
1332 Py_DECREF(value);
1333 Py_XDECREF(tback);
1334 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001335#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001336 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001337 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001338 PyArena_AddPyObject(c->c_arena, str);
1339 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 }
1341 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001342 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001343 if (!pynum)
1344 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001345
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001346 PyArena_AddPyObject(c->c_arena, pynum);
1347 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 }
1349 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001350 ch = CHILD(n, 1);
1351
1352 if (TYPE(ch) == RPAR)
1353 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1354
1355 if (TYPE(ch) == yield_expr)
1356 return ast_for_expr(c, ch);
1357
1358 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1359 return ast_for_genexp(c, ch);
1360
1361 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001363 ch = CHILD(n, 1);
1364
1365 if (TYPE(ch) == RSQB)
1366 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1367
1368 REQ(ch, listmaker);
1369 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1370 asdl_seq *elts = seq_for_testlist(c, ch);
1371 if (!elts)
1372 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001373
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001374 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1375 }
1376 else
1377 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001379 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1380 int i, size;
1381 asdl_seq *keys, *values;
1382
1383 ch = CHILD(n, 1);
1384 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1385 keys = asdl_seq_new(size, c->c_arena);
1386 if (!keys)
1387 return NULL;
1388
1389 values = asdl_seq_new(size, c->c_arena);
1390 if (!values)
1391 return NULL;
1392
1393 for (i = 0; i < NCH(ch); i += 4) {
1394 expr_ty expression;
1395
1396 expression = ast_for_expr(c, CHILD(ch, i));
1397 if (!expression)
1398 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001399
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001400 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001401
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001402 expression = ast_for_expr(c, CHILD(ch, i + 2));
1403 if (!expression)
1404 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001405
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001406 asdl_seq_SET(values, i / 4, expression);
1407 }
1408 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 }
1410 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001411 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001412 if (Py_Py3kWarningFlag &&
1413 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001414 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001415 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001416 if (!expression)
1417 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001418
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001419 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 }
1421 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001422 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1423 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 }
1425}
1426
1427static slice_ty
1428ast_for_slice(struct compiling *c, const node *n)
1429{
1430 node *ch;
1431 expr_ty lower = NULL, upper = NULL, step = NULL;
1432
1433 REQ(n, subscript);
1434
1435 /*
1436 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1437 sliceop: ':' [test]
1438 */
1439 ch = CHILD(n, 0);
1440 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001441 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442
1443 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001444 /* 'step' variable hold no significance in terms of being used over
1445 other vars */
1446 step = ast_for_expr(c, ch);
1447 if (!step)
1448 return NULL;
1449
1450 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 }
1452
1453 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001454 lower = ast_for_expr(c, ch);
1455 if (!lower)
1456 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 }
1458
1459 /* If there's an upper bound it's in the second or third position. */
1460 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001461 if (NCH(n) > 1) {
1462 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001464 if (TYPE(n2) == test) {
1465 upper = ast_for_expr(c, n2);
1466 if (!upper)
1467 return NULL;
1468 }
1469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001471 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001473 if (TYPE(n2) == test) {
1474 upper = ast_for_expr(c, n2);
1475 if (!upper)
1476 return NULL;
1477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 }
1479
1480 ch = CHILD(n, NCH(n) - 1);
1481 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001482 if (NCH(ch) == 1) {
1483 /* No expression, so step is None */
1484 ch = CHILD(ch, 0);
1485 step = Name(new_identifier("None", c->c_arena), Load,
1486 LINENO(ch), ch->n_col_offset, c->c_arena);
1487 if (!step)
1488 return NULL;
1489 } else {
1490 ch = CHILD(ch, 1);
1491 if (TYPE(ch) == test) {
1492 step = ast_for_expr(c, ch);
1493 if (!step)
1494 return NULL;
1495 }
1496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 }
1498
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001499 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
1502static expr_ty
1503ast_for_binop(struct compiling *c, const node *n)
1504{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001505 /* Must account for a sequence of expressions.
1506 How should A op B op C by represented?
1507 BinOp(BinOp(A, op, B), op, C).
1508 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001510 int i, nops;
1511 expr_ty expr1, expr2, result;
1512 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001514 expr1 = ast_for_expr(c, CHILD(n, 0));
1515 if (!expr1)
1516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001518 expr2 = ast_for_expr(c, CHILD(n, 2));
1519 if (!expr2)
1520 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001522 newoperator = get_operator(CHILD(n, 1));
1523 if (!newoperator)
1524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001526 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1527 c->c_arena);
1528 if (!result)
1529 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001531 nops = (NCH(n) - 1) / 2;
1532 for (i = 1; i < nops; i++) {
1533 expr_ty tmp_result, tmp;
1534 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001536 newoperator = get_operator(next_oper);
1537 if (!newoperator)
1538 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001540 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1541 if (!tmp)
1542 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001544 tmp_result = BinOp(result, newoperator, tmp,
1545 LINENO(next_oper), next_oper->n_col_offset,
1546 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001547 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001548 return NULL;
1549 result = tmp_result;
1550 }
1551 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552}
1553
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001554static expr_ty
1555ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1556{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001557 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1558 subscriptlist: subscript (',' subscript)* [',']
1559 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1560 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001561 REQ(n, trailer);
1562 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001563 if (NCH(n) == 2)
1564 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1565 n->n_col_offset, c->c_arena);
1566 else
1567 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001568 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001569 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson5cb67632008-11-25 04:00:37 +00001570 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1571 if (!attr_id)
1572 return NULL;
1573 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001574 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001575 }
1576 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001577 REQ(CHILD(n, 0), LSQB);
1578 REQ(CHILD(n, 2), RSQB);
1579 n = CHILD(n, 1);
1580 if (NCH(n) == 1) {
1581 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1582 if (!slc)
1583 return NULL;
1584 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1585 c->c_arena);
1586 }
1587 else {
1588 /* The grammar is ambiguous here. The ambiguity is resolved
1589 by treating the sequence as a tuple literal if there are
1590 no slice features.
1591 */
1592 int j;
1593 slice_ty slc;
1594 expr_ty e;
1595 bool simple = true;
1596 asdl_seq *slices, *elts;
1597 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1598 if (!slices)
1599 return NULL;
1600 for (j = 0; j < NCH(n); j += 2) {
1601 slc = ast_for_slice(c, CHILD(n, j));
1602 if (!slc)
1603 return NULL;
1604 if (slc->kind != Index_kind)
1605 simple = false;
1606 asdl_seq_SET(slices, j / 2, slc);
1607 }
1608 if (!simple) {
1609 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1610 Load, LINENO(n), n->n_col_offset, c->c_arena);
1611 }
1612 /* extract Index values and put them in a Tuple */
1613 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1614 if (!elts)
1615 return NULL;
1616 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1617 slc = (slice_ty)asdl_seq_GET(slices, j);
1618 assert(slc->kind == Index_kind && slc->v.Index.value);
1619 asdl_seq_SET(elts, j, slc->v.Index.value);
1620 }
1621 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1622 if (!e)
1623 return NULL;
1624 return Subscript(left_expr, Index(e, c->c_arena),
1625 Load, LINENO(n), n->n_col_offset, c->c_arena);
1626 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001627 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001628}
1629
1630static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001631ast_for_factor(struct compiling *c, const node *n)
1632{
1633 node *pfactor, *ppower, *patom, *pnum;
1634 expr_ty expression;
1635
1636 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001637 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001638 constant. The peephole optimizer already does something like
1639 this but it doesn't handle the case where the constant is
1640 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1641 PyLongObject.
1642 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001643 if (TYPE(CHILD(n, 0)) == MINUS &&
1644 NCH(n) == 2 &&
1645 TYPE((pfactor = CHILD(n, 1))) == factor &&
1646 NCH(pfactor) == 1 &&
1647 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1648 NCH(ppower) == 1 &&
1649 TYPE((patom = CHILD(ppower, 0))) == atom &&
1650 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1651 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1652 if (s == NULL)
1653 return NULL;
1654 s[0] = '-';
1655 strcpy(s + 1, STR(pnum));
1656 PyObject_FREE(STR(pnum));
1657 STR(pnum) = s;
1658 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001659 }
1660
1661 expression = ast_for_expr(c, CHILD(n, 1));
1662 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001663 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001664
1665 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001666 case PLUS:
1667 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1668 c->c_arena);
1669 case MINUS:
1670 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1671 c->c_arena);
1672 case TILDE:
1673 return UnaryOp(Invert, expression, LINENO(n),
1674 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001675 }
1676 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001677 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001678 return NULL;
1679}
1680
1681static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001682ast_for_power(struct compiling *c, const node *n)
1683{
1684 /* power: atom trailer* ('**' factor)*
1685 */
1686 int i;
1687 expr_ty e, tmp;
1688 REQ(n, power);
1689 e = ast_for_atom(c, CHILD(n, 0));
1690 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001691 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001692 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001693 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001694 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001695 node *ch = CHILD(n, i);
1696 if (TYPE(ch) != trailer)
1697 break;
1698 tmp = ast_for_trailer(c, ch, e);
1699 if (!tmp)
1700 return NULL;
1701 tmp->lineno = e->lineno;
1702 tmp->col_offset = e->col_offset;
1703 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001704 }
1705 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001706 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1707 if (!f)
1708 return NULL;
1709 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1710 if (!tmp)
1711 return NULL;
1712 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001713 }
1714 return e;
1715}
1716
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717/* Do not name a variable 'expr'! Will cause a compile error.
1718*/
1719
1720static expr_ty
1721ast_for_expr(struct compiling *c, const node *n)
1722{
1723 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001724 test: or_test ['if' or_test 'else' test] | lambdef
1725 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 and_test: not_test ('and' not_test)*
1727 not_test: 'not' not_test | comparison
1728 comparison: expr (comp_op expr)*
1729 expr: xor_expr ('|' xor_expr)*
1730 xor_expr: and_expr ('^' and_expr)*
1731 and_expr: shift_expr ('&' shift_expr)*
1732 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1733 arith_expr: term (('+'|'-') term)*
1734 term: factor (('*'|'/'|'%'|'//') factor)*
1735 factor: ('+'|'-'|'~') factor | power
1736 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001737
1738 As well as modified versions that exist for backward compatibility,
1739 to explicitly allow:
1740 [ x for x in lambda: 0, lambda: 1 ]
1741 (which would be ambiguous without these extra rules)
1742
1743 old_test: or_test | old_lambdef
1744 old_lambdef: 'lambda' [vararglist] ':' old_test
1745
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 */
1747
1748 asdl_seq *seq;
1749 int i;
1750
1751 loop:
1752 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001753 case test:
1754 case old_test:
1755 if (TYPE(CHILD(n, 0)) == lambdef ||
1756 TYPE(CHILD(n, 0)) == old_lambdef)
1757 return ast_for_lambdef(c, CHILD(n, 0));
1758 else if (NCH(n) > 1)
1759 return ast_for_ifexpr(c, n);
1760 /* Fallthrough */
1761 case or_test:
1762 case and_test:
1763 if (NCH(n) == 1) {
1764 n = CHILD(n, 0);
1765 goto loop;
1766 }
1767 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1768 if (!seq)
1769 return NULL;
1770 for (i = 0; i < NCH(n); i += 2) {
1771 expr_ty e = ast_for_expr(c, CHILD(n, i));
1772 if (!e)
1773 return NULL;
1774 asdl_seq_SET(seq, i / 2, e);
1775 }
1776 if (!strcmp(STR(CHILD(n, 1)), "and"))
1777 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1778 c->c_arena);
1779 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1780 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1781 case not_test:
1782 if (NCH(n) == 1) {
1783 n = CHILD(n, 0);
1784 goto loop;
1785 }
1786 else {
1787 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1788 if (!expression)
1789 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001791 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1792 c->c_arena);
1793 }
1794 case comparison:
1795 if (NCH(n) == 1) {
1796 n = CHILD(n, 0);
1797 goto loop;
1798 }
1799 else {
1800 expr_ty expression;
1801 asdl_int_seq *ops;
1802 asdl_seq *cmps;
1803 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1804 if (!ops)
1805 return NULL;
1806 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1807 if (!cmps) {
1808 return NULL;
1809 }
1810 for (i = 1; i < NCH(n); i += 2) {
1811 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001813 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001814 if (!newoperator) {
1815 return NULL;
1816 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001818 expression = ast_for_expr(c, CHILD(n, i + 1));
1819 if (!expression) {
1820 return NULL;
1821 }
1822
1823 asdl_seq_SET(ops, i / 2, newoperator);
1824 asdl_seq_SET(cmps, i / 2, expression);
1825 }
1826 expression = ast_for_expr(c, CHILD(n, 0));
1827 if (!expression) {
1828 return NULL;
1829 }
1830
1831 return Compare(expression, ops, cmps, LINENO(n),
1832 n->n_col_offset, c->c_arena);
1833 }
1834 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001836 /* The next five cases all handle BinOps. The main body of code
1837 is the same in each case, but the switch turned inside out to
1838 reuse the code for each type of operator.
1839 */
1840 case expr:
1841 case xor_expr:
1842 case and_expr:
1843 case shift_expr:
1844 case arith_expr:
1845 case term:
1846 if (NCH(n) == 1) {
1847 n = CHILD(n, 0);
1848 goto loop;
1849 }
1850 return ast_for_binop(c, n);
1851 case yield_expr: {
1852 expr_ty exp = NULL;
1853 if (NCH(n) == 2) {
1854 exp = ast_for_testlist(c, CHILD(n, 1));
1855 if (!exp)
1856 return NULL;
1857 }
1858 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1859 }
1860 case factor:
1861 if (NCH(n) == 1) {
1862 n = CHILD(n, 0);
1863 goto loop;
1864 }
1865 return ast_for_factor(c, n);
1866 case power:
1867 return ast_for_power(c, n);
1868 default:
1869 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1870 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001872 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 return NULL;
1874}
1875
1876static expr_ty
1877ast_for_call(struct compiling *c, const node *n, expr_ty func)
1878{
1879 /*
1880 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001881 | '**' test)
1882 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 */
1884
1885 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001886 asdl_seq *args;
1887 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 expr_ty vararg = NULL, kwarg = NULL;
1889
1890 REQ(n, arglist);
1891
1892 nargs = 0;
1893 nkeywords = 0;
1894 ngens = 0;
1895 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001896 node *ch = CHILD(n, i);
1897 if (TYPE(ch) == argument) {
1898 if (NCH(ch) == 1)
1899 nargs++;
1900 else if (TYPE(CHILD(ch, 1)) == gen_for)
1901 ngens++;
1902 else
1903 nkeywords++;
1904 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 }
1906 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001907 ast_error(n, "Generator expression must be parenthesized "
1908 "if not sole argument");
1909 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 }
1911
1912 if (nargs + nkeywords + ngens > 255) {
1913 ast_error(n, "more than 255 arguments");
1914 return NULL;
1915 }
1916
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001917 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001919 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001920 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001922 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923 nargs = 0;
1924 nkeywords = 0;
1925 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001926 node *ch = CHILD(n, i);
1927 if (TYPE(ch) == argument) {
1928 expr_ty e;
1929 if (NCH(ch) == 1) {
1930 if (nkeywords) {
1931 ast_error(CHILD(ch, 0),
1932 "non-keyword arg after keyword arg");
1933 return NULL;
1934 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001935 if (vararg) {
1936 ast_error(CHILD(ch, 0),
1937 "only named arguments may follow *expression");
1938 return NULL;
1939 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001940 e = ast_for_expr(c, CHILD(ch, 0));
1941 if (!e)
1942 return NULL;
1943 asdl_seq_SET(args, nargs++, e);
1944 }
1945 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1946 e = ast_for_genexp(c, ch);
1947 if (!e)
1948 return NULL;
1949 asdl_seq_SET(args, nargs++, e);
1950 }
1951 else {
1952 keyword_ty kw;
1953 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001954 int k;
1955 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001957 /* CHILD(ch, 0) is test, but must be an identifier? */
1958 e = ast_for_expr(c, CHILD(ch, 0));
1959 if (!e)
1960 return NULL;
1961 /* f(lambda x: x[0] = 3) ends up getting parsed with
1962 * LHS test = lambda x: x[0], and RHS test = 3.
1963 * SF bug 132313 points out that complaining about a keyword
1964 * then is very confusing.
1965 */
1966 if (e->kind == Lambda_kind) {
1967 ast_error(CHILD(ch, 0),
1968 "lambda cannot contain assignment");
1969 return NULL;
1970 } else if (e->kind != Name_kind) {
1971 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1972 return NULL;
1973 }
1974 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001975 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001976 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001977 for (k = 0; k < nkeywords; k++) {
1978 tmp = PyString_AS_STRING(
1979 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1980 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1981 ast_error(CHILD(ch, 0), "keyword argument repeated");
1982 return NULL;
1983 }
1984 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001985 e = ast_for_expr(c, CHILD(ch, 2));
1986 if (!e)
1987 return NULL;
1988 kw = keyword(key, e, c->c_arena);
1989 if (!kw)
1990 return NULL;
1991 asdl_seq_SET(keywords, nkeywords++, kw);
1992 }
1993 }
1994 else if (TYPE(ch) == STAR) {
1995 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001996 if (!vararg)
1997 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001998 i++;
1999 }
2000 else if (TYPE(ch) == DOUBLESTAR) {
2001 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002002 if (!kwarg)
2003 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002004 i++;
2005 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006 }
2007
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002008 return Call(func, args, keywords, vararg, kwarg, func->lineno,
2009 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010}
2011
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002013ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002015 /* testlist_gexp: test (',' test)* [','] */
2016 /* testlist: test (',' test)* [','] */
2017 /* testlist_safe: test (',' test)+ [','] */
2018 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002020 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002021 if (NCH(n) > 1)
2022 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002023 }
2024 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002025 assert(TYPE(n) == testlist ||
2026 TYPE(n) == testlist_safe ||
2027 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002028 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002030 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002032 asdl_seq *tmp = seq_for_testlist(c, n);
2033 if (!tmp)
2034 return NULL;
2035 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002037}
2038
2039static expr_ty
2040ast_for_testlist_gexp(struct compiling *c, const node* n)
2041{
2042 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2043 /* argument: test [ gen_for ] */
2044 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002045 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002046 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002047 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002048}
2049
2050/* like ast_for_testlist() but returns a sequence */
2051static asdl_seq*
2052ast_for_class_bases(struct compiling *c, const node* n)
2053{
2054 /* testlist: test (',' test)* [','] */
2055 assert(NCH(n) > 0);
2056 REQ(n, testlist);
2057 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002058 expr_ty base;
2059 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2060 if (!bases)
2061 return NULL;
2062 base = ast_for_expr(c, CHILD(n, 0));
2063 if (!base)
2064 return NULL;
2065 asdl_seq_SET(bases, 0, base);
2066 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002067 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002068
2069 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070}
2071
2072static stmt_ty
2073ast_for_expr_stmt(struct compiling *c, const node *n)
2074{
2075 REQ(n, expr_stmt);
2076 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002077 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 testlist: test (',' test)* [',']
2079 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002080 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 test: ... here starts the operator precendence dance
2082 */
2083
2084 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002085 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2086 if (!e)
2087 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002089 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 }
2091 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002092 expr_ty expr1, expr2;
2093 operator_ty newoperator;
2094 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002096 expr1 = ast_for_testlist(c, ch);
2097 if (!expr1)
2098 return NULL;
2099 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2100 switch (expr1->kind) {
2101 case GeneratorExp_kind:
2102 ast_error(ch, "augmented assignment to generator "
2103 "expression not possible");
2104 return NULL;
2105 case Yield_kind:
2106 ast_error(ch, "augmented assignment to yield "
2107 "expression not possible");
2108 return NULL;
2109 case Name_kind: {
Christian Heimes593daf52008-05-26 12:51:38 +00002110 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
Benjamin Petersond5efd202008-06-08 22:52:37 +00002111 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
2112 !forbidden_check(c, ch, var_name))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002113 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002114 break;
2115 }
2116 case Attribute_kind:
2117 case Subscript_kind:
2118 break;
2119 default:
2120 ast_error(ch, "illegal expression for augmented "
2121 "assignment");
2122 return NULL;
2123 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002124 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002125 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002127 ch = CHILD(n, 2);
2128 if (TYPE(ch) == testlist)
2129 expr2 = ast_for_testlist(c, ch);
2130 else
2131 expr2 = ast_for_expr(c, ch);
2132 if (!expr2)
2133 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002135 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002136 if (!newoperator)
2137 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002139 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2140 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 }
2142 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002143 int i;
2144 asdl_seq *targets;
2145 node *value;
2146 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002148 /* a normal assignment */
2149 REQ(CHILD(n, 1), EQUAL);
2150 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2151 if (!targets)
2152 return NULL;
2153 for (i = 0; i < NCH(n) - 2; i += 2) {
2154 expr_ty e;
2155 node *ch = CHILD(n, i);
2156 if (TYPE(ch) == yield_expr) {
2157 ast_error(ch, "assignment to yield expression not possible");
2158 return NULL;
2159 }
2160 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002162 /* set context to assign */
2163 if (!e)
2164 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002166 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002169 asdl_seq_SET(targets, i / 2, e);
2170 }
2171 value = CHILD(n, NCH(n) - 1);
2172 if (TYPE(value) == testlist)
2173 expression = ast_for_testlist(c, value);
2174 else
2175 expression = ast_for_expr(c, value);
2176 if (!expression)
2177 return NULL;
2178 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2179 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181}
2182
2183static stmt_ty
2184ast_for_print_stmt(struct compiling *c, const node *n)
2185{
2186 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002187 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 */
2189 expr_ty dest = NULL, expression;
2190 asdl_seq *seq;
2191 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002192 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193
2194 REQ(n, print_stmt);
2195 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002196 dest = ast_for_expr(c, CHILD(n, 2));
2197 if (!dest)
2198 return NULL;
2199 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002201 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002203 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002204 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002205 expression = ast_for_expr(c, CHILD(n, i));
2206 if (!expression)
2207 return NULL;
2208 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 }
2210 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002211 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212}
2213
2214static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002215ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216{
2217 asdl_seq *seq;
2218 int i;
2219 expr_ty e;
2220
2221 REQ(n, exprlist);
2222
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002223 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002227 e = ast_for_expr(c, CHILD(n, i));
2228 if (!e)
2229 return NULL;
2230 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002231 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002232 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233 }
2234 return seq;
2235}
2236
2237static stmt_ty
2238ast_for_del_stmt(struct compiling *c, const node *n)
2239{
2240 asdl_seq *expr_list;
2241
2242 /* del_stmt: 'del' exprlist */
2243 REQ(n, del_stmt);
2244
2245 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2246 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002247 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002248 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249}
2250
2251static stmt_ty
2252ast_for_flow_stmt(struct compiling *c, const node *n)
2253{
2254 /*
2255 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002256 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 break_stmt: 'break'
2258 continue_stmt: 'continue'
2259 return_stmt: 'return' [testlist]
2260 yield_stmt: yield_expr
2261 yield_expr: 'yield' testlist
2262 raise_stmt: 'raise' [test [',' test [',' test]]]
2263 */
2264 node *ch;
2265
2266 REQ(n, flow_stmt);
2267 ch = CHILD(n, 0);
2268 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002269 case break_stmt:
2270 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2271 case continue_stmt:
2272 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2273 case yield_stmt: { /* will reduce to yield_expr */
2274 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2275 if (!exp)
2276 return NULL;
2277 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2278 }
2279 case return_stmt:
2280 if (NCH(ch) == 1)
2281 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2282 else {
2283 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2284 if (!expression)
2285 return NULL;
2286 return Return(expression, LINENO(n), n->n_col_offset,
2287 c->c_arena);
2288 }
2289 case raise_stmt:
2290 if (NCH(ch) == 1)
2291 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2292 c->c_arena);
2293 else if (NCH(ch) == 2) {
2294 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2295 if (!expression)
2296 return NULL;
2297 return Raise(expression, NULL, NULL, LINENO(n),
2298 n->n_col_offset, c->c_arena);
2299 }
2300 else if (NCH(ch) == 4) {
2301 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002303 expr1 = ast_for_expr(c, CHILD(ch, 1));
2304 if (!expr1)
2305 return NULL;
2306 expr2 = ast_for_expr(c, CHILD(ch, 3));
2307 if (!expr2)
2308 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002310 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2311 c->c_arena);
2312 }
2313 else if (NCH(ch) == 6) {
2314 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002316 expr1 = ast_for_expr(c, CHILD(ch, 1));
2317 if (!expr1)
2318 return NULL;
2319 expr2 = ast_for_expr(c, CHILD(ch, 3));
2320 if (!expr2)
2321 return NULL;
2322 expr3 = ast_for_expr(c, CHILD(ch, 5));
2323 if (!expr3)
2324 return NULL;
2325
2326 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2327 c->c_arena);
2328 }
2329 default:
2330 PyErr_Format(PyExc_SystemError,
2331 "unexpected flow_stmt: %d", TYPE(ch));
2332 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002334
2335 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2336 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337}
2338
2339static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002340alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341{
2342 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002343 import_as_name: NAME ['as' NAME]
2344 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 dotted_name: NAME ('.' NAME)*
2346 */
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002347 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002348
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 loop:
2350 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002351 case import_as_name:
2352 str = NULL;
2353 if (NCH(n) == 3) {
2354 str = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002355 if (!str)
2356 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002357 }
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002358 name = NEW_IDENTIFIER(CHILD(n, 0));
2359 if (!name)
2360 return NULL;
2361 return alias(name, str, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002362 case dotted_as_name:
2363 if (NCH(n) == 1) {
2364 n = CHILD(n, 0);
2365 goto loop;
2366 }
2367 else {
2368 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2369 if (!a)
2370 return NULL;
2371 assert(!a->asname);
2372 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002373 if (!a->asname)
2374 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002375 return a;
2376 }
2377 break;
2378 case dotted_name:
Benjamin Peterson5cb67632008-11-25 04:00:37 +00002379 if (NCH(n) == 1) {
2380 name = NEW_IDENTIFIER(CHILD(n, 0));
2381 if (!name)
2382 return NULL;
2383 return alias(name, NULL, c->c_arena);
2384 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002385 else {
2386 /* Create a string of the form "a.b.c" */
2387 int i;
2388 size_t len;
2389 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002391 len = 0;
2392 for (i = 0; i < NCH(n); i += 2)
2393 /* length of string plus one for the dot */
2394 len += strlen(STR(CHILD(n, i))) + 1;
2395 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002396 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002397 if (!str)
2398 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002399 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002400 if (!s)
2401 return NULL;
2402 for (i = 0; i < NCH(n); i += 2) {
2403 char *sch = STR(CHILD(n, i));
2404 strcpy(s, STR(CHILD(n, i)));
2405 s += strlen(sch);
2406 *s++ = '.';
2407 }
2408 --s;
2409 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002410 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002411 PyArena_AddPyObject(c->c_arena, str);
2412 return alias(str, NULL, c->c_arena);
2413 }
2414 break;
2415 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002416 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002417 PyArena_AddPyObject(c->c_arena, str);
2418 return alias(str, NULL, c->c_arena);
2419 default:
2420 PyErr_Format(PyExc_SystemError,
2421 "unexpected import name: %d", TYPE(n));
2422 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002424
2425 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 return NULL;
2427}
2428
2429static stmt_ty
2430ast_for_import_stmt(struct compiling *c, const node *n)
2431{
2432 /*
2433 import_stmt: import_name | import_from
2434 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002435 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002436 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002438 int lineno;
2439 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 int i;
2441 asdl_seq *aliases;
2442
2443 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002444 lineno = LINENO(n);
2445 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002447 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002448 n = CHILD(n, 1);
2449 REQ(n, dotted_as_names);
2450 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2451 if (!aliases)
2452 return NULL;
2453 for (i = 0; i < NCH(n); i += 2) {
2454 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2455 if (!import_alias)
2456 return NULL;
2457 asdl_seq_SET(aliases, i / 2, import_alias);
2458 }
2459 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002461 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002462 int n_children;
2463 int idx, ndots = 0;
2464 alias_ty mod = NULL;
2465 identifier modname;
2466
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002467 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002468 optional module name */
2469 for (idx = 1; idx < NCH(n); idx++) {
2470 if (TYPE(CHILD(n, idx)) == dotted_name) {
2471 mod = alias_for_import_name(c, CHILD(n, idx));
2472 idx++;
2473 break;
2474 } else if (TYPE(CHILD(n, idx)) != DOT) {
2475 break;
2476 }
2477 ndots++;
2478 }
2479 idx++; /* skip over the 'import' keyword */
2480 switch (TYPE(CHILD(n, idx))) {
2481 case STAR:
2482 /* from ... import * */
2483 n = CHILD(n, idx);
2484 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002485 break;
2486 case LPAR:
2487 /* from ... import (x, y, z) */
2488 n = CHILD(n, idx + 1);
2489 n_children = NCH(n);
2490 break;
2491 case import_as_names:
2492 /* from ... import x, y, z */
2493 n = CHILD(n, idx);
2494 n_children = NCH(n);
2495 if (n_children % 2 == 0) {
2496 ast_error(n, "trailing comma not allowed without"
2497 " surrounding parentheses");
2498 return NULL;
2499 }
2500 break;
2501 default:
2502 ast_error(n, "Unexpected node-type in from-import");
2503 return NULL;
2504 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002506 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2507 if (!aliases)
2508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002510 /* handle "from ... import *" special b/c there's no children */
2511 if (TYPE(n) == STAR) {
2512 alias_ty import_alias = alias_for_import_name(c, n);
2513 if (!import_alias)
2514 return NULL;
2515 asdl_seq_SET(aliases, 0, import_alias);
2516 }
2517 else {
2518 for (i = 0; i < NCH(n); i += 2) {
2519 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2520 if (!import_alias)
2521 return NULL;
2522 asdl_seq_SET(aliases, i / 2, import_alias);
2523 }
2524 }
2525 if (mod != NULL)
2526 modname = mod->name;
2527 else
2528 modname = new_identifier("", c->c_arena);
2529 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2530 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 }
Neal Norwitz79792652005-11-14 04:25:03 +00002532 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002533 "unknown import statement: starts with command '%s'",
2534 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 return NULL;
2536}
2537
2538static stmt_ty
2539ast_for_global_stmt(struct compiling *c, const node *n)
2540{
2541 /* global_stmt: 'global' NAME (',' NAME)* */
2542 identifier name;
2543 asdl_seq *s;
2544 int i;
2545
2546 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002547 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002551 name = NEW_IDENTIFIER(CHILD(n, i));
2552 if (!name)
2553 return NULL;
2554 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002556 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557}
2558
2559static stmt_ty
2560ast_for_exec_stmt(struct compiling *c, const node *n)
2561{
2562 expr_ty expr1, globals = NULL, locals = NULL;
2563 int n_children = NCH(n);
2564 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002565 PyErr_Format(PyExc_SystemError,
2566 "poorly formed 'exec' statement: %d parts to statement",
2567 n_children);
2568 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 }
2570
2571 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2572 REQ(n, exec_stmt);
2573 expr1 = ast_for_expr(c, CHILD(n, 1));
2574 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002575 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002577 globals = ast_for_expr(c, CHILD(n, 3));
2578 if (!globals)
2579 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 }
2581 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002582 locals = ast_for_expr(c, CHILD(n, 5));
2583 if (!locals)
2584 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 }
2586
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002587 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2588 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589}
2590
2591static stmt_ty
2592ast_for_assert_stmt(struct compiling *c, const node *n)
2593{
2594 /* assert_stmt: 'assert' test [',' test] */
2595 REQ(n, assert_stmt);
2596 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002597 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2598 if (!expression)
2599 return NULL;
2600 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2601 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 }
2603 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002604 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002606 expr1 = ast_for_expr(c, CHILD(n, 1));
2607 if (!expr1)
2608 return NULL;
2609 expr2 = ast_for_expr(c, CHILD(n, 3));
2610 if (!expr2)
2611 return NULL;
2612
2613 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 }
Neal Norwitz79792652005-11-14 04:25:03 +00002615 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002616 "improper number of parts to 'assert' statement: %d",
2617 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618 return NULL;
2619}
2620
2621static asdl_seq *
2622ast_for_suite(struct compiling *c, const node *n)
2623{
2624 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002625 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 stmt_ty s;
2627 int i, total, num, end, pos = 0;
2628 node *ch;
2629
2630 REQ(n, suite);
2631
2632 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002633 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002635 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002637 n = CHILD(n, 0);
2638 /* simple_stmt always ends with a NEWLINE,
2639 and may have a trailing SEMI
2640 */
2641 end = NCH(n) - 1;
2642 if (TYPE(CHILD(n, end - 1)) == SEMI)
2643 end--;
2644 /* loop by 2 to skip semi-colons */
2645 for (i = 0; i < end; i += 2) {
2646 ch = CHILD(n, i);
2647 s = ast_for_stmt(c, ch);
2648 if (!s)
2649 return NULL;
2650 asdl_seq_SET(seq, pos++, s);
2651 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 }
2653 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002654 for (i = 2; i < (NCH(n) - 1); i++) {
2655 ch = CHILD(n, i);
2656 REQ(ch, stmt);
2657 num = num_stmts(ch);
2658 if (num == 1) {
2659 /* small_stmt or compound_stmt with only one child */
2660 s = ast_for_stmt(c, ch);
2661 if (!s)
2662 return NULL;
2663 asdl_seq_SET(seq, pos++, s);
2664 }
2665 else {
2666 int j;
2667 ch = CHILD(ch, 0);
2668 REQ(ch, simple_stmt);
2669 for (j = 0; j < NCH(ch); j += 2) {
2670 /* statement terminates with a semi-colon ';' */
2671 if (NCH(CHILD(ch, j)) == 0) {
2672 assert((j + 1) == NCH(ch));
2673 break;
2674 }
2675 s = ast_for_stmt(c, CHILD(ch, j));
2676 if (!s)
2677 return NULL;
2678 asdl_seq_SET(seq, pos++, s);
2679 }
2680 }
2681 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 }
2683 assert(pos == seq->size);
2684 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685}
2686
2687static stmt_ty
2688ast_for_if_stmt(struct compiling *c, const node *n)
2689{
2690 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2691 ['else' ':' suite]
2692 */
2693 char *s;
2694
2695 REQ(n, if_stmt);
2696
2697 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002698 expr_ty expression;
2699 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002701 expression = ast_for_expr(c, CHILD(n, 1));
2702 if (!expression)
2703 return NULL;
2704 suite_seq = ast_for_suite(c, CHILD(n, 3));
2705 if (!suite_seq)
2706 return NULL;
2707
2708 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2709 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002711
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712 s = STR(CHILD(n, 4));
2713 /* s[2], the third character in the string, will be
2714 's' for el_s_e, or
2715 'i' for el_i_f
2716 */
2717 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002718 expr_ty expression;
2719 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002721 expression = ast_for_expr(c, CHILD(n, 1));
2722 if (!expression)
2723 return NULL;
2724 seq1 = ast_for_suite(c, CHILD(n, 3));
2725 if (!seq1)
2726 return NULL;
2727 seq2 = ast_for_suite(c, CHILD(n, 6));
2728 if (!seq2)
2729 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002731 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2732 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 }
2734 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002735 int i, n_elif, has_else = 0;
2736 expr_ty expression;
2737 asdl_seq *suite_seq;
2738 asdl_seq *orelse = NULL;
2739 n_elif = NCH(n) - 4;
2740 /* must reference the child n_elif+1 since 'else' token is third,
2741 not fourth, child from the end. */
2742 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2743 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2744 has_else = 1;
2745 n_elif -= 3;
2746 }
2747 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002749 if (has_else) {
2750 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002752 orelse = asdl_seq_new(1, c->c_arena);
2753 if (!orelse)
2754 return NULL;
2755 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2756 if (!expression)
2757 return NULL;
2758 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2759 if (!suite_seq)
2760 return NULL;
2761 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2762 if (!suite_seq2)
2763 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002765 asdl_seq_SET(orelse, 0,
2766 If(expression, suite_seq, suite_seq2,
2767 LINENO(CHILD(n, NCH(n) - 6)),
2768 CHILD(n, NCH(n) - 6)->n_col_offset,
2769 c->c_arena));
2770 /* the just-created orelse handled the last elif */
2771 n_elif--;
2772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002774 for (i = 0; i < n_elif; i++) {
2775 int off = 5 + (n_elif - i - 1) * 4;
2776 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2777 if (!newobj)
2778 return NULL;
2779 expression = ast_for_expr(c, CHILD(n, off));
2780 if (!expression)
2781 return NULL;
2782 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2783 if (!suite_seq)
2784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002786 asdl_seq_SET(newobj, 0,
2787 If(expression, suite_seq, orelse,
2788 LINENO(CHILD(n, off)),
2789 CHILD(n, off)->n_col_offset, c->c_arena));
2790 orelse = newobj;
2791 }
2792 expression = ast_for_expr(c, CHILD(n, 1));
2793 if (!expression)
2794 return NULL;
2795 suite_seq = ast_for_suite(c, CHILD(n, 3));
2796 if (!suite_seq)
2797 return NULL;
2798 return If(expression, suite_seq, orelse,
2799 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002801
2802 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002803 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002804 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805}
2806
2807static stmt_ty
2808ast_for_while_stmt(struct compiling *c, const node *n)
2809{
2810 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2811 REQ(n, while_stmt);
2812
2813 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002814 expr_ty expression;
2815 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002817 expression = ast_for_expr(c, CHILD(n, 1));
2818 if (!expression)
2819 return NULL;
2820 suite_seq = ast_for_suite(c, CHILD(n, 3));
2821 if (!suite_seq)
2822 return NULL;
2823 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2824 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 }
2826 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002827 expr_ty expression;
2828 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002830 expression = ast_for_expr(c, CHILD(n, 1));
2831 if (!expression)
2832 return NULL;
2833 seq1 = ast_for_suite(c, CHILD(n, 3));
2834 if (!seq1)
2835 return NULL;
2836 seq2 = ast_for_suite(c, CHILD(n, 6));
2837 if (!seq2)
2838 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002840 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2841 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002843
2844 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002845 "wrong number of tokens for 'while' statement: %d",
2846 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002847 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848}
2849
2850static stmt_ty
2851ast_for_for_stmt(struct compiling *c, const node *n)
2852{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002853 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 expr_ty expression;
2855 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002856 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2858 REQ(n, for_stmt);
2859
2860 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002861 seq = ast_for_suite(c, CHILD(n, 8));
2862 if (!seq)
2863 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 }
2865
Neal Norwitzedef2be2006-07-12 05:26:17 +00002866 node_target = CHILD(n, 1);
2867 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002868 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002869 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002870 /* Check the # of children rather than the length of _target, since
2871 for x, in ... has 1 element in _target, but still requires a Tuple. */
2872 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002873 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002875 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002877 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002878 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002879 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002881 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002882 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002884 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002885 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886}
2887
2888static excepthandler_ty
2889ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2890{
Collin Winter62903052007-05-18 23:11:24 +00002891 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 REQ(exc, except_clause);
2893 REQ(body, suite);
2894
2895 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002896 asdl_seq *suite_seq = ast_for_suite(c, body);
2897 if (!suite_seq)
2898 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899
Georg Brandla48f3ab2008-03-30 06:40:17 +00002900 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002901 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902 }
2903 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002904 expr_ty expression;
2905 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002907 expression = ast_for_expr(c, CHILD(exc, 1));
2908 if (!expression)
2909 return NULL;
2910 suite_seq = ast_for_suite(c, body);
2911 if (!suite_seq)
2912 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913
Georg Brandla48f3ab2008-03-30 06:40:17 +00002914 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002915 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 }
2917 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002918 asdl_seq *suite_seq;
2919 expr_ty expression;
2920 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2921 if (!e)
2922 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002923 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002924 return NULL;
2925 expression = ast_for_expr(c, CHILD(exc, 1));
2926 if (!expression)
2927 return NULL;
2928 suite_seq = ast_for_suite(c, body);
2929 if (!suite_seq)
2930 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931
Georg Brandla48f3ab2008-03-30 06:40:17 +00002932 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002933 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002935
2936 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002937 "wrong number of children for 'except' clause: %d",
2938 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940}
2941
2942static stmt_ty
2943ast_for_try_stmt(struct compiling *c, const node *n)
2944{
Neal Norwitzf599f422005-12-17 21:33:47 +00002945 const int nch = NCH(n);
2946 int n_except = (nch - 3)/3;
2947 asdl_seq *body, *orelse = NULL, *finally = NULL;
2948
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 REQ(n, try_stmt);
2950
Neal Norwitzf599f422005-12-17 21:33:47 +00002951 body = ast_for_suite(c, CHILD(n, 2));
2952 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954
Neal Norwitzf599f422005-12-17 21:33:47 +00002955 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002956 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2957 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2958 /* we can assume it's an "else",
2959 because nch >= 9 for try-else-finally and
2960 it would otherwise have a type of except_clause */
2961 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2962 if (orelse == NULL)
2963 return NULL;
2964 n_except--;
2965 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002967 finally = ast_for_suite(c, CHILD(n, nch - 1));
2968 if (finally == NULL)
2969 return NULL;
2970 n_except--;
2971 }
2972 else {
2973 /* we can assume it's an "else",
2974 otherwise it would have a type of except_clause */
2975 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2976 if (orelse == NULL)
2977 return NULL;
2978 n_except--;
2979 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002981 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002982 ast_error(n, "malformed 'try' statement");
2983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002985
2986 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002987 int i;
2988 stmt_ty except_st;
2989 /* process except statements to create a try ... except */
2990 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2991 if (handlers == NULL)
2992 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002993
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002994 for (i = 0; i < n_except; i++) {
2995 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2996 CHILD(n, 5 + i * 3));
2997 if (!e)
2998 return NULL;
2999 asdl_seq_SET(handlers, i, e);
3000 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003001
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003002 except_st = TryExcept(body, handlers, orelse, LINENO(n),
3003 n->n_col_offset, c->c_arena);
3004 if (!finally)
3005 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00003006
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003007 /* if a 'finally' is present too, we nest the TryExcept within a
3008 TryFinally to emulate try ... except ... finally */
3009 body = asdl_seq_new(1, c->c_arena);
3010 if (body == NULL)
3011 return NULL;
3012 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00003013 }
3014
3015 /* must be a try ... finally (except clauses are in body, if any exist) */
3016 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003017 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018}
3019
Guido van Rossumc2e20742006-02-27 22:32:47 +00003020static expr_ty
3021ast_for_with_var(struct compiling *c, const node *n)
3022{
3023 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003024 return ast_for_expr(c, CHILD(n, 1));
3025}
3026
3027/* with_stmt: 'with' test [ with_var ] ':' suite */
3028static stmt_ty
3029ast_for_with_stmt(struct compiling *c, const node *n)
3030{
3031 expr_ty context_expr, optional_vars = NULL;
3032 int suite_index = 3; /* skip 'with', test, and ':' */
3033 asdl_seq *suite_seq;
3034
3035 assert(TYPE(n) == with_stmt);
3036 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00003037 if (!context_expr)
3038 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003039 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003040 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003041
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003042 if (!optional_vars) {
3043 return NULL;
3044 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003045 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003046 return NULL;
3047 }
3048 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003049 }
3050
3051 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
3052 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003053 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003054 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003055 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003056 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003057}
3058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003060ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061{
3062 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson5cb67632008-11-25 04:00:37 +00003063 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 asdl_seq *bases, *s;
3065
3066 REQ(n, classdef);
3067
Benjamin Petersond5efd202008-06-08 22:52:37 +00003068 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003069 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070
3071 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003072 s = ast_for_suite(c, CHILD(n, 3));
3073 if (!s)
3074 return NULL;
Benjamin Peterson5cb67632008-11-25 04:00:37 +00003075 classname = NEW_IDENTIFIER(CHILD(n, 1));
3076 if (!classname)
3077 return NULL;
3078 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3079 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 }
3081 /* check for empty base list */
3082 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003083 s = ast_for_suite(c, CHILD(n,5));
3084 if (!s)
Benjamin Peterson5cb67632008-11-25 04:00:37 +00003085 return NULL;
3086 classname = NEW_IDENTIFIER(CHILD(n, 1));
3087 if (!classname)
3088 return NULL;
3089 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3090 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 }
3092
3093 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003094 bases = ast_for_class_bases(c, CHILD(n, 3));
3095 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003096 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097
3098 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003099 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003100 return NULL;
Benjamin Peterson5cb67632008-11-25 04:00:37 +00003101 classname = NEW_IDENTIFIER(CHILD(n, 1));
3102 if (!classname)
3103 return NULL;
3104 return ClassDef(classname, bases, s, decorator_seq,
Christian Heimes5224d282008-02-23 15:01:05 +00003105 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106}
3107
3108static stmt_ty
3109ast_for_stmt(struct compiling *c, const node *n)
3110{
3111 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003112 assert(NCH(n) == 1);
3113 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114 }
3115 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003116 assert(num_stmts(n) == 1);
3117 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 }
3119 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003120 REQ(n, small_stmt);
3121 n = CHILD(n, 0);
3122 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3123 | flow_stmt | import_stmt | global_stmt | exec_stmt
3124 | assert_stmt
3125 */
3126 switch (TYPE(n)) {
3127 case expr_stmt:
3128 return ast_for_expr_stmt(c, n);
3129 case print_stmt:
3130 return ast_for_print_stmt(c, n);
3131 case del_stmt:
3132 return ast_for_del_stmt(c, n);
3133 case pass_stmt:
3134 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3135 case flow_stmt:
3136 return ast_for_flow_stmt(c, n);
3137 case import_stmt:
3138 return ast_for_import_stmt(c, n);
3139 case global_stmt:
3140 return ast_for_global_stmt(c, n);
3141 case exec_stmt:
3142 return ast_for_exec_stmt(c, n);
3143 case assert_stmt:
3144 return ast_for_assert_stmt(c, n);
3145 default:
3146 PyErr_Format(PyExc_SystemError,
3147 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3148 TYPE(n), NCH(n));
3149 return NULL;
3150 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 }
3152 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003153 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003154 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003155 */
3156 node *ch = CHILD(n, 0);
3157 REQ(n, compound_stmt);
3158 switch (TYPE(ch)) {
3159 case if_stmt:
3160 return ast_for_if_stmt(c, ch);
3161 case while_stmt:
3162 return ast_for_while_stmt(c, ch);
3163 case for_stmt:
3164 return ast_for_for_stmt(c, ch);
3165 case try_stmt:
3166 return ast_for_try_stmt(c, ch);
3167 case with_stmt:
3168 return ast_for_with_stmt(c, ch);
3169 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003170 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003171 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003172 return ast_for_classdef(c, ch, NULL);
3173 case decorated:
3174 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003175 default:
3176 PyErr_Format(PyExc_SystemError,
3177 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3178 TYPE(n), NCH(n));
3179 return NULL;
3180 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 }
3182}
3183
3184static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003185parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003187 const char *end;
3188 long x;
3189 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003191 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003192 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193#endif
3194
Mark Dickinson514624d2008-12-05 18:01:46 +00003195 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003196 errno = 0;
3197 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003199 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003201 if (*end == 'l' || *end == 'L')
3202 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003203 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003204 if (*end == '\0') {
3205 if (errno != 0)
3206 return PyLong_FromString((char *)s, (char **)0, 0);
3207 return PyInt_FromLong(x);
3208 }
3209 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003211 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003212 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003213 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003214 complex.imag = PyOS_ascii_atof(s);
3215 PyFPE_END_PROTECT(complex)
3216 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003217 }
3218 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003220 {
3221 PyFPE_START_PROTECT("atof", return 0)
3222 dx = PyOS_ascii_atof(s);
3223 PyFPE_END_PROTECT(dx)
3224 return PyFloat_FromDouble(dx);
3225 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226}
3227
3228static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003229decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230{
3231#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003232 Py_FatalError("decode_utf8 should not be called in this build.");
3233 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003235 PyObject *u, *v;
3236 char *s, *t;
3237 t = s = (char *)*sPtr;
3238 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3239 while (s < end && (*s & 0x80)) s++;
3240 *sPtr = s;
3241 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3242 if (u == NULL)
3243 return NULL;
3244 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3245 Py_DECREF(u);
3246 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247#endif
3248}
3249
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003250#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003252decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003254 PyObject *v, *u;
3255 char *buf;
3256 char *p;
3257 const char *end;
3258 if (encoding == NULL) {
3259 buf = (char *)s;
3260 u = NULL;
3261 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3262 buf = (char *)s;
3263 u = NULL;
3264 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003265 /* check for integer overflow */
Benjamin Petersonc717aec2009-10-29 02:02:47 +00003266 if (len > PY_SIZE_MAX / 6)
Gregory P. Smith9d534572008-06-11 07:41:16 +00003267 return NULL;
Benjamin Petersonc717aec2009-10-29 02:02:47 +00003268 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3269 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3270 u = PyString_FromStringAndSize((char *)NULL, len * 6);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003271 if (u == NULL)
3272 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003273 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003274 end = s + len;
3275 while (s < end) {
3276 if (*s == '\\') {
3277 *p++ = *s++;
3278 if (*s & 0x80) {
3279 strcpy(p, "u005c");
3280 p += 5;
3281 }
3282 }
3283 if (*s & 0x80) { /* XXX inefficient */
3284 PyObject *w;
3285 char *r;
3286 Py_ssize_t rn, i;
Benjamin Petersonc717aec2009-10-29 02:02:47 +00003287 w = decode_utf8(c, &s, end, "utf-32-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003288 if (w == NULL) {
3289 Py_DECREF(u);
3290 return NULL;
3291 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003292 r = PyString_AsString(w);
3293 rn = PyString_Size(w);
Benjamin Petersonc717aec2009-10-29 02:02:47 +00003294 assert(rn % 4 == 0);
3295 for (i = 0; i < rn; i += 4) {
3296 sprintf(p, "\\U%02x%02x%02x%02x",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003297 r[i + 0] & 0xFF,
Benjamin Petersonc717aec2009-10-29 02:02:47 +00003298 r[i + 1] & 0xFF,
3299 r[i + 2] & 0xFF,
3300 r[i + 3] & 0xFF);
3301 p += 10;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003302 }
3303 Py_DECREF(w);
3304 } else {
3305 *p++ = *s++;
3306 }
3307 }
3308 len = p - buf;
3309 s = buf;
3310 }
3311 if (rawmode)
3312 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3313 else
3314 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3315 Py_XDECREF(u);
3316 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003318#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319
3320/* s is a Python string literal, including the bracketing quote characters,
3321 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3322 * parsestr parses it, and returns the decoded Python string object.
3323 */
3324static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003325parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003327 size_t len;
3328 int quote = Py_CHARMASK(*s);
3329 int rawmode = 0;
3330 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003331 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003333 if (isalpha(quote) || quote == '_') {
3334 if (quote == 'u' || quote == 'U') {
3335 quote = *++s;
3336 unicode = 1;
3337 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003338 if (quote == 'b' || quote == 'B') {
3339 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003340 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003341 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003342 if (quote == 'r' || quote == 'R') {
3343 quote = *++s;
3344 rawmode = 1;
3345 }
3346 }
3347 if (quote != '\'' && quote != '\"') {
3348 PyErr_BadInternalCall();
3349 return NULL;
3350 }
3351 s++;
3352 len = strlen(s);
3353 if (len > INT_MAX) {
3354 PyErr_SetString(PyExc_OverflowError,
3355 "string to parse is too long");
3356 return NULL;
3357 }
3358 if (s[--len] != quote) {
3359 PyErr_BadInternalCall();
3360 return NULL;
3361 }
3362 if (len >= 4 && s[0] == quote && s[1] == quote) {
3363 s += 2;
3364 len -= 2;
3365 if (s[--len] != quote || s[--len] != quote) {
3366 PyErr_BadInternalCall();
3367 return NULL;
3368 }
3369 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003371 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003372 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003375 need_encoding = (c->c_encoding != NULL &&
3376 strcmp(c->c_encoding, "utf-8") != 0 &&
3377 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003378 if (rawmode || strchr(s, '\\') == NULL) {
3379 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003381 /* This should not happen - we never see any other
3382 encoding. */
3383 Py_FatalError(
3384 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003386 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3387 if (u == NULL)
3388 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003389 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003390 Py_DECREF(u);
3391 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003392#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003393 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003394 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003395 }
3396 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003398 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003399 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400}
3401
3402/* Build a Python string object out of a STRING atom. This takes care of
3403 * compile-time literal catenation, calling parsestr() on each piece, and
3404 * pasting the intermediate results together.
3405 */
3406static PyObject *
3407parsestrplus(struct compiling *c, const node *n)
3408{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003409 PyObject *v;
3410 int i;
3411 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003412 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003413 /* String literal concatenation */
3414 for (i = 1; i < NCH(n); i++) {
3415 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003416 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003417 if (s == NULL)
3418 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003419 if (PyString_Check(v) && PyString_Check(s)) {
3420 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003421 if (v == NULL)
3422 goto onError;
3423 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003424#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003425 else {
3426 PyObject *temp = PyUnicode_Concat(v, s);
3427 Py_DECREF(s);
3428 Py_DECREF(v);
3429 v = temp;
3430 if (v == NULL)
3431 goto onError;
3432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003434 }
3435 }
3436 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437
3438 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003439 Py_XDECREF(v);
3440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441}