blob: f8c83d934c6ee409fe62e692cdc0b63b7d7759d0 [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:
Benjamin Peterson947ce582010-06-24 00:12:40 +0000443 case Set_kind:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000444 case Num_kind:
445 case Str_kind:
446 expr_name = "literal";
447 break;
448 case Compare_kind:
449 expr_name = "comparison";
450 break;
451 case Repr_kind:
452 expr_name = "repr";
453 break;
454 case IfExp_kind:
455 expr_name = "conditional expression";
456 break;
457 default:
Brett Cannonfa84d922010-05-05 20:30:30 +0000458 PyErr_Format(PyExc_SystemError,
459 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000460 e->kind, e->lineno);
461 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000463 /* Check for error string set by switch */
464 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000465 char buf[300];
466 PyOS_snprintf(buf, sizeof(buf),
467 "can't %s %s",
468 ctx == Store ? "assign to" : "delete",
469 expr_name);
470 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000471 }
472
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473 /* If the LHS is a list or tuple, we need to set the assignment
Brett Cannonfa84d922010-05-05 20:30:30 +0000474 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 */
476 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000477 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000479 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000480 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000481 return 0;
482 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 }
484 return 1;
485}
486
487static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000488ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489{
490 REQ(n, augassign);
491 n = CHILD(n, 0);
492 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000493 case '+':
494 return Add;
495 case '-':
496 return Sub;
497 case '/':
498 if (STR(n)[1] == '/')
499 return FloorDiv;
500 else
501 return Div;
502 case '%':
503 return Mod;
504 case '<':
505 return LShift;
506 case '>':
507 return RShift;
508 case '&':
509 return BitAnd;
510 case '^':
511 return BitXor;
512 case '|':
513 return BitOr;
514 case '*':
515 if (STR(n)[1] == '*')
516 return Pow;
517 else
518 return Mult;
519 default:
520 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
521 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522 }
523}
524
525static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000526ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527{
528 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000529 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 */
531 REQ(n, comp_op);
532 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000533 n = CHILD(n, 0);
534 switch (TYPE(n)) {
535 case LESS:
536 return Lt;
537 case GREATER:
538 return Gt;
539 case EQEQUAL: /* == */
540 return Eq;
541 case LESSEQUAL:
542 return LtE;
543 case GREATEREQUAL:
544 return GtE;
545 case NOTEQUAL:
546 return NotEq;
547 case NAME:
548 if (strcmp(STR(n), "in") == 0)
549 return In;
550 if (strcmp(STR(n), "is") == 0)
551 return Is;
552 default:
553 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
554 STR(n));
555 return (cmpop_ty)0;
556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557 }
558 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000559 /* handle "not in" and "is not" */
560 switch (TYPE(CHILD(n, 0))) {
561 case NAME:
562 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
563 return NotIn;
564 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
565 return IsNot;
566 default:
567 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
568 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
569 return (cmpop_ty)0;
570 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 }
Neal Norwitz79792652005-11-14 04:25:03 +0000572 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000573 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000574 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575}
576
577static asdl_seq *
578seq_for_testlist(struct compiling *c, const node *n)
579{
580 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000581 asdl_seq *seq;
582 expr_ty expression;
583 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000584 assert(TYPE(n) == testlist ||
585 TYPE(n) == listmaker ||
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000586 TYPE(n) == testlist_comp ||
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000587 TYPE(n) == testlist_safe ||
588 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000590 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000592 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593
594 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000595 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000597 expression = ast_for_expr(c, CHILD(n, i));
598 if (!expression)
599 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000601 assert(i / 2 < seq->size);
602 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 }
604 return seq;
605}
606
607static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000608compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609{
610 int i, len = (NCH(n) + 1) / 2;
611 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000612 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000614 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615
Neal Norwitz3a230172006-09-22 08:18:10 +0000616 /* fpdef: NAME | '(' fplist ')'
617 fplist: fpdef (',' fpdef)* [',']
618 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 for (i = 0; i < len; i++) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000621 PyObject *arg_id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000622 const node *fpdef_node = CHILD(n, 2*i);
623 const node *child;
624 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000625set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000626 /* fpdef_node is either a NAME or an fplist */
627 child = CHILD(fpdef_node, 0);
628 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000629 if (!forbidden_check(c, n, STR(child)))
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000630 return NULL;
631 arg_id = NEW_IDENTIFIER(child);
632 if (!arg_id)
633 return NULL;
634 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
635 c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000636 }
637 else {
638 assert(TYPE(fpdef_node) == fpdef);
639 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
640 child = CHILD(fpdef_node, 1);
641 assert(TYPE(child) == fplist);
642 /* NCH == 1 means we have (x), we need to elide the extra parens */
643 if (NCH(child) == 1) {
644 fpdef_node = CHILD(child, 0);
645 assert(TYPE(fpdef_node) == fpdef);
646 goto set_name;
647 }
648 arg = compiler_complex_args(c, child);
649 }
650 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 }
652
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000653 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000654 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 return result;
657}
658
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659
Jeremy Hyltona8293132006-02-28 17:58:27 +0000660/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661
662static arguments_ty
663ast_for_arguments(struct compiling *c, const node *n)
664{
665 /* parameters: '(' [varargslist] ')'
666 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000667 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000669 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 asdl_seq *args, *defaults;
671 identifier vararg = NULL, kwarg = NULL;
672 node *ch;
673
674 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000675 if (NCH(n) == 2) /* () as argument list */
676 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
677 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 }
679 REQ(n, varargslist);
680
681 /* first count the number of normal args & defaults */
682 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000683 ch = CHILD(n, i);
684 if (TYPE(ch) == fpdef)
685 n_args++;
686 if (TYPE(ch) == EQUAL)
687 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000689 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000691 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000692 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000694 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695
696 /* fpdef: NAME | '(' fplist ')'
697 fplist: fpdef (',' fpdef)* [',']
698 */
699 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000700 j = 0; /* index for defaults */
701 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000703 ch = CHILD(n, i);
704 switch (TYPE(ch)) {
Benjamin Peterson99a50232009-11-19 22:54:57 +0000705 case fpdef: {
706 int complex_args = 0, parenthesized = 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000707 handle_fpdef:
708 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
709 anything other than EQUAL or a comma? */
710 /* XXX Should NCH(n) check be made a separate check? */
711 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
712 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
713 if (!expression)
714 goto error;
715 assert(defaults != NULL);
716 asdl_seq_SET(defaults, j++, expression);
717 i += 2;
718 found_default = 1;
719 }
720 else if (found_default) {
Benjamin Peterson99a50232009-11-19 22:54:57 +0000721 /* def f((x)=4): pass should raise an error.
722 def f((x, (y))): pass will just incur the tuple unpacking warning. */
723 if (parenthesized && !complex_args) {
724 ast_error(n, "parenthesized arg with default");
725 goto error;
726 }
Brett Cannonfa84d922010-05-05 20:30:30 +0000727 ast_error(n,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000728 "non-default argument follows default argument");
729 goto error;
730 }
731 if (NCH(ch) == 3) {
732 ch = CHILD(ch, 1);
733 /* def foo((x)): is not complex, special case. */
734 if (NCH(ch) != 1) {
735 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000736 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
737 "tuple parameter unpacking has been removed in 3.x"))
738 goto error;
Benjamin Peterson99a50232009-11-19 22:54:57 +0000739 complex_args = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000740 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000741 if (!asdl_seq_GET(args, k-1))
742 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000743 } else {
744 /* def foo((x)): setup for checking NAME below. */
745 /* Loop because there can be many parens and tuple
746 unpacking mixed in. */
Benjamin Peterson99a50232009-11-19 22:54:57 +0000747 parenthesized = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000748 ch = CHILD(ch, 0);
749 assert(TYPE(ch) == fpdef);
750 goto handle_fpdef;
751 }
752 }
753 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000754 PyObject *id;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000755 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000756 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000757 goto error;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000758 id = NEW_IDENTIFIER(CHILD(ch, 0));
759 if (!id)
760 goto error;
761 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000762 c->c_arena);
763 if (!name)
764 goto error;
765 asdl_seq_SET(args, k++, name);
Brett Cannonfa84d922010-05-05 20:30:30 +0000766
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000767 }
768 i += 2; /* the name and the comma */
Benjamin Peterson99a50232009-11-19 22:54:57 +0000769 if (parenthesized && Py_Py3kWarningFlag &&
770 !ast_warn(c, ch, "parenthesized argument names "
771 "are invalid in 3.x"))
772 goto error;
773
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000774 break;
Benjamin Peterson99a50232009-11-19 22:54:57 +0000775 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000776 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000777 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000778 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000779 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000780 if (!vararg)
781 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000782 i += 3;
783 break;
784 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000785 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000786 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000787 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000788 if (!kwarg)
789 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000790 i += 3;
791 break;
792 default:
793 PyErr_Format(PyExc_SystemError,
794 "unexpected node in varargslist: %d @ %d",
795 TYPE(ch), i);
796 goto error;
797 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
799
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000800 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801
802 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000803 Py_XDECREF(vararg);
804 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 return NULL;
806}
807
808static expr_ty
809ast_for_dotted_name(struct compiling *c, const node *n)
810{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000811 expr_ty e;
812 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000813 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 int i;
815
816 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000817
818 lineno = LINENO(n);
819 col_offset = n->n_col_offset;
820
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 id = NEW_IDENTIFIER(CHILD(n, 0));
822 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000823 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000824 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827
828 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000829 id = NEW_IDENTIFIER(CHILD(n, i));
830 if (!id)
831 return NULL;
832 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
833 if (!e)
834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 }
836
837 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838}
839
840static expr_ty
841ast_for_decorator(struct compiling *c, const node *n)
842{
843 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
844 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000845 expr_ty name_expr;
Brett Cannonfa84d922010-05-05 20:30:30 +0000846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000848 REQ(CHILD(n, 0), AT);
849 REQ(RCHILD(n, -1), NEWLINE);
Brett Cannonfa84d922010-05-05 20:30:30 +0000850
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
852 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000853 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +0000854
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000856 d = name_expr;
857 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
859 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000860 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
861 n->n_col_offset, c->c_arena);
862 if (!d)
863 return NULL;
864 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
866 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000867 d = ast_for_call(c, CHILD(n, 3), name_expr);
868 if (!d)
869 return NULL;
870 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 }
872
873 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874}
875
876static asdl_seq*
877ast_for_decorators(struct compiling *c, const node *n)
878{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000879 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000880 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 int i;
Brett Cannonfa84d922010-05-05 20:30:30 +0000882
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000884 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000886 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +0000887
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000889 d = ast_for_decorator(c, CHILD(n, i));
890 if (!d)
891 return NULL;
892 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893 }
894 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895}
896
897static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000898ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899{
Christian Heimes5224d282008-02-23 15:01:05 +0000900 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000901 identifier name;
902 arguments_ty args;
903 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000904 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905
906 REQ(n, funcdef);
907
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 name = NEW_IDENTIFIER(CHILD(n, name_i));
909 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000910 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000911 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000912 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913 args = ast_for_arguments(c, CHILD(n, name_i + 1));
914 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916 body = ast_for_suite(c, CHILD(n, name_i + 3));
917 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000920 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000921 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922}
923
Christian Heimes5224d282008-02-23 15:01:05 +0000924static stmt_ty
925ast_for_decorated(struct compiling *c, const node *n)
926{
927 /* decorated: decorators (classdef | funcdef) */
928 stmt_ty thing = NULL;
929 asdl_seq *decorator_seq = NULL;
930
931 REQ(n, decorated);
932
933 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
934 if (!decorator_seq)
935 return NULL;
936
937 assert(TYPE(CHILD(n, 1)) == funcdef ||
938 TYPE(CHILD(n, 1)) == classdef);
939
940 if (TYPE(CHILD(n, 1)) == funcdef) {
941 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
942 } else if (TYPE(CHILD(n, 1)) == classdef) {
943 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
944 }
945 /* we count the decorators in when talking about the class' or
946 function's line number */
947 if (thing) {
948 thing->lineno = LINENO(n);
949 thing->col_offset = n->n_col_offset;
950 }
951 return thing;
952}
953
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954static expr_ty
955ast_for_lambdef(struct compiling *c, const node *n)
956{
957 /* lambdef: 'lambda' [varargslist] ':' test */
958 arguments_ty args;
959 expr_ty expression;
960
961 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000962 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
963 if (!args)
964 return NULL;
965 expression = ast_for_expr(c, CHILD(n, 2));
966 if (!expression)
967 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968 }
969 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000970 args = ast_for_arguments(c, CHILD(n, 1));
971 if (!args)
972 return NULL;
973 expression = ast_for_expr(c, CHILD(n, 3));
974 if (!expression)
975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 }
977
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000978 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979}
980
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000981static expr_ty
982ast_for_ifexpr(struct compiling *c, const node *n)
983{
Brett Cannonfa84d922010-05-05 20:30:30 +0000984 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000985 expr_ty expression, body, orelse;
986
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000987 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000988 body = ast_for_expr(c, CHILD(n, 0));
989 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000990 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000991 expression = ast_for_expr(c, CHILD(n, 2));
992 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000993 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000994 orelse = ast_for_expr(c, CHILD(n, 4));
995 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000996 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000997 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000998 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000999}
1000
Neal Norwitze4d4f002006-09-05 03:58:26 +00001001/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
1002 so there is only a single version. Possibly for loops can also re-use
1003 the code.
1004*/
1005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006/* Count the number of 'for' loop in a list comprehension.
1007
1008 Helper for ast_for_listcomp().
1009*/
1010
1011static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001012count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013{
1014 int n_fors = 0;
1015 node *ch = CHILD(n, 1);
1016
1017 count_list_for:
1018 n_fors++;
1019 REQ(ch, list_for);
1020 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001021 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001023 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024 count_list_iter:
1025 REQ(ch, list_iter);
1026 ch = CHILD(ch, 0);
1027 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001028 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001030 if (NCH(ch) == 3) {
1031 ch = CHILD(ch, 2);
1032 goto count_list_iter;
1033 }
1034 else
1035 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001037
1038 /* Should never be reached */
1039 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1040 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041}
1042
1043/* Count the number of 'if' statements in a list comprehension.
1044
1045 Helper for ast_for_listcomp().
1046*/
1047
1048static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001049count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050{
1051 int n_ifs = 0;
1052
1053 count_list_iter:
1054 REQ(n, list_iter);
1055 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001056 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 n = CHILD(n, 0);
1058 REQ(n, list_if);
1059 n_ifs++;
1060 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001061 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 n = CHILD(n, 2);
1063 goto count_list_iter;
1064}
1065
1066static expr_ty
1067ast_for_listcomp(struct compiling *c, const node *n)
1068{
1069 /* listmaker: test ( list_for | (',' test)* [','] )
1070 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1071 list_iter: list_for | list_if
1072 list_if: 'if' test [list_iter]
1073 testlist_safe: test [(',' test)+ [',']]
1074 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001075 expr_ty elt, first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 asdl_seq *listcomps;
1077 int i, n_fors;
1078 node *ch;
1079
1080 REQ(n, listmaker);
1081 assert(NCH(n) > 1);
1082
1083 elt = ast_for_expr(c, CHILD(n, 0));
1084 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001085 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001087 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001089 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001091 listcomps = asdl_seq_new(n_fors, c->c_arena);
1092 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001093 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001094
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 ch = CHILD(n, 1);
1096 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001097 comprehension_ty lc;
1098 asdl_seq *t;
1099 expr_ty expression;
1100 node *for_ch;
Brett Cannonfa84d922010-05-05 20:30:30 +00001101
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001102 REQ(ch, list_for);
Brett Cannonfa84d922010-05-05 20:30:30 +00001103
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001104 for_ch = CHILD(ch, 1);
1105 t = ast_for_exprlist(c, for_ch, Store);
1106 if (!t)
1107 return NULL;
1108 expression = ast_for_testlist(c, CHILD(ch, 3));
1109 if (!expression)
1110 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001111
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001112 /* Check the # of children rather than the length of t, since
1113 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1114 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001115 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001116 if (NCH(for_ch) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001117 lc = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001118 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001119 lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001120 c->c_arena),
1121 expression, NULL, c->c_arena);
1122 if (!lc)
1123 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001125 if (NCH(ch) == 5) {
1126 int j, n_ifs;
1127 asdl_seq *ifs;
1128 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001130 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001131 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001132 if (n_ifs == -1)
1133 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001135 ifs = asdl_seq_new(n_ifs, c->c_arena);
1136 if (!ifs)
1137 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001139 for (j = 0; j < n_ifs; j++) {
1140 REQ(ch, list_iter);
1141 ch = CHILD(ch, 0);
1142 REQ(ch, list_if);
Brett Cannonfa84d922010-05-05 20:30:30 +00001143
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001144 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1145 if (!list_for_expr)
1146 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001147
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001148 asdl_seq_SET(ifs, j, list_for_expr);
1149 if (NCH(ch) == 3)
1150 ch = CHILD(ch, 2);
1151 }
1152 /* on exit, must guarantee that ch is a list_for */
1153 if (TYPE(ch) == list_iter)
1154 ch = CHILD(ch, 0);
1155 lc->ifs = ifs;
1156 }
1157 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 }
1159
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001160 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161}
1162
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001163/*
1164 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001166 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167*/
1168
1169static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001170count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001172 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001174 count_comp_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001175 n_fors++;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001176 REQ(n, comp_for);
1177 if (NCH(n) == 5)
1178 n = CHILD(n, 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001179 else
1180 return n_fors;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001181 count_comp_iter:
1182 REQ(n, comp_iter);
1183 n = CHILD(n, 0);
1184 if (TYPE(n) == comp_for)
1185 goto count_comp_for;
1186 else if (TYPE(n) == comp_if) {
1187 if (NCH(n) == 3) {
1188 n = CHILD(n, 2);
1189 goto count_comp_iter;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001190 }
1191 else
1192 return n_fors;
1193 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001194
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001195 /* Should never be reached */
1196 PyErr_SetString(PyExc_SystemError,
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001197 "logic error in count_comp_fors");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001198 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199}
1200
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001201/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001203 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204*/
1205
1206static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001207count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001209 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001211 while (1) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001212 REQ(n, comp_iter);
1213 if (TYPE(CHILD(n, 0)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001214 return n_ifs;
1215 n = CHILD(n, 0);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001216 REQ(n, comp_if);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001217 n_ifs++;
1218 if (NCH(n) == 2)
1219 return n_ifs;
1220 n = CHILD(n, 2);
1221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222}
1223
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001224static asdl_seq *
1225ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 int i, n_fors;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001228 asdl_seq *comps;
1229
1230 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001232 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001233
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001234 comps = asdl_seq_new(n_fors, c->c_arena);
1235 if (!comps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001236 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001237
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 for (i = 0; i < n_fors; i++) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001239 comprehension_ty comp;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001240 asdl_seq *t;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001241 expr_ty expression, first;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001242 node *for_ch;
Brett Cannonfa84d922010-05-05 20:30:30 +00001243
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001244 REQ(n, comp_for);
Brett Cannonfa84d922010-05-05 20:30:30 +00001245
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001246 for_ch = CHILD(n, 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001247 t = ast_for_exprlist(c, for_ch, Store);
1248 if (!t)
1249 return NULL;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001250 expression = ast_for_expr(c, CHILD(n, 3));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001251 if (!expression)
1252 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001253
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001254 /* Check the # of children rather than the length of t, since
1255 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001256 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001257 if (NCH(for_ch) == 1)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001258 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001259 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001260 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001261 c->c_arena),
1262 expression, NULL, c->c_arena);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001263 if (!comp)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001264 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001265
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001266 if (NCH(n) == 5) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001267 int j, n_ifs;
1268 asdl_seq *ifs;
Brett Cannonfa84d922010-05-05 20:30:30 +00001269
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001270 n = CHILD(n, 4);
1271 n_ifs = count_comp_ifs(c, n);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001272 if (n_ifs == -1)
1273 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001274
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001275 ifs = asdl_seq_new(n_ifs, c->c_arena);
1276 if (!ifs)
1277 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001278
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001279 for (j = 0; j < n_ifs; j++) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001280 REQ(n, comp_iter);
1281 n = CHILD(n, 0);
1282 REQ(n, comp_if);
Brett Cannonfa84d922010-05-05 20:30:30 +00001283
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001284 expression = ast_for_expr(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001285 if (!expression)
1286 return NULL;
1287 asdl_seq_SET(ifs, j, expression);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001288 if (NCH(n) == 3)
1289 n = CHILD(n, 2);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001290 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001291 /* on exit, must guarantee that n is a comp_for */
1292 if (TYPE(n) == comp_iter)
1293 n = CHILD(n, 0);
1294 comp->ifs = ifs;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001295 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001296 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001298 return comps;
1299}
1300
1301static expr_ty
1302ast_for_itercomp(struct compiling *c, const node *n, int type)
1303{
1304 expr_ty elt;
1305 asdl_seq *comps;
Brett Cannonfa84d922010-05-05 20:30:30 +00001306
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001307 assert(NCH(n) > 1);
Brett Cannonfa84d922010-05-05 20:30:30 +00001308
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001309 elt = ast_for_expr(c, CHILD(n, 0));
1310 if (!elt)
1311 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001312
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001313 comps = ast_for_comprehension(c, CHILD(n, 1));
1314 if (!comps)
1315 return NULL;
1316
1317 if (type == COMP_GENEXP)
1318 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1319 else if (type == COMP_SETCOMP)
1320 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1321 else
1322 /* Should never happen */
1323 return NULL;
1324}
1325
1326static expr_ty
1327ast_for_dictcomp(struct compiling *c, const node *n)
1328{
1329 expr_ty key, value;
1330 asdl_seq *comps;
Brett Cannonfa84d922010-05-05 20:30:30 +00001331
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001332 assert(NCH(n) > 3);
1333 REQ(CHILD(n, 1), COLON);
Brett Cannonfa84d922010-05-05 20:30:30 +00001334
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001335 key = ast_for_expr(c, CHILD(n, 0));
1336 if (!key)
1337 return NULL;
1338
1339 value = ast_for_expr(c, CHILD(n, 2));
1340 if (!value)
1341 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001342
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001343 comps = ast_for_comprehension(c, CHILD(n, 3));
1344 if (!comps)
1345 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001346
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001347 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1348}
1349
1350static expr_ty
1351ast_for_genexp(struct compiling *c, const node *n)
1352{
1353 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
1354 return ast_for_itercomp(c, n, COMP_GENEXP);
1355}
1356
1357static expr_ty
1358ast_for_setcomp(struct compiling *c, const node *n)
1359{
1360 assert(TYPE(n) == (dictorsetmaker));
1361 return ast_for_itercomp(c, n, COMP_SETCOMP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362}
1363
1364static expr_ty
1365ast_for_atom(struct compiling *c, const node *n)
1366{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001367 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1369 */
1370 node *ch = CHILD(n, 0);
Brett Cannonfa84d922010-05-05 20:30:30 +00001371
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 switch (TYPE(ch)) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001373 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001374 /* All names start in Load context, but may later be
1375 changed. */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001376 PyObject *name = NEW_IDENTIFIER(ch);
1377 if (!name)
1378 return NULL;
1379 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001382 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001383 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001384#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001385 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1386 PyObject *type, *value, *tback, *errstr;
1387 PyErr_Fetch(&type, &value, &tback);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001388 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001389 if (errstr) {
1390 char *s = "";
1391 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001392 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001393 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1394 ast_error(n, buf);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001395 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001396 } else {
1397 ast_error(n, "(unicode error) unknown error");
1398 }
1399 Py_DECREF(type);
1400 Py_DECREF(value);
1401 Py_XDECREF(tback);
1402 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001403#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001404 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001405 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001406 PyArena_AddPyObject(c->c_arena, str);
1407 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 }
1409 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001410 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001411 if (!pynum)
1412 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001413
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001414 PyArena_AddPyObject(c->c_arena, pynum);
1415 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 }
1417 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001418 ch = CHILD(n, 1);
Brett Cannonfa84d922010-05-05 20:30:30 +00001419
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001420 if (TYPE(ch) == RPAR)
1421 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Brett Cannonfa84d922010-05-05 20:30:30 +00001422
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001423 if (TYPE(ch) == yield_expr)
1424 return ast_for_expr(c, ch);
Brett Cannonfa84d922010-05-05 20:30:30 +00001425
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001426 return ast_for_testlist_comp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001428 ch = CHILD(n, 1);
Brett Cannonfa84d922010-05-05 20:30:30 +00001429
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001430 if (TYPE(ch) == RSQB)
1431 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Brett Cannonfa84d922010-05-05 20:30:30 +00001432
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001433 REQ(ch, listmaker);
1434 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1435 asdl_seq *elts = seq_for_testlist(c, ch);
1436 if (!elts)
1437 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001438
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001439 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1440 }
1441 else
1442 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 case LBRACE: {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001444 /* dictorsetmaker:
1445 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
1446 * (test (comp_for | (',' test)* [',']))
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001447 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001448 int i, size;
1449 asdl_seq *keys, *values;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001450
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001451 ch = CHILD(n, 1);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001452 if (TYPE(ch) == RBRACE) {
1453 /* it's an empty dict */
1454 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
1455 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1456 /* it's a simple set */
1457 asdl_seq *elts;
1458 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
1459 elts = asdl_seq_new(size, c->c_arena);
1460 if (!elts)
1461 return NULL;
1462 for (i = 0; i < NCH(ch); i += 2) {
1463 expr_ty expression;
1464 expression = ast_for_expr(c, CHILD(ch, i));
1465 if (!expression)
1466 return NULL;
1467 asdl_seq_SET(elts, i / 2, expression);
1468 }
1469 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001470 } else if (TYPE(CHILD(ch, 1)) == comp_for) {
1471 /* it's a set comprehension */
1472 return ast_for_setcomp(c, ch);
1473 } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {
1474 return ast_for_dictcomp(c, ch);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001475 } else {
1476 /* it's a dict */
1477 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1478 keys = asdl_seq_new(size, c->c_arena);
1479 if (!keys)
1480 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001481
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001482 values = asdl_seq_new(size, c->c_arena);
1483 if (!values)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001484 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001485
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001486 for (i = 0; i < NCH(ch); i += 4) {
1487 expr_ty expression;
Brett Cannonfa84d922010-05-05 20:30:30 +00001488
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001489 expression = ast_for_expr(c, CHILD(ch, i));
1490 if (!expression)
1491 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001492
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001493 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001494
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001495 expression = ast_for_expr(c, CHILD(ch, i + 2));
1496 if (!expression)
1497 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001498
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001499 asdl_seq_SET(values, i / 4, expression);
1500 }
1501 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001502 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 }
1504 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001505 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001506 if (Py_Py3kWarningFlag &&
1507 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001508 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001509 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001510 if (!expression)
1511 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001512
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001513 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 }
1515 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001516 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1517 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 }
1519}
1520
1521static slice_ty
1522ast_for_slice(struct compiling *c, const node *n)
1523{
1524 node *ch;
1525 expr_ty lower = NULL, upper = NULL, step = NULL;
1526
1527 REQ(n, subscript);
1528
1529 /*
1530 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1531 sliceop: ':' [test]
1532 */
1533 ch = CHILD(n, 0);
1534 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001535 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536
1537 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001538 /* 'step' variable hold no significance in terms of being used over
1539 other vars */
Brett Cannonfa84d922010-05-05 20:30:30 +00001540 step = ast_for_expr(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001541 if (!step)
1542 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001543
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001544 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 }
1546
1547 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001548 lower = ast_for_expr(c, ch);
1549 if (!lower)
1550 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 }
1552
1553 /* If there's an upper bound it's in the second or third position. */
1554 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001555 if (NCH(n) > 1) {
1556 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001558 if (TYPE(n2) == test) {
1559 upper = ast_for_expr(c, n2);
1560 if (!upper)
1561 return NULL;
1562 }
1563 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001565 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001567 if (TYPE(n2) == test) {
1568 upper = ast_for_expr(c, n2);
1569 if (!upper)
1570 return NULL;
1571 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 }
1573
1574 ch = CHILD(n, NCH(n) - 1);
1575 if (TYPE(ch) == sliceop) {
Benjamin Peterson4879c902009-07-20 20:28:08 +00001576 if (NCH(ch) == 1) {
Brett Cannonfa84d922010-05-05 20:30:30 +00001577 /*
Benjamin Peterson4879c902009-07-20 20:28:08 +00001578 This is an extended slice (ie "x[::]") with no expression in the
1579 step field. We set this literally to "None" in order to
1580 disambiguate it from x[:]. (The interpreter might have to call
1581 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1582 */
1583 identifier none = new_identifier("None", c->c_arena);
1584 if (!none)
1585 return NULL;
1586 ch = CHILD(ch, 0);
1587 step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);
1588 if (!step)
1589 return NULL;
1590 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001591 ch = CHILD(ch, 1);
1592 if (TYPE(ch) == test) {
1593 step = ast_for_expr(c, ch);
1594 if (!step)
1595 return NULL;
1596 }
1597 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 }
1599
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001600 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601}
1602
1603static expr_ty
1604ast_for_binop(struct compiling *c, const node *n)
1605{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001606 /* Must account for a sequence of expressions.
Brett Cannonfa84d922010-05-05 20:30:30 +00001607 How should A op B op C by represented?
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001608 BinOp(BinOp(A, op, B), op, C).
1609 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001611 int i, nops;
1612 expr_ty expr1, expr2, result;
1613 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001615 expr1 = ast_for_expr(c, CHILD(n, 0));
1616 if (!expr1)
1617 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001619 expr2 = ast_for_expr(c, CHILD(n, 2));
1620 if (!expr2)
1621 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001623 newoperator = get_operator(CHILD(n, 1));
1624 if (!newoperator)
1625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001627 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1628 c->c_arena);
1629 if (!result)
1630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001632 nops = (NCH(n) - 1) / 2;
1633 for (i = 1; i < nops; i++) {
1634 expr_ty tmp_result, tmp;
1635 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001637 newoperator = get_operator(next_oper);
1638 if (!newoperator)
1639 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001641 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1642 if (!tmp)
1643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644
Brett Cannonfa84d922010-05-05 20:30:30 +00001645 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001646 LINENO(next_oper), next_oper->n_col_offset,
1647 c->c_arena);
Brett Cannonfa84d922010-05-05 20:30:30 +00001648 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001649 return NULL;
1650 result = tmp_result;
1651 }
1652 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653}
1654
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001655static expr_ty
1656ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1657{
Brett Cannonfa84d922010-05-05 20:30:30 +00001658 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001659 subscriptlist: subscript (',' subscript)* [',']
1660 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1661 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001662 REQ(n, trailer);
1663 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001664 if (NCH(n) == 2)
1665 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1666 n->n_col_offset, c->c_arena);
1667 else
1668 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001669 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001670 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001671 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1672 if (!attr_id)
1673 return NULL;
1674 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001675 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001676 }
1677 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001678 REQ(CHILD(n, 0), LSQB);
1679 REQ(CHILD(n, 2), RSQB);
1680 n = CHILD(n, 1);
1681 if (NCH(n) == 1) {
1682 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1683 if (!slc)
1684 return NULL;
1685 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1686 c->c_arena);
1687 }
1688 else {
Brett Cannonfa84d922010-05-05 20:30:30 +00001689 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001690 by treating the sequence as a tuple literal if there are
1691 no slice features.
1692 */
1693 int j;
1694 slice_ty slc;
1695 expr_ty e;
1696 bool simple = true;
1697 asdl_seq *slices, *elts;
1698 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1699 if (!slices)
1700 return NULL;
1701 for (j = 0; j < NCH(n); j += 2) {
1702 slc = ast_for_slice(c, CHILD(n, j));
1703 if (!slc)
1704 return NULL;
1705 if (slc->kind != Index_kind)
1706 simple = false;
1707 asdl_seq_SET(slices, j / 2, slc);
1708 }
1709 if (!simple) {
1710 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1711 Load, LINENO(n), n->n_col_offset, c->c_arena);
1712 }
1713 /* extract Index values and put them in a Tuple */
1714 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1715 if (!elts)
1716 return NULL;
1717 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1718 slc = (slice_ty)asdl_seq_GET(slices, j);
1719 assert(slc->kind == Index_kind && slc->v.Index.value);
1720 asdl_seq_SET(elts, j, slc->v.Index.value);
1721 }
1722 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1723 if (!e)
1724 return NULL;
1725 return Subscript(left_expr, Index(e, c->c_arena),
1726 Load, LINENO(n), n->n_col_offset, c->c_arena);
1727 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001728 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001729}
1730
1731static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001732ast_for_factor(struct compiling *c, const node *n)
1733{
1734 node *pfactor, *ppower, *patom, *pnum;
1735 expr_ty expression;
1736
1737 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001738 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001739 constant. The peephole optimizer already does something like
1740 this but it doesn't handle the case where the constant is
1741 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1742 PyLongObject.
1743 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001744 if (TYPE(CHILD(n, 0)) == MINUS &&
1745 NCH(n) == 2 &&
1746 TYPE((pfactor = CHILD(n, 1))) == factor &&
1747 NCH(pfactor) == 1 &&
1748 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1749 NCH(ppower) == 1 &&
1750 TYPE((patom = CHILD(ppower, 0))) == atom &&
1751 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1752 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1753 if (s == NULL)
1754 return NULL;
1755 s[0] = '-';
1756 strcpy(s + 1, STR(pnum));
1757 PyObject_FREE(STR(pnum));
1758 STR(pnum) = s;
1759 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001760 }
1761
1762 expression = ast_for_expr(c, CHILD(n, 1));
1763 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001764 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001765
1766 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001767 case PLUS:
1768 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1769 c->c_arena);
1770 case MINUS:
1771 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1772 c->c_arena);
1773 case TILDE:
1774 return UnaryOp(Invert, expression, LINENO(n),
1775 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001776 }
1777 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001778 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001779 return NULL;
1780}
1781
1782static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001783ast_for_power(struct compiling *c, const node *n)
1784{
1785 /* power: atom trailer* ('**' factor)*
1786 */
1787 int i;
1788 expr_ty e, tmp;
1789 REQ(n, power);
1790 e = ast_for_atom(c, CHILD(n, 0));
1791 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001792 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001793 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001794 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001795 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001796 node *ch = CHILD(n, i);
1797 if (TYPE(ch) != trailer)
1798 break;
1799 tmp = ast_for_trailer(c, ch, e);
1800 if (!tmp)
1801 return NULL;
1802 tmp->lineno = e->lineno;
1803 tmp->col_offset = e->col_offset;
1804 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001805 }
1806 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001807 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1808 if (!f)
1809 return NULL;
1810 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1811 if (!tmp)
1812 return NULL;
1813 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001814 }
1815 return e;
1816}
1817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818/* Do not name a variable 'expr'! Will cause a compile error.
1819*/
1820
1821static expr_ty
1822ast_for_expr(struct compiling *c, const node *n)
1823{
1824 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001825 test: or_test ['if' or_test 'else' test] | lambdef
Brett Cannonfa84d922010-05-05 20:30:30 +00001826 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 and_test: not_test ('and' not_test)*
1828 not_test: 'not' not_test | comparison
1829 comparison: expr (comp_op expr)*
1830 expr: xor_expr ('|' xor_expr)*
1831 xor_expr: and_expr ('^' and_expr)*
1832 and_expr: shift_expr ('&' shift_expr)*
1833 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1834 arith_expr: term (('+'|'-') term)*
1835 term: factor (('*'|'/'|'%'|'//') factor)*
1836 factor: ('+'|'-'|'~') factor | power
1837 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001838
1839 As well as modified versions that exist for backward compatibility,
1840 to explicitly allow:
1841 [ x for x in lambda: 0, lambda: 1 ]
1842 (which would be ambiguous without these extra rules)
Brett Cannonfa84d922010-05-05 20:30:30 +00001843
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001844 old_test: or_test | old_lambdef
1845 old_lambdef: 'lambda' [vararglist] ':' old_test
1846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 */
1848
1849 asdl_seq *seq;
1850 int i;
1851
1852 loop:
1853 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001854 case test:
1855 case old_test:
1856 if (TYPE(CHILD(n, 0)) == lambdef ||
1857 TYPE(CHILD(n, 0)) == old_lambdef)
1858 return ast_for_lambdef(c, CHILD(n, 0));
1859 else if (NCH(n) > 1)
1860 return ast_for_ifexpr(c, n);
1861 /* Fallthrough */
1862 case or_test:
1863 case and_test:
1864 if (NCH(n) == 1) {
1865 n = CHILD(n, 0);
1866 goto loop;
1867 }
1868 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1869 if (!seq)
1870 return NULL;
1871 for (i = 0; i < NCH(n); i += 2) {
1872 expr_ty e = ast_for_expr(c, CHILD(n, i));
1873 if (!e)
1874 return NULL;
1875 asdl_seq_SET(seq, i / 2, e);
1876 }
1877 if (!strcmp(STR(CHILD(n, 1)), "and"))
1878 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1879 c->c_arena);
1880 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1881 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1882 case not_test:
1883 if (NCH(n) == 1) {
1884 n = CHILD(n, 0);
1885 goto loop;
1886 }
1887 else {
1888 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1889 if (!expression)
1890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001892 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1893 c->c_arena);
1894 }
1895 case comparison:
1896 if (NCH(n) == 1) {
1897 n = CHILD(n, 0);
1898 goto loop;
1899 }
1900 else {
1901 expr_ty expression;
1902 asdl_int_seq *ops;
1903 asdl_seq *cmps;
1904 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1905 if (!ops)
1906 return NULL;
1907 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1908 if (!cmps) {
1909 return NULL;
1910 }
1911 for (i = 1; i < NCH(n); i += 2) {
1912 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001914 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001915 if (!newoperator) {
1916 return NULL;
1917 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001919 expression = ast_for_expr(c, CHILD(n, i + 1));
1920 if (!expression) {
1921 return NULL;
1922 }
Brett Cannonfa84d922010-05-05 20:30:30 +00001923
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001924 asdl_seq_SET(ops, i / 2, newoperator);
1925 asdl_seq_SET(cmps, i / 2, expression);
1926 }
1927 expression = ast_for_expr(c, CHILD(n, 0));
1928 if (!expression) {
1929 return NULL;
1930 }
Brett Cannonfa84d922010-05-05 20:30:30 +00001931
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001932 return Compare(expression, ops, cmps, LINENO(n),
1933 n->n_col_offset, c->c_arena);
1934 }
1935 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001937 /* The next five cases all handle BinOps. The main body of code
1938 is the same in each case, but the switch turned inside out to
1939 reuse the code for each type of operator.
1940 */
1941 case expr:
1942 case xor_expr:
1943 case and_expr:
1944 case shift_expr:
1945 case arith_expr:
1946 case term:
1947 if (NCH(n) == 1) {
1948 n = CHILD(n, 0);
1949 goto loop;
1950 }
1951 return ast_for_binop(c, n);
1952 case yield_expr: {
1953 expr_ty exp = NULL;
1954 if (NCH(n) == 2) {
1955 exp = ast_for_testlist(c, CHILD(n, 1));
1956 if (!exp)
1957 return NULL;
1958 }
1959 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1960 }
1961 case factor:
1962 if (NCH(n) == 1) {
1963 n = CHILD(n, 0);
1964 goto loop;
1965 }
1966 return ast_for_factor(c, n);
1967 case power:
1968 return ast_for_power(c, n);
1969 default:
1970 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1971 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001973 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 return NULL;
1975}
1976
1977static expr_ty
1978ast_for_call(struct compiling *c, const node *n, expr_ty func)
1979{
1980 /*
1981 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001982 | '**' test)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001983 argument: [test '='] test [comp_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 */
1985
1986 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001987 asdl_seq *args;
1988 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 expr_ty vararg = NULL, kwarg = NULL;
1990
1991 REQ(n, arglist);
1992
1993 nargs = 0;
1994 nkeywords = 0;
1995 ngens = 0;
1996 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001997 node *ch = CHILD(n, i);
1998 if (TYPE(ch) == argument) {
1999 if (NCH(ch) == 1)
2000 nargs++;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002001 else if (TYPE(CHILD(ch, 1)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002002 ngens++;
2003 else
2004 nkeywords++;
2005 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006 }
2007 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002008 ast_error(n, "Generator expression must be parenthesized "
2009 "if not sole argument");
2010 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 }
2012
2013 if (nargs + nkeywords + ngens > 255) {
2014 ast_error(n, "more than 255 arguments");
2015 return NULL;
2016 }
2017
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002018 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002020 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002021 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002023 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 nargs = 0;
2025 nkeywords = 0;
2026 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002027 node *ch = CHILD(n, i);
2028 if (TYPE(ch) == argument) {
2029 expr_ty e;
2030 if (NCH(ch) == 1) {
2031 if (nkeywords) {
2032 ast_error(CHILD(ch, 0),
2033 "non-keyword arg after keyword arg");
2034 return NULL;
2035 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00002036 if (vararg) {
2037 ast_error(CHILD(ch, 0),
2038 "only named arguments may follow *expression");
2039 return NULL;
2040 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002041 e = ast_for_expr(c, CHILD(ch, 0));
2042 if (!e)
2043 return NULL;
2044 asdl_seq_SET(args, nargs++, e);
Brett Cannonfa84d922010-05-05 20:30:30 +00002045 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002046 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002047 e = ast_for_genexp(c, ch);
2048 if (!e)
2049 return NULL;
2050 asdl_seq_SET(args, nargs++, e);
2051 }
2052 else {
2053 keyword_ty kw;
2054 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00002055 int k;
2056 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057
Brett Cannonfa84d922010-05-05 20:30:30 +00002058 /* CHILD(ch, 0) is test, but must be an identifier? */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002059 e = ast_for_expr(c, CHILD(ch, 0));
2060 if (!e)
2061 return NULL;
2062 /* f(lambda x: x[0] = 3) ends up getting parsed with
2063 * LHS test = lambda x: x[0], and RHS test = 3.
2064 * SF bug 132313 points out that complaining about a keyword
2065 * then is very confusing.
2066 */
2067 if (e->kind == Lambda_kind) {
2068 ast_error(CHILD(ch, 0),
2069 "lambda cannot contain assignment");
2070 return NULL;
2071 } else if (e->kind != Name_kind) {
2072 ast_error(CHILD(ch, 0), "keyword can't be an expression");
2073 return NULL;
2074 }
2075 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00002076 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00002077 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00002078 for (k = 0; k < nkeywords; k++) {
2079 tmp = PyString_AS_STRING(
2080 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
2081 if (!strcmp(tmp, PyString_AS_STRING(key))) {
2082 ast_error(CHILD(ch, 0), "keyword argument repeated");
2083 return NULL;
2084 }
2085 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002086 e = ast_for_expr(c, CHILD(ch, 2));
2087 if (!e)
2088 return NULL;
2089 kw = keyword(key, e, c->c_arena);
2090 if (!kw)
2091 return NULL;
2092 asdl_seq_SET(keywords, nkeywords++, kw);
2093 }
2094 }
2095 else if (TYPE(ch) == STAR) {
2096 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002097 if (!vararg)
2098 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002099 i++;
2100 }
2101 else if (TYPE(ch) == DOUBLESTAR) {
2102 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002103 if (!kwarg)
2104 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002105 i++;
2106 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 }
2108
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002109 return Call(func, args, keywords, vararg, kwarg, func->lineno,
2110 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111}
2112
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002114ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002116 /* testlist_comp: test (',' test)* [','] */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002117 /* testlist: test (',' test)* [','] */
2118 /* testlist_safe: test (',' test)+ [','] */
2119 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 assert(NCH(n) > 0);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002121 if (TYPE(n) == testlist_comp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002122 if (NCH(n) > 1)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002123 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002124 }
2125 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002126 assert(TYPE(n) == testlist ||
2127 TYPE(n) == testlist_safe ||
2128 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002129 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002131 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002133 asdl_seq *tmp = seq_for_testlist(c, n);
2134 if (!tmp)
2135 return NULL;
2136 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002138}
2139
2140static expr_ty
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002141ast_for_testlist_comp(struct compiling *c, const node* n)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002142{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002143 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
2144 /* argument: test [ comp_for ] */
2145 assert(TYPE(n) == testlist_comp || TYPE(n) == argument);
2146 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002147 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002148 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002149}
2150
2151/* like ast_for_testlist() but returns a sequence */
2152static asdl_seq*
2153ast_for_class_bases(struct compiling *c, const node* n)
2154{
2155 /* testlist: test (',' test)* [','] */
2156 assert(NCH(n) > 0);
2157 REQ(n, testlist);
2158 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002159 expr_ty base;
2160 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2161 if (!bases)
2162 return NULL;
2163 base = ast_for_expr(c, CHILD(n, 0));
2164 if (!base)
2165 return NULL;
2166 asdl_seq_SET(bases, 0, base);
2167 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002168 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002169
2170 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171}
2172
2173static stmt_ty
2174ast_for_expr_stmt(struct compiling *c, const node *n)
2175{
2176 REQ(n, expr_stmt);
Brett Cannonfa84d922010-05-05 20:30:30 +00002177 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002178 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 testlist: test (',' test)* [',']
2180 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002181 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 test: ... here starts the operator precendence dance
2183 */
2184
2185 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002186 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2187 if (!e)
2188 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002190 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 }
2192 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002193 expr_ty expr1, expr2;
2194 operator_ty newoperator;
2195 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002197 expr1 = ast_for_testlist(c, ch);
2198 if (!expr1)
2199 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002200 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002201 return NULL;
Benjamin Peterson7adbb5a2009-10-03 20:23:24 +00002202 /* set_context checks that most expressions are not the left side.
2203 Augmented assignments can only have a name, a subscript, or an
2204 attribute on the left, though, so we have to explicitly check for
2205 those. */
2206 switch (expr1->kind) {
2207 case Name_kind:
2208 case Attribute_kind:
2209 case Subscript_kind:
2210 break;
2211 default:
2212 ast_error(ch, "illegal expression for augmented assignment");
2213 return NULL;
2214 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002216 ch = CHILD(n, 2);
2217 if (TYPE(ch) == testlist)
2218 expr2 = ast_for_testlist(c, ch);
2219 else
2220 expr2 = ast_for_expr(c, ch);
2221 if (!expr2)
2222 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002224 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002225 if (!newoperator)
2226 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002228 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2229 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 }
2231 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002232 int i;
2233 asdl_seq *targets;
2234 node *value;
2235 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002237 /* a normal assignment */
2238 REQ(CHILD(n, 1), EQUAL);
2239 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2240 if (!targets)
2241 return NULL;
2242 for (i = 0; i < NCH(n) - 2; i += 2) {
2243 expr_ty e;
2244 node *ch = CHILD(n, i);
Benjamin Petersonb2664812009-06-11 17:49:38 +00002245 if (TYPE(ch) == yield_expr) {
2246 ast_error(ch, "assignment to yield expression not possible");
2247 return NULL;
2248 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002249 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002251 /* set context to assign */
Brett Cannonfa84d922010-05-05 20:30:30 +00002252 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002253 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002255 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002256 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002258 asdl_seq_SET(targets, i / 2, e);
2259 }
2260 value = CHILD(n, NCH(n) - 1);
2261 if (TYPE(value) == testlist)
2262 expression = ast_for_testlist(c, value);
2263 else
2264 expression = ast_for_expr(c, value);
2265 if (!expression)
2266 return NULL;
2267 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2268 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270}
2271
2272static stmt_ty
2273ast_for_print_stmt(struct compiling *c, const node *n)
2274{
2275 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002276 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 */
2278 expr_ty dest = NULL, expression;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002279 asdl_seq *seq = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 bool nl;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002281 int i, j, values_count, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282
2283 REQ(n, print_stmt);
2284 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002285 dest = ast_for_expr(c, CHILD(n, 2));
2286 if (!dest)
2287 return NULL;
2288 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 }
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002290 values_count = (NCH(n) + 1 - start) / 2;
2291 if (values_count) {
2292 seq = asdl_seq_new(values_count, c->c_arena);
2293 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002294 return NULL;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002295 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
2296 expression = ast_for_expr(c, CHILD(n, i));
2297 if (!expression)
2298 return NULL;
2299 asdl_seq_SET(seq, j, expression);
2300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 }
2302 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002303 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304}
2305
2306static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002307ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308{
2309 asdl_seq *seq;
2310 int i;
2311 expr_ty e;
2312
2313 REQ(n, exprlist);
2314
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002315 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002319 e = ast_for_expr(c, CHILD(n, i));
2320 if (!e)
2321 return NULL;
2322 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002323 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 }
2326 return seq;
2327}
2328
2329static stmt_ty
2330ast_for_del_stmt(struct compiling *c, const node *n)
2331{
2332 asdl_seq *expr_list;
Brett Cannonfa84d922010-05-05 20:30:30 +00002333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 /* del_stmt: 'del' exprlist */
2335 REQ(n, del_stmt);
2336
2337 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2338 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002339 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002340 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341}
2342
2343static stmt_ty
2344ast_for_flow_stmt(struct compiling *c, const node *n)
2345{
2346 /*
2347 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002348 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 break_stmt: 'break'
2350 continue_stmt: 'continue'
2351 return_stmt: 'return' [testlist]
2352 yield_stmt: yield_expr
2353 yield_expr: 'yield' testlist
2354 raise_stmt: 'raise' [test [',' test [',' test]]]
2355 */
2356 node *ch;
2357
2358 REQ(n, flow_stmt);
2359 ch = CHILD(n, 0);
2360 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002361 case break_stmt:
2362 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2363 case continue_stmt:
2364 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2365 case yield_stmt: { /* will reduce to yield_expr */
2366 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2367 if (!exp)
2368 return NULL;
2369 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2370 }
2371 case return_stmt:
2372 if (NCH(ch) == 1)
2373 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2374 else {
2375 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2376 if (!expression)
2377 return NULL;
2378 return Return(expression, LINENO(n), n->n_col_offset,
2379 c->c_arena);
2380 }
2381 case raise_stmt:
2382 if (NCH(ch) == 1)
2383 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2384 c->c_arena);
2385 else if (NCH(ch) == 2) {
2386 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2387 if (!expression)
2388 return NULL;
2389 return Raise(expression, NULL, NULL, LINENO(n),
2390 n->n_col_offset, c->c_arena);
2391 }
2392 else if (NCH(ch) == 4) {
2393 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002395 expr1 = ast_for_expr(c, CHILD(ch, 1));
2396 if (!expr1)
2397 return NULL;
2398 expr2 = ast_for_expr(c, CHILD(ch, 3));
2399 if (!expr2)
2400 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002402 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2403 c->c_arena);
2404 }
2405 else if (NCH(ch) == 6) {
2406 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002408 expr1 = ast_for_expr(c, CHILD(ch, 1));
2409 if (!expr1)
2410 return NULL;
2411 expr2 = ast_for_expr(c, CHILD(ch, 3));
2412 if (!expr2)
2413 return NULL;
2414 expr3 = ast_for_expr(c, CHILD(ch, 5));
2415 if (!expr3)
2416 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002417
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002418 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2419 c->c_arena);
2420 }
2421 default:
2422 PyErr_Format(PyExc_SystemError,
2423 "unexpected flow_stmt: %d", TYPE(ch));
2424 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002426
2427 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2428 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429}
2430
2431static alias_ty
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002432alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433{
2434 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002435 import_as_name: NAME ['as' NAME]
2436 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 dotted_name: NAME ('.' NAME)*
2438 */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002439 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002440
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441 loop:
2442 switch (TYPE(n)) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002443 case import_as_name: {
2444 node *name_node = CHILD(n, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002445 str = NULL;
2446 if (NCH(n) == 3) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002447 node *str_node = CHILD(n, 2);
2448 if (store && !forbidden_check(c, str_node, STR(str_node)))
2449 return NULL;
2450 str = NEW_IDENTIFIER(str_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002451 if (!str)
2452 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002453 }
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002454 else {
2455 if (!forbidden_check(c, name_node, STR(name_node)))
2456 return NULL;
2457 }
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002458 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002459 if (!name)
2460 return NULL;
2461 return alias(name, str, c->c_arena);
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002462 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002463 case dotted_as_name:
2464 if (NCH(n) == 1) {
2465 n = CHILD(n, 0);
2466 goto loop;
2467 }
2468 else {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002469 node *asname_node = CHILD(n, 2);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002470 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002471 if (!a)
2472 return NULL;
2473 assert(!a->asname);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002474 if (!forbidden_check(c, asname_node, STR(asname_node)))
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002475 return NULL;
2476 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002477 if (!a->asname)
2478 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002479 return a;
2480 }
2481 break;
2482 case dotted_name:
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002483 if (NCH(n) == 1) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002484 node *name_node = CHILD(n, 0);
2485 if (store && !forbidden_check(c, name_node, STR(name_node)))
2486 return NULL;
2487 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002488 if (!name)
2489 return NULL;
2490 return alias(name, NULL, c->c_arena);
2491 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002492 else {
2493 /* Create a string of the form "a.b.c" */
2494 int i;
2495 size_t len;
2496 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002498 len = 0;
2499 for (i = 0; i < NCH(n); i += 2)
2500 /* length of string plus one for the dot */
2501 len += strlen(STR(CHILD(n, i))) + 1;
2502 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002503 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002504 if (!str)
2505 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002506 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002507 if (!s)
2508 return NULL;
2509 for (i = 0; i < NCH(n); i += 2) {
2510 char *sch = STR(CHILD(n, i));
2511 strcpy(s, STR(CHILD(n, i)));
2512 s += strlen(sch);
2513 *s++ = '.';
2514 }
2515 --s;
2516 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002517 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002518 PyArena_AddPyObject(c->c_arena, str);
2519 return alias(str, NULL, c->c_arena);
2520 }
2521 break;
2522 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002523 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002524 PyArena_AddPyObject(c->c_arena, str);
2525 return alias(str, NULL, c->c_arena);
2526 default:
2527 PyErr_Format(PyExc_SystemError,
2528 "unexpected import name: %d", TYPE(n));
2529 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002531
2532 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 return NULL;
2534}
2535
2536static stmt_ty
2537ast_for_import_stmt(struct compiling *c, const node *n)
2538{
2539 /*
2540 import_stmt: import_name | import_from
2541 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002542 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002543 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002545 int lineno;
2546 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 int i;
2548 asdl_seq *aliases;
2549
2550 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002551 lineno = LINENO(n);
2552 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002554 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002555 n = CHILD(n, 1);
2556 REQ(n, dotted_as_names);
2557 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2558 if (!aliases)
2559 return NULL;
2560 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002561 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002562 if (!import_alias)
2563 return NULL;
2564 asdl_seq_SET(aliases, i / 2, import_alias);
2565 }
2566 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002568 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002569 int n_children;
2570 int idx, ndots = 0;
2571 alias_ty mod = NULL;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002572 identifier modname = NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002573
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002574 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002575 optional module name */
2576 for (idx = 1; idx < NCH(n); idx++) {
2577 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002578 mod = alias_for_import_name(c, CHILD(n, idx), 0);
2579 if (!mod)
2580 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002581 idx++;
2582 break;
2583 } else if (TYPE(CHILD(n, idx)) != DOT) {
2584 break;
2585 }
2586 ndots++;
2587 }
2588 idx++; /* skip over the 'import' keyword */
2589 switch (TYPE(CHILD(n, idx))) {
2590 case STAR:
2591 /* from ... import * */
2592 n = CHILD(n, idx);
2593 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002594 break;
2595 case LPAR:
2596 /* from ... import (x, y, z) */
2597 n = CHILD(n, idx + 1);
2598 n_children = NCH(n);
2599 break;
2600 case import_as_names:
2601 /* from ... import x, y, z */
2602 n = CHILD(n, idx);
2603 n_children = NCH(n);
2604 if (n_children % 2 == 0) {
2605 ast_error(n, "trailing comma not allowed without"
2606 " surrounding parentheses");
2607 return NULL;
2608 }
2609 break;
2610 default:
2611 ast_error(n, "Unexpected node-type in from-import");
2612 return NULL;
2613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002615 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2616 if (!aliases)
2617 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002619 /* handle "from ... import *" special b/c there's no children */
2620 if (TYPE(n) == STAR) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002621 alias_ty import_alias = alias_for_import_name(c, n, 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002622 if (!import_alias)
2623 return NULL;
2624 asdl_seq_SET(aliases, 0, import_alias);
2625 }
2626 else {
2627 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002628 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002629 if (!import_alias)
2630 return NULL;
2631 asdl_seq_SET(aliases, i / 2, import_alias);
2632 }
2633 }
2634 if (mod != NULL)
2635 modname = mod->name;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002636 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2637 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 }
Neal Norwitz79792652005-11-14 04:25:03 +00002639 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002640 "unknown import statement: starts with command '%s'",
2641 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 return NULL;
2643}
2644
2645static stmt_ty
2646ast_for_global_stmt(struct compiling *c, const node *n)
2647{
2648 /* global_stmt: 'global' NAME (',' NAME)* */
2649 identifier name;
2650 asdl_seq *s;
2651 int i;
2652
2653 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002654 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002656 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002658 name = NEW_IDENTIFIER(CHILD(n, i));
2659 if (!name)
2660 return NULL;
2661 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002663 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664}
2665
2666static stmt_ty
2667ast_for_exec_stmt(struct compiling *c, const node *n)
2668{
2669 expr_ty expr1, globals = NULL, locals = NULL;
2670 int n_children = NCH(n);
2671 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002672 PyErr_Format(PyExc_SystemError,
2673 "poorly formed 'exec' statement: %d parts to statement",
2674 n_children);
2675 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 }
2677
2678 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2679 REQ(n, exec_stmt);
2680 expr1 = ast_for_expr(c, CHILD(n, 1));
2681 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002682 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002684 globals = ast_for_expr(c, CHILD(n, 3));
2685 if (!globals)
2686 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 }
2688 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002689 locals = ast_for_expr(c, CHILD(n, 5));
2690 if (!locals)
2691 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 }
2693
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002694 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2695 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696}
2697
2698static stmt_ty
2699ast_for_assert_stmt(struct compiling *c, const node *n)
2700{
2701 /* assert_stmt: 'assert' test [',' test] */
2702 REQ(n, assert_stmt);
2703 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002704 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2705 if (!expression)
2706 return NULL;
2707 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2708 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 }
2710 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002711 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002713 expr1 = ast_for_expr(c, CHILD(n, 1));
2714 if (!expr1)
2715 return NULL;
2716 expr2 = ast_for_expr(c, CHILD(n, 3));
2717 if (!expr2)
2718 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002719
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002720 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721 }
Neal Norwitz79792652005-11-14 04:25:03 +00002722 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002723 "improper number of parts to 'assert' statement: %d",
2724 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 return NULL;
2726}
2727
2728static asdl_seq *
2729ast_for_suite(struct compiling *c, const node *n)
2730{
2731 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002732 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 stmt_ty s;
2734 int i, total, num, end, pos = 0;
2735 node *ch;
2736
2737 REQ(n, suite);
2738
2739 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002740 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002744 n = CHILD(n, 0);
2745 /* simple_stmt always ends with a NEWLINE,
Brett Cannonfa84d922010-05-05 20:30:30 +00002746 and may have a trailing SEMI
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002747 */
2748 end = NCH(n) - 1;
2749 if (TYPE(CHILD(n, end - 1)) == SEMI)
2750 end--;
2751 /* loop by 2 to skip semi-colons */
2752 for (i = 0; i < end; i += 2) {
2753 ch = CHILD(n, i);
2754 s = ast_for_stmt(c, ch);
2755 if (!s)
2756 return NULL;
2757 asdl_seq_SET(seq, pos++, s);
2758 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 }
2760 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002761 for (i = 2; i < (NCH(n) - 1); i++) {
2762 ch = CHILD(n, i);
2763 REQ(ch, stmt);
2764 num = num_stmts(ch);
2765 if (num == 1) {
2766 /* small_stmt or compound_stmt with only one child */
2767 s = ast_for_stmt(c, ch);
2768 if (!s)
2769 return NULL;
2770 asdl_seq_SET(seq, pos++, s);
2771 }
2772 else {
2773 int j;
2774 ch = CHILD(ch, 0);
2775 REQ(ch, simple_stmt);
2776 for (j = 0; j < NCH(ch); j += 2) {
2777 /* statement terminates with a semi-colon ';' */
2778 if (NCH(CHILD(ch, j)) == 0) {
2779 assert((j + 1) == NCH(ch));
2780 break;
2781 }
2782 s = ast_for_stmt(c, CHILD(ch, j));
2783 if (!s)
2784 return NULL;
2785 asdl_seq_SET(seq, pos++, s);
2786 }
2787 }
2788 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 }
2790 assert(pos == seq->size);
2791 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792}
2793
2794static stmt_ty
2795ast_for_if_stmt(struct compiling *c, const node *n)
2796{
2797 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2798 ['else' ':' suite]
2799 */
2800 char *s;
2801
2802 REQ(n, if_stmt);
2803
2804 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002805 expr_ty expression;
2806 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002808 expression = ast_for_expr(c, CHILD(n, 1));
2809 if (!expression)
2810 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002811 suite_seq = ast_for_suite(c, CHILD(n, 3));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002812 if (!suite_seq)
2813 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002814
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002815 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2816 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 s = STR(CHILD(n, 4));
2820 /* s[2], the third character in the string, will be
2821 's' for el_s_e, or
2822 'i' for el_i_f
2823 */
2824 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002825 expr_ty expression;
2826 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002828 expression = ast_for_expr(c, CHILD(n, 1));
2829 if (!expression)
2830 return NULL;
2831 seq1 = ast_for_suite(c, CHILD(n, 3));
2832 if (!seq1)
2833 return NULL;
2834 seq2 = ast_for_suite(c, CHILD(n, 6));
2835 if (!seq2)
2836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002838 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2839 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 }
2841 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002842 int i, n_elif, has_else = 0;
2843 expr_ty expression;
2844 asdl_seq *suite_seq;
2845 asdl_seq *orelse = NULL;
2846 n_elif = NCH(n) - 4;
2847 /* must reference the child n_elif+1 since 'else' token is third,
2848 not fourth, child from the end. */
2849 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2850 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2851 has_else = 1;
2852 n_elif -= 3;
2853 }
2854 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002856 if (has_else) {
2857 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002859 orelse = asdl_seq_new(1, c->c_arena);
2860 if (!orelse)
2861 return NULL;
2862 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2863 if (!expression)
2864 return NULL;
2865 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2866 if (!suite_seq)
2867 return NULL;
2868 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2869 if (!suite_seq2)
2870 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871
Brett Cannonfa84d922010-05-05 20:30:30 +00002872 asdl_seq_SET(orelse, 0,
2873 If(expression, suite_seq, suite_seq2,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002874 LINENO(CHILD(n, NCH(n) - 6)),
2875 CHILD(n, NCH(n) - 6)->n_col_offset,
2876 c->c_arena));
2877 /* the just-created orelse handled the last elif */
2878 n_elif--;
2879 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002881 for (i = 0; i < n_elif; i++) {
2882 int off = 5 + (n_elif - i - 1) * 4;
2883 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2884 if (!newobj)
2885 return NULL;
2886 expression = ast_for_expr(c, CHILD(n, off));
2887 if (!expression)
2888 return NULL;
2889 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2890 if (!suite_seq)
2891 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002893 asdl_seq_SET(newobj, 0,
Brett Cannonfa84d922010-05-05 20:30:30 +00002894 If(expression, suite_seq, orelse,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002895 LINENO(CHILD(n, off)),
2896 CHILD(n, off)->n_col_offset, c->c_arena));
2897 orelse = newobj;
2898 }
2899 expression = ast_for_expr(c, CHILD(n, 1));
2900 if (!expression)
2901 return NULL;
2902 suite_seq = ast_for_suite(c, CHILD(n, 3));
2903 if (!suite_seq)
2904 return NULL;
2905 return If(expression, suite_seq, orelse,
2906 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002908
2909 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002910 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912}
2913
2914static stmt_ty
2915ast_for_while_stmt(struct compiling *c, const node *n)
2916{
2917 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2918 REQ(n, while_stmt);
2919
2920 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002921 expr_ty expression;
2922 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002924 expression = ast_for_expr(c, CHILD(n, 1));
2925 if (!expression)
2926 return NULL;
2927 suite_seq = ast_for_suite(c, CHILD(n, 3));
2928 if (!suite_seq)
2929 return NULL;
2930 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2931 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932 }
2933 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002934 expr_ty expression;
2935 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002937 expression = ast_for_expr(c, CHILD(n, 1));
2938 if (!expression)
2939 return NULL;
2940 seq1 = ast_for_suite(c, CHILD(n, 3));
2941 if (!seq1)
2942 return NULL;
2943 seq2 = ast_for_suite(c, CHILD(n, 6));
2944 if (!seq2)
2945 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002947 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2948 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002950
2951 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002952 "wrong number of tokens for 'while' statement: %d",
2953 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955}
2956
2957static stmt_ty
2958ast_for_for_stmt(struct compiling *c, const node *n)
2959{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002960 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 expr_ty expression;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002962 expr_ty target, first;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002963 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2965 REQ(n, for_stmt);
2966
2967 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002968 seq = ast_for_suite(c, CHILD(n, 8));
2969 if (!seq)
2970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 }
2972
Neal Norwitzedef2be2006-07-12 05:26:17 +00002973 node_target = CHILD(n, 1);
2974 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002975 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002976 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002977 /* Check the # of children rather than the length of _target, since
2978 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002979 first = (expr_ty)asdl_seq_GET(_target, 0);
Neal Norwitzedef2be2006-07-12 05:26:17 +00002980 if (NCH(node_target) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002981 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002983 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002985 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002986 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002989 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002990 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002992 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002993 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994}
2995
2996static excepthandler_ty
2997ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2998{
Collin Winter62903052007-05-18 23:11:24 +00002999 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 REQ(exc, except_clause);
3001 REQ(body, suite);
3002
3003 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003004 asdl_seq *suite_seq = ast_for_suite(c, body);
3005 if (!suite_seq)
3006 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007
Georg Brandla48f3ab2008-03-30 06:40:17 +00003008 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003009 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 }
3011 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003012 expr_ty expression;
3013 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003015 expression = ast_for_expr(c, CHILD(exc, 1));
3016 if (!expression)
3017 return NULL;
3018 suite_seq = ast_for_suite(c, body);
3019 if (!suite_seq)
3020 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021
Georg Brandla48f3ab2008-03-30 06:40:17 +00003022 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003023 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 }
3025 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003026 asdl_seq *suite_seq;
3027 expr_ty expression;
3028 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
3029 if (!e)
3030 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003031 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003032 return NULL;
3033 expression = ast_for_expr(c, CHILD(exc, 1));
3034 if (!expression)
3035 return NULL;
3036 suite_seq = ast_for_suite(c, body);
3037 if (!suite_seq)
3038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039
Georg Brandla48f3ab2008-03-30 06:40:17 +00003040 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003041 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003043
3044 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003045 "wrong number of children for 'except' clause: %d",
3046 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048}
3049
3050static stmt_ty
3051ast_for_try_stmt(struct compiling *c, const node *n)
3052{
Neal Norwitzf599f422005-12-17 21:33:47 +00003053 const int nch = NCH(n);
3054 int n_except = (nch - 3)/3;
3055 asdl_seq *body, *orelse = NULL, *finally = NULL;
3056
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 REQ(n, try_stmt);
3058
Neal Norwitzf599f422005-12-17 21:33:47 +00003059 body = ast_for_suite(c, CHILD(n, 2));
3060 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062
Neal Norwitzf599f422005-12-17 21:33:47 +00003063 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003064 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3065 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3066 /* we can assume it's an "else",
3067 because nch >= 9 for try-else-finally and
3068 it would otherwise have a type of except_clause */
3069 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3070 if (orelse == NULL)
3071 return NULL;
3072 n_except--;
3073 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003075 finally = ast_for_suite(c, CHILD(n, nch - 1));
3076 if (finally == NULL)
3077 return NULL;
3078 n_except--;
3079 }
3080 else {
3081 /* we can assume it's an "else",
3082 otherwise it would have a type of except_clause */
3083 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3084 if (orelse == NULL)
3085 return NULL;
3086 n_except--;
3087 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003089 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003090 ast_error(n, "malformed 'try' statement");
3091 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092 }
Brett Cannonfa84d922010-05-05 20:30:30 +00003093
Neal Norwitzf599f422005-12-17 21:33:47 +00003094 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003095 int i;
3096 stmt_ty except_st;
3097 /* process except statements to create a try ... except */
3098 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
3099 if (handlers == NULL)
3100 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003101
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003102 for (i = 0; i < n_except; i++) {
3103 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3104 CHILD(n, 5 + i * 3));
3105 if (!e)
3106 return NULL;
3107 asdl_seq_SET(handlers, i, e);
3108 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003109
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003110 except_st = TryExcept(body, handlers, orelse, LINENO(n),
3111 n->n_col_offset, c->c_arena);
3112 if (!finally)
3113 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00003114
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003115 /* if a 'finally' is present too, we nest the TryExcept within a
3116 TryFinally to emulate try ... except ... finally */
3117 body = asdl_seq_new(1, c->c_arena);
3118 if (body == NULL)
3119 return NULL;
3120 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00003121 }
3122
3123 /* must be a try ... finally (except clauses are in body, if any exist) */
3124 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003125 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126}
3127
Georg Brandl944f6842009-05-25 21:02:56 +00003128/* with_item: test ['as' expr] */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003129static stmt_ty
Georg Brandl944f6842009-05-25 21:02:56 +00003130ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003131{
3132 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003133
Georg Brandl944f6842009-05-25 21:02:56 +00003134 REQ(n, with_item);
3135 context_expr = ast_for_expr(c, CHILD(n, 0));
Collin Winter77c67bd2007-03-16 04:11:30 +00003136 if (!context_expr)
3137 return NULL;
Georg Brandl944f6842009-05-25 21:02:56 +00003138 if (NCH(n) == 3) {
3139 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003140
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003141 if (!optional_vars) {
3142 return NULL;
3143 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003144 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003145 return NULL;
3146 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003147 }
3148
Georg Brandl944f6842009-05-25 21:02:56 +00003149 return With(context_expr, optional_vars, content, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003150 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003151}
3152
Georg Brandl944f6842009-05-25 21:02:56 +00003153/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3154static stmt_ty
3155ast_for_with_stmt(struct compiling *c, const node *n)
3156{
3157 int i;
3158 stmt_ty ret;
3159 asdl_seq *inner;
3160
3161 REQ(n, with_stmt);
3162
3163 /* process the with items inside-out */
3164 i = NCH(n) - 1;
3165 /* the suite of the innermost with item is the suite of the with stmt */
3166 inner = ast_for_suite(c, CHILD(n, i));
3167 if (!inner)
3168 return NULL;
3169
3170 for (;;) {
3171 i -= 2;
3172 ret = ast_for_with_item(c, CHILD(n, i), inner);
3173 if (!ret)
3174 return NULL;
3175 /* was this the last item? */
3176 if (i == 1)
3177 break;
3178 /* if not, wrap the result so far in a new sequence */
3179 inner = asdl_seq_new(1, c->c_arena);
3180 if (!inner)
3181 return NULL;
3182 asdl_seq_SET(inner, 0, ret);
3183 }
3184
3185 return ret;
3186}
3187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003189ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190{
3191 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003192 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 asdl_seq *bases, *s;
Brett Cannonfa84d922010-05-05 20:30:30 +00003194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 REQ(n, classdef);
3196
Benjamin Petersond5efd202008-06-08 22:52:37 +00003197 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199
3200 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003201 s = ast_for_suite(c, CHILD(n, 3));
3202 if (!s)
3203 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003204 classname = NEW_IDENTIFIER(CHILD(n, 1));
3205 if (!classname)
3206 return NULL;
3207 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3208 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 }
3210 /* check for empty base list */
3211 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003212 s = ast_for_suite(c, CHILD(n,5));
3213 if (!s)
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003214 return NULL;
3215 classname = NEW_IDENTIFIER(CHILD(n, 1));
3216 if (!classname)
3217 return NULL;
3218 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3219 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 }
3221
3222 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003223 bases = ast_for_class_bases(c, CHILD(n, 3));
3224 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226
3227 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003228 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003229 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003230 classname = NEW_IDENTIFIER(CHILD(n, 1));
3231 if (!classname)
3232 return NULL;
3233 return ClassDef(classname, bases, s, decorator_seq,
Christian Heimes5224d282008-02-23 15:01:05 +00003234 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235}
3236
3237static stmt_ty
3238ast_for_stmt(struct compiling *c, const node *n)
3239{
3240 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003241 assert(NCH(n) == 1);
3242 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003243 }
3244 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003245 assert(num_stmts(n) == 1);
3246 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247 }
3248 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003249 n = CHILD(n, 0);
3250 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3251 | flow_stmt | import_stmt | global_stmt | exec_stmt
3252 | assert_stmt
3253 */
3254 switch (TYPE(n)) {
3255 case expr_stmt:
3256 return ast_for_expr_stmt(c, n);
3257 case print_stmt:
3258 return ast_for_print_stmt(c, n);
3259 case del_stmt:
3260 return ast_for_del_stmt(c, n);
3261 case pass_stmt:
3262 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3263 case flow_stmt:
3264 return ast_for_flow_stmt(c, n);
3265 case import_stmt:
3266 return ast_for_import_stmt(c, n);
3267 case global_stmt:
3268 return ast_for_global_stmt(c, n);
3269 case exec_stmt:
3270 return ast_for_exec_stmt(c, n);
3271 case assert_stmt:
3272 return ast_for_assert_stmt(c, n);
3273 default:
3274 PyErr_Format(PyExc_SystemError,
3275 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3276 TYPE(n), NCH(n));
3277 return NULL;
3278 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279 }
3280 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003281 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003282 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003283 */
3284 node *ch = CHILD(n, 0);
3285 REQ(n, compound_stmt);
3286 switch (TYPE(ch)) {
3287 case if_stmt:
3288 return ast_for_if_stmt(c, ch);
3289 case while_stmt:
3290 return ast_for_while_stmt(c, ch);
3291 case for_stmt:
3292 return ast_for_for_stmt(c, ch);
3293 case try_stmt:
3294 return ast_for_try_stmt(c, ch);
3295 case with_stmt:
3296 return ast_for_with_stmt(c, ch);
3297 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003298 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003299 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003300 return ast_for_classdef(c, ch, NULL);
3301 case decorated:
3302 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003303 default:
3304 PyErr_Format(PyExc_SystemError,
3305 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3306 TYPE(n), NCH(n));
3307 return NULL;
3308 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
3310}
3311
3312static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003313parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003315 const char *end;
3316 long x;
3317 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003319 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003320 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321#endif
3322
Mark Dickinson422ce062008-12-05 17:59:46 +00003323 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003324 errno = 0;
3325 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003327 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003329 if (*end == 'l' || *end == 'L')
3330 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003331 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003332 if (*end == '\0') {
3333 if (errno != 0)
3334 return PyLong_FromString((char *)s, (char **)0, 0);
3335 return PyInt_FromLong(x);
3336 }
3337 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003339 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003340 complex.real = 0.;
Eric Smithabc9f702009-10-27 18:33:14 +00003341 complex.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3342 if (complex.imag == -1.0 && PyErr_Occurred())
3343 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003344 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003345 }
3346 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003348 {
Eric Smithabc9f702009-10-27 18:33:14 +00003349 dx = PyOS_string_to_double(s, NULL, NULL);
3350 if (dx == -1.0 && PyErr_Occurred())
3351 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003352 return PyFloat_FromDouble(dx);
3353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354}
3355
3356static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003357decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358{
3359#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003360 Py_FatalError("decode_utf8 should not be called in this build.");
3361 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003363 PyObject *u, *v;
3364 char *s, *t;
3365 t = s = (char *)*sPtr;
3366 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3367 while (s < end && (*s & 0x80)) s++;
3368 *sPtr = s;
3369 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3370 if (u == NULL)
3371 return NULL;
3372 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3373 Py_DECREF(u);
3374 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375#endif
3376}
3377
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003378#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003380decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381{
Brett Cannonfa84d922010-05-05 20:30:30 +00003382 PyObject *v;
3383 PyObject *u = NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003384 char *buf;
3385 char *p;
3386 const char *end;
Brett Cannonfa84d922010-05-05 20:30:30 +00003387 if (encoding != NULL && strcmp(encoding, "iso-8859-1")) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003388 /* check for integer overflow */
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003389 if (len > PY_SIZE_MAX / 6)
Gregory P. Smith9d534572008-06-11 07:41:16 +00003390 return NULL;
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003391 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3392 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3393 u = PyString_FromStringAndSize((char *)NULL, len * 6);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003394 if (u == NULL)
3395 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003396 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003397 end = s + len;
3398 while (s < end) {
3399 if (*s == '\\') {
3400 *p++ = *s++;
3401 if (*s & 0x80) {
3402 strcpy(p, "u005c");
3403 p += 5;
3404 }
3405 }
3406 if (*s & 0x80) { /* XXX inefficient */
3407 PyObject *w;
3408 char *r;
3409 Py_ssize_t rn, i;
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003410 w = decode_utf8(c, &s, end, "utf-32-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003411 if (w == NULL) {
3412 Py_DECREF(u);
3413 return NULL;
3414 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003415 r = PyString_AsString(w);
3416 rn = PyString_Size(w);
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003417 assert(rn % 4 == 0);
3418 for (i = 0; i < rn; i += 4) {
3419 sprintf(p, "\\U%02x%02x%02x%02x",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003420 r[i + 0] & 0xFF,
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003421 r[i + 1] & 0xFF,
3422 r[i + 2] & 0xFF,
3423 r[i + 3] & 0xFF);
3424 p += 10;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003425 }
3426 Py_DECREF(w);
3427 } else {
3428 *p++ = *s++;
3429 }
3430 }
3431 len = p - buf;
3432 s = buf;
3433 }
3434 if (rawmode)
3435 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3436 else
3437 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3438 Py_XDECREF(u);
3439 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003441#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442
3443/* s is a Python string literal, including the bracketing quote characters,
3444 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3445 * parsestr parses it, and returns the decoded Python string object.
3446 */
3447static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003448parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003450 size_t len;
3451 int quote = Py_CHARMASK(*s);
3452 int rawmode = 0;
3453 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003454 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003456 if (isalpha(quote) || quote == '_') {
3457 if (quote == 'u' || quote == 'U') {
3458 quote = *++s;
3459 unicode = 1;
3460 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003461 if (quote == 'b' || quote == 'B') {
3462 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003463 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003464 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003465 if (quote == 'r' || quote == 'R') {
3466 quote = *++s;
3467 rawmode = 1;
3468 }
3469 }
3470 if (quote != '\'' && quote != '\"') {
3471 PyErr_BadInternalCall();
3472 return NULL;
3473 }
3474 s++;
3475 len = strlen(s);
3476 if (len > INT_MAX) {
Brett Cannonfa84d922010-05-05 20:30:30 +00003477 PyErr_SetString(PyExc_OverflowError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003478 "string to parse is too long");
3479 return NULL;
3480 }
3481 if (s[--len] != quote) {
3482 PyErr_BadInternalCall();
3483 return NULL;
3484 }
3485 if (len >= 4 && s[0] == quote && s[1] == quote) {
3486 s += 2;
3487 len -= 2;
3488 if (s[--len] != quote || s[--len] != quote) {
3489 PyErr_BadInternalCall();
3490 return NULL;
3491 }
3492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003494 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003495 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003498 need_encoding = (c->c_encoding != NULL &&
3499 strcmp(c->c_encoding, "utf-8") != 0 &&
3500 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003501 if (rawmode || strchr(s, '\\') == NULL) {
3502 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003504 /* This should not happen - we never see any other
3505 encoding. */
3506 Py_FatalError(
3507 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003509 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3510 if (u == NULL)
3511 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003512 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003513 Py_DECREF(u);
3514 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003516 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003517 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003518 }
3519 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003520
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003521 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003522 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523}
3524
3525/* Build a Python string object out of a STRING atom. This takes care of
3526 * compile-time literal catenation, calling parsestr() on each piece, and
3527 * pasting the intermediate results together.
3528 */
3529static PyObject *
3530parsestrplus(struct compiling *c, const node *n)
3531{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003532 PyObject *v;
3533 int i;
3534 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003535 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003536 /* String literal concatenation */
3537 for (i = 1; i < NCH(n); i++) {
3538 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003539 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003540 if (s == NULL)
3541 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003542 if (PyString_Check(v) && PyString_Check(s)) {
3543 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003544 if (v == NULL)
3545 goto onError;
3546 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003548 else {
3549 PyObject *temp = PyUnicode_Concat(v, s);
3550 Py_DECREF(s);
3551 Py_DECREF(v);
3552 v = temp;
3553 if (v == NULL)
3554 goto onError;
3555 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003557 }
3558 }
3559 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560
3561 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003562 Py_XDECREF(v);
3563 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564}