blob: c76014af8e74574172b764f73387a48ea7fa00b5 [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 *);
Alexandre Vassalottib6465472010-01-11 22:36:12 +000034static expr_ty ast_for_testlist_comp(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
Alexandre Vassalottib6465472010-01-11 22:36:12 +000047#define COMP_GENEXP 0
48#define COMP_SETCOMP 1
49
Neal Norwitzadb69fc2005-12-17 20:54:49 +000050static identifier
51new_identifier(const char* n, PyArena *arena) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000052 PyObject* id = PyString_InternFromString(n);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +000053 if (id != NULL)
54 PyArena_AddPyObject(arena, id);
Neal Norwitzadb69fc2005-12-17 20:54:49 +000055 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056}
57
Neal Norwitzadb69fc2005-12-17 20:54:49 +000058#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000059
60/* This routine provides an invalid object for the syntax error.
61 The outermost routine must unpack this error and create the
62 proper object. We do this so that we don't have to pass
63 the filename to everything function.
64
65 XXX Maybe we should just pass the filename...
66*/
67
68static int
69ast_error(const node *n, const char *errstr)
70{
71 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
72 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000073 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074 PyErr_SetObject(PyExc_SyntaxError, u);
75 Py_DECREF(u);
76 return 0;
77}
78
79static void
80ast_error_finish(const char *filename)
81{
82 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000083 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084
85 assert(PyErr_Occurred());
86 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000087 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088
89 PyErr_Fetch(&type, &value, &tback);
90 errstr = PyTuple_GetItem(value, 0);
91 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000092 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000093 Py_INCREF(errstr);
94 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000095 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000096 Py_DECREF(errstr);
97 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000098 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 Py_DECREF(value);
100
101 loc = PyErr_ProgramText(filename, lineno);
102 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000103 Py_INCREF(Py_None);
104 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000106 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000108 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000109 Py_DECREF(errstr);
110 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000111 }
Georg Brandl7784f122006-05-26 20:04:44 +0000112 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113 Py_DECREF(errstr);
114 Py_DECREF(tmp);
115 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000116 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000117 PyErr_Restore(type, value, tback);
118}
119
Benjamin Petersoncbd78132008-06-08 15:45:23 +0000120static int
121ast_warn(struct compiling *c, const node *n, char *msg)
122{
123 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
124 NULL, NULL) < 0) {
125 /* if -Werr, change it to a SyntaxError */
126 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
127 ast_error(n, msg);
128 return 0;
129 }
130 return 1;
131}
132
Benjamin Petersond5efd202008-06-08 22:52:37 +0000133static int
134forbidden_check(struct compiling *c, const node *n, const char *x)
135{
136 if (!strcmp(x, "None"))
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000137 return ast_error(n, "cannot assign to None");
138 if (!strcmp(x, "__debug__"))
139 return ast_error(n, "cannot assign to __debug__");
Benjamin Peterson399b1fe2008-10-25 02:53:28 +0000140 if (Py_Py3kWarningFlag) {
141 if (!(strcmp(x, "True") && strcmp(x, "False")) &&
142 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
143 return 0;
144 if (!strcmp(x, "nonlocal") &&
145 !ast_warn(c, n, "nonlocal is a keyword in 3.x"))
146 return 0;
147 }
Benjamin Petersond5efd202008-06-08 22:52:37 +0000148 return 1;
149}
150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151/* num_stmts() returns number of contained statements.
152
153 Use this routine to determine how big a sequence is needed for
154 the statements in a parse tree. Its raison d'etre is this bit of
155 grammar:
156
157 stmt: simple_stmt | compound_stmt
158 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
159
160 A simple_stmt can contain multiple small_stmt elements joined
161 by semicolons. If the arg is a simple_stmt, the number of
162 small_stmt elements is returned.
163*/
164
165static int
166num_stmts(const node *n)
167{
168 int i, l;
169 node *ch;
170
171 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000172 case single_input:
173 if (TYPE(CHILD(n, 0)) == NEWLINE)
174 return 0;
175 else
176 return num_stmts(CHILD(n, 0));
177 case file_input:
178 l = 0;
179 for (i = 0; i < NCH(n); i++) {
180 ch = CHILD(n, i);
181 if (TYPE(ch) == stmt)
182 l += num_stmts(ch);
183 }
184 return l;
185 case stmt:
186 return num_stmts(CHILD(n, 0));
187 case compound_stmt:
188 return 1;
189 case simple_stmt:
190 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
191 case suite:
192 if (NCH(n) == 1)
193 return num_stmts(CHILD(n, 0));
194 else {
195 l = 0;
196 for (i = 2; i < (NCH(n) - 1); i++)
197 l += num_stmts(CHILD(n, i));
198 return l;
199 }
200 default: {
201 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +0000203 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000204 TYPE(n), NCH(n));
205 Py_FatalError(buf);
206 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 }
208 assert(0);
209 return 0;
210}
211
212/* Transform the CST rooted at node * to the appropriate AST
213*/
214
215mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000216PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000217 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000219 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 asdl_seq *stmts = NULL;
221 stmt_ty s;
222 node *ch;
223 struct compiling c;
224
225 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000226 c.c_encoding = "utf-8";
227 if (TYPE(n) == encoding_decl) {
228 ast_error(n, "encoding declaration in Unicode string");
229 goto error;
230 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000232 c.c_encoding = STR(n);
233 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000235 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 }
Christian Heimes3c608332008-03-26 22:01:37 +0000237 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000238 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000239 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240
Jeremy Hyltona8293132006-02-28 17:58:27 +0000241 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000243 case file_input:
244 stmts = asdl_seq_new(num_stmts(n), arena);
245 if (!stmts)
246 return NULL;
247 for (i = 0; i < NCH(n) - 1; i++) {
248 ch = CHILD(n, i);
249 if (TYPE(ch) == NEWLINE)
250 continue;
251 REQ(ch, stmt);
252 num = num_stmts(ch);
253 if (num == 1) {
254 s = ast_for_stmt(&c, ch);
255 if (!s)
256 goto error;
257 asdl_seq_SET(stmts, k++, s);
258 }
259 else {
260 ch = CHILD(ch, 0);
261 REQ(ch, simple_stmt);
262 for (j = 0; j < num; j++) {
263 s = ast_for_stmt(&c, CHILD(ch, j * 2));
264 if (!s)
265 goto error;
266 asdl_seq_SET(stmts, k++, s);
267 }
268 }
269 }
270 return Module(stmts, arena);
271 case eval_input: {
272 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000274 /* XXX Why not comp_for here? */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000275 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
276 if (!testlist_ast)
277 goto error;
278 return Expression(testlist_ast, arena);
279 }
280 case single_input:
281 if (TYPE(CHILD(n, 0)) == NEWLINE) {
282 stmts = asdl_seq_new(1, arena);
283 if (!stmts)
284 goto error;
285 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
286 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000287 if (!asdl_seq_GET(stmts, 0))
288 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000289 return Interactive(stmts, arena);
290 }
291 else {
292 n = CHILD(n, 0);
293 num = num_stmts(n);
294 stmts = asdl_seq_new(num, arena);
295 if (!stmts)
296 goto error;
297 if (num == 1) {
298 s = ast_for_stmt(&c, n);
299 if (!s)
300 goto error;
301 asdl_seq_SET(stmts, 0, s);
302 }
303 else {
304 /* Only a simple_stmt can contain multiple statements. */
305 REQ(n, simple_stmt);
306 for (i = 0; i < NCH(n); i += 2) {
307 if (TYPE(CHILD(n, i)) == NEWLINE)
308 break;
309 s = ast_for_stmt(&c, CHILD(n, i));
310 if (!s)
311 goto error;
312 asdl_seq_SET(stmts, i / 2, s);
313 }
314 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000316 return Interactive(stmts, arena);
317 }
318 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000319 PyErr_Format(PyExc_SystemError,
320 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000321 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 }
323 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 ast_error_finish(filename);
325 return NULL;
326}
327
328/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
329*/
330
331static operator_ty
332get_operator(const node *n)
333{
334 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000335 case VBAR:
336 return BitOr;
337 case CIRCUMFLEX:
338 return BitXor;
339 case AMPER:
340 return BitAnd;
341 case LEFTSHIFT:
342 return LShift;
343 case RIGHTSHIFT:
344 return RShift;
345 case PLUS:
346 return Add;
347 case MINUS:
348 return Sub;
349 case STAR:
350 return Mult;
351 case SLASH:
352 return Div;
353 case DOUBLESLASH:
354 return FloorDiv;
355 case PERCENT:
356 return Mod;
357 default:
358 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359 }
360}
361
Jeremy Hyltona8293132006-02-28 17:58:27 +0000362/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363
364 Only sets context for expr kinds that "can appear in assignment context"
365 (according to ../Parser/Python.asdl). For other expr kinds, it sets
366 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367*/
368
369static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000370set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371{
372 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000373 /* If a particular expression type can't be used for assign / delete,
374 set expr_name to its name and an error message will be generated.
375 */
376 const char* expr_name = NULL;
377
378 /* The ast defines augmented store and load contexts, but the
379 implementation here doesn't actually use them. The code may be
380 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000381 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000383 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 */
385 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386
387 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000388 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000389 if (ctx == Store && !forbidden_check(c, n,
390 PyBytes_AS_STRING(e->v.Attribute.attr)))
391 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000392 e->v.Attribute.ctx = ctx;
393 break;
394 case Subscript_kind:
395 e->v.Subscript.ctx = ctx;
396 break;
397 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000398 if (ctx == Store && !forbidden_check(c, n,
399 PyBytes_AS_STRING(e->v.Name.id)))
400 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000401 e->v.Name.ctx = ctx;
402 break;
403 case List_kind:
404 e->v.List.ctx = ctx;
405 s = e->v.List.elts;
406 break;
407 case Tuple_kind:
Benjamin Peterson52c4bec2009-06-13 17:08:53 +0000408 if (asdl_seq_LEN(e->v.Tuple.elts)) {
409 e->v.Tuple.ctx = ctx;
410 s = e->v.Tuple.elts;
411 }
412 else {
413 expr_name = "()";
414 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000415 break;
416 case Lambda_kind:
417 expr_name = "lambda";
418 break;
419 case Call_kind:
420 expr_name = "function call";
421 break;
422 case BoolOp_kind:
423 case BinOp_kind:
424 case UnaryOp_kind:
425 expr_name = "operator";
426 break;
427 case GeneratorExp_kind:
428 expr_name = "generator expression";
429 break;
430 case Yield_kind:
431 expr_name = "yield expression";
432 break;
433 case ListComp_kind:
434 expr_name = "list comprehension";
435 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000436 case SetComp_kind:
437 expr_name = "set comprehension";
438 break;
439 case DictComp_kind:
440 expr_name = "dict comprehension";
441 break;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000442 case Dict_kind:
443 case Num_kind:
444 case Str_kind:
445 expr_name = "literal";
446 break;
447 case Compare_kind:
448 expr_name = "comparison";
449 break;
450 case Repr_kind:
451 expr_name = "repr";
452 break;
453 case IfExp_kind:
454 expr_name = "conditional expression";
455 break;
456 default:
457 PyErr_Format(PyExc_SystemError,
458 "unexpected expression in assignment %d (line %d)",
459 e->kind, e->lineno);
460 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000462 /* Check for error string set by switch */
463 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000464 char buf[300];
465 PyOS_snprintf(buf, sizeof(buf),
466 "can't %s %s",
467 ctx == Store ? "assign to" : "delete",
468 expr_name);
469 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000470 }
471
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000473 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474 */
475 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000476 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000478 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000479 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000480 return 0;
481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 }
483 return 1;
484}
485
486static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000487ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488{
489 REQ(n, augassign);
490 n = CHILD(n, 0);
491 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000492 case '+':
493 return Add;
494 case '-':
495 return Sub;
496 case '/':
497 if (STR(n)[1] == '/')
498 return FloorDiv;
499 else
500 return Div;
501 case '%':
502 return Mod;
503 case '<':
504 return LShift;
505 case '>':
506 return RShift;
507 case '&':
508 return BitAnd;
509 case '^':
510 return BitXor;
511 case '|':
512 return BitOr;
513 case '*':
514 if (STR(n)[1] == '*')
515 return Pow;
516 else
517 return Mult;
518 default:
519 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
520 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 }
522}
523
524static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000525ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526{
527 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000528 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 */
530 REQ(n, comp_op);
531 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000532 n = CHILD(n, 0);
533 switch (TYPE(n)) {
534 case LESS:
535 return Lt;
536 case GREATER:
537 return Gt;
538 case EQEQUAL: /* == */
539 return Eq;
540 case LESSEQUAL:
541 return LtE;
542 case GREATEREQUAL:
543 return GtE;
544 case NOTEQUAL:
545 return NotEq;
546 case NAME:
547 if (strcmp(STR(n), "in") == 0)
548 return In;
549 if (strcmp(STR(n), "is") == 0)
550 return Is;
551 default:
552 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
553 STR(n));
554 return (cmpop_ty)0;
555 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 }
557 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000558 /* handle "not in" and "is not" */
559 switch (TYPE(CHILD(n, 0))) {
560 case NAME:
561 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
562 return NotIn;
563 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
564 return IsNot;
565 default:
566 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
567 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
568 return (cmpop_ty)0;
569 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 }
Neal Norwitz79792652005-11-14 04:25:03 +0000571 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000572 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000573 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574}
575
576static asdl_seq *
577seq_for_testlist(struct compiling *c, const node *n)
578{
579 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000580 asdl_seq *seq;
581 expr_ty expression;
582 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000583 assert(TYPE(n) == testlist ||
584 TYPE(n) == listmaker ||
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000585 TYPE(n) == testlist_comp ||
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000586 TYPE(n) == testlist_safe ||
587 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000589 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000591 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592
593 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000594 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000596 expression = ast_for_expr(c, CHILD(n, i));
597 if (!expression)
598 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000600 assert(i / 2 < seq->size);
601 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 }
603 return seq;
604}
605
606static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000607compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608{
609 int i, len = (NCH(n) + 1) / 2;
610 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000611 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000613 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614
Neal Norwitz3a230172006-09-22 08:18:10 +0000615 /* fpdef: NAME | '(' fplist ')'
616 fplist: fpdef (',' fpdef)* [',']
617 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 for (i = 0; i < len; i++) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000620 PyObject *arg_id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000621 const node *fpdef_node = CHILD(n, 2*i);
622 const node *child;
623 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000624set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000625 /* fpdef_node is either a NAME or an fplist */
626 child = CHILD(fpdef_node, 0);
627 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000628 if (!forbidden_check(c, n, STR(child)))
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000629 return NULL;
630 arg_id = NEW_IDENTIFIER(child);
631 if (!arg_id)
632 return NULL;
633 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
634 c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000635 }
636 else {
637 assert(TYPE(fpdef_node) == fpdef);
638 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
639 child = CHILD(fpdef_node, 1);
640 assert(TYPE(child) == fplist);
641 /* NCH == 1 means we have (x), we need to elide the extra parens */
642 if (NCH(child) == 1) {
643 fpdef_node = CHILD(child, 0);
644 assert(TYPE(fpdef_node) == fpdef);
645 goto set_name;
646 }
647 arg = compiler_complex_args(c, child);
648 }
649 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 }
651
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000652 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000653 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000654 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 return result;
656}
657
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Jeremy Hyltona8293132006-02-28 17:58:27 +0000659/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
661static arguments_ty
662ast_for_arguments(struct compiling *c, const node *n)
663{
664 /* parameters: '(' [varargslist] ')'
665 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000666 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000668 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 asdl_seq *args, *defaults;
670 identifier vararg = NULL, kwarg = NULL;
671 node *ch;
672
673 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000674 if (NCH(n) == 2) /* () as argument list */
675 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
676 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 }
678 REQ(n, varargslist);
679
680 /* first count the number of normal args & defaults */
681 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000682 ch = CHILD(n, i);
683 if (TYPE(ch) == fpdef)
684 n_args++;
685 if (TYPE(ch) == EQUAL)
686 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000688 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000690 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000691 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000693 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694
695 /* fpdef: NAME | '(' fplist ')'
696 fplist: fpdef (',' fpdef)* [',']
697 */
698 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000699 j = 0; /* index for defaults */
700 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000702 ch = CHILD(n, i);
703 switch (TYPE(ch)) {
Benjamin Peterson99a50232009-11-19 22:54:57 +0000704 case fpdef: {
705 int complex_args = 0, parenthesized = 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000706 handle_fpdef:
707 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
708 anything other than EQUAL or a comma? */
709 /* XXX Should NCH(n) check be made a separate check? */
710 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
711 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
712 if (!expression)
713 goto error;
714 assert(defaults != NULL);
715 asdl_seq_SET(defaults, j++, expression);
716 i += 2;
717 found_default = 1;
718 }
719 else if (found_default) {
Benjamin Peterson99a50232009-11-19 22:54:57 +0000720 /* def f((x)=4): pass should raise an error.
721 def f((x, (y))): pass will just incur the tuple unpacking warning. */
722 if (parenthesized && !complex_args) {
723 ast_error(n, "parenthesized arg with default");
724 goto error;
725 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000726 ast_error(n,
727 "non-default argument follows default argument");
728 goto error;
729 }
730 if (NCH(ch) == 3) {
731 ch = CHILD(ch, 1);
732 /* def foo((x)): is not complex, special case. */
733 if (NCH(ch) != 1) {
734 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000735 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
736 "tuple parameter unpacking has been removed in 3.x"))
737 goto error;
Benjamin Peterson99a50232009-11-19 22:54:57 +0000738 complex_args = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000739 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000740 if (!asdl_seq_GET(args, k-1))
741 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000742 } else {
743 /* def foo((x)): setup for checking NAME below. */
744 /* Loop because there can be many parens and tuple
745 unpacking mixed in. */
Benjamin Peterson99a50232009-11-19 22:54:57 +0000746 parenthesized = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000747 ch = CHILD(ch, 0);
748 assert(TYPE(ch) == fpdef);
749 goto handle_fpdef;
750 }
751 }
752 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000753 PyObject *id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000754 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000755 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000756 goto error;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000757 id = NEW_IDENTIFIER(CHILD(ch, 0));
758 if (!id)
759 goto error;
760 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000761 c->c_arena);
762 if (!name)
763 goto error;
764 asdl_seq_SET(args, k++, name);
765
766 }
767 i += 2; /* the name and the comma */
Benjamin Peterson99a50232009-11-19 22:54:57 +0000768 if (parenthesized && Py_Py3kWarningFlag &&
769 !ast_warn(c, ch, "parenthesized argument names "
770 "are invalid in 3.x"))
771 goto error;
772
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000773 break;
Benjamin Peterson99a50232009-11-19 22:54:57 +0000774 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000775 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000776 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000777 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000778 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000779 if (!vararg)
780 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000781 i += 3;
782 break;
783 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000784 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000785 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000786 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000787 if (!kwarg)
788 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000789 i += 3;
790 break;
791 default:
792 PyErr_Format(PyExc_SystemError,
793 "unexpected node in varargslist: %d @ %d",
794 TYPE(ch), i);
795 goto error;
796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 }
798
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000799 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800
801 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000802 Py_XDECREF(vararg);
803 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 return NULL;
805}
806
807static expr_ty
808ast_for_dotted_name(struct compiling *c, const node *n)
809{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000810 expr_ty e;
811 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000812 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 int i;
814
815 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000816
817 lineno = LINENO(n);
818 col_offset = n->n_col_offset;
819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 id = NEW_IDENTIFIER(CHILD(n, 0));
821 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000822 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000823 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826
827 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000828 id = NEW_IDENTIFIER(CHILD(n, i));
829 if (!id)
830 return NULL;
831 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
832 if (!e)
833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 }
835
836 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837}
838
839static expr_ty
840ast_for_decorator(struct compiling *c, const node *n)
841{
842 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
843 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000844 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845
846 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000847 REQ(CHILD(n, 0), AT);
848 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849
850 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
851 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000852 return NULL;
853
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000855 d = name_expr;
856 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 }
858 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000859 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
860 n->n_col_offset, c->c_arena);
861 if (!d)
862 return NULL;
863 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
865 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000866 d = ast_for_call(c, CHILD(n, 3), name_expr);
867 if (!d)
868 return NULL;
869 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 }
871
872 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873}
874
875static asdl_seq*
876ast_for_decorators(struct compiling *c, const node *n)
877{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000878 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000879 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 int i;
881
882 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000883 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000885 return NULL;
886
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000888 d = ast_for_decorator(c, CHILD(n, i));
889 if (!d)
890 return NULL;
891 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 }
893 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894}
895
896static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000897ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898{
Christian Heimes5224d282008-02-23 15:01:05 +0000899 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000900 identifier name;
901 arguments_ty args;
902 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000903 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904
905 REQ(n, funcdef);
906
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907 name = NEW_IDENTIFIER(CHILD(n, name_i));
908 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000909 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000910 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 args = ast_for_arguments(c, CHILD(n, name_i + 1));
913 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000914 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 body = ast_for_suite(c, CHILD(n, name_i + 3));
916 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000917 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000919 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000920 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921}
922
Christian Heimes5224d282008-02-23 15:01:05 +0000923static stmt_ty
924ast_for_decorated(struct compiling *c, const node *n)
925{
926 /* decorated: decorators (classdef | funcdef) */
927 stmt_ty thing = NULL;
928 asdl_seq *decorator_seq = NULL;
929
930 REQ(n, decorated);
931
932 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
933 if (!decorator_seq)
934 return NULL;
935
936 assert(TYPE(CHILD(n, 1)) == funcdef ||
937 TYPE(CHILD(n, 1)) == classdef);
938
939 if (TYPE(CHILD(n, 1)) == funcdef) {
940 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
941 } else if (TYPE(CHILD(n, 1)) == classdef) {
942 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
943 }
944 /* we count the decorators in when talking about the class' or
945 function's line number */
946 if (thing) {
947 thing->lineno = LINENO(n);
948 thing->col_offset = n->n_col_offset;
949 }
950 return thing;
951}
952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953static expr_ty
954ast_for_lambdef(struct compiling *c, const node *n)
955{
956 /* lambdef: 'lambda' [varargslist] ':' test */
957 arguments_ty args;
958 expr_ty expression;
959
960 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000961 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
962 if (!args)
963 return NULL;
964 expression = ast_for_expr(c, CHILD(n, 2));
965 if (!expression)
966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 }
968 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000969 args = ast_for_arguments(c, CHILD(n, 1));
970 if (!args)
971 return NULL;
972 expression = ast_for_expr(c, CHILD(n, 3));
973 if (!expression)
974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 }
976
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000977 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978}
979
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000980static expr_ty
981ast_for_ifexpr(struct compiling *c, const node *n)
982{
983 /* test: or_test 'if' or_test 'else' test */
984 expr_ty expression, body, orelse;
985
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000986 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000987 body = ast_for_expr(c, CHILD(n, 0));
988 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000989 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000990 expression = ast_for_expr(c, CHILD(n, 2));
991 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000992 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000993 orelse = ast_for_expr(c, CHILD(n, 4));
994 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000995 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000996 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000997 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000998}
999
Neal Norwitze4d4f002006-09-05 03:58:26 +00001000/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
1001 so there is only a single version. Possibly for loops can also re-use
1002 the code.
1003*/
1004
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005/* Count the number of 'for' loop in a list comprehension.
1006
1007 Helper for ast_for_listcomp().
1008*/
1009
1010static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001011count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012{
1013 int n_fors = 0;
1014 node *ch = CHILD(n, 1);
1015
1016 count_list_for:
1017 n_fors++;
1018 REQ(ch, list_for);
1019 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001020 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001022 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 count_list_iter:
1024 REQ(ch, list_iter);
1025 ch = CHILD(ch, 0);
1026 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001027 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001029 if (NCH(ch) == 3) {
1030 ch = CHILD(ch, 2);
1031 goto count_list_iter;
1032 }
1033 else
1034 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001036
1037 /* Should never be reached */
1038 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1039 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040}
1041
1042/* Count the number of 'if' statements in a list comprehension.
1043
1044 Helper for ast_for_listcomp().
1045*/
1046
1047static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001048count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049{
1050 int n_ifs = 0;
1051
1052 count_list_iter:
1053 REQ(n, list_iter);
1054 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001055 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056 n = CHILD(n, 0);
1057 REQ(n, list_if);
1058 n_ifs++;
1059 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001060 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 n = CHILD(n, 2);
1062 goto count_list_iter;
1063}
1064
1065static expr_ty
1066ast_for_listcomp(struct compiling *c, const node *n)
1067{
1068 /* listmaker: test ( list_for | (',' test)* [','] )
1069 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1070 list_iter: list_for | list_if
1071 list_if: 'if' test [list_iter]
1072 testlist_safe: test [(',' test)+ [',']]
1073 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001074 expr_ty elt, first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 asdl_seq *listcomps;
1076 int i, n_fors;
1077 node *ch;
1078
1079 REQ(n, listmaker);
1080 assert(NCH(n) > 1);
1081
1082 elt = ast_for_expr(c, CHILD(n, 0));
1083 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001084 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001086 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001088 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001090 listcomps = asdl_seq_new(n_fors, c->c_arena);
1091 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001092 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 ch = CHILD(n, 1);
1095 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001096 comprehension_ty lc;
1097 asdl_seq *t;
1098 expr_ty expression;
1099 node *for_ch;
1100
1101 REQ(ch, list_for);
1102
1103 for_ch = CHILD(ch, 1);
1104 t = ast_for_exprlist(c, for_ch, Store);
1105 if (!t)
1106 return NULL;
1107 expression = ast_for_testlist(c, CHILD(ch, 3));
1108 if (!expression)
1109 return NULL;
1110
1111 /* Check the # of children rather than the length of t, since
1112 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1113 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001114 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001115 if (NCH(for_ch) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001116 lc = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001117 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001118 lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001119 c->c_arena),
1120 expression, NULL, c->c_arena);
1121 if (!lc)
1122 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001124 if (NCH(ch) == 5) {
1125 int j, n_ifs;
1126 asdl_seq *ifs;
1127 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001129 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001130 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001131 if (n_ifs == -1)
1132 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001134 ifs = asdl_seq_new(n_ifs, c->c_arena);
1135 if (!ifs)
1136 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001138 for (j = 0; j < n_ifs; j++) {
1139 REQ(ch, list_iter);
1140 ch = CHILD(ch, 0);
1141 REQ(ch, list_if);
1142
1143 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1144 if (!list_for_expr)
1145 return NULL;
1146
1147 asdl_seq_SET(ifs, j, list_for_expr);
1148 if (NCH(ch) == 3)
1149 ch = CHILD(ch, 2);
1150 }
1151 /* on exit, must guarantee that ch is a list_for */
1152 if (TYPE(ch) == list_iter)
1153 ch = CHILD(ch, 0);
1154 lc->ifs = ifs;
1155 }
1156 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 }
1158
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001159 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160}
1161
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001162/*
1163 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001165 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166*/
1167
1168static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001169count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001171 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001173 count_comp_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001174 n_fors++;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001175 REQ(n, comp_for);
1176 if (NCH(n) == 5)
1177 n = CHILD(n, 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001178 else
1179 return n_fors;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001180 count_comp_iter:
1181 REQ(n, comp_iter);
1182 n = CHILD(n, 0);
1183 if (TYPE(n) == comp_for)
1184 goto count_comp_for;
1185 else if (TYPE(n) == comp_if) {
1186 if (NCH(n) == 3) {
1187 n = CHILD(n, 2);
1188 goto count_comp_iter;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001189 }
1190 else
1191 return n_fors;
1192 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001193
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001194 /* Should never be reached */
1195 PyErr_SetString(PyExc_SystemError,
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001196 "logic error in count_comp_fors");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001197 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198}
1199
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001200/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001202 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203*/
1204
1205static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001206count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001208 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001210 while (1) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001211 REQ(n, comp_iter);
1212 if (TYPE(CHILD(n, 0)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001213 return n_ifs;
1214 n = CHILD(n, 0);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001215 REQ(n, comp_if);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001216 n_ifs++;
1217 if (NCH(n) == 2)
1218 return n_ifs;
1219 n = CHILD(n, 2);
1220 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221}
1222
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001223static asdl_seq *
1224ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 int i, n_fors;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001227 asdl_seq *comps;
1228
1229 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001231 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001232
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001233 comps = asdl_seq_new(n_fors, c->c_arena);
1234 if (!comps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001235 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001236
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 for (i = 0; i < n_fors; i++) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001238 comprehension_ty comp;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001239 asdl_seq *t;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001240 expr_ty expression, first;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001241 node *for_ch;
1242
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001243 REQ(n, comp_for);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001244
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001245 for_ch = CHILD(n, 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001246 t = ast_for_exprlist(c, for_ch, Store);
1247 if (!t)
1248 return NULL;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001249 expression = ast_for_expr(c, CHILD(n, 3));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001250 if (!expression)
1251 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001253 /* Check the # of children rather than the length of t, since
1254 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001255 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001256 if (NCH(for_ch) == 1)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001257 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001258 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001259 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001260 c->c_arena),
1261 expression, NULL, c->c_arena);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001262 if (!comp)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001263 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001265 if (NCH(n) == 5) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001266 int j, n_ifs;
1267 asdl_seq *ifs;
1268
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001269 n = CHILD(n, 4);
1270 n_ifs = count_comp_ifs(c, n);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001271 if (n_ifs == -1)
1272 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001274 ifs = asdl_seq_new(n_ifs, c->c_arena);
1275 if (!ifs)
1276 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001277
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001278 for (j = 0; j < n_ifs; j++) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001279 REQ(n, comp_iter);
1280 n = CHILD(n, 0);
1281 REQ(n, comp_if);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001282
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001283 expression = ast_for_expr(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001284 if (!expression)
1285 return NULL;
1286 asdl_seq_SET(ifs, j, expression);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001287 if (NCH(n) == 3)
1288 n = CHILD(n, 2);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001289 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001290 /* on exit, must guarantee that n is a comp_for */
1291 if (TYPE(n) == comp_iter)
1292 n = CHILD(n, 0);
1293 comp->ifs = ifs;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001294 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001295 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001297 return comps;
1298}
1299
1300static expr_ty
1301ast_for_itercomp(struct compiling *c, const node *n, int type)
1302{
1303 expr_ty elt;
1304 asdl_seq *comps;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001306 assert(NCH(n) > 1);
1307
1308 elt = ast_for_expr(c, CHILD(n, 0));
1309 if (!elt)
1310 return NULL;
1311
1312 comps = ast_for_comprehension(c, CHILD(n, 1));
1313 if (!comps)
1314 return NULL;
1315
1316 if (type == COMP_GENEXP)
1317 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1318 else if (type == COMP_SETCOMP)
1319 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1320 else
1321 /* Should never happen */
1322 return NULL;
1323}
1324
1325static expr_ty
1326ast_for_dictcomp(struct compiling *c, const node *n)
1327{
1328 expr_ty key, value;
1329 asdl_seq *comps;
1330
1331 assert(NCH(n) > 3);
1332 REQ(CHILD(n, 1), COLON);
1333
1334 key = ast_for_expr(c, CHILD(n, 0));
1335 if (!key)
1336 return NULL;
1337
1338 value = ast_for_expr(c, CHILD(n, 2));
1339 if (!value)
1340 return NULL;
1341
1342 comps = ast_for_comprehension(c, CHILD(n, 3));
1343 if (!comps)
1344 return NULL;
1345
1346 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1347}
1348
1349static expr_ty
1350ast_for_genexp(struct compiling *c, const node *n)
1351{
1352 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
1353 return ast_for_itercomp(c, n, COMP_GENEXP);
1354}
1355
1356static expr_ty
1357ast_for_setcomp(struct compiling *c, const node *n)
1358{
1359 assert(TYPE(n) == (dictorsetmaker));
1360 return ast_for_itercomp(c, n, COMP_SETCOMP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361}
1362
1363static expr_ty
1364ast_for_atom(struct compiling *c, const node *n)
1365{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001366 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1368 */
1369 node *ch = CHILD(n, 0);
1370
1371 switch (TYPE(ch)) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001372 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001373 /* All names start in Load context, but may later be
1374 changed. */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001375 PyObject *name = NEW_IDENTIFIER(ch);
1376 if (!name)
1377 return NULL;
1378 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1379 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001381 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001382 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001383#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001384 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1385 PyObject *type, *value, *tback, *errstr;
1386 PyErr_Fetch(&type, &value, &tback);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001387 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001388 if (errstr) {
1389 char *s = "";
1390 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001391 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001392 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1393 ast_error(n, buf);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001394 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001395 } else {
1396 ast_error(n, "(unicode error) unknown error");
1397 }
1398 Py_DECREF(type);
1399 Py_DECREF(value);
1400 Py_XDECREF(tback);
1401 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001402#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001403 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001404 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001405 PyArena_AddPyObject(c->c_arena, str);
1406 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 }
1408 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001409 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001410 if (!pynum)
1411 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001412
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001413 PyArena_AddPyObject(c->c_arena, pynum);
1414 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 }
1416 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001417 ch = CHILD(n, 1);
1418
1419 if (TYPE(ch) == RPAR)
1420 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1421
1422 if (TYPE(ch) == yield_expr)
1423 return ast_for_expr(c, ch);
1424
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001425 return ast_for_testlist_comp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001427 ch = CHILD(n, 1);
1428
1429 if (TYPE(ch) == RSQB)
1430 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1431
1432 REQ(ch, listmaker);
1433 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1434 asdl_seq *elts = seq_for_testlist(c, ch);
1435 if (!elts)
1436 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001437
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001438 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1439 }
1440 else
1441 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 case LBRACE: {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001443 /* dictorsetmaker:
1444 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
1445 * (test (comp_for | (',' test)* [',']))
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001446 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001447 int i, size;
1448 asdl_seq *keys, *values;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001449
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001450 ch = CHILD(n, 1);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001451 if (TYPE(ch) == RBRACE) {
1452 /* it's an empty dict */
1453 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
1454 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1455 /* it's a simple set */
1456 asdl_seq *elts;
1457 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
1458 elts = asdl_seq_new(size, c->c_arena);
1459 if (!elts)
1460 return NULL;
1461 for (i = 0; i < NCH(ch); i += 2) {
1462 expr_ty expression;
1463 expression = ast_for_expr(c, CHILD(ch, i));
1464 if (!expression)
1465 return NULL;
1466 asdl_seq_SET(elts, i / 2, expression);
1467 }
1468 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001469 } else if (TYPE(CHILD(ch, 1)) == comp_for) {
1470 /* it's a set comprehension */
1471 return ast_for_setcomp(c, ch);
1472 } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {
1473 return ast_for_dictcomp(c, ch);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001474 } else {
1475 /* it's a dict */
1476 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1477 keys = asdl_seq_new(size, c->c_arena);
1478 if (!keys)
1479 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001480
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001481 values = asdl_seq_new(size, c->c_arena);
1482 if (!values)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001483 return NULL;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001484
1485 for (i = 0; i < NCH(ch); i += 4) {
1486 expr_ty expression;
1487
1488 expression = ast_for_expr(c, CHILD(ch, i));
1489 if (!expression)
1490 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001491
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001492 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001493
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001494 expression = ast_for_expr(c, CHILD(ch, i + 2));
1495 if (!expression)
1496 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001497
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001498 asdl_seq_SET(values, i / 4, expression);
1499 }
1500 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001501 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 }
1503 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001504 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001505 if (Py_Py3kWarningFlag &&
1506 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001507 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001508 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001509 if (!expression)
1510 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001511
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001512 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 }
1514 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001515 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 }
1518}
1519
1520static slice_ty
1521ast_for_slice(struct compiling *c, const node *n)
1522{
1523 node *ch;
1524 expr_ty lower = NULL, upper = NULL, step = NULL;
1525
1526 REQ(n, subscript);
1527
1528 /*
1529 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1530 sliceop: ':' [test]
1531 */
1532 ch = CHILD(n, 0);
1533 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001534 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535
1536 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001537 /* 'step' variable hold no significance in terms of being used over
1538 other vars */
1539 step = ast_for_expr(c, ch);
1540 if (!step)
1541 return NULL;
1542
1543 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 }
1545
1546 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001547 lower = ast_for_expr(c, ch);
1548 if (!lower)
1549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 }
1551
1552 /* If there's an upper bound it's in the second or third position. */
1553 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001554 if (NCH(n) > 1) {
1555 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001557 if (TYPE(n2) == test) {
1558 upper = ast_for_expr(c, n2);
1559 if (!upper)
1560 return NULL;
1561 }
1562 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001564 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001566 if (TYPE(n2) == test) {
1567 upper = ast_for_expr(c, n2);
1568 if (!upper)
1569 return NULL;
1570 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 }
1572
1573 ch = CHILD(n, NCH(n) - 1);
1574 if (TYPE(ch) == sliceop) {
Benjamin Peterson4879c902009-07-20 20:28:08 +00001575 if (NCH(ch) == 1) {
1576 /*
1577 This is an extended slice (ie "x[::]") with no expression in the
1578 step field. We set this literally to "None" in order to
1579 disambiguate it from x[:]. (The interpreter might have to call
1580 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1581 */
1582 identifier none = new_identifier("None", c->c_arena);
1583 if (!none)
1584 return NULL;
1585 ch = CHILD(ch, 0);
1586 step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);
1587 if (!step)
1588 return NULL;
1589 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001590 ch = CHILD(ch, 1);
1591 if (TYPE(ch) == test) {
1592 step = ast_for_expr(c, ch);
1593 if (!step)
1594 return NULL;
1595 }
1596 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 }
1598
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001599 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
1602static expr_ty
1603ast_for_binop(struct compiling *c, const node *n)
1604{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001605 /* Must account for a sequence of expressions.
1606 How should A op B op C by represented?
1607 BinOp(BinOp(A, op, B), op, C).
1608 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001610 int i, nops;
1611 expr_ty expr1, expr2, result;
1612 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001614 expr1 = ast_for_expr(c, CHILD(n, 0));
1615 if (!expr1)
1616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001618 expr2 = ast_for_expr(c, CHILD(n, 2));
1619 if (!expr2)
1620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001622 newoperator = get_operator(CHILD(n, 1));
1623 if (!newoperator)
1624 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001626 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1627 c->c_arena);
1628 if (!result)
1629 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001631 nops = (NCH(n) - 1) / 2;
1632 for (i = 1; i < nops; i++) {
1633 expr_ty tmp_result, tmp;
1634 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001636 newoperator = get_operator(next_oper);
1637 if (!newoperator)
1638 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001640 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1641 if (!tmp)
1642 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001644 tmp_result = BinOp(result, newoperator, tmp,
1645 LINENO(next_oper), next_oper->n_col_offset,
1646 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001647 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001648 return NULL;
1649 result = tmp_result;
1650 }
1651 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652}
1653
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001654static expr_ty
1655ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1656{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001657 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1658 subscriptlist: subscript (',' subscript)* [',']
1659 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1660 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001661 REQ(n, trailer);
1662 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001663 if (NCH(n) == 2)
1664 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1665 n->n_col_offset, c->c_arena);
1666 else
1667 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001668 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001669 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001670 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1671 if (!attr_id)
1672 return NULL;
1673 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001674 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001675 }
1676 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001677 REQ(CHILD(n, 0), LSQB);
1678 REQ(CHILD(n, 2), RSQB);
1679 n = CHILD(n, 1);
1680 if (NCH(n) == 1) {
1681 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1682 if (!slc)
1683 return NULL;
1684 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1685 c->c_arena);
1686 }
1687 else {
1688 /* The grammar is ambiguous here. The ambiguity is resolved
1689 by treating the sequence as a tuple literal if there are
1690 no slice features.
1691 */
1692 int j;
1693 slice_ty slc;
1694 expr_ty e;
1695 bool simple = true;
1696 asdl_seq *slices, *elts;
1697 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1698 if (!slices)
1699 return NULL;
1700 for (j = 0; j < NCH(n); j += 2) {
1701 slc = ast_for_slice(c, CHILD(n, j));
1702 if (!slc)
1703 return NULL;
1704 if (slc->kind != Index_kind)
1705 simple = false;
1706 asdl_seq_SET(slices, j / 2, slc);
1707 }
1708 if (!simple) {
1709 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1710 Load, LINENO(n), n->n_col_offset, c->c_arena);
1711 }
1712 /* extract Index values and put them in a Tuple */
1713 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1714 if (!elts)
1715 return NULL;
1716 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1717 slc = (slice_ty)asdl_seq_GET(slices, j);
1718 assert(slc->kind == Index_kind && slc->v.Index.value);
1719 asdl_seq_SET(elts, j, slc->v.Index.value);
1720 }
1721 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1722 if (!e)
1723 return NULL;
1724 return Subscript(left_expr, Index(e, c->c_arena),
1725 Load, LINENO(n), n->n_col_offset, c->c_arena);
1726 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001727 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001728}
1729
1730static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001731ast_for_factor(struct compiling *c, const node *n)
1732{
1733 node *pfactor, *ppower, *patom, *pnum;
1734 expr_ty expression;
1735
1736 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001737 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001738 constant. The peephole optimizer already does something like
1739 this but it doesn't handle the case where the constant is
1740 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1741 PyLongObject.
1742 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001743 if (TYPE(CHILD(n, 0)) == MINUS &&
1744 NCH(n) == 2 &&
1745 TYPE((pfactor = CHILD(n, 1))) == factor &&
1746 NCH(pfactor) == 1 &&
1747 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1748 NCH(ppower) == 1 &&
1749 TYPE((patom = CHILD(ppower, 0))) == atom &&
1750 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1751 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1752 if (s == NULL)
1753 return NULL;
1754 s[0] = '-';
1755 strcpy(s + 1, STR(pnum));
1756 PyObject_FREE(STR(pnum));
1757 STR(pnum) = s;
1758 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001759 }
1760
1761 expression = ast_for_expr(c, CHILD(n, 1));
1762 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001763 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001764
1765 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001766 case PLUS:
1767 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1768 c->c_arena);
1769 case MINUS:
1770 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1771 c->c_arena);
1772 case TILDE:
1773 return UnaryOp(Invert, expression, LINENO(n),
1774 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001775 }
1776 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001777 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001778 return NULL;
1779}
1780
1781static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001782ast_for_power(struct compiling *c, const node *n)
1783{
1784 /* power: atom trailer* ('**' factor)*
1785 */
1786 int i;
1787 expr_ty e, tmp;
1788 REQ(n, power);
1789 e = ast_for_atom(c, CHILD(n, 0));
1790 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001791 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001792 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001793 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001794 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001795 node *ch = CHILD(n, i);
1796 if (TYPE(ch) != trailer)
1797 break;
1798 tmp = ast_for_trailer(c, ch, e);
1799 if (!tmp)
1800 return NULL;
1801 tmp->lineno = e->lineno;
1802 tmp->col_offset = e->col_offset;
1803 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001804 }
1805 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001806 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1807 if (!f)
1808 return NULL;
1809 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1810 if (!tmp)
1811 return NULL;
1812 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001813 }
1814 return e;
1815}
1816
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817/* Do not name a variable 'expr'! Will cause a compile error.
1818*/
1819
1820static expr_ty
1821ast_for_expr(struct compiling *c, const node *n)
1822{
1823 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001824 test: or_test ['if' or_test 'else' test] | lambdef
1825 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 and_test: not_test ('and' not_test)*
1827 not_test: 'not' not_test | comparison
1828 comparison: expr (comp_op expr)*
1829 expr: xor_expr ('|' xor_expr)*
1830 xor_expr: and_expr ('^' and_expr)*
1831 and_expr: shift_expr ('&' shift_expr)*
1832 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1833 arith_expr: term (('+'|'-') term)*
1834 term: factor (('*'|'/'|'%'|'//') factor)*
1835 factor: ('+'|'-'|'~') factor | power
1836 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001837
1838 As well as modified versions that exist for backward compatibility,
1839 to explicitly allow:
1840 [ x for x in lambda: 0, lambda: 1 ]
1841 (which would be ambiguous without these extra rules)
1842
1843 old_test: or_test | old_lambdef
1844 old_lambdef: 'lambda' [vararglist] ':' old_test
1845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 */
1847
1848 asdl_seq *seq;
1849 int i;
1850
1851 loop:
1852 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001853 case test:
1854 case old_test:
1855 if (TYPE(CHILD(n, 0)) == lambdef ||
1856 TYPE(CHILD(n, 0)) == old_lambdef)
1857 return ast_for_lambdef(c, CHILD(n, 0));
1858 else if (NCH(n) > 1)
1859 return ast_for_ifexpr(c, n);
1860 /* Fallthrough */
1861 case or_test:
1862 case and_test:
1863 if (NCH(n) == 1) {
1864 n = CHILD(n, 0);
1865 goto loop;
1866 }
1867 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1868 if (!seq)
1869 return NULL;
1870 for (i = 0; i < NCH(n); i += 2) {
1871 expr_ty e = ast_for_expr(c, CHILD(n, i));
1872 if (!e)
1873 return NULL;
1874 asdl_seq_SET(seq, i / 2, e);
1875 }
1876 if (!strcmp(STR(CHILD(n, 1)), "and"))
1877 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1878 c->c_arena);
1879 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1880 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1881 case not_test:
1882 if (NCH(n) == 1) {
1883 n = CHILD(n, 0);
1884 goto loop;
1885 }
1886 else {
1887 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1888 if (!expression)
1889 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001891 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1892 c->c_arena);
1893 }
1894 case comparison:
1895 if (NCH(n) == 1) {
1896 n = CHILD(n, 0);
1897 goto loop;
1898 }
1899 else {
1900 expr_ty expression;
1901 asdl_int_seq *ops;
1902 asdl_seq *cmps;
1903 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1904 if (!ops)
1905 return NULL;
1906 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1907 if (!cmps) {
1908 return NULL;
1909 }
1910 for (i = 1; i < NCH(n); i += 2) {
1911 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001913 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001914 if (!newoperator) {
1915 return NULL;
1916 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001918 expression = ast_for_expr(c, CHILD(n, i + 1));
1919 if (!expression) {
1920 return NULL;
1921 }
1922
1923 asdl_seq_SET(ops, i / 2, newoperator);
1924 asdl_seq_SET(cmps, i / 2, expression);
1925 }
1926 expression = ast_for_expr(c, CHILD(n, 0));
1927 if (!expression) {
1928 return NULL;
1929 }
1930
1931 return Compare(expression, ops, cmps, LINENO(n),
1932 n->n_col_offset, c->c_arena);
1933 }
1934 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001936 /* The next five cases all handle BinOps. The main body of code
1937 is the same in each case, but the switch turned inside out to
1938 reuse the code for each type of operator.
1939 */
1940 case expr:
1941 case xor_expr:
1942 case and_expr:
1943 case shift_expr:
1944 case arith_expr:
1945 case term:
1946 if (NCH(n) == 1) {
1947 n = CHILD(n, 0);
1948 goto loop;
1949 }
1950 return ast_for_binop(c, n);
1951 case yield_expr: {
1952 expr_ty exp = NULL;
1953 if (NCH(n) == 2) {
1954 exp = ast_for_testlist(c, CHILD(n, 1));
1955 if (!exp)
1956 return NULL;
1957 }
1958 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1959 }
1960 case factor:
1961 if (NCH(n) == 1) {
1962 n = CHILD(n, 0);
1963 goto loop;
1964 }
1965 return ast_for_factor(c, n);
1966 case power:
1967 return ast_for_power(c, n);
1968 default:
1969 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001972 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 return NULL;
1974}
1975
1976static expr_ty
1977ast_for_call(struct compiling *c, const node *n, expr_ty func)
1978{
1979 /*
1980 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001981 | '**' test)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001982 argument: [test '='] test [comp_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 */
1984
1985 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001986 asdl_seq *args;
1987 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 expr_ty vararg = NULL, kwarg = NULL;
1989
1990 REQ(n, arglist);
1991
1992 nargs = 0;
1993 nkeywords = 0;
1994 ngens = 0;
1995 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001996 node *ch = CHILD(n, i);
1997 if (TYPE(ch) == argument) {
1998 if (NCH(ch) == 1)
1999 nargs++;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002000 else if (TYPE(CHILD(ch, 1)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002001 ngens++;
2002 else
2003 nkeywords++;
2004 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 }
2006 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002007 ast_error(n, "Generator expression must be parenthesized "
2008 "if not sole argument");
2009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
2011
2012 if (nargs + nkeywords + ngens > 255) {
2013 ast_error(n, "more than 255 arguments");
2014 return NULL;
2015 }
2016
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002017 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002019 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002020 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002022 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 nargs = 0;
2024 nkeywords = 0;
2025 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002026 node *ch = CHILD(n, i);
2027 if (TYPE(ch) == argument) {
2028 expr_ty e;
2029 if (NCH(ch) == 1) {
2030 if (nkeywords) {
2031 ast_error(CHILD(ch, 0),
2032 "non-keyword arg after keyword arg");
2033 return NULL;
2034 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00002035 if (vararg) {
2036 ast_error(CHILD(ch, 0),
2037 "only named arguments may follow *expression");
2038 return NULL;
2039 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002040 e = ast_for_expr(c, CHILD(ch, 0));
2041 if (!e)
2042 return NULL;
2043 asdl_seq_SET(args, nargs++, e);
2044 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002045 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002046 e = ast_for_genexp(c, ch);
2047 if (!e)
2048 return NULL;
2049 asdl_seq_SET(args, nargs++, e);
2050 }
2051 else {
2052 keyword_ty kw;
2053 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00002054 int k;
2055 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002057 /* CHILD(ch, 0) is test, but must be an identifier? */
2058 e = ast_for_expr(c, CHILD(ch, 0));
2059 if (!e)
2060 return NULL;
2061 /* f(lambda x: x[0] = 3) ends up getting parsed with
2062 * LHS test = lambda x: x[0], and RHS test = 3.
2063 * SF bug 132313 points out that complaining about a keyword
2064 * then is very confusing.
2065 */
2066 if (e->kind == Lambda_kind) {
2067 ast_error(CHILD(ch, 0),
2068 "lambda cannot contain assignment");
2069 return NULL;
2070 } else if (e->kind != Name_kind) {
2071 ast_error(CHILD(ch, 0), "keyword can't be an expression");
2072 return NULL;
2073 }
2074 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00002075 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00002076 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00002077 for (k = 0; k < nkeywords; k++) {
2078 tmp = PyString_AS_STRING(
2079 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
2080 if (!strcmp(tmp, PyString_AS_STRING(key))) {
2081 ast_error(CHILD(ch, 0), "keyword argument repeated");
2082 return NULL;
2083 }
2084 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002085 e = ast_for_expr(c, CHILD(ch, 2));
2086 if (!e)
2087 return NULL;
2088 kw = keyword(key, e, c->c_arena);
2089 if (!kw)
2090 return NULL;
2091 asdl_seq_SET(keywords, nkeywords++, kw);
2092 }
2093 }
2094 else if (TYPE(ch) == STAR) {
2095 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002096 if (!vararg)
2097 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002098 i++;
2099 }
2100 else if (TYPE(ch) == DOUBLESTAR) {
2101 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002102 if (!kwarg)
2103 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002104 i++;
2105 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 }
2107
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002108 return Call(func, args, keywords, vararg, kwarg, func->lineno,
2109 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110}
2111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002113ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002115 /* testlist_comp: test (',' test)* [','] */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002116 /* testlist: test (',' test)* [','] */
2117 /* testlist_safe: test (',' test)+ [','] */
2118 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 assert(NCH(n) > 0);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002120 if (TYPE(n) == testlist_comp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002121 if (NCH(n) > 1)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002122 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002123 }
2124 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002125 assert(TYPE(n) == testlist ||
2126 TYPE(n) == testlist_safe ||
2127 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002128 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002130 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002132 asdl_seq *tmp = seq_for_testlist(c, n);
2133 if (!tmp)
2134 return NULL;
2135 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002137}
2138
2139static expr_ty
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002140ast_for_testlist_comp(struct compiling *c, const node* n)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002141{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002142 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
2143 /* argument: test [ comp_for ] */
2144 assert(TYPE(n) == testlist_comp || TYPE(n) == argument);
2145 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002146 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002147 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002148}
2149
2150/* like ast_for_testlist() but returns a sequence */
2151static asdl_seq*
2152ast_for_class_bases(struct compiling *c, const node* n)
2153{
2154 /* testlist: test (',' test)* [','] */
2155 assert(NCH(n) > 0);
2156 REQ(n, testlist);
2157 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002158 expr_ty base;
2159 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2160 if (!bases)
2161 return NULL;
2162 base = ast_for_expr(c, CHILD(n, 0));
2163 if (!base)
2164 return NULL;
2165 asdl_seq_SET(bases, 0, base);
2166 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002167 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002168
2169 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170}
2171
2172static stmt_ty
2173ast_for_expr_stmt(struct compiling *c, const node *n)
2174{
2175 REQ(n, expr_stmt);
2176 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002177 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 testlist: test (',' test)* [',']
2179 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002180 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 test: ... here starts the operator precendence dance
2182 */
2183
2184 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002185 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2186 if (!e)
2187 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002189 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 }
2191 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002192 expr_ty expr1, expr2;
2193 operator_ty newoperator;
2194 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002196 expr1 = ast_for_testlist(c, ch);
2197 if (!expr1)
2198 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002199 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002200 return NULL;
Benjamin Peterson7adbb5a2009-10-03 20:23:24 +00002201 /* set_context checks that most expressions are not the left side.
2202 Augmented assignments can only have a name, a subscript, or an
2203 attribute on the left, though, so we have to explicitly check for
2204 those. */
2205 switch (expr1->kind) {
2206 case Name_kind:
2207 case Attribute_kind:
2208 case Subscript_kind:
2209 break;
2210 default:
2211 ast_error(ch, "illegal expression for augmented assignment");
2212 return NULL;
2213 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002215 ch = CHILD(n, 2);
2216 if (TYPE(ch) == testlist)
2217 expr2 = ast_for_testlist(c, ch);
2218 else
2219 expr2 = ast_for_expr(c, ch);
2220 if (!expr2)
2221 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002223 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002224 if (!newoperator)
2225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002227 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2228 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 }
2230 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002231 int i;
2232 asdl_seq *targets;
2233 node *value;
2234 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002236 /* a normal assignment */
2237 REQ(CHILD(n, 1), EQUAL);
2238 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2239 if (!targets)
2240 return NULL;
2241 for (i = 0; i < NCH(n) - 2; i += 2) {
2242 expr_ty e;
2243 node *ch = CHILD(n, i);
Benjamin Petersonb2664812009-06-11 17:49:38 +00002244 if (TYPE(ch) == yield_expr) {
2245 ast_error(ch, "assignment to yield expression not possible");
2246 return NULL;
2247 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002248 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002250 /* set context to assign */
2251 if (!e)
2252 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002254 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002255 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002257 asdl_seq_SET(targets, i / 2, e);
2258 }
2259 value = CHILD(n, NCH(n) - 1);
2260 if (TYPE(value) == testlist)
2261 expression = ast_for_testlist(c, value);
2262 else
2263 expression = ast_for_expr(c, value);
2264 if (!expression)
2265 return NULL;
2266 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2267 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269}
2270
2271static stmt_ty
2272ast_for_print_stmt(struct compiling *c, const node *n)
2273{
2274 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002275 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 */
2277 expr_ty dest = NULL, expression;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002278 asdl_seq *seq = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 bool nl;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002280 int i, j, values_count, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281
2282 REQ(n, print_stmt);
2283 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002284 dest = ast_for_expr(c, CHILD(n, 2));
2285 if (!dest)
2286 return NULL;
2287 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 }
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002289 values_count = (NCH(n) + 1 - start) / 2;
2290 if (values_count) {
2291 seq = asdl_seq_new(values_count, c->c_arena);
2292 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002293 return NULL;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002294 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
2295 expression = ast_for_expr(c, CHILD(n, i));
2296 if (!expression)
2297 return NULL;
2298 asdl_seq_SET(seq, j, expression);
2299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 }
2301 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002302 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303}
2304
2305static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002306ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307{
2308 asdl_seq *seq;
2309 int i;
2310 expr_ty e;
2311
2312 REQ(n, exprlist);
2313
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002314 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002318 e = ast_for_expr(c, CHILD(n, i));
2319 if (!e)
2320 return NULL;
2321 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002322 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002323 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 }
2325 return seq;
2326}
2327
2328static stmt_ty
2329ast_for_del_stmt(struct compiling *c, const node *n)
2330{
2331 asdl_seq *expr_list;
2332
2333 /* del_stmt: 'del' exprlist */
2334 REQ(n, del_stmt);
2335
2336 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2337 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002338 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002339 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340}
2341
2342static stmt_ty
2343ast_for_flow_stmt(struct compiling *c, const node *n)
2344{
2345 /*
2346 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002347 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 break_stmt: 'break'
2349 continue_stmt: 'continue'
2350 return_stmt: 'return' [testlist]
2351 yield_stmt: yield_expr
2352 yield_expr: 'yield' testlist
2353 raise_stmt: 'raise' [test [',' test [',' test]]]
2354 */
2355 node *ch;
2356
2357 REQ(n, flow_stmt);
2358 ch = CHILD(n, 0);
2359 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002360 case break_stmt:
2361 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2362 case continue_stmt:
2363 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2364 case yield_stmt: { /* will reduce to yield_expr */
2365 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2366 if (!exp)
2367 return NULL;
2368 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2369 }
2370 case return_stmt:
2371 if (NCH(ch) == 1)
2372 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2373 else {
2374 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2375 if (!expression)
2376 return NULL;
2377 return Return(expression, LINENO(n), n->n_col_offset,
2378 c->c_arena);
2379 }
2380 case raise_stmt:
2381 if (NCH(ch) == 1)
2382 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2383 c->c_arena);
2384 else if (NCH(ch) == 2) {
2385 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2386 if (!expression)
2387 return NULL;
2388 return Raise(expression, NULL, NULL, LINENO(n),
2389 n->n_col_offset, c->c_arena);
2390 }
2391 else if (NCH(ch) == 4) {
2392 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002394 expr1 = ast_for_expr(c, CHILD(ch, 1));
2395 if (!expr1)
2396 return NULL;
2397 expr2 = ast_for_expr(c, CHILD(ch, 3));
2398 if (!expr2)
2399 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002401 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2402 c->c_arena);
2403 }
2404 else if (NCH(ch) == 6) {
2405 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002407 expr1 = ast_for_expr(c, CHILD(ch, 1));
2408 if (!expr1)
2409 return NULL;
2410 expr2 = ast_for_expr(c, CHILD(ch, 3));
2411 if (!expr2)
2412 return NULL;
2413 expr3 = ast_for_expr(c, CHILD(ch, 5));
2414 if (!expr3)
2415 return NULL;
2416
2417 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2418 c->c_arena);
2419 }
2420 default:
2421 PyErr_Format(PyExc_SystemError,
2422 "unexpected flow_stmt: %d", TYPE(ch));
2423 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002425
2426 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2427 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428}
2429
2430static alias_ty
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002431alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432{
2433 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002434 import_as_name: NAME ['as' NAME]
2435 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 dotted_name: NAME ('.' NAME)*
2437 */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002438 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002439
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 loop:
2441 switch (TYPE(n)) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002442 case import_as_name: {
2443 node *name_node = CHILD(n, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002444 str = NULL;
2445 if (NCH(n) == 3) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002446 node *str_node = CHILD(n, 2);
2447 if (store && !forbidden_check(c, str_node, STR(str_node)))
2448 return NULL;
2449 str = NEW_IDENTIFIER(str_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002450 if (!str)
2451 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002452 }
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002453 else {
2454 if (!forbidden_check(c, name_node, STR(name_node)))
2455 return NULL;
2456 }
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002457 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002458 if (!name)
2459 return NULL;
2460 return alias(name, str, c->c_arena);
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002461 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002462 case dotted_as_name:
2463 if (NCH(n) == 1) {
2464 n = CHILD(n, 0);
2465 goto loop;
2466 }
2467 else {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002468 node *asname_node = CHILD(n, 2);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002469 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002470 if (!a)
2471 return NULL;
2472 assert(!a->asname);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002473 if (!forbidden_check(c, asname_node, STR(asname_node)))
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002474 return NULL;
2475 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002476 if (!a->asname)
2477 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002478 return a;
2479 }
2480 break;
2481 case dotted_name:
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002482 if (NCH(n) == 1) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002483 node *name_node = CHILD(n, 0);
2484 if (store && !forbidden_check(c, name_node, STR(name_node)))
2485 return NULL;
2486 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002487 if (!name)
2488 return NULL;
2489 return alias(name, NULL, c->c_arena);
2490 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002491 else {
2492 /* Create a string of the form "a.b.c" */
2493 int i;
2494 size_t len;
2495 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002497 len = 0;
2498 for (i = 0; i < NCH(n); i += 2)
2499 /* length of string plus one for the dot */
2500 len += strlen(STR(CHILD(n, i))) + 1;
2501 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002502 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002503 if (!str)
2504 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002505 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002506 if (!s)
2507 return NULL;
2508 for (i = 0; i < NCH(n); i += 2) {
2509 char *sch = STR(CHILD(n, i));
2510 strcpy(s, STR(CHILD(n, i)));
2511 s += strlen(sch);
2512 *s++ = '.';
2513 }
2514 --s;
2515 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002516 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002517 PyArena_AddPyObject(c->c_arena, str);
2518 return alias(str, NULL, c->c_arena);
2519 }
2520 break;
2521 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002522 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002523 PyArena_AddPyObject(c->c_arena, str);
2524 return alias(str, NULL, c->c_arena);
2525 default:
2526 PyErr_Format(PyExc_SystemError,
2527 "unexpected import name: %d", TYPE(n));
2528 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002530
2531 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 return NULL;
2533}
2534
2535static stmt_ty
2536ast_for_import_stmt(struct compiling *c, const node *n)
2537{
2538 /*
2539 import_stmt: import_name | import_from
2540 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002541 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002542 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002544 int lineno;
2545 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 int i;
2547 asdl_seq *aliases;
2548
2549 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002550 lineno = LINENO(n);
2551 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002553 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002554 n = CHILD(n, 1);
2555 REQ(n, dotted_as_names);
2556 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2557 if (!aliases)
2558 return NULL;
2559 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002560 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002561 if (!import_alias)
2562 return NULL;
2563 asdl_seq_SET(aliases, i / 2, import_alias);
2564 }
2565 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002567 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002568 int n_children;
2569 int idx, ndots = 0;
2570 alias_ty mod = NULL;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002571 identifier modname = NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002572
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002573 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002574 optional module name */
2575 for (idx = 1; idx < NCH(n); idx++) {
2576 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002577 mod = alias_for_import_name(c, CHILD(n, idx), 0);
2578 if (!mod)
2579 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002580 idx++;
2581 break;
2582 } else if (TYPE(CHILD(n, idx)) != DOT) {
2583 break;
2584 }
2585 ndots++;
2586 }
2587 idx++; /* skip over the 'import' keyword */
2588 switch (TYPE(CHILD(n, idx))) {
2589 case STAR:
2590 /* from ... import * */
2591 n = CHILD(n, idx);
2592 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002593 break;
2594 case LPAR:
2595 /* from ... import (x, y, z) */
2596 n = CHILD(n, idx + 1);
2597 n_children = NCH(n);
2598 break;
2599 case import_as_names:
2600 /* from ... import x, y, z */
2601 n = CHILD(n, idx);
2602 n_children = NCH(n);
2603 if (n_children % 2 == 0) {
2604 ast_error(n, "trailing comma not allowed without"
2605 " surrounding parentheses");
2606 return NULL;
2607 }
2608 break;
2609 default:
2610 ast_error(n, "Unexpected node-type in from-import");
2611 return NULL;
2612 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002614 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2615 if (!aliases)
2616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002618 /* handle "from ... import *" special b/c there's no children */
2619 if (TYPE(n) == STAR) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002620 alias_ty import_alias = alias_for_import_name(c, n, 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002621 if (!import_alias)
2622 return NULL;
2623 asdl_seq_SET(aliases, 0, import_alias);
2624 }
2625 else {
2626 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002627 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002628 if (!import_alias)
2629 return NULL;
2630 asdl_seq_SET(aliases, i / 2, import_alias);
2631 }
2632 }
2633 if (mod != NULL)
2634 modname = mod->name;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002635 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2636 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 }
Neal Norwitz79792652005-11-14 04:25:03 +00002638 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002639 "unknown import statement: starts with command '%s'",
2640 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 return NULL;
2642}
2643
2644static stmt_ty
2645ast_for_global_stmt(struct compiling *c, const node *n)
2646{
2647 /* global_stmt: 'global' NAME (',' NAME)* */
2648 identifier name;
2649 asdl_seq *s;
2650 int i;
2651
2652 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002653 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002657 name = NEW_IDENTIFIER(CHILD(n, i));
2658 if (!name)
2659 return NULL;
2660 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002662 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663}
2664
2665static stmt_ty
2666ast_for_exec_stmt(struct compiling *c, const node *n)
2667{
2668 expr_ty expr1, globals = NULL, locals = NULL;
2669 int n_children = NCH(n);
2670 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002671 PyErr_Format(PyExc_SystemError,
2672 "poorly formed 'exec' statement: %d parts to statement",
2673 n_children);
2674 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 }
2676
2677 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2678 REQ(n, exec_stmt);
2679 expr1 = ast_for_expr(c, CHILD(n, 1));
2680 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002681 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002683 globals = ast_for_expr(c, CHILD(n, 3));
2684 if (!globals)
2685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 }
2687 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002688 locals = ast_for_expr(c, CHILD(n, 5));
2689 if (!locals)
2690 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 }
2692
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002693 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2694 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695}
2696
2697static stmt_ty
2698ast_for_assert_stmt(struct compiling *c, const node *n)
2699{
2700 /* assert_stmt: 'assert' test [',' test] */
2701 REQ(n, assert_stmt);
2702 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002703 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2704 if (!expression)
2705 return NULL;
2706 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2707 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 }
2709 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002710 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002712 expr1 = ast_for_expr(c, CHILD(n, 1));
2713 if (!expr1)
2714 return NULL;
2715 expr2 = ast_for_expr(c, CHILD(n, 3));
2716 if (!expr2)
2717 return NULL;
2718
2719 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 }
Neal Norwitz79792652005-11-14 04:25:03 +00002721 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002722 "improper number of parts to 'assert' statement: %d",
2723 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724 return NULL;
2725}
2726
2727static asdl_seq *
2728ast_for_suite(struct compiling *c, const node *n)
2729{
2730 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002731 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732 stmt_ty s;
2733 int i, total, num, end, pos = 0;
2734 node *ch;
2735
2736 REQ(n, suite);
2737
2738 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002739 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002743 n = CHILD(n, 0);
2744 /* simple_stmt always ends with a NEWLINE,
2745 and may have a trailing SEMI
2746 */
2747 end = NCH(n) - 1;
2748 if (TYPE(CHILD(n, end - 1)) == SEMI)
2749 end--;
2750 /* loop by 2 to skip semi-colons */
2751 for (i = 0; i < end; i += 2) {
2752 ch = CHILD(n, i);
2753 s = ast_for_stmt(c, ch);
2754 if (!s)
2755 return NULL;
2756 asdl_seq_SET(seq, pos++, s);
2757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 }
2759 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002760 for (i = 2; i < (NCH(n) - 1); i++) {
2761 ch = CHILD(n, i);
2762 REQ(ch, stmt);
2763 num = num_stmts(ch);
2764 if (num == 1) {
2765 /* small_stmt or compound_stmt with only one child */
2766 s = ast_for_stmt(c, ch);
2767 if (!s)
2768 return NULL;
2769 asdl_seq_SET(seq, pos++, s);
2770 }
2771 else {
2772 int j;
2773 ch = CHILD(ch, 0);
2774 REQ(ch, simple_stmt);
2775 for (j = 0; j < NCH(ch); j += 2) {
2776 /* statement terminates with a semi-colon ';' */
2777 if (NCH(CHILD(ch, j)) == 0) {
2778 assert((j + 1) == NCH(ch));
2779 break;
2780 }
2781 s = ast_for_stmt(c, CHILD(ch, j));
2782 if (!s)
2783 return NULL;
2784 asdl_seq_SET(seq, pos++, s);
2785 }
2786 }
2787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 }
2789 assert(pos == seq->size);
2790 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791}
2792
2793static stmt_ty
2794ast_for_if_stmt(struct compiling *c, const node *n)
2795{
2796 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2797 ['else' ':' suite]
2798 */
2799 char *s;
2800
2801 REQ(n, if_stmt);
2802
2803 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002804 expr_ty expression;
2805 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002807 expression = ast_for_expr(c, CHILD(n, 1));
2808 if (!expression)
2809 return NULL;
2810 suite_seq = ast_for_suite(c, CHILD(n, 3));
2811 if (!suite_seq)
2812 return NULL;
2813
2814 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2815 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 s = STR(CHILD(n, 4));
2819 /* s[2], the third character in the string, will be
2820 's' for el_s_e, or
2821 'i' for el_i_f
2822 */
2823 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002824 expr_ty expression;
2825 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002827 expression = ast_for_expr(c, CHILD(n, 1));
2828 if (!expression)
2829 return NULL;
2830 seq1 = ast_for_suite(c, CHILD(n, 3));
2831 if (!seq1)
2832 return NULL;
2833 seq2 = ast_for_suite(c, CHILD(n, 6));
2834 if (!seq2)
2835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002837 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2838 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 }
2840 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002841 int i, n_elif, has_else = 0;
2842 expr_ty expression;
2843 asdl_seq *suite_seq;
2844 asdl_seq *orelse = NULL;
2845 n_elif = NCH(n) - 4;
2846 /* must reference the child n_elif+1 since 'else' token is third,
2847 not fourth, child from the end. */
2848 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2849 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2850 has_else = 1;
2851 n_elif -= 3;
2852 }
2853 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002855 if (has_else) {
2856 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002858 orelse = asdl_seq_new(1, c->c_arena);
2859 if (!orelse)
2860 return NULL;
2861 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2862 if (!expression)
2863 return NULL;
2864 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2865 if (!suite_seq)
2866 return NULL;
2867 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2868 if (!suite_seq2)
2869 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002871 asdl_seq_SET(orelse, 0,
2872 If(expression, suite_seq, suite_seq2,
2873 LINENO(CHILD(n, NCH(n) - 6)),
2874 CHILD(n, NCH(n) - 6)->n_col_offset,
2875 c->c_arena));
2876 /* the just-created orelse handled the last elif */
2877 n_elif--;
2878 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002880 for (i = 0; i < n_elif; i++) {
2881 int off = 5 + (n_elif - i - 1) * 4;
2882 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2883 if (!newobj)
2884 return NULL;
2885 expression = ast_for_expr(c, CHILD(n, off));
2886 if (!expression)
2887 return NULL;
2888 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2889 if (!suite_seq)
2890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002892 asdl_seq_SET(newobj, 0,
2893 If(expression, suite_seq, orelse,
2894 LINENO(CHILD(n, off)),
2895 CHILD(n, off)->n_col_offset, c->c_arena));
2896 orelse = newobj;
2897 }
2898 expression = ast_for_expr(c, CHILD(n, 1));
2899 if (!expression)
2900 return NULL;
2901 suite_seq = ast_for_suite(c, CHILD(n, 3));
2902 if (!suite_seq)
2903 return NULL;
2904 return If(expression, suite_seq, orelse,
2905 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002907
2908 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002909 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002910 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911}
2912
2913static stmt_ty
2914ast_for_while_stmt(struct compiling *c, const node *n)
2915{
2916 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2917 REQ(n, while_stmt);
2918
2919 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002920 expr_ty expression;
2921 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002923 expression = ast_for_expr(c, CHILD(n, 1));
2924 if (!expression)
2925 return NULL;
2926 suite_seq = ast_for_suite(c, CHILD(n, 3));
2927 if (!suite_seq)
2928 return NULL;
2929 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2930 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 }
2932 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002933 expr_ty expression;
2934 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002936 expression = ast_for_expr(c, CHILD(n, 1));
2937 if (!expression)
2938 return NULL;
2939 seq1 = ast_for_suite(c, CHILD(n, 3));
2940 if (!seq1)
2941 return NULL;
2942 seq2 = ast_for_suite(c, CHILD(n, 6));
2943 if (!seq2)
2944 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002946 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2947 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002949
2950 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002951 "wrong number of tokens for 'while' statement: %d",
2952 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954}
2955
2956static stmt_ty
2957ast_for_for_stmt(struct compiling *c, const node *n)
2958{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002959 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 expr_ty expression;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002961 expr_ty target, first;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002962 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2964 REQ(n, for_stmt);
2965
2966 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002967 seq = ast_for_suite(c, CHILD(n, 8));
2968 if (!seq)
2969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 }
2971
Neal Norwitzedef2be2006-07-12 05:26:17 +00002972 node_target = CHILD(n, 1);
2973 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002974 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002975 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002976 /* Check the # of children rather than the length of _target, since
2977 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002978 first = (expr_ty)asdl_seq_GET(_target, 0);
Neal Norwitzedef2be2006-07-12 05:26:17 +00002979 if (NCH(node_target) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002980 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002982 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002984 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002985 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002986 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002988 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002991 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002992 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993}
2994
2995static excepthandler_ty
2996ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2997{
Collin Winter62903052007-05-18 23:11:24 +00002998 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999 REQ(exc, except_clause);
3000 REQ(body, suite);
3001
3002 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003003 asdl_seq *suite_seq = ast_for_suite(c, body);
3004 if (!suite_seq)
3005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006
Georg Brandla48f3ab2008-03-30 06:40:17 +00003007 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003008 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003009 }
3010 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003011 expr_ty expression;
3012 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003014 expression = ast_for_expr(c, CHILD(exc, 1));
3015 if (!expression)
3016 return NULL;
3017 suite_seq = ast_for_suite(c, body);
3018 if (!suite_seq)
3019 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020
Georg Brandla48f3ab2008-03-30 06:40:17 +00003021 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003022 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 }
3024 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003025 asdl_seq *suite_seq;
3026 expr_ty expression;
3027 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
3028 if (!e)
3029 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003030 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003031 return NULL;
3032 expression = ast_for_expr(c, CHILD(exc, 1));
3033 if (!expression)
3034 return NULL;
3035 suite_seq = ast_for_suite(c, body);
3036 if (!suite_seq)
3037 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038
Georg Brandla48f3ab2008-03-30 06:40:17 +00003039 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003040 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003042
3043 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003044 "wrong number of children for 'except' clause: %d",
3045 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003046 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047}
3048
3049static stmt_ty
3050ast_for_try_stmt(struct compiling *c, const node *n)
3051{
Neal Norwitzf599f422005-12-17 21:33:47 +00003052 const int nch = NCH(n);
3053 int n_except = (nch - 3)/3;
3054 asdl_seq *body, *orelse = NULL, *finally = NULL;
3055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 REQ(n, try_stmt);
3057
Neal Norwitzf599f422005-12-17 21:33:47 +00003058 body = ast_for_suite(c, CHILD(n, 2));
3059 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003060 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061
Neal Norwitzf599f422005-12-17 21:33:47 +00003062 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003063 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3064 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3065 /* we can assume it's an "else",
3066 because nch >= 9 for try-else-finally and
3067 it would otherwise have a type of except_clause */
3068 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3069 if (orelse == NULL)
3070 return NULL;
3071 n_except--;
3072 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003074 finally = ast_for_suite(c, CHILD(n, nch - 1));
3075 if (finally == NULL)
3076 return NULL;
3077 n_except--;
3078 }
3079 else {
3080 /* we can assume it's an "else",
3081 otherwise it would have a type of except_clause */
3082 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3083 if (orelse == NULL)
3084 return NULL;
3085 n_except--;
3086 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003088 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003089 ast_error(n, "malformed 'try' statement");
3090 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003092
3093 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003094 int i;
3095 stmt_ty except_st;
3096 /* process except statements to create a try ... except */
3097 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
3098 if (handlers == NULL)
3099 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003100
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003101 for (i = 0; i < n_except; i++) {
3102 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3103 CHILD(n, 5 + i * 3));
3104 if (!e)
3105 return NULL;
3106 asdl_seq_SET(handlers, i, e);
3107 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003108
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003109 except_st = TryExcept(body, handlers, orelse, LINENO(n),
3110 n->n_col_offset, c->c_arena);
3111 if (!finally)
3112 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00003113
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003114 /* if a 'finally' is present too, we nest the TryExcept within a
3115 TryFinally to emulate try ... except ... finally */
3116 body = asdl_seq_new(1, c->c_arena);
3117 if (body == NULL)
3118 return NULL;
3119 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00003120 }
3121
3122 /* must be a try ... finally (except clauses are in body, if any exist) */
3123 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003124 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125}
3126
Georg Brandl944f6842009-05-25 21:02:56 +00003127/* with_item: test ['as' expr] */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003128static stmt_ty
Georg Brandl944f6842009-05-25 21:02:56 +00003129ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003130{
3131 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003132
Georg Brandl944f6842009-05-25 21:02:56 +00003133 REQ(n, with_item);
3134 context_expr = ast_for_expr(c, CHILD(n, 0));
Collin Winter77c67bd2007-03-16 04:11:30 +00003135 if (!context_expr)
3136 return NULL;
Georg Brandl944f6842009-05-25 21:02:56 +00003137 if (NCH(n) == 3) {
3138 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003139
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003140 if (!optional_vars) {
3141 return NULL;
3142 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003143 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003144 return NULL;
3145 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003146 }
3147
Georg Brandl944f6842009-05-25 21:02:56 +00003148 return With(context_expr, optional_vars, content, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003149 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003150}
3151
Georg Brandl944f6842009-05-25 21:02:56 +00003152/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3153static stmt_ty
3154ast_for_with_stmt(struct compiling *c, const node *n)
3155{
3156 int i;
3157 stmt_ty ret;
3158 asdl_seq *inner;
3159
3160 REQ(n, with_stmt);
3161
3162 /* process the with items inside-out */
3163 i = NCH(n) - 1;
3164 /* the suite of the innermost with item is the suite of the with stmt */
3165 inner = ast_for_suite(c, CHILD(n, i));
3166 if (!inner)
3167 return NULL;
3168
3169 for (;;) {
3170 i -= 2;
3171 ret = ast_for_with_item(c, CHILD(n, i), inner);
3172 if (!ret)
3173 return NULL;
3174 /* was this the last item? */
3175 if (i == 1)
3176 break;
3177 /* if not, wrap the result so far in a new sequence */
3178 inner = asdl_seq_new(1, c->c_arena);
3179 if (!inner)
3180 return NULL;
3181 asdl_seq_SET(inner, 0, ret);
3182 }
3183
3184 return ret;
3185}
3186
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003188ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189{
3190 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003191 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 asdl_seq *bases, *s;
3193
3194 REQ(n, classdef);
3195
Benjamin Petersond5efd202008-06-08 22:52:37 +00003196 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003197 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198
3199 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003200 s = ast_for_suite(c, CHILD(n, 3));
3201 if (!s)
3202 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003203 classname = NEW_IDENTIFIER(CHILD(n, 1));
3204 if (!classname)
3205 return NULL;
3206 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3207 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 }
3209 /* check for empty base list */
3210 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003211 s = ast_for_suite(c, CHILD(n,5));
3212 if (!s)
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003213 return NULL;
3214 classname = NEW_IDENTIFIER(CHILD(n, 1));
3215 if (!classname)
3216 return NULL;
3217 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3218 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 }
3220
3221 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003222 bases = ast_for_class_bases(c, CHILD(n, 3));
3223 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003224 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225
3226 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003227 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003228 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003229 classname = NEW_IDENTIFIER(CHILD(n, 1));
3230 if (!classname)
3231 return NULL;
3232 return ClassDef(classname, bases, s, decorator_seq,
Christian Heimes5224d282008-02-23 15:01:05 +00003233 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234}
3235
3236static stmt_ty
3237ast_for_stmt(struct compiling *c, const node *n)
3238{
3239 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003240 assert(NCH(n) == 1);
3241 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242 }
3243 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003244 assert(num_stmts(n) == 1);
3245 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 }
3247 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003248 n = CHILD(n, 0);
3249 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3250 | flow_stmt | import_stmt | global_stmt | exec_stmt
3251 | assert_stmt
3252 */
3253 switch (TYPE(n)) {
3254 case expr_stmt:
3255 return ast_for_expr_stmt(c, n);
3256 case print_stmt:
3257 return ast_for_print_stmt(c, n);
3258 case del_stmt:
3259 return ast_for_del_stmt(c, n);
3260 case pass_stmt:
3261 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3262 case flow_stmt:
3263 return ast_for_flow_stmt(c, n);
3264 case import_stmt:
3265 return ast_for_import_stmt(c, n);
3266 case global_stmt:
3267 return ast_for_global_stmt(c, n);
3268 case exec_stmt:
3269 return ast_for_exec_stmt(c, n);
3270 case assert_stmt:
3271 return ast_for_assert_stmt(c, n);
3272 default:
3273 PyErr_Format(PyExc_SystemError,
3274 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3275 TYPE(n), NCH(n));
3276 return NULL;
3277 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278 }
3279 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003280 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003281 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003282 */
3283 node *ch = CHILD(n, 0);
3284 REQ(n, compound_stmt);
3285 switch (TYPE(ch)) {
3286 case if_stmt:
3287 return ast_for_if_stmt(c, ch);
3288 case while_stmt:
3289 return ast_for_while_stmt(c, ch);
3290 case for_stmt:
3291 return ast_for_for_stmt(c, ch);
3292 case try_stmt:
3293 return ast_for_try_stmt(c, ch);
3294 case with_stmt:
3295 return ast_for_with_stmt(c, ch);
3296 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003297 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003298 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003299 return ast_for_classdef(c, ch, NULL);
3300 case decorated:
3301 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003302 default:
3303 PyErr_Format(PyExc_SystemError,
3304 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3305 TYPE(n), NCH(n));
3306 return NULL;
3307 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 }
3309}
3310
3311static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003312parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003314 const char *end;
3315 long x;
3316 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003318 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003319 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320#endif
3321
Mark Dickinson422ce062008-12-05 17:59:46 +00003322 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003323 errno = 0;
3324 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003326 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003328 if (*end == 'l' || *end == 'L')
3329 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003330 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003331 if (*end == '\0') {
3332 if (errno != 0)
3333 return PyLong_FromString((char *)s, (char **)0, 0);
3334 return PyInt_FromLong(x);
3335 }
3336 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003338 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003339 complex.real = 0.;
Eric Smithabc9f702009-10-27 18:33:14 +00003340 complex.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3341 if (complex.imag == -1.0 && PyErr_Occurred())
3342 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003343 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003344 }
3345 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003346#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003347 {
Eric Smithabc9f702009-10-27 18:33:14 +00003348 dx = PyOS_string_to_double(s, NULL, NULL);
3349 if (dx == -1.0 && PyErr_Occurred())
3350 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003351 return PyFloat_FromDouble(dx);
3352 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353}
3354
3355static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003356decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357{
3358#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003359 Py_FatalError("decode_utf8 should not be called in this build.");
3360 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003362 PyObject *u, *v;
3363 char *s, *t;
3364 t = s = (char *)*sPtr;
3365 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3366 while (s < end && (*s & 0x80)) s++;
3367 *sPtr = s;
3368 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3369 if (u == NULL)
3370 return NULL;
3371 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3372 Py_DECREF(u);
3373 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374#endif
3375}
3376
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003377#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003379decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003381 PyObject *v, *u;
3382 char *buf;
3383 char *p;
3384 const char *end;
3385 if (encoding == NULL) {
3386 buf = (char *)s;
3387 u = NULL;
3388 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3389 buf = (char *)s;
3390 u = NULL;
3391 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003392 /* check for integer overflow */
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003393 if (len > PY_SIZE_MAX / 6)
Gregory P. Smith9d534572008-06-11 07:41:16 +00003394 return NULL;
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003395 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3396 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3397 u = PyString_FromStringAndSize((char *)NULL, len * 6);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003398 if (u == NULL)
3399 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003400 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003401 end = s + len;
3402 while (s < end) {
3403 if (*s == '\\') {
3404 *p++ = *s++;
3405 if (*s & 0x80) {
3406 strcpy(p, "u005c");
3407 p += 5;
3408 }
3409 }
3410 if (*s & 0x80) { /* XXX inefficient */
3411 PyObject *w;
3412 char *r;
3413 Py_ssize_t rn, i;
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003414 w = decode_utf8(c, &s, end, "utf-32-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003415 if (w == NULL) {
3416 Py_DECREF(u);
3417 return NULL;
3418 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003419 r = PyString_AsString(w);
3420 rn = PyString_Size(w);
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003421 assert(rn % 4 == 0);
3422 for (i = 0; i < rn; i += 4) {
3423 sprintf(p, "\\U%02x%02x%02x%02x",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003424 r[i + 0] & 0xFF,
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003425 r[i + 1] & 0xFF,
3426 r[i + 2] & 0xFF,
3427 r[i + 3] & 0xFF);
3428 p += 10;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003429 }
3430 Py_DECREF(w);
3431 } else {
3432 *p++ = *s++;
3433 }
3434 }
3435 len = p - buf;
3436 s = buf;
3437 }
3438 if (rawmode)
3439 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3440 else
3441 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3442 Py_XDECREF(u);
3443 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003445#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446
3447/* s is a Python string literal, including the bracketing quote characters,
3448 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3449 * parsestr parses it, and returns the decoded Python string object.
3450 */
3451static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003452parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003454 size_t len;
3455 int quote = Py_CHARMASK(*s);
3456 int rawmode = 0;
3457 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003458 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003460 if (isalpha(quote) || quote == '_') {
3461 if (quote == 'u' || quote == 'U') {
3462 quote = *++s;
3463 unicode = 1;
3464 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003465 if (quote == 'b' || quote == 'B') {
3466 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003467 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003468 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003469 if (quote == 'r' || quote == 'R') {
3470 quote = *++s;
3471 rawmode = 1;
3472 }
3473 }
3474 if (quote != '\'' && quote != '\"') {
3475 PyErr_BadInternalCall();
3476 return NULL;
3477 }
3478 s++;
3479 len = strlen(s);
3480 if (len > INT_MAX) {
3481 PyErr_SetString(PyExc_OverflowError,
3482 "string to parse is too long");
3483 return NULL;
3484 }
3485 if (s[--len] != quote) {
3486 PyErr_BadInternalCall();
3487 return NULL;
3488 }
3489 if (len >= 4 && s[0] == quote && s[1] == quote) {
3490 s += 2;
3491 len -= 2;
3492 if (s[--len] != quote || s[--len] != quote) {
3493 PyErr_BadInternalCall();
3494 return NULL;
3495 }
3496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003498 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003499 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003500 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003502 need_encoding = (c->c_encoding != NULL &&
3503 strcmp(c->c_encoding, "utf-8") != 0 &&
3504 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003505 if (rawmode || strchr(s, '\\') == NULL) {
3506 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003508 /* This should not happen - we never see any other
3509 encoding. */
3510 Py_FatalError(
3511 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003513 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3514 if (u == NULL)
3515 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003516 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003517 Py_DECREF(u);
3518 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003520 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003521 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003522 }
3523 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003525 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003526 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527}
3528
3529/* Build a Python string object out of a STRING atom. This takes care of
3530 * compile-time literal catenation, calling parsestr() on each piece, and
3531 * pasting the intermediate results together.
3532 */
3533static PyObject *
3534parsestrplus(struct compiling *c, const node *n)
3535{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003536 PyObject *v;
3537 int i;
3538 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003539 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003540 /* String literal concatenation */
3541 for (i = 1; i < NCH(n); i++) {
3542 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003543 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003544 if (s == NULL)
3545 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003546 if (PyString_Check(v) && PyString_Check(s)) {
3547 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003548 if (v == NULL)
3549 goto onError;
3550 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003552 else {
3553 PyObject *temp = PyUnicode_Concat(v, s);
3554 Py_DECREF(s);
3555 Py_DECREF(v);
3556 v = temp;
3557 if (v == NULL)
3558 goto onError;
3559 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003561 }
3562 }
3563 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564
3565 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003566 Py_XDECREF(v);
3567 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568}