blob: 2c03ad6acf58fd6e6ef790599b0045370254ec64 [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);
52 PyArena_AddPyObject(arena, id);
53 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057
58/* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
62
63 XXX Maybe we should just pass the filename...
64*/
65
66static int
67ast_error(const node *n, const char *errstr)
68{
69 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
70 if (!u)
71 return 0;
72 PyErr_SetObject(PyExc_SyntaxError, u);
73 Py_DECREF(u);
74 return 0;
75}
76
77static void
78ast_error_finish(const char *filename)
79{
80 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000081 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
85 return;
86
87 PyErr_Fetch(&type, &value, &tback);
88 errstr = PyTuple_GetItem(value, 0);
89 if (!errstr)
90 return;
91 Py_INCREF(errstr);
92 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000093 if (lineno == -1) {
94 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097 Py_DECREF(value);
98
99 loc = PyErr_ProgramText(filename, lineno);
100 if (!loc) {
101 Py_INCREF(Py_None);
102 loc = Py_None;
103 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000104 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000106 if (!tmp) {
107 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000109 }
Georg Brandl7784f122006-05-26 20:04:44 +0000110 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111 Py_DECREF(errstr);
112 Py_DECREF(tmp);
113 if (!value)
114 return;
115 PyErr_Restore(type, value, tback);
116}
117
118/* num_stmts() returns number of contained statements.
119
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
122 grammar:
123
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
126
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
130*/
131
132static int
133num_stmts(const node *n)
134{
135 int i, l;
136 node *ch;
137
138 switch (TYPE(n)) {
139 case single_input:
140 if (TYPE(CHILD(n, 0)) == NEWLINE)
141 return 0;
142 else
143 return num_stmts(CHILD(n, 0));
144 case file_input:
145 l = 0;
146 for (i = 0; i < NCH(n); i++) {
147 ch = CHILD(n, i);
148 if (TYPE(ch) == stmt)
149 l += num_stmts(ch);
150 }
151 return l;
152 case stmt:
153 return num_stmts(CHILD(n, 0));
154 case compound_stmt:
155 return 1;
156 case simple_stmt:
157 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
158 case suite:
159 if (NCH(n) == 1)
160 return num_stmts(CHILD(n, 0));
161 else {
162 l = 0;
163 for (i = 2; i < (NCH(n) - 1); i++)
164 l += num_stmts(CHILD(n, i));
165 return l;
166 }
167 default: {
168 char buf[128];
169
170 sprintf(buf, "Non-statement found: %d %d\n",
171 TYPE(n), NCH(n));
172 Py_FatalError(buf);
173 }
174 }
175 assert(0);
176 return 0;
177}
178
179/* Transform the CST rooted at node * to the appropriate AST
180*/
181
182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000183PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
184 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000186 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 asdl_seq *stmts = NULL;
188 stmt_ty s;
189 node *ch;
190 struct compiling c;
191
192 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000193 c.c_encoding = "utf-8";
Neal Norwitze98ccf62006-03-23 05:39:47 +0000194 if (TYPE(n) == encoding_decl) {
195 ast_error(n, "encoding declaration in Unicode string");
196 goto error;
197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 } else if (TYPE(n) == encoding_decl) {
199 c.c_encoding = STR(n);
200 n = CHILD(n, 0);
201 } else {
202 c.c_encoding = NULL;
203 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000204 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltona8293132006-02-28 17:58:27 +0000206 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 switch (TYPE(n)) {
208 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000209 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if (!stmts)
211 return NULL;
212 for (i = 0; i < NCH(n) - 1; i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == NEWLINE)
215 continue;
216 REQ(ch, stmt);
217 num = num_stmts(ch);
218 if (num == 1) {
219 s = ast_for_stmt(&c, ch);
220 if (!s)
221 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000222 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223 }
224 else {
225 ch = CHILD(ch, 0);
226 REQ(ch, simple_stmt);
227 for (j = 0; j < num; j++) {
228 s = ast_for_stmt(&c, CHILD(ch, j * 2));
229 if (!s)
230 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000231 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 }
233 }
234 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000235 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 case eval_input: {
237 expr_ty testlist_ast;
238
239 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000240 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 if (!testlist_ast)
242 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 }
245 case single_input:
246 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 if (!stmts)
249 goto error;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 }
254 else {
255 n = CHILD(n, 0);
256 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000257 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 if (!stmts)
259 goto error;
260 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000261 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 if (!s)
263 goto error;
264 asdl_seq_SET(stmts, 0, s);
265 }
266 else {
267 /* Only a simple_stmt can contain multiple statements. */
268 REQ(n, simple_stmt);
269 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (TYPE(CHILD(n, i)) == NEWLINE)
271 break;
272 s = ast_for_stmt(&c, CHILD(n, i));
273 if (!s)
274 goto error;
275 asdl_seq_SET(stmts, i / 2, s);
276 }
277 }
278
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000279 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 }
281 default:
282 goto error;
283 }
284 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 ast_error_finish(filename);
286 return NULL;
287}
288
289/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
290*/
291
292static operator_ty
293get_operator(const node *n)
294{
295 switch (TYPE(n)) {
296 case VBAR:
297 return BitOr;
298 case CIRCUMFLEX:
299 return BitXor;
300 case AMPER:
301 return BitAnd;
302 case LEFTSHIFT:
303 return LShift;
304 case RIGHTSHIFT:
305 return RShift;
306 case PLUS:
307 return Add;
308 case MINUS:
309 return Sub;
310 case STAR:
311 return Mult;
312 case SLASH:
313 return Div;
314 case DOUBLESLASH:
315 return FloorDiv;
316 case PERCENT:
317 return Mod;
318 default:
Martin v. Löwis28457502006-04-11 09:17:27 +0000319 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 }
321}
322
Jeremy Hyltona8293132006-02-28 17:58:27 +0000323/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
325 Only sets context for expr kinds that "can appear in assignment context"
326 (according to ../Parser/Python.asdl). For other expr kinds, it sets
327 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328*/
329
330static int
331set_context(expr_ty e, expr_context_ty ctx, const node *n)
332{
333 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334 /* If a particular expression type can't be used for assign / delete,
335 set expr_name to its name and an error message will be generated.
336 */
337 const char* expr_name = NULL;
338
339 /* The ast defines augmented store and load contexts, but the
340 implementation here doesn't actually use them. The code may be
341 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000342 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000343 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000344 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 */
346 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 switch (e->kind) {
349 case Attribute_kind:
350 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 return ast_error(n, "assignment to None");
353 }
354 e->v.Attribute.ctx = ctx;
355 break;
356 case Subscript_kind:
357 e->v.Subscript.ctx = ctx;
358 break;
359 case Name_kind:
360 if (ctx == Store &&
361 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
362 return ast_error(n, "assignment to None");
363 }
364 e->v.Name.ctx = ctx;
365 break;
366 case List_kind:
367 e->v.List.ctx = ctx;
368 s = e->v.List.elts;
369 break;
370 case Tuple_kind:
371 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
372 return ast_error(n, "can't assign to ()");
373 e->v.Tuple.ctx = ctx;
374 s = e->v.Tuple.elts;
375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case Lambda_kind:
377 expr_name = "lambda";
378 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 case UnaryOp_kind:
385 expr_name = "operator";
386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000388 expr_name = "generator expression";
389 break;
Neal Norwitz0d62a062006-07-30 06:53:31 +0000390 case Yield_kind:
391 expr_name = "yield expression";
392 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000393 case ListComp_kind:
394 expr_name = "list comprehension";
395 break;
396 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 case Num_kind:
398 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000399 expr_name = "literal";
400 break;
401 case Compare_kind:
402 expr_name = "comparison";
403 break;
404 case Repr_kind:
405 expr_name = "repr";
406 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000407 case IfExp_kind:
408 expr_name = "conditional expression";
409 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000410 default:
411 PyErr_Format(PyExc_SystemError,
412 "unexpected expression in assignment %d (line %d)",
413 e->kind, e->lineno);
414 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000416 /* Check for error string set by switch */
417 if (expr_name) {
418 char buf[300];
419 PyOS_snprintf(buf, sizeof(buf),
420 "can't %s %s",
421 ctx == Store ? "assign to" : "delete",
422 expr_name);
423 return ast_error(n, buf);
424 }
425
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000427 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 */
429 if (s) {
430 int i;
431
432 for (i = 0; i < asdl_seq_LEN(s); i++) {
Martin v. Löwis28457502006-04-11 09:17:27 +0000433 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 return 0;
435 }
436 }
437 return 1;
438}
439
440static operator_ty
441ast_for_augassign(const node *n)
442{
443 REQ(n, augassign);
444 n = CHILD(n, 0);
445 switch (STR(n)[0]) {
446 case '+':
447 return Add;
448 case '-':
449 return Sub;
450 case '/':
451 if (STR(n)[1] == '/')
452 return FloorDiv;
453 else
454 return Div;
455 case '%':
456 return Mod;
457 case '<':
458 return LShift;
459 case '>':
460 return RShift;
461 case '&':
462 return BitAnd;
463 case '^':
464 return BitXor;
465 case '|':
466 return BitOr;
467 case '*':
468 if (STR(n)[1] == '*')
469 return Pow;
470 else
471 return Mult;
472 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000473 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000474 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 }
476}
477
478static cmpop_ty
479ast_for_comp_op(const node *n)
480{
481 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
482 |'is' 'not'
483 */
484 REQ(n, comp_op);
485 if (NCH(n) == 1) {
486 n = CHILD(n, 0);
487 switch (TYPE(n)) {
488 case LESS:
489 return Lt;
490 case GREATER:
491 return Gt;
492 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 return Eq;
494 case LESSEQUAL:
495 return LtE;
496 case GREATEREQUAL:
497 return GtE;
498 case NOTEQUAL:
499 return NotEq;
500 case NAME:
501 if (strcmp(STR(n), "in") == 0)
502 return In;
503 if (strcmp(STR(n), "is") == 0)
504 return Is;
505 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000506 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000508 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 }
510 }
511 else if (NCH(n) == 2) {
512 /* handle "not in" and "is not" */
513 switch (TYPE(CHILD(n, 0))) {
514 case NAME:
515 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
516 return NotIn;
517 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
518 return IsNot;
519 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000520 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000522 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 }
524 }
Neal Norwitz79792652005-11-14 04:25:03 +0000525 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000527 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528}
529
530static asdl_seq *
531seq_for_testlist(struct compiling *c, const node *n)
532{
533 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000534 asdl_seq *seq;
535 expr_ty expression;
536 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 assert(TYPE(n) == testlist
538 || TYPE(n) == listmaker
539 || TYPE(n) == testlist_gexp
540 || TYPE(n) == testlist_safe
Neal Norwitza3ce6aa2006-11-04 19:32:54 +0000541 || TYPE(n) == testlist1
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000544 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 if (!seq)
546 return NULL;
547
548 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000549 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
551 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000552 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
555 assert(i / 2 < seq->size);
556 asdl_seq_SET(seq, i / 2, expression);
557 }
558 return seq;
559}
560
561static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000562compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563{
564 int i, len = (NCH(n) + 1) / 2;
565 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000566 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 if (!args)
568 return NULL;
569
Georg Brandlc57221e2006-09-25 07:04:10 +0000570 /* fpdef: NAME | '(' fplist ')'
571 fplist: fpdef (',' fpdef)* [',']
572 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 for (i = 0; i < len; i++) {
Georg Brandlc57221e2006-09-25 07:04:10 +0000575 const node *fpdef_node = CHILD(n, 2*i);
576 const node *child;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 expr_ty arg;
Georg Brandlc57221e2006-09-25 07:04:10 +0000578set_name:
579 /* fpdef_node is either a NAME or an fplist */
580 child = CHILD(fpdef_node, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000582 if (!strcmp(STR(child), "None")) {
583 ast_error(child, "assignment to None");
584 return NULL;
585 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000586 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
587 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000588 }
589 else {
Georg Brandlc57221e2006-09-25 07:04:10 +0000590 assert(TYPE(fpdef_node) == fpdef);
591 /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
592 child = CHILD(fpdef_node, 1);
593 assert(TYPE(child) == fplist);
594 /* NCH == 1 means we have (x), we need to elide the extra parens */
595 if (NCH(child) == 1) {
596 fpdef_node = CHILD(child, 0);
597 assert(TYPE(fpdef_node) == fpdef);
598 goto set_name;
599 }
600 arg = compiler_complex_args(c, child);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000601 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 asdl_seq_SET(args, i, arg);
603 }
604
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000605 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000606 if (!set_context(result, Store, n))
607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 return result;
609}
610
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Jeremy Hyltona8293132006-02-28 17:58:27 +0000612/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
614static arguments_ty
615ast_for_arguments(struct compiling *c, const node *n)
616{
617 /* parameters: '(' [varargslist] ')'
618 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
619 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
620 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000621 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 asdl_seq *args, *defaults;
623 identifier vararg = NULL, kwarg = NULL;
624 node *ch;
625
626 if (TYPE(n) == parameters) {
627 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000628 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629 n = CHILD(n, 1);
630 }
631 REQ(n, varargslist);
632
633 /* first count the number of normal args & defaults */
634 for (i = 0; i < NCH(n); i++) {
635 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000636 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 if (TYPE(ch) == EQUAL)
639 n_defaults++;
640 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000641 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 if (!args && n_args)
Neal Norwitzc173b482006-07-30 19:18:13 +0000643 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000644 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!defaults && n_defaults)
Neal Norwitzc173b482006-07-30 19:18:13 +0000646 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647
648 /* fpdef: NAME | '(' fplist ')'
649 fplist: fpdef (',' fpdef)* [',']
650 */
651 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000652 j = 0; /* index for defaults */
653 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 while (i < NCH(n)) {
655 ch = CHILD(n, i);
656 switch (TYPE(ch)) {
657 case fpdef:
Georg Brandlc57221e2006-09-25 07:04:10 +0000658 handle_fpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
660 anything other than EQUAL or a comma? */
661 /* XXX Should NCH(n) check be made a separate check? */
662 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000663 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
664 if (!expression)
665 goto error;
Neal Norwitzc173b482006-07-30 19:18:13 +0000666 assert(defaults != NULL);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000667 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 i += 2;
669 found_default = 1;
670 }
671 else if (found_default) {
672 ast_error(n,
673 "non-default argument follows default argument");
674 goto error;
675 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000677 ch = CHILD(ch, 1);
678 /* def foo((x)): is not complex, special case. */
679 if (NCH(ch) != 1) {
680 /* We have complex arguments, setup for unpacking. */
681 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
682 } else {
683 /* def foo((x)): setup for checking NAME below. */
Georg Brandlc57221e2006-09-25 07:04:10 +0000684 /* Loop because there can be many parens and tuple
685 upacking mixed in. */
Neal Norwitz33b730e2006-03-27 08:58:23 +0000686 ch = CHILD(ch, 0);
Georg Brandlc57221e2006-09-25 07:04:10 +0000687 assert(TYPE(ch) == fpdef);
688 goto handle_fpdef;
Neal Norwitz33b730e2006-03-27 08:58:23 +0000689 }
690 }
691 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000692 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
694 ast_error(CHILD(ch, 0), "assignment to None");
695 goto error;
696 }
Armin Rigo31441302005-10-21 12:57:31 +0000697 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000698 Param, LINENO(ch), ch->n_col_offset,
699 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 if (!name)
701 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000702 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
704 }
705 i += 2; /* the name and the comma */
706 break;
707 case STAR:
708 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
709 ast_error(CHILD(n, i+1), "assignment to None");
710 goto error;
711 }
712 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
713 i += 3;
714 break;
715 case DOUBLESTAR:
716 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
717 ast_error(CHILD(n, i+1), "assignment to None");
718 goto error;
719 }
720 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
721 i += 3;
722 break;
723 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000724 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 "unexpected node in varargslist: %d @ %d",
726 TYPE(ch), i);
727 goto error;
728 }
729 }
730
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000731 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732
733 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000734 Py_XDECREF(vararg);
735 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736 return NULL;
737}
738
739static expr_ty
740ast_for_dotted_name(struct compiling *c, const node *n)
741{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000742 expr_ty e;
743 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000744 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 int i;
746
747 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000748
749 lineno = LINENO(n);
750 col_offset = n->n_col_offset;
751
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 id = NEW_IDENTIFIER(CHILD(n, 0));
753 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000754 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000755 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000757 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
759 for (i = 2; i < NCH(n); i+=2) {
760 id = NEW_IDENTIFIER(CHILD(n, i));
761 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000762 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000763 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000764 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 }
767
768 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769}
770
771static expr_ty
772ast_for_decorator(struct compiling *c, const node *n)
773{
774 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
775 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000776 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
778 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000779 REQ(CHILD(n, 0), AT);
780 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
782 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
783 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
786 if (NCH(n) == 3) { /* No arguments */
787 d = name_expr;
788 name_expr = NULL;
789 }
790 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000791 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
792 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 name_expr = NULL;
796 }
797 else {
798 d = ast_for_call(c, CHILD(n, 3), name_expr);
799 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 name_expr = NULL;
802 }
803
804 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805}
806
807static asdl_seq*
808ast_for_decorators(struct compiling *c, const node *n)
809{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000810 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000811 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 int i;
813
814 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000815 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 if (!decorator_seq)
817 return NULL;
818
819 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000820 d = ast_for_decorator(c, CHILD(n, i));
821 if (!d)
822 return NULL;
823 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 }
825 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826}
827
828static stmt_ty
829ast_for_funcdef(struct compiling *c, const node *n)
830{
831 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000832 identifier name;
833 arguments_ty args;
834 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 asdl_seq *decorator_seq = NULL;
836 int name_i;
837
838 REQ(n, funcdef);
839
840 if (NCH(n) == 6) { /* decorators are present */
841 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
842 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 name_i = 2;
845 }
846 else {
847 name_i = 1;
848 }
849
850 name = NEW_IDENTIFIER(CHILD(n, name_i));
851 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000854 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 }
857 args = ast_for_arguments(c, CHILD(n, name_i + 1));
858 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 body = ast_for_suite(c, CHILD(n, name_i + 3));
861 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000864 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
865 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866}
867
868static expr_ty
869ast_for_lambdef(struct compiling *c, const node *n)
870{
871 /* lambdef: 'lambda' [varargslist] ':' test */
872 arguments_ty args;
873 expr_ty expression;
874
875 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000876 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 if (!args)
878 return NULL;
879 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000880 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 }
883 else {
884 args = ast_for_arguments(c, CHILD(n, 1));
885 if (!args)
886 return NULL;
887 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000888 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890 }
891
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000892 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893}
894
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000895static expr_ty
896ast_for_ifexpr(struct compiling *c, const node *n)
897{
898 /* test: or_test 'if' or_test 'else' test */
899 expr_ty expression, body, orelse;
900
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000901 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000902 body = ast_for_expr(c, CHILD(n, 0));
903 if (!body)
904 return NULL;
905 expression = ast_for_expr(c, CHILD(n, 2));
906 if (!expression)
907 return NULL;
908 orelse = ast_for_expr(c, CHILD(n, 4));
909 if (!orelse)
910 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000911 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
912 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000913}
914
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915/* Count the number of 'for' loop in a list comprehension.
916
917 Helper for ast_for_listcomp().
918*/
919
920static int
921count_list_fors(const node *n)
922{
923 int n_fors = 0;
924 node *ch = CHILD(n, 1);
925
926 count_list_for:
927 n_fors++;
928 REQ(ch, list_for);
929 if (NCH(ch) == 5)
930 ch = CHILD(ch, 4);
931 else
932 return n_fors;
933 count_list_iter:
934 REQ(ch, list_iter);
935 ch = CHILD(ch, 0);
936 if (TYPE(ch) == list_for)
937 goto count_list_for;
938 else if (TYPE(ch) == list_if) {
939 if (NCH(ch) == 3) {
940 ch = CHILD(ch, 2);
941 goto count_list_iter;
942 }
943 else
944 return n_fors;
945 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000946
947 /* Should never be reached */
948 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
949 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950}
951
952/* Count the number of 'if' statements in a list comprehension.
953
954 Helper for ast_for_listcomp().
955*/
956
957static int
958count_list_ifs(const node *n)
959{
960 int n_ifs = 0;
961
962 count_list_iter:
963 REQ(n, list_iter);
964 if (TYPE(CHILD(n, 0)) == list_for)
965 return n_ifs;
966 n = CHILD(n, 0);
967 REQ(n, list_if);
968 n_ifs++;
969 if (NCH(n) == 2)
970 return n_ifs;
971 n = CHILD(n, 2);
972 goto count_list_iter;
973}
974
975static expr_ty
976ast_for_listcomp(struct compiling *c, const node *n)
977{
978 /* listmaker: test ( list_for | (',' test)* [','] )
979 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
980 list_iter: list_for | list_if
981 list_if: 'if' test [list_iter]
982 testlist_safe: test [(',' test)+ [',']]
983 */
984 expr_ty elt;
985 asdl_seq *listcomps;
986 int i, n_fors;
987 node *ch;
988
989 REQ(n, listmaker);
990 assert(NCH(n) > 1);
991
992 elt = ast_for_expr(c, CHILD(n, 0));
993 if (!elt)
994 return NULL;
995
996 n_fors = count_list_fors(n);
997 if (n_fors == -1)
998 return NULL;
999
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001000 listcomps = asdl_seq_new(n_fors, c->c_arena);
1001 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001003
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 ch = CHILD(n, 1);
1005 for (i = 0; i < n_fors; i++) {
1006 comprehension_ty lc;
1007 asdl_seq *t;
1008 expr_ty expression;
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001009 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010
1011 REQ(ch, list_for);
1012
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001013 for_ch = CHILD(ch, 1);
1014 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001015 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001017 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001018 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001021 /* Check the # of children rather than the length of t, since
1022 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
1023 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001024 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001025 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001027 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1028 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001029 expression, NULL, c->c_arena);
1030 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032
1033 if (NCH(ch) == 5) {
1034 int j, n_ifs;
1035 asdl_seq *ifs;
Collin Winter7d9ac782007-03-16 04:12:48 +00001036 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037
1038 ch = CHILD(ch, 4);
1039 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001040 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001043 ifs = asdl_seq_new(n_ifs, c->c_arena);
1044 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046
1047 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001048 REQ(ch, list_iter);
1049 ch = CHILD(ch, 0);
1050 REQ(ch, list_if);
Collin Winter7d9ac782007-03-16 04:12:48 +00001051
1052 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1053 if (!list_for_expr)
1054 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055
Collin Winter7d9ac782007-03-16 04:12:48 +00001056 asdl_seq_SET(ifs, j, list_for_expr);
Jeremy Hyltona8293132006-02-28 17:58:27 +00001057 if (NCH(ch) == 3)
1058 ch = CHILD(ch, 2);
1059 }
1060 /* on exit, must guarantee that ch is a list_for */
1061 if (TYPE(ch) == list_iter)
1062 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001064 }
1065 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 }
1067
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001068 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069}
1070
1071/*
1072 Count the number of 'for' loops in a generator expression.
1073
1074 Helper for ast_for_genexp().
1075*/
1076
1077static int
1078count_gen_fors(const node *n)
1079{
1080 int n_fors = 0;
1081 node *ch = CHILD(n, 1);
1082
1083 count_gen_for:
1084 n_fors++;
1085 REQ(ch, gen_for);
1086 if (NCH(ch) == 5)
1087 ch = CHILD(ch, 4);
1088 else
1089 return n_fors;
1090 count_gen_iter:
1091 REQ(ch, gen_iter);
1092 ch = CHILD(ch, 0);
1093 if (TYPE(ch) == gen_for)
1094 goto count_gen_for;
1095 else if (TYPE(ch) == gen_if) {
1096 if (NCH(ch) == 3) {
1097 ch = CHILD(ch, 2);
1098 goto count_gen_iter;
1099 }
1100 else
1101 return n_fors;
1102 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001103
1104 /* Should never be reached */
1105 PyErr_SetString(PyExc_SystemError,
1106 "logic error in count_gen_fors");
1107 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108}
1109
1110/* Count the number of 'if' statements in a generator expression.
1111
1112 Helper for ast_for_genexp().
1113*/
1114
1115static int
1116count_gen_ifs(const node *n)
1117{
1118 int n_ifs = 0;
1119
1120 while (1) {
1121 REQ(n, gen_iter);
1122 if (TYPE(CHILD(n, 0)) == gen_for)
1123 return n_ifs;
1124 n = CHILD(n, 0);
1125 REQ(n, gen_if);
1126 n_ifs++;
1127 if (NCH(n) == 2)
1128 return n_ifs;
1129 n = CHILD(n, 2);
1130 }
1131}
1132
Jeremy Hyltona8293132006-02-28 17:58:27 +00001133/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134static expr_ty
1135ast_for_genexp(struct compiling *c, const node *n)
1136{
1137 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1138 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1139 expr_ty elt;
1140 asdl_seq *genexps;
1141 int i, n_fors;
1142 node *ch;
1143
1144 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1145 assert(NCH(n) > 1);
1146
1147 elt = ast_for_expr(c, CHILD(n, 0));
1148 if (!elt)
1149 return NULL;
1150
1151 n_fors = count_gen_fors(n);
1152 if (n_fors == -1)
1153 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001154
1155 genexps = asdl_seq_new(n_fors, c->c_arena);
1156 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 ch = CHILD(n, 1);
1160 for (i = 0; i < n_fors; i++) {
1161 comprehension_ty ge;
1162 asdl_seq *t;
1163 expr_ty expression;
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001164 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165
1166 REQ(ch, gen_for);
1167
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001168 for_ch = CHILD(ch, 1);
1169 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001170 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001172 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001173 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001175
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001176 /* Check the # of children rather than the length of t, since
1177 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1178 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001179 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001180 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001182 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1183 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001184 expression, NULL, c->c_arena);
1185
1186 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 if (NCH(ch) == 5) {
1190 int j, n_ifs;
1191 asdl_seq *ifs;
1192
1193 ch = CHILD(ch, 4);
1194 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001195 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001197
1198 ifs = asdl_seq_new(n_ifs, c->c_arena);
1199 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001201
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 for (j = 0; j < n_ifs; j++) {
1203 REQ(ch, gen_iter);
1204 ch = CHILD(ch, 0);
1205 REQ(ch, gen_if);
1206
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001207 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001208 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001209 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001210 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 if (NCH(ch) == 3)
1212 ch = CHILD(ch, 2);
1213 }
1214 /* on exit, must guarantee that ch is a gen_for */
1215 if (TYPE(ch) == gen_iter)
1216 ch = CHILD(ch, 0);
1217 ge->ifs = ifs;
1218 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001219 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 }
1221
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001222 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223}
1224
1225static expr_ty
1226ast_for_atom(struct compiling *c, const node *n)
1227{
1228 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1229 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1230 */
1231 node *ch = CHILD(n, 0);
1232
1233 switch (TYPE(ch)) {
1234 case NAME:
1235 /* All names start in Load context, but may later be
1236 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001237 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 case STRING: {
1239 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 if (!str)
1241 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242
1243 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001244 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 }
1246 case NUMBER: {
1247 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 if (!pynum)
1249 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001250
1251 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001252 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 }
1254 case LPAR: /* some parenthesized expressions */
1255 ch = CHILD(n, 1);
1256
1257 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001258 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259
1260 if (TYPE(ch) == yield_expr)
1261 return ast_for_expr(c, ch);
1262
1263 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1264 return ast_for_genexp(c, ch);
1265
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001266 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 case LSQB: /* list (or list comprehension) */
1268 ch = CHILD(n, 1);
1269
1270 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001271 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272
1273 REQ(ch, listmaker);
1274 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1275 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 if (!elts)
1277 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001278
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001279 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 }
1281 else
1282 return ast_for_listcomp(c, ch);
1283 case LBRACE: {
1284 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1285 int i, size;
1286 asdl_seq *keys, *values;
1287
1288 ch = CHILD(n, 1);
1289 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001290 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 if (!keys)
1292 return NULL;
1293
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001294 values = asdl_seq_new(size, c->c_arena);
1295 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297
1298 for (i = 0; i < NCH(ch); i += 4) {
1299 expr_ty expression;
1300
1301 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001302 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001304
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001306
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001308 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001310
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 asdl_seq_SET(values, i / 4, expression);
1312 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001313 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 }
1315 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001316 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 if (!expression)
1318 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001319
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001320 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 }
1322 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001323 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 return NULL;
1325 }
1326}
1327
1328static slice_ty
1329ast_for_slice(struct compiling *c, const node *n)
1330{
1331 node *ch;
1332 expr_ty lower = NULL, upper = NULL, step = NULL;
1333
1334 REQ(n, subscript);
1335
1336 /*
1337 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1338 sliceop: ':' [test]
1339 */
1340 ch = CHILD(n, 0);
1341 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001342 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343
1344 if (NCH(n) == 1 && TYPE(ch) == test) {
1345 /* 'step' variable hold no significance in terms of being used over
1346 other vars */
1347 step = ast_for_expr(c, ch);
1348 if (!step)
1349 return NULL;
1350
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001351 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 }
1353
1354 if (TYPE(ch) == test) {
1355 lower = ast_for_expr(c, ch);
1356 if (!lower)
1357 return NULL;
1358 }
1359
1360 /* If there's an upper bound it's in the second or third position. */
1361 if (TYPE(ch) == COLON) {
1362 if (NCH(n) > 1) {
1363 node *n2 = CHILD(n, 1);
1364
1365 if (TYPE(n2) == test) {
1366 upper = ast_for_expr(c, n2);
1367 if (!upper)
1368 return NULL;
1369 }
1370 }
1371 } else if (NCH(n) > 2) {
1372 node *n2 = CHILD(n, 2);
1373
1374 if (TYPE(n2) == test) {
1375 upper = ast_for_expr(c, n2);
1376 if (!upper)
1377 return NULL;
1378 }
1379 }
1380
1381 ch = CHILD(n, NCH(n) - 1);
1382 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001383 if (NCH(ch) == 1) {
1384 /* No expression, so step is None */
1385 ch = CHILD(ch, 0);
1386 step = Name(new_identifier("None", c->c_arena), Load,
1387 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 if (!step)
1389 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001390 } else {
1391 ch = CHILD(ch, 1);
1392 if (TYPE(ch) == test) {
1393 step = ast_for_expr(c, ch);
1394 if (!step)
1395 return NULL;
1396 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 }
1398 }
1399
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001400 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401}
1402
1403static expr_ty
1404ast_for_binop(struct compiling *c, const node *n)
1405{
1406 /* Must account for a sequence of expressions.
1407 How should A op B op C by represented?
1408 BinOp(BinOp(A, op, B), op, C).
1409 */
1410
1411 int i, nops;
1412 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001413 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414
1415 expr1 = ast_for_expr(c, CHILD(n, 0));
1416 if (!expr1)
1417 return NULL;
1418
1419 expr2 = ast_for_expr(c, CHILD(n, 2));
1420 if (!expr2)
1421 return NULL;
1422
Anthony Baxtera863d332006-04-11 07:43:46 +00001423 newoperator = get_operator(CHILD(n, 1));
1424 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 return NULL;
1426
Anthony Baxtera863d332006-04-11 07:43:46 +00001427 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001428 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 if (!result)
1430 return NULL;
1431
1432 nops = (NCH(n) - 1) / 2;
1433 for (i = 1; i < nops; i++) {
1434 expr_ty tmp_result, tmp;
1435 const node* next_oper = CHILD(n, i * 2 + 1);
1436
Anthony Baxtera863d332006-04-11 07:43:46 +00001437 newoperator = get_operator(next_oper);
1438 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 return NULL;
1440
1441 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1442 if (!tmp)
1443 return NULL;
1444
Anthony Baxtera863d332006-04-11 07:43:46 +00001445 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001446 LINENO(next_oper), next_oper->n_col_offset,
1447 c->c_arena);
Neal Norwitz14f848b2007-10-05 03:45:42 +00001448 if (!tmp_result)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 return NULL;
1450 result = tmp_result;
1451 }
1452 return result;
1453}
1454
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001455static expr_ty
1456ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1457{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001458 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1459 subscriptlist: subscript (',' subscript)* [',']
1460 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1461 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001462 REQ(n, trailer);
1463 if (TYPE(CHILD(n, 0)) == LPAR) {
1464 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001465 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1466 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001467 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001468 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001469 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001470 else if (TYPE(CHILD(n, 0)) == DOT ) {
1471 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001472 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001473 }
1474 else {
1475 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001476 REQ(CHILD(n, 2), RSQB);
1477 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001478 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001479 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1480 if (!slc)
1481 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001482 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1483 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001484 }
1485 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001486 /* The grammar is ambiguous here. The ambiguity is resolved
1487 by treating the sequence as a tuple literal if there are
1488 no slice features.
1489 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001490 int j;
1491 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001492 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001493 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001494 asdl_seq *slices, *elts;
1495 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001496 if (!slices)
1497 return NULL;
1498 for (j = 0; j < NCH(n); j += 2) {
1499 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001500 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001501 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001502 if (slc->kind != Index_kind)
1503 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001504 asdl_seq_SET(slices, j / 2, slc);
1505 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001506 if (!simple) {
1507 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001508 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001509 }
1510 /* extract Index values and put them in a Tuple */
1511 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001512 if (!elts)
1513 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001514 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1515 slc = (slice_ty)asdl_seq_GET(slices, j);
1516 assert(slc->kind == Index_kind && slc->v.Index.value);
1517 asdl_seq_SET(elts, j, slc->v.Index.value);
1518 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001519 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001520 if (!e)
1521 return NULL;
1522 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001523 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001524 }
1525 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001526}
1527
1528static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001529ast_for_factor(struct compiling *c, const node *n)
1530{
1531 node *pfactor, *ppower, *patom, *pnum;
1532 expr_ty expression;
1533
1534 /* If the unary - operator is applied to a constant, don't generate
1535 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1536 constant. The peephole optimizer already does something like
1537 this but it doesn't handle the case where the constant is
1538 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1539 PyLongObject.
1540 */
1541 if (TYPE(CHILD(n, 0)) == MINUS
1542 && NCH(n) == 2
1543 && TYPE((pfactor = CHILD(n, 1))) == factor
1544 && NCH(pfactor) == 1
1545 && TYPE((ppower = CHILD(pfactor, 0))) == power
1546 && NCH(ppower) == 1
1547 && TYPE((patom = CHILD(ppower, 0))) == atom
1548 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1549 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1550 if (s == NULL)
1551 return NULL;
1552 s[0] = '-';
1553 strcpy(s + 1, STR(pnum));
1554 PyObject_FREE(STR(pnum));
1555 STR(pnum) = s;
1556 return ast_for_atom(c, patom);
1557 }
1558
1559 expression = ast_for_expr(c, CHILD(n, 1));
1560 if (!expression)
1561 return NULL;
1562
1563 switch (TYPE(CHILD(n, 0))) {
1564 case PLUS:
1565 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1566 c->c_arena);
1567 case MINUS:
1568 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1569 c->c_arena);
1570 case TILDE:
1571 return UnaryOp(Invert, expression, LINENO(n),
1572 n->n_col_offset, c->c_arena);
1573 }
1574 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1575 TYPE(CHILD(n, 0)));
1576 return NULL;
1577}
1578
1579static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001580ast_for_power(struct compiling *c, const node *n)
1581{
1582 /* power: atom trailer* ('**' factor)*
1583 */
1584 int i;
1585 expr_ty e, tmp;
1586 REQ(n, power);
1587 e = ast_for_atom(c, CHILD(n, 0));
1588 if (!e)
1589 return NULL;
1590 if (NCH(n) == 1)
1591 return e;
1592 for (i = 1; i < NCH(n); i++) {
1593 node *ch = CHILD(n, i);
1594 if (TYPE(ch) != trailer)
1595 break;
1596 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001597 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001598 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001599 tmp->lineno = e->lineno;
1600 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001601 e = tmp;
1602 }
1603 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1604 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001605 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001606 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001607 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001608 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001609 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001610 e = tmp;
1611 }
1612 return e;
1613}
1614
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615/* Do not name a variable 'expr'! Will cause a compile error.
1616*/
1617
1618static expr_ty
1619ast_for_expr(struct compiling *c, const node *n)
1620{
1621 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001622 test: or_test ['if' or_test 'else' test] | lambdef
1623 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624 and_test: not_test ('and' not_test)*
1625 not_test: 'not' not_test | comparison
1626 comparison: expr (comp_op expr)*
1627 expr: xor_expr ('|' xor_expr)*
1628 xor_expr: and_expr ('^' and_expr)*
1629 and_expr: shift_expr ('&' shift_expr)*
1630 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1631 arith_expr: term (('+'|'-') term)*
1632 term: factor (('*'|'/'|'%'|'//') factor)*
1633 factor: ('+'|'-'|'~') factor | power
1634 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001635
1636 As well as modified versions that exist for backward compatibility,
1637 to explicitly allow:
1638 [ x for x in lambda: 0, lambda: 1 ]
1639 (which would be ambiguous without these extra rules)
1640
1641 old_test: or_test | old_lambdef
1642 old_lambdef: 'lambda' [vararglist] ':' old_test
1643
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644 */
1645
1646 asdl_seq *seq;
1647 int i;
1648
1649 loop:
1650 switch (TYPE(n)) {
1651 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001652 case old_test:
1653 if (TYPE(CHILD(n, 0)) == lambdef ||
1654 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001656 else if (NCH(n) > 1)
1657 return ast_for_ifexpr(c, n);
1658 /* Fallthrough */
1659 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 case and_test:
1661 if (NCH(n) == 1) {
1662 n = CHILD(n, 0);
1663 goto loop;
1664 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001665 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666 if (!seq)
1667 return NULL;
1668 for (i = 0; i < NCH(n); i += 2) {
1669 expr_ty e = ast_for_expr(c, CHILD(n, i));
1670 if (!e)
1671 return NULL;
1672 asdl_seq_SET(seq, i / 2, e);
1673 }
1674 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001675 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1676 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001677 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001678 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 case not_test:
1680 if (NCH(n) == 1) {
1681 n = CHILD(n, 0);
1682 goto loop;
1683 }
1684 else {
1685 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1686 if (!expression)
1687 return NULL;
1688
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001689 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1690 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 }
1692 case comparison:
1693 if (NCH(n) == 1) {
1694 n = CHILD(n, 0);
1695 goto loop;
1696 }
1697 else {
1698 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001699 asdl_int_seq *ops;
1700 asdl_seq *cmps;
1701 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 if (!ops)
1703 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001704 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 return NULL;
1707 }
1708 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001709 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710
Anthony Baxtera863d332006-04-11 07:43:46 +00001711 newoperator = ast_for_comp_op(CHILD(n, i));
1712 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001714 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715
1716 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001717 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001719 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001721 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 asdl_seq_SET(cmps, i / 2, expression);
1723 }
1724 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001725 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001727 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001729 return Compare(expression, ops, cmps, LINENO(n),
1730 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 }
1732 break;
1733
1734 /* The next five cases all handle BinOps. The main body of code
1735 is the same in each case, but the switch turned inside out to
1736 reuse the code for each type of operator.
1737 */
1738 case expr:
1739 case xor_expr:
1740 case and_expr:
1741 case shift_expr:
1742 case arith_expr:
1743 case term:
1744 if (NCH(n) == 1) {
1745 n = CHILD(n, 0);
1746 goto loop;
1747 }
1748 return ast_for_binop(c, n);
1749 case yield_expr: {
1750 expr_ty exp = NULL;
1751 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001752 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753 if (!exp)
1754 return NULL;
1755 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001756 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001758 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 if (NCH(n) == 1) {
1760 n = CHILD(n, 0);
1761 goto loop;
1762 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001763 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001764 case power:
1765 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001767 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 return NULL;
1769 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001770 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 return NULL;
1772}
1773
1774static expr_ty
1775ast_for_call(struct compiling *c, const node *n, expr_ty func)
1776{
1777 /*
1778 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1779 | '**' test)
1780 argument: [test '='] test [gen_for] # Really [keyword '='] test
1781 */
1782
1783 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001784 asdl_seq *args;
1785 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786 expr_ty vararg = NULL, kwarg = NULL;
1787
1788 REQ(n, arglist);
1789
1790 nargs = 0;
1791 nkeywords = 0;
1792 ngens = 0;
1793 for (i = 0; i < NCH(n); i++) {
1794 node *ch = CHILD(n, i);
1795 if (TYPE(ch) == argument) {
1796 if (NCH(ch) == 1)
1797 nargs++;
1798 else if (TYPE(CHILD(ch, 1)) == gen_for)
1799 ngens++;
1800 else
1801 nkeywords++;
1802 }
1803 }
1804 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001805 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 "if not sole argument");
1807 return NULL;
1808 }
1809
1810 if (nargs + nkeywords + ngens > 255) {
1811 ast_error(n, "more than 255 arguments");
1812 return NULL;
1813 }
1814
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001815 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817 return NULL;
1818 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 nargs = 0;
1822 nkeywords = 0;
1823 for (i = 0; i < NCH(n); i++) {
1824 node *ch = CHILD(n, i);
1825 if (TYPE(ch) == argument) {
1826 expr_ty e;
1827 if (NCH(ch) == 1) {
Neal Norwitz5ef92242006-05-19 06:43:50 +00001828 if (nkeywords) {
1829 ast_error(CHILD(ch, 0),
1830 "non-keyword arg after keyword arg");
1831 return NULL;
1832 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 e = ast_for_expr(c, CHILD(ch, 0));
1834 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 asdl_seq_SET(args, nargs++, e);
1837 }
1838 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1839 e = ast_for_genexp(c, ch);
1840 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001841 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 asdl_seq_SET(args, nargs++, e);
1843 }
1844 else {
1845 keyword_ty kw;
1846 identifier key;
1847
1848 /* CHILD(ch, 0) is test, but must be an identifier? */
1849 e = ast_for_expr(c, CHILD(ch, 0));
1850 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 /* f(lambda x: x[0] = 3) ends up getting parsed with
1853 * LHS test = lambda x: x[0], and RHS test = 3.
1854 * SF bug 132313 points out that complaining about a keyword
1855 * then is very confusing.
1856 */
1857 if (e->kind == Lambda_kind) {
Georg Brandl73c958a2007-06-07 13:23:28 +00001858 ast_error(CHILD(ch, 0),
1859 "lambda cannot contain assignment");
1860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 } else if (e->kind != Name_kind) {
Georg Brandl73c958a2007-06-07 13:23:28 +00001862 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1863 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 }
1865 key = e->v.Name.id;
Georg Brandl73c958a2007-06-07 13:23:28 +00001866 if (!strcmp(PyString_AS_STRING(key), "None")) {
1867 ast_error(CHILD(ch, 0), "assignment to None");
1868 return NULL;
1869 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 e = ast_for_expr(c, CHILD(ch, 2));
1871 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872 return NULL;
1873 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001875 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 asdl_seq_SET(keywords, nkeywords++, kw);
1877 }
1878 }
1879 else if (TYPE(ch) == STAR) {
1880 vararg = ast_for_expr(c, CHILD(n, i+1));
1881 i++;
1882 }
1883 else if (TYPE(ch) == DOUBLESTAR) {
1884 kwarg = ast_for_expr(c, CHILD(n, i+1));
1885 i++;
1886 }
1887 }
1888
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001889 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890}
1891
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001893ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001895 /* testlist_gexp: test (',' test)* [','] */
1896 /* testlist: test (',' test)* [','] */
1897 /* testlist_safe: test (',' test)+ [','] */
1898 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001900 if (TYPE(n) == testlist_gexp) {
1901 if (NCH(n) > 1)
1902 assert(TYPE(CHILD(n, 1)) != gen_for);
1903 }
1904 else {
1905 assert(TYPE(n) == testlist ||
1906 TYPE(n) == testlist_safe ||
1907 TYPE(n) == testlist1);
1908 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 if (NCH(n) == 1)
1910 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 else {
1912 asdl_seq *tmp = seq_for_testlist(c, n);
1913 if (!tmp)
1914 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001915 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001917}
1918
1919static expr_ty
1920ast_for_testlist_gexp(struct compiling *c, const node* n)
1921{
1922 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1923 /* argument: test [ gen_for ] */
1924 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001925 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001926 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001927 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001928}
1929
1930/* like ast_for_testlist() but returns a sequence */
1931static asdl_seq*
1932ast_for_class_bases(struct compiling *c, const node* n)
1933{
1934 /* testlist: test (',' test)* [','] */
1935 assert(NCH(n) > 0);
1936 REQ(n, testlist);
1937 if (NCH(n) == 1) {
1938 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001939 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001940 if (!bases)
1941 return NULL;
1942 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001943 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001944 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001945 asdl_seq_SET(bases, 0, base);
1946 return bases;
1947 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001948
1949 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950}
1951
1952static stmt_ty
1953ast_for_expr_stmt(struct compiling *c, const node *n)
1954{
1955 REQ(n, expr_stmt);
1956 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1957 | ('=' (yield_expr|testlist))*)
1958 testlist: test (',' test)* [',']
1959 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1960 | '<<=' | '>>=' | '**=' | '//='
1961 test: ... here starts the operator precendence dance
1962 */
1963
1964 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001965 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 if (!e)
1967 return NULL;
1968
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001969 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 }
1971 else if (TYPE(CHILD(n, 1)) == augassign) {
1972 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001973 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 node *ch = CHILD(n, 0);
1975
Neal Norwitz0d62a062006-07-30 06:53:31 +00001976 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 if (!expr1)
1978 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001979 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001980 switch (expr1->kind) {
1981 case GeneratorExp_kind:
1982 ast_error(ch, "augmented assignment to generator "
1983 "expression not possible");
1984 return NULL;
Neal Norwitz0d62a062006-07-30 06:53:31 +00001985 case Yield_kind:
1986 ast_error(ch, "augmented assignment to yield "
1987 "expression not possible");
1988 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001989 case Name_kind: {
1990 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1991 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1992 ast_error(ch, "assignment to None");
1993 return NULL;
1994 }
1995 break;
1996 }
1997 case Attribute_kind:
1998 case Subscript_kind:
1999 break;
2000 default:
2001 ast_error(ch, "illegal expression for augmented "
2002 "assignment");
2003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 }
Collin Winter7d9ac782007-03-16 04:12:48 +00002005 if (!set_context(expr1, Store, ch))
2006 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007
2008 ch = CHILD(n, 2);
2009 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002010 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 else
Neal Norwitz0d62a062006-07-30 06:53:31 +00002012 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002013 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 return NULL;
2015
Anthony Baxtera863d332006-04-11 07:43:46 +00002016 newoperator = ast_for_augassign(CHILD(n, 1));
2017 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 return NULL;
2019
Anthony Baxtera863d332006-04-11 07:43:46 +00002020 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 }
2022 else {
2023 int i;
2024 asdl_seq *targets;
2025 node *value;
2026 expr_ty expression;
2027
2028 /* a normal assignment */
2029 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002030 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 if (!targets)
2032 return NULL;
2033 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002034 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 node *ch = CHILD(n, i);
2036 if (TYPE(ch) == yield_expr) {
2037 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002040 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041
2042 /* set context to assign */
2043 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045
Neal Norwitz84456bd2005-12-18 03:16:20 +00002046 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048
2049 asdl_seq_SET(targets, i / 2, e);
2050 }
2051 value = CHILD(n, NCH(n) - 1);
2052 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002053 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 else
2055 expression = ast_for_expr(c, value);
2056 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002057 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002058 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060}
2061
2062static stmt_ty
2063ast_for_print_stmt(struct compiling *c, const node *n)
2064{
2065 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2066 | '>>' test [ (',' test)+ [','] ] )
2067 */
2068 expr_ty dest = NULL, expression;
2069 asdl_seq *seq;
2070 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002071 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072
2073 REQ(n, print_stmt);
2074 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2075 dest = ast_for_expr(c, CHILD(n, 2));
2076 if (!dest)
2077 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002078 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002080 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002082 return NULL;
2083 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002085 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002087 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 }
2089 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002090 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091}
2092
2093static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002094ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095{
2096 asdl_seq *seq;
2097 int i;
2098 expr_ty e;
2099
2100 REQ(n, exprlist);
2101
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002102 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 if (!seq)
2104 return NULL;
2105 for (i = 0; i < NCH(n); i += 2) {
2106 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002107 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002108 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002109 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002110 if (context && !set_context(e, context, CHILD(n, i)))
2111 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 }
2113 return seq;
2114}
2115
2116static stmt_ty
2117ast_for_del_stmt(struct compiling *c, const node *n)
2118{
2119 asdl_seq *expr_list;
2120
2121 /* del_stmt: 'del' exprlist */
2122 REQ(n, del_stmt);
2123
2124 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2125 if (!expr_list)
2126 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002127 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128}
2129
2130static stmt_ty
2131ast_for_flow_stmt(struct compiling *c, const node *n)
2132{
2133 /*
2134 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2135 | yield_stmt
2136 break_stmt: 'break'
2137 continue_stmt: 'continue'
2138 return_stmt: 'return' [testlist]
2139 yield_stmt: yield_expr
2140 yield_expr: 'yield' testlist
2141 raise_stmt: 'raise' [test [',' test [',' test]]]
2142 */
2143 node *ch;
2144
2145 REQ(n, flow_stmt);
2146 ch = CHILD(n, 0);
2147 switch (TYPE(ch)) {
2148 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002149 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002151 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 case yield_stmt: { /* will reduce to yield_expr */
2153 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2154 if (!exp)
2155 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002156 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 }
2158 case return_stmt:
2159 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002160 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002162 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 if (!expression)
2164 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002165 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 }
2167 case raise_stmt:
2168 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002169 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 else if (NCH(ch) == 2) {
2171 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2172 if (!expression)
2173 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002174 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 }
2176 else if (NCH(ch) == 4) {
2177 expr_ty expr1, expr2;
2178
2179 expr1 = ast_for_expr(c, CHILD(ch, 1));
2180 if (!expr1)
2181 return NULL;
2182 expr2 = ast_for_expr(c, CHILD(ch, 3));
2183 if (!expr2)
2184 return NULL;
2185
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002186 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 }
2188 else if (NCH(ch) == 6) {
2189 expr_ty expr1, expr2, expr3;
2190
2191 expr1 = ast_for_expr(c, CHILD(ch, 1));
2192 if (!expr1)
2193 return NULL;
2194 expr2 = ast_for_expr(c, CHILD(ch, 3));
2195 if (!expr2)
2196 return NULL;
2197 expr3 = ast_for_expr(c, CHILD(ch, 5));
2198 if (!expr3)
2199 return NULL;
2200
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002201 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 }
2203 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002204 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205 "unexpected flow_stmt: %d", TYPE(ch));
2206 return NULL;
2207 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002208
2209 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211}
2212
2213static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002214alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215{
2216 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002217 import_as_name: NAME ['as' NAME]
2218 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 dotted_name: NAME ('.' NAME)*
2220 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002221 PyObject *str;
2222
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 loop:
2224 switch (TYPE(n)) {
2225 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002226 str = NULL;
2227 if (NCH(n) == 3) {
2228 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2229 ast_error(n, "must use 'as' in import");
2230 return NULL;
2231 }
2232 str = NEW_IDENTIFIER(CHILD(n, 2));
2233 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002234 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 case dotted_as_name:
2236 if (NCH(n) == 1) {
2237 n = CHILD(n, 0);
2238 goto loop;
2239 }
2240 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002241 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00002242 if (!a)
2243 return NULL;
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002244 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2245 ast_error(n, "must use 'as' in import");
2246 return NULL;
2247 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 assert(!a->asname);
2249 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2250 return a;
2251 }
2252 break;
2253 case dotted_name:
2254 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002255 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 else {
2257 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002258 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002259 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 char *s;
2261
2262 len = 0;
2263 for (i = 0; i < NCH(n); i += 2)
2264 /* length of string plus one for the dot */
2265 len += strlen(STR(CHILD(n, i))) + 1;
2266 len--; /* the last name doesn't have a dot */
2267 str = PyString_FromStringAndSize(NULL, len);
2268 if (!str)
2269 return NULL;
2270 s = PyString_AS_STRING(str);
2271 if (!s)
2272 return NULL;
2273 for (i = 0; i < NCH(n); i += 2) {
2274 char *sch = STR(CHILD(n, i));
2275 strcpy(s, STR(CHILD(n, i)));
2276 s += strlen(sch);
2277 *s++ = '.';
2278 }
2279 --s;
2280 *s = '\0';
2281 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002282 PyArena_AddPyObject(c->c_arena, str);
2283 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 }
2285 break;
2286 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002287 str = PyString_InternFromString("*");
2288 PyArena_AddPyObject(c->c_arena, str);
2289 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002291 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 "unexpected import name: %d", TYPE(n));
2293 return NULL;
2294 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002295
2296 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 return NULL;
2298}
2299
2300static stmt_ty
2301ast_for_import_stmt(struct compiling *c, const node *n)
2302{
2303 /*
2304 import_stmt: import_name | import_from
2305 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002306 import_from: 'from' ('.'* dotted_name | '.') 'import'
2307 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002309 int lineno;
2310 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 int i;
2312 asdl_seq *aliases;
2313
2314 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002315 lineno = LINENO(n);
2316 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002318 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002320 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002321 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 if (!aliases)
2323 return NULL;
2324 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002325 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002326 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 asdl_seq_SET(aliases, i / 2, import_alias);
2329 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002330 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002332 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002334 int idx, ndots = 0;
2335 alias_ty mod = NULL;
2336 identifier modname;
2337
2338 /* Count the number of dots (for relative imports) and check for the
2339 optional module name */
2340 for (idx = 1; idx < NCH(n); idx++) {
2341 if (TYPE(CHILD(n, idx)) == dotted_name) {
2342 mod = alias_for_import_name(c, CHILD(n, idx));
2343 idx++;
2344 break;
2345 } else if (TYPE(CHILD(n, idx)) != DOT) {
2346 break;
2347 }
2348 ndots++;
2349 }
2350 idx++; /* skip over the 'import' keyword */
2351 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002352 case STAR:
2353 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002354 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002355 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002356 if (ndots) {
2357 ast_error(n, "'import *' not allowed with 'from .'");
2358 return NULL;
2359 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002360 break;
2361 case LPAR:
2362 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002363 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002364 n_children = NCH(n);
2365 break;
2366 case import_as_names:
2367 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002368 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002369 n_children = NCH(n);
2370 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 ast_error(n, "trailing comma not allowed without"
2372 " surrounding parentheses");
2373 return NULL;
2374 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002375 break;
2376 default:
2377 ast_error(n, "Unexpected node-type in from-import");
2378 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002379 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002381 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002382 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384
2385 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002386 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002387 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002388 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002390 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002392 else {
2393 for (i = 0; i < NCH(n); i += 2) {
2394 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2395 if (!import_alias)
2396 return NULL;
2397 asdl_seq_SET(aliases, i / 2, import_alias);
2398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002400 if (mod != NULL)
2401 modname = mod->name;
2402 else
2403 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002404 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002405 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 }
Neal Norwitz79792652005-11-14 04:25:03 +00002407 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 "unknown import statement: starts with command '%s'",
2409 STR(CHILD(n, 0)));
2410 return NULL;
2411}
2412
2413static stmt_ty
2414ast_for_global_stmt(struct compiling *c, const node *n)
2415{
2416 /* global_stmt: 'global' NAME (',' NAME)* */
2417 identifier name;
2418 asdl_seq *s;
2419 int i;
2420
2421 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002422 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 if (!s)
2424 return NULL;
2425 for (i = 1; i < NCH(n); i += 2) {
2426 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002427 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 asdl_seq_SET(s, i / 2, name);
2430 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002431 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432}
2433
2434static stmt_ty
2435ast_for_exec_stmt(struct compiling *c, const node *n)
2436{
2437 expr_ty expr1, globals = NULL, locals = NULL;
2438 int n_children = NCH(n);
2439 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002440 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441 "poorly formed 'exec' statement: %d parts to statement",
2442 n_children);
2443 return NULL;
2444 }
2445
2446 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2447 REQ(n, exec_stmt);
2448 expr1 = ast_for_expr(c, CHILD(n, 1));
2449 if (!expr1)
2450 return NULL;
2451 if (n_children >= 4) {
2452 globals = ast_for_expr(c, CHILD(n, 3));
2453 if (!globals)
2454 return NULL;
2455 }
2456 if (n_children == 6) {
2457 locals = ast_for_expr(c, CHILD(n, 5));
2458 if (!locals)
2459 return NULL;
2460 }
2461
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002462 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463}
2464
2465static stmt_ty
2466ast_for_assert_stmt(struct compiling *c, const node *n)
2467{
2468 /* assert_stmt: 'assert' test [',' test] */
2469 REQ(n, assert_stmt);
2470 if (NCH(n) == 2) {
2471 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2472 if (!expression)
2473 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002474 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 }
2476 else if (NCH(n) == 4) {
2477 expr_ty expr1, expr2;
2478
2479 expr1 = ast_for_expr(c, CHILD(n, 1));
2480 if (!expr1)
2481 return NULL;
2482 expr2 = ast_for_expr(c, CHILD(n, 3));
2483 if (!expr2)
2484 return NULL;
2485
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002486 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 }
Neal Norwitz79792652005-11-14 04:25:03 +00002488 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 "improper number of parts to 'assert' statement: %d",
2490 NCH(n));
2491 return NULL;
2492}
2493
2494static asdl_seq *
2495ast_for_suite(struct compiling *c, const node *n)
2496{
2497 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002498 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 stmt_ty s;
2500 int i, total, num, end, pos = 0;
2501 node *ch;
2502
2503 REQ(n, suite);
2504
2505 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002506 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 if (!seq)
2508 return NULL;
2509 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2510 n = CHILD(n, 0);
2511 /* simple_stmt always ends with a NEWLINE,
2512 and may have a trailing SEMI
2513 */
2514 end = NCH(n) - 1;
2515 if (TYPE(CHILD(n, end - 1)) == SEMI)
2516 end--;
2517 /* loop by 2 to skip semi-colons */
2518 for (i = 0; i < end; i += 2) {
2519 ch = CHILD(n, i);
2520 s = ast_for_stmt(c, ch);
2521 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002522 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 asdl_seq_SET(seq, pos++, s);
2524 }
2525 }
2526 else {
2527 for (i = 2; i < (NCH(n) - 1); i++) {
2528 ch = CHILD(n, i);
2529 REQ(ch, stmt);
2530 num = num_stmts(ch);
2531 if (num == 1) {
2532 /* small_stmt or compound_stmt with only one child */
2533 s = ast_for_stmt(c, ch);
2534 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 asdl_seq_SET(seq, pos++, s);
2537 }
2538 else {
2539 int j;
2540 ch = CHILD(ch, 0);
2541 REQ(ch, simple_stmt);
2542 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002543 /* statement terminates with a semi-colon ';' */
2544 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002545 assert((j + 1) == NCH(ch));
2546 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002547 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 s = ast_for_stmt(c, CHILD(ch, j));
2549 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002550 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 asdl_seq_SET(seq, pos++, s);
2552 }
2553 }
2554 }
2555 }
2556 assert(pos == seq->size);
2557 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558}
2559
2560static stmt_ty
2561ast_for_if_stmt(struct compiling *c, const node *n)
2562{
2563 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2564 ['else' ':' suite]
2565 */
2566 char *s;
2567
2568 REQ(n, if_stmt);
2569
2570 if (NCH(n) == 4) {
2571 expr_ty expression;
2572 asdl_seq *suite_seq;
2573
2574 expression = ast_for_expr(c, CHILD(n, 1));
2575 if (!expression)
2576 return NULL;
2577 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002578 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579 return NULL;
2580
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002581 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 s = STR(CHILD(n, 4));
2585 /* s[2], the third character in the string, will be
2586 's' for el_s_e, or
2587 'i' for el_i_f
2588 */
2589 if (s[2] == 's') {
2590 expr_ty expression;
2591 asdl_seq *seq1, *seq2;
2592
2593 expression = ast_for_expr(c, CHILD(n, 1));
2594 if (!expression)
2595 return NULL;
2596 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002597 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 return NULL;
2599 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002600 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 return NULL;
2602
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002603 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 }
2605 else if (s[2] == 'i') {
2606 int i, n_elif, has_else = 0;
Collin Winter7d9ac782007-03-16 04:12:48 +00002607 expr_ty expression;
2608 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 asdl_seq *orelse = NULL;
2610 n_elif = NCH(n) - 4;
2611 /* must reference the child n_elif+1 since 'else' token is third,
2612 not fourth, child from the end. */
2613 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2614 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2615 has_else = 1;
2616 n_elif -= 3;
2617 }
2618 n_elif /= 4;
2619
2620 if (has_else) {
Collin Winter7d9ac782007-03-16 04:12:48 +00002621 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002623 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 if (!orelse)
2625 return NULL;
2626 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002627 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 return NULL;
Collin Winter7d9ac782007-03-16 04:12:48 +00002629 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2630 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 return NULL;
Collin Winter7d9ac782007-03-16 04:12:48 +00002632 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2633 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635
Collin Winter7d9ac782007-03-16 04:12:48 +00002636 asdl_seq_SET(orelse, 0, If(expression, suite_seq, suite_seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002637 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002638 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 /* the just-created orelse handled the last elif */
2640 n_elif--;
2641 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642
2643 for (i = 0; i < n_elif; i++) {
2644 int off = 5 + (n_elif - i - 1) * 4;
Anthony Baxtera863d332006-04-11 07:43:46 +00002645 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2646 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 return NULL;
2648 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002649 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002652 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654
Anthony Baxtera863d332006-04-11 07:43:46 +00002655 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002657 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Anthony Baxtera863d332006-04-11 07:43:46 +00002658 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 }
Collin Winter7d9ac782007-03-16 04:12:48 +00002660 expression = ast_for_expr(c, CHILD(n, 1));
2661 if (!expression)
2662 return NULL;
2663 suite_seq = ast_for_suite(c, CHILD(n, 3));
2664 if (!suite_seq)
2665 return NULL;
2666 return If(expression, suite_seq, orelse,
2667 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002669
2670 PyErr_Format(PyExc_SystemError,
2671 "unexpected token in 'if' statement: %s", s);
2672 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673}
2674
2675static stmt_ty
2676ast_for_while_stmt(struct compiling *c, const node *n)
2677{
2678 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2679 REQ(n, while_stmt);
2680
2681 if (NCH(n) == 4) {
2682 expr_ty expression;
2683 asdl_seq *suite_seq;
2684
2685 expression = ast_for_expr(c, CHILD(n, 1));
2686 if (!expression)
2687 return NULL;
2688 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002689 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002691 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 }
2693 else if (NCH(n) == 7) {
2694 expr_ty expression;
2695 asdl_seq *seq1, *seq2;
2696
2697 expression = ast_for_expr(c, CHILD(n, 1));
2698 if (!expression)
2699 return NULL;
2700 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002701 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 return NULL;
2703 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002704 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 return NULL;
2706
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002707 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002709
2710 PyErr_Format(PyExc_SystemError,
2711 "wrong number of tokens for 'while' statement: %d",
2712 NCH(n));
2713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714}
2715
2716static stmt_ty
2717ast_for_for_stmt(struct compiling *c, const node *n)
2718{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002719 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 expr_ty expression;
2721 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002722 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2724 REQ(n, for_stmt);
2725
2726 if (NCH(n) == 9) {
2727 seq = ast_for_suite(c, CHILD(n, 8));
2728 if (!seq)
2729 return NULL;
2730 }
2731
Neal Norwitzedef2be2006-07-12 05:26:17 +00002732 node_target = CHILD(n, 1);
2733 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002734 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002736 /* Check the # of children rather than the length of _target, since
2737 for x, in ... has 1 element in _target, but still requires a Tuple. */
2738 if (NCH(node_target) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00002739 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002741 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002743 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002744 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 return NULL;
2746 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002747 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 return NULL;
2749
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002750 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2751 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752}
2753
2754static excepthandler_ty
2755ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2756{
2757 /* except_clause: 'except' [test [',' test]] */
2758 REQ(exc, except_clause);
2759 REQ(body, suite);
2760
2761 if (NCH(exc) == 1) {
2762 asdl_seq *suite_seq = ast_for_suite(c, body);
2763 if (!suite_seq)
2764 return NULL;
2765
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002766 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2767 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 }
2769 else if (NCH(exc) == 2) {
2770 expr_ty expression;
2771 asdl_seq *suite_seq;
2772
2773 expression = ast_for_expr(c, CHILD(exc, 1));
2774 if (!expression)
2775 return NULL;
2776 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002777 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 return NULL;
2779
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002780 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2781 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 }
2783 else if (NCH(exc) == 4) {
2784 asdl_seq *suite_seq;
2785 expr_ty expression;
2786 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2787 if (!e)
2788 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002789 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 return NULL;
2791 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002792 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 return NULL;
2794 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002795 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 return NULL;
2797
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002798 return excepthandler(expression, e, suite_seq, LINENO(exc),
2799 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002801
2802 PyErr_Format(PyExc_SystemError,
2803 "wrong number of children for 'except' clause: %d",
2804 NCH(exc));
2805 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806}
2807
2808static stmt_ty
2809ast_for_try_stmt(struct compiling *c, const node *n)
2810{
Neal Norwitzf599f422005-12-17 21:33:47 +00002811 const int nch = NCH(n);
2812 int n_except = (nch - 3)/3;
2813 asdl_seq *body, *orelse = NULL, *finally = NULL;
2814
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 REQ(n, try_stmt);
2816
Neal Norwitzf599f422005-12-17 21:33:47 +00002817 body = ast_for_suite(c, CHILD(n, 2));
2818 if (body == NULL)
2819 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820
Neal Norwitzf599f422005-12-17 21:33:47 +00002821 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2822 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2823 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2824 /* we can assume it's an "else",
2825 because nch >= 9 for try-else-finally and
2826 it would otherwise have a type of except_clause */
2827 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2828 if (orelse == NULL)
2829 return NULL;
2830 n_except--;
2831 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832
Neal Norwitzf599f422005-12-17 21:33:47 +00002833 finally = ast_for_suite(c, CHILD(n, nch - 1));
2834 if (finally == NULL)
2835 return NULL;
2836 n_except--;
2837 }
2838 else {
2839 /* we can assume it's an "else",
2840 otherwise it would have a type of except_clause */
2841 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2842 if (orelse == NULL)
2843 return NULL;
2844 n_except--;
2845 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002847 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002848 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 return NULL;
2850 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002851
2852 if (n_except > 0) {
2853 int i;
2854 stmt_ty except_st;
2855 /* process except statements to create a try ... except */
2856 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2857 if (handlers == NULL)
2858 return NULL;
2859
2860 for (i = 0; i < n_except; i++) {
2861 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2862 CHILD(n, 5 + i * 3));
2863 if (!e)
2864 return NULL;
2865 asdl_seq_SET(handlers, i, e);
2866 }
2867
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002868 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2869 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002870 if (!finally)
2871 return except_st;
2872
2873 /* if a 'finally' is present too, we nest the TryExcept within a
2874 TryFinally to emulate try ... except ... finally */
2875 body = asdl_seq_new(1, c->c_arena);
2876 if (body == NULL)
2877 return NULL;
2878 asdl_seq_SET(body, 0, except_st);
2879 }
2880
2881 /* must be a try ... finally (except clauses are in body, if any exist) */
2882 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002883 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884}
2885
Guido van Rossumc2e20742006-02-27 22:32:47 +00002886static expr_ty
2887ast_for_with_var(struct compiling *c, const node *n)
2888{
2889 REQ(n, with_var);
2890 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2891 ast_error(n, "expected \"with [expr] as [var]\"");
2892 return NULL;
2893 }
2894 return ast_for_expr(c, CHILD(n, 1));
2895}
2896
2897/* with_stmt: 'with' test [ with_var ] ':' suite */
2898static stmt_ty
2899ast_for_with_stmt(struct compiling *c, const node *n)
2900{
2901 expr_ty context_expr, optional_vars = NULL;
2902 int suite_index = 3; /* skip 'with', test, and ':' */
2903 asdl_seq *suite_seq;
2904
2905 assert(TYPE(n) == with_stmt);
2906 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter7d9ac782007-03-16 04:12:48 +00002907 if (!context_expr)
2908 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002909 if (TYPE(CHILD(n, 2)) == with_var) {
2910 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2911
2912 if (!optional_vars) {
2913 return NULL;
2914 }
2915 if (!set_context(optional_vars, Store, n)) {
2916 return NULL;
2917 }
2918 suite_index = 4;
2919 }
2920
2921 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2922 if (!suite_seq) {
2923 return NULL;
2924 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002925 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2926 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002927}
2928
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929static stmt_ty
2930ast_for_classdef(struct compiling *c, const node *n)
2931{
2932 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933 asdl_seq *bases, *s;
2934
2935 REQ(n, classdef);
2936
2937 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2938 ast_error(n, "assignment to None");
2939 return NULL;
2940 }
2941
2942 if (NCH(n) == 4) {
2943 s = ast_for_suite(c, CHILD(n, 3));
2944 if (!s)
2945 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002946 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2947 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 }
2949 /* check for empty base list */
2950 if (TYPE(CHILD(n,3)) == RPAR) {
2951 s = ast_for_suite(c, CHILD(n,5));
2952 if (!s)
2953 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002954 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2955 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956 }
2957
2958 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002959 bases = ast_for_class_bases(c, CHILD(n, 3));
2960 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962
2963 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002964 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002966 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2967 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968}
2969
2970static stmt_ty
2971ast_for_stmt(struct compiling *c, const node *n)
2972{
2973 if (TYPE(n) == stmt) {
2974 assert(NCH(n) == 1);
2975 n = CHILD(n, 0);
2976 }
2977 if (TYPE(n) == simple_stmt) {
2978 assert(num_stmts(n) == 1);
2979 n = CHILD(n, 0);
2980 }
2981 if (TYPE(n) == small_stmt) {
2982 REQ(n, small_stmt);
2983 n = CHILD(n, 0);
2984 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2985 | flow_stmt | import_stmt | global_stmt | exec_stmt
2986 | assert_stmt
2987 */
2988 switch (TYPE(n)) {
2989 case expr_stmt:
2990 return ast_for_expr_stmt(c, n);
2991 case print_stmt:
2992 return ast_for_print_stmt(c, n);
2993 case del_stmt:
2994 return ast_for_del_stmt(c, n);
2995 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002996 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997 case flow_stmt:
2998 return ast_for_flow_stmt(c, n);
2999 case import_stmt:
3000 return ast_for_import_stmt(c, n);
3001 case global_stmt:
3002 return ast_for_global_stmt(c, n);
3003 case exec_stmt:
3004 return ast_for_exec_stmt(c, n);
3005 case assert_stmt:
3006 return ast_for_assert_stmt(c, n);
3007 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003008 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003009 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3010 TYPE(n), NCH(n));
3011 return NULL;
3012 }
3013 }
3014 else {
3015 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3016 | funcdef | classdef
3017 */
3018 node *ch = CHILD(n, 0);
3019 REQ(n, compound_stmt);
3020 switch (TYPE(ch)) {
3021 case if_stmt:
3022 return ast_for_if_stmt(c, ch);
3023 case while_stmt:
3024 return ast_for_while_stmt(c, ch);
3025 case for_stmt:
3026 return ast_for_for_stmt(c, ch);
3027 case try_stmt:
3028 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003029 case with_stmt:
3030 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 case funcdef:
3032 return ast_for_funcdef(c, ch);
3033 case classdef:
3034 return ast_for_classdef(c, ch);
3035 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003036 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3038 TYPE(n), NCH(n));
3039 return NULL;
3040 }
3041 }
3042}
3043
3044static PyObject *
3045parsenumber(const char *s)
3046{
3047 const char *end;
3048 long x;
3049 double dx;
3050#ifndef WITHOUT_COMPLEX
3051 Py_complex c;
3052 int imflag;
3053#endif
3054
3055 errno = 0;
3056 end = s + strlen(s) - 1;
3057#ifndef WITHOUT_COMPLEX
3058 imflag = *end == 'j' || *end == 'J';
3059#endif
3060 if (*end == 'l' || *end == 'L')
3061 return PyLong_FromString((char *)s, (char **)0, 0);
3062 if (s[0] == '0') {
3063 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3064 if (x < 0 && errno == 0) {
3065 return PyLong_FromString((char *)s,
3066 (char **)0,
3067 0);
3068 }
3069 }
3070 else
3071 x = PyOS_strtol((char *)s, (char **)&end, 0);
3072 if (*end == '\0') {
3073 if (errno != 0)
3074 return PyLong_FromString((char *)s, (char **)0, 0);
3075 return PyInt_FromLong(x);
3076 }
3077 /* XXX Huge floats may silently fail */
3078#ifndef WITHOUT_COMPLEX
3079 if (imflag) {
3080 c.real = 0.;
3081 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003082 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 PyFPE_END_PROTECT(c)
3084 return PyComplex_FromCComplex(c);
3085 }
3086 else
3087#endif
3088 {
3089 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003090 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 PyFPE_END_PROTECT(dx)
3092 return PyFloat_FromDouble(dx);
3093 }
3094}
3095
3096static PyObject *
3097decode_utf8(const char **sPtr, const char *end, char* encoding)
3098{
3099#ifndef Py_USING_UNICODE
3100 Py_FatalError("decode_utf8 should not be called in this build.");
3101 return NULL;
3102#else
3103 PyObject *u, *v;
3104 char *s, *t;
3105 t = s = (char *)*sPtr;
3106 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3107 while (s < end && (*s & 0x80)) s++;
3108 *sPtr = s;
3109 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3110 if (u == NULL)
3111 return NULL;
3112 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3113 Py_DECREF(u);
3114 return v;
3115#endif
3116}
3117
Georg Brandleec47f32007-08-23 18:08:33 +00003118#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119static PyObject *
3120decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3121{
3122 PyObject *v, *u;
3123 char *buf;
3124 char *p;
3125 const char *end;
3126 if (encoding == NULL) {
3127 buf = (char *)s;
3128 u = NULL;
3129 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3130 buf = (char *)s;
3131 u = NULL;
3132 } else {
3133 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3134 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3135 if (u == NULL)
3136 return NULL;
3137 p = buf = PyString_AsString(u);
3138 end = s + len;
3139 while (s < end) {
3140 if (*s == '\\') {
3141 *p++ = *s++;
3142 if (*s & 0x80) {
3143 strcpy(p, "u005c");
3144 p += 5;
3145 }
3146 }
3147 if (*s & 0x80) { /* XXX inefficient */
3148 PyObject *w;
3149 char *r;
Martin v. Löwis66851282006-04-22 11:40:03 +00003150 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 w = decode_utf8(&s, end, "utf-16-be");
3152 if (w == NULL) {
3153 Py_DECREF(u);
3154 return NULL;
3155 }
3156 r = PyString_AsString(w);
3157 rn = PyString_Size(w);
3158 assert(rn % 2 == 0);
3159 for (i = 0; i < rn; i += 2) {
3160 sprintf(p, "\\u%02x%02x",
3161 r[i + 0] & 0xFF,
3162 r[i + 1] & 0xFF);
3163 p += 6;
3164 }
3165 Py_DECREF(w);
3166 } else {
3167 *p++ = *s++;
3168 }
3169 }
3170 len = p - buf;
3171 s = buf;
3172 }
3173 if (rawmode)
3174 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3175 else
3176 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3177 Py_XDECREF(u);
3178 return v;
3179}
Georg Brandleec47f32007-08-23 18:08:33 +00003180#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181
3182/* s is a Python string literal, including the bracketing quote characters,
3183 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3184 * parsestr parses it, and returns the decoded Python string object.
3185 */
3186static PyObject *
3187parsestr(const char *s, const char *encoding)
3188{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003190 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 int rawmode = 0;
3192 int need_encoding;
3193 int unicode = 0;
3194
3195 if (isalpha(quote) || quote == '_') {
3196 if (quote == 'u' || quote == 'U') {
3197 quote = *++s;
3198 unicode = 1;
3199 }
3200 if (quote == 'r' || quote == 'R') {
3201 quote = *++s;
3202 rawmode = 1;
3203 }
3204 }
3205 if (quote != '\'' && quote != '\"') {
3206 PyErr_BadInternalCall();
3207 return NULL;
3208 }
3209 s++;
3210 len = strlen(s);
3211 if (len > INT_MAX) {
3212 PyErr_SetString(PyExc_OverflowError,
3213 "string to parse is too long");
3214 return NULL;
3215 }
3216 if (s[--len] != quote) {
3217 PyErr_BadInternalCall();
3218 return NULL;
3219 }
3220 if (len >= 4 && s[0] == quote && s[1] == quote) {
3221 s += 2;
3222 len -= 2;
3223 if (s[--len] != quote || s[--len] != quote) {
3224 PyErr_BadInternalCall();
3225 return NULL;
3226 }
3227 }
3228#ifdef Py_USING_UNICODE
3229 if (unicode || Py_UnicodeFlag) {
3230 return decode_unicode(s, len, rawmode, encoding);
3231 }
3232#endif
3233 need_encoding = (encoding != NULL &&
3234 strcmp(encoding, "utf-8") != 0 &&
3235 strcmp(encoding, "iso-8859-1") != 0);
3236 if (rawmode || strchr(s, '\\') == NULL) {
3237 if (need_encoding) {
3238#ifndef Py_USING_UNICODE
3239 /* This should not happen - we never see any other
3240 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003241 Py_FatalError(
3242 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003243#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003244 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245 if (u == NULL)
3246 return NULL;
3247 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3248 Py_DECREF(u);
3249 return v;
3250#endif
3251 } else {
3252 return PyString_FromStringAndSize(s, len);
3253 }
3254 }
3255
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003256 return PyString_DecodeEscape(s, len, NULL, unicode,
3257 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258}
3259
3260/* Build a Python string object out of a STRING atom. This takes care of
3261 * compile-time literal catenation, calling parsestr() on each piece, and
3262 * pasting the intermediate results together.
3263 */
3264static PyObject *
3265parsestrplus(struct compiling *c, const node *n)
3266{
3267 PyObject *v;
3268 int i;
3269 REQ(CHILD(n, 0), STRING);
3270 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3271 /* String literal concatenation */
3272 for (i = 1; i < NCH(n); i++) {
3273 PyObject *s;
3274 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3275 if (s == NULL)
3276 goto onError;
3277 if (PyString_Check(v) && PyString_Check(s)) {
3278 PyString_ConcatAndDel(&v, s);
3279 if (v == NULL)
3280 goto onError;
3281 }
3282#ifdef Py_USING_UNICODE
3283 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003284 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286 Py_DECREF(v);
3287 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003288 if (v == NULL)
3289 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 }
3291#endif
3292 }
3293 }
3294 return v;
3295
3296 onError:
3297 Py_XDECREF(v);
3298 return NULL;
3299}