blob: 672a715e24a95710a1a04f7e15bdc7687d705bd2 [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 *);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 }
Thomas Wouters477c8d52006-05-27 19:21:47 +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";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000252 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 }
254 else {
255 n = CHILD(n, 0);
256 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000257 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 if (!stmts)
259 goto error;
260 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000261 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 if (!s)
263 goto error;
264 asdl_seq_SET(stmts, 0, s);
265 }
266 else {
267 /* Only a simple_stmt can contain multiple statements. */
268 REQ(n, simple_stmt);
269 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (TYPE(CHILD(n, i)) == NEWLINE)
271 break;
272 s = ast_for_stmt(&c, CHILD(n, i));
273 if (!s)
274 goto error;
275 asdl_seq_SET(stmts, i / 2, s);
276 }
277 }
278
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000279 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 }
281 default:
282 goto error;
283 }
284 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 ast_error_finish(filename);
286 return NULL;
287}
288
289/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
290*/
291
292static operator_ty
293get_operator(const node *n)
294{
295 switch (TYPE(n)) {
296 case VBAR:
297 return BitOr;
298 case CIRCUMFLEX:
299 return BitXor;
300 case AMPER:
301 return BitAnd;
302 case LEFTSHIFT:
303 return LShift;
304 case RIGHTSHIFT:
305 return RShift;
306 case PLUS:
307 return Add;
308 case MINUS:
309 return Sub;
310 case STAR:
311 return Mult;
312 case SLASH:
313 return Div;
314 case DOUBLESLASH:
315 return FloorDiv;
316 case PERCENT:
317 return Mod;
318 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 }
321}
322
Jeremy Hyltona8293132006-02-28 17:58:27 +0000323/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
325 Only sets context for expr kinds that "can appear in assignment context"
326 (according to ../Parser/Python.asdl). For other expr kinds, it sets
327 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328*/
329
330static int
331set_context(expr_ty e, expr_context_ty ctx, const node *n)
332{
333 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334 /* If a particular expression type can't be used for assign / delete,
335 set expr_name to its name and an error message will be generated.
336 */
337 const char* expr_name = NULL;
338
339 /* The ast defines augmented store and load contexts, but the
340 implementation here doesn't actually use them. The code may be
341 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000342 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000343 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000344 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345 */
346 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 switch (e->kind) {
349 case Attribute_kind:
350 if (ctx == Store &&
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 return ast_error(n, "assignment to None");
353 }
354 e->v.Attribute.ctx = ctx;
355 break;
356 case Subscript_kind:
357 e->v.Subscript.ctx = ctx;
358 break;
359 case Name_kind:
360 if (ctx == Store &&
361 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
362 return ast_error(n, "assignment to None");
363 }
364 e->v.Name.ctx = ctx;
365 break;
366 case List_kind:
367 e->v.List.ctx = ctx;
368 s = e->v.List.elts;
369 break;
370 case Tuple_kind:
371 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
372 return ast_error(n, "can't assign to ()");
373 e->v.Tuple.ctx = ctx;
374 s = e->v.Tuple.elts;
375 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376 case Lambda_kind:
377 expr_name = "lambda";
378 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000384 case UnaryOp_kind:
385 expr_name = "operator";
386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000388 expr_name = "generator expression";
389 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000390 case Yield_kind:
391 expr_name = "yield expression";
392 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000393 case ListComp_kind:
394 expr_name = "list comprehension";
395 break;
396 case Dict_kind:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000397 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398 case Num_kind:
399 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000400 expr_name = "literal";
401 break;
Georg Brandl52318d62006-09-06 07:06:08 +0000402 case Ellipsis_kind:
403 expr_name = "Ellipsis";
404 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000405 case Compare_kind:
406 expr_name = "comparison";
407 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000408 case IfExp_kind:
409 expr_name = "conditional expression";
410 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000411 default:
412 PyErr_Format(PyExc_SystemError,
413 "unexpected expression in assignment %d (line %d)",
414 e->kind, e->lineno);
415 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000417 /* Check for error string set by switch */
418 if (expr_name) {
419 char buf[300];
420 PyOS_snprintf(buf, sizeof(buf),
421 "can't %s %s",
422 ctx == Store ? "assign to" : "delete",
423 expr_name);
424 return ast_error(n, buf);
425 }
426
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltona8293132006-02-28 17:58:27 +0000428 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429 */
430 if (s) {
431 int i;
432
433 for (i = 0; i < asdl_seq_LEN(s); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 return 0;
436 }
437 }
438 return 1;
439}
440
441static operator_ty
442ast_for_augassign(const node *n)
443{
444 REQ(n, augassign);
445 n = CHILD(n, 0);
446 switch (STR(n)[0]) {
447 case '+':
448 return Add;
449 case '-':
450 return Sub;
451 case '/':
452 if (STR(n)[1] == '/')
453 return FloorDiv;
454 else
455 return Div;
456 case '%':
457 return Mod;
458 case '<':
459 return LShift;
460 case '>':
461 return RShift;
462 case '&':
463 return BitAnd;
464 case '^':
465 return BitXor;
466 case '|':
467 return BitOr;
468 case '*':
469 if (STR(n)[1] == '*')
470 return Pow;
471 else
472 return Mult;
473 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000474 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 }
477}
478
479static cmpop_ty
480ast_for_comp_op(const node *n)
481{
Guido van Rossumb053cd82006-08-24 03:53:23 +0000482 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 |'is' 'not'
484 */
485 REQ(n, comp_op);
486 if (NCH(n) == 1) {
487 n = CHILD(n, 0);
488 switch (TYPE(n)) {
489 case LESS:
490 return Lt;
491 case GREATER:
492 return Gt;
493 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494 return Eq;
495 case LESSEQUAL:
496 return LtE;
497 case GREATEREQUAL:
498 return GtE;
499 case NOTEQUAL:
500 return NotEq;
501 case NAME:
502 if (strcmp(STR(n), "in") == 0)
503 return In;
504 if (strcmp(STR(n), "is") == 0)
505 return Is;
506 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000507 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 }
511 }
512 else if (NCH(n) == 2) {
513 /* handle "not in" and "is not" */
514 switch (TYPE(CHILD(n, 0))) {
515 case NAME:
516 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
517 return NotIn;
518 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
519 return IsNot;
520 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000521 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000523 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 }
525 }
Neal Norwitz79792652005-11-14 04:25:03 +0000526 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000528 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529}
530
531static asdl_seq *
532seq_for_testlist(struct compiling *c, const node *n)
533{
534 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000535 asdl_seq *seq;
536 expr_ty expression;
537 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 assert(TYPE(n) == testlist
539 || TYPE(n) == listmaker
540 || TYPE(n) == testlist_gexp
541 || TYPE(n) == testlist_safe
542 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000544 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 if (!seq)
546 return NULL;
547
548 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000549 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
551 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000552 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
555 assert(i / 2 < seq->size);
556 asdl_seq_SET(seq, i / 2, expression);
557 }
558 return seq;
559}
560
561static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000562compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563{
564 int i, len = (NCH(n) + 1) / 2;
565 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000566 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 if (!args)
568 return NULL;
569
570 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 for (i = 0; i < len; i++) {
572 const node *child = CHILD(CHILD(n, 2*i), 0);
573 expr_ty arg;
574 if (TYPE(child) == NAME) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000575 if (!strcmp(STR(child), "None")) {
576 ast_error(child, "assignment to None");
577 return NULL;
578 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
580 child->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000581 }
582 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000583 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hyltona8293132006-02-28 17:58:27 +0000584 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585 asdl_seq_SET(args, i, arg);
586 }
587
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000588 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltona8293132006-02-28 17:58:27 +0000589 if (!set_context(result, Store, n))
590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 return result;
592}
593
Guido van Rossum4f72a782006-10-27 23:31:49 +0000594/* returns -1 if failed to handle keyword only arguments
595 returns new position to keep processing if successful
596 (',' NAME ['=' test])*
597 ^^^
598 start pointing here
599 */
600static int
601handle_keywordonly_args(struct compiling *c, const node *n, int start,
602 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
603{
604 node *ch;
605 expr_ty name;
606 int i = start;
607 int j = 0; /* index for kwdefaults and kwonlyargs */
608 assert(kwonlyargs != NULL);
609 assert(kwdefaults != NULL);
610 while (i < NCH(n)) {
611 ch = CHILD(n, i);
612 switch (TYPE(ch)) {
613 case NAME:
614 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
615 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
616 if (!expression) {
617 ast_error(ch, "assignment to None");
618 goto error;
619 }
620 asdl_seq_SET(kwdefaults, j, expression);
621 i += 2; /* '=' and test */
622 }
623 else { /* setting NULL if no default value exists */
624 asdl_seq_SET(kwdefaults, j, NULL);
625 }
626 if (!strcmp(STR(ch), "None")) {
627 ast_error(ch, "assignment to None");
628 goto error;
629 }
630 name = Name(NEW_IDENTIFIER(ch),
631 Param, LINENO(ch), ch->n_col_offset,
632 c->c_arena);
633 if (!name) {
634 ast_error(ch, "expecting name");
635 goto error;
636 }
637 asdl_seq_SET(kwonlyargs, j++, name);
638 i += 2; /* the name and the comma */
639 break;
640 case DOUBLESTAR:
641 return i;
642 default:
643 ast_error(ch, "unexpected node");
644 goto error;
645 }
646 }
647 return i;
648 error:
649 return -1;
650}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651
Jeremy Hyltona8293132006-02-28 17:58:27 +0000652/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653
654static arguments_ty
655ast_for_arguments(struct compiling *c, const node *n)
656{
657 /* parameters: '(' [varargslist] ')'
Guido van Rossum4f72a782006-10-27 23:31:49 +0000658 varargslist: (fpdef ['=' test] ',')*
659 ('*' [NAME] (',' fpdef ['=' test])* [',' '**' NAME] | '**' NAME)
660 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000662 int i, j, k, nposargs = 0, nkwonlyargs = 0;
663 int nposdefaults = 0, found_default = 0;
664 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 identifier vararg = NULL, kwarg = NULL;
666 node *ch;
667
668 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000669 if (NCH(n) == 2) /* () as argument list */
670 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
671 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 }
673 REQ(n, varargslist);
674
Guido van Rossum4f72a782006-10-27 23:31:49 +0000675 /* first count the number of positional args & defaults */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000677 ch = CHILD(n, i);
678 if (TYPE(ch) == STAR) {
679 if (TYPE(CHILD(n, i+1)) == NAME) {
680 /* skip NAME of vararg */
681 /* so that following can count only keyword only args */
682 i += 2;
683 }
684 else {
685 i++;
686 }
687 break;
688 }
689 if (TYPE(ch) == fpdef) nposargs++;
690 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000692 /* count the number of keyword only args &
693 defaults for keyword only args */
694 for ( ; i < NCH(n); ++i) {
695 ch = CHILD(n, i);
696 if (TYPE(ch) == DOUBLESTAR) break;
697 if (TYPE(ch) == NAME) nkwonlyargs++;
698 }
699
700 posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL);
701 if (!posargs && nposargs)
702 return NULL; /* Don't need to goto error; no objects allocated */
703 kwonlyargs = (nkwonlyargs ?
704 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
705 if (!kwonlyargs && nkwonlyargs)
706 return NULL; /* Don't need to goto error; no objects allocated */
707 posdefaults = (nposdefaults ?
708 asdl_seq_new(nposdefaults, c->c_arena) : NULL);
709 if (!posdefaults && nposdefaults)
710 return NULL; /* Don't need to goto error; no objects allocated */
711 /* The length of kwonlyargs and kwdefaults are same
712 since we set NULL as default for keyword only argument w/o default
713 - we have sequence data structure, but no dictionary */
714 kwdefaults = (nkwonlyargs ?
715 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
716 if (!kwdefaults && nkwonlyargs)
717 return NULL; /* Don't need to goto error; no objects allocated */
718
719 if (nposargs + nkwonlyargs > 255) {
720 ast_error(n, "more than 255 arguments");
721 return NULL;
722 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723
724 /* fpdef: NAME | '(' fplist ')'
725 fplist: fpdef (',' fpdef)* [',']
726 */
727 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000728 j = 0; /* index for defaults */
729 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000731 ch = CHILD(n, i);
732 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733 case fpdef:
734 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
735 anything other than EQUAL or a comma? */
736 /* XXX Should NCH(n) check be made a separate check? */
737 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000738 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
739 if (!expression)
Guido van Rossum4f72a782006-10-27 23:31:49 +0000740 goto error;
741 assert(posdefaults != NULL);
742 asdl_seq_SET(posdefaults, j++, expression);
743
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000745 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000747 else if (found_default) {
748 ast_error(n,
749 "non-default argument follows default argument");
750 goto error;
751 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 if (NCH(ch) == 3) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000753 ch = CHILD(ch, 1);
754 /* def foo((x)): is not complex, special case. */
755 if (NCH(ch) != 1) {
756 /* We have complex arguments, setup for unpacking. */
757 asdl_seq_SET(posargs, k++,
758 compiler_complex_args(c, ch));
759 } else {
760 /* def foo((x)): setup for checking NAME below. */
761 ch = CHILD(ch, 0);
762 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763 }
764 if (TYPE(CHILD(ch, 0)) == NAME) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000765 expr_ty name;
766 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
767 ast_error(CHILD(ch, 0), "assignment to None");
768 goto error;
769 }
Armin Rigo31441302005-10-21 12:57:31 +0000770 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000771 Param, LINENO(ch), ch->n_col_offset,
772 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 if (!name)
774 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000775 asdl_seq_SET(posargs, k++, name);
776
777 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 i += 2; /* the name and the comma */
779 break;
780 case STAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +0000781 if (i+1 >= NCH(n)) {
782 ast_error(CHILD(n, i), "no name for vararg");
783 goto error;
784 }
785 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
786 ast_error(CHILD(n, i+1), "assignment to None");
787 goto error;
788 }
789 if (TYPE(CHILD(n, i+1)) == COMMA) {
790 int res = 0;
791 i += 2; /* now follows keyword only arguments */
792 res = handle_keywordonly_args(c, n, i,
793 kwonlyargs, kwdefaults);
794 if (res == -1) goto error;
795 i = res; /* res has new position to process */
796 }
797 else {
798 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
799 i += 3;
800 if (i < NCH(n) && TYPE(CHILD(n, i)) == NAME) {
801 int res = 0;
802 res = handle_keywordonly_args(c, n, i,
803 kwonlyargs, kwdefaults);
804 if (res == -1) goto error;
805 i = res; /* res has new position to process */
806 }
807 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 break;
809 case DOUBLESTAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +0000810 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
811 ast_error(CHILD(n, i+1), "assignment to None");
812 goto error;
813 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
815 i += 3;
816 break;
817 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000818 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 "unexpected node in varargslist: %d @ %d",
820 TYPE(ch), i);
821 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000822 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000824 return arguments(posargs, vararg, kwonlyargs, kwarg,
825 posdefaults, kwdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000827 Py_XDECREF(vararg);
828 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 return NULL;
830}
831
832static expr_ty
833ast_for_dotted_name(struct compiling *c, const node *n)
834{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000835 expr_ty e;
836 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000837 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 int i;
839
840 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000841
842 lineno = LINENO(n);
843 col_offset = n->n_col_offset;
844
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 id = NEW_IDENTIFIER(CHILD(n, 0));
846 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000847 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000848 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000850 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851
852 for (i = 2; i < NCH(n); i+=2) {
853 id = NEW_IDENTIFIER(CHILD(n, i));
854 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000855 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000856 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000857 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000858 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 }
860
861 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862}
863
864static expr_ty
865ast_for_decorator(struct compiling *c, const node *n)
866{
867 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
868 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000869 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870
871 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000872 REQ(CHILD(n, 0), AT);
873 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874
875 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
876 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000877 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878
879 if (NCH(n) == 3) { /* No arguments */
880 d = name_expr;
881 name_expr = NULL;
882 }
883 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000884 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
885 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 name_expr = NULL;
889 }
890 else {
891 d = ast_for_call(c, CHILD(n, 3), name_expr);
892 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894 name_expr = NULL;
895 }
896
897 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898}
899
900static asdl_seq*
901ast_for_decorators(struct compiling *c, const node *n)
902{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000903 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000904 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 int i;
906
907 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000908 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 if (!decorator_seq)
910 return NULL;
911
912 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +0000913 d = ast_for_decorator(c, CHILD(n, i));
914 if (!d)
915 return NULL;
916 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917 }
918 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919}
920
921static stmt_ty
922ast_for_funcdef(struct compiling *c, const node *n)
923{
924 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000925 identifier name;
926 arguments_ty args;
927 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 asdl_seq *decorator_seq = NULL;
929 int name_i;
930
931 REQ(n, funcdef);
932
933 if (NCH(n) == 6) { /* decorators are present */
934 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
935 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000936 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 name_i = 2;
938 }
939 else {
940 name_i = 1;
941 }
942
943 name = NEW_IDENTIFIER(CHILD(n, name_i));
944 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000945 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000947 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000948 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949 }
950 args = ast_for_arguments(c, CHILD(n, name_i + 1));
951 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953 body = ast_for_suite(c, CHILD(n, name_i + 3));
954 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000957 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
958 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959}
960
961static expr_ty
962ast_for_lambdef(struct compiling *c, const node *n)
963{
964 /* lambdef: 'lambda' [varargslist] ':' test */
965 arguments_ty args;
966 expr_ty expression;
967
968 if (NCH(n) == 3) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000969 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 if (!args)
971 return NULL;
972 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000973 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 }
976 else {
977 args = ast_for_arguments(c, CHILD(n, 1));
978 if (!args)
979 return NULL;
980 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000981 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983 }
984
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000985 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986}
987
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000988static expr_ty
989ast_for_ifexpr(struct compiling *c, const node *n)
990{
991 /* test: or_test 'if' or_test 'else' test */
992 expr_ty expression, body, orelse;
993
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000994 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000995 body = ast_for_expr(c, CHILD(n, 0));
996 if (!body)
997 return NULL;
998 expression = ast_for_expr(c, CHILD(n, 2));
999 if (!expression)
1000 return NULL;
1001 orelse = ast_for_expr(c, CHILD(n, 4));
1002 if (!orelse)
1003 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001004 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1005 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001006}
1007
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008/* Count the number of 'for' loop in a list comprehension.
1009
1010 Helper for ast_for_listcomp().
1011*/
1012
1013static int
1014count_list_fors(const node *n)
1015{
1016 int n_fors = 0;
1017 node *ch = CHILD(n, 1);
1018
1019 count_list_for:
1020 n_fors++;
1021 REQ(ch, list_for);
1022 if (NCH(ch) == 5)
1023 ch = CHILD(ch, 4);
1024 else
1025 return n_fors;
1026 count_list_iter:
1027 REQ(ch, list_iter);
1028 ch = CHILD(ch, 0);
1029 if (TYPE(ch) == list_for)
1030 goto count_list_for;
1031 else if (TYPE(ch) == list_if) {
1032 if (NCH(ch) == 3) {
1033 ch = CHILD(ch, 2);
1034 goto count_list_iter;
1035 }
1036 else
1037 return n_fors;
1038 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001039
1040 /* Should never be reached */
1041 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1042 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043}
1044
1045/* Count the number of 'if' statements in a list comprehension.
1046
1047 Helper for ast_for_listcomp().
1048*/
1049
1050static int
1051count_list_ifs(const node *n)
1052{
1053 int n_ifs = 0;
1054
1055 count_list_iter:
1056 REQ(n, list_iter);
1057 if (TYPE(CHILD(n, 0)) == list_for)
1058 return n_ifs;
1059 n = CHILD(n, 0);
1060 REQ(n, list_if);
1061 n_ifs++;
1062 if (NCH(n) == 2)
1063 return n_ifs;
1064 n = CHILD(n, 2);
1065 goto count_list_iter;
1066}
1067
1068static expr_ty
1069ast_for_listcomp(struct compiling *c, const node *n)
1070{
1071 /* listmaker: test ( list_for | (',' test)* [','] )
1072 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1073 list_iter: list_for | list_if
1074 list_if: 'if' test [list_iter]
1075 testlist_safe: test [(',' test)+ [',']]
1076 */
1077 expr_ty elt;
1078 asdl_seq *listcomps;
1079 int i, n_fors;
1080 node *ch;
1081
1082 REQ(n, listmaker);
1083 assert(NCH(n) > 1);
1084
1085 elt = ast_for_expr(c, CHILD(n, 0));
1086 if (!elt)
1087 return NULL;
1088
1089 n_fors = count_list_fors(n);
1090 if (n_fors == -1)
1091 return NULL;
1092
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001093 listcomps = asdl_seq_new(n_fors, c->c_arena);
1094 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 ch = CHILD(n, 1);
1098 for (i = 0; i < n_fors; i++) {
1099 comprehension_ty lc;
1100 asdl_seq *t;
1101 expr_ty expression;
1102
1103 REQ(ch, list_for);
1104
1105 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001106 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001108 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001109 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001112 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001114 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001116 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1117 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001118 expression, NULL, c->c_arena);
1119 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121
1122 if (NCH(ch) == 5) {
1123 int j, n_ifs;
1124 asdl_seq *ifs;
1125
1126 ch = CHILD(ch, 4);
1127 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001128 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001131 ifs = asdl_seq_new(n_ifs, c->c_arena);
1132 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134
1135 for (j = 0; j < n_ifs; j++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001136 REQ(ch, list_iter);
1137 ch = CHILD(ch, 0);
1138 REQ(ch, list_if);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139
Jeremy Hyltona8293132006-02-28 17:58:27 +00001140 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
1141 if (NCH(ch) == 3)
1142 ch = CHILD(ch, 2);
1143 }
1144 /* on exit, must guarantee that ch is a list_for */
1145 if (TYPE(ch) == list_iter)
1146 ch = CHILD(ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 lc->ifs = ifs;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001148 }
1149 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 }
1151
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001152 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153}
1154
1155/*
1156 Count the number of 'for' loops in a generator expression.
1157
1158 Helper for ast_for_genexp().
1159*/
1160
1161static int
1162count_gen_fors(const node *n)
1163{
1164 int n_fors = 0;
1165 node *ch = CHILD(n, 1);
1166
1167 count_gen_for:
1168 n_fors++;
1169 REQ(ch, gen_for);
1170 if (NCH(ch) == 5)
1171 ch = CHILD(ch, 4);
1172 else
1173 return n_fors;
1174 count_gen_iter:
1175 REQ(ch, gen_iter);
1176 ch = CHILD(ch, 0);
1177 if (TYPE(ch) == gen_for)
1178 goto count_gen_for;
1179 else if (TYPE(ch) == gen_if) {
1180 if (NCH(ch) == 3) {
1181 ch = CHILD(ch, 2);
1182 goto count_gen_iter;
1183 }
1184 else
1185 return n_fors;
1186 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001187
1188 /* Should never be reached */
1189 PyErr_SetString(PyExc_SystemError,
1190 "logic error in count_gen_fors");
1191 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192}
1193
1194/* Count the number of 'if' statements in a generator expression.
1195
1196 Helper for ast_for_genexp().
1197*/
1198
1199static int
1200count_gen_ifs(const node *n)
1201{
1202 int n_ifs = 0;
1203
1204 while (1) {
1205 REQ(n, gen_iter);
1206 if (TYPE(CHILD(n, 0)) == gen_for)
1207 return n_ifs;
1208 n = CHILD(n, 0);
1209 REQ(n, gen_if);
1210 n_ifs++;
1211 if (NCH(n) == 2)
1212 return n_ifs;
1213 n = CHILD(n, 2);
1214 }
1215}
1216
Jeremy Hyltona8293132006-02-28 17:58:27 +00001217/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218static expr_ty
1219ast_for_genexp(struct compiling *c, const node *n)
1220{
1221 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1222 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1223 expr_ty elt;
1224 asdl_seq *genexps;
1225 int i, n_fors;
1226 node *ch;
1227
1228 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1229 assert(NCH(n) > 1);
1230
1231 elt = ast_for_expr(c, CHILD(n, 0));
1232 if (!elt)
1233 return NULL;
1234
1235 n_fors = count_gen_fors(n);
1236 if (n_fors == -1)
1237 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001238
1239 genexps = asdl_seq_new(n_fors, c->c_arena);
1240 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 ch = CHILD(n, 1);
1244 for (i = 0; i < n_fors; i++) {
1245 comprehension_ty ge;
1246 asdl_seq *t;
1247 expr_ty expression;
1248
1249 REQ(ch, gen_for);
1250
1251 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001254 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001255 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001257
1258 if (asdl_seq_LEN(t) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001260 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001262 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1263 c->c_arena),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264 expression, NULL, c->c_arena);
1265
1266 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001268
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 if (NCH(ch) == 5) {
1270 int j, n_ifs;
1271 asdl_seq *ifs;
1272
1273 ch = CHILD(ch, 4);
1274 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001275 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001277
1278 ifs = asdl_seq_new(n_ifs, c->c_arena);
1279 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001281
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 for (j = 0; j < n_ifs; j++) {
1283 REQ(ch, gen_iter);
1284 ch = CHILD(ch, 0);
1285 REQ(ch, gen_if);
1286
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001287 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001288 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001289 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001290 asdl_seq_SET(ifs, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 if (NCH(ch) == 3)
1292 ch = CHILD(ch, 2);
1293 }
1294 /* on exit, must guarantee that ch is a gen_for */
1295 if (TYPE(ch) == gen_iter)
1296 ch = CHILD(ch, 0);
1297 ge->ifs = ifs;
1298 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00001299 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 }
1301
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001302 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303}
1304
1305static expr_ty
1306ast_for_atom(struct compiling *c, const node *n)
1307{
1308 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
Guido van Rossum86e58e22006-08-28 15:27:34 +00001309 | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING+
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 */
1311 node *ch = CHILD(n, 0);
1312
1313 switch (TYPE(ch)) {
1314 case NAME:
1315 /* All names start in Load context, but may later be
1316 changed. */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001317 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 case STRING: {
1319 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 if (!str)
1321 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001322
1323 PyArena_AddPyObject(c->c_arena, str);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001324 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 }
1326 case NUMBER: {
1327 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328 if (!pynum)
1329 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001330
1331 PyArena_AddPyObject(c->c_arena, pynum);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001332 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 }
Georg Brandl52318d62006-09-06 07:06:08 +00001334 case DOT:
1335 /* Ellipsis */
1336 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 case LPAR: /* some parenthesized expressions */
1338 ch = CHILD(n, 1);
1339
1340 if (TYPE(ch) == RPAR)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001341 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342
1343 if (TYPE(ch) == yield_expr)
1344 return ast_for_expr(c, ch);
1345
1346 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1347 return ast_for_genexp(c, ch);
1348
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001349 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 case LSQB: /* list (or list comprehension) */
1351 ch = CHILD(n, 1);
1352
1353 if (TYPE(ch) == RSQB)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001354 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355
1356 REQ(ch, listmaker);
1357 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1358 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 if (!elts)
1360 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001361
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001362 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 }
1364 else
1365 return ast_for_listcomp(c, ch);
1366 case LBRACE: {
Guido van Rossum86e58e22006-08-28 15:27:34 +00001367 /* dictsetmaker: test ':' test (',' test ':' test)* [','] |
1368 * test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 int i, size;
1370 asdl_seq *keys, *values;
1371
1372 ch = CHILD(n, 1);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001373 if (NCH(ch) == 1 || (NCH(ch) > 0 && STR(CHILD(ch, 1))[0] == ',')) {
1374 /* it's a set */
1375 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
1376 keys = asdl_seq_new(size, c->c_arena);
1377 if (!keys)
1378 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001379
Guido van Rossum86e58e22006-08-28 15:27:34 +00001380 for (i = 0; i < NCH(ch); i += 2) {
1381 expr_ty expression;
1382 expression = ast_for_expr(c, CHILD(ch, i));
1383 if (!expression)
1384 return NULL;
1385 asdl_seq_SET(keys, i / 2, expression);
1386 }
1387 return Set(keys, LINENO(n), n->n_col_offset, c->c_arena);
1388 } else {
1389 /* it's a dict */
1390 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1391 keys = asdl_seq_new(size, c->c_arena);
1392 if (!keys)
1393 return NULL;
1394
1395 values = asdl_seq_new(size, c->c_arena);
1396 if (!values)
1397 return NULL;
1398
1399 for (i = 0; i < NCH(ch); i += 4) {
1400 expr_ty expression;
1401
1402 expression = ast_for_expr(c, CHILD(ch, i));
1403 if (!expression)
1404 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001405
Guido van Rossum86e58e22006-08-28 15:27:34 +00001406 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitze76adcd2005-11-15 05:04:31 +00001407
Guido van Rossum86e58e22006-08-28 15:27:34 +00001408 expression = ast_for_expr(c, CHILD(ch, i + 2));
1409 if (!expression)
1410 return NULL;
1411
1412 asdl_seq_SET(values, i / 4, expression);
1413 }
1414 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1415 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001418 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 return NULL;
1420 }
1421}
1422
1423static slice_ty
1424ast_for_slice(struct compiling *c, const node *n)
1425{
1426 node *ch;
1427 expr_ty lower = NULL, upper = NULL, step = NULL;
1428
1429 REQ(n, subscript);
1430
1431 /*
Georg Brandl52318d62006-09-06 07:06:08 +00001432 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 sliceop: ':' [test]
1434 */
1435 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 if (NCH(n) == 1 && TYPE(ch) == test) {
1437 /* 'step' variable hold no significance in terms of being used over
1438 other vars */
1439 step = ast_for_expr(c, ch);
1440 if (!step)
1441 return NULL;
1442
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001443 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 }
1445
1446 if (TYPE(ch) == test) {
1447 lower = ast_for_expr(c, ch);
1448 if (!lower)
1449 return NULL;
1450 }
1451
1452 /* If there's an upper bound it's in the second or third position. */
1453 if (TYPE(ch) == COLON) {
1454 if (NCH(n) > 1) {
1455 node *n2 = CHILD(n, 1);
1456
1457 if (TYPE(n2) == test) {
1458 upper = ast_for_expr(c, n2);
1459 if (!upper)
1460 return NULL;
1461 }
1462 }
1463 } else if (NCH(n) > 2) {
1464 node *n2 = CHILD(n, 2);
1465
1466 if (TYPE(n2) == test) {
1467 upper = ast_for_expr(c, n2);
1468 if (!upper)
1469 return NULL;
1470 }
1471 }
1472
1473 ch = CHILD(n, NCH(n) - 1);
1474 if (TYPE(ch) == sliceop) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001475 if (NCH(ch) == 1) {
1476 /* No expression, so step is None */
1477 ch = CHILD(ch, 0);
1478 step = Name(new_identifier("None", c->c_arena), Load,
1479 LINENO(ch), ch->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 if (!step)
1481 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001482 } else {
1483 ch = CHILD(ch, 1);
1484 if (TYPE(ch) == test) {
1485 step = ast_for_expr(c, ch);
1486 if (!step)
1487 return NULL;
1488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 }
1490 }
1491
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001492 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493}
1494
1495static expr_ty
1496ast_for_binop(struct compiling *c, const node *n)
1497{
1498 /* Must account for a sequence of expressions.
1499 How should A op B op C by represented?
1500 BinOp(BinOp(A, op, B), op, C).
1501 */
1502
1503 int i, nops;
1504 expr_ty expr1, expr2, result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001505 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
1507 expr1 = ast_for_expr(c, CHILD(n, 0));
1508 if (!expr1)
1509 return NULL;
1510
1511 expr2 = ast_for_expr(c, CHILD(n, 2));
1512 if (!expr2)
1513 return NULL;
1514
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001515 newoperator = get_operator(CHILD(n, 1));
1516 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 return NULL;
1518
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001519 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1520 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 if (!result)
1522 return NULL;
1523
1524 nops = (NCH(n) - 1) / 2;
1525 for (i = 1; i < nops; i++) {
1526 expr_ty tmp_result, tmp;
1527 const node* next_oper = CHILD(n, i * 2 + 1);
1528
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529 newoperator = get_operator(next_oper);
1530 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 return NULL;
1532
1533 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1534 if (!tmp)
1535 return NULL;
1536
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537 tmp_result = BinOp(result, newoperator, tmp,
1538 LINENO(next_oper), next_oper->n_col_offset,
1539 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 if (!tmp)
1541 return NULL;
1542 result = tmp_result;
1543 }
1544 return result;
1545}
1546
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001547static expr_ty
1548ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1549{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001550 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1551 subscriptlist: subscript (',' subscript)* [',']
1552 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1553 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001554 REQ(n, trailer);
1555 if (TYPE(CHILD(n, 0)) == LPAR) {
1556 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001557 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1558 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001559 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001560 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001561 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001562 else if (TYPE(CHILD(n, 0)) == DOT ) {
1563 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001564 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001565 }
1566 else {
1567 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001568 REQ(CHILD(n, 2), RSQB);
1569 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001570 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001571 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1572 if (!slc)
1573 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001574 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1575 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001576 }
1577 else {
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001578 /* The grammar is ambiguous here. The ambiguity is resolved
1579 by treating the sequence as a tuple literal if there are
1580 no slice features.
1581 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001582 int j;
1583 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001584 expr_ty e;
Thomas Wouters65b3dab2006-03-01 22:06:23 +00001585 bool simple = true;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001586 asdl_seq *slices, *elts;
1587 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001588 if (!slices)
1589 return NULL;
1590 for (j = 0; j < NCH(n); j += 2) {
1591 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001592 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001593 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001594 if (slc->kind != Index_kind)
1595 simple = false;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001596 asdl_seq_SET(slices, j / 2, slc);
1597 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001598 if (!simple) {
1599 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001600 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001601 }
1602 /* extract Index values and put them in a Tuple */
1603 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001604 if (!elts)
1605 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001606 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1607 slc = (slice_ty)asdl_seq_GET(slices, j);
1608 assert(slc->kind == Index_kind && slc->v.Index.value);
1609 asdl_seq_SET(elts, j, slc->v.Index.value);
1610 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001611 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001612 if (!e)
1613 return NULL;
1614 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001615 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001616 }
1617 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001618}
1619
1620static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001621ast_for_factor(struct compiling *c, const node *n)
1622{
1623 node *pfactor, *ppower, *patom, *pnum;
1624 expr_ty expression;
1625
1626 /* If the unary - operator is applied to a constant, don't generate
1627 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1628 constant. The peephole optimizer already does something like
1629 this but it doesn't handle the case where the constant is
1630 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1631 PyLongObject.
1632 */
1633 if (TYPE(CHILD(n, 0)) == MINUS
1634 && NCH(n) == 2
1635 && TYPE((pfactor = CHILD(n, 1))) == factor
1636 && NCH(pfactor) == 1
1637 && TYPE((ppower = CHILD(pfactor, 0))) == power
1638 && NCH(ppower) == 1
1639 && TYPE((patom = CHILD(ppower, 0))) == atom
1640 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1641 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1642 if (s == NULL)
1643 return NULL;
1644 s[0] = '-';
1645 strcpy(s + 1, STR(pnum));
1646 PyObject_FREE(STR(pnum));
1647 STR(pnum) = s;
1648 return ast_for_atom(c, patom);
1649 }
1650
1651 expression = ast_for_expr(c, CHILD(n, 1));
1652 if (!expression)
1653 return NULL;
1654
1655 switch (TYPE(CHILD(n, 0))) {
1656 case PLUS:
1657 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1658 c->c_arena);
1659 case MINUS:
1660 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1661 c->c_arena);
1662 case TILDE:
1663 return UnaryOp(Invert, expression, LINENO(n),
1664 n->n_col_offset, c->c_arena);
1665 }
1666 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1667 TYPE(CHILD(n, 0)));
1668 return NULL;
1669}
1670
1671static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001672ast_for_power(struct compiling *c, const node *n)
1673{
1674 /* power: atom trailer* ('**' factor)*
1675 */
1676 int i;
1677 expr_ty e, tmp;
1678 REQ(n, power);
1679 e = ast_for_atom(c, CHILD(n, 0));
1680 if (!e)
1681 return NULL;
1682 if (NCH(n) == 1)
1683 return e;
1684 for (i = 1; i < NCH(n); i++) {
1685 node *ch = CHILD(n, i);
1686 if (TYPE(ch) != trailer)
1687 break;
1688 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001689 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001690 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001691 tmp->lineno = e->lineno;
1692 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001693 e = tmp;
1694 }
1695 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1696 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001697 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001698 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001699 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001700 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001701 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001702 e = tmp;
1703 }
1704 return e;
1705}
1706
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707/* Do not name a variable 'expr'! Will cause a compile error.
1708*/
1709
1710static expr_ty
1711ast_for_expr(struct compiling *c, const node *n)
1712{
1713 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001714 test: or_test ['if' or_test 'else' test] | lambdef
1715 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 and_test: not_test ('and' not_test)*
1717 not_test: 'not' not_test | comparison
1718 comparison: expr (comp_op expr)*
1719 expr: xor_expr ('|' xor_expr)*
1720 xor_expr: and_expr ('^' and_expr)*
1721 and_expr: shift_expr ('&' shift_expr)*
1722 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1723 arith_expr: term (('+'|'-') term)*
1724 term: factor (('*'|'/'|'%'|'//') factor)*
1725 factor: ('+'|'-'|'~') factor | power
1726 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001727
1728 As well as modified versions that exist for backward compatibility,
1729 to explicitly allow:
1730 [ x for x in lambda: 0, lambda: 1 ]
1731 (which would be ambiguous without these extra rules)
1732
1733 old_test: or_test | old_lambdef
1734 old_lambdef: 'lambda' [vararglist] ':' old_test
1735
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 */
1737
1738 asdl_seq *seq;
1739 int i;
1740
1741 loop:
1742 switch (TYPE(n)) {
1743 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001744 case old_test:
1745 if (TYPE(CHILD(n, 0)) == lambdef ||
1746 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001748 else if (NCH(n) > 1)
1749 return ast_for_ifexpr(c, n);
1750 /* Fallthrough */
1751 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 case and_test:
1753 if (NCH(n) == 1) {
1754 n = CHILD(n, 0);
1755 goto loop;
1756 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001757 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 if (!seq)
1759 return NULL;
1760 for (i = 0; i < NCH(n); i += 2) {
1761 expr_ty e = ast_for_expr(c, CHILD(n, i));
1762 if (!e)
1763 return NULL;
1764 asdl_seq_SET(seq, i / 2, e);
1765 }
1766 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001767 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1768 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001769 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001770 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 case not_test:
1772 if (NCH(n) == 1) {
1773 n = CHILD(n, 0);
1774 goto loop;
1775 }
1776 else {
1777 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1778 if (!expression)
1779 return NULL;
1780
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1782 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783 }
1784 case comparison:
1785 if (NCH(n) == 1) {
1786 n = CHILD(n, 0);
1787 goto loop;
1788 }
1789 else {
1790 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001791 asdl_int_seq *ops;
1792 asdl_seq *cmps;
1793 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 if (!ops)
1795 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001796 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 return NULL;
1799 }
1800 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001801 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001803 newoperator = ast_for_comp_op(CHILD(n, i));
1804 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001806 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807
1808 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001809 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001811 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001813 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 asdl_seq_SET(cmps, i / 2, expression);
1815 }
1816 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001817 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001819 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001821 return Compare(expression, ops, cmps, LINENO(n),
1822 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 }
1824 break;
1825
1826 /* The next five cases all handle BinOps. The main body of code
1827 is the same in each case, but the switch turned inside out to
1828 reuse the code for each type of operator.
1829 */
1830 case expr:
1831 case xor_expr:
1832 case and_expr:
1833 case shift_expr:
1834 case arith_expr:
1835 case term:
1836 if (NCH(n) == 1) {
1837 n = CHILD(n, 0);
1838 goto loop;
1839 }
1840 return ast_for_binop(c, n);
1841 case yield_expr: {
1842 expr_ty exp = NULL;
1843 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001844 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 if (!exp)
1846 return NULL;
1847 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001848 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001850 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 if (NCH(n) == 1) {
1852 n = CHILD(n, 0);
1853 goto loop;
1854 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001855 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001856 case power:
1857 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001859 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 return NULL;
1861 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001862 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 return NULL;
1864}
1865
1866static expr_ty
1867ast_for_call(struct compiling *c, const node *n, expr_ty func)
1868{
1869 /*
1870 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1871 | '**' test)
1872 argument: [test '='] test [gen_for] # Really [keyword '='] test
1873 */
1874
1875 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001876 asdl_seq *args;
1877 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 expr_ty vararg = NULL, kwarg = NULL;
1879
1880 REQ(n, arglist);
1881
1882 nargs = 0;
1883 nkeywords = 0;
1884 ngens = 0;
1885 for (i = 0; i < NCH(n); i++) {
1886 node *ch = CHILD(n, i);
1887 if (TYPE(ch) == argument) {
1888 if (NCH(ch) == 1)
1889 nargs++;
1890 else if (TYPE(CHILD(ch, 1)) == gen_for)
1891 ngens++;
1892 else
1893 nkeywords++;
1894 }
1895 }
1896 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001897 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 "if not sole argument");
1899 return NULL;
1900 }
1901
1902 if (nargs + nkeywords + ngens > 255) {
1903 ast_error(n, "more than 255 arguments");
1904 return NULL;
1905 }
1906
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001907 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001909 return NULL;
1910 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001912 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 nargs = 0;
1914 nkeywords = 0;
1915 for (i = 0; i < NCH(n); i++) {
1916 node *ch = CHILD(n, i);
1917 if (TYPE(ch) == argument) {
1918 expr_ty e;
1919 if (NCH(ch) == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001920 if (nkeywords) {
1921 ast_error(CHILD(ch, 0),
1922 "non-keyword arg after keyword arg");
1923 return NULL;
1924 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 e = ast_for_expr(c, CHILD(ch, 0));
1926 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001927 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 asdl_seq_SET(args, nargs++, e);
1929 }
1930 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1931 e = ast_for_genexp(c, ch);
1932 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001933 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 asdl_seq_SET(args, nargs++, e);
1935 }
1936 else {
1937 keyword_ty kw;
1938 identifier key;
1939
1940 /* CHILD(ch, 0) is test, but must be an identifier? */
1941 e = ast_for_expr(c, CHILD(ch, 0));
1942 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 /* f(lambda x: x[0] = 3) ends up getting parsed with
1945 * LHS test = lambda x: x[0], and RHS test = 3.
1946 * SF bug 132313 points out that complaining about a keyword
1947 * then is very confusing.
1948 */
1949 if (e->kind == Lambda_kind) {
1950 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001951 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 } else if (e->kind != Name_kind) {
1953 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 }
1956 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 e = ast_for_expr(c, CHILD(ch, 2));
1958 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001959 return NULL;
1960 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001962 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963 asdl_seq_SET(keywords, nkeywords++, kw);
1964 }
1965 }
1966 else if (TYPE(ch) == STAR) {
1967 vararg = ast_for_expr(c, CHILD(n, i+1));
1968 i++;
1969 }
1970 else if (TYPE(ch) == DOUBLESTAR) {
1971 kwarg = ast_for_expr(c, CHILD(n, i+1));
1972 i++;
1973 }
1974 }
1975
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001976 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977}
1978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001980ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001982 /* testlist_gexp: test (',' test)* [','] */
1983 /* testlist: test (',' test)* [','] */
1984 /* testlist_safe: test (',' test)+ [','] */
1985 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001987 if (TYPE(n) == testlist_gexp) {
1988 if (NCH(n) > 1)
1989 assert(TYPE(CHILD(n, 1)) != gen_for);
1990 }
1991 else {
1992 assert(TYPE(n) == testlist ||
1993 TYPE(n) == testlist_safe ||
1994 TYPE(n) == testlist1);
1995 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 if (NCH(n) == 1)
1997 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 else {
1999 asdl_seq *tmp = seq_for_testlist(c, n);
2000 if (!tmp)
2001 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002002 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002004}
2005
2006static expr_ty
2007ast_for_testlist_gexp(struct compiling *c, const node* n)
2008{
2009 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2010 /* argument: test [ gen_for ] */
2011 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002012 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002013 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002014 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002015}
2016
2017/* like ast_for_testlist() but returns a sequence */
2018static asdl_seq*
2019ast_for_class_bases(struct compiling *c, const node* n)
2020{
2021 /* testlist: test (',' test)* [','] */
2022 assert(NCH(n) > 0);
2023 REQ(n, testlist);
2024 if (NCH(n) == 1) {
2025 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002026 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002027 if (!bases)
2028 return NULL;
2029 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002030 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002031 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002032 asdl_seq_SET(bases, 0, base);
2033 return bases;
2034 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002035
2036 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037}
2038
2039static stmt_ty
2040ast_for_expr_stmt(struct compiling *c, const node *n)
2041{
2042 REQ(n, expr_stmt);
2043 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2044 | ('=' (yield_expr|testlist))*)
2045 testlist: test (',' test)* [',']
2046 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2047 | '<<=' | '>>=' | '**=' | '//='
2048 test: ... here starts the operator precendence dance
2049 */
2050
2051 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002052 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 if (!e)
2054 return NULL;
2055
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002056 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 }
2058 else if (TYPE(CHILD(n, 1)) == augassign) {
2059 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002060 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 node *ch = CHILD(n, 0);
2062
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002063 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 if (!expr1)
2065 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002066 /* TODO(nas): Remove duplicated error checks (set_context does it) */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00002067 switch (expr1->kind) {
2068 case GeneratorExp_kind:
2069 ast_error(ch, "augmented assignment to generator "
2070 "expression not possible");
2071 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002072 case Yield_kind:
2073 ast_error(ch, "augmented assignment to yield "
2074 "expression not possible");
2075 return NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00002076 case Name_kind: {
2077 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2078 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2079 ast_error(ch, "assignment to None");
2080 return NULL;
2081 }
2082 break;
2083 }
2084 case Attribute_kind:
2085 case Subscript_kind:
2086 break;
2087 default:
2088 ast_error(ch, "illegal expression for augmented "
2089 "assignment");
2090 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002092 set_context(expr1, Store, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093
2094 ch = CHILD(n, 2);
2095 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002096 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 else
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002098 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002099 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 return NULL;
2101
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002102 newoperator = ast_for_augassign(CHILD(n, 1));
2103 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 return NULL;
2105
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002106 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 }
2108 else {
2109 int i;
2110 asdl_seq *targets;
2111 node *value;
2112 expr_ty expression;
2113
2114 /* a normal assignment */
2115 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002116 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 if (!targets)
2118 return NULL;
2119 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002120 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 node *ch = CHILD(n, i);
2122 if (TYPE(ch) == yield_expr) {
2123 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002124 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002126 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127
2128 /* set context to assign */
2129 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131
Neal Norwitz84456bd2005-12-18 03:16:20 +00002132 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002133 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
2135 asdl_seq_SET(targets, i / 2, e);
2136 }
2137 value = CHILD(n, NCH(n) - 1);
2138 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002139 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 else
2141 expression = ast_for_expr(c, value);
2142 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002143 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002144 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146}
2147
2148static stmt_ty
2149ast_for_print_stmt(struct compiling *c, const node *n)
2150{
2151 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2152 | '>>' test [ (',' test)+ [','] ] )
2153 */
2154 expr_ty dest = NULL, expression;
2155 asdl_seq *seq;
2156 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002157 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158
2159 REQ(n, print_stmt);
2160 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2161 dest = ast_for_expr(c, CHILD(n, 2));
2162 if (!dest)
2163 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002164 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002166 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 if (!seq)
Jeremy Hyltona8293132006-02-28 17:58:27 +00002168 return NULL;
2169 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002171 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002173 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 }
2175 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002176 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177}
2178
2179static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002180ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181{
2182 asdl_seq *seq;
2183 int i;
2184 expr_ty e;
2185
2186 REQ(n, exprlist);
2187
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002188 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 if (!seq)
2190 return NULL;
2191 for (i = 0; i < NCH(n); i += 2) {
2192 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002193 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002194 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00002195 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002196 if (context && !set_context(e, context, CHILD(n, i)))
2197 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 }
2199 return seq;
2200}
2201
2202static stmt_ty
2203ast_for_del_stmt(struct compiling *c, const node *n)
2204{
2205 asdl_seq *expr_list;
2206
2207 /* del_stmt: 'del' exprlist */
2208 REQ(n, del_stmt);
2209
2210 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2211 if (!expr_list)
2212 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002213 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214}
2215
2216static stmt_ty
2217ast_for_flow_stmt(struct compiling *c, const node *n)
2218{
2219 /*
2220 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2221 | yield_stmt
2222 break_stmt: 'break'
2223 continue_stmt: 'continue'
2224 return_stmt: 'return' [testlist]
2225 yield_stmt: yield_expr
2226 yield_expr: 'yield' testlist
2227 raise_stmt: 'raise' [test [',' test [',' test]]]
2228 */
2229 node *ch;
2230
2231 REQ(n, flow_stmt);
2232 ch = CHILD(n, 0);
2233 switch (TYPE(ch)) {
2234 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002235 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002237 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 case yield_stmt: { /* will reduce to yield_expr */
2239 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2240 if (!exp)
2241 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002242 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 }
2244 case return_stmt:
2245 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002246 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002248 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 if (!expression)
2250 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002251 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 }
2253 case raise_stmt:
2254 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002255 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 else if (NCH(ch) == 2) {
2257 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2258 if (!expression)
2259 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002260 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 }
2262 else if (NCH(ch) == 4) {
2263 expr_ty expr1, expr2;
2264
2265 expr1 = ast_for_expr(c, CHILD(ch, 1));
2266 if (!expr1)
2267 return NULL;
2268 expr2 = ast_for_expr(c, CHILD(ch, 3));
2269 if (!expr2)
2270 return NULL;
2271
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002272 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 }
2274 else if (NCH(ch) == 6) {
2275 expr_ty expr1, expr2, expr3;
2276
2277 expr1 = ast_for_expr(c, CHILD(ch, 1));
2278 if (!expr1)
2279 return NULL;
2280 expr2 = ast_for_expr(c, CHILD(ch, 3));
2281 if (!expr2)
2282 return NULL;
2283 expr3 = ast_for_expr(c, CHILD(ch, 5));
2284 if (!expr3)
2285 return NULL;
2286
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002287 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 }
2289 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002290 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 "unexpected flow_stmt: %d", TYPE(ch));
2292 return NULL;
2293 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002294
2295 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297}
2298
2299static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002300alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301{
2302 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002303 import_as_name: NAME ['as' NAME]
2304 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 dotted_name: NAME ('.' NAME)*
2306 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002307 PyObject *str;
2308
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 loop:
2310 switch (TYPE(n)) {
2311 case import_as_name:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002312 str = NULL;
2313 if (NCH(n) == 3) {
2314 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2315 ast_error(n, "must use 'as' in import");
2316 return NULL;
2317 }
2318 str = NEW_IDENTIFIER(CHILD(n, 2));
2319 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002320 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321 case dotted_as_name:
2322 if (NCH(n) == 1) {
2323 n = CHILD(n, 0);
2324 goto loop;
2325 }
2326 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002327 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002328 if (!a)
2329 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002330 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2331 ast_error(n, "must use 'as' in import");
2332 return NULL;
2333 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 assert(!a->asname);
2335 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2336 return a;
2337 }
2338 break;
2339 case dotted_name:
2340 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002341 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 else {
2343 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002344 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002345 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 char *s;
2347
2348 len = 0;
2349 for (i = 0; i < NCH(n); i += 2)
2350 /* length of string plus one for the dot */
2351 len += strlen(STR(CHILD(n, i))) + 1;
2352 len--; /* the last name doesn't have a dot */
2353 str = PyString_FromStringAndSize(NULL, len);
2354 if (!str)
2355 return NULL;
2356 s = PyString_AS_STRING(str);
2357 if (!s)
2358 return NULL;
2359 for (i = 0; i < NCH(n); i += 2) {
2360 char *sch = STR(CHILD(n, i));
2361 strcpy(s, STR(CHILD(n, i)));
2362 s += strlen(sch);
2363 *s++ = '.';
2364 }
2365 --s;
2366 *s = '\0';
2367 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002368 PyArena_AddPyObject(c->c_arena, str);
2369 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370 }
2371 break;
2372 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002373 str = PyString_InternFromString("*");
2374 PyArena_AddPyObject(c->c_arena, str);
2375 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002377 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 "unexpected import name: %d", TYPE(n));
2379 return NULL;
2380 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002381
2382 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 return NULL;
2384}
2385
2386static stmt_ty
2387ast_for_import_stmt(struct compiling *c, const node *n)
2388{
2389 /*
2390 import_stmt: import_name | import_from
2391 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002392 import_from: 'from' ('.'* dotted_name | '.') 'import'
2393 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002395 int lineno;
2396 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 int i;
2398 asdl_seq *aliases;
2399
2400 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002401 lineno = LINENO(n);
2402 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002404 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002406 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002407 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 if (!aliases)
2409 return NULL;
2410 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002411 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002412 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 asdl_seq_SET(aliases, i / 2, import_alias);
2415 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002416 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002417 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002418 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419 int n_children;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002420 int idx, ndots = 0;
2421 alias_ty mod = NULL;
2422 identifier modname;
2423
2424 /* Count the number of dots (for relative imports) and check for the
2425 optional module name */
2426 for (idx = 1; idx < NCH(n); idx++) {
2427 if (TYPE(CHILD(n, idx)) == dotted_name) {
2428 mod = alias_for_import_name(c, CHILD(n, idx));
2429 idx++;
2430 break;
2431 } else if (TYPE(CHILD(n, idx)) != DOT) {
2432 break;
2433 }
2434 ndots++;
2435 }
2436 idx++; /* skip over the 'import' keyword */
2437 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002438 case STAR:
2439 /* from ... import * */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002440 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002441 n_children = 1;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002442 if (ndots) {
2443 ast_error(n, "'import *' not allowed with 'from .'");
2444 return NULL;
2445 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002446 break;
2447 case LPAR:
2448 /* from ... import (x, y, z) */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002449 n = CHILD(n, idx + 1);
Thomas Wouters106203c2006-02-27 17:05:19 +00002450 n_children = NCH(n);
2451 break;
2452 case import_as_names:
2453 /* from ... import x, y, z */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002454 n = CHILD(n, idx);
Thomas Wouters106203c2006-02-27 17:05:19 +00002455 n_children = NCH(n);
2456 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 ast_error(n, "trailing comma not allowed without"
2458 " surrounding parentheses");
2459 return NULL;
2460 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002461 break;
2462 default:
2463 ast_error(n, "Unexpected node-type in from-import");
2464 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002467 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002468 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470
2471 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002472 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002473 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002474 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002476 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002478 else {
2479 for (i = 0; i < NCH(n); i += 2) {
2480 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2481 if (!import_alias)
2482 return NULL;
2483 asdl_seq_SET(aliases, i / 2, import_alias);
2484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002486 if (mod != NULL)
2487 modname = mod->name;
2488 else
2489 modname = new_identifier("", c->c_arena);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002490 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002491 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 }
Neal Norwitz79792652005-11-14 04:25:03 +00002493 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 "unknown import statement: starts with command '%s'",
2495 STR(CHILD(n, 0)));
2496 return NULL;
2497}
2498
2499static stmt_ty
2500ast_for_global_stmt(struct compiling *c, const node *n)
2501{
2502 /* global_stmt: 'global' NAME (',' NAME)* */
2503 identifier name;
2504 asdl_seq *s;
2505 int i;
2506
2507 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002508 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 if (!s)
2510 return NULL;
2511 for (i = 1; i < NCH(n); i += 2) {
2512 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002513 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 asdl_seq_SET(s, i / 2, name);
2516 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002517 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518}
2519
2520static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521ast_for_assert_stmt(struct compiling *c, const node *n)
2522{
2523 /* assert_stmt: 'assert' test [',' test] */
2524 REQ(n, assert_stmt);
2525 if (NCH(n) == 2) {
2526 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2527 if (!expression)
2528 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002529 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 }
2531 else if (NCH(n) == 4) {
2532 expr_ty expr1, expr2;
2533
2534 expr1 = ast_for_expr(c, CHILD(n, 1));
2535 if (!expr1)
2536 return NULL;
2537 expr2 = ast_for_expr(c, CHILD(n, 3));
2538 if (!expr2)
2539 return NULL;
2540
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002541 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 }
Neal Norwitz79792652005-11-14 04:25:03 +00002543 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 "improper number of parts to 'assert' statement: %d",
2545 NCH(n));
2546 return NULL;
2547}
2548
2549static asdl_seq *
2550ast_for_suite(struct compiling *c, const node *n)
2551{
2552 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002553 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 stmt_ty s;
2555 int i, total, num, end, pos = 0;
2556 node *ch;
2557
2558 REQ(n, suite);
2559
2560 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002561 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 if (!seq)
2563 return NULL;
2564 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2565 n = CHILD(n, 0);
2566 /* simple_stmt always ends with a NEWLINE,
2567 and may have a trailing SEMI
2568 */
2569 end = NCH(n) - 1;
2570 if (TYPE(CHILD(n, end - 1)) == SEMI)
2571 end--;
2572 /* loop by 2 to skip semi-colons */
2573 for (i = 0; i < end; i += 2) {
2574 ch = CHILD(n, i);
2575 s = ast_for_stmt(c, ch);
2576 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002577 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 asdl_seq_SET(seq, pos++, s);
2579 }
2580 }
2581 else {
2582 for (i = 2; i < (NCH(n) - 1); i++) {
2583 ch = CHILD(n, i);
2584 REQ(ch, stmt);
2585 num = num_stmts(ch);
2586 if (num == 1) {
2587 /* small_stmt or compound_stmt with only one child */
2588 s = ast_for_stmt(c, ch);
2589 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 asdl_seq_SET(seq, pos++, s);
2592 }
2593 else {
2594 int j;
2595 ch = CHILD(ch, 0);
2596 REQ(ch, simple_stmt);
2597 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002598 /* statement terminates with a semi-colon ';' */
2599 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002600 assert((j + 1) == NCH(ch));
2601 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 s = ast_for_stmt(c, CHILD(ch, j));
2604 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 asdl_seq_SET(seq, pos++, s);
2607 }
2608 }
2609 }
2610 }
2611 assert(pos == seq->size);
2612 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613}
2614
2615static stmt_ty
2616ast_for_if_stmt(struct compiling *c, const node *n)
2617{
2618 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2619 ['else' ':' suite]
2620 */
2621 char *s;
2622
2623 REQ(n, if_stmt);
2624
2625 if (NCH(n) == 4) {
2626 expr_ty expression;
2627 asdl_seq *suite_seq;
2628
2629 expression = ast_for_expr(c, CHILD(n, 1));
2630 if (!expression)
2631 return NULL;
2632 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002633 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 return NULL;
2635
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002636 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002638
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 s = STR(CHILD(n, 4));
2640 /* s[2], the third character in the string, will be
2641 's' for el_s_e, or
2642 'i' for el_i_f
2643 */
2644 if (s[2] == 's') {
2645 expr_ty expression;
2646 asdl_seq *seq1, *seq2;
2647
2648 expression = ast_for_expr(c, CHILD(n, 1));
2649 if (!expression)
2650 return NULL;
2651 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002652 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 return NULL;
2654 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002655 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 return NULL;
2657
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002658 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 }
2660 else if (s[2] == 'i') {
2661 int i, n_elif, has_else = 0;
2662 asdl_seq *orelse = NULL;
2663 n_elif = NCH(n) - 4;
2664 /* must reference the child n_elif+1 since 'else' token is third,
2665 not fourth, child from the end. */
2666 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2667 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2668 has_else = 1;
2669 n_elif -= 3;
2670 }
2671 n_elif /= 4;
2672
2673 if (has_else) {
2674 expr_ty expression;
2675 asdl_seq *seq1, *seq2;
2676
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002677 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 if (!orelse)
2679 return NULL;
2680 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002681 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002684 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002687 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689
2690 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002691 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002692 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 /* the just-created orelse handled the last elif */
2694 n_elif--;
2695 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696
2697 for (i = 0; i < n_elif; i++) {
2698 int off = 5 + (n_elif - i - 1) * 4;
2699 expr_ty expression;
2700 asdl_seq *suite_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002701 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2702 if (!newobj)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 return NULL;
2704 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002705 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002708 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002711 asdl_seq_SET(newobj, 0,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712 If(expression, suite_seq, orelse,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002713 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002714 orelse = newobj;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 }
2716 return If(ast_for_expr(c, CHILD(n, 1)),
2717 ast_for_suite(c, CHILD(n, 3)),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002718 orelse, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002720
2721 PyErr_Format(PyExc_SystemError,
2722 "unexpected token in 'if' statement: %s", s);
2723 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724}
2725
2726static stmt_ty
2727ast_for_while_stmt(struct compiling *c, const node *n)
2728{
2729 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2730 REQ(n, while_stmt);
2731
2732 if (NCH(n) == 4) {
2733 expr_ty expression;
2734 asdl_seq *suite_seq;
2735
2736 expression = ast_for_expr(c, CHILD(n, 1));
2737 if (!expression)
2738 return NULL;
2739 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002740 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002742 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 }
2744 else if (NCH(n) == 7) {
2745 expr_ty expression;
2746 asdl_seq *seq1, *seq2;
2747
2748 expression = ast_for_expr(c, CHILD(n, 1));
2749 if (!expression)
2750 return NULL;
2751 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002752 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 return NULL;
2754 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002755 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 return NULL;
2757
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002758 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002760
2761 PyErr_Format(PyExc_SystemError,
2762 "wrong number of tokens for 'while' statement: %d",
2763 NCH(n));
2764 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765}
2766
2767static stmt_ty
2768ast_for_for_stmt(struct compiling *c, const node *n)
2769{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002770 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771 expr_ty expression;
2772 expr_ty target;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002773 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2775 REQ(n, for_stmt);
2776
2777 if (NCH(n) == 9) {
2778 seq = ast_for_suite(c, CHILD(n, 8));
2779 if (!seq)
2780 return NULL;
2781 }
2782
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002783 node_target = CHILD(n, 1);
2784 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002785 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002787 /* Check the # of children rather than the length of _target, since
2788 for x, in ... has 1 element in _target, but still requires a Tuple. */
2789 if (NCH(node_target) == 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002790 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 else
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002792 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002794 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002795 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 return NULL;
2797 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002798 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 return NULL;
2800
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002801 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2802 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803}
2804
2805static excepthandler_ty
2806ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2807{
2808 /* except_clause: 'except' [test [',' test]] */
2809 REQ(exc, except_clause);
2810 REQ(body, suite);
2811
2812 if (NCH(exc) == 1) {
2813 asdl_seq *suite_seq = ast_for_suite(c, body);
2814 if (!suite_seq)
2815 return NULL;
2816
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002817 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2818 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 }
2820 else if (NCH(exc) == 2) {
2821 expr_ty expression;
2822 asdl_seq *suite_seq;
2823
2824 expression = ast_for_expr(c, CHILD(exc, 1));
2825 if (!expression)
2826 return NULL;
2827 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002828 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 return NULL;
2830
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002831 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2832 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 }
2834 else if (NCH(exc) == 4) {
2835 asdl_seq *suite_seq;
2836 expr_ty expression;
2837 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2838 if (!e)
2839 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002840 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 return NULL;
2842 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002843 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 return NULL;
2845 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002846 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 return NULL;
2848
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002849 return excepthandler(expression, e, suite_seq, LINENO(exc),
2850 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002852
2853 PyErr_Format(PyExc_SystemError,
2854 "wrong number of children for 'except' clause: %d",
2855 NCH(exc));
2856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857}
2858
2859static stmt_ty
2860ast_for_try_stmt(struct compiling *c, const node *n)
2861{
Neal Norwitzf599f422005-12-17 21:33:47 +00002862 const int nch = NCH(n);
2863 int n_except = (nch - 3)/3;
2864 asdl_seq *body, *orelse = NULL, *finally = NULL;
2865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 REQ(n, try_stmt);
2867
Neal Norwitzf599f422005-12-17 21:33:47 +00002868 body = ast_for_suite(c, CHILD(n, 2));
2869 if (body == NULL)
2870 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871
Neal Norwitzf599f422005-12-17 21:33:47 +00002872 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2873 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2874 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2875 /* we can assume it's an "else",
2876 because nch >= 9 for try-else-finally and
2877 it would otherwise have a type of except_clause */
2878 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2879 if (orelse == NULL)
2880 return NULL;
2881 n_except--;
2882 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883
Neal Norwitzf599f422005-12-17 21:33:47 +00002884 finally = ast_for_suite(c, CHILD(n, nch - 1));
2885 if (finally == NULL)
2886 return NULL;
2887 n_except--;
2888 }
2889 else {
2890 /* we can assume it's an "else",
2891 otherwise it would have a type of except_clause */
2892 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2893 if (orelse == NULL)
2894 return NULL;
2895 n_except--;
2896 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002898 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002899 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 return NULL;
2901 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002902
2903 if (n_except > 0) {
2904 int i;
2905 stmt_ty except_st;
2906 /* process except statements to create a try ... except */
2907 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2908 if (handlers == NULL)
2909 return NULL;
2910
2911 for (i = 0; i < n_except; i++) {
2912 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2913 CHILD(n, 5 + i * 3));
2914 if (!e)
2915 return NULL;
2916 asdl_seq_SET(handlers, i, e);
2917 }
2918
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002919 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2920 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002921 if (!finally)
2922 return except_st;
2923
2924 /* if a 'finally' is present too, we nest the TryExcept within a
2925 TryFinally to emulate try ... except ... finally */
2926 body = asdl_seq_new(1, c->c_arena);
2927 if (body == NULL)
2928 return NULL;
2929 asdl_seq_SET(body, 0, except_st);
2930 }
2931
2932 /* must be a try ... finally (except clauses are in body, if any exist) */
2933 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002934 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935}
2936
Guido van Rossumc2e20742006-02-27 22:32:47 +00002937static expr_ty
2938ast_for_with_var(struct compiling *c, const node *n)
2939{
2940 REQ(n, with_var);
2941 if (strcmp(STR(CHILD(n, 0)), "as") != 0) {
2942 ast_error(n, "expected \"with [expr] as [var]\"");
2943 return NULL;
2944 }
2945 return ast_for_expr(c, CHILD(n, 1));
2946}
2947
2948/* with_stmt: 'with' test [ with_var ] ':' suite */
2949static stmt_ty
2950ast_for_with_stmt(struct compiling *c, const node *n)
2951{
2952 expr_ty context_expr, optional_vars = NULL;
2953 int suite_index = 3; /* skip 'with', test, and ':' */
2954 asdl_seq *suite_seq;
2955
2956 assert(TYPE(n) == with_stmt);
2957 context_expr = ast_for_expr(c, CHILD(n, 1));
2958 if (TYPE(CHILD(n, 2)) == with_var) {
2959 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2960
2961 if (!optional_vars) {
2962 return NULL;
2963 }
2964 if (!set_context(optional_vars, Store, n)) {
2965 return NULL;
2966 }
2967 suite_index = 4;
2968 }
2969
2970 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2971 if (!suite_seq) {
2972 return NULL;
2973 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002974 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2975 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002976}
2977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978static stmt_ty
2979ast_for_classdef(struct compiling *c, const node *n)
2980{
2981 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 asdl_seq *bases, *s;
2983
2984 REQ(n, classdef);
2985
2986 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2987 ast_error(n, "assignment to None");
2988 return NULL;
2989 }
2990
2991 if (NCH(n) == 4) {
2992 s = ast_for_suite(c, CHILD(n, 3));
2993 if (!s)
2994 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002995 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2996 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997 }
2998 /* check for empty base list */
2999 if (TYPE(CHILD(n,3)) == RPAR) {
3000 s = ast_for_suite(c, CHILD(n,5));
3001 if (!s)
3002 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003003 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
3004 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005 }
3006
3007 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003008 bases = ast_for_class_bases(c, CHILD(n, 3));
3009 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011
3012 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003013 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003015 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
3016 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017}
3018
3019static stmt_ty
3020ast_for_stmt(struct compiling *c, const node *n)
3021{
3022 if (TYPE(n) == stmt) {
3023 assert(NCH(n) == 1);
3024 n = CHILD(n, 0);
3025 }
3026 if (TYPE(n) == simple_stmt) {
3027 assert(num_stmts(n) == 1);
3028 n = CHILD(n, 0);
3029 }
3030 if (TYPE(n) == small_stmt) {
3031 REQ(n, small_stmt);
3032 n = CHILD(n, 0);
3033 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00003034 | flow_stmt | import_stmt | global_stmt | assert_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035 */
3036 switch (TYPE(n)) {
3037 case expr_stmt:
3038 return ast_for_expr_stmt(c, n);
3039 case print_stmt:
3040 return ast_for_print_stmt(c, n);
3041 case del_stmt:
3042 return ast_for_del_stmt(c, n);
3043 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003044 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 case flow_stmt:
3046 return ast_for_flow_stmt(c, n);
3047 case import_stmt:
3048 return ast_for_import_stmt(c, n);
3049 case global_stmt:
3050 return ast_for_global_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 case assert_stmt:
3052 return ast_for_assert_stmt(c, n);
3053 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003054 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3056 TYPE(n), NCH(n));
3057 return NULL;
3058 }
3059 }
3060 else {
3061 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3062 | funcdef | classdef
3063 */
3064 node *ch = CHILD(n, 0);
3065 REQ(n, compound_stmt);
3066 switch (TYPE(ch)) {
3067 case if_stmt:
3068 return ast_for_if_stmt(c, ch);
3069 case while_stmt:
3070 return ast_for_while_stmt(c, ch);
3071 case for_stmt:
3072 return ast_for_for_stmt(c, ch);
3073 case try_stmt:
3074 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003075 case with_stmt:
3076 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 case funcdef:
3078 return ast_for_funcdef(c, ch);
3079 case classdef:
3080 return ast_for_classdef(c, ch);
3081 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003082 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3084 TYPE(n), NCH(n));
3085 return NULL;
3086 }
3087 }
3088}
3089
3090static PyObject *
3091parsenumber(const char *s)
3092{
3093 const char *end;
3094 long x;
3095 double dx;
3096#ifndef WITHOUT_COMPLEX
3097 Py_complex c;
3098 int imflag;
3099#endif
3100
3101 errno = 0;
3102 end = s + strlen(s) - 1;
3103#ifndef WITHOUT_COMPLEX
3104 imflag = *end == 'j' || *end == 'J';
3105#endif
3106 if (*end == 'l' || *end == 'L')
3107 return PyLong_FromString((char *)s, (char **)0, 0);
3108 if (s[0] == '0') {
3109 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3110 if (x < 0 && errno == 0) {
3111 return PyLong_FromString((char *)s,
3112 (char **)0,
3113 0);
3114 }
3115 }
3116 else
3117 x = PyOS_strtol((char *)s, (char **)&end, 0);
3118 if (*end == '\0') {
3119 if (errno != 0)
3120 return PyLong_FromString((char *)s, (char **)0, 0);
3121 return PyInt_FromLong(x);
3122 }
3123 /* XXX Huge floats may silently fail */
3124#ifndef WITHOUT_COMPLEX
3125 if (imflag) {
3126 c.real = 0.;
3127 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003128 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 PyFPE_END_PROTECT(c)
3130 return PyComplex_FromCComplex(c);
3131 }
3132 else
3133#endif
3134 {
3135 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00003136 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 PyFPE_END_PROTECT(dx)
3138 return PyFloat_FromDouble(dx);
3139 }
3140}
3141
3142static PyObject *
3143decode_utf8(const char **sPtr, const char *end, char* encoding)
3144{
3145#ifndef Py_USING_UNICODE
3146 Py_FatalError("decode_utf8 should not be called in this build.");
3147 return NULL;
3148#else
3149 PyObject *u, *v;
3150 char *s, *t;
3151 t = s = (char *)*sPtr;
3152 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3153 while (s < end && (*s & 0x80)) s++;
3154 *sPtr = s;
3155 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3156 if (u == NULL)
3157 return NULL;
3158 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3159 Py_DECREF(u);
3160 return v;
3161#endif
3162}
3163
3164static PyObject *
3165decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3166{
3167 PyObject *v, *u;
3168 char *buf;
3169 char *p;
3170 const char *end;
3171 if (encoding == NULL) {
3172 buf = (char *)s;
3173 u = NULL;
3174 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3175 buf = (char *)s;
3176 u = NULL;
3177 } else {
3178 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3179 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3180 if (u == NULL)
3181 return NULL;
3182 p = buf = PyString_AsString(u);
3183 end = s + len;
3184 while (s < end) {
3185 if (*s == '\\') {
3186 *p++ = *s++;
3187 if (*s & 0x80) {
3188 strcpy(p, "u005c");
3189 p += 5;
3190 }
3191 }
3192 if (*s & 0x80) { /* XXX inefficient */
3193 PyObject *w;
3194 char *r;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003195 Py_ssize_t rn, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 w = decode_utf8(&s, end, "utf-16-be");
3197 if (w == NULL) {
3198 Py_DECREF(u);
3199 return NULL;
3200 }
3201 r = PyString_AsString(w);
3202 rn = PyString_Size(w);
3203 assert(rn % 2 == 0);
3204 for (i = 0; i < rn; i += 2) {
3205 sprintf(p, "\\u%02x%02x",
3206 r[i + 0] & 0xFF,
3207 r[i + 1] & 0xFF);
3208 p += 6;
3209 }
3210 Py_DECREF(w);
3211 } else {
3212 *p++ = *s++;
3213 }
3214 }
3215 len = p - buf;
3216 s = buf;
3217 }
3218 if (rawmode)
3219 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3220 else
3221 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3222 Py_XDECREF(u);
3223 return v;
3224}
3225
3226/* s is a Python string literal, including the bracketing quote characters,
3227 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3228 * parsestr parses it, and returns the decoded Python string object.
3229 */
3230static PyObject *
3231parsestr(const char *s, const char *encoding)
3232{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00003234 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235 int rawmode = 0;
3236 int need_encoding;
3237 int unicode = 0;
3238
3239 if (isalpha(quote) || quote == '_') {
3240 if (quote == 'u' || quote == 'U') {
3241 quote = *++s;
3242 unicode = 1;
3243 }
3244 if (quote == 'r' || quote == 'R') {
3245 quote = *++s;
3246 rawmode = 1;
3247 }
3248 }
3249 if (quote != '\'' && quote != '\"') {
3250 PyErr_BadInternalCall();
3251 return NULL;
3252 }
3253 s++;
3254 len = strlen(s);
3255 if (len > INT_MAX) {
3256 PyErr_SetString(PyExc_OverflowError,
3257 "string to parse is too long");
3258 return NULL;
3259 }
3260 if (s[--len] != quote) {
3261 PyErr_BadInternalCall();
3262 return NULL;
3263 }
3264 if (len >= 4 && s[0] == quote && s[1] == quote) {
3265 s += 2;
3266 len -= 2;
3267 if (s[--len] != quote || s[--len] != quote) {
3268 PyErr_BadInternalCall();
3269 return NULL;
3270 }
3271 }
3272#ifdef Py_USING_UNICODE
3273 if (unicode || Py_UnicodeFlag) {
3274 return decode_unicode(s, len, rawmode, encoding);
3275 }
3276#endif
3277 need_encoding = (encoding != NULL &&
3278 strcmp(encoding, "utf-8") != 0 &&
3279 strcmp(encoding, "iso-8859-1") != 0);
3280 if (rawmode || strchr(s, '\\') == NULL) {
3281 if (need_encoding) {
3282#ifndef Py_USING_UNICODE
3283 /* This should not happen - we never see any other
3284 encoding. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003285 Py_FatalError(
3286 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003288 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 if (u == NULL)
3290 return NULL;
3291 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3292 Py_DECREF(u);
3293 return v;
3294#endif
3295 } else {
3296 return PyString_FromStringAndSize(s, len);
3297 }
3298 }
3299
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003300 return PyString_DecodeEscape(s, len, NULL, unicode,
3301 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302}
3303
3304/* Build a Python string object out of a STRING atom. This takes care of
3305 * compile-time literal catenation, calling parsestr() on each piece, and
3306 * pasting the intermediate results together.
3307 */
3308static PyObject *
3309parsestrplus(struct compiling *c, const node *n)
3310{
3311 PyObject *v;
3312 int i;
3313 REQ(CHILD(n, 0), STRING);
3314 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3315 /* String literal concatenation */
3316 for (i = 1; i < NCH(n); i++) {
3317 PyObject *s;
3318 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3319 if (s == NULL)
3320 goto onError;
3321 if (PyString_Check(v) && PyString_Check(s)) {
3322 PyString_ConcatAndDel(&v, s);
3323 if (v == NULL)
3324 goto onError;
3325 }
3326#ifdef Py_USING_UNICODE
3327 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003328 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330 Py_DECREF(v);
3331 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003332 if (v == NULL)
3333 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334 }
3335#endif
3336 }
3337 }
3338 return v;
3339
3340 onError:
3341 Py_XDECREF(v);
3342 return NULL;
3343}