blob: 057b64df32583dfafba6cb19a9fce0b79d0393fd [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));
Georg Brandld297f1a2008-06-15 19:53:12 +0000252 if (!asdl_seq_GET(stmts, 0))
253 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000254 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255 }
256 else {
257 n = CHILD(n, 0);
258 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000259 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260 if (!stmts)
261 goto error;
262 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000263 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 if (!s)
265 goto error;
266 asdl_seq_SET(stmts, 0, s);
267 }
268 else {
269 /* Only a simple_stmt can contain multiple statements. */
270 REQ(n, simple_stmt);
271 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272 if (TYPE(CHILD(n, i)) == NEWLINE)
273 break;
274 s = ast_for_stmt(&c, CHILD(n, i));
275 if (!s)
276 goto error;
277 asdl_seq_SET(stmts, i / 2, s);
278 }
279 }
280
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000281 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282 }
283 default:
284 goto error;
285 }
286 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287 ast_error_finish(filename);
288 return NULL;
289}
290
291/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
292*/
293
294static operator_ty
295get_operator(const node *n)
296{
297 switch (TYPE(n)) {
298 case VBAR:
299 return BitOr;
300 case CIRCUMFLEX:
301 return BitXor;
302 case AMPER:
303 return BitAnd;
304 case LEFTSHIFT:
305 return LShift;
306 case RIGHTSHIFT:
307 return RShift;
308 case PLUS:
309 return Add;
310 case MINUS:
311 return Sub;
312 case STAR:
313 return Mult;
314 case SLASH:
315 return Div;
316 case DOUBLESLASH:
317 return FloorDiv;
318 case PERCENT:
319 return Mod;
320 default:
Martin v. Löwis28457502006-04-11 09:17:27 +0000321 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 }
323}
324
Jeremy Hyltona8293132006-02-28 17:58:27 +0000325/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326
327 Only sets context for expr kinds that "can appear in assignment context"
328 (according to ../Parser/Python.asdl). For other expr kinds, it sets
329 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330*/
331
332static int
333set_context(expr_ty e, expr_context_ty ctx, const node *n)
334{
335 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000336 /* If a particular expression type can't be used for assign / delete,
337 set expr_name to its name and an error message will be generated.
338 */
339 const char* expr_name = NULL;
340
341 /* The ast defines augmented store and load contexts, but the
342 implementation here doesn't actually use them. The code may be
343 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000344 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000346 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000347 */
348 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
350 switch (e->kind) {
351 case Attribute_kind:
352 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000353 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354 return ast_error(n, "assignment to None");
355 }
356 e->v.Attribute.ctx = ctx;
357 break;
358 case Subscript_kind:
359 e->v.Subscript.ctx = ctx;
360 break;
361 case Name_kind:
362 if (ctx == Store &&
363 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
364 return ast_error(n, "assignment to None");
365 }
366 e->v.Name.ctx = ctx;
367 break;
368 case List_kind:
369 e->v.List.ctx = ctx;
370 s = e->v.List.elts;
371 break;
372 case Tuple_kind:
373 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
374 return ast_error(n, "can't assign to ()");
375 e->v.Tuple.ctx = ctx;
376 s = e->v.Tuple.elts;
377 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000378 case Lambda_kind:
379 expr_name = "lambda";
380 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000386 case UnaryOp_kind:
387 expr_name = "operator";
388 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000390 expr_name = "generator expression";
391 break;
Neal Norwitz0d62a062006-07-30 06:53:31 +0000392 case Yield_kind:
393 expr_name = "yield expression";
394 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000395 case ListComp_kind:
396 expr_name = "list comprehension";
397 break;
398 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399 case Num_kind:
400 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000401 expr_name = "literal";
402 break;
403 case Compare_kind:
404 expr_name = "comparison";
405 break;
406 case Repr_kind:
407 expr_name = "repr";
408 break;
Neal Norwitz373f0a72006-05-15 07:04:36 +0000409 case IfExp_kind:
410 expr_name = "conditional expression";
411 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000412 default:
413 PyErr_Format(PyExc_SystemError,
414 "unexpected expression in assignment %d (line %d)",
415 e->kind, e->lineno);
416 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000418 /* Check for error string set by switch */
419 if (expr_name) {
420 char buf[300];
421 PyOS_snprintf(buf, sizeof(buf),
422 "can't %s %s",
423 ctx == Store ? "assign to" : "delete",
424 expr_name);
425 return ast_error(n, buf);
426 }
427
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000429 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430 */
431 if (s) {
432 int i;
433
434 for (i = 0; i < asdl_seq_LEN(s); i++) {
Martin v. Löwis28457502006-04-11 09:17:27 +0000435 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436 return 0;
437 }
438 }
439 return 1;
440}
441
442static operator_ty
443ast_for_augassign(const node *n)
444{
445 REQ(n, augassign);
446 n = CHILD(n, 0);
447 switch (STR(n)[0]) {
448 case '+':
449 return Add;
450 case '-':
451 return Sub;
452 case '/':
453 if (STR(n)[1] == '/')
454 return FloorDiv;
455 else
456 return Div;
457 case '%':
458 return Mod;
459 case '<':
460 return LShift;
461 case '>':
462 return RShift;
463 case '&':
464 return BitAnd;
465 case '^':
466 return BitXor;
467 case '|':
468 return BitOr;
469 case '*':
470 if (STR(n)[1] == '*')
471 return Pow;
472 else
473 return Mult;
474 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000475 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000476 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 }
478}
479
480static cmpop_ty
481ast_for_comp_op(const node *n)
482{
483 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
484 |'is' 'not'
485 */
486 REQ(n, comp_op);
487 if (NCH(n) == 1) {
488 n = CHILD(n, 0);
489 switch (TYPE(n)) {
490 case LESS:
491 return Lt;
492 case GREATER:
493 return Gt;
494 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 return Eq;
496 case LESSEQUAL:
497 return LtE;
498 case GREATEREQUAL:
499 return GtE;
500 case NOTEQUAL:
501 return NotEq;
502 case NAME:
503 if (strcmp(STR(n), "in") == 0)
504 return In;
505 if (strcmp(STR(n), "is") == 0)
506 return Is;
507 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000508 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 STR(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000510 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 }
512 }
513 else if (NCH(n) == 2) {
514 /* handle "not in" and "is not" */
515 switch (TYPE(CHILD(n, 0))) {
516 case NAME:
517 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
518 return NotIn;
519 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
520 return IsNot;
521 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000522 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Martin v. Löwis28457502006-04-11 09:17:27 +0000524 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 }
526 }
Neal Norwitz79792652005-11-14 04:25:03 +0000527 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000529 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530}
531
532static asdl_seq *
533seq_for_testlist(struct compiling *c, const node *n)
534{
535 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000536 asdl_seq *seq;
537 expr_ty expression;
538 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 assert(TYPE(n) == testlist
540 || TYPE(n) == listmaker
541 || TYPE(n) == testlist_gexp
542 || TYPE(n) == testlist_safe
Neal Norwitza3ce6aa2006-11-04 19:32:54 +0000543 || TYPE(n) == testlist1
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000546 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 if (!seq)
548 return NULL;
549
550 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000551 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552
553 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000554 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556
557 assert(i / 2 < seq->size);
558 asdl_seq_SET(seq, i / 2, expression);
559 }
560 return seq;
561}
562
563static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000564compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565{
566 int i, len = (NCH(n) + 1) / 2;
567 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000568 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569 if (!args)
570 return NULL;
571
Georg Brandlc57221e2006-09-25 07:04:10 +0000572 /* fpdef: NAME | '(' fplist ')'
573 fplist: fpdef (',' fpdef)* [',']
574 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 for (i = 0; i < len; i++) {
Georg Brandlc57221e2006-09-25 07:04:10 +0000577 const node *fpdef_node = CHILD(n, 2*i);
578 const node *child;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579 expr_ty arg;
Georg Brandlc57221e2006-09-25 07:04:10 +0000580set_name:
581 /* fpdef_node is either a NAME or an fplist */
582 child = CHILD(fpdef_node, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000584 if (!strcmp(STR(child), "None")) {
585 ast_error(child, "assignment to None");
586 return NULL;
587 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000588 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
589 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000590 }
591 else {
Georg Brandlc57221e2006-09-25 07:04:10 +0000592 assert(TYPE(fpdef_node) == fpdef);
593 /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
594 child = CHILD(fpdef_node, 1);
595 assert(TYPE(child) == fplist);
596 /* NCH == 1 means we have (x), we need to elide the extra parens */
597 if (NCH(child) == 1) {
598 fpdef_node = CHILD(child, 0);
599 assert(TYPE(fpdef_node) == fpdef);
600 goto set_name;
601 }
602 arg = compiler_complex_args(c, child);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000603 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 asdl_seq_SET(args, i, arg);
605 }
606
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000607 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000608 if (!set_context(result, Store, n))
609 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 return result;
611}
612
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
Jeremy Hyltona8293132006-02-28 17:58:27 +0000614/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615
616static arguments_ty
617ast_for_arguments(struct compiling *c, const node *n)
618{
619 /* parameters: '(' [varargslist] ')'
620 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
621 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
622 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000623 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 asdl_seq *args, *defaults;
625 identifier vararg = NULL, kwarg = NULL;
626 node *ch;
627
628 if (TYPE(n) == parameters) {
629 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000630 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 n = CHILD(n, 1);
632 }
633 REQ(n, varargslist);
634
635 /* first count the number of normal args & defaults */
636 for (i = 0; i < NCH(n); i++) {
637 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000638 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640 if (TYPE(ch) == EQUAL)
641 n_defaults++;
642 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000643 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 if (!args && n_args)
Neal Norwitzc173b482006-07-30 19:18:13 +0000645 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000646 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 if (!defaults && n_defaults)
Neal Norwitzc173b482006-07-30 19:18:13 +0000648 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649
650 /* fpdef: NAME | '(' fplist ')'
651 fplist: fpdef (',' fpdef)* [',']
652 */
653 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000654 j = 0; /* index for defaults */
655 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 while (i < NCH(n)) {
657 ch = CHILD(n, i);
658 switch (TYPE(ch)) {
659 case fpdef:
Georg Brandlc57221e2006-09-25 07:04:10 +0000660 handle_fpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
662 anything other than EQUAL or a comma? */
663 /* XXX Should NCH(n) check be made a separate check? */
664 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000665 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
666 if (!expression)
667 goto error;
Neal Norwitzc173b482006-07-30 19:18:13 +0000668 assert(defaults != NULL);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000669 asdl_seq_SET(defaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 i += 2;
671 found_default = 1;
672 }
673 else if (found_default) {
674 ast_error(n,
675 "non-default argument follows default argument");
676 goto error;
677 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 if (NCH(ch) == 3) {
Neal Norwitz33b730e2006-03-27 08:58:23 +0000679 ch = CHILD(ch, 1);
680 /* def foo((x)): is not complex, special case. */
681 if (NCH(ch) != 1) {
682 /* We have complex arguments, setup for unpacking. */
683 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Georg Brandld297f1a2008-06-15 19:53:12 +0000684 if (!asdl_seq_GET(args, k-1))
685 goto error;
Neal Norwitz33b730e2006-03-27 08:58:23 +0000686 } else {
687 /* def foo((x)): setup for checking NAME below. */
Georg Brandlc57221e2006-09-25 07:04:10 +0000688 /* Loop because there can be many parens and tuple
689 upacking mixed in. */
Neal Norwitz33b730e2006-03-27 08:58:23 +0000690 ch = CHILD(ch, 0);
Georg Brandlc57221e2006-09-25 07:04:10 +0000691 assert(TYPE(ch) == fpdef);
692 goto handle_fpdef;
Neal Norwitz33b730e2006-03-27 08:58:23 +0000693 }
694 }
695 if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000696 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
698 ast_error(CHILD(ch, 0), "assignment to None");
699 goto error;
700 }
Armin Rigo31441302005-10-21 12:57:31 +0000701 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000702 Param, LINENO(ch), ch->n_col_offset,
703 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 if (!name)
705 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000706 asdl_seq_SET(args, k++, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707
708 }
709 i += 2; /* the name and the comma */
710 break;
711 case STAR:
712 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
713 ast_error(CHILD(n, i+1), "assignment to None");
714 goto error;
715 }
716 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
717 i += 3;
718 break;
719 case DOUBLESTAR:
720 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
721 ast_error(CHILD(n, i+1), "assignment to None");
722 goto error;
723 }
724 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
725 i += 3;
726 break;
727 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000728 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 "unexpected node in varargslist: %d @ %d",
730 TYPE(ch), i);
731 goto error;
732 }
733 }
734
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000735 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
737 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000738 Py_XDECREF(vararg);
739 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740 return NULL;
741}
742
743static expr_ty
744ast_for_dotted_name(struct compiling *c, const node *n)
745{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000746 expr_ty e;
747 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000748 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 int i;
750
751 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000752
753 lineno = LINENO(n);
754 col_offset = n->n_col_offset;
755
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 id = NEW_IDENTIFIER(CHILD(n, 0));
757 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000758 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000759 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000761 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762
763 for (i = 2; i < NCH(n); i+=2) {
764 id = NEW_IDENTIFIER(CHILD(n, i));
765 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000766 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000767 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000768 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000769 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 }
771
772 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773}
774
775static expr_ty
776ast_for_decorator(struct compiling *c, const node *n)
777{
778 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
779 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000780 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
782 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000783 REQ(CHILD(n, 0), AT);
784 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
786 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
787 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789
790 if (NCH(n) == 3) { /* No arguments */
791 d = name_expr;
792 name_expr = NULL;
793 }
794 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000795 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
796 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 name_expr = NULL;
800 }
801 else {
802 d = ast_for_call(c, CHILD(n, 3), name_expr);
803 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000804 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 name_expr = NULL;
806 }
807
808 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809}
810
811static asdl_seq*
812ast_for_decorators(struct compiling *c, const node *n)
813{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000814 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000815 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 int i;
817
818 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000819 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 if (!decorator_seq)
821 return NULL;
822
823 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000824 d = ast_for_decorator(c, CHILD(n, i));
825 if (!d)
826 return NULL;
827 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 }
829 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830}
831
832static stmt_ty
833ast_for_funcdef(struct compiling *c, const node *n)
834{
835 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000836 identifier name;
837 arguments_ty args;
838 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 asdl_seq *decorator_seq = NULL;
840 int name_i;
841
842 REQ(n, funcdef);
843
844 if (NCH(n) == 6) { /* decorators are present */
845 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
846 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000847 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 name_i = 2;
849 }
850 else {
851 name_i = 1;
852 }
853
854 name = NEW_IDENTIFIER(CHILD(n, name_i));
855 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000858 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 }
861 args = ast_for_arguments(c, CHILD(n, name_i + 1));
862 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000863 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 body = ast_for_suite(c, CHILD(n, name_i + 3));
865 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000866 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000868 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
869 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870}
871
872static expr_ty
873ast_for_lambdef(struct compiling *c, const node *n)
874{
875 /* lambdef: 'lambda' [varargslist] ':' test */
876 arguments_ty args;
877 expr_ty expression;
878
879 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000880 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 if (!args)
882 return NULL;
883 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000884 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 }
887 else {
888 args = ast_for_arguments(c, CHILD(n, 1));
889 if (!args)
890 return NULL;
891 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000892 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894 }
895
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000896 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897}
898
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000899static expr_ty
900ast_for_ifexpr(struct compiling *c, const node *n)
901{
902 /* test: or_test 'if' or_test 'else' test */
903 expr_ty expression, body, orelse;
904
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000905 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000906 body = ast_for_expr(c, CHILD(n, 0));
907 if (!body)
908 return NULL;
909 expression = ast_for_expr(c, CHILD(n, 2));
910 if (!expression)
911 return NULL;
912 orelse = ast_for_expr(c, CHILD(n, 4));
913 if (!orelse)
914 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000915 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
916 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000917}
918
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919/* Count the number of 'for' loop in a list comprehension.
920
921 Helper for ast_for_listcomp().
922*/
923
924static int
925count_list_fors(const node *n)
926{
927 int n_fors = 0;
928 node *ch = CHILD(n, 1);
929
930 count_list_for:
931 n_fors++;
932 REQ(ch, list_for);
933 if (NCH(ch) == 5)
934 ch = CHILD(ch, 4);
935 else
936 return n_fors;
937 count_list_iter:
938 REQ(ch, list_iter);
939 ch = CHILD(ch, 0);
940 if (TYPE(ch) == list_for)
941 goto count_list_for;
942 else if (TYPE(ch) == list_if) {
943 if (NCH(ch) == 3) {
944 ch = CHILD(ch, 2);
945 goto count_list_iter;
946 }
947 else
948 return n_fors;
949 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000950
951 /* Should never be reached */
952 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
953 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954}
955
956/* Count the number of 'if' statements in a list comprehension.
957
958 Helper for ast_for_listcomp().
959*/
960
961static int
962count_list_ifs(const node *n)
963{
964 int n_ifs = 0;
965
966 count_list_iter:
967 REQ(n, list_iter);
968 if (TYPE(CHILD(n, 0)) == list_for)
969 return n_ifs;
970 n = CHILD(n, 0);
971 REQ(n, list_if);
972 n_ifs++;
973 if (NCH(n) == 2)
974 return n_ifs;
975 n = CHILD(n, 2);
976 goto count_list_iter;
977}
978
979static expr_ty
980ast_for_listcomp(struct compiling *c, const node *n)
981{
982 /* listmaker: test ( list_for | (',' test)* [','] )
983 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
984 list_iter: list_for | list_if
985 list_if: 'if' test [list_iter]
986 testlist_safe: test [(',' test)+ [',']]
987 */
988 expr_ty elt;
989 asdl_seq *listcomps;
990 int i, n_fors;
991 node *ch;
992
993 REQ(n, listmaker);
994 assert(NCH(n) > 1);
995
996 elt = ast_for_expr(c, CHILD(n, 0));
997 if (!elt)
998 return NULL;
999
1000 n_fors = count_list_fors(n);
1001 if (n_fors == -1)
1002 return NULL;
1003
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001004 listcomps = asdl_seq_new(n_fors, c->c_arena);
1005 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001007
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 ch = CHILD(n, 1);
1009 for (i = 0; i < n_fors; i++) {
1010 comprehension_ty lc;
1011 asdl_seq *t;
1012 expr_ty expression;
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001013 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014
1015 REQ(ch, list_for);
1016
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001017 for_ch = CHILD(ch, 1);
1018 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001019 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001021 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001022 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001025 /* Check the # of children rather than the length of t, since
1026 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
1027 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001028 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001029 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001031 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1032 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001033 expression, NULL, c->c_arena);
1034 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036
1037 if (NCH(ch) == 5) {
1038 int j, n_ifs;
1039 asdl_seq *ifs;
Collin Winter7d9ac782007-03-16 04:12:48 +00001040 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041
1042 ch = CHILD(ch, 4);
1043 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001044 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001047 ifs = asdl_seq_new(n_ifs, c->c_arena);
1048 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050
1051 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001052 REQ(ch, list_iter);
1053 ch = CHILD(ch, 0);
1054 REQ(ch, list_if);
Collin Winter7d9ac782007-03-16 04:12:48 +00001055
1056 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1057 if (!list_for_expr)
1058 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059
Collin Winter7d9ac782007-03-16 04:12:48 +00001060 asdl_seq_SET(ifs, j, list_for_expr);
Jeremy Hyltona8293132006-02-28 17:58:27 +00001061 if (NCH(ch) == 3)
1062 ch = CHILD(ch, 2);
1063 }
1064 /* on exit, must guarantee that ch is a list_for */
1065 if (TYPE(ch) == list_iter)
1066 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001068 }
1069 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 }
1071
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001072 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073}
1074
1075/*
1076 Count the number of 'for' loops in a generator expression.
1077
1078 Helper for ast_for_genexp().
1079*/
1080
1081static int
1082count_gen_fors(const node *n)
1083{
1084 int n_fors = 0;
1085 node *ch = CHILD(n, 1);
1086
1087 count_gen_for:
1088 n_fors++;
1089 REQ(ch, gen_for);
1090 if (NCH(ch) == 5)
1091 ch = CHILD(ch, 4);
1092 else
1093 return n_fors;
1094 count_gen_iter:
1095 REQ(ch, gen_iter);
1096 ch = CHILD(ch, 0);
1097 if (TYPE(ch) == gen_for)
1098 goto count_gen_for;
1099 else if (TYPE(ch) == gen_if) {
1100 if (NCH(ch) == 3) {
1101 ch = CHILD(ch, 2);
1102 goto count_gen_iter;
1103 }
1104 else
1105 return n_fors;
1106 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001107
1108 /* Should never be reached */
1109 PyErr_SetString(PyExc_SystemError,
1110 "logic error in count_gen_fors");
1111 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112}
1113
1114/* Count the number of 'if' statements in a generator expression.
1115
1116 Helper for ast_for_genexp().
1117*/
1118
1119static int
1120count_gen_ifs(const node *n)
1121{
1122 int n_ifs = 0;
1123
1124 while (1) {
1125 REQ(n, gen_iter);
1126 if (TYPE(CHILD(n, 0)) == gen_for)
1127 return n_ifs;
1128 n = CHILD(n, 0);
1129 REQ(n, gen_if);
1130 n_ifs++;
1131 if (NCH(n) == 2)
1132 return n_ifs;
1133 n = CHILD(n, 2);
1134 }
1135}
1136
Jeremy Hyltona8293132006-02-28 17:58:27 +00001137/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138static expr_ty
1139ast_for_genexp(struct compiling *c, const node *n)
1140{
1141 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1142 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1143 expr_ty elt;
1144 asdl_seq *genexps;
1145 int i, n_fors;
1146 node *ch;
1147
1148 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1149 assert(NCH(n) > 1);
1150
1151 elt = ast_for_expr(c, CHILD(n, 0));
1152 if (!elt)
1153 return NULL;
1154
1155 n_fors = count_gen_fors(n);
1156 if (n_fors == -1)
1157 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001158
1159 genexps = asdl_seq_new(n_fors, c->c_arena);
1160 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001162
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 ch = CHILD(n, 1);
1164 for (i = 0; i < n_fors; i++) {
1165 comprehension_ty ge;
1166 asdl_seq *t;
1167 expr_ty expression;
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001168 node *for_ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169
1170 REQ(ch, gen_for);
1171
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001172 for_ch = CHILD(ch, 1);
1173 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001174 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001176 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001177 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001179
Neal Norwitz3b3aae02006-09-05 03:56:01 +00001180 /* Check the # of children rather than the length of t, since
1181 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1182 if (NCH(for_ch) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00001183 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001184 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 else
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001186 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1187 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001188 expression, NULL, c->c_arena);
1189
1190 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001192
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 if (NCH(ch) == 5) {
1194 int j, n_ifs;
1195 asdl_seq *ifs;
1196
1197 ch = CHILD(ch, 4);
1198 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001199 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001201
1202 ifs = asdl_seq_new(n_ifs, c->c_arena);
1203 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001205
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 for (j = 0; j < n_ifs; j++) {
1207 REQ(ch, gen_iter);
1208 ch = CHILD(ch, 0);
1209 REQ(ch, gen_if);
1210
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001211 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001212 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001213 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001214 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 if (NCH(ch) == 3)
1216 ch = CHILD(ch, 2);
1217 }
1218 /* on exit, must guarantee that ch is a gen_for */
1219 if (TYPE(ch) == gen_iter)
1220 ch = CHILD(ch, 0);
1221 ge->ifs = ifs;
1222 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001223 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 }
1225
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001226 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227}
1228
1229static expr_ty
1230ast_for_atom(struct compiling *c, const node *n)
1231{
1232 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1233 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1234 */
1235 node *ch = CHILD(n, 0);
1236
1237 switch (TYPE(ch)) {
1238 case NAME:
1239 /* All names start in Load context, but may later be
1240 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001241 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 case STRING: {
1243 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 if (!str)
1245 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001246
1247 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001248 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 }
1250 case NUMBER: {
1251 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 if (!pynum)
1253 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001254
1255 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001256 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 }
1258 case LPAR: /* some parenthesized expressions */
1259 ch = CHILD(n, 1);
1260
1261 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001262 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263
1264 if (TYPE(ch) == yield_expr)
1265 return ast_for_expr(c, ch);
1266
1267 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1268 return ast_for_genexp(c, ch);
1269
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001270 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 case LSQB: /* list (or list comprehension) */
1272 ch = CHILD(n, 1);
1273
1274 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001275 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276
1277 REQ(ch, listmaker);
1278 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1279 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 if (!elts)
1281 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001282
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001283 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 }
1285 else
1286 return ast_for_listcomp(c, ch);
1287 case LBRACE: {
1288 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1289 int i, size;
1290 asdl_seq *keys, *values;
1291
1292 ch = CHILD(n, 1);
1293 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001294 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 if (!keys)
1296 return NULL;
1297
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001298 values = asdl_seq_new(size, c->c_arena);
1299 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301
1302 for (i = 0; i < NCH(ch); i += 4) {
1303 expr_ty expression;
1304
1305 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001306 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001308
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001310
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001312 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001314
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 asdl_seq_SET(values, i / 4, expression);
1316 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001317 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 }
1319 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001320 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 if (!expression)
1322 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001323
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001324 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 }
1326 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001327 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328 return NULL;
1329 }
1330}
1331
1332static slice_ty
1333ast_for_slice(struct compiling *c, const node *n)
1334{
1335 node *ch;
1336 expr_ty lower = NULL, upper = NULL, step = NULL;
1337
1338 REQ(n, subscript);
1339
1340 /*
1341 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1342 sliceop: ':' [test]
1343 */
1344 ch = CHILD(n, 0);
1345 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001346 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347
1348 if (NCH(n) == 1 && TYPE(ch) == test) {
1349 /* 'step' variable hold no significance in terms of being used over
1350 other vars */
1351 step = ast_for_expr(c, ch);
1352 if (!step)
1353 return NULL;
1354
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001355 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 }
1357
1358 if (TYPE(ch) == test) {
1359 lower = ast_for_expr(c, ch);
1360 if (!lower)
1361 return NULL;
1362 }
1363
1364 /* If there's an upper bound it's in the second or third position. */
1365 if (TYPE(ch) == COLON) {
1366 if (NCH(n) > 1) {
1367 node *n2 = CHILD(n, 1);
1368
1369 if (TYPE(n2) == test) {
1370 upper = ast_for_expr(c, n2);
1371 if (!upper)
1372 return NULL;
1373 }
1374 }
1375 } else if (NCH(n) > 2) {
1376 node *n2 = CHILD(n, 2);
1377
1378 if (TYPE(n2) == test) {
1379 upper = ast_for_expr(c, n2);
1380 if (!upper)
1381 return NULL;
1382 }
1383 }
1384
1385 ch = CHILD(n, NCH(n) - 1);
1386 if (TYPE(ch) == sliceop) {
Nick Coghlan77858682006-03-17 17:59:10 +00001387 if (NCH(ch) == 1) {
1388 /* No expression, so step is None */
1389 ch = CHILD(ch, 0);
1390 step = Name(new_identifier("None", c->c_arena), Load,
1391 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 if (!step)
1393 return NULL;
Nick Coghlan77858682006-03-17 17:59:10 +00001394 } else {
1395 ch = CHILD(ch, 1);
1396 if (TYPE(ch) == test) {
1397 step = ast_for_expr(c, ch);
1398 if (!step)
1399 return NULL;
1400 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 }
1402 }
1403
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001404 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405}
1406
1407static expr_ty
1408ast_for_binop(struct compiling *c, const node *n)
1409{
1410 /* Must account for a sequence of expressions.
1411 How should A op B op C by represented?
1412 BinOp(BinOp(A, op, B), op, C).
1413 */
1414
1415 int i, nops;
1416 expr_ty expr1, expr2, result;
Anthony Baxtera863d332006-04-11 07:43:46 +00001417 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418
1419 expr1 = ast_for_expr(c, CHILD(n, 0));
1420 if (!expr1)
1421 return NULL;
1422
1423 expr2 = ast_for_expr(c, CHILD(n, 2));
1424 if (!expr2)
1425 return NULL;
1426
Anthony Baxtera863d332006-04-11 07:43:46 +00001427 newoperator = get_operator(CHILD(n, 1));
1428 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 return NULL;
1430
Anthony Baxtera863d332006-04-11 07:43:46 +00001431 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001432 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 if (!result)
1434 return NULL;
1435
1436 nops = (NCH(n) - 1) / 2;
1437 for (i = 1; i < nops; i++) {
1438 expr_ty tmp_result, tmp;
1439 const node* next_oper = CHILD(n, i * 2 + 1);
1440
Anthony Baxtera863d332006-04-11 07:43:46 +00001441 newoperator = get_operator(next_oper);
1442 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 return NULL;
1444
1445 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1446 if (!tmp)
1447 return NULL;
1448
Anthony Baxtera863d332006-04-11 07:43:46 +00001449 tmp_result = BinOp(result, newoperator, tmp,
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001450 LINENO(next_oper), next_oper->n_col_offset,
1451 c->c_arena);
Neal Norwitz14f848b2007-10-05 03:45:42 +00001452 if (!tmp_result)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 return NULL;
1454 result = tmp_result;
1455 }
1456 return result;
1457}
1458
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001459static expr_ty
1460ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1461{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001462 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1463 subscriptlist: subscript (',' subscript)* [',']
1464 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1465 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001466 REQ(n, trailer);
1467 if (TYPE(CHILD(n, 0)) == LPAR) {
1468 if (NCH(n) == 2)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001469 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1470 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001471 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001472 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001473 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001474 else if (TYPE(CHILD(n, 0)) == DOT ) {
1475 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001476 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001477 }
1478 else {
1479 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001480 REQ(CHILD(n, 2), RSQB);
1481 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001482 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001483 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1484 if (!slc)
1485 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001486 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1487 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001488 }
1489 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001490 /* The grammar is ambiguous here. The ambiguity is resolved
1491 by treating the sequence as a tuple literal if there are
1492 no slice features.
1493 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001494 int j;
1495 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001496 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001497 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001498 asdl_seq *slices, *elts;
1499 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001500 if (!slices)
1501 return NULL;
1502 for (j = 0; j < NCH(n); j += 2) {
1503 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001504 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001505 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001506 if (slc->kind != Index_kind)
1507 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001508 asdl_seq_SET(slices, j / 2, slc);
1509 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001510 if (!simple) {
1511 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001512 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001513 }
1514 /* extract Index values and put them in a Tuple */
1515 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001516 if (!elts)
1517 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001518 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1519 slc = (slice_ty)asdl_seq_GET(slices, j);
1520 assert(slc->kind == Index_kind && slc->v.Index.value);
1521 asdl_seq_SET(elts, j, slc->v.Index.value);
1522 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001523 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001524 if (!e)
1525 return NULL;
1526 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001527 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001528 }
1529 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001530}
1531
1532static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001533ast_for_factor(struct compiling *c, const node *n)
1534{
1535 node *pfactor, *ppower, *patom, *pnum;
1536 expr_ty expression;
1537
1538 /* If the unary - operator is applied to a constant, don't generate
1539 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1540 constant. The peephole optimizer already does something like
1541 this but it doesn't handle the case where the constant is
1542 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1543 PyLongObject.
1544 */
1545 if (TYPE(CHILD(n, 0)) == MINUS
1546 && NCH(n) == 2
1547 && TYPE((pfactor = CHILD(n, 1))) == factor
1548 && NCH(pfactor) == 1
1549 && TYPE((ppower = CHILD(pfactor, 0))) == power
1550 && NCH(ppower) == 1
1551 && TYPE((patom = CHILD(ppower, 0))) == atom
1552 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1553 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1554 if (s == NULL)
1555 return NULL;
1556 s[0] = '-';
1557 strcpy(s + 1, STR(pnum));
1558 PyObject_FREE(STR(pnum));
1559 STR(pnum) = s;
1560 return ast_for_atom(c, patom);
1561 }
1562
1563 expression = ast_for_expr(c, CHILD(n, 1));
1564 if (!expression)
1565 return NULL;
1566
1567 switch (TYPE(CHILD(n, 0))) {
1568 case PLUS:
1569 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1570 c->c_arena);
1571 case MINUS:
1572 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1573 c->c_arena);
1574 case TILDE:
1575 return UnaryOp(Invert, expression, LINENO(n),
1576 n->n_col_offset, c->c_arena);
1577 }
1578 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1579 TYPE(CHILD(n, 0)));
1580 return NULL;
1581}
1582
1583static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001584ast_for_power(struct compiling *c, const node *n)
1585{
1586 /* power: atom trailer* ('**' factor)*
1587 */
1588 int i;
1589 expr_ty e, tmp;
1590 REQ(n, power);
1591 e = ast_for_atom(c, CHILD(n, 0));
1592 if (!e)
1593 return NULL;
1594 if (NCH(n) == 1)
1595 return e;
1596 for (i = 1; i < NCH(n); i++) {
1597 node *ch = CHILD(n, i);
1598 if (TYPE(ch) != trailer)
1599 break;
1600 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001601 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001602 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001603 tmp->lineno = e->lineno;
1604 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001605 e = tmp;
1606 }
1607 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1608 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001609 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001610 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001611 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001612 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001613 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001614 e = tmp;
1615 }
1616 return e;
1617}
1618
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619/* Do not name a variable 'expr'! Will cause a compile error.
1620*/
1621
1622static expr_ty
1623ast_for_expr(struct compiling *c, const node *n)
1624{
1625 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001626 test: or_test ['if' or_test 'else' test] | lambdef
1627 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 and_test: not_test ('and' not_test)*
1629 not_test: 'not' not_test | comparison
1630 comparison: expr (comp_op expr)*
1631 expr: xor_expr ('|' xor_expr)*
1632 xor_expr: and_expr ('^' and_expr)*
1633 and_expr: shift_expr ('&' shift_expr)*
1634 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1635 arith_expr: term (('+'|'-') term)*
1636 term: factor (('*'|'/'|'%'|'//') factor)*
1637 factor: ('+'|'-'|'~') factor | power
1638 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001639
1640 As well as modified versions that exist for backward compatibility,
1641 to explicitly allow:
1642 [ x for x in lambda: 0, lambda: 1 ]
1643 (which would be ambiguous without these extra rules)
1644
1645 old_test: or_test | old_lambdef
1646 old_lambdef: 'lambda' [vararglist] ':' old_test
1647
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 */
1649
1650 asdl_seq *seq;
1651 int i;
1652
1653 loop:
1654 switch (TYPE(n)) {
1655 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001656 case old_test:
1657 if (TYPE(CHILD(n, 0)) == lambdef ||
1658 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001660 else if (NCH(n) > 1)
1661 return ast_for_ifexpr(c, n);
1662 /* Fallthrough */
1663 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664 case and_test:
1665 if (NCH(n) == 1) {
1666 n = CHILD(n, 0);
1667 goto loop;
1668 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001669 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 if (!seq)
1671 return NULL;
1672 for (i = 0; i < NCH(n); i += 2) {
1673 expr_ty e = ast_for_expr(c, CHILD(n, i));
1674 if (!e)
1675 return NULL;
1676 asdl_seq_SET(seq, i / 2, e);
1677 }
1678 if (!strcmp(STR(CHILD(n, 1)), "and"))
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001679 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1680 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001681 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001682 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 case not_test:
1684 if (NCH(n) == 1) {
1685 n = CHILD(n, 0);
1686 goto loop;
1687 }
1688 else {
1689 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1690 if (!expression)
1691 return NULL;
1692
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001693 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1694 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 }
1696 case comparison:
1697 if (NCH(n) == 1) {
1698 n = CHILD(n, 0);
1699 goto loop;
1700 }
1701 else {
1702 expr_ty expression;
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001703 asdl_int_seq *ops;
1704 asdl_seq *cmps;
1705 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 if (!ops)
1707 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001708 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 return NULL;
1711 }
1712 for (i = 1; i < NCH(n); i += 2) {
Anthony Baxtera863d332006-04-11 07:43:46 +00001713 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714
Anthony Baxtera863d332006-04-11 07:43:46 +00001715 newoperator = ast_for_comp_op(CHILD(n, i));
1716 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001718 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719
1720 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001721 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001723 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001725 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 asdl_seq_SET(cmps, i / 2, expression);
1727 }
1728 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001729 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001731 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001733 return Compare(expression, ops, cmps, LINENO(n),
1734 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 }
1736 break;
1737
1738 /* The next five cases all handle BinOps. The main body of code
1739 is the same in each case, but the switch turned inside out to
1740 reuse the code for each type of operator.
1741 */
1742 case expr:
1743 case xor_expr:
1744 case and_expr:
1745 case shift_expr:
1746 case arith_expr:
1747 case term:
1748 if (NCH(n) == 1) {
1749 n = CHILD(n, 0);
1750 goto loop;
1751 }
1752 return ast_for_binop(c, n);
1753 case yield_expr: {
1754 expr_ty exp = NULL;
1755 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001756 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 if (!exp)
1758 return NULL;
1759 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001760 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001762 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 if (NCH(n) == 1) {
1764 n = CHILD(n, 0);
1765 goto loop;
1766 }
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001767 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001768 case power:
1769 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001771 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 return NULL;
1773 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001774 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 return NULL;
1776}
1777
1778static expr_ty
1779ast_for_call(struct compiling *c, const node *n, expr_ty func)
1780{
1781 /*
1782 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1783 | '**' test)
1784 argument: [test '='] test [gen_for] # Really [keyword '='] test
1785 */
1786
1787 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001788 asdl_seq *args;
1789 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 expr_ty vararg = NULL, kwarg = NULL;
1791
1792 REQ(n, arglist);
1793
1794 nargs = 0;
1795 nkeywords = 0;
1796 ngens = 0;
1797 for (i = 0; i < NCH(n); i++) {
1798 node *ch = CHILD(n, i);
1799 if (TYPE(ch) == argument) {
1800 if (NCH(ch) == 1)
1801 nargs++;
1802 else if (TYPE(CHILD(ch, 1)) == gen_for)
1803 ngens++;
1804 else
1805 nkeywords++;
1806 }
1807 }
1808 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001809 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 "if not sole argument");
1811 return NULL;
1812 }
1813
1814 if (nargs + nkeywords + ngens > 255) {
1815 ast_error(n, "more than 255 arguments");
1816 return NULL;
1817 }
1818
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001819 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001821 return NULL;
1822 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001824 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 nargs = 0;
1826 nkeywords = 0;
1827 for (i = 0; i < NCH(n); i++) {
1828 node *ch = CHILD(n, i);
1829 if (TYPE(ch) == argument) {
1830 expr_ty e;
1831 if (NCH(ch) == 1) {
Neal Norwitz5ef92242006-05-19 06:43:50 +00001832 if (nkeywords) {
1833 ast_error(CHILD(ch, 0),
1834 "non-keyword arg after keyword arg");
1835 return NULL;
1836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 e = ast_for_expr(c, CHILD(ch, 0));
1838 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 asdl_seq_SET(args, nargs++, e);
1841 }
1842 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1843 e = ast_for_genexp(c, ch);
1844 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 asdl_seq_SET(args, nargs++, e);
1847 }
1848 else {
1849 keyword_ty kw;
1850 identifier key;
1851
1852 /* CHILD(ch, 0) is test, but must be an identifier? */
1853 e = ast_for_expr(c, CHILD(ch, 0));
1854 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 /* f(lambda x: x[0] = 3) ends up getting parsed with
1857 * LHS test = lambda x: x[0], and RHS test = 3.
1858 * SF bug 132313 points out that complaining about a keyword
1859 * then is very confusing.
1860 */
1861 if (e->kind == Lambda_kind) {
Georg Brandl73c958a2007-06-07 13:23:28 +00001862 ast_error(CHILD(ch, 0),
1863 "lambda cannot contain assignment");
1864 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 } else if (e->kind != Name_kind) {
Georg Brandl73c958a2007-06-07 13:23:28 +00001866 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1867 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 }
1869 key = e->v.Name.id;
Georg Brandl73c958a2007-06-07 13:23:28 +00001870 if (!strcmp(PyString_AS_STRING(key), "None")) {
1871 ast_error(CHILD(ch, 0), "assignment to None");
1872 return NULL;
1873 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 e = ast_for_expr(c, CHILD(ch, 2));
1875 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001876 return NULL;
1877 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001879 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 asdl_seq_SET(keywords, nkeywords++, kw);
1881 }
1882 }
1883 else if (TYPE(ch) == STAR) {
1884 vararg = ast_for_expr(c, CHILD(n, i+1));
Sean Reifscheider4af861c2008-03-20 17:39:31 +00001885 if (!vararg)
1886 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 i++;
1888 }
1889 else if (TYPE(ch) == DOUBLESTAR) {
1890 kwarg = ast_for_expr(c, CHILD(n, i+1));
Sean Reifscheider4af861c2008-03-20 17:39:31 +00001891 if (!kwarg)
1892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 i++;
1894 }
1895 }
1896
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001897 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898}
1899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001901ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001903 /* testlist_gexp: test (',' test)* [','] */
1904 /* testlist: test (',' test)* [','] */
1905 /* testlist_safe: test (',' test)+ [','] */
1906 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001908 if (TYPE(n) == testlist_gexp) {
1909 if (NCH(n) > 1)
1910 assert(TYPE(CHILD(n, 1)) != gen_for);
1911 }
1912 else {
1913 assert(TYPE(n) == testlist ||
1914 TYPE(n) == testlist_safe ||
1915 TYPE(n) == testlist1);
1916 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 if (NCH(n) == 1)
1918 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 else {
1920 asdl_seq *tmp = seq_for_testlist(c, n);
1921 if (!tmp)
1922 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001923 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001925}
1926
1927static expr_ty
1928ast_for_testlist_gexp(struct compiling *c, const node* n)
1929{
1930 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1931 /* argument: test [ gen_for ] */
1932 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001933 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001934 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001935 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001936}
1937
1938/* like ast_for_testlist() but returns a sequence */
1939static asdl_seq*
1940ast_for_class_bases(struct compiling *c, const node* n)
1941{
1942 /* testlist: test (',' test)* [','] */
1943 assert(NCH(n) > 0);
1944 REQ(n, testlist);
1945 if (NCH(n) == 1) {
1946 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001947 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001948 if (!bases)
1949 return NULL;
1950 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001951 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001952 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001953 asdl_seq_SET(bases, 0, base);
1954 return bases;
1955 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001956
1957 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958}
1959
1960static stmt_ty
1961ast_for_expr_stmt(struct compiling *c, const node *n)
1962{
1963 REQ(n, expr_stmt);
1964 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1965 | ('=' (yield_expr|testlist))*)
1966 testlist: test (',' test)* [',']
1967 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1968 | '<<=' | '>>=' | '**=' | '//='
1969 test: ... here starts the operator precendence dance
1970 */
1971
1972 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001973 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 if (!e)
1975 return NULL;
1976
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001977 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978 }
1979 else if (TYPE(CHILD(n, 1)) == augassign) {
1980 expr_ty expr1, expr2;
Anthony Baxtera863d332006-04-11 07:43:46 +00001981 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 node *ch = CHILD(n, 0);
1983
Neal Norwitz0d62a062006-07-30 06:53:31 +00001984 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 if (!expr1)
1986 return NULL;
Neil Schemenauer0e07b602006-07-09 16:16:34 +00001987 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001988 switch (expr1->kind) {
1989 case GeneratorExp_kind:
1990 ast_error(ch, "augmented assignment to generator "
1991 "expression not possible");
1992 return NULL;
Neal Norwitz0d62a062006-07-30 06:53:31 +00001993 case Yield_kind:
1994 ast_error(ch, "augmented assignment to yield "
1995 "expression not possible");
1996 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001997 case Name_kind: {
1998 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1999 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2000 ast_error(ch, "assignment to None");
2001 return NULL;
2002 }
2003 break;
2004 }
2005 case Attribute_kind:
2006 case Subscript_kind:
2007 break;
2008 default:
2009 ast_error(ch, "illegal expression for augmented "
2010 "assignment");
2011 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 }
Collin Winter7d9ac782007-03-16 04:12:48 +00002013 if (!set_context(expr1, Store, ch))
2014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015
2016 ch = CHILD(n, 2);
2017 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002018 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 else
Neal Norwitz0d62a062006-07-30 06:53:31 +00002020 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002021 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 return NULL;
2023
Anthony Baxtera863d332006-04-11 07:43:46 +00002024 newoperator = ast_for_augassign(CHILD(n, 1));
2025 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026 return NULL;
2027
Anthony Baxtera863d332006-04-11 07:43:46 +00002028 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 }
2030 else {
2031 int i;
2032 asdl_seq *targets;
2033 node *value;
2034 expr_ty expression;
2035
2036 /* a normal assignment */
2037 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002038 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 if (!targets)
2040 return NULL;
2041 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002042 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 node *ch = CHILD(n, i);
2044 if (TYPE(ch) == yield_expr) {
2045 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002046 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002048 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049
2050 /* set context to assign */
2051 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002052 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053
Neal Norwitz84456bd2005-12-18 03:16:20 +00002054 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002055 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056
2057 asdl_seq_SET(targets, i / 2, e);
2058 }
2059 value = CHILD(n, NCH(n) - 1);
2060 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002061 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062 else
2063 expression = ast_for_expr(c, value);
2064 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002066 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068}
2069
2070static stmt_ty
2071ast_for_print_stmt(struct compiling *c, const node *n)
2072{
2073 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2074 | '>>' test [ (',' test)+ [','] ] )
2075 */
2076 expr_ty dest = NULL, expression;
2077 asdl_seq *seq;
2078 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002079 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080
2081 REQ(n, print_stmt);
2082 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2083 dest = ast_for_expr(c, CHILD(n, 2));
2084 if (!dest)
2085 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002086 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002088 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002090 return NULL;
2091 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002093 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002095 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 }
2097 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002098 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099}
2100
2101static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002102ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103{
2104 asdl_seq *seq;
2105 int i;
2106 expr_ty e;
2107
2108 REQ(n, exprlist);
2109
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002110 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 if (!seq)
2112 return NULL;
2113 for (i = 0; i < NCH(n); i += 2) {
2114 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002115 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002116 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002117 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002118 if (context && !set_context(e, context, CHILD(n, i)))
2119 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 }
2121 return seq;
2122}
2123
2124static stmt_ty
2125ast_for_del_stmt(struct compiling *c, const node *n)
2126{
2127 asdl_seq *expr_list;
2128
2129 /* del_stmt: 'del' exprlist */
2130 REQ(n, del_stmt);
2131
2132 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2133 if (!expr_list)
2134 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002135 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136}
2137
2138static stmt_ty
2139ast_for_flow_stmt(struct compiling *c, const node *n)
2140{
2141 /*
2142 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2143 | yield_stmt
2144 break_stmt: 'break'
2145 continue_stmt: 'continue'
2146 return_stmt: 'return' [testlist]
2147 yield_stmt: yield_expr
2148 yield_expr: 'yield' testlist
2149 raise_stmt: 'raise' [test [',' test [',' test]]]
2150 */
2151 node *ch;
2152
2153 REQ(n, flow_stmt);
2154 ch = CHILD(n, 0);
2155 switch (TYPE(ch)) {
2156 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002157 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002159 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 case yield_stmt: { /* will reduce to yield_expr */
2161 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2162 if (!exp)
2163 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002164 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 }
2166 case return_stmt:
2167 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002168 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002170 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 if (!expression)
2172 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002173 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 }
2175 case raise_stmt:
2176 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002177 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 else if (NCH(ch) == 2) {
2179 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2180 if (!expression)
2181 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002182 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 }
2184 else if (NCH(ch) == 4) {
2185 expr_ty expr1, expr2;
2186
2187 expr1 = ast_for_expr(c, CHILD(ch, 1));
2188 if (!expr1)
2189 return NULL;
2190 expr2 = ast_for_expr(c, CHILD(ch, 3));
2191 if (!expr2)
2192 return NULL;
2193
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002194 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 }
2196 else if (NCH(ch) == 6) {
2197 expr_ty expr1, expr2, expr3;
2198
2199 expr1 = ast_for_expr(c, CHILD(ch, 1));
2200 if (!expr1)
2201 return NULL;
2202 expr2 = ast_for_expr(c, CHILD(ch, 3));
2203 if (!expr2)
2204 return NULL;
2205 expr3 = ast_for_expr(c, CHILD(ch, 5));
2206 if (!expr3)
2207 return NULL;
2208
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002209 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 }
2211 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002212 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 "unexpected flow_stmt: %d", TYPE(ch));
2214 return NULL;
2215 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002216
2217 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2218 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219}
2220
2221static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002222alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223{
2224 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002225 import_as_name: NAME ['as' NAME]
2226 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 dotted_name: NAME ('.' NAME)*
2228 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002229 PyObject *str;
2230
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231 loop:
2232 switch (TYPE(n)) {
2233 case import_as_name:
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002234 str = NULL;
2235 if (NCH(n) == 3) {
2236 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2237 ast_error(n, "must use 'as' in import");
2238 return NULL;
2239 }
2240 str = NEW_IDENTIFIER(CHILD(n, 2));
2241 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002242 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 case dotted_as_name:
2244 if (NCH(n) == 1) {
2245 n = CHILD(n, 0);
2246 goto loop;
2247 }
2248 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002249 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00002250 if (!a)
2251 return NULL;
Neal Norwitzfb48afa2006-07-08 05:31:37 +00002252 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2253 ast_error(n, "must use 'as' in import");
2254 return NULL;
2255 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 assert(!a->asname);
2257 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2258 return a;
2259 }
2260 break;
2261 case dotted_name:
2262 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002263 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 else {
2265 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002266 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002267 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 char *s;
2269
2270 len = 0;
2271 for (i = 0; i < NCH(n); i += 2)
2272 /* length of string plus one for the dot */
2273 len += strlen(STR(CHILD(n, i))) + 1;
2274 len--; /* the last name doesn't have a dot */
2275 str = PyString_FromStringAndSize(NULL, len);
2276 if (!str)
2277 return NULL;
2278 s = PyString_AS_STRING(str);
2279 if (!s)
2280 return NULL;
2281 for (i = 0; i < NCH(n); i += 2) {
2282 char *sch = STR(CHILD(n, i));
2283 strcpy(s, STR(CHILD(n, i)));
2284 s += strlen(sch);
2285 *s++ = '.';
2286 }
2287 --s;
2288 *s = '\0';
2289 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002290 PyArena_AddPyObject(c->c_arena, str);
2291 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 }
2293 break;
2294 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002295 str = PyString_InternFromString("*");
2296 PyArena_AddPyObject(c->c_arena, str);
2297 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002299 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 "unexpected import name: %d", TYPE(n));
2301 return NULL;
2302 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002303
2304 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 return NULL;
2306}
2307
2308static stmt_ty
2309ast_for_import_stmt(struct compiling *c, const node *n)
2310{
2311 /*
2312 import_stmt: import_name | import_from
2313 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002314 import_from: 'from' ('.'* dotted_name | '.') 'import'
2315 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002317 int lineno;
2318 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319 int i;
2320 asdl_seq *aliases;
2321
2322 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002323 lineno = LINENO(n);
2324 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002326 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002328 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002329 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 if (!aliases)
2331 return NULL;
2332 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002333 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002334 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 asdl_seq_SET(aliases, i / 2, import_alias);
2337 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002338 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002340 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002342 int idx, ndots = 0;
2343 alias_ty mod = NULL;
2344 identifier modname;
2345
2346 /* Count the number of dots (for relative imports) and check for the
2347 optional module name */
2348 for (idx = 1; idx < NCH(n); idx++) {
2349 if (TYPE(CHILD(n, idx)) == dotted_name) {
2350 mod = alias_for_import_name(c, CHILD(n, idx));
2351 idx++;
2352 break;
2353 } else if (TYPE(CHILD(n, idx)) != DOT) {
2354 break;
2355 }
2356 ndots++;
2357 }
2358 idx++; /* skip over the 'import' keyword */
2359 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002360 case STAR:
2361 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002362 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002363 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002364 if (ndots) {
2365 ast_error(n, "'import *' not allowed with 'from .'");
2366 return NULL;
2367 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002368 break;
2369 case LPAR:
2370 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002371 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002372 n_children = NCH(n);
2373 break;
2374 case import_as_names:
2375 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002376 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002377 n_children = NCH(n);
2378 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 ast_error(n, "trailing comma not allowed without"
2380 " surrounding parentheses");
2381 return NULL;
2382 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002383 break;
2384 default:
2385 ast_error(n, "Unexpected node-type in from-import");
2386 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002387 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002389 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002390 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392
2393 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002394 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002395 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002396 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002398 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002400 else {
2401 for (i = 0; i < NCH(n); i += 2) {
2402 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2403 if (!import_alias)
2404 return NULL;
2405 asdl_seq_SET(aliases, i / 2, import_alias);
2406 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002408 if (mod != NULL)
2409 modname = mod->name;
2410 else
2411 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002412 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002413 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 }
Neal Norwitz79792652005-11-14 04:25:03 +00002415 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 "unknown import statement: starts with command '%s'",
2417 STR(CHILD(n, 0)));
2418 return NULL;
2419}
2420
2421static stmt_ty
2422ast_for_global_stmt(struct compiling *c, const node *n)
2423{
2424 /* global_stmt: 'global' NAME (',' NAME)* */
2425 identifier name;
2426 asdl_seq *s;
2427 int i;
2428
2429 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002430 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 if (!s)
2432 return NULL;
2433 for (i = 1; i < NCH(n); i += 2) {
2434 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002435 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 asdl_seq_SET(s, i / 2, name);
2438 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002439 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440}
2441
2442static stmt_ty
2443ast_for_exec_stmt(struct compiling *c, const node *n)
2444{
2445 expr_ty expr1, globals = NULL, locals = NULL;
2446 int n_children = NCH(n);
2447 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002448 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 "poorly formed 'exec' statement: %d parts to statement",
2450 n_children);
2451 return NULL;
2452 }
2453
2454 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2455 REQ(n, exec_stmt);
2456 expr1 = ast_for_expr(c, CHILD(n, 1));
2457 if (!expr1)
2458 return NULL;
2459 if (n_children >= 4) {
2460 globals = ast_for_expr(c, CHILD(n, 3));
2461 if (!globals)
2462 return NULL;
2463 }
2464 if (n_children == 6) {
2465 locals = ast_for_expr(c, CHILD(n, 5));
2466 if (!locals)
2467 return NULL;
2468 }
2469
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002470 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471}
2472
2473static stmt_ty
2474ast_for_assert_stmt(struct compiling *c, const node *n)
2475{
2476 /* assert_stmt: 'assert' test [',' test] */
2477 REQ(n, assert_stmt);
2478 if (NCH(n) == 2) {
2479 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2480 if (!expression)
2481 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002482 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 }
2484 else if (NCH(n) == 4) {
2485 expr_ty expr1, expr2;
2486
2487 expr1 = ast_for_expr(c, CHILD(n, 1));
2488 if (!expr1)
2489 return NULL;
2490 expr2 = ast_for_expr(c, CHILD(n, 3));
2491 if (!expr2)
2492 return NULL;
2493
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002494 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 }
Neal Norwitz79792652005-11-14 04:25:03 +00002496 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 "improper number of parts to 'assert' statement: %d",
2498 NCH(n));
2499 return NULL;
2500}
2501
2502static asdl_seq *
2503ast_for_suite(struct compiling *c, const node *n)
2504{
2505 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002506 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 stmt_ty s;
2508 int i, total, num, end, pos = 0;
2509 node *ch;
2510
2511 REQ(n, suite);
2512
2513 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002514 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 if (!seq)
2516 return NULL;
2517 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2518 n = CHILD(n, 0);
2519 /* simple_stmt always ends with a NEWLINE,
2520 and may have a trailing SEMI
2521 */
2522 end = NCH(n) - 1;
2523 if (TYPE(CHILD(n, end - 1)) == SEMI)
2524 end--;
2525 /* loop by 2 to skip semi-colons */
2526 for (i = 0; i < end; i += 2) {
2527 ch = CHILD(n, i);
2528 s = ast_for_stmt(c, ch);
2529 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002530 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 asdl_seq_SET(seq, pos++, s);
2532 }
2533 }
2534 else {
2535 for (i = 2; i < (NCH(n) - 1); i++) {
2536 ch = CHILD(n, i);
2537 REQ(ch, stmt);
2538 num = num_stmts(ch);
2539 if (num == 1) {
2540 /* small_stmt or compound_stmt with only one child */
2541 s = ast_for_stmt(c, ch);
2542 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002543 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 asdl_seq_SET(seq, pos++, s);
2545 }
2546 else {
2547 int j;
2548 ch = CHILD(ch, 0);
2549 REQ(ch, simple_stmt);
2550 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002551 /* statement terminates with a semi-colon ';' */
2552 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002553 assert((j + 1) == NCH(ch));
2554 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002555 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 s = ast_for_stmt(c, CHILD(ch, j));
2557 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002558 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 asdl_seq_SET(seq, pos++, s);
2560 }
2561 }
2562 }
2563 }
2564 assert(pos == seq->size);
2565 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566}
2567
2568static stmt_ty
2569ast_for_if_stmt(struct compiling *c, const node *n)
2570{
2571 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2572 ['else' ':' suite]
2573 */
2574 char *s;
2575
2576 REQ(n, if_stmt);
2577
2578 if (NCH(n) == 4) {
2579 expr_ty expression;
2580 asdl_seq *suite_seq;
2581
2582 expression = ast_for_expr(c, CHILD(n, 1));
2583 if (!expression)
2584 return NULL;
2585 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002586 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 return NULL;
2588
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002589 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 s = STR(CHILD(n, 4));
2593 /* s[2], the third character in the string, will be
2594 's' for el_s_e, or
2595 'i' for el_i_f
2596 */
2597 if (s[2] == 's') {
2598 expr_ty expression;
2599 asdl_seq *seq1, *seq2;
2600
2601 expression = ast_for_expr(c, CHILD(n, 1));
2602 if (!expression)
2603 return NULL;
2604 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002605 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 return NULL;
2607 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002608 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 return NULL;
2610
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002611 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 }
2613 else if (s[2] == 'i') {
2614 int i, n_elif, has_else = 0;
Collin Winter7d9ac782007-03-16 04:12:48 +00002615 expr_ty expression;
2616 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 asdl_seq *orelse = NULL;
2618 n_elif = NCH(n) - 4;
2619 /* must reference the child n_elif+1 since 'else' token is third,
2620 not fourth, child from the end. */
2621 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2622 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2623 has_else = 1;
2624 n_elif -= 3;
2625 }
2626 n_elif /= 4;
2627
2628 if (has_else) {
Collin Winter7d9ac782007-03-16 04:12:48 +00002629 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002631 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 if (!orelse)
2633 return NULL;
2634 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002635 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
Collin Winter7d9ac782007-03-16 04:12:48 +00002637 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2638 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 return NULL;
Collin Winter7d9ac782007-03-16 04:12:48 +00002640 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2641 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643
Collin Winter7d9ac782007-03-16 04:12:48 +00002644 asdl_seq_SET(orelse, 0, If(expression, suite_seq, suite_seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002645 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002646 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 /* the just-created orelse handled the last elif */
2648 n_elif--;
2649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650
2651 for (i = 0; i < n_elif; i++) {
2652 int off = 5 + (n_elif - i - 1) * 4;
Anthony Baxtera863d332006-04-11 07:43:46 +00002653 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2654 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 return NULL;
2656 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002657 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002660 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662
Anthony Baxtera863d332006-04-11 07:43:46 +00002663 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002665 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Anthony Baxtera863d332006-04-11 07:43:46 +00002666 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 }
Collin Winter7d9ac782007-03-16 04:12:48 +00002668 expression = ast_for_expr(c, CHILD(n, 1));
2669 if (!expression)
2670 return NULL;
2671 suite_seq = ast_for_suite(c, CHILD(n, 3));
2672 if (!suite_seq)
2673 return NULL;
2674 return If(expression, suite_seq, orelse,
2675 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002677
2678 PyErr_Format(PyExc_SystemError,
2679 "unexpected token in 'if' statement: %s", s);
2680 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681}
2682
2683static stmt_ty
2684ast_for_while_stmt(struct compiling *c, const node *n)
2685{
2686 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2687 REQ(n, while_stmt);
2688
2689 if (NCH(n) == 4) {
2690 expr_ty expression;
2691 asdl_seq *suite_seq;
2692
2693 expression = ast_for_expr(c, CHILD(n, 1));
2694 if (!expression)
2695 return NULL;
2696 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002697 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002699 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 }
2701 else if (NCH(n) == 7) {
2702 expr_ty expression;
2703 asdl_seq *seq1, *seq2;
2704
2705 expression = ast_for_expr(c, CHILD(n, 1));
2706 if (!expression)
2707 return NULL;
2708 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002709 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 return NULL;
2711 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002712 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 return NULL;
2714
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002715 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002717
2718 PyErr_Format(PyExc_SystemError,
2719 "wrong number of tokens for 'while' statement: %d",
2720 NCH(n));
2721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722}
2723
2724static stmt_ty
2725ast_for_for_stmt(struct compiling *c, const node *n)
2726{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002727 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 expr_ty expression;
2729 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002730 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2732 REQ(n, for_stmt);
2733
2734 if (NCH(n) == 9) {
2735 seq = ast_for_suite(c, CHILD(n, 8));
2736 if (!seq)
2737 return NULL;
2738 }
2739
Neal Norwitzedef2be2006-07-12 05:26:17 +00002740 node_target = CHILD(n, 1);
2741 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002742 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002744 /* Check the # of children rather than the length of _target, since
2745 for x, in ... has 1 element in _target, but still requires a Tuple. */
2746 if (NCH(node_target) == 1)
Martin v. Löwis28457502006-04-11 09:17:27 +00002747 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002749 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002751 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002752 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 return NULL;
2754 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002755 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 return NULL;
2757
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002758 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2759 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760}
2761
2762static excepthandler_ty
2763ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2764{
2765 /* except_clause: 'except' [test [',' test]] */
2766 REQ(exc, except_clause);
2767 REQ(body, suite);
2768
2769 if (NCH(exc) == 1) {
2770 asdl_seq *suite_seq = ast_for_suite(c, body);
2771 if (!suite_seq)
2772 return NULL;
2773
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002774 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2775 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 }
2777 else if (NCH(exc) == 2) {
2778 expr_ty expression;
2779 asdl_seq *suite_seq;
2780
2781 expression = ast_for_expr(c, CHILD(exc, 1));
2782 if (!expression)
2783 return NULL;
2784 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002785 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 return NULL;
2787
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002788 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2789 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 }
2791 else if (NCH(exc) == 4) {
2792 asdl_seq *suite_seq;
2793 expr_ty expression;
2794 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2795 if (!e)
2796 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002797 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 return NULL;
2799 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002800 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 return NULL;
2802 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002803 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 return NULL;
2805
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002806 return excepthandler(expression, e, suite_seq, LINENO(exc),
2807 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002809
2810 PyErr_Format(PyExc_SystemError,
2811 "wrong number of children for 'except' clause: %d",
2812 NCH(exc));
2813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814}
2815
2816static stmt_ty
2817ast_for_try_stmt(struct compiling *c, const node *n)
2818{
Neal Norwitzf599f422005-12-17 21:33:47 +00002819 const int nch = NCH(n);
2820 int n_except = (nch - 3)/3;
2821 asdl_seq *body, *orelse = NULL, *finally = NULL;
2822
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 REQ(n, try_stmt);
2824
Neal Norwitzf599f422005-12-17 21:33:47 +00002825 body = ast_for_suite(c, CHILD(n, 2));
2826 if (body == NULL)
2827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828
Neal Norwitzf599f422005-12-17 21:33:47 +00002829 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2830 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2831 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2832 /* we can assume it's an "else",
2833 because nch >= 9 for try-else-finally and
2834 it would otherwise have a type of except_clause */
2835 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2836 if (orelse == NULL)
2837 return NULL;
2838 n_except--;
2839 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840
Neal Norwitzf599f422005-12-17 21:33:47 +00002841 finally = ast_for_suite(c, CHILD(n, nch - 1));
2842 if (finally == NULL)
2843 return NULL;
2844 n_except--;
2845 }
2846 else {
2847 /* we can assume it's an "else",
2848 otherwise it would have a type of except_clause */
2849 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2850 if (orelse == NULL)
2851 return NULL;
2852 n_except--;
2853 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002855 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002856 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 return NULL;
2858 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002859
2860 if (n_except > 0) {
2861 int i;
2862 stmt_ty except_st;
2863 /* process except statements to create a try ... except */
2864 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2865 if (handlers == NULL)
2866 return NULL;
2867
2868 for (i = 0; i < n_except; i++) {
2869 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2870 CHILD(n, 5 + i * 3));
2871 if (!e)
2872 return NULL;
2873 asdl_seq_SET(handlers, i, e);
2874 }
2875
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002876 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2877 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002878 if (!finally)
2879 return except_st;
2880
2881 /* if a 'finally' is present too, we nest the TryExcept within a
2882 TryFinally to emulate try ... except ... finally */
2883 body = asdl_seq_new(1, c->c_arena);
2884 if (body == NULL)
2885 return NULL;
2886 asdl_seq_SET(body, 0, except_st);
2887 }
2888
2889 /* must be a try ... finally (except clauses are in body, if any exist) */
2890 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002891 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892}
2893
Guido van Rossumc2e20742006-02-27 22:32:47 +00002894static expr_ty
2895ast_for_with_var(struct compiling *c, const node *n)
2896{
2897 REQ(n, with_var);
2898 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2899 ast_error(n, "expected \"with [expr] as [var]\"");
2900 return NULL;
2901 }
2902 return ast_for_expr(c, CHILD(n, 1));
2903}
2904
2905/* with_stmt: 'with' test [ with_var ] ':' suite */
2906static stmt_ty
2907ast_for_with_stmt(struct compiling *c, const node *n)
2908{
2909 expr_ty context_expr, optional_vars = NULL;
2910 int suite_index = 3; /* skip 'with', test, and ':' */
2911 asdl_seq *suite_seq;
2912
2913 assert(TYPE(n) == with_stmt);
2914 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter7d9ac782007-03-16 04:12:48 +00002915 if (!context_expr)
2916 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002917 if (TYPE(CHILD(n, 2)) == with_var) {
2918 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2919
2920 if (!optional_vars) {
2921 return NULL;
2922 }
2923 if (!set_context(optional_vars, Store, n)) {
2924 return NULL;
2925 }
2926 suite_index = 4;
2927 }
2928
2929 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2930 if (!suite_seq) {
2931 return NULL;
2932 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002933 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2934 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002935}
2936
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937static stmt_ty
2938ast_for_classdef(struct compiling *c, const node *n)
2939{
2940 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941 asdl_seq *bases, *s;
2942
2943 REQ(n, classdef);
2944
2945 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2946 ast_error(n, "assignment to None");
2947 return NULL;
2948 }
2949
2950 if (NCH(n) == 4) {
2951 s = ast_for_suite(c, CHILD(n, 3));
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 /* check for empty base list */
2958 if (TYPE(CHILD(n,3)) == RPAR) {
2959 s = ast_for_suite(c, CHILD(n,5));
2960 if (!s)
2961 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002962 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2963 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 }
2965
2966 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002967 bases = ast_for_class_bases(c, CHILD(n, 3));
2968 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970
2971 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002972 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002974 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2975 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976}
2977
2978static stmt_ty
2979ast_for_stmt(struct compiling *c, const node *n)
2980{
2981 if (TYPE(n) == stmt) {
2982 assert(NCH(n) == 1);
2983 n = CHILD(n, 0);
2984 }
2985 if (TYPE(n) == simple_stmt) {
2986 assert(num_stmts(n) == 1);
2987 n = CHILD(n, 0);
2988 }
2989 if (TYPE(n) == small_stmt) {
2990 REQ(n, small_stmt);
2991 n = CHILD(n, 0);
2992 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2993 | flow_stmt | import_stmt | global_stmt | exec_stmt
2994 | assert_stmt
2995 */
2996 switch (TYPE(n)) {
2997 case expr_stmt:
2998 return ast_for_expr_stmt(c, n);
2999 case print_stmt:
3000 return ast_for_print_stmt(c, n);
3001 case del_stmt:
3002 return ast_for_del_stmt(c, n);
3003 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003004 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005 case flow_stmt:
3006 return ast_for_flow_stmt(c, n);
3007 case import_stmt:
3008 return ast_for_import_stmt(c, n);
3009 case global_stmt:
3010 return ast_for_global_stmt(c, n);
3011 case exec_stmt:
3012 return ast_for_exec_stmt(c, n);
3013 case assert_stmt:
3014 return ast_for_assert_stmt(c, n);
3015 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003016 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3018 TYPE(n), NCH(n));
3019 return NULL;
3020 }
3021 }
3022 else {
3023 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3024 | funcdef | classdef
3025 */
3026 node *ch = CHILD(n, 0);
3027 REQ(n, compound_stmt);
3028 switch (TYPE(ch)) {
3029 case if_stmt:
3030 return ast_for_if_stmt(c, ch);
3031 case while_stmt:
3032 return ast_for_while_stmt(c, ch);
3033 case for_stmt:
3034 return ast_for_for_stmt(c, ch);
3035 case try_stmt:
3036 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003037 case with_stmt:
3038 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 case funcdef:
3040 return ast_for_funcdef(c, ch);
3041 case classdef:
3042 return ast_for_classdef(c, ch);
3043 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003044 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3046 TYPE(n), NCH(n));
3047 return NULL;
3048 }
3049 }
3050}
3051
3052static PyObject *
3053parsenumber(const char *s)
3054{
3055 const char *end;
3056 long x;
3057 double dx;
3058#ifndef WITHOUT_COMPLEX
3059 Py_complex c;
3060 int imflag;
3061#endif
3062
3063 errno = 0;
3064 end = s + strlen(s) - 1;
3065#ifndef WITHOUT_COMPLEX
3066 imflag = *end == 'j' || *end == 'J';
3067#endif
3068 if (*end == 'l' || *end == 'L')
3069 return PyLong_FromString((char *)s, (char **)0, 0);
3070 if (s[0] == '0') {
3071 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3072 if (x < 0 && errno == 0) {
3073 return PyLong_FromString((char *)s,
3074 (char **)0,
3075 0);
3076 }
3077 }
3078 else
3079 x = PyOS_strtol((char *)s, (char **)&end, 0);
3080 if (*end == '\0') {
3081 if (errno != 0)
3082 return PyLong_FromString((char *)s, (char **)0, 0);
3083 return PyInt_FromLong(x);
3084 }
3085 /* XXX Huge floats may silently fail */
3086#ifndef WITHOUT_COMPLEX
3087 if (imflag) {
3088 c.real = 0.;
3089 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003090 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 PyFPE_END_PROTECT(c)
3092 return PyComplex_FromCComplex(c);
3093 }
3094 else
3095#endif
3096 {
3097 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003098 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003099 PyFPE_END_PROTECT(dx)
3100 return PyFloat_FromDouble(dx);
3101 }
3102}
3103
3104static PyObject *
3105decode_utf8(const char **sPtr, const char *end, char* encoding)
3106{
3107#ifndef Py_USING_UNICODE
3108 Py_FatalError("decode_utf8 should not be called in this build.");
3109 return NULL;
3110#else
3111 PyObject *u, *v;
3112 char *s, *t;
3113 t = s = (char *)*sPtr;
3114 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3115 while (s < end && (*s & 0x80)) s++;
3116 *sPtr = s;
3117 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3118 if (u == NULL)
3119 return NULL;
3120 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3121 Py_DECREF(u);
3122 return v;
3123#endif
3124}
3125
Georg Brandleec47f32007-08-23 18:08:33 +00003126#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127static PyObject *
3128decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3129{
3130 PyObject *v, *u;
3131 char *buf;
3132 char *p;
3133 const char *end;
3134 if (encoding == NULL) {
3135 buf = (char *)s;
3136 u = NULL;
3137 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3138 buf = (char *)s;
3139 u = NULL;
3140 } else {
Martin v. Löwis73c01d42008-02-14 11:26:18 +00003141 /* check for integer overflow */
3142 if (len > PY_SIZE_MAX / 4)
3143 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3145 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3146 if (u == NULL)
3147 return NULL;
3148 p = buf = PyString_AsString(u);
3149 end = s + len;
3150 while (s < end) {
3151 if (*s == '\\') {
3152 *p++ = *s++;
3153 if (*s & 0x80) {
3154 strcpy(p, "u005c");
3155 p += 5;
3156 }
3157 }
3158 if (*s & 0x80) { /* XXX inefficient */
3159 PyObject *w;
3160 char *r;
Martin v. Löwis66851282006-04-22 11:40:03 +00003161 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 w = decode_utf8(&s, end, "utf-16-be");
3163 if (w == NULL) {
3164 Py_DECREF(u);
3165 return NULL;
3166 }
3167 r = PyString_AsString(w);
3168 rn = PyString_Size(w);
3169 assert(rn % 2 == 0);
3170 for (i = 0; i < rn; i += 2) {
3171 sprintf(p, "\\u%02x%02x",
3172 r[i + 0] & 0xFF,
3173 r[i + 1] & 0xFF);
3174 p += 6;
3175 }
3176 Py_DECREF(w);
3177 } else {
3178 *p++ = *s++;
3179 }
3180 }
3181 len = p - buf;
3182 s = buf;
3183 }
3184 if (rawmode)
3185 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3186 else
3187 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3188 Py_XDECREF(u);
3189 return v;
3190}
Georg Brandleec47f32007-08-23 18:08:33 +00003191#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192
3193/* s is a Python string literal, including the bracketing quote characters,
3194 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3195 * parsestr parses it, and returns the decoded Python string object.
3196 */
3197static PyObject *
3198parsestr(const char *s, const char *encoding)
3199{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003201 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 int rawmode = 0;
3203 int need_encoding;
3204 int unicode = 0;
3205
3206 if (isalpha(quote) || quote == '_') {
3207 if (quote == 'u' || quote == 'U') {
3208 quote = *++s;
3209 unicode = 1;
3210 }
3211 if (quote == 'r' || quote == 'R') {
3212 quote = *++s;
3213 rawmode = 1;
3214 }
3215 }
3216 if (quote != '\'' && quote != '\"') {
3217 PyErr_BadInternalCall();
3218 return NULL;
3219 }
3220 s++;
3221 len = strlen(s);
3222 if (len > INT_MAX) {
3223 PyErr_SetString(PyExc_OverflowError,
3224 "string to parse is too long");
3225 return NULL;
3226 }
3227 if (s[--len] != quote) {
3228 PyErr_BadInternalCall();
3229 return NULL;
3230 }
3231 if (len >= 4 && s[0] == quote && s[1] == quote) {
3232 s += 2;
3233 len -= 2;
3234 if (s[--len] != quote || s[--len] != quote) {
3235 PyErr_BadInternalCall();
3236 return NULL;
3237 }
3238 }
3239#ifdef Py_USING_UNICODE
3240 if (unicode || Py_UnicodeFlag) {
3241 return decode_unicode(s, len, rawmode, encoding);
3242 }
3243#endif
3244 need_encoding = (encoding != NULL &&
3245 strcmp(encoding, "utf-8") != 0 &&
3246 strcmp(encoding, "iso-8859-1") != 0);
3247 if (rawmode || strchr(s, '\\') == NULL) {
3248 if (need_encoding) {
3249#ifndef Py_USING_UNICODE
3250 /* This should not happen - we never see any other
3251 encoding. */
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003252 Py_FatalError(
3253 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003255 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 if (u == NULL)
3257 return NULL;
3258 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3259 Py_DECREF(u);
3260 return v;
3261#endif
3262 } else {
3263 return PyString_FromStringAndSize(s, len);
3264 }
3265 }
3266
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003267 return PyString_DecodeEscape(s, len, NULL, unicode,
3268 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269}
3270
3271/* Build a Python string object out of a STRING atom. This takes care of
3272 * compile-time literal catenation, calling parsestr() on each piece, and
3273 * pasting the intermediate results together.
3274 */
3275static PyObject *
3276parsestrplus(struct compiling *c, const node *n)
3277{
3278 PyObject *v;
3279 int i;
3280 REQ(CHILD(n, 0), STRING);
3281 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3282 /* String literal concatenation */
3283 for (i = 1; i < NCH(n); i++) {
3284 PyObject *s;
3285 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3286 if (s == NULL)
3287 goto onError;
3288 if (PyString_Check(v) && PyString_Check(s)) {
3289 PyString_ConcatAndDel(&v, s);
3290 if (v == NULL)
3291 goto onError;
3292 }
3293#ifdef Py_USING_UNICODE
3294 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003295 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 Py_DECREF(v);
3298 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003299 if (v == NULL)
3300 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301 }
3302#endif
3303 }
3304 }
3305 return v;
3306
3307 onError:
3308 Py_XDECREF(v);
3309 return NULL;
3310}