blob: e14ff3a3da19a055fd57ccc467fdea36fdfa18bb [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 */
21 PyArena *c_arena; /* arena for allocating memeory */
Christian Heimesffcd1e12007-11-24 01:36:02 +000022 const char *c_filename; /* filename */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023};
24
25static asdl_seq *seq_for_testlist(struct compiling *, const node *);
26static expr_ty ast_for_expr(struct compiling *, const node *);
27static stmt_ty ast_for_stmt(struct compiling *, const node *);
28static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000029static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
30 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000031static expr_ty ast_for_testlist(struct compiling *, const node *);
Christian Heimes5224d282008-02-23 15:01:05 +000032static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000033static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034
35/* Note different signature for ast_for_call */
36static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
37
38static PyObject *parsenumber(const char *);
39static PyObject *parsestr(const char *s, const char *encoding);
40static PyObject *parsestrplus(struct compiling *, const node *n);
41
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000043#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000044#endif
45
Neal Norwitzadb69fc2005-12-17 20:54:49 +000046static identifier
47new_identifier(const char* n, PyArena *arena) {
48 PyObject* id = PyString_InternFromString(n);
49 PyArena_AddPyObject(arena, id);
50 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000051}
52
Neal Norwitzadb69fc2005-12-17 20:54:49 +000053#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054
55/* This routine provides an invalid object for the syntax error.
56 The outermost routine must unpack this error and create the
57 proper object. We do this so that we don't have to pass
58 the filename to everything function.
59
60 XXX Maybe we should just pass the filename...
61*/
62
63static int
64ast_error(const node *n, const char *errstr)
65{
66 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
67 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000068 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000069 PyErr_SetObject(PyExc_SyntaxError, u);
70 Py_DECREF(u);
71 return 0;
72}
73
74static void
75ast_error_finish(const char *filename)
76{
77 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000078 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079
80 assert(PyErr_Occurred());
81 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000082 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083
84 PyErr_Fetch(&type, &value, &tback);
85 errstr = PyTuple_GetItem(value, 0);
86 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000087 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088 Py_INCREF(errstr);
89 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000090 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000091 Py_DECREF(errstr);
92 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000094 Py_DECREF(value);
95
96 loc = PyErr_ProgramText(filename, lineno);
97 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000098 Py_INCREF(Py_None);
99 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000101 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000103 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000104 Py_DECREF(errstr);
105 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 }
Georg Brandl7784f122006-05-26 20:04:44 +0000107 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 Py_DECREF(errstr);
109 Py_DECREF(tmp);
110 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000111 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112 PyErr_Restore(type, value, tback);
113}
114
115/* num_stmts() returns number of contained statements.
116
117 Use this routine to determine how big a sequence is needed for
118 the statements in a parse tree. Its raison d'etre is this bit of
119 grammar:
120
121 stmt: simple_stmt | compound_stmt
122 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
123
124 A simple_stmt can contain multiple small_stmt elements joined
125 by semicolons. If the arg is a simple_stmt, the number of
126 small_stmt elements is returned.
127*/
128
129static int
130num_stmts(const node *n)
131{
132 int i, l;
133 node *ch;
134
135 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000136 case single_input:
137 if (TYPE(CHILD(n, 0)) == NEWLINE)
138 return 0;
139 else
140 return num_stmts(CHILD(n, 0));
141 case file_input:
142 l = 0;
143 for (i = 0; i < NCH(n); i++) {
144 ch = CHILD(n, i);
145 if (TYPE(ch) == stmt)
146 l += num_stmts(ch);
147 }
148 return l;
149 case stmt:
150 return num_stmts(CHILD(n, 0));
151 case compound_stmt:
152 return 1;
153 case simple_stmt:
154 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
155 case suite:
156 if (NCH(n) == 1)
157 return num_stmts(CHILD(n, 0));
158 else {
159 l = 0;
160 for (i = 2; i < (NCH(n) - 1); i++)
161 l += num_stmts(CHILD(n, i));
162 return l;
163 }
164 default: {
165 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000167 sprintf(buf, "Non-statement found: %d %d\n",
168 TYPE(n), NCH(n));
169 Py_FatalError(buf);
170 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171 }
172 assert(0);
173 return 0;
174}
175
176/* Transform the CST rooted at node * to the appropriate AST
177*/
178
179mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000180PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000181 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000183 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184 asdl_seq *stmts = NULL;
185 stmt_ty s;
186 node *ch;
187 struct compiling c;
188
189 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000190 c.c_encoding = "utf-8";
191 if (TYPE(n) == encoding_decl) {
192 ast_error(n, "encoding declaration in Unicode string");
193 goto error;
194 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000196 c.c_encoding = STR(n);
197 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000199 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000201 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000202 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Jeremy Hyltona8293132006-02-28 17:58:27 +0000204 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000206 case file_input:
207 stmts = asdl_seq_new(num_stmts(n), arena);
208 if (!stmts)
209 return NULL;
210 for (i = 0; i < NCH(n) - 1; i++) {
211 ch = CHILD(n, i);
212 if (TYPE(ch) == NEWLINE)
213 continue;
214 REQ(ch, stmt);
215 num = num_stmts(ch);
216 if (num == 1) {
217 s = ast_for_stmt(&c, ch);
218 if (!s)
219 goto error;
220 asdl_seq_SET(stmts, k++, s);
221 }
222 else {
223 ch = CHILD(ch, 0);
224 REQ(ch, simple_stmt);
225 for (j = 0; j < num; j++) {
226 s = ast_for_stmt(&c, CHILD(ch, j * 2));
227 if (!s)
228 goto error;
229 asdl_seq_SET(stmts, k++, s);
230 }
231 }
232 }
233 return Module(stmts, arena);
234 case eval_input: {
235 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000237 /* XXX Why not gen_for here? */
238 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
239 if (!testlist_ast)
240 goto error;
241 return Expression(testlist_ast, arena);
242 }
243 case single_input:
244 if (TYPE(CHILD(n, 0)) == NEWLINE) {
245 stmts = asdl_seq_new(1, arena);
246 if (!stmts)
247 goto error;
248 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
249 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000250 if (!asdl_seq_GET(stmts, 0))
251 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000252 return Interactive(stmts, arena);
253 }
254 else {
255 n = CHILD(n, 0);
256 num = num_stmts(n);
257 stmts = asdl_seq_new(num, arena);
258 if (!stmts)
259 goto error;
260 if (num == 1) {
261 s = ast_for_stmt(&c, n);
262 if (!s)
263 goto error;
264 asdl_seq_SET(stmts, 0, s);
265 }
266 else {
267 /* Only a simple_stmt can contain multiple statements. */
268 REQ(n, simple_stmt);
269 for (i = 0; i < NCH(n); i += 2) {
270 if (TYPE(CHILD(n, i)) == NEWLINE)
271 break;
272 s = ast_for_stmt(&c, CHILD(n, i));
273 if (!s)
274 goto error;
275 asdl_seq_SET(stmts, i / 2, s);
276 }
277 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000279 return Interactive(stmts, arena);
280 }
281 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000282 PyErr_Format(PyExc_SystemError,
283 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000284 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 }
286 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287 ast_error_finish(filename);
288 return NULL;
289}
290
291/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
292*/
293
294static operator_ty
295get_operator(const node *n)
296{
297 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000298 case VBAR:
299 return BitOr;
300 case CIRCUMFLEX:
301 return BitXor;
302 case AMPER:
303 return BitAnd;
304 case LEFTSHIFT:
305 return LShift;
306 case RIGHTSHIFT:
307 return RShift;
308 case PLUS:
309 return Add;
310 case MINUS:
311 return Sub;
312 case STAR:
313 return Mult;
314 case SLASH:
315 return Div;
316 case DOUBLESLASH:
317 return FloorDiv;
318 case PERCENT:
319 return Mod;
320 default:
321 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 }
323}
324
Jeremy Hyltona8293132006-02-28 17:58:27 +0000325/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326
327 Only sets context for expr kinds that "can appear in assignment context"
328 (according to ../Parser/Python.asdl). For other expr kinds, it sets
329 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330*/
331
332static int
333set_context(expr_ty e, expr_context_ty ctx, const node *n)
334{
335 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000336 /* If a particular expression type can't be used for assign / delete,
337 set expr_name to its name and an error message will be generated.
338 */
339 const char* expr_name = NULL;
340
341 /* The ast defines augmented store and load contexts, but the
342 implementation here doesn't actually use them. The code may be
343 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000344 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000346 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000347 */
348 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
350 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000351 case Attribute_kind:
352 if (ctx == Store &&
353 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
354 return ast_error(n, "assignment to None");
355 }
356 e->v.Attribute.ctx = ctx;
357 break;
358 case Subscript_kind:
359 e->v.Subscript.ctx = ctx;
360 break;
361 case Name_kind:
362 if (ctx == Store &&
363 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
364 return ast_error(n, "assignment to None");
365 }
366 e->v.Name.ctx = ctx;
367 break;
368 case List_kind:
369 e->v.List.ctx = ctx;
370 s = e->v.List.elts;
371 break;
372 case Tuple_kind:
373 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
374 return ast_error(n, "can't assign to ()");
375 e->v.Tuple.ctx = ctx;
376 s = e->v.Tuple.elts;
377 break;
378 case Lambda_kind:
379 expr_name = "lambda";
380 break;
381 case Call_kind:
382 expr_name = "function call";
383 break;
384 case BoolOp_kind:
385 case BinOp_kind:
386 case UnaryOp_kind:
387 expr_name = "operator";
388 break;
389 case GeneratorExp_kind:
390 expr_name = "generator expression";
391 break;
392 case Yield_kind:
393 expr_name = "yield expression";
394 break;
395 case ListComp_kind:
396 expr_name = "list comprehension";
397 break;
398 case Dict_kind:
399 case Num_kind:
400 case Str_kind:
401 expr_name = "literal";
402 break;
403 case Compare_kind:
404 expr_name = "comparison";
405 break;
406 case Repr_kind:
407 expr_name = "repr";
408 break;
409 case IfExp_kind:
410 expr_name = "conditional expression";
411 break;
412 default:
413 PyErr_Format(PyExc_SystemError,
414 "unexpected expression in assignment %d (line %d)",
415 e->kind, e->lineno);
416 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000418 /* Check for error string set by switch */
419 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000420 char buf[300];
421 PyOS_snprintf(buf, sizeof(buf),
422 "can't %s %s",
423 ctx == Store ? "assign to" : "delete",
424 expr_name);
425 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000426 }
427
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000429 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430 */
431 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000432 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000434 for (i = 0; i < asdl_seq_LEN(s); i++) {
435 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
436 return 0;
437 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438 }
439 return 1;
440}
441
442static operator_ty
443ast_for_augassign(const node *n)
444{
445 REQ(n, augassign);
446 n = CHILD(n, 0);
447 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000448 case '+':
449 return Add;
450 case '-':
451 return Sub;
452 case '/':
453 if (STR(n)[1] == '/')
454 return FloorDiv;
455 else
456 return Div;
457 case '%':
458 return Mod;
459 case '<':
460 return LShift;
461 case '>':
462 return RShift;
463 case '&':
464 return BitAnd;
465 case '^':
466 return BitXor;
467 case '|':
468 return BitOr;
469 case '*':
470 if (STR(n)[1] == '*')
471 return Pow;
472 else
473 return Mult;
474 default:
475 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
476 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 }
478}
479
480static cmpop_ty
481ast_for_comp_op(const node *n)
482{
483 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000484 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485 */
486 REQ(n, comp_op);
487 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000488 n = CHILD(n, 0);
489 switch (TYPE(n)) {
490 case LESS:
491 return Lt;
492 case GREATER:
493 return Gt;
494 case EQEQUAL: /* == */
495 return Eq;
496 case LESSEQUAL:
497 return LtE;
498 case GREATEREQUAL:
499 return GtE;
500 case NOTEQUAL:
501 return NotEq;
502 case NAME:
503 if (strcmp(STR(n), "in") == 0)
504 return In;
505 if (strcmp(STR(n), "is") == 0)
506 return Is;
507 default:
508 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
509 STR(n));
510 return (cmpop_ty)0;
511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 }
513 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000514 /* handle "not in" and "is not" */
515 switch (TYPE(CHILD(n, 0))) {
516 case NAME:
517 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
518 return NotIn;
519 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
520 return IsNot;
521 default:
522 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
523 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
524 return (cmpop_ty)0;
525 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 }
Neal Norwitz79792652005-11-14 04:25:03 +0000527 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000528 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000529 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530}
531
532static asdl_seq *
533seq_for_testlist(struct compiling *c, const node *n)
534{
535 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000536 asdl_seq *seq;
537 expr_ty expression;
538 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000539 assert(TYPE(n) == testlist ||
540 TYPE(n) == listmaker ||
541 TYPE(n) == testlist_gexp ||
542 TYPE(n) == testlist_safe ||
543 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000545 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000547 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
549 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000550 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000552 expression = ast_for_expr(c, CHILD(n, i));
553 if (!expression)
554 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000556 assert(i / 2 < seq->size);
557 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558 }
559 return seq;
560}
561
562static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000563compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564{
565 int i, len = (NCH(n) + 1) / 2;
566 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000567 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000569 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570
Neal Norwitz3a230172006-09-22 08:18:10 +0000571 /* fpdef: NAME | '(' fplist ')'
572 fplist: fpdef (',' fpdef)* [',']
573 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000576 const node *fpdef_node = CHILD(n, 2*i);
577 const node *child;
578 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000579set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000580 /* fpdef_node is either a NAME or an fplist */
581 child = CHILD(fpdef_node, 0);
582 if (TYPE(child) == NAME) {
583 if (!strcmp(STR(child), "None")) {
584 ast_error(child, "assignment to None");
585 return NULL;
586 }
587 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
588 child->n_col_offset, c->c_arena);
589 }
590 else {
591 assert(TYPE(fpdef_node) == fpdef);
592 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
593 child = CHILD(fpdef_node, 1);
594 assert(TYPE(child) == fplist);
595 /* NCH == 1 means we have (x), we need to elide the extra parens */
596 if (NCH(child) == 1) {
597 fpdef_node = CHILD(child, 0);
598 assert(TYPE(fpdef_node) == fpdef);
599 goto set_name;
600 }
601 arg = compiler_complex_args(c, child);
602 }
603 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 }
605
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000606 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000607 if (!set_context(result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000608 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 return result;
610}
611
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612
Jeremy Hyltona8293132006-02-28 17:58:27 +0000613/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614
615static arguments_ty
616ast_for_arguments(struct compiling *c, const node *n)
617{
618 /* parameters: '(' [varargslist] ')'
619 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000620 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000622 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 asdl_seq *args, *defaults;
624 identifier vararg = NULL, kwarg = NULL;
625 node *ch;
626
627 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000628 if (NCH(n) == 2) /* () as argument list */
629 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
630 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 }
632 REQ(n, varargslist);
633
634 /* first count the number of normal args & defaults */
635 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000636 ch = CHILD(n, i);
637 if (TYPE(ch) == fpdef)
638 n_args++;
639 if (TYPE(ch) == EQUAL)
640 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000642 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000644 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000645 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000647 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648
649 /* fpdef: NAME | '(' fplist ')'
650 fplist: fpdef (',' fpdef)* [',']
651 */
652 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000653 j = 0; /* index for defaults */
654 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000656 ch = CHILD(n, i);
657 switch (TYPE(ch)) {
658 case fpdef:
659 handle_fpdef:
660 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
661 anything other than EQUAL or a comma? */
662 /* XXX Should NCH(n) check be made a separate check? */
663 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
664 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
665 if (!expression)
666 goto error;
667 assert(defaults != NULL);
668 asdl_seq_SET(defaults, j++, expression);
669 i += 2;
670 found_default = 1;
671 }
672 else if (found_default) {
673 ast_error(n,
674 "non-default argument follows default argument");
675 goto error;
676 }
677 if (NCH(ch) == 3) {
678 ch = CHILD(ch, 1);
679 /* def foo((x)): is not complex, special case. */
680 if (NCH(ch) != 1) {
681 /* We have complex arguments, setup for unpacking. */
682 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000683 if (!asdl_seq_GET(args, k-1))
684 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000685 } else {
686 /* def foo((x)): setup for checking NAME below. */
687 /* Loop because there can be many parens and tuple
688 unpacking mixed in. */
689 ch = CHILD(ch, 0);
690 assert(TYPE(ch) == fpdef);
691 goto handle_fpdef;
692 }
693 }
694 if (TYPE(CHILD(ch, 0)) == NAME) {
695 expr_ty name;
696 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
697 ast_error(CHILD(ch, 0), "assignment to None");
698 goto error;
699 }
700 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
701 Param, LINENO(ch), ch->n_col_offset,
702 c->c_arena);
703 if (!name)
704 goto error;
705 asdl_seq_SET(args, k++, name);
706
707 }
708 i += 2; /* the name and the comma */
709 break;
710 case STAR:
711 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
712 ast_error(CHILD(n, i+1), "assignment to None");
713 goto error;
714 }
715 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
716 i += 3;
717 break;
718 case DOUBLESTAR:
719 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
720 ast_error(CHILD(n, i+1), "assignment to None");
721 goto error;
722 }
723 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
724 i += 3;
725 break;
726 default:
727 PyErr_Format(PyExc_SystemError,
728 "unexpected node in varargslist: %d @ %d",
729 TYPE(ch), i);
730 goto error;
731 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 }
733
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000734 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735
736 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000737 Py_XDECREF(vararg);
738 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 return NULL;
740}
741
742static expr_ty
743ast_for_dotted_name(struct compiling *c, const node *n)
744{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000745 expr_ty e;
746 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000747 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 int i;
749
750 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000751
752 lineno = LINENO(n);
753 col_offset = n->n_col_offset;
754
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 id = NEW_IDENTIFIER(CHILD(n, 0));
756 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000757 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000758 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000760 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761
762 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000763 id = NEW_IDENTIFIER(CHILD(n, i));
764 if (!id)
765 return NULL;
766 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
767 if (!e)
768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 }
770
771 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772}
773
774static expr_ty
775ast_for_decorator(struct compiling *c, const node *n)
776{
777 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
778 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000779 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780
781 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000782 REQ(CHILD(n, 0), AT);
783 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
785 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
786 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000787 return NULL;
788
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000790 d = name_expr;
791 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 }
793 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000794 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
795 n->n_col_offset, c->c_arena);
796 if (!d)
797 return NULL;
798 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 }
800 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000801 d = ast_for_call(c, CHILD(n, 3), name_expr);
802 if (!d)
803 return NULL;
804 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 }
806
807 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808}
809
810static asdl_seq*
811ast_for_decorators(struct compiling *c, const node *n)
812{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000813 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000814 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 int i;
816
817 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000818 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000820 return NULL;
821
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000823 d = ast_for_decorator(c, CHILD(n, i));
824 if (!d)
825 return NULL;
826 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 }
828 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829}
830
831static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000832ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833{
Christian Heimes5224d282008-02-23 15:01:05 +0000834 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000835 identifier name;
836 arguments_ty args;
837 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000838 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839
840 REQ(n, funcdef);
841
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 name = NEW_IDENTIFIER(CHILD(n, name_i));
843 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000844 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000846 ast_error(CHILD(n, name_i), "assignment to None");
847 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 }
849 args = ast_for_arguments(c, CHILD(n, name_i + 1));
850 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 body = ast_for_suite(c, CHILD(n, name_i + 3));
853 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000854 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000856 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000857 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858}
859
Christian Heimes5224d282008-02-23 15:01:05 +0000860static stmt_ty
861ast_for_decorated(struct compiling *c, const node *n)
862{
863 /* decorated: decorators (classdef | funcdef) */
864 stmt_ty thing = NULL;
865 asdl_seq *decorator_seq = NULL;
866
867 REQ(n, decorated);
868
869 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
870 if (!decorator_seq)
871 return NULL;
872
873 assert(TYPE(CHILD(n, 1)) == funcdef ||
874 TYPE(CHILD(n, 1)) == classdef);
875
876 if (TYPE(CHILD(n, 1)) == funcdef) {
877 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
878 } else if (TYPE(CHILD(n, 1)) == classdef) {
879 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
880 }
881 /* we count the decorators in when talking about the class' or
882 function's line number */
883 if (thing) {
884 thing->lineno = LINENO(n);
885 thing->col_offset = n->n_col_offset;
886 }
887 return thing;
888}
889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890static expr_ty
891ast_for_lambdef(struct compiling *c, const node *n)
892{
893 /* lambdef: 'lambda' [varargslist] ':' test */
894 arguments_ty args;
895 expr_ty expression;
896
897 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000898 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
899 if (!args)
900 return NULL;
901 expression = ast_for_expr(c, CHILD(n, 2));
902 if (!expression)
903 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 }
905 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000906 args = ast_for_arguments(c, CHILD(n, 1));
907 if (!args)
908 return NULL;
909 expression = ast_for_expr(c, CHILD(n, 3));
910 if (!expression)
911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 }
913
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000914 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915}
916
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000917static expr_ty
918ast_for_ifexpr(struct compiling *c, const node *n)
919{
920 /* test: or_test 'if' or_test 'else' test */
921 expr_ty expression, body, orelse;
922
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000923 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000924 body = ast_for_expr(c, CHILD(n, 0));
925 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000926 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000927 expression = ast_for_expr(c, CHILD(n, 2));
928 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000929 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000930 orelse = ast_for_expr(c, CHILD(n, 4));
931 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000932 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000933 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000934 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000935}
936
Neal Norwitze4d4f002006-09-05 03:58:26 +0000937/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
938 so there is only a single version. Possibly for loops can also re-use
939 the code.
940*/
941
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942/* Count the number of 'for' loop in a list comprehension.
943
944 Helper for ast_for_listcomp().
945*/
946
947static int
948count_list_fors(const node *n)
949{
950 int n_fors = 0;
951 node *ch = CHILD(n, 1);
952
953 count_list_for:
954 n_fors++;
955 REQ(ch, list_for);
956 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000957 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000959 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 count_list_iter:
961 REQ(ch, list_iter);
962 ch = CHILD(ch, 0);
963 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000964 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000966 if (NCH(ch) == 3) {
967 ch = CHILD(ch, 2);
968 goto count_list_iter;
969 }
970 else
971 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000973
974 /* Should never be reached */
975 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
976 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977}
978
979/* Count the number of 'if' statements in a list comprehension.
980
981 Helper for ast_for_listcomp().
982*/
983
984static int
985count_list_ifs(const node *n)
986{
987 int n_ifs = 0;
988
989 count_list_iter:
990 REQ(n, list_iter);
991 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000992 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 n = CHILD(n, 0);
994 REQ(n, list_if);
995 n_ifs++;
996 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000997 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 n = CHILD(n, 2);
999 goto count_list_iter;
1000}
1001
1002static expr_ty
1003ast_for_listcomp(struct compiling *c, const node *n)
1004{
1005 /* listmaker: test ( list_for | (',' test)* [','] )
1006 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1007 list_iter: list_for | list_if
1008 list_if: 'if' test [list_iter]
1009 testlist_safe: test [(',' test)+ [',']]
1010 */
1011 expr_ty elt;
1012 asdl_seq *listcomps;
1013 int i, n_fors;
1014 node *ch;
1015
1016 REQ(n, listmaker);
1017 assert(NCH(n) > 1);
1018
1019 elt = ast_for_expr(c, CHILD(n, 0));
1020 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001021 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022
1023 n_fors = count_list_fors(n);
1024 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001025 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001027 listcomps = asdl_seq_new(n_fors, c->c_arena);
1028 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001029 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001030
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 ch = CHILD(n, 1);
1032 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001033 comprehension_ty lc;
1034 asdl_seq *t;
1035 expr_ty expression;
1036 node *for_ch;
1037
1038 REQ(ch, list_for);
1039
1040 for_ch = CHILD(ch, 1);
1041 t = ast_for_exprlist(c, for_ch, Store);
1042 if (!t)
1043 return NULL;
1044 expression = ast_for_testlist(c, CHILD(ch, 3));
1045 if (!expression)
1046 return NULL;
1047
1048 /* Check the # of children rather than the length of t, since
1049 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1050 */
1051 if (NCH(for_ch) == 1)
1052 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1053 c->c_arena);
1054 else
1055 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1056 c->c_arena),
1057 expression, NULL, c->c_arena);
1058 if (!lc)
1059 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001061 if (NCH(ch) == 5) {
1062 int j, n_ifs;
1063 asdl_seq *ifs;
1064 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001066 ch = CHILD(ch, 4);
1067 n_ifs = count_list_ifs(ch);
1068 if (n_ifs == -1)
1069 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001071 ifs = asdl_seq_new(n_ifs, c->c_arena);
1072 if (!ifs)
1073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001075 for (j = 0; j < n_ifs; j++) {
1076 REQ(ch, list_iter);
1077 ch = CHILD(ch, 0);
1078 REQ(ch, list_if);
1079
1080 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1081 if (!list_for_expr)
1082 return NULL;
1083
1084 asdl_seq_SET(ifs, j, list_for_expr);
1085 if (NCH(ch) == 3)
1086 ch = CHILD(ch, 2);
1087 }
1088 /* on exit, must guarantee that ch is a list_for */
1089 if (TYPE(ch) == list_iter)
1090 ch = CHILD(ch, 0);
1091 lc->ifs = ifs;
1092 }
1093 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 }
1095
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001096 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097}
1098
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001099/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100
1101 Helper for ast_for_genexp().
1102*/
1103
1104static int
1105count_gen_fors(const node *n)
1106{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001107 int n_fors = 0;
1108 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109
1110 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001111 n_fors++;
1112 REQ(ch, gen_for);
1113 if (NCH(ch) == 5)
1114 ch = CHILD(ch, 4);
1115 else
1116 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001118 REQ(ch, gen_iter);
1119 ch = CHILD(ch, 0);
1120 if (TYPE(ch) == gen_for)
1121 goto count_gen_for;
1122 else if (TYPE(ch) == gen_if) {
1123 if (NCH(ch) == 3) {
1124 ch = CHILD(ch, 2);
1125 goto count_gen_iter;
1126 }
1127 else
1128 return n_fors;
1129 }
1130
1131 /* Should never be reached */
1132 PyErr_SetString(PyExc_SystemError,
1133 "logic error in count_gen_fors");
1134 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135}
1136
1137/* Count the number of 'if' statements in a generator expression.
1138
1139 Helper for ast_for_genexp().
1140*/
1141
1142static int
1143count_gen_ifs(const node *n)
1144{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001145 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001147 while (1) {
1148 REQ(n, gen_iter);
1149 if (TYPE(CHILD(n, 0)) == gen_for)
1150 return n_ifs;
1151 n = CHILD(n, 0);
1152 REQ(n, gen_if);
1153 n_ifs++;
1154 if (NCH(n) == 2)
1155 return n_ifs;
1156 n = CHILD(n, 2);
1157 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158}
1159
Jeremy Hyltona8293132006-02-28 17:58:27 +00001160/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161static expr_ty
1162ast_for_genexp(struct compiling *c, const node *n)
1163{
1164 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001165 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 expr_ty elt;
1167 asdl_seq *genexps;
1168 int i, n_fors;
1169 node *ch;
1170
1171 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1172 assert(NCH(n) > 1);
1173
1174 elt = ast_for_expr(c, CHILD(n, 0));
1175 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001176 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177
1178 n_fors = count_gen_fors(n);
1179 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001180 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001181
1182 genexps = asdl_seq_new(n_fors, c->c_arena);
1183 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001184 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 ch = CHILD(n, 1);
1187 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001188 comprehension_ty ge;
1189 asdl_seq *t;
1190 expr_ty expression;
1191 node *for_ch;
1192
1193 REQ(ch, gen_for);
1194
1195 for_ch = CHILD(ch, 1);
1196 t = ast_for_exprlist(c, for_ch, Store);
1197 if (!t)
1198 return NULL;
1199 expression = ast_for_expr(c, CHILD(ch, 3));
1200 if (!expression)
1201 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001202
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001203 /* Check the # of children rather than the length of t, since
1204 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1205 if (NCH(for_ch) == 1)
1206 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1207 NULL, c->c_arena);
1208 else
1209 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1210 c->c_arena),
1211 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001212
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001213 if (!ge)
1214 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001215
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001216 if (NCH(ch) == 5) {
1217 int j, n_ifs;
1218 asdl_seq *ifs;
1219
1220 ch = CHILD(ch, 4);
1221 n_ifs = count_gen_ifs(ch);
1222 if (n_ifs == -1)
1223 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001224
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001225 ifs = asdl_seq_new(n_ifs, c->c_arena);
1226 if (!ifs)
1227 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001228
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001229 for (j = 0; j < n_ifs; j++) {
1230 REQ(ch, gen_iter);
1231 ch = CHILD(ch, 0);
1232 REQ(ch, gen_if);
1233
1234 expression = ast_for_expr(c, CHILD(ch, 1));
1235 if (!expression)
1236 return NULL;
1237 asdl_seq_SET(ifs, j, expression);
1238 if (NCH(ch) == 3)
1239 ch = CHILD(ch, 2);
1240 }
1241 /* on exit, must guarantee that ch is a gen_for */
1242 if (TYPE(ch) == gen_iter)
1243 ch = CHILD(ch, 0);
1244 ge->ifs = ifs;
1245 }
1246 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 }
1248
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001249 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250}
1251
1252static expr_ty
1253ast_for_atom(struct compiling *c, const node *n)
1254{
1255 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1256 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1257 */
1258 node *ch = CHILD(n, 0);
1259
1260 switch (TYPE(ch)) {
1261 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001262 /* All names start in Load context, but may later be
1263 changed. */
1264 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1265 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001267 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001268 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001269#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001270 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1271 PyObject *type, *value, *tback, *errstr;
1272 PyErr_Fetch(&type, &value, &tback);
1273 errstr = ((PyUnicodeErrorObject *)value)->reason;
1274 if (errstr) {
1275 char *s = "";
1276 char buf[128];
1277 s = PyString_AsString(errstr);
1278 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1279 ast_error(n, buf);
1280 } else {
1281 ast_error(n, "(unicode error) unknown error");
1282 }
1283 Py_DECREF(type);
1284 Py_DECREF(value);
1285 Py_XDECREF(tback);
1286 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001287#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001288 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001289 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001290 PyArena_AddPyObject(c->c_arena, str);
1291 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 }
1293 case NUMBER: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001294 PyObject *pynum = parsenumber(STR(ch));
1295 if (!pynum)
1296 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001297
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001298 PyArena_AddPyObject(c->c_arena, pynum);
1299 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 }
1301 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001302 ch = CHILD(n, 1);
1303
1304 if (TYPE(ch) == RPAR)
1305 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1306
1307 if (TYPE(ch) == yield_expr)
1308 return ast_for_expr(c, ch);
1309
1310 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1311 return ast_for_genexp(c, ch);
1312
1313 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001315 ch = CHILD(n, 1);
1316
1317 if (TYPE(ch) == RSQB)
1318 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1319
1320 REQ(ch, listmaker);
1321 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1322 asdl_seq *elts = seq_for_testlist(c, ch);
1323 if (!elts)
1324 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001325
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001326 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1327 }
1328 else
1329 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001331 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1332 int i, size;
1333 asdl_seq *keys, *values;
1334
1335 ch = CHILD(n, 1);
1336 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1337 keys = asdl_seq_new(size, c->c_arena);
1338 if (!keys)
1339 return NULL;
1340
1341 values = asdl_seq_new(size, c->c_arena);
1342 if (!values)
1343 return NULL;
1344
1345 for (i = 0; i < NCH(ch); i += 4) {
1346 expr_ty expression;
1347
1348 expression = ast_for_expr(c, CHILD(ch, i));
1349 if (!expression)
1350 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001351
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001352 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001353
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001354 expression = ast_for_expr(c, CHILD(ch, i + 2));
1355 if (!expression)
1356 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001357
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001358 asdl_seq_SET(values, i / 4, expression);
1359 }
1360 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 }
1362 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001363 expr_ty expression;
Christian Heimes02c9ab52007-11-23 12:12:02 +00001364 if (Py_Py3kWarningFlag) {
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001365 if (PyErr_WarnExplicit(PyExc_DeprecationWarning,
1366 "backquote not supported in 3.x",
Christian Heimesffcd1e12007-11-24 01:36:02 +00001367 c->c_filename, LINENO(n),
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001368 NULL, NULL)) {
1369 return NULL;
1370 }
1371 }
1372 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001373 if (!expression)
1374 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001375
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001376 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 }
1378 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001379 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1380 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 }
1382}
1383
1384static slice_ty
1385ast_for_slice(struct compiling *c, const node *n)
1386{
1387 node *ch;
1388 expr_ty lower = NULL, upper = NULL, step = NULL;
1389
1390 REQ(n, subscript);
1391
1392 /*
1393 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1394 sliceop: ':' [test]
1395 */
1396 ch = CHILD(n, 0);
1397 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001398 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399
1400 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001401 /* 'step' variable hold no significance in terms of being used over
1402 other vars */
1403 step = ast_for_expr(c, ch);
1404 if (!step)
1405 return NULL;
1406
1407 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 }
1409
1410 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001411 lower = ast_for_expr(c, ch);
1412 if (!lower)
1413 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 }
1415
1416 /* If there's an upper bound it's in the second or third position. */
1417 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001418 if (NCH(n) > 1) {
1419 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001421 if (TYPE(n2) == test) {
1422 upper = ast_for_expr(c, n2);
1423 if (!upper)
1424 return NULL;
1425 }
1426 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001428 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001430 if (TYPE(n2) == test) {
1431 upper = ast_for_expr(c, n2);
1432 if (!upper)
1433 return NULL;
1434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 }
1436
1437 ch = CHILD(n, NCH(n) - 1);
1438 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001439 if (NCH(ch) == 1) {
1440 /* No expression, so step is None */
1441 ch = CHILD(ch, 0);
1442 step = Name(new_identifier("None", c->c_arena), Load,
1443 LINENO(ch), ch->n_col_offset, c->c_arena);
1444 if (!step)
1445 return NULL;
1446 } else {
1447 ch = CHILD(ch, 1);
1448 if (TYPE(ch) == test) {
1449 step = ast_for_expr(c, ch);
1450 if (!step)
1451 return NULL;
1452 }
1453 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 }
1455
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001456 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457}
1458
1459static expr_ty
1460ast_for_binop(struct compiling *c, const node *n)
1461{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001462 /* Must account for a sequence of expressions.
1463 How should A op B op C by represented?
1464 BinOp(BinOp(A, op, B), op, C).
1465 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001467 int i, nops;
1468 expr_ty expr1, expr2, result;
1469 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001471 expr1 = ast_for_expr(c, CHILD(n, 0));
1472 if (!expr1)
1473 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001475 expr2 = ast_for_expr(c, CHILD(n, 2));
1476 if (!expr2)
1477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001479 newoperator = get_operator(CHILD(n, 1));
1480 if (!newoperator)
1481 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001483 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1484 c->c_arena);
1485 if (!result)
1486 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001488 nops = (NCH(n) - 1) / 2;
1489 for (i = 1; i < nops; i++) {
1490 expr_ty tmp_result, tmp;
1491 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001493 newoperator = get_operator(next_oper);
1494 if (!newoperator)
1495 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001497 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1498 if (!tmp)
1499 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001501 tmp_result = BinOp(result, newoperator, tmp,
1502 LINENO(next_oper), next_oper->n_col_offset,
1503 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001504 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001505 return NULL;
1506 result = tmp_result;
1507 }
1508 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509}
1510
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001511static expr_ty
1512ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1513{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001514 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1515 subscriptlist: subscript (',' subscript)* [',']
1516 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1517 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001518 REQ(n, trailer);
1519 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001520 if (NCH(n) == 2)
1521 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1522 n->n_col_offset, c->c_arena);
1523 else
1524 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001525 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001526 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001527 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1528 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001529 }
1530 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001531 REQ(CHILD(n, 0), LSQB);
1532 REQ(CHILD(n, 2), RSQB);
1533 n = CHILD(n, 1);
1534 if (NCH(n) == 1) {
1535 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1536 if (!slc)
1537 return NULL;
1538 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1539 c->c_arena);
1540 }
1541 else {
1542 /* The grammar is ambiguous here. The ambiguity is resolved
1543 by treating the sequence as a tuple literal if there are
1544 no slice features.
1545 */
1546 int j;
1547 slice_ty slc;
1548 expr_ty e;
1549 bool simple = true;
1550 asdl_seq *slices, *elts;
1551 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1552 if (!slices)
1553 return NULL;
1554 for (j = 0; j < NCH(n); j += 2) {
1555 slc = ast_for_slice(c, CHILD(n, j));
1556 if (!slc)
1557 return NULL;
1558 if (slc->kind != Index_kind)
1559 simple = false;
1560 asdl_seq_SET(slices, j / 2, slc);
1561 }
1562 if (!simple) {
1563 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1564 Load, LINENO(n), n->n_col_offset, c->c_arena);
1565 }
1566 /* extract Index values and put them in a Tuple */
1567 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1568 if (!elts)
1569 return NULL;
1570 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1571 slc = (slice_ty)asdl_seq_GET(slices, j);
1572 assert(slc->kind == Index_kind && slc->v.Index.value);
1573 asdl_seq_SET(elts, j, slc->v.Index.value);
1574 }
1575 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1576 if (!e)
1577 return NULL;
1578 return Subscript(left_expr, Index(e, c->c_arena),
1579 Load, LINENO(n), n->n_col_offset, c->c_arena);
1580 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001581 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001582}
1583
1584static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001585ast_for_factor(struct compiling *c, const node *n)
1586{
1587 node *pfactor, *ppower, *patom, *pnum;
1588 expr_ty expression;
1589
1590 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001591 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001592 constant. The peephole optimizer already does something like
1593 this but it doesn't handle the case where the constant is
1594 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1595 PyLongObject.
1596 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001597 if (TYPE(CHILD(n, 0)) == MINUS &&
1598 NCH(n) == 2 &&
1599 TYPE((pfactor = CHILD(n, 1))) == factor &&
1600 NCH(pfactor) == 1 &&
1601 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1602 NCH(ppower) == 1 &&
1603 TYPE((patom = CHILD(ppower, 0))) == atom &&
1604 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1605 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1606 if (s == NULL)
1607 return NULL;
1608 s[0] = '-';
1609 strcpy(s + 1, STR(pnum));
1610 PyObject_FREE(STR(pnum));
1611 STR(pnum) = s;
1612 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001613 }
1614
1615 expression = ast_for_expr(c, CHILD(n, 1));
1616 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001617 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001618
1619 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001620 case PLUS:
1621 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1622 c->c_arena);
1623 case MINUS:
1624 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1625 c->c_arena);
1626 case TILDE:
1627 return UnaryOp(Invert, expression, LINENO(n),
1628 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001629 }
1630 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001631 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001632 return NULL;
1633}
1634
1635static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001636ast_for_power(struct compiling *c, const node *n)
1637{
1638 /* power: atom trailer* ('**' factor)*
1639 */
1640 int i;
1641 expr_ty e, tmp;
1642 REQ(n, power);
1643 e = ast_for_atom(c, CHILD(n, 0));
1644 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001645 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001646 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001647 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001648 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001649 node *ch = CHILD(n, i);
1650 if (TYPE(ch) != trailer)
1651 break;
1652 tmp = ast_for_trailer(c, ch, e);
1653 if (!tmp)
1654 return NULL;
1655 tmp->lineno = e->lineno;
1656 tmp->col_offset = e->col_offset;
1657 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001658 }
1659 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001660 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1661 if (!f)
1662 return NULL;
1663 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1664 if (!tmp)
1665 return NULL;
1666 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001667 }
1668 return e;
1669}
1670
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671/* Do not name a variable 'expr'! Will cause a compile error.
1672*/
1673
1674static expr_ty
1675ast_for_expr(struct compiling *c, const node *n)
1676{
1677 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001678 test: or_test ['if' or_test 'else' test] | lambdef
1679 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 and_test: not_test ('and' not_test)*
1681 not_test: 'not' not_test | comparison
1682 comparison: expr (comp_op expr)*
1683 expr: xor_expr ('|' xor_expr)*
1684 xor_expr: and_expr ('^' and_expr)*
1685 and_expr: shift_expr ('&' shift_expr)*
1686 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1687 arith_expr: term (('+'|'-') term)*
1688 term: factor (('*'|'/'|'%'|'//') factor)*
1689 factor: ('+'|'-'|'~') factor | power
1690 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001691
1692 As well as modified versions that exist for backward compatibility,
1693 to explicitly allow:
1694 [ x for x in lambda: 0, lambda: 1 ]
1695 (which would be ambiguous without these extra rules)
1696
1697 old_test: or_test | old_lambdef
1698 old_lambdef: 'lambda' [vararglist] ':' old_test
1699
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 */
1701
1702 asdl_seq *seq;
1703 int i;
1704
1705 loop:
1706 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001707 case test:
1708 case old_test:
1709 if (TYPE(CHILD(n, 0)) == lambdef ||
1710 TYPE(CHILD(n, 0)) == old_lambdef)
1711 return ast_for_lambdef(c, CHILD(n, 0));
1712 else if (NCH(n) > 1)
1713 return ast_for_ifexpr(c, n);
1714 /* Fallthrough */
1715 case or_test:
1716 case and_test:
1717 if (NCH(n) == 1) {
1718 n = CHILD(n, 0);
1719 goto loop;
1720 }
1721 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1722 if (!seq)
1723 return NULL;
1724 for (i = 0; i < NCH(n); i += 2) {
1725 expr_ty e = ast_for_expr(c, CHILD(n, i));
1726 if (!e)
1727 return NULL;
1728 asdl_seq_SET(seq, i / 2, e);
1729 }
1730 if (!strcmp(STR(CHILD(n, 1)), "and"))
1731 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1732 c->c_arena);
1733 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1734 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1735 case not_test:
1736 if (NCH(n) == 1) {
1737 n = CHILD(n, 0);
1738 goto loop;
1739 }
1740 else {
1741 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1742 if (!expression)
1743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001745 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1746 c->c_arena);
1747 }
1748 case comparison:
1749 if (NCH(n) == 1) {
1750 n = CHILD(n, 0);
1751 goto loop;
1752 }
1753 else {
1754 expr_ty expression;
1755 asdl_int_seq *ops;
1756 asdl_seq *cmps;
1757 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1758 if (!ops)
1759 return NULL;
1760 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1761 if (!cmps) {
1762 return NULL;
1763 }
1764 for (i = 1; i < NCH(n); i += 2) {
1765 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001767 newoperator = ast_for_comp_op(CHILD(n, i));
1768 if (!newoperator) {
1769 return NULL;
1770 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001772 expression = ast_for_expr(c, CHILD(n, i + 1));
1773 if (!expression) {
1774 return NULL;
1775 }
1776
1777 asdl_seq_SET(ops, i / 2, newoperator);
1778 asdl_seq_SET(cmps, i / 2, expression);
1779 }
1780 expression = ast_for_expr(c, CHILD(n, 0));
1781 if (!expression) {
1782 return NULL;
1783 }
1784
1785 return Compare(expression, ops, cmps, LINENO(n),
1786 n->n_col_offset, c->c_arena);
1787 }
1788 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001790 /* The next five cases all handle BinOps. The main body of code
1791 is the same in each case, but the switch turned inside out to
1792 reuse the code for each type of operator.
1793 */
1794 case expr:
1795 case xor_expr:
1796 case and_expr:
1797 case shift_expr:
1798 case arith_expr:
1799 case term:
1800 if (NCH(n) == 1) {
1801 n = CHILD(n, 0);
1802 goto loop;
1803 }
1804 return ast_for_binop(c, n);
1805 case yield_expr: {
1806 expr_ty exp = NULL;
1807 if (NCH(n) == 2) {
1808 exp = ast_for_testlist(c, CHILD(n, 1));
1809 if (!exp)
1810 return NULL;
1811 }
1812 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1813 }
1814 case factor:
1815 if (NCH(n) == 1) {
1816 n = CHILD(n, 0);
1817 goto loop;
1818 }
1819 return ast_for_factor(c, n);
1820 case power:
1821 return ast_for_power(c, n);
1822 default:
1823 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001826 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 return NULL;
1828}
1829
1830static expr_ty
1831ast_for_call(struct compiling *c, const node *n, expr_ty func)
1832{
1833 /*
1834 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001835 | '**' test)
1836 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 */
1838
1839 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001840 asdl_seq *args;
1841 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 expr_ty vararg = NULL, kwarg = NULL;
1843
1844 REQ(n, arglist);
1845
1846 nargs = 0;
1847 nkeywords = 0;
1848 ngens = 0;
1849 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001850 node *ch = CHILD(n, i);
1851 if (TYPE(ch) == argument) {
1852 if (NCH(ch) == 1)
1853 nargs++;
1854 else if (TYPE(CHILD(ch, 1)) == gen_for)
1855 ngens++;
1856 else
1857 nkeywords++;
1858 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 }
1860 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001861 ast_error(n, "Generator expression must be parenthesized "
1862 "if not sole argument");
1863 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 }
1865
1866 if (nargs + nkeywords + ngens > 255) {
1867 ast_error(n, "more than 255 arguments");
1868 return NULL;
1869 }
1870
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001873 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001874 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001876 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 nargs = 0;
1878 nkeywords = 0;
1879 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001880 node *ch = CHILD(n, i);
1881 if (TYPE(ch) == argument) {
1882 expr_ty e;
1883 if (NCH(ch) == 1) {
1884 if (nkeywords) {
1885 ast_error(CHILD(ch, 0),
1886 "non-keyword arg after keyword arg");
1887 return NULL;
1888 }
1889 e = ast_for_expr(c, CHILD(ch, 0));
1890 if (!e)
1891 return NULL;
1892 asdl_seq_SET(args, nargs++, e);
1893 }
1894 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1895 e = ast_for_genexp(c, ch);
1896 if (!e)
1897 return NULL;
1898 asdl_seq_SET(args, nargs++, e);
1899 }
1900 else {
1901 keyword_ty kw;
1902 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001904 /* CHILD(ch, 0) is test, but must be an identifier? */
1905 e = ast_for_expr(c, CHILD(ch, 0));
1906 if (!e)
1907 return NULL;
1908 /* f(lambda x: x[0] = 3) ends up getting parsed with
1909 * LHS test = lambda x: x[0], and RHS test = 3.
1910 * SF bug 132313 points out that complaining about a keyword
1911 * then is very confusing.
1912 */
1913 if (e->kind == Lambda_kind) {
1914 ast_error(CHILD(ch, 0),
1915 "lambda cannot contain assignment");
1916 return NULL;
1917 } else if (e->kind != Name_kind) {
1918 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1919 return NULL;
1920 }
1921 key = e->v.Name.id;
Georg Brandle06cf452007-06-07 13:23:24 +00001922 if (!strcmp(PyString_AS_STRING(key), "None")) {
1923 ast_error(CHILD(ch, 0), "assignment to None");
1924 return NULL;
1925 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001926 e = ast_for_expr(c, CHILD(ch, 2));
1927 if (!e)
1928 return NULL;
1929 kw = keyword(key, e, c->c_arena);
1930 if (!kw)
1931 return NULL;
1932 asdl_seq_SET(keywords, nkeywords++, kw);
1933 }
1934 }
1935 else if (TYPE(ch) == STAR) {
1936 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001937 if (!vararg)
1938 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001939 i++;
1940 }
1941 else if (TYPE(ch) == DOUBLESTAR) {
1942 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001943 if (!kwarg)
1944 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001945 i++;
1946 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947 }
1948
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001949 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1950 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951}
1952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001954ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001956 /* testlist_gexp: test (',' test)* [','] */
1957 /* testlist: test (',' test)* [','] */
1958 /* testlist_safe: test (',' test)+ [','] */
1959 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001961 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001962 if (NCH(n) > 1)
1963 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001964 }
1965 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001966 assert(TYPE(n) == testlist ||
1967 TYPE(n) == testlist_safe ||
1968 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001969 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001971 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001973 asdl_seq *tmp = seq_for_testlist(c, n);
1974 if (!tmp)
1975 return NULL;
1976 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001978}
1979
1980static expr_ty
1981ast_for_testlist_gexp(struct compiling *c, const node* n)
1982{
1983 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1984 /* argument: test [ gen_for ] */
1985 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001986 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001987 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001988 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001989}
1990
1991/* like ast_for_testlist() but returns a sequence */
1992static asdl_seq*
1993ast_for_class_bases(struct compiling *c, const node* n)
1994{
1995 /* testlist: test (',' test)* [','] */
1996 assert(NCH(n) > 0);
1997 REQ(n, testlist);
1998 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001999 expr_ty base;
2000 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2001 if (!bases)
2002 return NULL;
2003 base = ast_for_expr(c, CHILD(n, 0));
2004 if (!base)
2005 return NULL;
2006 asdl_seq_SET(bases, 0, base);
2007 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002008 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002009
2010 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011}
2012
2013static stmt_ty
2014ast_for_expr_stmt(struct compiling *c, const node *n)
2015{
2016 REQ(n, expr_stmt);
2017 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002018 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 testlist: test (',' test)* [',']
2020 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002021 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 test: ... here starts the operator precendence dance
2023 */
2024
2025 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002026 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2027 if (!e)
2028 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002030 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 }
2032 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002033 expr_ty expr1, expr2;
2034 operator_ty newoperator;
2035 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002037 expr1 = ast_for_testlist(c, ch);
2038 if (!expr1)
2039 return NULL;
2040 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2041 switch (expr1->kind) {
2042 case GeneratorExp_kind:
2043 ast_error(ch, "augmented assignment to generator "
2044 "expression not possible");
2045 return NULL;
2046 case Yield_kind:
2047 ast_error(ch, "augmented assignment to yield "
2048 "expression not possible");
2049 return NULL;
2050 case Name_kind: {
2051 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2052 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2053 ast_error(ch, "assignment to None");
2054 return NULL;
2055 }
2056 break;
2057 }
2058 case Attribute_kind:
2059 case Subscript_kind:
2060 break;
2061 default:
2062 ast_error(ch, "illegal expression for augmented "
2063 "assignment");
2064 return NULL;
2065 }
2066 if(!set_context(expr1, Store, ch))
2067 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002069 ch = CHILD(n, 2);
2070 if (TYPE(ch) == testlist)
2071 expr2 = ast_for_testlist(c, ch);
2072 else
2073 expr2 = ast_for_expr(c, ch);
2074 if (!expr2)
2075 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002077 newoperator = ast_for_augassign(CHILD(n, 1));
2078 if (!newoperator)
2079 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002081 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2082 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 }
2084 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002085 int i;
2086 asdl_seq *targets;
2087 node *value;
2088 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002090 /* a normal assignment */
2091 REQ(CHILD(n, 1), EQUAL);
2092 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2093 if (!targets)
2094 return NULL;
2095 for (i = 0; i < NCH(n) - 2; i += 2) {
2096 expr_ty e;
2097 node *ch = CHILD(n, i);
2098 if (TYPE(ch) == yield_expr) {
2099 ast_error(ch, "assignment to yield expression not possible");
2100 return NULL;
2101 }
2102 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002104 /* set context to assign */
2105 if (!e)
2106 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002108 if (!set_context(e, Store, CHILD(n, i)))
2109 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002111 asdl_seq_SET(targets, i / 2, e);
2112 }
2113 value = CHILD(n, NCH(n) - 1);
2114 if (TYPE(value) == testlist)
2115 expression = ast_for_testlist(c, value);
2116 else
2117 expression = ast_for_expr(c, value);
2118 if (!expression)
2119 return NULL;
2120 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2121 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123}
2124
2125static stmt_ty
2126ast_for_print_stmt(struct compiling *c, const node *n)
2127{
2128 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002129 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 */
2131 expr_ty dest = NULL, expression;
2132 asdl_seq *seq;
2133 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002134 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135
2136 REQ(n, print_stmt);
2137 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002138 dest = ast_for_expr(c, CHILD(n, 2));
2139 if (!dest)
2140 return NULL;
2141 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002143 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002145 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002146 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002147 expression = ast_for_expr(c, CHILD(n, i));
2148 if (!expression)
2149 return NULL;
2150 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 }
2152 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002153 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154}
2155
2156static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002157ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158{
2159 asdl_seq *seq;
2160 int i;
2161 expr_ty e;
2162
2163 REQ(n, exprlist);
2164
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002165 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002169 e = ast_for_expr(c, CHILD(n, i));
2170 if (!e)
2171 return NULL;
2172 asdl_seq_SET(seq, i / 2, e);
2173 if (context && !set_context(e, context, CHILD(n, i)))
2174 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 }
2176 return seq;
2177}
2178
2179static stmt_ty
2180ast_for_del_stmt(struct compiling *c, const node *n)
2181{
2182 asdl_seq *expr_list;
2183
2184 /* del_stmt: 'del' exprlist */
2185 REQ(n, del_stmt);
2186
2187 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2188 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002189 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002190 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191}
2192
2193static stmt_ty
2194ast_for_flow_stmt(struct compiling *c, const node *n)
2195{
2196 /*
2197 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002198 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 break_stmt: 'break'
2200 continue_stmt: 'continue'
2201 return_stmt: 'return' [testlist]
2202 yield_stmt: yield_expr
2203 yield_expr: 'yield' testlist
2204 raise_stmt: 'raise' [test [',' test [',' test]]]
2205 */
2206 node *ch;
2207
2208 REQ(n, flow_stmt);
2209 ch = CHILD(n, 0);
2210 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002211 case break_stmt:
2212 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2213 case continue_stmt:
2214 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2215 case yield_stmt: { /* will reduce to yield_expr */
2216 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2217 if (!exp)
2218 return NULL;
2219 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2220 }
2221 case return_stmt:
2222 if (NCH(ch) == 1)
2223 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2224 else {
2225 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2226 if (!expression)
2227 return NULL;
2228 return Return(expression, LINENO(n), n->n_col_offset,
2229 c->c_arena);
2230 }
2231 case raise_stmt:
2232 if (NCH(ch) == 1)
2233 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2234 c->c_arena);
2235 else if (NCH(ch) == 2) {
2236 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2237 if (!expression)
2238 return NULL;
2239 return Raise(expression, NULL, NULL, LINENO(n),
2240 n->n_col_offset, c->c_arena);
2241 }
2242 else if (NCH(ch) == 4) {
2243 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002245 expr1 = ast_for_expr(c, CHILD(ch, 1));
2246 if (!expr1)
2247 return NULL;
2248 expr2 = ast_for_expr(c, CHILD(ch, 3));
2249 if (!expr2)
2250 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002252 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2253 c->c_arena);
2254 }
2255 else if (NCH(ch) == 6) {
2256 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002258 expr1 = ast_for_expr(c, CHILD(ch, 1));
2259 if (!expr1)
2260 return NULL;
2261 expr2 = ast_for_expr(c, CHILD(ch, 3));
2262 if (!expr2)
2263 return NULL;
2264 expr3 = ast_for_expr(c, CHILD(ch, 5));
2265 if (!expr3)
2266 return NULL;
2267
2268 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2269 c->c_arena);
2270 }
2271 default:
2272 PyErr_Format(PyExc_SystemError,
2273 "unexpected flow_stmt: %d", TYPE(ch));
2274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002276
2277 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2278 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279}
2280
2281static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002282alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283{
2284 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002285 import_as_name: NAME ['as' NAME]
2286 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 dotted_name: NAME ('.' NAME)*
2288 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002289 PyObject *str;
2290
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 loop:
2292 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002293 case import_as_name:
2294 str = NULL;
2295 if (NCH(n) == 3) {
2296 str = NEW_IDENTIFIER(CHILD(n, 2));
2297 }
2298 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2299 case dotted_as_name:
2300 if (NCH(n) == 1) {
2301 n = CHILD(n, 0);
2302 goto loop;
2303 }
2304 else {
2305 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2306 if (!a)
2307 return NULL;
2308 assert(!a->asname);
2309 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2310 return a;
2311 }
2312 break;
2313 case dotted_name:
2314 if (NCH(n) == 1)
2315 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2316 else {
2317 /* Create a string of the form "a.b.c" */
2318 int i;
2319 size_t len;
2320 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002322 len = 0;
2323 for (i = 0; i < NCH(n); i += 2)
2324 /* length of string plus one for the dot */
2325 len += strlen(STR(CHILD(n, i))) + 1;
2326 len--; /* the last name doesn't have a dot */
2327 str = PyString_FromStringAndSize(NULL, len);
2328 if (!str)
2329 return NULL;
2330 s = PyString_AS_STRING(str);
2331 if (!s)
2332 return NULL;
2333 for (i = 0; i < NCH(n); i += 2) {
2334 char *sch = STR(CHILD(n, i));
2335 strcpy(s, STR(CHILD(n, i)));
2336 s += strlen(sch);
2337 *s++ = '.';
2338 }
2339 --s;
2340 *s = '\0';
2341 PyString_InternInPlace(&str);
2342 PyArena_AddPyObject(c->c_arena, str);
2343 return alias(str, NULL, c->c_arena);
2344 }
2345 break;
2346 case STAR:
2347 str = PyString_InternFromString("*");
2348 PyArena_AddPyObject(c->c_arena, str);
2349 return alias(str, NULL, c->c_arena);
2350 default:
2351 PyErr_Format(PyExc_SystemError,
2352 "unexpected import name: %d", TYPE(n));
2353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002355
2356 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 return NULL;
2358}
2359
2360static stmt_ty
2361ast_for_import_stmt(struct compiling *c, const node *n)
2362{
2363 /*
2364 import_stmt: import_name | import_from
2365 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002366 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002367 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002369 int lineno;
2370 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 int i;
2372 asdl_seq *aliases;
2373
2374 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002375 lineno = LINENO(n);
2376 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002378 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002379 n = CHILD(n, 1);
2380 REQ(n, dotted_as_names);
2381 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2382 if (!aliases)
2383 return NULL;
2384 for (i = 0; i < NCH(n); i += 2) {
2385 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2386 if (!import_alias)
2387 return NULL;
2388 asdl_seq_SET(aliases, i / 2, import_alias);
2389 }
2390 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002392 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002393 int n_children;
2394 int idx, ndots = 0;
2395 alias_ty mod = NULL;
2396 identifier modname;
2397
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002398 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002399 optional module name */
2400 for (idx = 1; idx < NCH(n); idx++) {
2401 if (TYPE(CHILD(n, idx)) == dotted_name) {
2402 mod = alias_for_import_name(c, CHILD(n, idx));
2403 idx++;
2404 break;
2405 } else if (TYPE(CHILD(n, idx)) != DOT) {
2406 break;
2407 }
2408 ndots++;
2409 }
2410 idx++; /* skip over the 'import' keyword */
2411 switch (TYPE(CHILD(n, idx))) {
2412 case STAR:
2413 /* from ... import * */
2414 n = CHILD(n, idx);
2415 n_children = 1;
2416 if (ndots) {
2417 ast_error(n, "'import *' not allowed with 'from .'");
2418 return NULL;
2419 }
2420 break;
2421 case LPAR:
2422 /* from ... import (x, y, z) */
2423 n = CHILD(n, idx + 1);
2424 n_children = NCH(n);
2425 break;
2426 case import_as_names:
2427 /* from ... import x, y, z */
2428 n = CHILD(n, idx);
2429 n_children = NCH(n);
2430 if (n_children % 2 == 0) {
2431 ast_error(n, "trailing comma not allowed without"
2432 " surrounding parentheses");
2433 return NULL;
2434 }
2435 break;
2436 default:
2437 ast_error(n, "Unexpected node-type in from-import");
2438 return NULL;
2439 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002441 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2442 if (!aliases)
2443 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002445 /* handle "from ... import *" special b/c there's no children */
2446 if (TYPE(n) == STAR) {
2447 alias_ty import_alias = alias_for_import_name(c, n);
2448 if (!import_alias)
2449 return NULL;
2450 asdl_seq_SET(aliases, 0, import_alias);
2451 }
2452 else {
2453 for (i = 0; i < NCH(n); i += 2) {
2454 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2455 if (!import_alias)
2456 return NULL;
2457 asdl_seq_SET(aliases, i / 2, import_alias);
2458 }
2459 }
2460 if (mod != NULL)
2461 modname = mod->name;
2462 else
2463 modname = new_identifier("", c->c_arena);
2464 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2465 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 }
Neal Norwitz79792652005-11-14 04:25:03 +00002467 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002468 "unknown import statement: starts with command '%s'",
2469 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 return NULL;
2471}
2472
2473static stmt_ty
2474ast_for_global_stmt(struct compiling *c, const node *n)
2475{
2476 /* global_stmt: 'global' NAME (',' NAME)* */
2477 identifier name;
2478 asdl_seq *s;
2479 int i;
2480
2481 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002482 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002484 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002486 name = NEW_IDENTIFIER(CHILD(n, i));
2487 if (!name)
2488 return NULL;
2489 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002491 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492}
2493
2494static stmt_ty
2495ast_for_exec_stmt(struct compiling *c, const node *n)
2496{
2497 expr_ty expr1, globals = NULL, locals = NULL;
2498 int n_children = NCH(n);
2499 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002500 PyErr_Format(PyExc_SystemError,
2501 "poorly formed 'exec' statement: %d parts to statement",
2502 n_children);
2503 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 }
2505
2506 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2507 REQ(n, exec_stmt);
2508 expr1 = ast_for_expr(c, CHILD(n, 1));
2509 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002510 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002512 globals = ast_for_expr(c, CHILD(n, 3));
2513 if (!globals)
2514 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 }
2516 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002517 locals = ast_for_expr(c, CHILD(n, 5));
2518 if (!locals)
2519 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 }
2521
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002522 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2523 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524}
2525
2526static stmt_ty
2527ast_for_assert_stmt(struct compiling *c, const node *n)
2528{
2529 /* assert_stmt: 'assert' test [',' test] */
2530 REQ(n, assert_stmt);
2531 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002532 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2533 if (!expression)
2534 return NULL;
2535 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2536 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 }
2538 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002539 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002541 expr1 = ast_for_expr(c, CHILD(n, 1));
2542 if (!expr1)
2543 return NULL;
2544 expr2 = ast_for_expr(c, CHILD(n, 3));
2545 if (!expr2)
2546 return NULL;
2547
2548 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 }
Neal Norwitz79792652005-11-14 04:25:03 +00002550 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002551 "improper number of parts to 'assert' statement: %d",
2552 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 return NULL;
2554}
2555
2556static asdl_seq *
2557ast_for_suite(struct compiling *c, const node *n)
2558{
2559 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002560 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 stmt_ty s;
2562 int i, total, num, end, pos = 0;
2563 node *ch;
2564
2565 REQ(n, suite);
2566
2567 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002568 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002570 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002572 n = CHILD(n, 0);
2573 /* simple_stmt always ends with a NEWLINE,
2574 and may have a trailing SEMI
2575 */
2576 end = NCH(n) - 1;
2577 if (TYPE(CHILD(n, end - 1)) == SEMI)
2578 end--;
2579 /* loop by 2 to skip semi-colons */
2580 for (i = 0; i < end; i += 2) {
2581 ch = CHILD(n, i);
2582 s = ast_for_stmt(c, ch);
2583 if (!s)
2584 return NULL;
2585 asdl_seq_SET(seq, pos++, s);
2586 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 }
2588 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002589 for (i = 2; i < (NCH(n) - 1); i++) {
2590 ch = CHILD(n, i);
2591 REQ(ch, stmt);
2592 num = num_stmts(ch);
2593 if (num == 1) {
2594 /* small_stmt or compound_stmt with only one child */
2595 s = ast_for_stmt(c, ch);
2596 if (!s)
2597 return NULL;
2598 asdl_seq_SET(seq, pos++, s);
2599 }
2600 else {
2601 int j;
2602 ch = CHILD(ch, 0);
2603 REQ(ch, simple_stmt);
2604 for (j = 0; j < NCH(ch); j += 2) {
2605 /* statement terminates with a semi-colon ';' */
2606 if (NCH(CHILD(ch, j)) == 0) {
2607 assert((j + 1) == NCH(ch));
2608 break;
2609 }
2610 s = ast_for_stmt(c, CHILD(ch, j));
2611 if (!s)
2612 return NULL;
2613 asdl_seq_SET(seq, pos++, s);
2614 }
2615 }
2616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 }
2618 assert(pos == seq->size);
2619 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620}
2621
2622static stmt_ty
2623ast_for_if_stmt(struct compiling *c, const node *n)
2624{
2625 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2626 ['else' ':' suite]
2627 */
2628 char *s;
2629
2630 REQ(n, if_stmt);
2631
2632 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002633 expr_ty expression;
2634 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002636 expression = ast_for_expr(c, CHILD(n, 1));
2637 if (!expression)
2638 return NULL;
2639 suite_seq = ast_for_suite(c, CHILD(n, 3));
2640 if (!suite_seq)
2641 return NULL;
2642
2643 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2644 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002646
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 s = STR(CHILD(n, 4));
2648 /* s[2], the third character in the string, will be
2649 's' for el_s_e, or
2650 'i' for el_i_f
2651 */
2652 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002653 expr_ty expression;
2654 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002656 expression = ast_for_expr(c, CHILD(n, 1));
2657 if (!expression)
2658 return NULL;
2659 seq1 = ast_for_suite(c, CHILD(n, 3));
2660 if (!seq1)
2661 return NULL;
2662 seq2 = ast_for_suite(c, CHILD(n, 6));
2663 if (!seq2)
2664 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002666 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2667 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 }
2669 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002670 int i, n_elif, has_else = 0;
2671 expr_ty expression;
2672 asdl_seq *suite_seq;
2673 asdl_seq *orelse = NULL;
2674 n_elif = NCH(n) - 4;
2675 /* must reference the child n_elif+1 since 'else' token is third,
2676 not fourth, child from the end. */
2677 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2678 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2679 has_else = 1;
2680 n_elif -= 3;
2681 }
2682 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002684 if (has_else) {
2685 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002687 orelse = asdl_seq_new(1, c->c_arena);
2688 if (!orelse)
2689 return NULL;
2690 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2691 if (!expression)
2692 return NULL;
2693 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2694 if (!suite_seq)
2695 return NULL;
2696 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2697 if (!suite_seq2)
2698 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002700 asdl_seq_SET(orelse, 0,
2701 If(expression, suite_seq, suite_seq2,
2702 LINENO(CHILD(n, NCH(n) - 6)),
2703 CHILD(n, NCH(n) - 6)->n_col_offset,
2704 c->c_arena));
2705 /* the just-created orelse handled the last elif */
2706 n_elif--;
2707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002709 for (i = 0; i < n_elif; i++) {
2710 int off = 5 + (n_elif - i - 1) * 4;
2711 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2712 if (!newobj)
2713 return NULL;
2714 expression = ast_for_expr(c, CHILD(n, off));
2715 if (!expression)
2716 return NULL;
2717 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2718 if (!suite_seq)
2719 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002721 asdl_seq_SET(newobj, 0,
2722 If(expression, suite_seq, orelse,
2723 LINENO(CHILD(n, off)),
2724 CHILD(n, off)->n_col_offset, c->c_arena));
2725 orelse = newobj;
2726 }
2727 expression = ast_for_expr(c, CHILD(n, 1));
2728 if (!expression)
2729 return NULL;
2730 suite_seq = ast_for_suite(c, CHILD(n, 3));
2731 if (!suite_seq)
2732 return NULL;
2733 return If(expression, suite_seq, orelse,
2734 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002736
2737 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002738 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740}
2741
2742static stmt_ty
2743ast_for_while_stmt(struct compiling *c, const node *n)
2744{
2745 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2746 REQ(n, while_stmt);
2747
2748 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002749 expr_ty expression;
2750 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002752 expression = ast_for_expr(c, CHILD(n, 1));
2753 if (!expression)
2754 return NULL;
2755 suite_seq = ast_for_suite(c, CHILD(n, 3));
2756 if (!suite_seq)
2757 return NULL;
2758 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2759 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760 }
2761 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002762 expr_ty expression;
2763 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002765 expression = ast_for_expr(c, CHILD(n, 1));
2766 if (!expression)
2767 return NULL;
2768 seq1 = ast_for_suite(c, CHILD(n, 3));
2769 if (!seq1)
2770 return NULL;
2771 seq2 = ast_for_suite(c, CHILD(n, 6));
2772 if (!seq2)
2773 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002775 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2776 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002778
2779 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002780 "wrong number of tokens for 'while' statement: %d",
2781 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002782 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783}
2784
2785static stmt_ty
2786ast_for_for_stmt(struct compiling *c, const node *n)
2787{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002788 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 expr_ty expression;
2790 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002791 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2793 REQ(n, for_stmt);
2794
2795 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002796 seq = ast_for_suite(c, CHILD(n, 8));
2797 if (!seq)
2798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 }
2800
Neal Norwitzedef2be2006-07-12 05:26:17 +00002801 node_target = CHILD(n, 1);
2802 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002803 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002804 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002805 /* Check the # of children rather than the length of _target, since
2806 for x, in ... has 1 element in _target, but still requires a Tuple. */
2807 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002808 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002810 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002812 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002813 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002816 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002819 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002820 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821}
2822
2823static excepthandler_ty
2824ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2825{
Collin Winter62903052007-05-18 23:11:24 +00002826 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 REQ(exc, except_clause);
2828 REQ(body, suite);
2829
2830 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002831 asdl_seq *suite_seq = ast_for_suite(c, body);
2832 if (!suite_seq)
2833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002835 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2836 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 }
2838 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002839 expr_ty expression;
2840 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002842 expression = ast_for_expr(c, CHILD(exc, 1));
2843 if (!expression)
2844 return NULL;
2845 suite_seq = ast_for_suite(c, body);
2846 if (!suite_seq)
2847 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002849 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2850 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 }
2852 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002853 asdl_seq *suite_seq;
2854 expr_ty expression;
2855 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2856 if (!e)
2857 return NULL;
2858 if (!set_context(e, Store, CHILD(exc, 3)))
2859 return NULL;
2860 expression = ast_for_expr(c, CHILD(exc, 1));
2861 if (!expression)
2862 return NULL;
2863 suite_seq = ast_for_suite(c, body);
2864 if (!suite_seq)
2865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002867 return excepthandler(expression, e, suite_seq, LINENO(exc),
2868 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002870
2871 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002872 "wrong number of children for 'except' clause: %d",
2873 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875}
2876
2877static stmt_ty
2878ast_for_try_stmt(struct compiling *c, const node *n)
2879{
Neal Norwitzf599f422005-12-17 21:33:47 +00002880 const int nch = NCH(n);
2881 int n_except = (nch - 3)/3;
2882 asdl_seq *body, *orelse = NULL, *finally = NULL;
2883
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 REQ(n, try_stmt);
2885
Neal Norwitzf599f422005-12-17 21:33:47 +00002886 body = ast_for_suite(c, CHILD(n, 2));
2887 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002888 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889
Neal Norwitzf599f422005-12-17 21:33:47 +00002890 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002891 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2892 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2893 /* we can assume it's an "else",
2894 because nch >= 9 for try-else-finally and
2895 it would otherwise have a type of except_clause */
2896 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2897 if (orelse == NULL)
2898 return NULL;
2899 n_except--;
2900 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002902 finally = ast_for_suite(c, CHILD(n, nch - 1));
2903 if (finally == NULL)
2904 return NULL;
2905 n_except--;
2906 }
2907 else {
2908 /* we can assume it's an "else",
2909 otherwise it would have a type of except_clause */
2910 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2911 if (orelse == NULL)
2912 return NULL;
2913 n_except--;
2914 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002916 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002917 ast_error(n, "malformed 'try' statement");
2918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002920
2921 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002922 int i;
2923 stmt_ty except_st;
2924 /* process except statements to create a try ... except */
2925 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2926 if (handlers == NULL)
2927 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002928
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002929 for (i = 0; i < n_except; i++) {
2930 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2931 CHILD(n, 5 + i * 3));
2932 if (!e)
2933 return NULL;
2934 asdl_seq_SET(handlers, i, e);
2935 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002936
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002937 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2938 n->n_col_offset, c->c_arena);
2939 if (!finally)
2940 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002941
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002942 /* if a 'finally' is present too, we nest the TryExcept within a
2943 TryFinally to emulate try ... except ... finally */
2944 body = asdl_seq_new(1, c->c_arena);
2945 if (body == NULL)
2946 return NULL;
2947 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002948 }
2949
2950 /* must be a try ... finally (except clauses are in body, if any exist) */
2951 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002952 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953}
2954
Guido van Rossumc2e20742006-02-27 22:32:47 +00002955static expr_ty
2956ast_for_with_var(struct compiling *c, const node *n)
2957{
2958 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002959 return ast_for_expr(c, CHILD(n, 1));
2960}
2961
2962/* with_stmt: 'with' test [ with_var ] ':' suite */
2963static stmt_ty
2964ast_for_with_stmt(struct compiling *c, const node *n)
2965{
2966 expr_ty context_expr, optional_vars = NULL;
2967 int suite_index = 3; /* skip 'with', test, and ':' */
2968 asdl_seq *suite_seq;
2969
2970 assert(TYPE(n) == with_stmt);
2971 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002972 if (!context_expr)
2973 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002974 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002975 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002976
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002977 if (!optional_vars) {
2978 return NULL;
2979 }
2980 if (!set_context(optional_vars, Store, n)) {
2981 return NULL;
2982 }
2983 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002984 }
2985
2986 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2987 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002988 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002989 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002990 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002991 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002992}
2993
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00002995ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996{
2997 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998 asdl_seq *bases, *s;
2999
3000 REQ(n, classdef);
3001
3002 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003003 ast_error(n, "assignment to None");
3004 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005 }
3006
3007 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003008 s = ast_for_suite(c, CHILD(n, 3));
3009 if (!s)
3010 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003011 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3012 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 }
3014 /* check for empty base list */
3015 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003016 s = ast_for_suite(c, CHILD(n,5));
3017 if (!s)
3018 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003019 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3020 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 }
3022
3023 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003024 bases = ast_for_class_bases(c, CHILD(n, 3));
3025 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003026 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027
3028 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003029 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003030 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003031 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3032 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033}
3034
3035static stmt_ty
3036ast_for_stmt(struct compiling *c, const node *n)
3037{
3038 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003039 assert(NCH(n) == 1);
3040 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 }
3042 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003043 assert(num_stmts(n) == 1);
3044 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 }
3046 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003047 REQ(n, small_stmt);
3048 n = CHILD(n, 0);
3049 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3050 | flow_stmt | import_stmt | global_stmt | exec_stmt
3051 | assert_stmt
3052 */
3053 switch (TYPE(n)) {
3054 case expr_stmt:
3055 return ast_for_expr_stmt(c, n);
3056 case print_stmt:
3057 return ast_for_print_stmt(c, n);
3058 case del_stmt:
3059 return ast_for_del_stmt(c, n);
3060 case pass_stmt:
3061 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3062 case flow_stmt:
3063 return ast_for_flow_stmt(c, n);
3064 case import_stmt:
3065 return ast_for_import_stmt(c, n);
3066 case global_stmt:
3067 return ast_for_global_stmt(c, n);
3068 case exec_stmt:
3069 return ast_for_exec_stmt(c, n);
3070 case assert_stmt:
3071 return ast_for_assert_stmt(c, n);
3072 default:
3073 PyErr_Format(PyExc_SystemError,
3074 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3075 TYPE(n), NCH(n));
3076 return NULL;
3077 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078 }
3079 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003080 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003081 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003082 */
3083 node *ch = CHILD(n, 0);
3084 REQ(n, compound_stmt);
3085 switch (TYPE(ch)) {
3086 case if_stmt:
3087 return ast_for_if_stmt(c, ch);
3088 case while_stmt:
3089 return ast_for_while_stmt(c, ch);
3090 case for_stmt:
3091 return ast_for_for_stmt(c, ch);
3092 case try_stmt:
3093 return ast_for_try_stmt(c, ch);
3094 case with_stmt:
3095 return ast_for_with_stmt(c, ch);
3096 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003097 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003098 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003099 return ast_for_classdef(c, ch, NULL);
3100 case decorated:
3101 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003102 default:
3103 PyErr_Format(PyExc_SystemError,
3104 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3105 TYPE(n), NCH(n));
3106 return NULL;
3107 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 }
3109}
3110
3111static PyObject *
3112parsenumber(const char *s)
3113{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003114 const char *end;
3115 long x;
3116 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003118 Py_complex c;
3119 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120#endif
3121
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003122 errno = 0;
3123 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003125 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003127 if (*end == 'l' || *end == 'L')
3128 return PyLong_FromString((char *)s, (char **)0, 0);
3129 if (s[0] == '0') {
3130 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3131 if (x < 0 && errno == 0) {
3132 return PyLong_FromString((char *)s,
3133 (char **)0,
3134 0);
3135 }
3136 }
3137 else
3138 x = PyOS_strtol((char *)s, (char **)&end, 0);
3139 if (*end == '\0') {
3140 if (errno != 0)
3141 return PyLong_FromString((char *)s, (char **)0, 0);
3142 return PyInt_FromLong(x);
3143 }
3144 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003146 if (imflag) {
3147 c.real = 0.;
3148 PyFPE_START_PROTECT("atof", return 0)
3149 c.imag = PyOS_ascii_atof(s);
3150 PyFPE_END_PROTECT(c)
3151 return PyComplex_FromCComplex(c);
3152 }
3153 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003155 {
3156 PyFPE_START_PROTECT("atof", return 0)
3157 dx = PyOS_ascii_atof(s);
3158 PyFPE_END_PROTECT(dx)
3159 return PyFloat_FromDouble(dx);
3160 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161}
3162
3163static PyObject *
3164decode_utf8(const char **sPtr, const char *end, char* encoding)
3165{
3166#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003167 Py_FatalError("decode_utf8 should not be called in this build.");
3168 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003170 PyObject *u, *v;
3171 char *s, *t;
3172 t = s = (char *)*sPtr;
3173 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3174 while (s < end && (*s & 0x80)) s++;
3175 *sPtr = s;
3176 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3177 if (u == NULL)
3178 return NULL;
3179 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3180 Py_DECREF(u);
3181 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182#endif
3183}
3184
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003185#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186static PyObject *
3187decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3188{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003189 PyObject *v, *u;
3190 char *buf;
3191 char *p;
3192 const char *end;
3193 if (encoding == NULL) {
3194 buf = (char *)s;
3195 u = NULL;
3196 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3197 buf = (char *)s;
3198 u = NULL;
3199 } else {
3200 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3201 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3202 if (u == NULL)
3203 return NULL;
3204 p = buf = PyString_AsString(u);
3205 end = s + len;
3206 while (s < end) {
3207 if (*s == '\\') {
3208 *p++ = *s++;
3209 if (*s & 0x80) {
3210 strcpy(p, "u005c");
3211 p += 5;
3212 }
3213 }
3214 if (*s & 0x80) { /* XXX inefficient */
3215 PyObject *w;
3216 char *r;
3217 Py_ssize_t rn, i;
3218 w = decode_utf8(&s, end, "utf-16-be");
3219 if (w == NULL) {
3220 Py_DECREF(u);
3221 return NULL;
3222 }
3223 r = PyString_AsString(w);
3224 rn = PyString_Size(w);
3225 assert(rn % 2 == 0);
3226 for (i = 0; i < rn; i += 2) {
3227 sprintf(p, "\\u%02x%02x",
3228 r[i + 0] & 0xFF,
3229 r[i + 1] & 0xFF);
3230 p += 6;
3231 }
3232 Py_DECREF(w);
3233 } else {
3234 *p++ = *s++;
3235 }
3236 }
3237 len = p - buf;
3238 s = buf;
3239 }
3240 if (rawmode)
3241 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3242 else
3243 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3244 Py_XDECREF(u);
3245 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003247#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248
3249/* s is a Python string literal, including the bracketing quote characters,
3250 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3251 * parsestr parses it, and returns the decoded Python string object.
3252 */
3253static PyObject *
3254parsestr(const char *s, const char *encoding)
3255{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003256 size_t len;
3257 int quote = Py_CHARMASK(*s);
3258 int rawmode = 0;
3259 int need_encoding;
3260 int unicode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003262 if (isalpha(quote) || quote == '_') {
3263 if (quote == 'u' || quote == 'U') {
3264 quote = *++s;
3265 unicode = 1;
3266 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003267 if (quote == 'b' || quote == 'B') {
3268 quote = *++s;
3269 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003270 if (quote == 'r' || quote == 'R') {
3271 quote = *++s;
3272 rawmode = 1;
3273 }
3274 }
3275 if (quote != '\'' && quote != '\"') {
3276 PyErr_BadInternalCall();
3277 return NULL;
3278 }
3279 s++;
3280 len = strlen(s);
3281 if (len > INT_MAX) {
3282 PyErr_SetString(PyExc_OverflowError,
3283 "string to parse is too long");
3284 return NULL;
3285 }
3286 if (s[--len] != quote) {
3287 PyErr_BadInternalCall();
3288 return NULL;
3289 }
3290 if (len >= 4 && s[0] == quote && s[1] == quote) {
3291 s += 2;
3292 len -= 2;
3293 if (s[--len] != quote || s[--len] != quote) {
3294 PyErr_BadInternalCall();
3295 return NULL;
3296 }
3297 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003299 if (unicode || Py_UnicodeFlag) {
3300 return decode_unicode(s, len, rawmode, encoding);
3301 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003303 need_encoding = (encoding != NULL &&
3304 strcmp(encoding, "utf-8") != 0 &&
3305 strcmp(encoding, "iso-8859-1") != 0);
3306 if (rawmode || strchr(s, '\\') == NULL) {
3307 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003309 /* This should not happen - we never see any other
3310 encoding. */
3311 Py_FatalError(
3312 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003314 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3315 if (u == NULL)
3316 return NULL;
3317 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3318 Py_DECREF(u);
3319 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003321 } else {
3322 return PyString_FromStringAndSize(s, len);
3323 }
3324 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003326 return PyString_DecodeEscape(s, len, NULL, unicode,
3327 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328}
3329
3330/* Build a Python string object out of a STRING atom. This takes care of
3331 * compile-time literal catenation, calling parsestr() on each piece, and
3332 * pasting the intermediate results together.
3333 */
3334static PyObject *
3335parsestrplus(struct compiling *c, const node *n)
3336{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003337 PyObject *v;
3338 int i;
3339 REQ(CHILD(n, 0), STRING);
3340 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3341 /* String literal concatenation */
3342 for (i = 1; i < NCH(n); i++) {
3343 PyObject *s;
3344 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3345 if (s == NULL)
3346 goto onError;
3347 if (PyString_Check(v) && PyString_Check(s)) {
3348 PyString_ConcatAndDel(&v, s);
3349 if (v == NULL)
3350 goto onError;
3351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003353 else {
3354 PyObject *temp = PyUnicode_Concat(v, s);
3355 Py_DECREF(s);
3356 Py_DECREF(v);
3357 v = temp;
3358 if (v == NULL)
3359 goto onError;
3360 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003362 }
3363 }
3364 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365
3366 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003367 Py_XDECREF(v);
3368 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369}