blob: 88e47450b69b0c0b61800e93a1864dbdcd19cef0 [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)
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000691 return NULL;
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)
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000694 return NULL;
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)
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000714 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000715 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");
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000725 return NULL;
Benjamin Peterson99a50232009-11-19 22:54:57 +0000726 }
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");
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000729 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000730 }
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"))
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000738 return NULL;
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))
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000742 return NULL;
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))))
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000757 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000758 id = NEW_IDENTIFIER(CHILD(ch, 0));
759 if (!id)
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000760 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +0000761 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000762 c->c_arena);
763 if (!name)
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000764 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000765 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"))
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000772 return NULL;
Benjamin Peterson99a50232009-11-19 22:54:57 +0000773
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))))
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000778 return NULL;
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)
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000781 return NULL;
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))))
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000786 return NULL;
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)
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000789 return NULL;
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);
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000796 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000797 }
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
803static expr_ty
804ast_for_dotted_name(struct compiling *c, const node *n)
805{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000806 expr_ty e;
807 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000808 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 int i;
810
811 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000812
813 lineno = LINENO(n);
814 col_offset = n->n_col_offset;
815
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 id = NEW_IDENTIFIER(CHILD(n, 0));
817 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000818 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000819 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000821 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822
823 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000824 id = NEW_IDENTIFIER(CHILD(n, i));
825 if (!id)
826 return NULL;
827 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
828 if (!e)
829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 }
831
832 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833}
834
835static expr_ty
836ast_for_decorator(struct compiling *c, const node *n)
837{
838 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
839 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000840 expr_ty name_expr;
Brett Cannonfa84d922010-05-05 20:30:30 +0000841
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000843 REQ(CHILD(n, 0), AT);
844 REQ(RCHILD(n, -1), NEWLINE);
Brett Cannonfa84d922010-05-05 20:30:30 +0000845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
847 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000848 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +0000849
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000851 d = name_expr;
852 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
854 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000855 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
856 n->n_col_offset, c->c_arena);
857 if (!d)
858 return NULL;
859 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 }
861 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000862 d = ast_for_call(c, CHILD(n, 3), name_expr);
863 if (!d)
864 return NULL;
865 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 }
867
868 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869}
870
871static asdl_seq*
872ast_for_decorators(struct compiling *c, const node *n)
873{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000874 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000875 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 int i;
Brett Cannonfa84d922010-05-05 20:30:30 +0000877
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000879 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000881 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +0000882
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000884 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000885 if (!d)
886 return NULL;
887 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 }
889 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890}
891
892static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000893ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894{
Christian Heimes5224d282008-02-23 15:01:05 +0000895 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000896 identifier name;
897 arguments_ty args;
898 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000899 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900
901 REQ(n, funcdef);
902
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 name = NEW_IDENTIFIER(CHILD(n, name_i));
904 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000905 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000906 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000907 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 args = ast_for_arguments(c, CHILD(n, name_i + 1));
909 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000910 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 body = ast_for_suite(c, CHILD(n, name_i + 3));
912 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000913 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000915 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000916 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917}
918
Christian Heimes5224d282008-02-23 15:01:05 +0000919static stmt_ty
920ast_for_decorated(struct compiling *c, const node *n)
921{
922 /* decorated: decorators (classdef | funcdef) */
923 stmt_ty thing = NULL;
924 asdl_seq *decorator_seq = NULL;
925
926 REQ(n, decorated);
927
928 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
929 if (!decorator_seq)
930 return NULL;
931
932 assert(TYPE(CHILD(n, 1)) == funcdef ||
933 TYPE(CHILD(n, 1)) == classdef);
934
935 if (TYPE(CHILD(n, 1)) == funcdef) {
936 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
937 } else if (TYPE(CHILD(n, 1)) == classdef) {
938 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
939 }
940 /* we count the decorators in when talking about the class' or
941 function's line number */
942 if (thing) {
943 thing->lineno = LINENO(n);
944 thing->col_offset = n->n_col_offset;
945 }
946 return thing;
947}
948
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949static expr_ty
950ast_for_lambdef(struct compiling *c, const node *n)
951{
952 /* lambdef: 'lambda' [varargslist] ':' test */
953 arguments_ty args;
954 expr_ty expression;
955
956 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000957 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
958 if (!args)
959 return NULL;
960 expression = ast_for_expr(c, CHILD(n, 2));
961 if (!expression)
962 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 }
964 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000965 args = ast_for_arguments(c, CHILD(n, 1));
966 if (!args)
967 return NULL;
968 expression = ast_for_expr(c, CHILD(n, 3));
969 if (!expression)
970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 }
972
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000973 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974}
975
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000976static expr_ty
977ast_for_ifexpr(struct compiling *c, const node *n)
978{
Brett Cannonfa84d922010-05-05 20:30:30 +0000979 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000980 expr_ty expression, body, orelse;
981
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000982 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000983 body = ast_for_expr(c, CHILD(n, 0));
984 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000985 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000986 expression = ast_for_expr(c, CHILD(n, 2));
987 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000988 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000989 orelse = ast_for_expr(c, CHILD(n, 4));
990 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000991 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000992 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000993 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000994}
995
Neal Norwitze4d4f002006-09-05 03:58:26 +0000996/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
997 so there is only a single version. Possibly for loops can also re-use
998 the code.
999*/
1000
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001/* Count the number of 'for' loop in a list comprehension.
1002
1003 Helper for ast_for_listcomp().
1004*/
1005
1006static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001007count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008{
1009 int n_fors = 0;
1010 node *ch = CHILD(n, 1);
1011
1012 count_list_for:
1013 n_fors++;
1014 REQ(ch, list_for);
1015 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001016 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001018 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 count_list_iter:
1020 REQ(ch, list_iter);
1021 ch = CHILD(ch, 0);
1022 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001023 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001025 if (NCH(ch) == 3) {
1026 ch = CHILD(ch, 2);
1027 goto count_list_iter;
1028 }
1029 else
1030 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001032
1033 /* Should never be reached */
1034 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1035 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036}
1037
1038/* Count the number of 'if' statements in a list comprehension.
1039
1040 Helper for ast_for_listcomp().
1041*/
1042
1043static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001044count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045{
1046 int n_ifs = 0;
1047
1048 count_list_iter:
1049 REQ(n, list_iter);
1050 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001051 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 n = CHILD(n, 0);
1053 REQ(n, list_if);
1054 n_ifs++;
1055 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001056 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 n = CHILD(n, 2);
1058 goto count_list_iter;
1059}
1060
1061static expr_ty
1062ast_for_listcomp(struct compiling *c, const node *n)
1063{
1064 /* listmaker: test ( list_for | (',' test)* [','] )
1065 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1066 list_iter: list_for | list_if
1067 list_if: 'if' test [list_iter]
1068 testlist_safe: test [(',' test)+ [',']]
1069 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001070 expr_ty elt, first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 asdl_seq *listcomps;
1072 int i, n_fors;
1073 node *ch;
1074
1075 REQ(n, listmaker);
1076 assert(NCH(n) > 1);
1077
1078 elt = ast_for_expr(c, CHILD(n, 0));
1079 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001080 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001082 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001084 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001086 listcomps = asdl_seq_new(n_fors, c->c_arena);
1087 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001088 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001089
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090 ch = CHILD(n, 1);
1091 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001092 comprehension_ty lc;
1093 asdl_seq *t;
1094 expr_ty expression;
1095 node *for_ch;
Brett Cannonfa84d922010-05-05 20:30:30 +00001096
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001097 REQ(ch, list_for);
Brett Cannonfa84d922010-05-05 20:30:30 +00001098
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001099 for_ch = CHILD(ch, 1);
1100 t = ast_for_exprlist(c, for_ch, Store);
1101 if (!t)
1102 return NULL;
1103 expression = ast_for_testlist(c, CHILD(ch, 3));
1104 if (!expression)
1105 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001106
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001107 /* Check the # of children rather than the length of t, since
1108 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1109 */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001110 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001111 if (NCH(for_ch) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001112 lc = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001113 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001114 lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001115 c->c_arena),
1116 expression, NULL, c->c_arena);
1117 if (!lc)
1118 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001120 if (NCH(ch) == 5) {
1121 int j, n_ifs;
1122 asdl_seq *ifs;
1123 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001125 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001126 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001127 if (n_ifs == -1)
1128 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001130 ifs = asdl_seq_new(n_ifs, c->c_arena);
1131 if (!ifs)
1132 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001134 for (j = 0; j < n_ifs; j++) {
1135 REQ(ch, list_iter);
1136 ch = CHILD(ch, 0);
1137 REQ(ch, list_if);
Brett Cannonfa84d922010-05-05 20:30:30 +00001138
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001139 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1140 if (!list_for_expr)
1141 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001142
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001143 asdl_seq_SET(ifs, j, list_for_expr);
1144 if (NCH(ch) == 3)
1145 ch = CHILD(ch, 2);
1146 }
1147 /* on exit, must guarantee that ch is a list_for */
1148 if (TYPE(ch) == list_iter)
1149 ch = CHILD(ch, 0);
1150 lc->ifs = ifs;
1151 }
1152 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 }
1154
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001155 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156}
1157
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001158/*
1159 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001161 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162*/
1163
1164static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001165count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001167 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001169 count_comp_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001170 n_fors++;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001171 REQ(n, comp_for);
1172 if (NCH(n) == 5)
1173 n = CHILD(n, 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001174 else
1175 return n_fors;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001176 count_comp_iter:
1177 REQ(n, comp_iter);
1178 n = CHILD(n, 0);
1179 if (TYPE(n) == comp_for)
1180 goto count_comp_for;
1181 else if (TYPE(n) == comp_if) {
1182 if (NCH(n) == 3) {
1183 n = CHILD(n, 2);
1184 goto count_comp_iter;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001185 }
1186 else
1187 return n_fors;
1188 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001189
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001190 /* Should never be reached */
1191 PyErr_SetString(PyExc_SystemError,
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001192 "logic error in count_comp_fors");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001193 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194}
1195
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001196/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001198 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199*/
1200
1201static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001202count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001204 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001206 while (1) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001207 REQ(n, comp_iter);
1208 if (TYPE(CHILD(n, 0)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001209 return n_ifs;
1210 n = CHILD(n, 0);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001211 REQ(n, comp_if);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001212 n_ifs++;
1213 if (NCH(n) == 2)
1214 return n_ifs;
1215 n = CHILD(n, 2);
1216 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217}
1218
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001219static asdl_seq *
1220ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 int i, n_fors;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001223 asdl_seq *comps;
1224
1225 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001227 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001228
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001229 comps = asdl_seq_new(n_fors, c->c_arena);
1230 if (!comps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001231 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 for (i = 0; i < n_fors; i++) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001234 comprehension_ty comp;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001235 asdl_seq *t;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001236 expr_ty expression, first;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001237 node *for_ch;
Brett Cannonfa84d922010-05-05 20:30:30 +00001238
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001239 REQ(n, comp_for);
Brett Cannonfa84d922010-05-05 20:30:30 +00001240
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001241 for_ch = CHILD(n, 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001242 t = ast_for_exprlist(c, for_ch, Store);
1243 if (!t)
1244 return NULL;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001245 expression = ast_for_expr(c, CHILD(n, 3));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001246 if (!expression)
1247 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001249 /* Check the # of children rather than the length of t, since
1250 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00001251 first = (expr_ty)asdl_seq_GET(t, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001252 if (NCH(for_ch) == 1)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001253 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001254 else
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001255 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001256 c->c_arena),
1257 expression, NULL, c->c_arena);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001258 if (!comp)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001259 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001260
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001261 if (NCH(n) == 5) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001262 int j, n_ifs;
1263 asdl_seq *ifs;
Brett Cannonfa84d922010-05-05 20:30:30 +00001264
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001265 n = CHILD(n, 4);
1266 n_ifs = count_comp_ifs(c, n);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001267 if (n_ifs == -1)
1268 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001269
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001270 ifs = asdl_seq_new(n_ifs, c->c_arena);
1271 if (!ifs)
1272 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001273
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001274 for (j = 0; j < n_ifs; j++) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001275 REQ(n, comp_iter);
1276 n = CHILD(n, 0);
1277 REQ(n, comp_if);
Brett Cannonfa84d922010-05-05 20:30:30 +00001278
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001279 expression = ast_for_expr(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001280 if (!expression)
1281 return NULL;
1282 asdl_seq_SET(ifs, j, expression);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001283 if (NCH(n) == 3)
1284 n = CHILD(n, 2);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001285 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001286 /* on exit, must guarantee that n is a comp_for */
1287 if (TYPE(n) == comp_iter)
1288 n = CHILD(n, 0);
1289 comp->ifs = ifs;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001290 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001291 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001293 return comps;
1294}
1295
1296static expr_ty
1297ast_for_itercomp(struct compiling *c, const node *n, int type)
1298{
1299 expr_ty elt;
1300 asdl_seq *comps;
Brett Cannonfa84d922010-05-05 20:30:30 +00001301
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001302 assert(NCH(n) > 1);
Brett Cannonfa84d922010-05-05 20:30:30 +00001303
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001304 elt = ast_for_expr(c, CHILD(n, 0));
1305 if (!elt)
1306 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001307
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001308 comps = ast_for_comprehension(c, CHILD(n, 1));
1309 if (!comps)
1310 return NULL;
1311
1312 if (type == COMP_GENEXP)
1313 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1314 else if (type == COMP_SETCOMP)
1315 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1316 else
1317 /* Should never happen */
1318 return NULL;
1319}
1320
1321static expr_ty
1322ast_for_dictcomp(struct compiling *c, const node *n)
1323{
1324 expr_ty key, value;
1325 asdl_seq *comps;
Brett Cannonfa84d922010-05-05 20:30:30 +00001326
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001327 assert(NCH(n) > 3);
1328 REQ(CHILD(n, 1), COLON);
Brett Cannonfa84d922010-05-05 20:30:30 +00001329
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001330 key = ast_for_expr(c, CHILD(n, 0));
1331 if (!key)
1332 return NULL;
1333
1334 value = ast_for_expr(c, CHILD(n, 2));
1335 if (!value)
1336 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001337
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001338 comps = ast_for_comprehension(c, CHILD(n, 3));
1339 if (!comps)
1340 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001341
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001342 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1343}
1344
1345static expr_ty
1346ast_for_genexp(struct compiling *c, const node *n)
1347{
1348 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
1349 return ast_for_itercomp(c, n, COMP_GENEXP);
1350}
1351
1352static expr_ty
1353ast_for_setcomp(struct compiling *c, const node *n)
1354{
1355 assert(TYPE(n) == (dictorsetmaker));
1356 return ast_for_itercomp(c, n, COMP_SETCOMP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357}
1358
1359static expr_ty
1360ast_for_atom(struct compiling *c, const node *n)
1361{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001362 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1364 */
1365 node *ch = CHILD(n, 0);
Brett Cannonfa84d922010-05-05 20:30:30 +00001366
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 switch (TYPE(ch)) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001368 case NAME: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001369 /* All names start in Load context, but may later be
1370 changed. */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001371 PyObject *name = NEW_IDENTIFIER(ch);
1372 if (!name)
1373 return NULL;
1374 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1375 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001377 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001378 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001379#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001380 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1381 PyObject *type, *value, *tback, *errstr;
1382 PyErr_Fetch(&type, &value, &tback);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001383 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001384 if (errstr) {
1385 char *s = "";
1386 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001387 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001388 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1389 ast_error(n, buf);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001390 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001391 } else {
1392 ast_error(n, "(unicode error) unknown error");
1393 }
1394 Py_DECREF(type);
1395 Py_DECREF(value);
1396 Py_XDECREF(tback);
1397 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001398#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001399 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001400 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001401 PyArena_AddPyObject(c->c_arena, str);
1402 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 }
1404 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001405 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001406 if (!pynum)
1407 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001408
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001409 PyArena_AddPyObject(c->c_arena, pynum);
1410 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 }
1412 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001413 ch = CHILD(n, 1);
Brett Cannonfa84d922010-05-05 20:30:30 +00001414
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001415 if (TYPE(ch) == RPAR)
1416 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Brett Cannonfa84d922010-05-05 20:30:30 +00001417
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001418 if (TYPE(ch) == yield_expr)
1419 return ast_for_expr(c, ch);
Brett Cannonfa84d922010-05-05 20:30:30 +00001420
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001421 return ast_for_testlist_comp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001423 ch = CHILD(n, 1);
Brett Cannonfa84d922010-05-05 20:30:30 +00001424
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001425 if (TYPE(ch) == RSQB)
1426 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Brett Cannonfa84d922010-05-05 20:30:30 +00001427
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001428 REQ(ch, listmaker);
1429 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1430 asdl_seq *elts = seq_for_testlist(c, ch);
1431 if (!elts)
1432 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001433
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001434 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1435 }
1436 else
1437 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 case LBRACE: {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001439 /* dictorsetmaker:
1440 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
1441 * (test (comp_for | (',' test)* [',']))
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001442 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001443 int i, size;
1444 asdl_seq *keys, *values;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001445
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001446 ch = CHILD(n, 1);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001447 if (TYPE(ch) == RBRACE) {
1448 /* it's an empty dict */
1449 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
1450 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1451 /* it's a simple set */
1452 asdl_seq *elts;
1453 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
1454 elts = asdl_seq_new(size, c->c_arena);
1455 if (!elts)
1456 return NULL;
1457 for (i = 0; i < NCH(ch); i += 2) {
1458 expr_ty expression;
1459 expression = ast_for_expr(c, CHILD(ch, i));
1460 if (!expression)
1461 return NULL;
1462 asdl_seq_SET(elts, i / 2, expression);
1463 }
1464 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001465 } else if (TYPE(CHILD(ch, 1)) == comp_for) {
1466 /* it's a set comprehension */
1467 return ast_for_setcomp(c, ch);
1468 } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {
1469 return ast_for_dictcomp(c, ch);
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001470 } else {
1471 /* it's a dict */
1472 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1473 keys = asdl_seq_new(size, c->c_arena);
1474 if (!keys)
1475 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001476
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001477 values = asdl_seq_new(size, c->c_arena);
1478 if (!values)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001479 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001480
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001481 for (i = 0; i < NCH(ch); i += 4) {
1482 expr_ty expression;
Brett Cannonfa84d922010-05-05 20:30:30 +00001483
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001484 expression = ast_for_expr(c, CHILD(ch, i));
1485 if (!expression)
1486 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001487
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001488 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001489
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001490 expression = ast_for_expr(c, CHILD(ch, i + 2));
1491 if (!expression)
1492 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001493
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001494 asdl_seq_SET(values, i / 4, expression);
1495 }
1496 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 }
1499 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001500 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001501 if (Py_Py3kWarningFlag &&
1502 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001503 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001504 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001505 if (!expression)
1506 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001507
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001508 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 }
1510 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001511 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 }
1514}
1515
1516static slice_ty
1517ast_for_slice(struct compiling *c, const node *n)
1518{
1519 node *ch;
1520 expr_ty lower = NULL, upper = NULL, step = NULL;
1521
1522 REQ(n, subscript);
1523
1524 /*
1525 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1526 sliceop: ':' [test]
1527 */
1528 ch = CHILD(n, 0);
1529 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001530 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531
1532 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001533 /* 'step' variable hold no significance in terms of being used over
1534 other vars */
Brett Cannonfa84d922010-05-05 20:30:30 +00001535 step = ast_for_expr(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001536 if (!step)
1537 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00001538
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001539 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 }
1541
1542 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001543 lower = ast_for_expr(c, ch);
1544 if (!lower)
1545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 }
1547
1548 /* If there's an upper bound it's in the second or third position. */
1549 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001550 if (NCH(n) > 1) {
1551 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001553 if (TYPE(n2) == test) {
1554 upper = ast_for_expr(c, n2);
1555 if (!upper)
1556 return NULL;
1557 }
1558 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001560 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001562 if (TYPE(n2) == test) {
1563 upper = ast_for_expr(c, n2);
1564 if (!upper)
1565 return NULL;
1566 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 }
1568
1569 ch = CHILD(n, NCH(n) - 1);
1570 if (TYPE(ch) == sliceop) {
Benjamin Peterson4879c902009-07-20 20:28:08 +00001571 if (NCH(ch) == 1) {
Brett Cannonfa84d922010-05-05 20:30:30 +00001572 /*
Benjamin Peterson4879c902009-07-20 20:28:08 +00001573 This is an extended slice (ie "x[::]") with no expression in the
1574 step field. We set this literally to "None" in order to
1575 disambiguate it from x[:]. (The interpreter might have to call
1576 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1577 */
1578 identifier none = new_identifier("None", c->c_arena);
1579 if (!none)
1580 return NULL;
1581 ch = CHILD(ch, 0);
1582 step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);
1583 if (!step)
1584 return NULL;
1585 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001586 ch = CHILD(ch, 1);
1587 if (TYPE(ch) == test) {
1588 step = ast_for_expr(c, ch);
1589 if (!step)
1590 return NULL;
1591 }
1592 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 }
1594
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001595 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
1598static expr_ty
1599ast_for_binop(struct compiling *c, const node *n)
1600{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001601 /* Must account for a sequence of expressions.
Brett Cannonfa84d922010-05-05 20:30:30 +00001602 How should A op B op C by represented?
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001603 BinOp(BinOp(A, op, B), op, C).
1604 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001606 int i, nops;
1607 expr_ty expr1, expr2, result;
1608 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001610 expr1 = ast_for_expr(c, CHILD(n, 0));
1611 if (!expr1)
1612 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001614 expr2 = ast_for_expr(c, CHILD(n, 2));
1615 if (!expr2)
1616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001618 newoperator = get_operator(CHILD(n, 1));
1619 if (!newoperator)
1620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001622 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1623 c->c_arena);
1624 if (!result)
1625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001627 nops = (NCH(n) - 1) / 2;
1628 for (i = 1; i < nops; i++) {
1629 expr_ty tmp_result, tmp;
1630 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001632 newoperator = get_operator(next_oper);
1633 if (!newoperator)
1634 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001636 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1637 if (!tmp)
1638 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639
Brett Cannonfa84d922010-05-05 20:30:30 +00001640 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001641 LINENO(next_oper), next_oper->n_col_offset,
1642 c->c_arena);
Brett Cannonfa84d922010-05-05 20:30:30 +00001643 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001644 return NULL;
1645 result = tmp_result;
1646 }
1647 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648}
1649
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001650static expr_ty
1651ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1652{
Brett Cannonfa84d922010-05-05 20:30:30 +00001653 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001654 subscriptlist: subscript (',' subscript)* [',']
1655 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1656 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001657 REQ(n, trailer);
1658 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001659 if (NCH(n) == 2)
1660 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1661 n->n_col_offset, c->c_arena);
1662 else
1663 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001664 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001665 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00001666 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1667 if (!attr_id)
1668 return NULL;
1669 return Attribute(left_expr, attr_id, Load,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001670 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001671 }
1672 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001673 REQ(CHILD(n, 0), LSQB);
1674 REQ(CHILD(n, 2), RSQB);
1675 n = CHILD(n, 1);
1676 if (NCH(n) == 1) {
1677 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1678 if (!slc)
1679 return NULL;
1680 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1681 c->c_arena);
1682 }
1683 else {
Brett Cannonfa84d922010-05-05 20:30:30 +00001684 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001685 by treating the sequence as a tuple literal if there are
1686 no slice features.
1687 */
1688 int j;
1689 slice_ty slc;
1690 expr_ty e;
1691 bool simple = true;
1692 asdl_seq *slices, *elts;
1693 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1694 if (!slices)
1695 return NULL;
1696 for (j = 0; j < NCH(n); j += 2) {
1697 slc = ast_for_slice(c, CHILD(n, j));
1698 if (!slc)
1699 return NULL;
1700 if (slc->kind != Index_kind)
1701 simple = false;
1702 asdl_seq_SET(slices, j / 2, slc);
1703 }
1704 if (!simple) {
1705 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1706 Load, LINENO(n), n->n_col_offset, c->c_arena);
1707 }
1708 /* extract Index values and put them in a Tuple */
1709 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1710 if (!elts)
1711 return NULL;
1712 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1713 slc = (slice_ty)asdl_seq_GET(slices, j);
1714 assert(slc->kind == Index_kind && slc->v.Index.value);
1715 asdl_seq_SET(elts, j, slc->v.Index.value);
1716 }
1717 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1718 if (!e)
1719 return NULL;
1720 return Subscript(left_expr, Index(e, c->c_arena),
1721 Load, LINENO(n), n->n_col_offset, c->c_arena);
1722 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001723 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001724}
1725
1726static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001727ast_for_factor(struct compiling *c, const node *n)
1728{
1729 node *pfactor, *ppower, *patom, *pnum;
1730 expr_ty expression;
1731
1732 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001733 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001734 constant. The peephole optimizer already does something like
1735 this but it doesn't handle the case where the constant is
1736 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1737 PyLongObject.
1738 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001739 if (TYPE(CHILD(n, 0)) == MINUS &&
1740 NCH(n) == 2 &&
1741 TYPE((pfactor = CHILD(n, 1))) == factor &&
1742 NCH(pfactor) == 1 &&
1743 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1744 NCH(ppower) == 1 &&
1745 TYPE((patom = CHILD(ppower, 0))) == atom &&
1746 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1747 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1748 if (s == NULL)
1749 return NULL;
1750 s[0] = '-';
1751 strcpy(s + 1, STR(pnum));
1752 PyObject_FREE(STR(pnum));
1753 STR(pnum) = s;
1754 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001755 }
1756
1757 expression = ast_for_expr(c, CHILD(n, 1));
1758 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001759 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001760
1761 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001762 case PLUS:
1763 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1764 c->c_arena);
1765 case MINUS:
1766 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1767 c->c_arena);
1768 case TILDE:
1769 return UnaryOp(Invert, expression, LINENO(n),
1770 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001771 }
1772 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001773 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001774 return NULL;
1775}
1776
1777static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001778ast_for_power(struct compiling *c, const node *n)
1779{
1780 /* power: atom trailer* ('**' factor)*
1781 */
1782 int i;
1783 expr_ty e, tmp;
1784 REQ(n, power);
1785 e = ast_for_atom(c, CHILD(n, 0));
1786 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001787 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001788 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001789 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001790 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001791 node *ch = CHILD(n, i);
1792 if (TYPE(ch) != trailer)
1793 break;
1794 tmp = ast_for_trailer(c, ch, e);
1795 if (!tmp)
1796 return NULL;
1797 tmp->lineno = e->lineno;
1798 tmp->col_offset = e->col_offset;
1799 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001800 }
1801 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001802 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1803 if (!f)
1804 return NULL;
1805 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1806 if (!tmp)
1807 return NULL;
1808 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001809 }
1810 return e;
1811}
1812
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813/* Do not name a variable 'expr'! Will cause a compile error.
1814*/
1815
1816static expr_ty
1817ast_for_expr(struct compiling *c, const node *n)
1818{
1819 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001820 test: or_test ['if' or_test 'else' test] | lambdef
Brett Cannonfa84d922010-05-05 20:30:30 +00001821 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 and_test: not_test ('and' not_test)*
1823 not_test: 'not' not_test | comparison
1824 comparison: expr (comp_op expr)*
1825 expr: xor_expr ('|' xor_expr)*
1826 xor_expr: and_expr ('^' and_expr)*
1827 and_expr: shift_expr ('&' shift_expr)*
1828 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1829 arith_expr: term (('+'|'-') term)*
1830 term: factor (('*'|'/'|'%'|'//') factor)*
1831 factor: ('+'|'-'|'~') factor | power
1832 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001833
1834 As well as modified versions that exist for backward compatibility,
1835 to explicitly allow:
1836 [ x for x in lambda: 0, lambda: 1 ]
1837 (which would be ambiguous without these extra rules)
Brett Cannonfa84d922010-05-05 20:30:30 +00001838
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001839 old_test: or_test | old_lambdef
1840 old_lambdef: 'lambda' [vararglist] ':' old_test
1841
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 */
1843
1844 asdl_seq *seq;
1845 int i;
1846
1847 loop:
1848 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001849 case test:
1850 case old_test:
1851 if (TYPE(CHILD(n, 0)) == lambdef ||
1852 TYPE(CHILD(n, 0)) == old_lambdef)
1853 return ast_for_lambdef(c, CHILD(n, 0));
1854 else if (NCH(n) > 1)
1855 return ast_for_ifexpr(c, n);
1856 /* Fallthrough */
1857 case or_test:
1858 case and_test:
1859 if (NCH(n) == 1) {
1860 n = CHILD(n, 0);
1861 goto loop;
1862 }
1863 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1864 if (!seq)
1865 return NULL;
1866 for (i = 0; i < NCH(n); i += 2) {
1867 expr_ty e = ast_for_expr(c, CHILD(n, i));
1868 if (!e)
1869 return NULL;
1870 asdl_seq_SET(seq, i / 2, e);
1871 }
1872 if (!strcmp(STR(CHILD(n, 1)), "and"))
1873 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1874 c->c_arena);
1875 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1876 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1877 case not_test:
1878 if (NCH(n) == 1) {
1879 n = CHILD(n, 0);
1880 goto loop;
1881 }
1882 else {
1883 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1884 if (!expression)
1885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001887 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1888 c->c_arena);
1889 }
1890 case comparison:
1891 if (NCH(n) == 1) {
1892 n = CHILD(n, 0);
1893 goto loop;
1894 }
1895 else {
1896 expr_ty expression;
1897 asdl_int_seq *ops;
1898 asdl_seq *cmps;
1899 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1900 if (!ops)
1901 return NULL;
1902 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1903 if (!cmps) {
1904 return NULL;
1905 }
1906 for (i = 1; i < NCH(n); i += 2) {
1907 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001909 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001910 if (!newoperator) {
1911 return NULL;
1912 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001914 expression = ast_for_expr(c, CHILD(n, i + 1));
1915 if (!expression) {
1916 return NULL;
1917 }
Brett Cannonfa84d922010-05-05 20:30:30 +00001918
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001919 asdl_seq_SET(ops, i / 2, newoperator);
1920 asdl_seq_SET(cmps, i / 2, expression);
1921 }
1922 expression = ast_for_expr(c, CHILD(n, 0));
1923 if (!expression) {
1924 return NULL;
1925 }
Brett Cannonfa84d922010-05-05 20:30:30 +00001926
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001927 return Compare(expression, ops, cmps, LINENO(n),
1928 n->n_col_offset, c->c_arena);
1929 }
1930 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001932 /* The next five cases all handle BinOps. The main body of code
1933 is the same in each case, but the switch turned inside out to
1934 reuse the code for each type of operator.
1935 */
1936 case expr:
1937 case xor_expr:
1938 case and_expr:
1939 case shift_expr:
1940 case arith_expr:
1941 case term:
1942 if (NCH(n) == 1) {
1943 n = CHILD(n, 0);
1944 goto loop;
1945 }
1946 return ast_for_binop(c, n);
1947 case yield_expr: {
1948 expr_ty exp = NULL;
1949 if (NCH(n) == 2) {
1950 exp = ast_for_testlist(c, CHILD(n, 1));
1951 if (!exp)
1952 return NULL;
1953 }
1954 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1955 }
1956 case factor:
1957 if (NCH(n) == 1) {
1958 n = CHILD(n, 0);
1959 goto loop;
1960 }
1961 return ast_for_factor(c, n);
1962 case power:
1963 return ast_for_power(c, n);
1964 default:
1965 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001968 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 return NULL;
1970}
1971
1972static expr_ty
1973ast_for_call(struct compiling *c, const node *n, expr_ty func)
1974{
1975 /*
1976 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001977 | '**' test)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001978 argument: [test '='] test [comp_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 */
1980
1981 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001982 asdl_seq *args;
1983 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 expr_ty vararg = NULL, kwarg = NULL;
1985
1986 REQ(n, arglist);
1987
1988 nargs = 0;
1989 nkeywords = 0;
1990 ngens = 0;
1991 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001992 node *ch = CHILD(n, i);
1993 if (TYPE(ch) == argument) {
1994 if (NCH(ch) == 1)
1995 nargs++;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001996 else if (TYPE(CHILD(ch, 1)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001997 ngens++;
1998 else
1999 nkeywords++;
2000 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 }
2002 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002003 ast_error(n, "Generator expression must be parenthesized "
2004 "if not sole argument");
2005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006 }
2007
2008 if (nargs + nkeywords + ngens > 255) {
2009 ast_error(n, "more than 255 arguments");
2010 return NULL;
2011 }
2012
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002013 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002015 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002016 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 nargs = 0;
2020 nkeywords = 0;
2021 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002022 node *ch = CHILD(n, i);
2023 if (TYPE(ch) == argument) {
2024 expr_ty e;
2025 if (NCH(ch) == 1) {
2026 if (nkeywords) {
2027 ast_error(CHILD(ch, 0),
2028 "non-keyword arg after keyword arg");
2029 return NULL;
2030 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00002031 if (vararg) {
2032 ast_error(CHILD(ch, 0),
2033 "only named arguments may follow *expression");
2034 return NULL;
2035 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002036 e = ast_for_expr(c, CHILD(ch, 0));
2037 if (!e)
2038 return NULL;
2039 asdl_seq_SET(args, nargs++, e);
Brett Cannonfa84d922010-05-05 20:30:30 +00002040 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002041 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002042 e = ast_for_genexp(c, ch);
2043 if (!e)
2044 return NULL;
2045 asdl_seq_SET(args, nargs++, e);
2046 }
2047 else {
2048 keyword_ty kw;
2049 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00002050 int k;
2051 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052
Brett Cannonfa84d922010-05-05 20:30:30 +00002053 /* CHILD(ch, 0) is test, but must be an identifier? */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002054 e = ast_for_expr(c, CHILD(ch, 0));
2055 if (!e)
2056 return NULL;
2057 /* f(lambda x: x[0] = 3) ends up getting parsed with
2058 * LHS test = lambda x: x[0], and RHS test = 3.
2059 * SF bug 132313 points out that complaining about a keyword
2060 * then is very confusing.
2061 */
2062 if (e->kind == Lambda_kind) {
2063 ast_error(CHILD(ch, 0),
2064 "lambda cannot contain assignment");
2065 return NULL;
2066 } else if (e->kind != Name_kind) {
2067 ast_error(CHILD(ch, 0), "keyword can't be an expression");
2068 return NULL;
2069 }
2070 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00002071 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00002072 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00002073 for (k = 0; k < nkeywords; k++) {
2074 tmp = PyString_AS_STRING(
2075 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
2076 if (!strcmp(tmp, PyString_AS_STRING(key))) {
2077 ast_error(CHILD(ch, 0), "keyword argument repeated");
2078 return NULL;
2079 }
2080 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002081 e = ast_for_expr(c, CHILD(ch, 2));
2082 if (!e)
2083 return NULL;
2084 kw = keyword(key, e, c->c_arena);
2085 if (!kw)
2086 return NULL;
2087 asdl_seq_SET(keywords, nkeywords++, kw);
2088 }
2089 }
2090 else if (TYPE(ch) == STAR) {
2091 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002092 if (!vararg)
2093 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002094 i++;
2095 }
2096 else if (TYPE(ch) == DOUBLESTAR) {
2097 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00002098 if (!kwarg)
2099 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002100 i++;
2101 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 }
2103
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002104 return Call(func, args, keywords, vararg, kwarg, func->lineno,
2105 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106}
2107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002109ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002111 /* testlist_comp: test (',' test)* [','] */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002112 /* testlist: test (',' test)* [','] */
2113 /* testlist_safe: test (',' test)+ [','] */
2114 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 assert(NCH(n) > 0);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002116 if (TYPE(n) == testlist_comp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002117 if (NCH(n) > 1)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002118 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002119 }
2120 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002121 assert(TYPE(n) == testlist ||
2122 TYPE(n) == testlist_safe ||
2123 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002124 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002126 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002128 asdl_seq *tmp = seq_for_testlist(c, n);
2129 if (!tmp)
2130 return NULL;
2131 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002133}
2134
2135static expr_ty
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002136ast_for_testlist_comp(struct compiling *c, const node* n)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002137{
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002138 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
2139 /* argument: test [ comp_for ] */
2140 assert(TYPE(n) == testlist_comp || TYPE(n) == argument);
2141 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == comp_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002142 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002143 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002144}
2145
2146/* like ast_for_testlist() but returns a sequence */
2147static asdl_seq*
2148ast_for_class_bases(struct compiling *c, const node* n)
2149{
2150 /* testlist: test (',' test)* [','] */
2151 assert(NCH(n) > 0);
2152 REQ(n, testlist);
2153 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002154 expr_ty base;
2155 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2156 if (!bases)
2157 return NULL;
2158 base = ast_for_expr(c, CHILD(n, 0));
2159 if (!base)
2160 return NULL;
2161 asdl_seq_SET(bases, 0, base);
2162 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002163 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002164
2165 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166}
2167
2168static stmt_ty
2169ast_for_expr_stmt(struct compiling *c, const node *n)
2170{
2171 REQ(n, expr_stmt);
Brett Cannonfa84d922010-05-05 20:30:30 +00002172 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002173 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 testlist: test (',' test)* [',']
2175 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002176 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 test: ... here starts the operator precendence dance
2178 */
2179
2180 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002181 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2182 if (!e)
2183 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002185 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 }
2187 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002188 expr_ty expr1, expr2;
2189 operator_ty newoperator;
2190 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002192 expr1 = ast_for_testlist(c, ch);
2193 if (!expr1)
2194 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002195 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002196 return NULL;
Benjamin Peterson7adbb5a2009-10-03 20:23:24 +00002197 /* set_context checks that most expressions are not the left side.
2198 Augmented assignments can only have a name, a subscript, or an
2199 attribute on the left, though, so we have to explicitly check for
2200 those. */
2201 switch (expr1->kind) {
2202 case Name_kind:
2203 case Attribute_kind:
2204 case Subscript_kind:
2205 break;
2206 default:
2207 ast_error(ch, "illegal expression for augmented assignment");
2208 return NULL;
2209 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002211 ch = CHILD(n, 2);
2212 if (TYPE(ch) == testlist)
2213 expr2 = ast_for_testlist(c, ch);
2214 else
2215 expr2 = ast_for_expr(c, ch);
2216 if (!expr2)
2217 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002219 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002220 if (!newoperator)
2221 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002223 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2224 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 }
2226 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002227 int i;
2228 asdl_seq *targets;
2229 node *value;
2230 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002232 /* a normal assignment */
2233 REQ(CHILD(n, 1), EQUAL);
2234 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2235 if (!targets)
2236 return NULL;
2237 for (i = 0; i < NCH(n) - 2; i += 2) {
2238 expr_ty e;
2239 node *ch = CHILD(n, i);
Benjamin Petersonb2664812009-06-11 17:49:38 +00002240 if (TYPE(ch) == yield_expr) {
2241 ast_error(ch, "assignment to yield expression not possible");
2242 return NULL;
2243 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002244 e = ast_for_testlist(c, ch);
Brett Cannonfa84d922010-05-05 20:30:30 +00002245 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002246 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +00002248 /* set context to assign */
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002249 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002250 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002252 asdl_seq_SET(targets, i / 2, e);
2253 }
2254 value = CHILD(n, NCH(n) - 1);
2255 if (TYPE(value) == testlist)
2256 expression = ast_for_testlist(c, value);
2257 else
2258 expression = ast_for_expr(c, value);
2259 if (!expression)
2260 return NULL;
2261 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2262 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264}
2265
2266static stmt_ty
2267ast_for_print_stmt(struct compiling *c, const node *n)
2268{
2269 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002270 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 */
2272 expr_ty dest = NULL, expression;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002273 asdl_seq *seq = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 bool nl;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002275 int i, j, values_count, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276
2277 REQ(n, print_stmt);
2278 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002279 dest = ast_for_expr(c, CHILD(n, 2));
2280 if (!dest)
2281 return NULL;
2282 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 }
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002284 values_count = (NCH(n) + 1 - start) / 2;
2285 if (values_count) {
2286 seq = asdl_seq_new(values_count, c->c_arena);
2287 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002288 return NULL;
Benjamin Peterson5d1ff942009-06-13 16:19:19 +00002289 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
2290 expression = ast_for_expr(c, CHILD(n, i));
2291 if (!expression)
2292 return NULL;
2293 asdl_seq_SET(seq, j, expression);
2294 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 }
2296 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002297 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298}
2299
2300static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002301ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302{
2303 asdl_seq *seq;
2304 int i;
2305 expr_ty e;
2306
2307 REQ(n, exprlist);
2308
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002309 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002311 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002313 e = ast_for_expr(c, CHILD(n, i));
2314 if (!e)
2315 return NULL;
2316 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002317 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002318 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319 }
2320 return seq;
2321}
2322
2323static stmt_ty
2324ast_for_del_stmt(struct compiling *c, const node *n)
2325{
2326 asdl_seq *expr_list;
Brett Cannonfa84d922010-05-05 20:30:30 +00002327
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 /* del_stmt: 'del' exprlist */
2329 REQ(n, del_stmt);
2330
2331 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2332 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002333 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002334 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335}
2336
2337static stmt_ty
2338ast_for_flow_stmt(struct compiling *c, const node *n)
2339{
2340 /*
2341 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002342 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 break_stmt: 'break'
2344 continue_stmt: 'continue'
2345 return_stmt: 'return' [testlist]
2346 yield_stmt: yield_expr
2347 yield_expr: 'yield' testlist
2348 raise_stmt: 'raise' [test [',' test [',' test]]]
2349 */
2350 node *ch;
2351
2352 REQ(n, flow_stmt);
2353 ch = CHILD(n, 0);
2354 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002355 case break_stmt:
2356 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2357 case continue_stmt:
2358 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2359 case yield_stmt: { /* will reduce to yield_expr */
2360 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2361 if (!exp)
2362 return NULL;
2363 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2364 }
2365 case return_stmt:
2366 if (NCH(ch) == 1)
2367 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2368 else {
2369 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2370 if (!expression)
2371 return NULL;
2372 return Return(expression, LINENO(n), n->n_col_offset,
2373 c->c_arena);
2374 }
2375 case raise_stmt:
2376 if (NCH(ch) == 1)
2377 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2378 c->c_arena);
2379 else if (NCH(ch) == 2) {
2380 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2381 if (!expression)
2382 return NULL;
2383 return Raise(expression, NULL, NULL, LINENO(n),
2384 n->n_col_offset, c->c_arena);
2385 }
2386 else if (NCH(ch) == 4) {
2387 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002389 expr1 = ast_for_expr(c, CHILD(ch, 1));
2390 if (!expr1)
2391 return NULL;
2392 expr2 = ast_for_expr(c, CHILD(ch, 3));
2393 if (!expr2)
2394 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002396 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2397 c->c_arena);
2398 }
2399 else if (NCH(ch) == 6) {
2400 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002402 expr1 = ast_for_expr(c, CHILD(ch, 1));
2403 if (!expr1)
2404 return NULL;
2405 expr2 = ast_for_expr(c, CHILD(ch, 3));
2406 if (!expr2)
2407 return NULL;
2408 expr3 = ast_for_expr(c, CHILD(ch, 5));
2409 if (!expr3)
2410 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002411
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002412 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2413 c->c_arena);
2414 }
2415 default:
2416 PyErr_Format(PyExc_SystemError,
2417 "unexpected flow_stmt: %d", TYPE(ch));
2418 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002420
2421 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2422 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423}
2424
2425static alias_ty
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002426alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427{
2428 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002429 import_as_name: NAME ['as' NAME]
2430 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 dotted_name: NAME ('.' NAME)*
2432 */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002433 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435 loop:
2436 switch (TYPE(n)) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002437 case import_as_name: {
2438 node *name_node = CHILD(n, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002439 str = NULL;
2440 if (NCH(n) == 3) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002441 node *str_node = CHILD(n, 2);
2442 if (store && !forbidden_check(c, str_node, STR(str_node)))
2443 return NULL;
2444 str = NEW_IDENTIFIER(str_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002445 if (!str)
2446 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002447 }
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002448 else {
2449 if (!forbidden_check(c, name_node, STR(name_node)))
2450 return NULL;
2451 }
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002452 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002453 if (!name)
2454 return NULL;
2455 return alias(name, str, c->c_arena);
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002456 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002457 case dotted_as_name:
2458 if (NCH(n) == 1) {
2459 n = CHILD(n, 0);
2460 goto loop;
2461 }
2462 else {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002463 node *asname_node = CHILD(n, 2);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002464 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002465 if (!a)
2466 return NULL;
2467 assert(!a->asname);
Benjamin Petersond1f5a592009-06-13 13:06:21 +00002468 if (!forbidden_check(c, asname_node, STR(asname_node)))
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002469 return NULL;
2470 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002471 if (!a->asname)
2472 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002473 return a;
2474 }
2475 break;
2476 case dotted_name:
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002477 if (NCH(n) == 1) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002478 node *name_node = CHILD(n, 0);
2479 if (store && !forbidden_check(c, name_node, STR(name_node)))
2480 return NULL;
2481 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00002482 if (!name)
2483 return NULL;
2484 return alias(name, NULL, c->c_arena);
2485 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002486 else {
2487 /* Create a string of the form "a.b.c" */
2488 int i;
2489 size_t len;
2490 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002492 len = 0;
2493 for (i = 0; i < NCH(n); i += 2)
2494 /* length of string plus one for the dot */
2495 len += strlen(STR(CHILD(n, i))) + 1;
2496 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002497 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002498 if (!str)
2499 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002500 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002501 if (!s)
2502 return NULL;
2503 for (i = 0; i < NCH(n); i += 2) {
2504 char *sch = STR(CHILD(n, i));
2505 strcpy(s, STR(CHILD(n, i)));
2506 s += strlen(sch);
2507 *s++ = '.';
2508 }
2509 --s;
2510 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002511 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002512 PyArena_AddPyObject(c->c_arena, str);
2513 return alias(str, NULL, c->c_arena);
2514 }
2515 break;
2516 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002517 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002518 PyArena_AddPyObject(c->c_arena, str);
2519 return alias(str, NULL, c->c_arena);
2520 default:
2521 PyErr_Format(PyExc_SystemError,
2522 "unexpected import name: %d", TYPE(n));
2523 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002525
2526 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 return NULL;
2528}
2529
2530static stmt_ty
2531ast_for_import_stmt(struct compiling *c, const node *n)
2532{
2533 /*
2534 import_stmt: import_name | import_from
2535 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002536 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002537 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002539 int lineno;
2540 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 int i;
2542 asdl_seq *aliases;
2543
2544 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002545 lineno = LINENO(n);
2546 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002548 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002549 n = CHILD(n, 1);
2550 REQ(n, dotted_as_names);
2551 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2552 if (!aliases)
2553 return NULL;
2554 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002555 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002556 if (!import_alias)
2557 return NULL;
2558 asdl_seq_SET(aliases, i / 2, import_alias);
2559 }
2560 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002562 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002563 int n_children;
2564 int idx, ndots = 0;
2565 alias_ty mod = NULL;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002566 identifier modname = NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002567
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002568 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002569 optional module name */
2570 for (idx = 1; idx < NCH(n); idx++) {
2571 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002572 mod = alias_for_import_name(c, CHILD(n, idx), 0);
2573 if (!mod)
2574 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002575 idx++;
2576 break;
2577 } else if (TYPE(CHILD(n, idx)) != DOT) {
2578 break;
2579 }
2580 ndots++;
2581 }
2582 idx++; /* skip over the 'import' keyword */
2583 switch (TYPE(CHILD(n, idx))) {
2584 case STAR:
2585 /* from ... import * */
2586 n = CHILD(n, idx);
2587 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002588 break;
2589 case LPAR:
2590 /* from ... import (x, y, z) */
2591 n = CHILD(n, idx + 1);
2592 n_children = NCH(n);
2593 break;
2594 case import_as_names:
2595 /* from ... import x, y, z */
2596 n = CHILD(n, idx);
2597 n_children = NCH(n);
2598 if (n_children % 2 == 0) {
2599 ast_error(n, "trailing comma not allowed without"
2600 " surrounding parentheses");
2601 return NULL;
2602 }
2603 break;
2604 default:
2605 ast_error(n, "Unexpected node-type in from-import");
2606 return NULL;
2607 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002609 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2610 if (!aliases)
2611 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002613 /* handle "from ... import *" special b/c there's no children */
2614 if (TYPE(n) == STAR) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002615 alias_ty import_alias = alias_for_import_name(c, n, 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002616 if (!import_alias)
2617 return NULL;
2618 asdl_seq_SET(aliases, 0, import_alias);
2619 }
2620 else {
2621 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson565e1b62009-06-13 03:46:30 +00002622 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002623 if (!import_alias)
2624 return NULL;
2625 asdl_seq_SET(aliases, i / 2, import_alias);
2626 }
2627 }
2628 if (mod != NULL)
2629 modname = mod->name;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002630 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2631 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 }
Neal Norwitz79792652005-11-14 04:25:03 +00002633 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002634 "unknown import statement: starts with command '%s'",
2635 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
2637}
2638
2639static stmt_ty
2640ast_for_global_stmt(struct compiling *c, const node *n)
2641{
2642 /* global_stmt: 'global' NAME (',' NAME)* */
2643 identifier name;
2644 asdl_seq *s;
2645 int i;
2646
2647 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002648 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002650 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002652 name = NEW_IDENTIFIER(CHILD(n, i));
2653 if (!name)
2654 return NULL;
2655 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002657 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658}
2659
2660static stmt_ty
2661ast_for_exec_stmt(struct compiling *c, const node *n)
2662{
2663 expr_ty expr1, globals = NULL, locals = NULL;
2664 int n_children = NCH(n);
2665 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002666 PyErr_Format(PyExc_SystemError,
2667 "poorly formed 'exec' statement: %d parts to statement",
2668 n_children);
2669 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 }
2671
2672 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2673 REQ(n, exec_stmt);
2674 expr1 = ast_for_expr(c, CHILD(n, 1));
2675 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002676 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002678 globals = ast_for_expr(c, CHILD(n, 3));
2679 if (!globals)
2680 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681 }
2682 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002683 locals = ast_for_expr(c, CHILD(n, 5));
2684 if (!locals)
2685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 }
2687
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002688 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2689 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690}
2691
2692static stmt_ty
2693ast_for_assert_stmt(struct compiling *c, const node *n)
2694{
2695 /* assert_stmt: 'assert' test [',' test] */
2696 REQ(n, assert_stmt);
2697 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002698 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2699 if (!expression)
2700 return NULL;
2701 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2702 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 }
2704 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002705 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002707 expr1 = ast_for_expr(c, CHILD(n, 1));
2708 if (!expr1)
2709 return NULL;
2710 expr2 = ast_for_expr(c, CHILD(n, 3));
2711 if (!expr2)
2712 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002713
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002714 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 }
Neal Norwitz79792652005-11-14 04:25:03 +00002716 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002717 "improper number of parts to 'assert' statement: %d",
2718 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 return NULL;
2720}
2721
2722static asdl_seq *
2723ast_for_suite(struct compiling *c, const node *n)
2724{
2725 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002726 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727 stmt_ty s;
2728 int i, total, num, end, pos = 0;
2729 node *ch;
2730
2731 REQ(n, suite);
2732
2733 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002734 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002736 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002738 n = CHILD(n, 0);
2739 /* simple_stmt always ends with a NEWLINE,
Brett Cannonfa84d922010-05-05 20:30:30 +00002740 and may have a trailing SEMI
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002741 */
2742 end = NCH(n) - 1;
2743 if (TYPE(CHILD(n, end - 1)) == SEMI)
2744 end--;
2745 /* loop by 2 to skip semi-colons */
2746 for (i = 0; i < end; i += 2) {
2747 ch = CHILD(n, i);
2748 s = ast_for_stmt(c, ch);
2749 if (!s)
2750 return NULL;
2751 asdl_seq_SET(seq, pos++, s);
2752 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 }
2754 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002755 for (i = 2; i < (NCH(n) - 1); i++) {
2756 ch = CHILD(n, i);
2757 REQ(ch, stmt);
2758 num = num_stmts(ch);
2759 if (num == 1) {
2760 /* small_stmt or compound_stmt with only one child */
2761 s = ast_for_stmt(c, ch);
2762 if (!s)
2763 return NULL;
2764 asdl_seq_SET(seq, pos++, s);
2765 }
2766 else {
2767 int j;
2768 ch = CHILD(ch, 0);
2769 REQ(ch, simple_stmt);
2770 for (j = 0; j < NCH(ch); j += 2) {
2771 /* statement terminates with a semi-colon ';' */
2772 if (NCH(CHILD(ch, j)) == 0) {
2773 assert((j + 1) == NCH(ch));
2774 break;
2775 }
2776 s = ast_for_stmt(c, CHILD(ch, j));
2777 if (!s)
2778 return NULL;
2779 asdl_seq_SET(seq, pos++, s);
2780 }
2781 }
2782 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 }
2784 assert(pos == seq->size);
2785 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786}
2787
2788static stmt_ty
2789ast_for_if_stmt(struct compiling *c, const node *n)
2790{
2791 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2792 ['else' ':' suite]
2793 */
2794 char *s;
2795
2796 REQ(n, if_stmt);
2797
2798 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002799 expr_ty expression;
2800 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002802 expression = ast_for_expr(c, CHILD(n, 1));
2803 if (!expression)
2804 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002805 suite_seq = ast_for_suite(c, CHILD(n, 3));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002806 if (!suite_seq)
2807 return NULL;
Brett Cannonfa84d922010-05-05 20:30:30 +00002808
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002809 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2810 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002812
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 s = STR(CHILD(n, 4));
2814 /* s[2], the third character in the string, will be
2815 's' for el_s_e, or
2816 'i' for el_i_f
2817 */
2818 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002819 expr_ty expression;
2820 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002822 expression = ast_for_expr(c, CHILD(n, 1));
2823 if (!expression)
2824 return NULL;
2825 seq1 = ast_for_suite(c, CHILD(n, 3));
2826 if (!seq1)
2827 return NULL;
2828 seq2 = ast_for_suite(c, CHILD(n, 6));
2829 if (!seq2)
2830 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002832 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2833 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 }
2835 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002836 int i, n_elif, has_else = 0;
2837 expr_ty expression;
2838 asdl_seq *suite_seq;
2839 asdl_seq *orelse = NULL;
2840 n_elif = NCH(n) - 4;
2841 /* must reference the child n_elif+1 since 'else' token is third,
2842 not fourth, child from the end. */
2843 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2844 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2845 has_else = 1;
2846 n_elif -= 3;
2847 }
2848 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002850 if (has_else) {
2851 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002853 orelse = asdl_seq_new(1, c->c_arena);
2854 if (!orelse)
2855 return NULL;
2856 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2857 if (!expression)
2858 return NULL;
2859 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2860 if (!suite_seq)
2861 return NULL;
2862 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2863 if (!suite_seq2)
2864 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865
Brett Cannonfa84d922010-05-05 20:30:30 +00002866 asdl_seq_SET(orelse, 0,
2867 If(expression, suite_seq, suite_seq2,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002868 LINENO(CHILD(n, NCH(n) - 6)),
2869 CHILD(n, NCH(n) - 6)->n_col_offset,
2870 c->c_arena));
2871 /* the just-created orelse handled the last elif */
2872 n_elif--;
2873 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002875 for (i = 0; i < n_elif; i++) {
2876 int off = 5 + (n_elif - i - 1) * 4;
2877 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2878 if (!newobj)
2879 return NULL;
2880 expression = ast_for_expr(c, CHILD(n, off));
2881 if (!expression)
2882 return NULL;
2883 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2884 if (!suite_seq)
2885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002887 asdl_seq_SET(newobj, 0,
Brett Cannonfa84d922010-05-05 20:30:30 +00002888 If(expression, suite_seq, orelse,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002889 LINENO(CHILD(n, off)),
2890 CHILD(n, off)->n_col_offset, c->c_arena));
2891 orelse = newobj;
2892 }
2893 expression = ast_for_expr(c, CHILD(n, 1));
2894 if (!expression)
2895 return NULL;
2896 suite_seq = ast_for_suite(c, CHILD(n, 3));
2897 if (!suite_seq)
2898 return NULL;
2899 return If(expression, suite_seq, orelse,
2900 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002902
2903 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002904 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002905 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906}
2907
2908static stmt_ty
2909ast_for_while_stmt(struct compiling *c, const node *n)
2910{
2911 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2912 REQ(n, while_stmt);
2913
2914 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002915 expr_ty expression;
2916 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002918 expression = ast_for_expr(c, CHILD(n, 1));
2919 if (!expression)
2920 return NULL;
2921 suite_seq = ast_for_suite(c, CHILD(n, 3));
2922 if (!suite_seq)
2923 return NULL;
2924 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2925 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926 }
2927 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002928 expr_ty expression;
2929 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002931 expression = ast_for_expr(c, CHILD(n, 1));
2932 if (!expression)
2933 return NULL;
2934 seq1 = ast_for_suite(c, CHILD(n, 3));
2935 if (!seq1)
2936 return NULL;
2937 seq2 = ast_for_suite(c, CHILD(n, 6));
2938 if (!seq2)
2939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002941 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2942 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002944
2945 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002946 "wrong number of tokens for 'while' statement: %d",
2947 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002948 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949}
2950
2951static stmt_ty
2952ast_for_for_stmt(struct compiling *c, const node *n)
2953{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002954 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 expr_ty expression;
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002956 expr_ty target, first;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002957 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2959 REQ(n, for_stmt);
2960
2961 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002962 seq = ast_for_suite(c, CHILD(n, 8));
2963 if (!seq)
2964 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 }
2966
Neal Norwitzedef2be2006-07-12 05:26:17 +00002967 node_target = CHILD(n, 1);
2968 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002969 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002970 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002971 /* Check the # of children rather than the length of _target, since
2972 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002973 first = (expr_ty)asdl_seq_GET(_target, 0);
Neal Norwitzedef2be2006-07-12 05:26:17 +00002974 if (NCH(node_target) == 1)
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002975 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 else
Benjamin Petersonc0ba8282009-08-15 22:59:21 +00002977 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002979 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002980 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002981 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002983 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002984 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002986 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002987 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988}
2989
2990static excepthandler_ty
2991ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2992{
Collin Winter62903052007-05-18 23:11:24 +00002993 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994 REQ(exc, except_clause);
2995 REQ(body, suite);
2996
2997 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002998 asdl_seq *suite_seq = ast_for_suite(c, body);
2999 if (!suite_seq)
3000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001
Georg Brandla48f3ab2008-03-30 06:40:17 +00003002 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003003 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 }
3005 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003006 expr_ty expression;
3007 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003009 expression = ast_for_expr(c, CHILD(exc, 1));
3010 if (!expression)
3011 return NULL;
3012 suite_seq = ast_for_suite(c, body);
3013 if (!suite_seq)
3014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015
Georg Brandla48f3ab2008-03-30 06:40:17 +00003016 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003017 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 }
3019 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003020 asdl_seq *suite_seq;
3021 expr_ty expression;
3022 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
3023 if (!e)
3024 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003025 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003026 return NULL;
3027 expression = ast_for_expr(c, CHILD(exc, 1));
3028 if (!expression)
3029 return NULL;
3030 suite_seq = ast_for_suite(c, body);
3031 if (!suite_seq)
3032 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033
Georg Brandla48f3ab2008-03-30 06:40:17 +00003034 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003035 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003037
3038 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003039 "wrong number of children for 'except' clause: %d",
3040 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042}
3043
3044static stmt_ty
3045ast_for_try_stmt(struct compiling *c, const node *n)
3046{
Neal Norwitzf599f422005-12-17 21:33:47 +00003047 const int nch = NCH(n);
3048 int n_except = (nch - 3)/3;
3049 asdl_seq *body, *orelse = NULL, *finally = NULL;
3050
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 REQ(n, try_stmt);
3052
Neal Norwitzf599f422005-12-17 21:33:47 +00003053 body = ast_for_suite(c, CHILD(n, 2));
3054 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003055 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056
Neal Norwitzf599f422005-12-17 21:33:47 +00003057 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003058 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3059 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3060 /* we can assume it's an "else",
3061 because nch >= 9 for try-else-finally and
3062 it would otherwise have a type of except_clause */
3063 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3064 if (orelse == NULL)
3065 return NULL;
3066 n_except--;
3067 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003069 finally = ast_for_suite(c, CHILD(n, nch - 1));
3070 if (finally == NULL)
3071 return NULL;
3072 n_except--;
3073 }
3074 else {
3075 /* we can assume it's an "else",
3076 otherwise it would have a type of except_clause */
3077 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3078 if (orelse == NULL)
3079 return NULL;
3080 n_except--;
3081 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003083 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003084 ast_error(n, "malformed 'try' statement");
3085 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 }
Brett Cannonfa84d922010-05-05 20:30:30 +00003087
Neal Norwitzf599f422005-12-17 21:33:47 +00003088 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003089 int i;
3090 stmt_ty except_st;
3091 /* process except statements to create a try ... except */
3092 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
3093 if (handlers == NULL)
3094 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003095
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003096 for (i = 0; i < n_except; i++) {
3097 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3098 CHILD(n, 5 + i * 3));
3099 if (!e)
3100 return NULL;
3101 asdl_seq_SET(handlers, i, e);
3102 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003103
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003104 except_st = TryExcept(body, handlers, orelse, LINENO(n),
3105 n->n_col_offset, c->c_arena);
3106 if (!finally)
3107 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00003108
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003109 /* if a 'finally' is present too, we nest the TryExcept within a
3110 TryFinally to emulate try ... except ... finally */
3111 body = asdl_seq_new(1, c->c_arena);
3112 if (body == NULL)
3113 return NULL;
3114 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00003115 }
3116
3117 /* must be a try ... finally (except clauses are in body, if any exist) */
3118 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003119 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120}
3121
Georg Brandl944f6842009-05-25 21:02:56 +00003122/* with_item: test ['as' expr] */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003123static stmt_ty
Georg Brandl944f6842009-05-25 21:02:56 +00003124ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003125{
3126 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003127
Georg Brandl944f6842009-05-25 21:02:56 +00003128 REQ(n, with_item);
3129 context_expr = ast_for_expr(c, CHILD(n, 0));
Collin Winter77c67bd2007-03-16 04:11:30 +00003130 if (!context_expr)
3131 return NULL;
Georg Brandl944f6842009-05-25 21:02:56 +00003132 if (NCH(n) == 3) {
3133 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003134
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003135 if (!optional_vars) {
3136 return NULL;
3137 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003138 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003139 return NULL;
3140 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003141 }
3142
Georg Brandl944f6842009-05-25 21:02:56 +00003143 return With(context_expr, optional_vars, content, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003144 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003145}
3146
Georg Brandl944f6842009-05-25 21:02:56 +00003147/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3148static stmt_ty
3149ast_for_with_stmt(struct compiling *c, const node *n)
3150{
3151 int i;
3152 stmt_ty ret;
3153 asdl_seq *inner;
3154
3155 REQ(n, with_stmt);
3156
3157 /* process the with items inside-out */
3158 i = NCH(n) - 1;
3159 /* the suite of the innermost with item is the suite of the with stmt */
3160 inner = ast_for_suite(c, CHILD(n, i));
3161 if (!inner)
3162 return NULL;
3163
3164 for (;;) {
3165 i -= 2;
3166 ret = ast_for_with_item(c, CHILD(n, i), inner);
3167 if (!ret)
3168 return NULL;
3169 /* was this the last item? */
3170 if (i == 1)
3171 break;
3172 /* if not, wrap the result so far in a new sequence */
3173 inner = asdl_seq_new(1, c->c_arena);
3174 if (!inner)
3175 return NULL;
3176 asdl_seq_SET(inner, 0, ret);
3177 }
3178
3179 return ret;
3180}
3181
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003183ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184{
3185 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003186 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187 asdl_seq *bases, *s;
Brett Cannonfa84d922010-05-05 20:30:30 +00003188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 REQ(n, classdef);
3190
Benjamin Petersond5efd202008-06-08 22:52:37 +00003191 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003192 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193
3194 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003195 s = ast_for_suite(c, CHILD(n, 3));
3196 if (!s)
3197 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003198 classname = NEW_IDENTIFIER(CHILD(n, 1));
3199 if (!classname)
3200 return NULL;
3201 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3202 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 }
3204 /* check for empty base list */
3205 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003206 s = ast_for_suite(c, CHILD(n,5));
3207 if (!s)
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003208 return NULL;
3209 classname = NEW_IDENTIFIER(CHILD(n, 1));
3210 if (!classname)
3211 return NULL;
3212 return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
3213 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 }
3215
3216 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003217 bases = ast_for_class_bases(c, CHILD(n, 3));
3218 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003219 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220
3221 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003222 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003223 return NULL;
Benjamin Peterson9e6310d2008-11-25 03:43:14 +00003224 classname = NEW_IDENTIFIER(CHILD(n, 1));
3225 if (!classname)
3226 return NULL;
3227 return ClassDef(classname, bases, s, decorator_seq,
Christian Heimes5224d282008-02-23 15:01:05 +00003228 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229}
3230
3231static stmt_ty
3232ast_for_stmt(struct compiling *c, const node *n)
3233{
3234 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003235 assert(NCH(n) == 1);
3236 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 }
3238 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003239 assert(num_stmts(n) == 1);
3240 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241 }
3242 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003243 n = CHILD(n, 0);
3244 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3245 | flow_stmt | import_stmt | global_stmt | exec_stmt
3246 | assert_stmt
3247 */
3248 switch (TYPE(n)) {
3249 case expr_stmt:
3250 return ast_for_expr_stmt(c, n);
3251 case print_stmt:
3252 return ast_for_print_stmt(c, n);
3253 case del_stmt:
3254 return ast_for_del_stmt(c, n);
3255 case pass_stmt:
3256 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3257 case flow_stmt:
3258 return ast_for_flow_stmt(c, n);
3259 case import_stmt:
3260 return ast_for_import_stmt(c, n);
3261 case global_stmt:
3262 return ast_for_global_stmt(c, n);
3263 case exec_stmt:
3264 return ast_for_exec_stmt(c, n);
3265 case assert_stmt:
3266 return ast_for_assert_stmt(c, n);
3267 default:
3268 PyErr_Format(PyExc_SystemError,
3269 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3270 TYPE(n), NCH(n));
3271 return NULL;
3272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 }
3274 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003275 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003276 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003277 */
3278 node *ch = CHILD(n, 0);
3279 REQ(n, compound_stmt);
3280 switch (TYPE(ch)) {
3281 case if_stmt:
3282 return ast_for_if_stmt(c, ch);
3283 case while_stmt:
3284 return ast_for_while_stmt(c, ch);
3285 case for_stmt:
3286 return ast_for_for_stmt(c, ch);
3287 case try_stmt:
3288 return ast_for_try_stmt(c, ch);
3289 case with_stmt:
3290 return ast_for_with_stmt(c, ch);
3291 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003292 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003293 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003294 return ast_for_classdef(c, ch, NULL);
3295 case decorated:
3296 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003297 default:
3298 PyErr_Format(PyExc_SystemError,
3299 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3300 TYPE(n), NCH(n));
3301 return NULL;
3302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 }
3304}
3305
3306static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003307parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003309 const char *end;
3310 long x;
3311 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003313 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003314 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315#endif
3316
Mark Dickinson422ce062008-12-05 17:59:46 +00003317 assert(s != NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003318 errno = 0;
3319 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003321 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003323 if (*end == 'l' || *end == 'L')
3324 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003325 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003326 if (*end == '\0') {
3327 if (errno != 0)
3328 return PyLong_FromString((char *)s, (char **)0, 0);
3329 return PyInt_FromLong(x);
3330 }
3331 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003333 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003334 complex.real = 0.;
Eric Smithabc9f702009-10-27 18:33:14 +00003335 complex.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3336 if (complex.imag == -1.0 && PyErr_Occurred())
3337 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003338 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003339 }
3340 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003342 {
Eric Smithabc9f702009-10-27 18:33:14 +00003343 dx = PyOS_string_to_double(s, NULL, NULL);
3344 if (dx == -1.0 && PyErr_Occurred())
3345 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003346 return PyFloat_FromDouble(dx);
3347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348}
3349
3350static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003351decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352{
3353#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003354 Py_FatalError("decode_utf8 should not be called in this build.");
3355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003357 PyObject *u, *v;
3358 char *s, *t;
3359 t = s = (char *)*sPtr;
3360 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3361 while (s < end && (*s & 0x80)) s++;
3362 *sPtr = s;
3363 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3364 if (u == NULL)
3365 return NULL;
3366 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3367 Py_DECREF(u);
3368 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369#endif
3370}
3371
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003372#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003374decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375{
Brett Cannonfa84d922010-05-05 20:30:30 +00003376 PyObject *v;
3377 PyObject *u = NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003378 char *buf;
3379 char *p;
3380 const char *end;
Brett Cannonfa84d922010-05-05 20:30:30 +00003381 if (encoding != NULL && strcmp(encoding, "iso-8859-1")) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003382 /* check for integer overflow */
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003383 if (len > PY_SIZE_MAX / 6)
Gregory P. Smith9d534572008-06-11 07:41:16 +00003384 return NULL;
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003385 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3386 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3387 u = PyString_FromStringAndSize((char *)NULL, len * 6);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003388 if (u == NULL)
3389 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003390 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003391 end = s + len;
3392 while (s < end) {
3393 if (*s == '\\') {
3394 *p++ = *s++;
3395 if (*s & 0x80) {
3396 strcpy(p, "u005c");
3397 p += 5;
3398 }
3399 }
3400 if (*s & 0x80) { /* XXX inefficient */
3401 PyObject *w;
3402 char *r;
3403 Py_ssize_t rn, i;
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003404 w = decode_utf8(c, &s, end, "utf-32-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003405 if (w == NULL) {
3406 Py_DECREF(u);
3407 return NULL;
3408 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003409 r = PyString_AsString(w);
3410 rn = PyString_Size(w);
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003411 assert(rn % 4 == 0);
3412 for (i = 0; i < rn; i += 4) {
3413 sprintf(p, "\\U%02x%02x%02x%02x",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003414 r[i + 0] & 0xFF,
Benjamin Petersonea0e3b02009-10-29 01:49:07 +00003415 r[i + 1] & 0xFF,
3416 r[i + 2] & 0xFF,
3417 r[i + 3] & 0xFF);
3418 p += 10;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003419 }
3420 Py_DECREF(w);
3421 } else {
3422 *p++ = *s++;
3423 }
3424 }
3425 len = p - buf;
3426 s = buf;
3427 }
3428 if (rawmode)
3429 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3430 else
3431 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3432 Py_XDECREF(u);
3433 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003435#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436
3437/* s is a Python string literal, including the bracketing quote characters,
3438 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3439 * parsestr parses it, and returns the decoded Python string object.
3440 */
3441static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003442parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003444 size_t len;
3445 int quote = Py_CHARMASK(*s);
3446 int rawmode = 0;
3447 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003448 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003450 if (isalpha(quote) || quote == '_') {
3451 if (quote == 'u' || quote == 'U') {
3452 quote = *++s;
3453 unicode = 1;
3454 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003455 if (quote == 'b' || quote == 'B') {
3456 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003457 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003458 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003459 if (quote == 'r' || quote == 'R') {
3460 quote = *++s;
3461 rawmode = 1;
3462 }
3463 }
3464 if (quote != '\'' && quote != '\"') {
3465 PyErr_BadInternalCall();
3466 return NULL;
3467 }
3468 s++;
3469 len = strlen(s);
3470 if (len > INT_MAX) {
Brett Cannonfa84d922010-05-05 20:30:30 +00003471 PyErr_SetString(PyExc_OverflowError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003472 "string to parse is too long");
3473 return NULL;
3474 }
3475 if (s[--len] != quote) {
3476 PyErr_BadInternalCall();
3477 return NULL;
3478 }
3479 if (len >= 4 && s[0] == quote && s[1] == quote) {
3480 s += 2;
3481 len -= 2;
3482 if (s[--len] != quote || s[--len] != quote) {
3483 PyErr_BadInternalCall();
3484 return NULL;
3485 }
3486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003488 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003489 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003490 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003492 need_encoding = (c->c_encoding != NULL &&
3493 strcmp(c->c_encoding, "utf-8") != 0 &&
3494 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003495 if (rawmode || strchr(s, '\\') == NULL) {
3496 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003498 /* This should not happen - we never see any other
3499 encoding. */
3500 Py_FatalError(
3501 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003503 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3504 if (u == NULL)
3505 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003506 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003507 Py_DECREF(u);
3508 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003510 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003511 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003512 }
3513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003515 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003516 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517}
3518
3519/* Build a Python string object out of a STRING atom. This takes care of
3520 * compile-time literal catenation, calling parsestr() on each piece, and
3521 * pasting the intermediate results together.
3522 */
3523static PyObject *
3524parsestrplus(struct compiling *c, const node *n)
3525{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003526 PyObject *v;
3527 int i;
3528 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003529 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003530 /* String literal concatenation */
3531 for (i = 1; i < NCH(n); i++) {
3532 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003533 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003534 if (s == NULL)
3535 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003536 if (PyString_Check(v) && PyString_Check(s)) {
3537 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003538 if (v == NULL)
3539 goto onError;
3540 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003541#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003542 else {
3543 PyObject *temp = PyUnicode_Concat(v, s);
3544 Py_DECREF(s);
3545 Py_DECREF(v);
3546 v = temp;
3547 if (v == NULL)
3548 goto onError;
3549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003551 }
3552 }
3553 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554
3555 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003556 Py_XDECREF(v);
3557 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558}