blob: 1d439ba891da8798ad1ea03bd11e65d2ad4cd669 [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/* XXX TO DO
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
21 - syntax errors
22*/
23
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024/* Data structure used internally */
25struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026 char *c_encoding; /* source encoding */
27 PyArena *c_arena; /* arena for allocating memeory */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028};
29
30static asdl_seq *seq_for_testlist(struct compiling *, const node *);
31static expr_ty ast_for_expr(struct compiling *, const node *);
32static stmt_ty ast_for_stmt(struct compiling *, const node *);
33static asdl_seq *ast_for_suite(struct compiling *, const node *);
Martin v. Löwis28457502006-04-11 09:17:27 +000034static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000035static expr_ty ast_for_testlist(struct compiling *, const node *);
36static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
38/* Note different signature for ast_for_call */
39static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
40
41static PyObject *parsenumber(const char *);
42static PyObject *parsestr(const char *s, const char *encoding);
43static PyObject *parsestrplus(struct compiling *, const node *n);
44
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#ifndef LINENO
46#define LINENO(n) ((n)->n_lineno)
47#endif
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
50new_identifier(const char* n, PyArena *arena) {
51 PyObject* id = PyString_InternFromString(n);
Benjamin Peterson1bd74de2008-11-25 22:49:28 +000052 if (id != NULL)
53 PyArena_AddPyObject(arena, id);
Neal Norwitzadb69fc2005-12-17 20:54:49 +000054 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055}
56
Neal Norwitzadb69fc2005-12-17 20:54:49 +000057#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058
59/* This routine provides an invalid object for the syntax error.
60 The outermost routine must unpack this error and create the
61 proper object. We do this so that we don't have to pass
62 the filename to everything function.
63
64 XXX Maybe we should just pass the filename...
65*/
66
67static int
68ast_error(const node *n, const char *errstr)
69{
70 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
71 if (!u)
72 return 0;
73 PyErr_SetObject(PyExc_SyntaxError, u);
74 Py_DECREF(u);
75 return 0;
76}
77
78static void
79ast_error_finish(const char *filename)
80{
81 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000082 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083
84 assert(PyErr_Occurred());
85 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
86 return;
87
88 PyErr_Fetch(&type, &value, &tback);
89 errstr = PyTuple_GetItem(value, 0);
90 if (!errstr)
91 return;
92 Py_INCREF(errstr);
93 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000094 if (lineno == -1) {
95 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000097 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098 Py_DECREF(value);
99
100 loc = PyErr_ProgramText(filename, lineno);
101 if (!loc) {
102 Py_INCREF(Py_None);
103 loc = Py_None;
104 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000105 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000107 if (!tmp) {
108 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000110 }
Georg Brandl7784f122006-05-26 20:04:44 +0000111 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112 Py_DECREF(errstr);
113 Py_DECREF(tmp);
114 if (!value)
115 return;
116 PyErr_Restore(type, value, tback);
117}
118
119/* num_stmts() returns number of contained statements.
120
121 Use this routine to determine how big a sequence is needed for
122 the statements in a parse tree. Its raison d'etre is this bit of
123 grammar:
124
125 stmt: simple_stmt | compound_stmt
126 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
127
128 A simple_stmt can contain multiple small_stmt elements joined
129 by semicolons. If the arg is a simple_stmt, the number of
130 small_stmt elements is returned.
131*/
132
133static int
134num_stmts(const node *n)
135{
136 int i, l;
137 node *ch;
138
139 switch (TYPE(n)) {
140 case single_input:
141 if (TYPE(CHILD(n, 0)) == NEWLINE)
142 return 0;
143 else
144 return num_stmts(CHILD(n, 0));
145 case file_input:
146 l = 0;
147 for (i = 0; i < NCH(n); i++) {
148 ch = CHILD(n, i);
149 if (TYPE(ch) == stmt)
150 l += num_stmts(ch);
151 }
152 return l;
153 case stmt:
154 return num_stmts(CHILD(n, 0));
155 case compound_stmt:
156 return 1;
157 case simple_stmt:
158 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
159 case suite:
160 if (NCH(n) == 1)
161 return num_stmts(CHILD(n, 0));
162 else {
163 l = 0;
164 for (i = 2; i < (NCH(n) - 1); i++)
165 l += num_stmts(CHILD(n, i));
166 return l;
167 }
168 default: {
169 char buf[128];
170
171 sprintf(buf, "Non-statement found: %d %d\n",
172 TYPE(n), NCH(n));
173 Py_FatalError(buf);
174 }
175 }
176 assert(0);
177 return 0;
178}
179
180/* Transform the CST rooted at node * to the appropriate AST
181*/
182
183mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000184PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
185 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000187 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188 asdl_seq *stmts = NULL;
189 stmt_ty s;
190 node *ch;
191 struct compiling c;
192
193 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000194 c.c_encoding = "utf-8";
Neal Norwitze98ccf62006-03-23 05:39:47 +0000195 if (TYPE(n) == encoding_decl) {
196 ast_error(n, "encoding declaration in Unicode string");
197 goto error;
198 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 } else if (TYPE(n) == encoding_decl) {
200 c.c_encoding = STR(n);
201 n = CHILD(n, 0);
202 } else {
203 c.c_encoding = NULL;
204 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000205 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
Jeremy Hyltona8293132006-02-28 17:58:27 +0000207 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208 switch (TYPE(n)) {
209 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000210 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211 if (!stmts)
212 return NULL;
213 for (i = 0; i < NCH(n) - 1; i++) {
214 ch = CHILD(n, i);
215 if (TYPE(ch) == NEWLINE)
216 continue;
217 REQ(ch, stmt);
218 num = num_stmts(ch);
219 if (num == 1) {
220 s = ast_for_stmt(&c, ch);
221 if (!s)
222 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000223 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224 }
225 else {
226 ch = CHILD(ch, 0);
227 REQ(ch, simple_stmt);
228 for (j = 0; j < num; j++) {
229 s = ast_for_stmt(&c, CHILD(ch, j * 2));
230 if (!s)
231 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000232 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 }
234 }
235 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000236 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 case eval_input: {
238 expr_ty testlist_ast;
239
240 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000241 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 if (!testlist_ast)
243 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000244 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245 }
246 case single_input:
247 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000248 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249 if (!stmts)
250 goto error;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000251 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
252 arena));
Georg Brandld297f1a2008-06-15 19:53:12 +0000253 if (!asdl_seq_GET(stmts, 0))
254 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000255 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256 }
257 else {
258 n = CHILD(n, 0);
259 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000260 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 if (!stmts)
262 goto error;
263 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000264 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 if (!s)
266 goto error;
267 asdl_seq_SET(stmts, 0, s);
268 }
269 else {
270 /* Only a simple_stmt can contain multiple statements. */
271 REQ(n, simple_stmt);
272 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273 if (TYPE(CHILD(n, i)) == NEWLINE)
274 break;
275 s = ast_for_stmt(&c, CHILD(n, i));
276 if (!s)
277 goto error;
278 asdl_seq_SET(stmts, i / 2, s);
279 }
280 }
281
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000282 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 }
284 default:
285 goto error;
286 }
287 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288 ast_error_finish(filename);
289 return NULL;
290}
291
292/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
293*/
294
295static operator_ty
296get_operator(const node *n)
297{
298 switch (TYPE(n)) {
299 case VBAR:
300 return BitOr;
301 case CIRCUMFLEX:
302 return BitXor;
303 case AMPER:
304 return BitAnd;
305 case LEFTSHIFT:
306 return LShift;
307 case RIGHTSHIFT:
308 return RShift;
309 case PLUS:
310 return Add;
311 case MINUS:
312 return Sub;
313 case STAR:
314 return Mult;
315 case SLASH:
316 return Div;
317 case DOUBLESLASH:
318 return FloorDiv;
319 case PERCENT:
320 return Mod;
321 default:
Martin v. Löwis28457502006-04-11 09:17:27 +0000322 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323 }
324}
325
Jeremy Hyltona8293132006-02-28 17:58:27 +0000326/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327
328 Only sets context for expr kinds that "can appear in assignment context"
329 (according to ../Parser/Python.asdl). For other expr kinds, it sets
330 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331*/
332
333static int
334set_context(expr_ty e, expr_context_ty ctx, const node *n)
335{
336 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000337 /* If a particular expression type can't be used for assign / delete,
338 set expr_name to its name and an error message will be generated.
339 */
340 const char* expr_name = NULL;
341
342 /* The ast defines augmented store and load contexts, but the
343 implementation here doesn't actually use them. The code may be
344 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000345 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000346 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000347 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000348 */
349 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350
351 switch (e->kind) {
352 case Attribute_kind:
353 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000354 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355 return ast_error(n, "assignment to None");
356 }
357 e->v.Attribute.ctx = ctx;
358 break;
359 case Subscript_kind:
360 e->v.Subscript.ctx = ctx;
361 break;
362 case Name_kind:
363 if (ctx == Store &&
364 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
365 return ast_error(n, "assignment to None");
366 }
367 e->v.Name.ctx = ctx;
368 break;
369 case List_kind:
370 e->v.List.ctx = ctx;
371 s = e->v.List.elts;
372 break;
373 case Tuple_kind:
374 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
375 return ast_error(n, "can't assign to ()");
376 e->v.Tuple.ctx = ctx;
377 s = e->v.Tuple.elts;
378 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000379 case Lambda_kind:
380 expr_name = "lambda";
381 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000383 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000385 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000387 case UnaryOp_kind:
388 expr_name = "operator";
389 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000391 expr_name = "generator expression";
392 break;
Neal Norwitz0d62a062006-07-30 06:53:31 +0000393 case Yield_kind:
394 expr_name = "yield expression";
395 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000396 case ListComp_kind:
397 expr_name = "list comprehension";
398 break;
399 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400 case Num_kind:
401 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000402 expr_name = "literal";
403 break;
404 case Compare_kind:
405 expr_name = "comparison";
406 break;
407 case Repr_kind:
408 expr_name = "repr";
409 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000410 case IfExp_kind:
411 expr_name = "conditional expression";
412 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000413 default:
414 PyErr_Format(PyExc_SystemError,
415 "unexpected expression in assignment %d (line %d)",
416 e->kind, e->lineno);
417 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000419 /* Check for error string set by switch */
420 if (expr_name) {
421 char buf[300];
422 PyOS_snprintf(buf, sizeof(buf),
423 "can't %s %s",
424 ctx == Store ? "assign to" : "delete",
425 expr_name);
426 return ast_error(n, buf);
427 }
428
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000430 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431 */
432 if (s) {
433 int i;
434
435 for (i = 0; i < asdl_seq_LEN(s); i++) {
Martin v. Löwis28457502006-04-11 09:17:27 +0000436 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437 return 0;
438 }
439 }
440 return 1;
441}
442
443static operator_ty
444ast_for_augassign(const node *n)
445{
446 REQ(n, augassign);
447 n = CHILD(n, 0);
448 switch (STR(n)[0]) {
449 case '+':
450 return Add;
451 case '-':
452 return Sub;
453 case '/':
454 if (STR(n)[1] == '/')
455 return FloorDiv;
456 else
457 return Div;
458 case '%':
459 return Mod;
460 case '<':
461 return LShift;
462 case '>':
463 return RShift;
464 case '&':
465 return BitAnd;
466 case '^':
467 return BitXor;
468 case '|':
469 return BitOr;
470 case '*':
471 if (STR(n)[1] == '*')
472 return Pow;
473 else
474 return Mult;
475 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000476 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000477 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478 }
479}
480
481static cmpop_ty
482ast_for_comp_op(const node *n)
483{
484 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
485 |'is' 'not'
486 */
487 REQ(n, comp_op);
488 if (NCH(n) == 1) {
489 n = CHILD(n, 0);
490 switch (TYPE(n)) {
491 case LESS:
492 return Lt;
493 case GREATER:
494 return Gt;
495 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 return Eq;
497 case LESSEQUAL:
498 return LtE;
499 case GREATEREQUAL:
500 return GtE;
501 case NOTEQUAL:
502 return NotEq;
503 case NAME:
504 if (strcmp(STR(n), "in") == 0)
505 return In;
506 if (strcmp(STR(n), "is") == 0)
507 return Is;
508 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000509 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000511 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 }
513 }
514 else if (NCH(n) == 2) {
515 /* handle "not in" and "is not" */
516 switch (TYPE(CHILD(n, 0))) {
517 case NAME:
518 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
519 return NotIn;
520 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
521 return IsNot;
522 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000523 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000525 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 }
527 }
Neal Norwitz79792652005-11-14 04:25:03 +0000528 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000530 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531}
532
533static asdl_seq *
534seq_for_testlist(struct compiling *c, const node *n)
535{
536 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000537 asdl_seq *seq;
538 expr_ty expression;
539 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540 assert(TYPE(n) == testlist
541 || TYPE(n) == listmaker
542 || TYPE(n) == testlist_gexp
543 || TYPE(n) == testlist_safe
Neal Norwitza3ce6aa2006-11-04 19:32:54 +0000544 || TYPE(n) == testlist1
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000547 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 if (!seq)
549 return NULL;
550
551 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000552 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553
554 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000555 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557
558 assert(i / 2 < seq->size);
559 asdl_seq_SET(seq, i / 2, expression);
560 }
561 return seq;
562}
563
564static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000565compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566{
567 int i, len = (NCH(n) + 1) / 2;
568 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000569 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 if (!args)
571 return NULL;
572
Georg Brandlc57221e2006-09-25 07:04:10 +0000573 /* fpdef: NAME | '(' fplist ')'
574 fplist: fpdef (',' fpdef)* [',']
575 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 for (i = 0; i < len; i++) {
Benjamin Peterson1bd74de2008-11-25 22:49:28 +0000578 PyObject *arg_id;
Georg Brandlc57221e2006-09-25 07:04:10 +0000579 const node *fpdef_node = CHILD(n, 2*i);
580 const node *child;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 expr_ty arg;
Georg Brandlc57221e2006-09-25 07:04:10 +0000582set_name:
583 /* fpdef_node is either a NAME or an fplist */
584 child = CHILD(fpdef_node, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585 if (TYPE(child) == NAME) {
Benjamin Peterson1bd74de2008-11-25 22:49:28 +0000586 if (!strcmp(STR(child), "None")) {
587 ast_error(child, "assignment to None");
588 return NULL;
589 }
590 arg_id = NEW_IDENTIFIER(child);
591 if (!arg_id)
592 return NULL;
593 arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
594 c->c_arena);
595 }
Jeremy Hyltona8293132006-02-28 17:58:27 +0000596 else {
Georg Brandlc57221e2006-09-25 07:04:10 +0000597 assert(TYPE(fpdef_node) == fpdef);
598 /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
599 child = CHILD(fpdef_node, 1);
600 assert(TYPE(child) == fplist);
601 /* NCH == 1 means we have (x), we need to elide the extra parens */
602 if (NCH(child) == 1) {
603 fpdef_node = CHILD(child, 0);
604 assert(TYPE(fpdef_node) == fpdef);
605 goto set_name;
606 }
607 arg = compiler_complex_args(c, child);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000608 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 asdl_seq_SET(args, i, arg);
610 }
611
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000612 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000613 if (!set_context(result, Store, n))
614 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 return result;
616}
617
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618
Jeremy Hyltona8293132006-02-28 17:58:27 +0000619/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620
621static arguments_ty
622ast_for_arguments(struct compiling *c, const node *n)
623{
624 /* parameters: '(' [varargslist] ')'
625 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
626 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
627 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000628 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629 asdl_seq *args, *defaults;
630 identifier vararg = NULL, kwarg = NULL;
631 node *ch;
632
633 if (TYPE(n) == parameters) {
634 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000635 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 n = CHILD(n, 1);
637 }
638 REQ(n, varargslist);
639
640 /* first count the number of normal args & defaults */
641 for (i = 0; i < NCH(n); i++) {
642 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000643 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (TYPE(ch) == EQUAL)
646 n_defaults++;
647 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000648 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 if (!args && n_args)
Neal Norwitzc173b482006-07-30 19:18:13 +0000650 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000651 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 if (!defaults && n_defaults)
Neal Norwitzc173b482006-07-30 19:18:13 +0000653 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
655 /* fpdef: NAME | '(' fplist ')'
656 fplist: fpdef (',' fpdef)* [',']
657 */
658 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000659 j = 0; /* index for defaults */
660 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 while (i < NCH(n)) {
662 ch = CHILD(n, i);
663 switch (TYPE(ch)) {
664 case fpdef:
Georg Brandlc57221e2006-09-25 07:04:10 +0000665 handle_fpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
667 anything other than EQUAL or a comma? */
668 /* XXX Should NCH(n) check be made a separate check? */
669 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000670 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
671 if (!expression)
672 goto error;
Neal Norwitzc173b482006-07-30 19:18:13 +0000673 assert(defaults != NULL);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000674 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 i += 2;
676 found_default = 1;
677 }
678 else if (found_default) {
679 ast_error(n,
680 "non-default argument follows default argument");
681 goto error;
682 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000684 ch = CHILD(ch, 1);
685 /* def foo((x)): is not complex, special case. */
686 if (NCH(ch) != 1) {
687 /* We have complex arguments, setup for unpacking. */
688 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Georg Brandld297f1a2008-06-15 19:53:12 +0000689 if (!asdl_seq_GET(args, k-1))
690 goto error;
Neal Norwitz33b730e2006-03-27 08:58:23 +0000691 } else {
692 /* def foo((x)): setup for checking NAME below. */
Georg Brandlc57221e2006-09-25 07:04:10 +0000693 /* Loop because there can be many parens and tuple
694 upacking mixed in. */
Neal Norwitz33b730e2006-03-27 08:58:23 +0000695 ch = CHILD(ch, 0);
Georg Brandlc57221e2006-09-25 07:04:10 +0000696 assert(TYPE(ch) == fpdef);
697 goto handle_fpdef;
Neal Norwitz33b730e2006-03-27 08:58:23 +0000698 }
699 }
700 if (TYPE(CHILD(ch, 0)) == NAME) {
Benjamin Peterson1bd74de2008-11-25 22:49:28 +0000701 PyObject *id;
Armin Rigo31441302005-10-21 12:57:31 +0000702 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
704 ast_error(CHILD(ch, 0), "assignment to None");
705 goto error;
706 }
Benjamin Peterson1bd74de2008-11-25 22:49:28 +0000707 id = NEW_IDENTIFIER(CHILD(ch, 0));
708 if (!id)
709 goto error;
710 name = Name(id, Param, LINENO(ch), ch->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000711 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 if (!name)
713 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000714 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715
716 }
717 i += 2; /* the name and the comma */
718 break;
719 case STAR:
720 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
721 ast_error(CHILD(n, i+1), "assignment to None");
722 goto error;
723 }
724 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson1bd74de2008-11-25 22:49:28 +0000725 if (!vararg)
726 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 i += 3;
728 break;
729 case DOUBLESTAR:
730 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
731 ast_error(CHILD(n, i+1), "assignment to None");
732 goto error;
733 }
734 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
Benjamin Peterson1bd74de2008-11-25 22:49:28 +0000735 if (!kwarg)
736 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 i += 3;
738 break;
739 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000740 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 "unexpected node in varargslist: %d @ %d",
742 TYPE(ch), i);
743 goto error;
744 }
745 }
746
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000747 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748
749 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000750 Py_XDECREF(vararg);
751 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 return NULL;
753}
754
755static expr_ty
756ast_for_dotted_name(struct compiling *c, const node *n)
757{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000758 expr_ty e;
759 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000760 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 int i;
762
763 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000764
765 lineno = LINENO(n);
766 col_offset = n->n_col_offset;
767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 id = NEW_IDENTIFIER(CHILD(n, 0));
769 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000770 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000771 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000773 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774
775 for (i = 2; i < NCH(n); i+=2) {
776 id = NEW_IDENTIFIER(CHILD(n, i));
777 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000778 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000779 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000780 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000781 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 }
783
784 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785}
786
787static expr_ty
788ast_for_decorator(struct compiling *c, const node *n)
789{
790 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
791 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000792 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
794 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000795 REQ(CHILD(n, 0), AT);
796 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797
798 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
799 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801
802 if (NCH(n) == 3) { /* No arguments */
803 d = name_expr;
804 name_expr = NULL;
805 }
806 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000807 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
808 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000810 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 name_expr = NULL;
812 }
813 else {
814 d = ast_for_call(c, CHILD(n, 3), name_expr);
815 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 name_expr = NULL;
818 }
819
820 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821}
822
823static asdl_seq*
824ast_for_decorators(struct compiling *c, const node *n)
825{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000826 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000827 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 int i;
829
830 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000831 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 if (!decorator_seq)
833 return NULL;
834
835 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000836 d = ast_for_decorator(c, CHILD(n, i));
837 if (!d)
838 return NULL;
839 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 }
841 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842}
843
844static stmt_ty
845ast_for_funcdef(struct compiling *c, const node *n)
846{
847 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000848 identifier name;
849 arguments_ty args;
850 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 asdl_seq *decorator_seq = NULL;
852 int name_i;
853
854 REQ(n, funcdef);
855
856 if (NCH(n) == 6) { /* decorators are present */
857 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
858 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 name_i = 2;
861 }
862 else {
863 name_i = 1;
864 }
865
866 name = NEW_IDENTIFIER(CHILD(n, name_i));
867 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000868 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000870 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000871 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 }
873 args = ast_for_arguments(c, CHILD(n, name_i + 1));
874 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000875 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 body = ast_for_suite(c, CHILD(n, name_i + 3));
877 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000878 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000880 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
881 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882}
883
884static expr_ty
885ast_for_lambdef(struct compiling *c, const node *n)
886{
887 /* lambdef: 'lambda' [varargslist] ':' test */
888 arguments_ty args;
889 expr_ty expression;
890
891 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000892 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893 if (!args)
894 return NULL;
895 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000896 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898 }
899 else {
900 args = ast_for_arguments(c, CHILD(n, 1));
901 if (!args)
902 return NULL;
903 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000904 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 }
907
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000908 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909}
910
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000911static expr_ty
912ast_for_ifexpr(struct compiling *c, const node *n)
913{
914 /* test: or_test 'if' or_test 'else' test */
915 expr_ty expression, body, orelse;
916
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000917 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000918 body = ast_for_expr(c, CHILD(n, 0));
919 if (!body)
920 return NULL;
921 expression = ast_for_expr(c, CHILD(n, 2));
922 if (!expression)
923 return NULL;
924 orelse = ast_for_expr(c, CHILD(n, 4));
925 if (!orelse)
926 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000927 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
928 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000929}
930
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931/* Count the number of 'for' loop in a list comprehension.
932
933 Helper for ast_for_listcomp().
934*/
935
936static int
937count_list_fors(const node *n)
938{
939 int n_fors = 0;
940 node *ch = CHILD(n, 1);
941
942 count_list_for:
943 n_fors++;
944 REQ(ch, list_for);
945 if (NCH(ch) == 5)
946 ch = CHILD(ch, 4);
947 else
948 return n_fors;
949 count_list_iter:
950 REQ(ch, list_iter);
951 ch = CHILD(ch, 0);
952 if (TYPE(ch) == list_for)
953 goto count_list_for;
954 else if (TYPE(ch) == list_if) {
955 if (NCH(ch) == 3) {
956 ch = CHILD(ch, 2);
957 goto count_list_iter;
958 }
959 else
960 return n_fors;
961 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000962
963 /* Should never be reached */
964 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
965 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966}
967
968/* Count the number of 'if' statements in a list comprehension.
969
970 Helper for ast_for_listcomp().
971*/
972
973static int
974count_list_ifs(const node *n)
975{
976 int n_ifs = 0;
977
978 count_list_iter:
979 REQ(n, list_iter);
980 if (TYPE(CHILD(n, 0)) == list_for)
981 return n_ifs;
982 n = CHILD(n, 0);
983 REQ(n, list_if);
984 n_ifs++;
985 if (NCH(n) == 2)
986 return n_ifs;
987 n = CHILD(n, 2);
988 goto count_list_iter;
989}
990
991static expr_ty
992ast_for_listcomp(struct compiling *c, const node *n)
993{
994 /* listmaker: test ( list_for | (',' test)* [','] )
995 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
996 list_iter: list_for | list_if
997 list_if: 'if' test [list_iter]
998 testlist_safe: test [(',' test)+ [',']]
999 */
1000 expr_ty elt;
1001 asdl_seq *listcomps;
1002 int i, n_fors;
1003 node *ch;
1004
1005 REQ(n, listmaker);
1006 assert(NCH(n) > 1);
1007
1008 elt = ast_for_expr(c, CHILD(n, 0));
1009 if (!elt)
1010 return NULL;
1011
1012 n_fors = count_list_fors(n);
1013 if (n_fors == -1)
1014 return NULL;
1015
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001016 listcomps = asdl_seq_new(n_fors, c->c_arena);
1017 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 ch = CHILD(n, 1);
1021 for (i = 0; i < n_fors; i++) {
1022 comprehension_ty lc;
1023 asdl_seq *t;
1024 expr_ty expression;
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001025 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
1027 REQ(ch, list_for);
1028
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001029 for_ch = CHILD(ch, 1);
1030 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001031 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001033 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001034 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001037 /* Check the # of children rather than the length of t, since
1038 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
1039 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001040 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001041 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001043 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1044 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001045 expression, NULL, c->c_arena);
1046 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048
1049 if (NCH(ch) == 5) {
1050 int j, n_ifs;
1051 asdl_seq *ifs;
Collin Winter7d9ac782007-03-16 04:12:48 +00001052 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053
1054 ch = CHILD(ch, 4);
1055 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001056 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001059 ifs = asdl_seq_new(n_ifs, c->c_arena);
1060 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062
1063 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001064 REQ(ch, list_iter);
1065 ch = CHILD(ch, 0);
1066 REQ(ch, list_if);
Collin Winter7d9ac782007-03-16 04:12:48 +00001067
1068 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1069 if (!list_for_expr)
1070 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071
Collin Winter7d9ac782007-03-16 04:12:48 +00001072 asdl_seq_SET(ifs, j, list_for_expr);
Jeremy Hyltona8293132006-02-28 17:58:27 +00001073 if (NCH(ch) == 3)
1074 ch = CHILD(ch, 2);
1075 }
1076 /* on exit, must guarantee that ch is a list_for */
1077 if (TYPE(ch) == list_iter)
1078 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001080 }
1081 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082 }
1083
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001084 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085}
1086
1087/*
1088 Count the number of 'for' loops in a generator expression.
1089
1090 Helper for ast_for_genexp().
1091*/
1092
1093static int
1094count_gen_fors(const node *n)
1095{
1096 int n_fors = 0;
1097 node *ch = CHILD(n, 1);
1098
1099 count_gen_for:
1100 n_fors++;
1101 REQ(ch, gen_for);
1102 if (NCH(ch) == 5)
1103 ch = CHILD(ch, 4);
1104 else
1105 return n_fors;
1106 count_gen_iter:
1107 REQ(ch, gen_iter);
1108 ch = CHILD(ch, 0);
1109 if (TYPE(ch) == gen_for)
1110 goto count_gen_for;
1111 else if (TYPE(ch) == gen_if) {
1112 if (NCH(ch) == 3) {
1113 ch = CHILD(ch, 2);
1114 goto count_gen_iter;
1115 }
1116 else
1117 return n_fors;
1118 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001119
1120 /* Should never be reached */
1121 PyErr_SetString(PyExc_SystemError,
1122 "logic error in count_gen_fors");
1123 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124}
1125
1126/* Count the number of 'if' statements in a generator expression.
1127
1128 Helper for ast_for_genexp().
1129*/
1130
1131static int
1132count_gen_ifs(const node *n)
1133{
1134 int n_ifs = 0;
1135
1136 while (1) {
1137 REQ(n, gen_iter);
1138 if (TYPE(CHILD(n, 0)) == gen_for)
1139 return n_ifs;
1140 n = CHILD(n, 0);
1141 REQ(n, gen_if);
1142 n_ifs++;
1143 if (NCH(n) == 2)
1144 return n_ifs;
1145 n = CHILD(n, 2);
1146 }
1147}
1148
Jeremy Hyltona8293132006-02-28 17:58:27 +00001149/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150static expr_ty
1151ast_for_genexp(struct compiling *c, const node *n)
1152{
1153 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1154 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1155 expr_ty elt;
1156 asdl_seq *genexps;
1157 int i, n_fors;
1158 node *ch;
1159
1160 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1161 assert(NCH(n) > 1);
1162
1163 elt = ast_for_expr(c, CHILD(n, 0));
1164 if (!elt)
1165 return NULL;
1166
1167 n_fors = count_gen_fors(n);
1168 if (n_fors == -1)
1169 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001170
1171 genexps = asdl_seq_new(n_fors, c->c_arena);
1172 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001174
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 ch = CHILD(n, 1);
1176 for (i = 0; i < n_fors; i++) {
1177 comprehension_ty ge;
1178 asdl_seq *t;
1179 expr_ty expression;
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001180 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181
1182 REQ(ch, gen_for);
1183
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001184 for_ch = CHILD(ch, 1);
1185 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001186 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001188 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001189 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001191
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001192 /* Check the # of children rather than the length of t, since
1193 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1194 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001195 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001196 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001198 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1199 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200 expression, NULL, c->c_arena);
1201
1202 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (NCH(ch) == 5) {
1206 int j, n_ifs;
1207 asdl_seq *ifs;
1208
1209 ch = CHILD(ch, 4);
1210 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001211 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001213
1214 ifs = asdl_seq_new(n_ifs, c->c_arena);
1215 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001217
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 for (j = 0; j < n_ifs; j++) {
1219 REQ(ch, gen_iter);
1220 ch = CHILD(ch, 0);
1221 REQ(ch, gen_if);
1222
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001223 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001224 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001225 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001226 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 if (NCH(ch) == 3)
1228 ch = CHILD(ch, 2);
1229 }
1230 /* on exit, must guarantee that ch is a gen_for */
1231 if (TYPE(ch) == gen_iter)
1232 ch = CHILD(ch, 0);
1233 ge->ifs = ifs;
1234 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001235 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 }
1237
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001238 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239}
1240
1241static expr_ty
1242ast_for_atom(struct compiling *c, const node *n)
1243{
1244 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1245 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1246 */
1247 node *ch = CHILD(n, 0);
1248
1249 switch (TYPE(ch)) {
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00001250 case NAME: {
1251 /* All names start in Load context, but may later be
1252 changed. */
1253 PyObject *id = NEW_IDENTIFIER(ch);
1254 if (!id)
1255 return NULL;
1256 return Name(id, Load, LINENO(n), n->n_col_offset, c->c_arena);
1257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 case STRING: {
1259 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 if (!str)
1261 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001262
1263 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001264 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 }
1266 case NUMBER: {
1267 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 if (!pynum)
1269 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001270
1271 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001272 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 }
1274 case LPAR: /* some parenthesized expressions */
1275 ch = CHILD(n, 1);
1276
1277 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001278 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279
1280 if (TYPE(ch) == yield_expr)
1281 return ast_for_expr(c, ch);
1282
1283 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1284 return ast_for_genexp(c, ch);
1285
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001286 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 case LSQB: /* list (or list comprehension) */
1288 ch = CHILD(n, 1);
1289
1290 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001291 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292
1293 REQ(ch, listmaker);
1294 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1295 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 if (!elts)
1297 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001298
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001299 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 }
1301 else
1302 return ast_for_listcomp(c, ch);
1303 case LBRACE: {
1304 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1305 int i, size;
1306 asdl_seq *keys, *values;
1307
1308 ch = CHILD(n, 1);
1309 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001310 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 if (!keys)
1312 return NULL;
1313
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001314 values = asdl_seq_new(size, c->c_arena);
1315 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317
1318 for (i = 0; i < NCH(ch); i += 4) {
1319 expr_ty expression;
1320
1321 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001322 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001324
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001326
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001328 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001330
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 asdl_seq_SET(values, i / 4, expression);
1332 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001333 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 }
1335 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001336 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 if (!expression)
1338 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001339
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001340 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 }
1342 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001343 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 return NULL;
1345 }
1346}
1347
1348static slice_ty
1349ast_for_slice(struct compiling *c, const node *n)
1350{
1351 node *ch;
1352 expr_ty lower = NULL, upper = NULL, step = NULL;
1353
1354 REQ(n, subscript);
1355
1356 /*
1357 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1358 sliceop: ':' [test]
1359 */
1360 ch = CHILD(n, 0);
1361 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001362 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363
1364 if (NCH(n) == 1 && TYPE(ch) == test) {
1365 /* 'step' variable hold no significance in terms of being used over
1366 other vars */
1367 step = ast_for_expr(c, ch);
1368 if (!step)
1369 return NULL;
1370
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001371 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 }
1373
1374 if (TYPE(ch) == test) {
1375 lower = ast_for_expr(c, ch);
1376 if (!lower)
1377 return NULL;
1378 }
1379
1380 /* If there's an upper bound it's in the second or third position. */
1381 if (TYPE(ch) == COLON) {
1382 if (NCH(n) > 1) {
1383 node *n2 = CHILD(n, 1);
1384
1385 if (TYPE(n2) == test) {
1386 upper = ast_for_expr(c, n2);
1387 if (!upper)
1388 return NULL;
1389 }
1390 }
1391 } else if (NCH(n) > 2) {
1392 node *n2 = CHILD(n, 2);
1393
1394 if (TYPE(n2) == test) {
1395 upper = ast_for_expr(c, n2);
1396 if (!upper)
1397 return NULL;
1398 }
1399 }
1400
1401 ch = CHILD(n, NCH(n) - 1);
1402 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001403 if (NCH(ch) == 1) {
1404 /* No expression, so step is None */
1405 ch = CHILD(ch, 0);
1406 step = Name(new_identifier("None", c->c_arena), Load,
1407 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 if (!step)
1409 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001410 } else {
1411 ch = CHILD(ch, 1);
1412 if (TYPE(ch) == test) {
1413 step = ast_for_expr(c, ch);
1414 if (!step)
1415 return NULL;
1416 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 }
1418 }
1419
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001420 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421}
1422
1423static expr_ty
1424ast_for_binop(struct compiling *c, const node *n)
1425{
1426 /* Must account for a sequence of expressions.
1427 How should A op B op C by represented?
1428 BinOp(BinOp(A, op, B), op, C).
1429 */
1430
1431 int i, nops;
1432 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001433 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434
1435 expr1 = ast_for_expr(c, CHILD(n, 0));
1436 if (!expr1)
1437 return NULL;
1438
1439 expr2 = ast_for_expr(c, CHILD(n, 2));
1440 if (!expr2)
1441 return NULL;
1442
Anthony Baxtera863d332006-04-11 07:43:46 +00001443 newoperator = get_operator(CHILD(n, 1));
1444 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 return NULL;
1446
Anthony Baxtera863d332006-04-11 07:43:46 +00001447 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001448 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 if (!result)
1450 return NULL;
1451
1452 nops = (NCH(n) - 1) / 2;
1453 for (i = 1; i < nops; i++) {
1454 expr_ty tmp_result, tmp;
1455 const node* next_oper = CHILD(n, i * 2 + 1);
1456
Anthony Baxtera863d332006-04-11 07:43:46 +00001457 newoperator = get_operator(next_oper);
1458 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 return NULL;
1460
1461 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1462 if (!tmp)
1463 return NULL;
1464
Anthony Baxtera863d332006-04-11 07:43:46 +00001465 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001466 LINENO(next_oper), next_oper->n_col_offset,
1467 c->c_arena);
Neal Norwitz14f848b2007-10-05 03:45:42 +00001468 if (!tmp_result)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 return NULL;
1470 result = tmp_result;
1471 }
1472 return result;
1473}
1474
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001475static expr_ty
1476ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1477{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001478 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1479 subscriptlist: subscript (',' subscript)* [',']
1480 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1481 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001482 REQ(n, trailer);
1483 if (TYPE(CHILD(n, 0)) == LPAR) {
1484 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001485 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1486 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001487 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001488 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001489 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001490 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00001491 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1492 if (!attr_id)
1493 return NULL;
1494 return Attribute(left_expr, attr_id, Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001495 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001496 }
1497 else {
1498 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001499 REQ(CHILD(n, 2), RSQB);
1500 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001501 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001502 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1503 if (!slc)
1504 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001505 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1506 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001507 }
1508 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001509 /* The grammar is ambiguous here. The ambiguity is resolved
1510 by treating the sequence as a tuple literal if there are
1511 no slice features.
1512 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001513 int j;
1514 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001515 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001516 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001517 asdl_seq *slices, *elts;
1518 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001519 if (!slices)
1520 return NULL;
1521 for (j = 0; j < NCH(n); j += 2) {
1522 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001523 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001524 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001525 if (slc->kind != Index_kind)
1526 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001527 asdl_seq_SET(slices, j / 2, slc);
1528 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001529 if (!simple) {
1530 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001531 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001532 }
1533 /* extract Index values and put them in a Tuple */
1534 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001535 if (!elts)
1536 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001537 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1538 slc = (slice_ty)asdl_seq_GET(slices, j);
1539 assert(slc->kind == Index_kind && slc->v.Index.value);
1540 asdl_seq_SET(elts, j, slc->v.Index.value);
1541 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001542 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001543 if (!e)
1544 return NULL;
1545 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001546 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001547 }
1548 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001549}
1550
1551static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001552ast_for_factor(struct compiling *c, const node *n)
1553{
1554 node *pfactor, *ppower, *patom, *pnum;
1555 expr_ty expression;
1556
1557 /* If the unary - operator is applied to a constant, don't generate
1558 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1559 constant. The peephole optimizer already does something like
1560 this but it doesn't handle the case where the constant is
1561 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1562 PyLongObject.
1563 */
1564 if (TYPE(CHILD(n, 0)) == MINUS
1565 && NCH(n) == 2
1566 && TYPE((pfactor = CHILD(n, 1))) == factor
1567 && NCH(pfactor) == 1
1568 && TYPE((ppower = CHILD(pfactor, 0))) == power
1569 && NCH(ppower) == 1
1570 && TYPE((patom = CHILD(ppower, 0))) == atom
1571 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1572 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1573 if (s == NULL)
1574 return NULL;
1575 s[0] = '-';
1576 strcpy(s + 1, STR(pnum));
1577 PyObject_FREE(STR(pnum));
1578 STR(pnum) = s;
1579 return ast_for_atom(c, patom);
1580 }
1581
1582 expression = ast_for_expr(c, CHILD(n, 1));
1583 if (!expression)
1584 return NULL;
1585
1586 switch (TYPE(CHILD(n, 0))) {
1587 case PLUS:
1588 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1589 c->c_arena);
1590 case MINUS:
1591 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1592 c->c_arena);
1593 case TILDE:
1594 return UnaryOp(Invert, expression, LINENO(n),
1595 n->n_col_offset, c->c_arena);
1596 }
1597 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1598 TYPE(CHILD(n, 0)));
1599 return NULL;
1600}
1601
1602static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001603ast_for_power(struct compiling *c, const node *n)
1604{
1605 /* power: atom trailer* ('**' factor)*
1606 */
1607 int i;
1608 expr_ty e, tmp;
1609 REQ(n, power);
1610 e = ast_for_atom(c, CHILD(n, 0));
1611 if (!e)
1612 return NULL;
1613 if (NCH(n) == 1)
1614 return e;
1615 for (i = 1; i < NCH(n); i++) {
1616 node *ch = CHILD(n, i);
1617 if (TYPE(ch) != trailer)
1618 break;
1619 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001620 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001621 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001622 tmp->lineno = e->lineno;
1623 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001624 e = tmp;
1625 }
1626 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1627 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001628 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001629 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001630 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001631 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001632 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001633 e = tmp;
1634 }
1635 return e;
1636}
1637
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638/* Do not name a variable 'expr'! Will cause a compile error.
1639*/
1640
1641static expr_ty
1642ast_for_expr(struct compiling *c, const node *n)
1643{
1644 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001645 test: or_test ['if' or_test 'else' test] | lambdef
1646 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 and_test: not_test ('and' not_test)*
1648 not_test: 'not' not_test | comparison
1649 comparison: expr (comp_op expr)*
1650 expr: xor_expr ('|' xor_expr)*
1651 xor_expr: and_expr ('^' and_expr)*
1652 and_expr: shift_expr ('&' shift_expr)*
1653 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1654 arith_expr: term (('+'|'-') term)*
1655 term: factor (('*'|'/'|'%'|'//') factor)*
1656 factor: ('+'|'-'|'~') factor | power
1657 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001658
1659 As well as modified versions that exist for backward compatibility,
1660 to explicitly allow:
1661 [ x for x in lambda: 0, lambda: 1 ]
1662 (which would be ambiguous without these extra rules)
1663
1664 old_test: or_test | old_lambdef
1665 old_lambdef: 'lambda' [vararglist] ':' old_test
1666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 */
1668
1669 asdl_seq *seq;
1670 int i;
1671
1672 loop:
1673 switch (TYPE(n)) {
1674 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001675 case old_test:
1676 if (TYPE(CHILD(n, 0)) == lambdef ||
1677 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001679 else if (NCH(n) > 1)
1680 return ast_for_ifexpr(c, n);
1681 /* Fallthrough */
1682 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 case and_test:
1684 if (NCH(n) == 1) {
1685 n = CHILD(n, 0);
1686 goto loop;
1687 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001688 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 if (!seq)
1690 return NULL;
1691 for (i = 0; i < NCH(n); i += 2) {
1692 expr_ty e = ast_for_expr(c, CHILD(n, i));
1693 if (!e)
1694 return NULL;
1695 asdl_seq_SET(seq, i / 2, e);
1696 }
1697 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001698 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1699 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001700 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001701 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 case not_test:
1703 if (NCH(n) == 1) {
1704 n = CHILD(n, 0);
1705 goto loop;
1706 }
1707 else {
1708 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1709 if (!expression)
1710 return NULL;
1711
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001712 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1713 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 }
1715 case comparison:
1716 if (NCH(n) == 1) {
1717 n = CHILD(n, 0);
1718 goto loop;
1719 }
1720 else {
1721 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001722 asdl_int_seq *ops;
1723 asdl_seq *cmps;
1724 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 if (!ops)
1726 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001727 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 return NULL;
1730 }
1731 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001732 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733
Anthony Baxtera863d332006-04-11 07:43:46 +00001734 newoperator = ast_for_comp_op(CHILD(n, i));
1735 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001737 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738
1739 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001740 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001742 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001744 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 asdl_seq_SET(cmps, i / 2, expression);
1746 }
1747 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001748 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001752 return Compare(expression, ops, cmps, LINENO(n),
1753 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 }
1755 break;
1756
1757 /* The next five cases all handle BinOps. The main body of code
1758 is the same in each case, but the switch turned inside out to
1759 reuse the code for each type of operator.
1760 */
1761 case expr:
1762 case xor_expr:
1763 case and_expr:
1764 case shift_expr:
1765 case arith_expr:
1766 case term:
1767 if (NCH(n) == 1) {
1768 n = CHILD(n, 0);
1769 goto loop;
1770 }
1771 return ast_for_binop(c, n);
1772 case yield_expr: {
1773 expr_ty exp = NULL;
1774 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001775 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776 if (!exp)
1777 return NULL;
1778 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001779 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001781 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 if (NCH(n) == 1) {
1783 n = CHILD(n, 0);
1784 goto loop;
1785 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001786 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001787 case power:
1788 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001790 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791 return NULL;
1792 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001793 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 return NULL;
1795}
1796
1797static expr_ty
1798ast_for_call(struct compiling *c, const node *n, expr_ty func)
1799{
1800 /*
1801 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1802 | '**' test)
1803 argument: [test '='] test [gen_for] # Really [keyword '='] test
1804 */
1805
1806 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001807 asdl_seq *args;
1808 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 expr_ty vararg = NULL, kwarg = NULL;
1810
1811 REQ(n, arglist);
1812
1813 nargs = 0;
1814 nkeywords = 0;
1815 ngens = 0;
1816 for (i = 0; i < NCH(n); i++) {
1817 node *ch = CHILD(n, i);
1818 if (TYPE(ch) == argument) {
1819 if (NCH(ch) == 1)
1820 nargs++;
1821 else if (TYPE(CHILD(ch, 1)) == gen_for)
1822 ngens++;
1823 else
1824 nkeywords++;
1825 }
1826 }
1827 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001828 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 "if not sole argument");
1830 return NULL;
1831 }
1832
1833 if (nargs + nkeywords + ngens > 255) {
1834 ast_error(n, "more than 255 arguments");
1835 return NULL;
1836 }
1837
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001838 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001840 return NULL;
1841 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 nargs = 0;
1845 nkeywords = 0;
1846 for (i = 0; i < NCH(n); i++) {
1847 node *ch = CHILD(n, i);
1848 if (TYPE(ch) == argument) {
1849 expr_ty e;
1850 if (NCH(ch) == 1) {
Neal Norwitz5ef92242006-05-19 06:43:50 +00001851 if (nkeywords) {
1852 ast_error(CHILD(ch, 0),
1853 "non-keyword arg after keyword arg");
1854 return NULL;
1855 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 e = ast_for_expr(c, CHILD(ch, 0));
1857 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 asdl_seq_SET(args, nargs++, e);
1860 }
1861 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1862 e = ast_for_genexp(c, ch);
1863 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001864 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 asdl_seq_SET(args, nargs++, e);
1866 }
1867 else {
1868 keyword_ty kw;
1869 identifier key;
1870
1871 /* CHILD(ch, 0) is test, but must be an identifier? */
1872 e = ast_for_expr(c, CHILD(ch, 0));
1873 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 /* f(lambda x: x[0] = 3) ends up getting parsed with
1876 * LHS test = lambda x: x[0], and RHS test = 3.
1877 * SF bug 132313 points out that complaining about a keyword
1878 * then is very confusing.
1879 */
1880 if (e->kind == Lambda_kind) {
Georg Brandl73c958a2007-06-07 13:23:28 +00001881 ast_error(CHILD(ch, 0),
1882 "lambda cannot contain assignment");
1883 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 } else if (e->kind != Name_kind) {
Georg Brandl73c958a2007-06-07 13:23:28 +00001885 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1886 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 }
1888 key = e->v.Name.id;
Georg Brandl73c958a2007-06-07 13:23:28 +00001889 if (!strcmp(PyString_AS_STRING(key), "None")) {
1890 ast_error(CHILD(ch, 0), "assignment to None");
1891 return NULL;
1892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 e = ast_for_expr(c, CHILD(ch, 2));
1894 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895 return NULL;
1896 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001898 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 asdl_seq_SET(keywords, nkeywords++, kw);
1900 }
1901 }
1902 else if (TYPE(ch) == STAR) {
1903 vararg = ast_for_expr(c, CHILD(n, i+1));
Sean Reifscheider4af861c2008-03-20 17:39:31 +00001904 if (!vararg)
1905 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 i++;
1907 }
1908 else if (TYPE(ch) == DOUBLESTAR) {
1909 kwarg = ast_for_expr(c, CHILD(n, i+1));
Sean Reifscheider4af861c2008-03-20 17:39:31 +00001910 if (!kwarg)
1911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 i++;
1913 }
1914 }
1915
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001916 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917}
1918
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001920ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001922 /* testlist_gexp: test (',' test)* [','] */
1923 /* testlist: test (',' test)* [','] */
1924 /* testlist_safe: test (',' test)+ [','] */
1925 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001927 if (TYPE(n) == testlist_gexp) {
1928 if (NCH(n) > 1)
1929 assert(TYPE(CHILD(n, 1)) != gen_for);
1930 }
1931 else {
1932 assert(TYPE(n) == testlist ||
1933 TYPE(n) == testlist_safe ||
1934 TYPE(n) == testlist1);
1935 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 if (NCH(n) == 1)
1937 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938 else {
1939 asdl_seq *tmp = seq_for_testlist(c, n);
1940 if (!tmp)
1941 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001942 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001944}
1945
1946static expr_ty
1947ast_for_testlist_gexp(struct compiling *c, const node* n)
1948{
1949 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1950 /* argument: test [ gen_for ] */
1951 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001952 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001953 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001954 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001955}
1956
1957/* like ast_for_testlist() but returns a sequence */
1958static asdl_seq*
1959ast_for_class_bases(struct compiling *c, const node* n)
1960{
1961 /* testlist: test (',' test)* [','] */
1962 assert(NCH(n) > 0);
1963 REQ(n, testlist);
1964 if (NCH(n) == 1) {
1965 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001966 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001967 if (!bases)
1968 return NULL;
1969 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001970 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001971 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001972 asdl_seq_SET(bases, 0, base);
1973 return bases;
1974 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001975
1976 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977}
1978
1979static stmt_ty
1980ast_for_expr_stmt(struct compiling *c, const node *n)
1981{
1982 REQ(n, expr_stmt);
1983 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1984 | ('=' (yield_expr|testlist))*)
1985 testlist: test (',' test)* [',']
1986 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1987 | '<<=' | '>>=' | '**=' | '//='
1988 test: ... here starts the operator precendence dance
1989 */
1990
1991 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001992 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 if (!e)
1994 return NULL;
1995
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001996 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 }
1998 else if (TYPE(CHILD(n, 1)) == augassign) {
1999 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00002000 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 node *ch = CHILD(n, 0);
2002
Neal Norwitz0d62a062006-07-30 06:53:31 +00002003 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 if (!expr1)
2005 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00002006 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00002007 switch (expr1->kind) {
2008 case GeneratorExp_kind:
2009 ast_error(ch, "augmented assignment to generator "
2010 "expression not possible");
2011 return NULL;
Neal Norwitz0d62a062006-07-30 06:53:31 +00002012 case Yield_kind:
2013 ast_error(ch, "augmented assignment to yield "
2014 "expression not possible");
2015 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00002016 case Name_kind: {
2017 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2018 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2019 ast_error(ch, "assignment to None");
2020 return NULL;
2021 }
2022 break;
2023 }
2024 case Attribute_kind:
2025 case Subscript_kind:
2026 break;
2027 default:
2028 ast_error(ch, "illegal expression for augmented "
2029 "assignment");
2030 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 }
Collin Winter7d9ac782007-03-16 04:12:48 +00002032 if (!set_context(expr1, Store, ch))
2033 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034
2035 ch = CHILD(n, 2);
2036 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002037 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 else
Neal Norwitz0d62a062006-07-30 06:53:31 +00002039 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002040 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 return NULL;
2042
Anthony Baxtera863d332006-04-11 07:43:46 +00002043 newoperator = ast_for_augassign(CHILD(n, 1));
2044 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 return NULL;
2046
Anthony Baxtera863d332006-04-11 07:43:46 +00002047 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048 }
2049 else {
2050 int i;
2051 asdl_seq *targets;
2052 node *value;
2053 expr_ty expression;
2054
2055 /* a normal assignment */
2056 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002057 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 if (!targets)
2059 return NULL;
2060 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002061 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062 node *ch = CHILD(n, i);
2063 if (TYPE(ch) == yield_expr) {
2064 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002067 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068
2069 /* set context to assign */
2070 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002071 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072
Neal Norwitz84456bd2005-12-18 03:16:20 +00002073 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002074 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075
2076 asdl_seq_SET(targets, i / 2, e);
2077 }
2078 value = CHILD(n, NCH(n) - 1);
2079 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002080 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 else
2082 expression = ast_for_expr(c, value);
2083 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002084 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002085 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087}
2088
2089static stmt_ty
2090ast_for_print_stmt(struct compiling *c, const node *n)
2091{
2092 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2093 | '>>' test [ (',' test)+ [','] ] )
2094 */
2095 expr_ty dest = NULL, expression;
2096 asdl_seq *seq;
2097 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002098 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099
2100 REQ(n, print_stmt);
2101 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2102 dest = ast_for_expr(c, CHILD(n, 2));
2103 if (!dest)
2104 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002105 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002107 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002109 return NULL;
2110 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002112 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002114 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 }
2116 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002117 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118}
2119
2120static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002121ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122{
2123 asdl_seq *seq;
2124 int i;
2125 expr_ty e;
2126
2127 REQ(n, exprlist);
2128
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002129 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 if (!seq)
2131 return NULL;
2132 for (i = 0; i < NCH(n); i += 2) {
2133 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002134 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002135 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002136 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002137 if (context && !set_context(e, context, CHILD(n, i)))
2138 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 }
2140 return seq;
2141}
2142
2143static stmt_ty
2144ast_for_del_stmt(struct compiling *c, const node *n)
2145{
2146 asdl_seq *expr_list;
2147
2148 /* del_stmt: 'del' exprlist */
2149 REQ(n, del_stmt);
2150
2151 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2152 if (!expr_list)
2153 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002154 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155}
2156
2157static stmt_ty
2158ast_for_flow_stmt(struct compiling *c, const node *n)
2159{
2160 /*
2161 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2162 | yield_stmt
2163 break_stmt: 'break'
2164 continue_stmt: 'continue'
2165 return_stmt: 'return' [testlist]
2166 yield_stmt: yield_expr
2167 yield_expr: 'yield' testlist
2168 raise_stmt: 'raise' [test [',' test [',' test]]]
2169 */
2170 node *ch;
2171
2172 REQ(n, flow_stmt);
2173 ch = CHILD(n, 0);
2174 switch (TYPE(ch)) {
2175 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002176 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002178 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 case yield_stmt: { /* will reduce to yield_expr */
2180 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2181 if (!exp)
2182 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002183 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 }
2185 case return_stmt:
2186 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002187 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002189 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 if (!expression)
2191 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002192 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 }
2194 case raise_stmt:
2195 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002196 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 else if (NCH(ch) == 2) {
2198 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2199 if (!expression)
2200 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002201 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 }
2203 else if (NCH(ch) == 4) {
2204 expr_ty expr1, expr2;
2205
2206 expr1 = ast_for_expr(c, CHILD(ch, 1));
2207 if (!expr1)
2208 return NULL;
2209 expr2 = ast_for_expr(c, CHILD(ch, 3));
2210 if (!expr2)
2211 return NULL;
2212
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002213 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214 }
2215 else if (NCH(ch) == 6) {
2216 expr_ty expr1, expr2, expr3;
2217
2218 expr1 = ast_for_expr(c, CHILD(ch, 1));
2219 if (!expr1)
2220 return NULL;
2221 expr2 = ast_for_expr(c, CHILD(ch, 3));
2222 if (!expr2)
2223 return NULL;
2224 expr3 = ast_for_expr(c, CHILD(ch, 5));
2225 if (!expr3)
2226 return NULL;
2227
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002228 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 }
2230 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002231 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 "unexpected flow_stmt: %d", TYPE(ch));
2233 return NULL;
2234 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002235
2236 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2237 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238}
2239
2240static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002241alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242{
2243 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002244 import_as_name: NAME ['as' NAME]
2245 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 dotted_name: NAME ('.' NAME)*
2247 */
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00002248 PyObject *str, *name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002249
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 loop:
2251 switch (TYPE(n)) {
2252 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002253 str = NULL;
2254 if (NCH(n) == 3) {
2255 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2256 ast_error(n, "must use 'as' in import");
2257 return NULL;
2258 }
2259 str = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00002260 if (!str)
2261 return NULL;
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002262 }
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00002263 name = NEW_IDENTIFIER(CHILD(n, 0));
2264 if (!name)
2265 return NULL;
2266 return alias(name, str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 case dotted_as_name:
2268 if (NCH(n) == 1) {
2269 n = CHILD(n, 0);
2270 goto loop;
2271 }
2272 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002273 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00002274 if (!a)
2275 return NULL;
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002276 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2277 ast_error(n, "must use 'as' in import");
2278 return NULL;
2279 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 assert(!a->asname);
2281 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00002282 if (!a->asname)
2283 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 return a;
2285 }
2286 break;
2287 case dotted_name:
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00002288 if (NCH(n) == 1) {
2289 name = NEW_IDENTIFIER(CHILD(n, 0));
2290 if (!name)
2291 return NULL;
2292 return alias(name, NULL, c->c_arena);
2293 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 else {
2295 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002296 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002297 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 char *s;
2299
2300 len = 0;
2301 for (i = 0; i < NCH(n); i += 2)
2302 /* length of string plus one for the dot */
2303 len += strlen(STR(CHILD(n, i))) + 1;
2304 len--; /* the last name doesn't have a dot */
2305 str = PyString_FromStringAndSize(NULL, len);
2306 if (!str)
2307 return NULL;
2308 s = PyString_AS_STRING(str);
2309 if (!s)
2310 return NULL;
2311 for (i = 0; i < NCH(n); i += 2) {
2312 char *sch = STR(CHILD(n, i));
2313 strcpy(s, STR(CHILD(n, i)));
2314 s += strlen(sch);
2315 *s++ = '.';
2316 }
2317 --s;
2318 *s = '\0';
2319 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002320 PyArena_AddPyObject(c->c_arena, str);
2321 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 }
2323 break;
2324 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002325 str = PyString_InternFromString("*");
2326 PyArena_AddPyObject(c->c_arena, str);
2327 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002329 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 "unexpected import name: %d", TYPE(n));
2331 return NULL;
2332 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002333
2334 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 return NULL;
2336}
2337
2338static stmt_ty
2339ast_for_import_stmt(struct compiling *c, const node *n)
2340{
2341 /*
2342 import_stmt: import_name | import_from
2343 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002344 import_from: 'from' ('.'* dotted_name | '.') 'import'
2345 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002347 int lineno;
2348 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 int i;
2350 asdl_seq *aliases;
2351
2352 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002353 lineno = LINENO(n);
2354 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002356 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002358 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002359 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 if (!aliases)
2361 return NULL;
2362 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002363 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002364 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366 asdl_seq_SET(aliases, i / 2, import_alias);
2367 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002368 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002369 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002370 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002372 int idx, ndots = 0;
2373 alias_ty mod = NULL;
2374 identifier modname;
2375
2376 /* Count the number of dots (for relative imports) and check for the
2377 optional module name */
2378 for (idx = 1; idx < NCH(n); idx++) {
2379 if (TYPE(CHILD(n, idx)) == dotted_name) {
2380 mod = alias_for_import_name(c, CHILD(n, idx));
2381 idx++;
2382 break;
2383 } else if (TYPE(CHILD(n, idx)) != DOT) {
2384 break;
2385 }
2386 ndots++;
2387 }
2388 idx++; /* skip over the 'import' keyword */
2389 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002390 case STAR:
2391 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002392 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002393 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002394 if (ndots) {
2395 ast_error(n, "'import *' not allowed with 'from .'");
2396 return NULL;
2397 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002398 break;
2399 case LPAR:
2400 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002401 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002402 n_children = NCH(n);
2403 break;
2404 case import_as_names:
2405 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002406 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002407 n_children = NCH(n);
2408 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 ast_error(n, "trailing comma not allowed without"
2410 " surrounding parentheses");
2411 return NULL;
2412 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002413 break;
2414 default:
2415 ast_error(n, "Unexpected node-type in from-import");
2416 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002419 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002420 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422
2423 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002424 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002425 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002426 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002428 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002430 else {
2431 for (i = 0; i < NCH(n); i += 2) {
2432 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2433 if (!import_alias)
2434 return NULL;
2435 asdl_seq_SET(aliases, i / 2, import_alias);
2436 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002438 if (mod != NULL)
2439 modname = mod->name;
2440 else
2441 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002442 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002443 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 }
Neal Norwitz79792652005-11-14 04:25:03 +00002445 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 "unknown import statement: starts with command '%s'",
2447 STR(CHILD(n, 0)));
2448 return NULL;
2449}
2450
2451static stmt_ty
2452ast_for_global_stmt(struct compiling *c, const node *n)
2453{
2454 /* global_stmt: 'global' NAME (',' NAME)* */
2455 identifier name;
2456 asdl_seq *s;
2457 int i;
2458
2459 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002460 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 if (!s)
2462 return NULL;
2463 for (i = 1; i < NCH(n); i += 2) {
2464 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002465 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 asdl_seq_SET(s, i / 2, name);
2468 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002469 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470}
2471
2472static stmt_ty
2473ast_for_exec_stmt(struct compiling *c, const node *n)
2474{
2475 expr_ty expr1, globals = NULL, locals = NULL;
2476 int n_children = NCH(n);
2477 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002478 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 "poorly formed 'exec' statement: %d parts to statement",
2480 n_children);
2481 return NULL;
2482 }
2483
2484 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2485 REQ(n, exec_stmt);
2486 expr1 = ast_for_expr(c, CHILD(n, 1));
2487 if (!expr1)
2488 return NULL;
2489 if (n_children >= 4) {
2490 globals = ast_for_expr(c, CHILD(n, 3));
2491 if (!globals)
2492 return NULL;
2493 }
2494 if (n_children == 6) {
2495 locals = ast_for_expr(c, CHILD(n, 5));
2496 if (!locals)
2497 return NULL;
2498 }
2499
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002500 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501}
2502
2503static stmt_ty
2504ast_for_assert_stmt(struct compiling *c, const node *n)
2505{
2506 /* assert_stmt: 'assert' test [',' test] */
2507 REQ(n, assert_stmt);
2508 if (NCH(n) == 2) {
2509 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2510 if (!expression)
2511 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002512 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 }
2514 else if (NCH(n) == 4) {
2515 expr_ty expr1, expr2;
2516
2517 expr1 = ast_for_expr(c, CHILD(n, 1));
2518 if (!expr1)
2519 return NULL;
2520 expr2 = ast_for_expr(c, CHILD(n, 3));
2521 if (!expr2)
2522 return NULL;
2523
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002524 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 }
Neal Norwitz79792652005-11-14 04:25:03 +00002526 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 "improper number of parts to 'assert' statement: %d",
2528 NCH(n));
2529 return NULL;
2530}
2531
2532static asdl_seq *
2533ast_for_suite(struct compiling *c, const node *n)
2534{
2535 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002536 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 stmt_ty s;
2538 int i, total, num, end, pos = 0;
2539 node *ch;
2540
2541 REQ(n, suite);
2542
2543 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002544 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 if (!seq)
2546 return NULL;
2547 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2548 n = CHILD(n, 0);
2549 /* simple_stmt always ends with a NEWLINE,
2550 and may have a trailing SEMI
2551 */
2552 end = NCH(n) - 1;
2553 if (TYPE(CHILD(n, end - 1)) == SEMI)
2554 end--;
2555 /* loop by 2 to skip semi-colons */
2556 for (i = 0; i < end; i += 2) {
2557 ch = CHILD(n, i);
2558 s = ast_for_stmt(c, ch);
2559 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002560 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 asdl_seq_SET(seq, pos++, s);
2562 }
2563 }
2564 else {
2565 for (i = 2; i < (NCH(n) - 1); i++) {
2566 ch = CHILD(n, i);
2567 REQ(ch, stmt);
2568 num = num_stmts(ch);
2569 if (num == 1) {
2570 /* small_stmt or compound_stmt with only one child */
2571 s = ast_for_stmt(c, ch);
2572 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002573 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 asdl_seq_SET(seq, pos++, s);
2575 }
2576 else {
2577 int j;
2578 ch = CHILD(ch, 0);
2579 REQ(ch, simple_stmt);
2580 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002581 /* statement terminates with a semi-colon ';' */
2582 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002583 assert((j + 1) == NCH(ch));
2584 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 s = ast_for_stmt(c, CHILD(ch, j));
2587 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002588 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 asdl_seq_SET(seq, pos++, s);
2590 }
2591 }
2592 }
2593 }
2594 assert(pos == seq->size);
2595 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596}
2597
2598static stmt_ty
2599ast_for_if_stmt(struct compiling *c, const node *n)
2600{
2601 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2602 ['else' ':' suite]
2603 */
2604 char *s;
2605
2606 REQ(n, if_stmt);
2607
2608 if (NCH(n) == 4) {
2609 expr_ty expression;
2610 asdl_seq *suite_seq;
2611
2612 expression = ast_for_expr(c, CHILD(n, 1));
2613 if (!expression)
2614 return NULL;
2615 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002616 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 return NULL;
2618
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002619 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002621
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 s = STR(CHILD(n, 4));
2623 /* s[2], the third character in the string, will be
2624 's' for el_s_e, or
2625 'i' for el_i_f
2626 */
2627 if (s[2] == 's') {
2628 expr_ty expression;
2629 asdl_seq *seq1, *seq2;
2630
2631 expression = ast_for_expr(c, CHILD(n, 1));
2632 if (!expression)
2633 return NULL;
2634 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002635 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
2637 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002638 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 return NULL;
2640
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002641 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 }
2643 else if (s[2] == 'i') {
2644 int i, n_elif, has_else = 0;
Collin Winter7d9ac782007-03-16 04:12:48 +00002645 expr_ty expression;
2646 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 asdl_seq *orelse = NULL;
2648 n_elif = NCH(n) - 4;
2649 /* must reference the child n_elif+1 since 'else' token is third,
2650 not fourth, child from the end. */
2651 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2652 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2653 has_else = 1;
2654 n_elif -= 3;
2655 }
2656 n_elif /= 4;
2657
2658 if (has_else) {
Collin Winter7d9ac782007-03-16 04:12:48 +00002659 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002661 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 if (!orelse)
2663 return NULL;
2664 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002665 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 return NULL;
Collin Winter7d9ac782007-03-16 04:12:48 +00002667 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2668 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 return NULL;
Collin Winter7d9ac782007-03-16 04:12:48 +00002670 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2671 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673
Collin Winter7d9ac782007-03-16 04:12:48 +00002674 asdl_seq_SET(orelse, 0, If(expression, suite_seq, suite_seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002675 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002676 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 /* the just-created orelse handled the last elif */
2678 n_elif--;
2679 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680
2681 for (i = 0; i < n_elif; i++) {
2682 int off = 5 + (n_elif - i - 1) * 4;
Anthony Baxtera863d332006-04-11 07:43:46 +00002683 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2684 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685 return NULL;
2686 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002687 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002690 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692
Anthony Baxtera863d332006-04-11 07:43:46 +00002693 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002695 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Anthony Baxtera863d332006-04-11 07:43:46 +00002696 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 }
Collin Winter7d9ac782007-03-16 04:12:48 +00002698 expression = ast_for_expr(c, CHILD(n, 1));
2699 if (!expression)
2700 return NULL;
2701 suite_seq = ast_for_suite(c, CHILD(n, 3));
2702 if (!suite_seq)
2703 return NULL;
2704 return If(expression, suite_seq, orelse,
2705 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002707
2708 PyErr_Format(PyExc_SystemError,
2709 "unexpected token in 'if' statement: %s", s);
2710 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711}
2712
2713static stmt_ty
2714ast_for_while_stmt(struct compiling *c, const node *n)
2715{
2716 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2717 REQ(n, while_stmt);
2718
2719 if (NCH(n) == 4) {
2720 expr_ty expression;
2721 asdl_seq *suite_seq;
2722
2723 expression = ast_for_expr(c, CHILD(n, 1));
2724 if (!expression)
2725 return NULL;
2726 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002727 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002729 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 }
2731 else if (NCH(n) == 7) {
2732 expr_ty expression;
2733 asdl_seq *seq1, *seq2;
2734
2735 expression = ast_for_expr(c, CHILD(n, 1));
2736 if (!expression)
2737 return NULL;
2738 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002739 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 return NULL;
2741 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002742 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 return NULL;
2744
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002745 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002747
2748 PyErr_Format(PyExc_SystemError,
2749 "wrong number of tokens for 'while' statement: %d",
2750 NCH(n));
2751 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752}
2753
2754static stmt_ty
2755ast_for_for_stmt(struct compiling *c, const node *n)
2756{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002757 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 expr_ty expression;
2759 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002760 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2762 REQ(n, for_stmt);
2763
2764 if (NCH(n) == 9) {
2765 seq = ast_for_suite(c, CHILD(n, 8));
2766 if (!seq)
2767 return NULL;
2768 }
2769
Neal Norwitzedef2be2006-07-12 05:26:17 +00002770 node_target = CHILD(n, 1);
2771 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002772 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002774 /* Check the # of children rather than the length of _target, since
2775 for x, in ... has 1 element in _target, but still requires a Tuple. */
2776 if (NCH(node_target) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00002777 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002779 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002781 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002782 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 return NULL;
2784 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002785 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 return NULL;
2787
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002788 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2789 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790}
2791
2792static excepthandler_ty
2793ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2794{
2795 /* except_clause: 'except' [test [',' test]] */
2796 REQ(exc, except_clause);
2797 REQ(body, suite);
2798
2799 if (NCH(exc) == 1) {
2800 asdl_seq *suite_seq = ast_for_suite(c, body);
2801 if (!suite_seq)
2802 return NULL;
2803
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002804 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2805 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 }
2807 else if (NCH(exc) == 2) {
2808 expr_ty expression;
2809 asdl_seq *suite_seq;
2810
2811 expression = ast_for_expr(c, CHILD(exc, 1));
2812 if (!expression)
2813 return NULL;
2814 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002815 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 return NULL;
2817
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002818 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2819 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 }
2821 else if (NCH(exc) == 4) {
2822 asdl_seq *suite_seq;
2823 expr_ty expression;
2824 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2825 if (!e)
2826 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002827 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 return NULL;
2829 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002830 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 return NULL;
2832 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002833 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 return NULL;
2835
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002836 return excepthandler(expression, e, suite_seq, LINENO(exc),
2837 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002839
2840 PyErr_Format(PyExc_SystemError,
2841 "wrong number of children for 'except' clause: %d",
2842 NCH(exc));
2843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844}
2845
2846static stmt_ty
2847ast_for_try_stmt(struct compiling *c, const node *n)
2848{
Neal Norwitzf599f422005-12-17 21:33:47 +00002849 const int nch = NCH(n);
2850 int n_except = (nch - 3)/3;
2851 asdl_seq *body, *orelse = NULL, *finally = NULL;
2852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 REQ(n, try_stmt);
2854
Neal Norwitzf599f422005-12-17 21:33:47 +00002855 body = ast_for_suite(c, CHILD(n, 2));
2856 if (body == NULL)
2857 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858
Neal Norwitzf599f422005-12-17 21:33:47 +00002859 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2860 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2861 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2862 /* we can assume it's an "else",
2863 because nch >= 9 for try-else-finally and
2864 it would otherwise have a type of except_clause */
2865 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2866 if (orelse == NULL)
2867 return NULL;
2868 n_except--;
2869 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870
Neal Norwitzf599f422005-12-17 21:33:47 +00002871 finally = ast_for_suite(c, CHILD(n, nch - 1));
2872 if (finally == NULL)
2873 return NULL;
2874 n_except--;
2875 }
2876 else {
2877 /* we can assume it's an "else",
2878 otherwise it would have a type of except_clause */
2879 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2880 if (orelse == NULL)
2881 return NULL;
2882 n_except--;
2883 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002885 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002886 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 return NULL;
2888 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002889
2890 if (n_except > 0) {
2891 int i;
2892 stmt_ty except_st;
2893 /* process except statements to create a try ... except */
2894 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2895 if (handlers == NULL)
2896 return NULL;
2897
2898 for (i = 0; i < n_except; i++) {
2899 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2900 CHILD(n, 5 + i * 3));
2901 if (!e)
2902 return NULL;
2903 asdl_seq_SET(handlers, i, e);
2904 }
2905
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002906 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2907 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002908 if (!finally)
2909 return except_st;
2910
2911 /* if a 'finally' is present too, we nest the TryExcept within a
2912 TryFinally to emulate try ... except ... finally */
2913 body = asdl_seq_new(1, c->c_arena);
2914 if (body == NULL)
2915 return NULL;
2916 asdl_seq_SET(body, 0, except_st);
2917 }
2918
2919 /* must be a try ... finally (except clauses are in body, if any exist) */
2920 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002921 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922}
2923
Guido van Rossumc2e20742006-02-27 22:32:47 +00002924static expr_ty
2925ast_for_with_var(struct compiling *c, const node *n)
2926{
2927 REQ(n, with_var);
2928 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2929 ast_error(n, "expected \"with [expr] as [var]\"");
2930 return NULL;
2931 }
2932 return ast_for_expr(c, CHILD(n, 1));
2933}
2934
2935/* with_stmt: 'with' test [ with_var ] ':' suite */
2936static stmt_ty
2937ast_for_with_stmt(struct compiling *c, const node *n)
2938{
2939 expr_ty context_expr, optional_vars = NULL;
2940 int suite_index = 3; /* skip 'with', test, and ':' */
2941 asdl_seq *suite_seq;
2942
2943 assert(TYPE(n) == with_stmt);
2944 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter7d9ac782007-03-16 04:12:48 +00002945 if (!context_expr)
2946 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002947 if (TYPE(CHILD(n, 2)) == with_var) {
2948 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2949
2950 if (!optional_vars) {
2951 return NULL;
2952 }
2953 if (!set_context(optional_vars, Store, n)) {
2954 return NULL;
2955 }
2956 suite_index = 4;
2957 }
2958
2959 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2960 if (!suite_seq) {
2961 return NULL;
2962 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002963 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2964 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002965}
2966
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967static stmt_ty
2968ast_for_classdef(struct compiling *c, const node *n)
2969{
2970 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00002971 PyObject *classname;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972 asdl_seq *bases, *s;
2973
2974 REQ(n, classdef);
2975
2976 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2977 ast_error(n, "assignment to None");
2978 return NULL;
2979 }
2980
2981 if (NCH(n) == 4) {
2982 s = ast_for_suite(c, CHILD(n, 3));
2983 if (!s)
2984 return NULL;
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00002985 classname = NEW_IDENTIFIER(CHILD(n, 1));
2986 if (!classname)
2987 return NULL;
2988 return ClassDef(classname, NULL, s, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002989 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 }
2991 /* check for empty base list */
2992 if (TYPE(CHILD(n,3)) == RPAR) {
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00002993 s = ast_for_suite(c, CHILD(n, 5));
2994 if (!s)
2995 return NULL;
2996 classname = NEW_IDENTIFIER(CHILD(n, 1));
2997 if (!classname)
2998 return NULL;
2999 return ClassDef(classname, NULL, s, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003000 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001 }
3002
3003 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003004 bases = ast_for_class_bases(c, CHILD(n, 3));
3005 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007
3008 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003009 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 return NULL;
Benjamin Peterson1bd74de2008-11-25 22:49:28 +00003011 classname = NEW_IDENTIFIER(CHILD(n, 1));
3012 if (!classname)
3013 return NULL;
3014 return ClassDef(classname, bases, s, LINENO(n),
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003015 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016}
3017
3018static stmt_ty
3019ast_for_stmt(struct compiling *c, const node *n)
3020{
3021 if (TYPE(n) == stmt) {
3022 assert(NCH(n) == 1);
3023 n = CHILD(n, 0);
3024 }
3025 if (TYPE(n) == simple_stmt) {
3026 assert(num_stmts(n) == 1);
3027 n = CHILD(n, 0);
3028 }
3029 if (TYPE(n) == small_stmt) {
3030 REQ(n, small_stmt);
3031 n = CHILD(n, 0);
3032 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3033 | flow_stmt | import_stmt | global_stmt | exec_stmt
3034 | assert_stmt
3035 */
3036 switch (TYPE(n)) {
3037 case expr_stmt:
3038 return ast_for_expr_stmt(c, n);
3039 case print_stmt:
3040 return ast_for_print_stmt(c, n);
3041 case del_stmt:
3042 return ast_for_del_stmt(c, n);
3043 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003044 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 case flow_stmt:
3046 return ast_for_flow_stmt(c, n);
3047 case import_stmt:
3048 return ast_for_import_stmt(c, n);
3049 case global_stmt:
3050 return ast_for_global_stmt(c, n);
3051 case exec_stmt:
3052 return ast_for_exec_stmt(c, n);
3053 case assert_stmt:
3054 return ast_for_assert_stmt(c, n);
3055 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003056 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3058 TYPE(n), NCH(n));
3059 return NULL;
3060 }
3061 }
3062 else {
3063 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3064 | funcdef | classdef
3065 */
3066 node *ch = CHILD(n, 0);
3067 REQ(n, compound_stmt);
3068 switch (TYPE(ch)) {
3069 case if_stmt:
3070 return ast_for_if_stmt(c, ch);
3071 case while_stmt:
3072 return ast_for_while_stmt(c, ch);
3073 case for_stmt:
3074 return ast_for_for_stmt(c, ch);
3075 case try_stmt:
3076 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003077 case with_stmt:
3078 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 case funcdef:
3080 return ast_for_funcdef(c, ch);
3081 case classdef:
3082 return ast_for_classdef(c, ch);
3083 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003084 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3086 TYPE(n), NCH(n));
3087 return NULL;
3088 }
3089 }
3090}
3091
3092static PyObject *
3093parsenumber(const char *s)
3094{
3095 const char *end;
3096 long x;
3097 double dx;
3098#ifndef WITHOUT_COMPLEX
3099 Py_complex c;
3100 int imflag;
3101#endif
3102
3103 errno = 0;
3104 end = s + strlen(s) - 1;
3105#ifndef WITHOUT_COMPLEX
3106 imflag = *end == 'j' || *end == 'J';
3107#endif
3108 if (*end == 'l' || *end == 'L')
3109 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinsonab396e02008-07-16 11:04:17 +00003110 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 if (*end == '\0') {
3112 if (errno != 0)
3113 return PyLong_FromString((char *)s, (char **)0, 0);
3114 return PyInt_FromLong(x);
3115 }
3116 /* XXX Huge floats may silently fail */
3117#ifndef WITHOUT_COMPLEX
3118 if (imflag) {
3119 c.real = 0.;
3120 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003121 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 PyFPE_END_PROTECT(c)
3123 return PyComplex_FromCComplex(c);
3124 }
3125 else
3126#endif
3127 {
3128 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003129 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 PyFPE_END_PROTECT(dx)
3131 return PyFloat_FromDouble(dx);
3132 }
3133}
3134
3135static PyObject *
3136decode_utf8(const char **sPtr, const char *end, char* encoding)
3137{
3138#ifndef Py_USING_UNICODE
3139 Py_FatalError("decode_utf8 should not be called in this build.");
3140 return NULL;
3141#else
3142 PyObject *u, *v;
3143 char *s, *t;
3144 t = s = (char *)*sPtr;
3145 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3146 while (s < end && (*s & 0x80)) s++;
3147 *sPtr = s;
3148 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3149 if (u == NULL)
3150 return NULL;
3151 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3152 Py_DECREF(u);
3153 return v;
3154#endif
3155}
3156
Georg Brandleec47f32007-08-23 18:08:33 +00003157#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158static PyObject *
3159decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3160{
3161 PyObject *v, *u;
3162 char *buf;
3163 char *p;
3164 const char *end;
3165 if (encoding == NULL) {
3166 buf = (char *)s;
3167 u = NULL;
3168 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3169 buf = (char *)s;
3170 u = NULL;
3171 } else {
Martin v. Löwis73c01d42008-02-14 11:26:18 +00003172 /* check for integer overflow */
3173 if (len > PY_SIZE_MAX / 4)
3174 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3176 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3177 if (u == NULL)
3178 return NULL;
3179 p = buf = PyString_AsString(u);
3180 end = s + len;
3181 while (s < end) {
3182 if (*s == '\\') {
3183 *p++ = *s++;
3184 if (*s & 0x80) {
3185 strcpy(p, "u005c");
3186 p += 5;
3187 }
3188 }
3189 if (*s & 0x80) { /* XXX inefficient */
3190 PyObject *w;
3191 char *r;
Martin v. Löwis66851282006-04-22 11:40:03 +00003192 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 w = decode_utf8(&s, end, "utf-16-be");
3194 if (w == NULL) {
3195 Py_DECREF(u);
3196 return NULL;
3197 }
3198 r = PyString_AsString(w);
3199 rn = PyString_Size(w);
3200 assert(rn % 2 == 0);
3201 for (i = 0; i < rn; i += 2) {
3202 sprintf(p, "\\u%02x%02x",
3203 r[i + 0] & 0xFF,
3204 r[i + 1] & 0xFF);
3205 p += 6;
3206 }
3207 Py_DECREF(w);
3208 } else {
3209 *p++ = *s++;
3210 }
3211 }
3212 len = p - buf;
3213 s = buf;
3214 }
3215 if (rawmode)
3216 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3217 else
3218 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3219 Py_XDECREF(u);
3220 return v;
3221}
Georg Brandleec47f32007-08-23 18:08:33 +00003222#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223
3224/* s is a Python string literal, including the bracketing quote characters,
3225 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3226 * parsestr parses it, and returns the decoded Python string object.
3227 */
3228static PyObject *
3229parsestr(const char *s, const char *encoding)
3230{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003232 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 int rawmode = 0;
3234 int need_encoding;
3235 int unicode = 0;
3236
3237 if (isalpha(quote) || quote == '_') {
3238 if (quote == 'u' || quote == 'U') {
3239 quote = *++s;
3240 unicode = 1;
3241 }
3242 if (quote == 'r' || quote == 'R') {
3243 quote = *++s;
3244 rawmode = 1;
3245 }
3246 }
3247 if (quote != '\'' && quote != '\"') {
3248 PyErr_BadInternalCall();
3249 return NULL;
3250 }
3251 s++;
3252 len = strlen(s);
3253 if (len > INT_MAX) {
3254 PyErr_SetString(PyExc_OverflowError,
3255 "string to parse is too long");
3256 return NULL;
3257 }
3258 if (s[--len] != quote) {
3259 PyErr_BadInternalCall();
3260 return NULL;
3261 }
3262 if (len >= 4 && s[0] == quote && s[1] == quote) {
3263 s += 2;
3264 len -= 2;
3265 if (s[--len] != quote || s[--len] != quote) {
3266 PyErr_BadInternalCall();
3267 return NULL;
3268 }
3269 }
3270#ifdef Py_USING_UNICODE
3271 if (unicode || Py_UnicodeFlag) {
3272 return decode_unicode(s, len, rawmode, encoding);
3273 }
3274#endif
3275 need_encoding = (encoding != NULL &&
3276 strcmp(encoding, "utf-8") != 0 &&
3277 strcmp(encoding, "iso-8859-1") != 0);
3278 if (rawmode || strchr(s, '\\') == NULL) {
3279 if (need_encoding) {
3280#ifndef Py_USING_UNICODE
3281 /* This should not happen - we never see any other
3282 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003283 Py_FatalError(
3284 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003286 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 if (u == NULL)
3288 return NULL;
3289 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3290 Py_DECREF(u);
3291 return v;
3292#endif
3293 } else {
3294 return PyString_FromStringAndSize(s, len);
3295 }
3296 }
3297
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003298 return PyString_DecodeEscape(s, len, NULL, unicode,
3299 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300}
3301
3302/* Build a Python string object out of a STRING atom. This takes care of
3303 * compile-time literal catenation, calling parsestr() on each piece, and
3304 * pasting the intermediate results together.
3305 */
3306static PyObject *
3307parsestrplus(struct compiling *c, const node *n)
3308{
3309 PyObject *v;
3310 int i;
3311 REQ(CHILD(n, 0), STRING);
3312 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3313 /* String literal concatenation */
3314 for (i = 1; i < NCH(n); i++) {
3315 PyObject *s;
3316 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3317 if (s == NULL)
3318 goto onError;
3319 if (PyString_Check(v) && PyString_Check(s)) {
3320 PyString_ConcatAndDel(&v, s);
3321 if (v == NULL)
3322 goto onError;
3323 }
3324#ifdef Py_USING_UNICODE
3325 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003326 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328 Py_DECREF(v);
3329 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003330 if (v == NULL)
3331 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332 }
3333#endif
3334 }
3335 }
3336 return v;
3337
3338 onError:
3339 Py_XDECREF(v);
3340 return NULL;
3341}