blob: 5594ef3cf63d2435741a6c5db6cfbd1433a9e98a [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 *);
34static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
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 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 value = Py_BuildValue("(OO)", errstr, tmp);
111 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{
186 int i, j, num;
187 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";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 } else if (TYPE(n) == encoding_decl) {
195 c.c_encoding = STR(n);
196 n = CHILD(n, 0);
197 } else {
198 c.c_encoding = NULL;
199 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000200 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
202 switch (TYPE(n)) {
203 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000204 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205 if (!stmts)
206 return NULL;
207 for (i = 0; i < NCH(n) - 1; i++) {
208 ch = CHILD(n, i);
209 if (TYPE(ch) == NEWLINE)
210 continue;
211 REQ(ch, stmt);
212 num = num_stmts(ch);
213 if (num == 1) {
214 s = ast_for_stmt(&c, ch);
215 if (!s)
216 goto error;
217 asdl_seq_APPEND(stmts, s);
218 }
219 else {
220 ch = CHILD(ch, 0);
221 REQ(ch, simple_stmt);
222 for (j = 0; j < num; j++) {
223 s = ast_for_stmt(&c, CHILD(ch, j * 2));
224 if (!s)
225 goto error;
226 asdl_seq_APPEND(stmts, s);
227 }
228 }
229 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000230 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 case eval_input: {
232 expr_ty testlist_ast;
233
234 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000235 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 if (!testlist_ast)
237 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000238 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 }
240 case single_input:
241 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000242 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 if (!stmts)
244 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000245 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, arena));
246 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247 }
248 else {
249 n = CHILD(n, 0);
250 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 if (!stmts)
253 goto error;
254 if (num == 1) {
Neal Norwitz406c6402006-01-07 21:23:26 +0000255 s = ast_for_stmt(&c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256 if (!s)
257 goto error;
258 asdl_seq_SET(stmts, 0, s);
259 }
260 else {
261 /* Only a simple_stmt can contain multiple statements. */
262 REQ(n, simple_stmt);
263 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 if (TYPE(CHILD(n, i)) == NEWLINE)
265 break;
266 s = ast_for_stmt(&c, CHILD(n, i));
267 if (!s)
268 goto error;
269 asdl_seq_SET(stmts, i / 2, s);
270 }
271 }
272
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000273 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 }
275 default:
276 goto error;
277 }
278 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 ast_error_finish(filename);
280 return NULL;
281}
282
283/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
284*/
285
286static operator_ty
287get_operator(const node *n)
288{
289 switch (TYPE(n)) {
290 case VBAR:
291 return BitOr;
292 case CIRCUMFLEX:
293 return BitXor;
294 case AMPER:
295 return BitAnd;
296 case LEFTSHIFT:
297 return LShift;
298 case RIGHTSHIFT:
299 return RShift;
300 case PLUS:
301 return Add;
302 case MINUS:
303 return Sub;
304 case STAR:
305 return Mult;
306 case SLASH:
307 return Div;
308 case DOUBLESLASH:
309 return FloorDiv;
310 case PERCENT:
311 return Mod;
312 default:
313 return 0;
314 }
315}
316
317/* Set the context ctx for expr_ty e returning 0 on success, -1 on error.
318
319 Only sets context for expr kinds that "can appear in assignment context"
320 (according to ../Parser/Python.asdl). For other expr kinds, it sets
321 an appropriate syntax error and returns false.
322
323 If e is a sequential type, items in sequence will also have their context
324 set.
325
326*/
327
328static int
329set_context(expr_ty e, expr_context_ty ctx, const node *n)
330{
331 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000332 /* If a particular expression type can't be used for assign / delete,
333 set expr_name to its name and an error message will be generated.
334 */
335 const char* expr_name = NULL;
336
337 /* The ast defines augmented store and load contexts, but the
338 implementation here doesn't actually use them. The code may be
339 a little more complex than necessary as a result. It also means
340 that expressions in an augmented assignment have no context.
341 Consider restructuring so that augmented assignment uses
342 set_context(), too
343 */
344 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345
346 switch (e->kind) {
347 case Attribute_kind:
348 if (ctx == Store &&
349 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
350 return ast_error(n, "assignment to None");
351 }
352 e->v.Attribute.ctx = ctx;
353 break;
354 case Subscript_kind:
355 e->v.Subscript.ctx = ctx;
356 break;
357 case Name_kind:
358 if (ctx == Store &&
359 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
360 return ast_error(n, "assignment to None");
361 }
362 e->v.Name.ctx = ctx;
363 break;
364 case List_kind:
365 e->v.List.ctx = ctx;
366 s = e->v.List.elts;
367 break;
368 case Tuple_kind:
369 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
370 return ast_error(n, "can't assign to ()");
371 e->v.Tuple.ctx = ctx;
372 s = e->v.Tuple.elts;
373 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000374 case Lambda_kind:
375 expr_name = "lambda";
376 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000378 expr_name = "function call";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000382 case UnaryOp_kind:
383 expr_name = "operator";
384 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000386 expr_name = "generator expression";
387 break;
388 case ListComp_kind:
389 expr_name = "list comprehension";
390 break;
391 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392 case Num_kind:
393 case Str_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000394 expr_name = "literal";
395 break;
396 case Compare_kind:
397 expr_name = "comparison";
398 break;
399 case Repr_kind:
400 expr_name = "repr";
401 break;
402 default:
403 PyErr_Format(PyExc_SystemError,
404 "unexpected expression in assignment %d (line %d)",
405 e->kind, e->lineno);
406 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000408 /* Check for error string set by switch */
409 if (expr_name) {
410 char buf[300];
411 PyOS_snprintf(buf, sizeof(buf),
412 "can't %s %s",
413 ctx == Store ? "assign to" : "delete",
414 expr_name);
415 return ast_error(n, buf);
416 }
417
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418 /* If the LHS is a list or tuple, we need to set the assignment
419 context for all the tuple elements.
420 */
421 if (s) {
422 int i;
423
424 for (i = 0; i < asdl_seq_LEN(s); i++) {
425 if (!set_context(asdl_seq_GET(s, i), ctx, n))
426 return 0;
427 }
428 }
429 return 1;
430}
431
432static operator_ty
433ast_for_augassign(const node *n)
434{
435 REQ(n, augassign);
436 n = CHILD(n, 0);
437 switch (STR(n)[0]) {
438 case '+':
439 return Add;
440 case '-':
441 return Sub;
442 case '/':
443 if (STR(n)[1] == '/')
444 return FloorDiv;
445 else
446 return Div;
447 case '%':
448 return Mod;
449 case '<':
450 return LShift;
451 case '>':
452 return RShift;
453 case '&':
454 return BitAnd;
455 case '^':
456 return BitXor;
457 case '|':
458 return BitOr;
459 case '*':
460 if (STR(n)[1] == '*')
461 return Pow;
462 else
463 return Mult;
464 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000465 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466 return 0;
467 }
468}
469
470static cmpop_ty
471ast_for_comp_op(const node *n)
472{
473 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
474 |'is' 'not'
475 */
476 REQ(n, comp_op);
477 if (NCH(n) == 1) {
478 n = CHILD(n, 0);
479 switch (TYPE(n)) {
480 case LESS:
481 return Lt;
482 case GREATER:
483 return Gt;
484 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485 return Eq;
486 case LESSEQUAL:
487 return LtE;
488 case GREATEREQUAL:
489 return GtE;
490 case NOTEQUAL:
491 return NotEq;
492 case NAME:
493 if (strcmp(STR(n), "in") == 0)
494 return In;
495 if (strcmp(STR(n), "is") == 0)
496 return Is;
497 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000498 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 STR(n));
500 return 0;
501 }
502 }
503 else if (NCH(n) == 2) {
504 /* handle "not in" and "is not" */
505 switch (TYPE(CHILD(n, 0))) {
506 case NAME:
507 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
508 return NotIn;
509 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
510 return IsNot;
511 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000512 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
514 return 0;
515 }
516 }
Neal Norwitz79792652005-11-14 04:25:03 +0000517 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 NCH(n));
519 return 0;
520}
521
522static asdl_seq *
523seq_for_testlist(struct compiling *c, const node *n)
524{
525 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000526 asdl_seq *seq;
527 expr_ty expression;
528 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 assert(TYPE(n) == testlist
530 || TYPE(n) == listmaker
531 || TYPE(n) == testlist_gexp
532 || TYPE(n) == testlist_safe
533 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000535 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536 if (!seq)
537 return NULL;
538
539 for (i = 0; i < NCH(n); i += 2) {
Thomas Woutersfa443cd2006-02-27 15:43:57 +0000540 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
542 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000543 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
546 assert(i / 2 < seq->size);
547 asdl_seq_SET(seq, i / 2, expression);
548 }
549 return seq;
550}
551
552static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000553compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554{
555 int i, len = (NCH(n) + 1) / 2;
556 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000557 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558 if (!args)
559 return NULL;
560
561 REQ(n, fplist);
562
563 for (i = 0; i < len; i++) {
564 const node *child = CHILD(CHILD(n, 2*i), 0);
565 expr_ty arg;
566 if (TYPE(child) == NAME) {
567 if (!strcmp(STR(child), "None")) {
568 ast_error(child, "assignment to None");
569 return NULL;
570 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000571 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
572 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 }
574 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000575 arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 set_context(arg, Store, n);
577 asdl_seq_SET(args, i, arg);
578 }
579
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000580 result = Tuple(args, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 set_context(result, Store, n);
582 return result;
583}
584
585/* Create AST for argument list.
586
587 XXX TO DO:
588 - check for invalid argument lists like normal after default
589*/
590
591static arguments_ty
592ast_for_arguments(struct compiling *c, const node *n)
593{
594 /* parameters: '(' [varargslist] ')'
595 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
596 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
597 */
598 int i, n_args = 0, n_defaults = 0, found_default = 0;
599 asdl_seq *args, *defaults;
600 identifier vararg = NULL, kwarg = NULL;
601 node *ch;
602
603 if (TYPE(n) == parameters) {
604 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000605 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 n = CHILD(n, 1);
607 }
608 REQ(n, varargslist);
609
610 /* first count the number of normal args & defaults */
611 for (i = 0; i < NCH(n); i++) {
612 ch = CHILD(n, i);
Neal Norwitz84456bd2005-12-18 03:16:20 +0000613 if (TYPE(ch) == fpdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614 n_args++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 if (TYPE(ch) == EQUAL)
616 n_defaults++;
617 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000618 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 if (!args && n_args)
620 return NULL; /* Don't need to go to NULL; nothing allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000621 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 if (!defaults && n_defaults)
623 goto error;
624
625 /* fpdef: NAME | '(' fplist ')'
626 fplist: fpdef (',' fpdef)* [',']
627 */
628 i = 0;
629 while (i < NCH(n)) {
630 ch = CHILD(n, i);
631 switch (TYPE(ch)) {
632 case fpdef:
633 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
634 anything other than EQUAL or a comma? */
635 /* XXX Should NCH(n) check be made a separate check? */
636 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
637 asdl_seq_APPEND(defaults,
638 ast_for_expr(c, CHILD(n, i + 2)));
639 i += 2;
640 found_default = 1;
641 }
642 else if (found_default) {
643 ast_error(n,
644 "non-default argument follows default argument");
645 goto error;
646 }
647
648 if (NCH(ch) == 3) {
649 asdl_seq_APPEND(args,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000650 compiler_complex_args(c, CHILD(ch, 1)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 }
652 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000653 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
655 ast_error(CHILD(ch, 0), "assignment to None");
656 goto error;
657 }
Armin Rigo31441302005-10-21 12:57:31 +0000658 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000659 Param, LINENO(ch), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660 if (!name)
661 goto error;
662 asdl_seq_APPEND(args, name);
663
664 }
665 i += 2; /* the name and the comma */
666 break;
667 case STAR:
668 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
669 ast_error(CHILD(n, i+1), "assignment to None");
670 goto error;
671 }
672 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
673 i += 3;
674 break;
675 case DOUBLESTAR:
676 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
677 ast_error(CHILD(n, i+1), "assignment to None");
678 goto error;
679 }
680 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
681 i += 3;
682 break;
683 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000684 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 "unexpected node in varargslist: %d @ %d",
686 TYPE(ch), i);
687 goto error;
688 }
689 }
690
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000691 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692
693 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000694 Py_XDECREF(vararg);
695 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 return NULL;
697}
698
699static expr_ty
700ast_for_dotted_name(struct compiling *c, const node *n)
701{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000702 expr_ty e;
703 identifier id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 int i;
705
706 REQ(n, dotted_name);
707
708 id = NEW_IDENTIFIER(CHILD(n, 0));
709 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000710 return NULL;
711 e = Name(id, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714
715 for (i = 2; i < NCH(n); i+=2) {
716 id = NEW_IDENTIFIER(CHILD(n, i));
717 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000718 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000719 e = Attribute(e, id, Load, LINENO(CHILD(n, i)), c->c_arena);
720 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 }
723
724 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725}
726
727static expr_ty
728ast_for_decorator(struct compiling *c, const node *n)
729{
730 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
731 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000732 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733
734 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000735 REQ(CHILD(n, 0), AT);
736 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737
738 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
739 if (!name_expr)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741
742 if (NCH(n) == 3) { /* No arguments */
743 d = name_expr;
744 name_expr = NULL;
745 }
746 else if (NCH(n) == 5) { /* Call with no arguments */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000747 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000749 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 name_expr = NULL;
751 }
752 else {
753 d = ast_for_call(c, CHILD(n, 3), name_expr);
754 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 name_expr = NULL;
757 }
758
759 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760}
761
762static asdl_seq*
763ast_for_decorators(struct compiling *c, const node *n)
764{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000765 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000766 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 int i;
768
769 REQ(n, decorators);
770
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000771 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 if (!decorator_seq)
773 return NULL;
774
775 for (i = 0; i < NCH(n); i++) {
776 d = ast_for_decorator(c, CHILD(n, i));
777 if (!d)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000778 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 asdl_seq_APPEND(decorator_seq, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 }
781 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782}
783
784static stmt_ty
785ast_for_funcdef(struct compiling *c, const node *n)
786{
787 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000788 identifier name;
789 arguments_ty args;
790 asdl_seq *body;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 asdl_seq *decorator_seq = NULL;
792 int name_i;
793
794 REQ(n, funcdef);
795
796 if (NCH(n) == 6) { /* decorators are present */
797 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
798 if (!decorator_seq)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000799 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 name_i = 2;
801 }
802 else {
803 name_i = 1;
804 }
805
806 name = NEW_IDENTIFIER(CHILD(n, name_i));
807 if (!name)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000808 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000810 ast_error(CHILD(n, name_i), "assignment to None");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000811 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 }
813 args = ast_for_arguments(c, CHILD(n, name_i + 1));
814 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 body = ast_for_suite(c, CHILD(n, name_i + 3));
817 if (!body)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000818 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000820 return FunctionDef(name, args, body, decorator_seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821}
822
823static expr_ty
824ast_for_lambdef(struct compiling *c, const node *n)
825{
826 /* lambdef: 'lambda' [varargslist] ':' test */
827 arguments_ty args;
828 expr_ty expression;
829
830 if (NCH(n) == 3) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000831 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 if (!args)
833 return NULL;
834 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000835 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 }
838 else {
839 args = ast_for_arguments(c, CHILD(n, 1));
840 if (!args)
841 return NULL;
842 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000843 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 }
846
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000847 return Lambda(args, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848}
849
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000850static expr_ty
851ast_for_ifexpr(struct compiling *c, const node *n)
852{
853 /* test: or_test 'if' or_test 'else' test */
854 expr_ty expression, body, orelse;
855
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000856 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000857 body = ast_for_expr(c, CHILD(n, 0));
858 if (!body)
859 return NULL;
860 expression = ast_for_expr(c, CHILD(n, 2));
861 if (!expression)
862 return NULL;
863 orelse = ast_for_expr(c, CHILD(n, 4));
864 if (!orelse)
865 return NULL;
866 return IfExp(expression, body, orelse, LINENO(n), c->c_arena);
867}
868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869/* Count the number of 'for' loop in a list comprehension.
870
871 Helper for ast_for_listcomp().
872*/
873
874static int
875count_list_fors(const node *n)
876{
877 int n_fors = 0;
878 node *ch = CHILD(n, 1);
879
880 count_list_for:
881 n_fors++;
882 REQ(ch, list_for);
883 if (NCH(ch) == 5)
884 ch = CHILD(ch, 4);
885 else
886 return n_fors;
887 count_list_iter:
888 REQ(ch, list_iter);
889 ch = CHILD(ch, 0);
890 if (TYPE(ch) == list_for)
891 goto count_list_for;
892 else if (TYPE(ch) == list_if) {
893 if (NCH(ch) == 3) {
894 ch = CHILD(ch, 2);
895 goto count_list_iter;
896 }
897 else
898 return n_fors;
899 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000900
901 /* Should never be reached */
902 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
903 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904}
905
906/* Count the number of 'if' statements in a list comprehension.
907
908 Helper for ast_for_listcomp().
909*/
910
911static int
912count_list_ifs(const node *n)
913{
914 int n_ifs = 0;
915
916 count_list_iter:
917 REQ(n, list_iter);
918 if (TYPE(CHILD(n, 0)) == list_for)
919 return n_ifs;
920 n = CHILD(n, 0);
921 REQ(n, list_if);
922 n_ifs++;
923 if (NCH(n) == 2)
924 return n_ifs;
925 n = CHILD(n, 2);
926 goto count_list_iter;
927}
928
929static expr_ty
930ast_for_listcomp(struct compiling *c, const node *n)
931{
932 /* listmaker: test ( list_for | (',' test)* [','] )
933 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
934 list_iter: list_for | list_if
935 list_if: 'if' test [list_iter]
936 testlist_safe: test [(',' test)+ [',']]
937 */
938 expr_ty elt;
939 asdl_seq *listcomps;
940 int i, n_fors;
941 node *ch;
942
943 REQ(n, listmaker);
944 assert(NCH(n) > 1);
945
946 elt = ast_for_expr(c, CHILD(n, 0));
947 if (!elt)
948 return NULL;
949
950 n_fors = count_list_fors(n);
951 if (n_fors == -1)
952 return NULL;
953
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000954 listcomps = asdl_seq_new(n_fors, c->c_arena);
955 if (!listcomps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 ch = CHILD(n, 1);
959 for (i = 0; i < n_fors; i++) {
960 comprehension_ty lc;
961 asdl_seq *t;
962 expr_ty expression;
963
964 REQ(ch, list_for);
965
966 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000967 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000969 expression = ast_for_testlist(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000970 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000973 if (asdl_seq_LEN(t) == 1)
974 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
975 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000977 lc = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
978 expression, NULL, c->c_arena);
979 if (!lc)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981
982 if (NCH(ch) == 5) {
983 int j, n_ifs;
984 asdl_seq *ifs;
985
986 ch = CHILD(ch, 4);
987 n_ifs = count_list_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000988 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000991 ifs = asdl_seq_new(n_ifs, c->c_arena);
992 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994
995 for (j = 0; j < n_ifs; j++) {
996 REQ(ch, list_iter);
997
998 ch = CHILD(ch, 0);
999 REQ(ch, list_if);
1000
1001 asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1)));
1002 if (NCH(ch) == 3)
1003 ch = CHILD(ch, 2);
1004 }
1005 /* on exit, must guarantee that ch is a list_for */
1006 if (TYPE(ch) == list_iter)
1007 ch = CHILD(ch, 0);
1008 lc->ifs = ifs;
1009 }
1010 asdl_seq_APPEND(listcomps, lc);
1011 }
1012
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001013 return ListComp(elt, listcomps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014}
1015
1016/*
1017 Count the number of 'for' loops in a generator expression.
1018
1019 Helper for ast_for_genexp().
1020*/
1021
1022static int
1023count_gen_fors(const node *n)
1024{
1025 int n_fors = 0;
1026 node *ch = CHILD(n, 1);
1027
1028 count_gen_for:
1029 n_fors++;
1030 REQ(ch, gen_for);
1031 if (NCH(ch) == 5)
1032 ch = CHILD(ch, 4);
1033 else
1034 return n_fors;
1035 count_gen_iter:
1036 REQ(ch, gen_iter);
1037 ch = CHILD(ch, 0);
1038 if (TYPE(ch) == gen_for)
1039 goto count_gen_for;
1040 else if (TYPE(ch) == gen_if) {
1041 if (NCH(ch) == 3) {
1042 ch = CHILD(ch, 2);
1043 goto count_gen_iter;
1044 }
1045 else
1046 return n_fors;
1047 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001048
1049 /* Should never be reached */
1050 PyErr_SetString(PyExc_SystemError,
1051 "logic error in count_gen_fors");
1052 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053}
1054
1055/* Count the number of 'if' statements in a generator expression.
1056
1057 Helper for ast_for_genexp().
1058*/
1059
1060static int
1061count_gen_ifs(const node *n)
1062{
1063 int n_ifs = 0;
1064
1065 while (1) {
1066 REQ(n, gen_iter);
1067 if (TYPE(CHILD(n, 0)) == gen_for)
1068 return n_ifs;
1069 n = CHILD(n, 0);
1070 REQ(n, gen_if);
1071 n_ifs++;
1072 if (NCH(n) == 2)
1073 return n_ifs;
1074 n = CHILD(n, 2);
1075 }
1076}
1077
1078static expr_ty
1079ast_for_genexp(struct compiling *c, const node *n)
1080{
1081 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1082 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1083 expr_ty elt;
1084 asdl_seq *genexps;
1085 int i, n_fors;
1086 node *ch;
1087
1088 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1089 assert(NCH(n) > 1);
1090
1091 elt = ast_for_expr(c, CHILD(n, 0));
1092 if (!elt)
1093 return NULL;
1094
1095 n_fors = count_gen_fors(n);
1096 if (n_fors == -1)
1097 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001098
1099 genexps = asdl_seq_new(n_fors, c->c_arena);
1100 if (!genexps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 ch = CHILD(n, 1);
1104 for (i = 0; i < n_fors; i++) {
1105 comprehension_ty ge;
1106 asdl_seq *t;
1107 expr_ty expression;
1108
1109 REQ(ch, gen_for);
1110
1111 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001112 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001114 expression = ast_for_expr(c, CHILD(ch, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001115 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001117
1118 if (asdl_seq_LEN(t) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 ge = comprehension(asdl_seq_GET(t, 0), expression,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001120 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001122 ge = comprehension(Tuple(t, Store, LINENO(ch), c->c_arena),
1123 expression, NULL, c->c_arena);
1124
1125 if (!ge)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001127
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 if (NCH(ch) == 5) {
1129 int j, n_ifs;
1130 asdl_seq *ifs;
1131
1132 ch = CHILD(ch, 4);
1133 n_ifs = count_gen_ifs(ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001134 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001136
1137 ifs = asdl_seq_new(n_ifs, c->c_arena);
1138 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 for (j = 0; j < n_ifs; j++) {
1142 REQ(ch, gen_iter);
1143 ch = CHILD(ch, 0);
1144 REQ(ch, gen_if);
1145
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001146 expression = ast_for_expr(c, CHILD(ch, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001147 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001148 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001149 asdl_seq_APPEND(ifs, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 if (NCH(ch) == 3)
1151 ch = CHILD(ch, 2);
1152 }
1153 /* on exit, must guarantee that ch is a gen_for */
1154 if (TYPE(ch) == gen_iter)
1155 ch = CHILD(ch, 0);
1156 ge->ifs = ifs;
1157 }
1158 asdl_seq_APPEND(genexps, ge);
1159 }
1160
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001161 return GeneratorExp(elt, genexps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162}
1163
1164static expr_ty
1165ast_for_atom(struct compiling *c, const node *n)
1166{
1167 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1168 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1169 */
1170 node *ch = CHILD(n, 0);
1171
1172 switch (TYPE(ch)) {
1173 case NAME:
1174 /* All names start in Load context, but may later be
1175 changed. */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001176 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 case STRING: {
1178 PyObject *str = parsestrplus(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 if (!str)
1180 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001181
1182 PyArena_AddPyObject(c->c_arena, str);
1183 return Str(str, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 }
1185 case NUMBER: {
1186 PyObject *pynum = parsenumber(STR(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 if (!pynum)
1188 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001189
1190 PyArena_AddPyObject(c->c_arena, pynum);
1191 return Num(pynum, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 }
1193 case LPAR: /* some parenthesized expressions */
1194 ch = CHILD(n, 1);
1195
1196 if (TYPE(ch) == RPAR)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001197 return Tuple(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198
1199 if (TYPE(ch) == yield_expr)
1200 return ast_for_expr(c, ch);
1201
1202 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1203 return ast_for_genexp(c, ch);
1204
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001205 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 case LSQB: /* list (or list comprehension) */
1207 ch = CHILD(n, 1);
1208
1209 if (TYPE(ch) == RSQB)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001210 return List(NULL, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211
1212 REQ(ch, listmaker);
1213 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1214 asdl_seq *elts = seq_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 if (!elts)
1216 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001217
1218 return List(elts, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 }
1220 else
1221 return ast_for_listcomp(c, ch);
1222 case LBRACE: {
1223 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1224 int i, size;
1225 asdl_seq *keys, *values;
1226
1227 ch = CHILD(n, 1);
1228 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001229 keys = asdl_seq_new(size, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 if (!keys)
1231 return NULL;
1232
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001233 values = asdl_seq_new(size, c->c_arena);
1234 if (!values)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236
1237 for (i = 0; i < NCH(ch); i += 4) {
1238 expr_ty expression;
1239
1240 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001243
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001247 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001249
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 asdl_seq_SET(values, i / 4, expression);
1251 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252 return Dict(keys, values, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 }
1254 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001255 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 if (!expression)
1257 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001258
1259 return Repr(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 }
1261 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001262 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 return NULL;
1264 }
1265}
1266
1267static slice_ty
1268ast_for_slice(struct compiling *c, const node *n)
1269{
1270 node *ch;
1271 expr_ty lower = NULL, upper = NULL, step = NULL;
1272
1273 REQ(n, subscript);
1274
1275 /*
1276 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1277 sliceop: ':' [test]
1278 */
1279 ch = CHILD(n, 0);
1280 if (TYPE(ch) == DOT)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001281 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282
1283 if (NCH(n) == 1 && TYPE(ch) == test) {
1284 /* 'step' variable hold no significance in terms of being used over
1285 other vars */
1286 step = ast_for_expr(c, ch);
1287 if (!step)
1288 return NULL;
1289
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001290 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 }
1292
1293 if (TYPE(ch) == test) {
1294 lower = ast_for_expr(c, ch);
1295 if (!lower)
1296 return NULL;
1297 }
1298
1299 /* If there's an upper bound it's in the second or third position. */
1300 if (TYPE(ch) == COLON) {
1301 if (NCH(n) > 1) {
1302 node *n2 = CHILD(n, 1);
1303
1304 if (TYPE(n2) == test) {
1305 upper = ast_for_expr(c, n2);
1306 if (!upper)
1307 return NULL;
1308 }
1309 }
1310 } else if (NCH(n) > 2) {
1311 node *n2 = CHILD(n, 2);
1312
1313 if (TYPE(n2) == test) {
1314 upper = ast_for_expr(c, n2);
1315 if (!upper)
1316 return NULL;
1317 }
1318 }
1319
1320 ch = CHILD(n, NCH(n) - 1);
1321 if (TYPE(ch) == sliceop) {
1322 if (NCH(ch) == 1)
1323 /* XXX: If only 1 child, then should just be a colon. Should we
1324 just skip assigning and just get to the return? */
1325 ch = CHILD(ch, 0);
1326 else
1327 ch = CHILD(ch, 1);
1328 if (TYPE(ch) == test) {
1329 step = ast_for_expr(c, ch);
1330 if (!step)
1331 return NULL;
1332 }
1333 }
1334
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001335 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336}
1337
1338static expr_ty
1339ast_for_binop(struct compiling *c, const node *n)
1340{
1341 /* Must account for a sequence of expressions.
1342 How should A op B op C by represented?
1343 BinOp(BinOp(A, op, B), op, C).
1344 */
1345
1346 int i, nops;
1347 expr_ty expr1, expr2, result;
1348 operator_ty operator;
1349
1350 expr1 = ast_for_expr(c, CHILD(n, 0));
1351 if (!expr1)
1352 return NULL;
1353
1354 expr2 = ast_for_expr(c, CHILD(n, 2));
1355 if (!expr2)
1356 return NULL;
1357
1358 operator = get_operator(CHILD(n, 1));
1359 if (!operator)
1360 return NULL;
1361
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001362 result = BinOp(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 if (!result)
1364 return NULL;
1365
1366 nops = (NCH(n) - 1) / 2;
1367 for (i = 1; i < nops; i++) {
1368 expr_ty tmp_result, tmp;
1369 const node* next_oper = CHILD(n, i * 2 + 1);
1370
1371 operator = get_operator(next_oper);
1372 if (!operator)
1373 return NULL;
1374
1375 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1376 if (!tmp)
1377 return NULL;
1378
1379 tmp_result = BinOp(result, operator, tmp,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001380 LINENO(next_oper), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 if (!tmp)
1382 return NULL;
1383 result = tmp_result;
1384 }
1385 return result;
1386}
1387
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001388static expr_ty
1389ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1390{
1391 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001392 REQ(n, trailer);
1393 if (TYPE(CHILD(n, 0)) == LPAR) {
1394 if (NCH(n) == 2)
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001395 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001396 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001397 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001398 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001399 else if (TYPE(CHILD(n, 0)) == DOT ) {
1400 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1401 LINENO(n), c->c_arena);
1402 }
1403 else {
1404 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001405 REQ(CHILD(n, 2), RSQB);
1406 n = CHILD(n, 1);
1407 if (NCH(n) <= 2) {
1408 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1409 if (!slc)
1410 return NULL;
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001411 return Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001412 }
1413 else {
1414 int j;
1415 slice_ty slc;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001416 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001417 if (!slices)
1418 return NULL;
1419 for (j = 0; j < NCH(n); j += 2) {
1420 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001421 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001422 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001423 asdl_seq_SET(slices, j / 2, slc);
1424 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001425 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1426 Load, LINENO(n), c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001427 }
1428 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001429}
1430
1431static expr_ty
1432ast_for_power(struct compiling *c, const node *n)
1433{
1434 /* power: atom trailer* ('**' factor)*
1435 */
1436 int i;
1437 expr_ty e, tmp;
1438 REQ(n, power);
1439 e = ast_for_atom(c, CHILD(n, 0));
1440 if (!e)
1441 return NULL;
1442 if (NCH(n) == 1)
1443 return e;
1444 for (i = 1; i < NCH(n); i++) {
1445 node *ch = CHILD(n, i);
1446 if (TYPE(ch) != trailer)
1447 break;
1448 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001449 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001450 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001451 e = tmp;
1452 }
1453 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1454 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001455 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001456 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001457 tmp = BinOp(e, Pow, f, LINENO(n), c->c_arena);
1458 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001459 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001460 e = tmp;
1461 }
1462 return e;
1463}
1464
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465/* Do not name a variable 'expr'! Will cause a compile error.
1466*/
1467
1468static expr_ty
1469ast_for_expr(struct compiling *c, const node *n)
1470{
1471 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001472 test: or_test ['if' or_test 'else' test] | lambdef
1473 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 and_test: not_test ('and' not_test)*
1475 not_test: 'not' not_test | comparison
1476 comparison: expr (comp_op expr)*
1477 expr: xor_expr ('|' xor_expr)*
1478 xor_expr: and_expr ('^' and_expr)*
1479 and_expr: shift_expr ('&' shift_expr)*
1480 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1481 arith_expr: term (('+'|'-') term)*
1482 term: factor (('*'|'/'|'%'|'//') factor)*
1483 factor: ('+'|'-'|'~') factor | power
1484 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001485
1486 As well as modified versions that exist for backward compatibility,
1487 to explicitly allow:
1488 [ x for x in lambda: 0, lambda: 1 ]
1489 (which would be ambiguous without these extra rules)
1490
1491 old_test: or_test | old_lambdef
1492 old_lambdef: 'lambda' [vararglist] ':' old_test
1493
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 */
1495
1496 asdl_seq *seq;
1497 int i;
1498
1499 loop:
1500 switch (TYPE(n)) {
1501 case test:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001502 case old_test:
1503 if (TYPE(CHILD(n, 0)) == lambdef ||
1504 TYPE(CHILD(n, 0)) == old_lambdef)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001506 else if (NCH(n) > 1)
1507 return ast_for_ifexpr(c, n);
1508 /* Fallthrough */
1509 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 case and_test:
1511 if (NCH(n) == 1) {
1512 n = CHILD(n, 0);
1513 goto loop;
1514 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001515 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 if (!seq)
1517 return NULL;
1518 for (i = 0; i < NCH(n); i += 2) {
1519 expr_ty e = ast_for_expr(c, CHILD(n, i));
1520 if (!e)
1521 return NULL;
1522 asdl_seq_SET(seq, i / 2, e);
1523 }
1524 if (!strcmp(STR(CHILD(n, 1)), "and"))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001525 return BoolOp(And, seq, LINENO(n), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001526 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1527 return BoolOp(Or, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 case not_test:
1529 if (NCH(n) == 1) {
1530 n = CHILD(n, 0);
1531 goto loop;
1532 }
1533 else {
1534 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1535 if (!expression)
1536 return NULL;
1537
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001538 return UnaryOp(Not, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 }
1540 case comparison:
1541 if (NCH(n) == 1) {
1542 n = CHILD(n, 0);
1543 goto loop;
1544 }
1545 else {
1546 expr_ty expression;
1547 asdl_seq *ops, *cmps;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001548 ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 if (!ops)
1550 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001551 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 return NULL;
1554 }
1555 for (i = 1; i < NCH(n); i += 2) {
1556 /* XXX cmpop_ty is just an enum */
1557 cmpop_ty operator;
1558
1559 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001560 if (!operator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001562 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563
1564 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001565 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001567 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001569 asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 asdl_seq_SET(cmps, i / 2, expression);
1571 }
1572 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001573 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001575 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001577 return Compare(expression, ops, cmps, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 }
1579 break;
1580
1581 /* The next five cases all handle BinOps. The main body of code
1582 is the same in each case, but the switch turned inside out to
1583 reuse the code for each type of operator.
1584 */
1585 case expr:
1586 case xor_expr:
1587 case and_expr:
1588 case shift_expr:
1589 case arith_expr:
1590 case term:
1591 if (NCH(n) == 1) {
1592 n = CHILD(n, 0);
1593 goto loop;
1594 }
1595 return ast_for_binop(c, n);
1596 case yield_expr: {
1597 expr_ty exp = NULL;
1598 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001599 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 if (!exp)
1601 return NULL;
1602 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001603 return Yield(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 }
1605 case factor: {
1606 expr_ty expression;
1607
1608 if (NCH(n) == 1) {
1609 n = CHILD(n, 0);
1610 goto loop;
1611 }
1612
1613 expression = ast_for_expr(c, CHILD(n, 1));
1614 if (!expression)
1615 return NULL;
1616
1617 switch (TYPE(CHILD(n, 0))) {
1618 case PLUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001619 return UnaryOp(UAdd, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 case MINUS:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001621 return UnaryOp(USub, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 case TILDE:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001623 return UnaryOp(Invert, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001625 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1626 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627 break;
1628 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001629 case power:
1630 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001632 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633 return NULL;
1634 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001635 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 return NULL;
1637}
1638
1639static expr_ty
1640ast_for_call(struct compiling *c, const node *n, expr_ty func)
1641{
1642 /*
1643 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1644 | '**' test)
1645 argument: [test '='] test [gen_for] # Really [keyword '='] test
1646 */
1647
1648 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001649 asdl_seq *args;
1650 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651 expr_ty vararg = NULL, kwarg = NULL;
1652
1653 REQ(n, arglist);
1654
1655 nargs = 0;
1656 nkeywords = 0;
1657 ngens = 0;
1658 for (i = 0; i < NCH(n); i++) {
1659 node *ch = CHILD(n, i);
1660 if (TYPE(ch) == argument) {
1661 if (NCH(ch) == 1)
1662 nargs++;
1663 else if (TYPE(CHILD(ch, 1)) == gen_for)
1664 ngens++;
1665 else
1666 nkeywords++;
1667 }
1668 }
1669 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001670 ast_error(n, "Generator expression must be parenthesized "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 "if not sole argument");
1672 return NULL;
1673 }
1674
1675 if (nargs + nkeywords + ngens > 255) {
1676 ast_error(n, "more than 255 arguments");
1677 return NULL;
1678 }
1679
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001680 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001682 return NULL;
1683 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 nargs = 0;
1687 nkeywords = 0;
1688 for (i = 0; i < NCH(n); i++) {
1689 node *ch = CHILD(n, i);
1690 if (TYPE(ch) == argument) {
1691 expr_ty e;
1692 if (NCH(ch) == 1) {
1693 e = ast_for_expr(c, CHILD(ch, 0));
1694 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001695 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 asdl_seq_SET(args, nargs++, e);
1697 }
1698 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1699 e = ast_for_genexp(c, ch);
1700 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001701 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 asdl_seq_SET(args, nargs++, e);
1703 }
1704 else {
1705 keyword_ty kw;
1706 identifier key;
1707
1708 /* CHILD(ch, 0) is test, but must be an identifier? */
1709 e = ast_for_expr(c, CHILD(ch, 0));
1710 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001711 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 /* f(lambda x: x[0] = 3) ends up getting parsed with
1713 * LHS test = lambda x: x[0], and RHS test = 3.
1714 * SF bug 132313 points out that complaining about a keyword
1715 * then is very confusing.
1716 */
1717 if (e->kind == Lambda_kind) {
1718 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001719 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 } else if (e->kind != Name_kind) {
1721 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001722 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 }
1724 key = e->v.Name.id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 e = ast_for_expr(c, CHILD(ch, 2));
1726 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001727 return NULL;
1728 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001730 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 asdl_seq_SET(keywords, nkeywords++, kw);
1732 }
1733 }
1734 else if (TYPE(ch) == STAR) {
1735 vararg = ast_for_expr(c, CHILD(n, i+1));
1736 i++;
1737 }
1738 else if (TYPE(ch) == DOUBLESTAR) {
1739 kwarg = ast_for_expr(c, CHILD(n, i+1));
1740 i++;
1741 }
1742 }
1743
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001744 return Call(func, args, keywords, vararg, kwarg, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745}
1746
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001748ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001750 /* testlist_gexp: test (',' test)* [','] */
1751 /* testlist: test (',' test)* [','] */
1752 /* testlist_safe: test (',' test)+ [','] */
1753 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001755 if (TYPE(n) == testlist_gexp) {
1756 if (NCH(n) > 1)
1757 assert(TYPE(CHILD(n, 1)) != gen_for);
1758 }
1759 else {
1760 assert(TYPE(n) == testlist ||
1761 TYPE(n) == testlist_safe ||
1762 TYPE(n) == testlist1);
1763 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 if (NCH(n) == 1)
1765 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 else {
1767 asdl_seq *tmp = seq_for_testlist(c, n);
1768 if (!tmp)
1769 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001770 return Tuple(tmp, Load, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001772}
1773
1774static expr_ty
1775ast_for_testlist_gexp(struct compiling *c, const node* n)
1776{
1777 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1778 /* argument: test [ gen_for ] */
1779 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001780 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001781 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001782 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001783}
1784
1785/* like ast_for_testlist() but returns a sequence */
1786static asdl_seq*
1787ast_for_class_bases(struct compiling *c, const node* n)
1788{
1789 /* testlist: test (',' test)* [','] */
1790 assert(NCH(n) > 0);
1791 REQ(n, testlist);
1792 if (NCH(n) == 1) {
1793 expr_ty base;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001794 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001795 if (!bases)
1796 return NULL;
1797 base = ast_for_expr(c, CHILD(n, 0));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001798 if (!base)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001799 return NULL;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001800 asdl_seq_SET(bases, 0, base);
1801 return bases;
1802 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001803
1804 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805}
1806
1807static stmt_ty
1808ast_for_expr_stmt(struct compiling *c, const node *n)
1809{
1810 REQ(n, expr_stmt);
1811 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1812 | ('=' (yield_expr|testlist))*)
1813 testlist: test (',' test)* [',']
1814 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1815 | '<<=' | '>>=' | '**=' | '//='
1816 test: ... here starts the operator precendence dance
1817 */
1818
1819 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001820 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 if (!e)
1822 return NULL;
1823
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001824 return Expr(e, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 }
1826 else if (TYPE(CHILD(n, 1)) == augassign) {
1827 expr_ty expr1, expr2;
1828 operator_ty operator;
1829 node *ch = CHILD(n, 0);
1830
1831 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001832 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001834 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch),
1835 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836
1837 if (!expr1)
1838 return NULL;
Neal Norwitz96e48d42006-02-05 02:07:19 +00001839 /* TODO(jhylton): Figure out why set_context() can't be used here. */
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001840 switch (expr1->kind) {
1841 case GeneratorExp_kind:
1842 ast_error(ch, "augmented assignment to generator "
1843 "expression not possible");
1844 return NULL;
1845 case Name_kind: {
1846 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1847 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1848 ast_error(ch, "assignment to None");
1849 return NULL;
1850 }
1851 break;
1852 }
1853 case Attribute_kind:
1854 case Subscript_kind:
1855 break;
1856 default:
1857 ast_error(ch, "illegal expression for augmented "
1858 "assignment");
1859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861
1862 ch = CHILD(n, 2);
1863 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001864 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001866 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001867 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 return NULL;
1869
1870 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001871 if (!operator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 return NULL;
1873
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001874 return AugAssign(expr1, operator, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 }
1876 else {
1877 int i;
1878 asdl_seq *targets;
1879 node *value;
1880 expr_ty expression;
1881
1882 /* a normal assignment */
1883 REQ(CHILD(n, 1), EQUAL);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 if (!targets)
1886 return NULL;
1887 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001888 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 node *ch = CHILD(n, i);
1890 if (TYPE(ch) == yield_expr) {
1891 ast_error(ch, "assignment to yield expression not possible");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001894 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895
1896 /* set context to assign */
1897 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001898 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899
Neal Norwitz84456bd2005-12-18 03:16:20 +00001900 if (!set_context(e, Store, CHILD(n, i)))
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001901 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902
1903 asdl_seq_SET(targets, i / 2, e);
1904 }
1905 value = CHILD(n, NCH(n) - 1);
1906 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001907 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 else
1909 expression = ast_for_expr(c, value);
1910 if (!expression)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001911 return NULL;
1912 return Assign(targets, expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914}
1915
1916static stmt_ty
1917ast_for_print_stmt(struct compiling *c, const node *n)
1918{
1919 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1920 | '>>' test [ (',' test)+ [','] ] )
1921 */
1922 expr_ty dest = NULL, expression;
1923 asdl_seq *seq;
1924 bool nl;
1925 int i, start = 1;
1926
1927 REQ(n, print_stmt);
1928 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1929 dest = ast_for_expr(c, CHILD(n, 2));
1930 if (!dest)
1931 return NULL;
1932 start = 4;
1933 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001934 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 if (!seq)
1936 return NULL;
1937 for (i = start; i < NCH(n); i += 2) {
1938 expression = ast_for_expr(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00001939 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941
1942 asdl_seq_APPEND(seq, expression);
1943 }
1944 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001945 return Print(dest, seq, nl, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946}
1947
1948static asdl_seq *
1949ast_for_exprlist(struct compiling *c, const node *n, int context)
1950{
1951 asdl_seq *seq;
1952 int i;
1953 expr_ty e;
1954
1955 REQ(n, exprlist);
1956
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001957 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 if (!seq)
1959 return NULL;
1960 for (i = 0; i < NCH(n); i += 2) {
1961 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001962 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001963 return NULL;
Neal Norwitz6b347892005-11-15 07:17:53 +00001964 asdl_seq_SET(seq, i / 2, e);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001965 if (context && !set_context(e, context, CHILD(n, i)))
1966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 }
1968 return seq;
1969}
1970
1971static stmt_ty
1972ast_for_del_stmt(struct compiling *c, const node *n)
1973{
1974 asdl_seq *expr_list;
1975
1976 /* del_stmt: 'del' exprlist */
1977 REQ(n, del_stmt);
1978
1979 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
1980 if (!expr_list)
1981 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982 return Delete(expr_list, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983}
1984
1985static stmt_ty
1986ast_for_flow_stmt(struct compiling *c, const node *n)
1987{
1988 /*
1989 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
1990 | yield_stmt
1991 break_stmt: 'break'
1992 continue_stmt: 'continue'
1993 return_stmt: 'return' [testlist]
1994 yield_stmt: yield_expr
1995 yield_expr: 'yield' testlist
1996 raise_stmt: 'raise' [test [',' test [',' test]]]
1997 */
1998 node *ch;
1999
2000 REQ(n, flow_stmt);
2001 ch = CHILD(n, 0);
2002 switch (TYPE(ch)) {
2003 case break_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002004 return Break(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 case continue_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002006 return Continue(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 case yield_stmt: { /* will reduce to yield_expr */
2008 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2009 if (!exp)
2010 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002011 return Expr(exp, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 }
2013 case return_stmt:
2014 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002015 return Return(NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002017 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 if (!expression)
2019 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002020 return Return(expression, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 }
2022 case raise_stmt:
2023 if (NCH(ch) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002024 return Raise(NULL, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 else if (NCH(ch) == 2) {
2026 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2027 if (!expression)
2028 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002029 return Raise(expression, NULL, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 }
2031 else if (NCH(ch) == 4) {
2032 expr_ty expr1, expr2;
2033
2034 expr1 = ast_for_expr(c, CHILD(ch, 1));
2035 if (!expr1)
2036 return NULL;
2037 expr2 = ast_for_expr(c, CHILD(ch, 3));
2038 if (!expr2)
2039 return NULL;
2040
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002041 return Raise(expr1, expr2, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 }
2043 else if (NCH(ch) == 6) {
2044 expr_ty expr1, expr2, expr3;
2045
2046 expr1 = ast_for_expr(c, CHILD(ch, 1));
2047 if (!expr1)
2048 return NULL;
2049 expr2 = ast_for_expr(c, CHILD(ch, 3));
2050 if (!expr2)
2051 return NULL;
2052 expr3 = ast_for_expr(c, CHILD(ch, 5));
2053 if (!expr3)
2054 return NULL;
2055
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002056 return Raise(expr1, expr2, expr3, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 }
2058 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002059 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 "unexpected flow_stmt: %d", TYPE(ch));
2061 return NULL;
2062 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002063
2064 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2065 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066}
2067
2068static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002069alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070{
2071 /*
2072 import_as_name: NAME [NAME NAME]
2073 dotted_as_name: dotted_name [NAME NAME]
2074 dotted_name: NAME ('.' NAME)*
2075 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002076 PyObject *str;
2077
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 loop:
2079 switch (TYPE(n)) {
2080 case import_as_name:
Neal Norwitz84456bd2005-12-18 03:16:20 +00002081 str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
2082 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 case dotted_as_name:
2084 if (NCH(n) == 1) {
2085 n = CHILD(n, 0);
2086 goto loop;
2087 }
2088 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002089 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 assert(!a->asname);
2091 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2092 return a;
2093 }
2094 break;
2095 case dotted_name:
2096 if (NCH(n) == 1)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 else {
2099 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002100 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002101 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 char *s;
2103
2104 len = 0;
2105 for (i = 0; i < NCH(n); i += 2)
2106 /* length of string plus one for the dot */
2107 len += strlen(STR(CHILD(n, i))) + 1;
2108 len--; /* the last name doesn't have a dot */
2109 str = PyString_FromStringAndSize(NULL, len);
2110 if (!str)
2111 return NULL;
2112 s = PyString_AS_STRING(str);
2113 if (!s)
2114 return NULL;
2115 for (i = 0; i < NCH(n); i += 2) {
2116 char *sch = STR(CHILD(n, i));
2117 strcpy(s, STR(CHILD(n, i)));
2118 s += strlen(sch);
2119 *s++ = '.';
2120 }
2121 --s;
2122 *s = '\0';
2123 PyString_InternInPlace(&str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002124 PyArena_AddPyObject(c->c_arena, str);
2125 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 }
2127 break;
2128 case STAR:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002129 str = PyString_InternFromString("*");
2130 PyArena_AddPyObject(c->c_arena, str);
2131 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002133 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 "unexpected import name: %d", TYPE(n));
2135 return NULL;
2136 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002137
2138 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 return NULL;
2140}
2141
2142static stmt_ty
2143ast_for_import_stmt(struct compiling *c, const node *n)
2144{
2145 /*
2146 import_stmt: import_name | import_from
2147 import_name: 'import' dotted_as_names
2148 import_from: 'from' dotted_name 'import' ('*' |
2149 '(' import_as_names ')' |
2150 import_as_names)
2151 */
2152 int i;
2153 asdl_seq *aliases;
2154
2155 REQ(n, import_stmt);
2156 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002157 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002159 REQ(n, dotted_as_names);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002160 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 if (!aliases)
2162 return NULL;
2163 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002164 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002165 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 asdl_seq_SET(aliases, i / 2, import_alias);
2168 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002169 return Import(aliases, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002171 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 int n_children;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 int lineno = LINENO(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002174 alias_ty mod = alias_for_import_name(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 if (!mod)
2176 return NULL;
2177
Thomas Wouters106203c2006-02-27 17:05:19 +00002178 switch (TYPE(CHILD(n, 3))) {
2179 case STAR:
2180 /* from ... import * */
2181 n = CHILD(n, 3);
2182 n_children = 1;
2183 break;
2184 case LPAR:
2185 /* from ... import (x, y, z) */
2186 n = CHILD(n, 4);
2187 n_children = NCH(n);
2188 break;
2189 case import_as_names:
2190 /* from ... import x, y, z */
2191 n = CHILD(n, 3);
2192 n_children = NCH(n);
2193 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 ast_error(n, "trailing comma not allowed without"
2195 " surrounding parentheses");
2196 return NULL;
2197 }
Thomas Wouters106203c2006-02-27 17:05:19 +00002198 break;
2199 default:
2200 ast_error(n, "Unexpected node-type in from-import");
2201 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002202 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002204 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002205 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207
2208 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002209 if (TYPE(n) == STAR) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002210 alias_ty import_alias = alias_for_import_name(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002211 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 asdl_seq_APPEND(aliases, import_alias);
2214 }
2215
2216 for (i = 0; i < NCH(n); i += 2) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002217 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002218 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 asdl_seq_APPEND(aliases, import_alias);
2221 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002222 return ImportFrom(mod->name, aliases, lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 }
Neal Norwitz79792652005-11-14 04:25:03 +00002224 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 "unknown import statement: starts with command '%s'",
2226 STR(CHILD(n, 0)));
2227 return NULL;
2228}
2229
2230static stmt_ty
2231ast_for_global_stmt(struct compiling *c, const node *n)
2232{
2233 /* global_stmt: 'global' NAME (',' NAME)* */
2234 identifier name;
2235 asdl_seq *s;
2236 int i;
2237
2238 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002239 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 if (!s)
2241 return NULL;
2242 for (i = 1; i < NCH(n); i += 2) {
2243 name = NEW_IDENTIFIER(CHILD(n, i));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002244 if (!name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 asdl_seq_SET(s, i / 2, name);
2247 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002248 return Global(s, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249}
2250
2251static stmt_ty
2252ast_for_exec_stmt(struct compiling *c, const node *n)
2253{
2254 expr_ty expr1, globals = NULL, locals = NULL;
2255 int n_children = NCH(n);
2256 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002257 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 "poorly formed 'exec' statement: %d parts to statement",
2259 n_children);
2260 return NULL;
2261 }
2262
2263 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2264 REQ(n, exec_stmt);
2265 expr1 = ast_for_expr(c, CHILD(n, 1));
2266 if (!expr1)
2267 return NULL;
2268 if (n_children >= 4) {
2269 globals = ast_for_expr(c, CHILD(n, 3));
2270 if (!globals)
2271 return NULL;
2272 }
2273 if (n_children == 6) {
2274 locals = ast_for_expr(c, CHILD(n, 5));
2275 if (!locals)
2276 return NULL;
2277 }
2278
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002279 return Exec(expr1, globals, locals, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280}
2281
2282static stmt_ty
2283ast_for_assert_stmt(struct compiling *c, const node *n)
2284{
2285 /* assert_stmt: 'assert' test [',' test] */
2286 REQ(n, assert_stmt);
2287 if (NCH(n) == 2) {
2288 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2289 if (!expression)
2290 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002291 return Assert(expression, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 }
2293 else if (NCH(n) == 4) {
2294 expr_ty expr1, expr2;
2295
2296 expr1 = ast_for_expr(c, CHILD(n, 1));
2297 if (!expr1)
2298 return NULL;
2299 expr2 = ast_for_expr(c, CHILD(n, 3));
2300 if (!expr2)
2301 return NULL;
2302
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002303 return Assert(expr1, expr2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 }
Neal Norwitz79792652005-11-14 04:25:03 +00002305 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 "improper number of parts to 'assert' statement: %d",
2307 NCH(n));
2308 return NULL;
2309}
2310
2311static asdl_seq *
2312ast_for_suite(struct compiling *c, const node *n)
2313{
2314 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002315 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 stmt_ty s;
2317 int i, total, num, end, pos = 0;
2318 node *ch;
2319
2320 REQ(n, suite);
2321
2322 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002323 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 if (!seq)
2325 return NULL;
2326 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2327 n = CHILD(n, 0);
2328 /* simple_stmt always ends with a NEWLINE,
2329 and may have a trailing SEMI
2330 */
2331 end = NCH(n) - 1;
2332 if (TYPE(CHILD(n, end - 1)) == SEMI)
2333 end--;
2334 /* loop by 2 to skip semi-colons */
2335 for (i = 0; i < end; i += 2) {
2336 ch = CHILD(n, i);
2337 s = ast_for_stmt(c, ch);
2338 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002339 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 asdl_seq_SET(seq, pos++, s);
2341 }
2342 }
2343 else {
2344 for (i = 2; i < (NCH(n) - 1); i++) {
2345 ch = CHILD(n, i);
2346 REQ(ch, stmt);
2347 num = num_stmts(ch);
2348 if (num == 1) {
2349 /* small_stmt or compound_stmt with only one child */
2350 s = ast_for_stmt(c, ch);
2351 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002352 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002353 asdl_seq_SET(seq, pos++, s);
2354 }
2355 else {
2356 int j;
2357 ch = CHILD(ch, 0);
2358 REQ(ch, simple_stmt);
2359 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002360 /* statement terminates with a semi-colon ';' */
2361 if (NCH(CHILD(ch, j)) == 0) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002362 assert((j + 1) == NCH(ch));
2363 break;
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002364 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 s = ast_for_stmt(c, CHILD(ch, j));
2366 if (!s)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 asdl_seq_SET(seq, pos++, s);
2369 }
2370 }
2371 }
2372 }
2373 assert(pos == seq->size);
2374 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375}
2376
2377static stmt_ty
2378ast_for_if_stmt(struct compiling *c, const node *n)
2379{
2380 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2381 ['else' ':' suite]
2382 */
2383 char *s;
2384
2385 REQ(n, if_stmt);
2386
2387 if (NCH(n) == 4) {
2388 expr_ty expression;
2389 asdl_seq *suite_seq;
2390
2391 expression = ast_for_expr(c, CHILD(n, 1));
2392 if (!expression)
2393 return NULL;
2394 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002395 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 return NULL;
2397
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002398 return If(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002400
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 s = STR(CHILD(n, 4));
2402 /* s[2], the third character in the string, will be
2403 's' for el_s_e, or
2404 'i' for el_i_f
2405 */
2406 if (s[2] == 's') {
2407 expr_ty expression;
2408 asdl_seq *seq1, *seq2;
2409
2410 expression = ast_for_expr(c, CHILD(n, 1));
2411 if (!expression)
2412 return NULL;
2413 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002414 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 return NULL;
2416 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002417 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418 return NULL;
2419
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002420 return If(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 }
2422 else if (s[2] == 'i') {
2423 int i, n_elif, has_else = 0;
2424 asdl_seq *orelse = NULL;
2425 n_elif = NCH(n) - 4;
2426 /* must reference the child n_elif+1 since 'else' token is third,
2427 not fourth, child from the end. */
2428 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2429 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2430 has_else = 1;
2431 n_elif -= 3;
2432 }
2433 n_elif /= 4;
2434
2435 if (has_else) {
2436 expr_ty expression;
2437 asdl_seq *seq1, *seq2;
2438
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002439 orelse = asdl_seq_new(1, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 if (!orelse)
2441 return NULL;
2442 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002443 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002446 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002449 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451
2452 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002453 LINENO(CHILD(n, NCH(n) - 6)),
2454 c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 /* the just-created orelse handled the last elif */
2456 n_elif--;
2457 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458
2459 for (i = 0; i < n_elif; i++) {
2460 int off = 5 + (n_elif - i - 1) * 4;
2461 expr_ty expression;
2462 asdl_seq *suite_seq;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002463 asdl_seq *new = asdl_seq_new(1, c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002464 if (!new)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 return NULL;
2466 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002467 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002470 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472
2473 asdl_seq_SET(new, 0,
2474 If(expression, suite_seq, orelse,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002475 LINENO(CHILD(n, off)), c->c_arena));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 orelse = new;
2477 }
2478 return If(ast_for_expr(c, CHILD(n, 1)),
2479 ast_for_suite(c, CHILD(n, 3)),
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002480 orelse, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002482
2483 PyErr_Format(PyExc_SystemError,
2484 "unexpected token in 'if' statement: %s", s);
2485 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486}
2487
2488static stmt_ty
2489ast_for_while_stmt(struct compiling *c, const node *n)
2490{
2491 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2492 REQ(n, while_stmt);
2493
2494 if (NCH(n) == 4) {
2495 expr_ty expression;
2496 asdl_seq *suite_seq;
2497
2498 expression = ast_for_expr(c, CHILD(n, 1));
2499 if (!expression)
2500 return NULL;
2501 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002502 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002504 return While(expression, suite_seq, NULL, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 }
2506 else if (NCH(n) == 7) {
2507 expr_ty expression;
2508 asdl_seq *seq1, *seq2;
2509
2510 expression = ast_for_expr(c, CHILD(n, 1));
2511 if (!expression)
2512 return NULL;
2513 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002514 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 return NULL;
2516 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002517 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 return NULL;
2519
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002520 return While(expression, seq1, seq2, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002522
2523 PyErr_Format(PyExc_SystemError,
2524 "wrong number of tokens for 'while' statement: %d",
2525 NCH(n));
2526 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527}
2528
2529static stmt_ty
2530ast_for_for_stmt(struct compiling *c, const node *n)
2531{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002532 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 expr_ty expression;
2534 expr_ty target;
2535 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2536 REQ(n, for_stmt);
2537
2538 if (NCH(n) == 9) {
2539 seq = ast_for_suite(c, CHILD(n, 8));
2540 if (!seq)
2541 return NULL;
2542 }
2543
2544 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002545 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002547 if (asdl_seq_LEN(_target) == 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 target = asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002550 target = Tuple(_target, Store, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002552 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002553 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 return NULL;
2555 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002556 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 return NULL;
2558
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002559 return For(target, expression, suite_seq, seq, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560}
2561
2562static excepthandler_ty
2563ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2564{
2565 /* except_clause: 'except' [test [',' test]] */
2566 REQ(exc, except_clause);
2567 REQ(body, suite);
2568
2569 if (NCH(exc) == 1) {
2570 asdl_seq *suite_seq = ast_for_suite(c, body);
2571 if (!suite_seq)
2572 return NULL;
2573
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002574 return excepthandler(NULL, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 }
2576 else if (NCH(exc) == 2) {
2577 expr_ty expression;
2578 asdl_seq *suite_seq;
2579
2580 expression = ast_for_expr(c, CHILD(exc, 1));
2581 if (!expression)
2582 return NULL;
2583 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002584 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 return NULL;
2586
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002587 return excepthandler(expression, NULL, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 }
2589 else if (NCH(exc) == 4) {
2590 asdl_seq *suite_seq;
2591 expr_ty expression;
2592 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2593 if (!e)
2594 return NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002595 if (!set_context(e, Store, CHILD(exc, 3)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 return NULL;
2597 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002598 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 return NULL;
2600 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002601 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 return NULL;
2603
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002604 return excepthandler(expression, e, suite_seq, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002606
2607 PyErr_Format(PyExc_SystemError,
2608 "wrong number of children for 'except' clause: %d",
2609 NCH(exc));
2610 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611}
2612
2613static stmt_ty
2614ast_for_try_stmt(struct compiling *c, const node *n)
2615{
Neal Norwitzf599f422005-12-17 21:33:47 +00002616 const int nch = NCH(n);
2617 int n_except = (nch - 3)/3;
2618 asdl_seq *body, *orelse = NULL, *finally = NULL;
2619
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 REQ(n, try_stmt);
2621
Neal Norwitzf599f422005-12-17 21:33:47 +00002622 body = ast_for_suite(c, CHILD(n, 2));
2623 if (body == NULL)
2624 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625
Neal Norwitzf599f422005-12-17 21:33:47 +00002626 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2627 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2628 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2629 /* we can assume it's an "else",
2630 because nch >= 9 for try-else-finally and
2631 it would otherwise have a type of except_clause */
2632 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2633 if (orelse == NULL)
2634 return NULL;
2635 n_except--;
2636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637
Neal Norwitzf599f422005-12-17 21:33:47 +00002638 finally = ast_for_suite(c, CHILD(n, nch - 1));
2639 if (finally == NULL)
2640 return NULL;
2641 n_except--;
2642 }
2643 else {
2644 /* we can assume it's an "else",
2645 otherwise it would have a type of except_clause */
2646 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2647 if (orelse == NULL)
2648 return NULL;
2649 n_except--;
2650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002652 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002653 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 return NULL;
2655 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002656
2657 if (n_except > 0) {
2658 int i;
2659 stmt_ty except_st;
2660 /* process except statements to create a try ... except */
2661 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2662 if (handlers == NULL)
2663 return NULL;
2664
2665 for (i = 0; i < n_except; i++) {
2666 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2667 CHILD(n, 5 + i * 3));
2668 if (!e)
2669 return NULL;
2670 asdl_seq_SET(handlers, i, e);
2671 }
2672
2673 except_st = TryExcept(body, handlers, orelse, LINENO(n), c->c_arena);
2674 if (!finally)
2675 return except_st;
2676
2677 /* if a 'finally' is present too, we nest the TryExcept within a
2678 TryFinally to emulate try ... except ... finally */
2679 body = asdl_seq_new(1, c->c_arena);
2680 if (body == NULL)
2681 return NULL;
2682 asdl_seq_SET(body, 0, except_st);
2683 }
2684
2685 /* must be a try ... finally (except clauses are in body, if any exist) */
2686 assert(finally != NULL);
2687 return TryFinally(body, finally, LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688}
2689
2690static stmt_ty
2691ast_for_classdef(struct compiling *c, const node *n)
2692{
2693 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 asdl_seq *bases, *s;
2695
2696 REQ(n, classdef);
2697
2698 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2699 ast_error(n, "assignment to None");
2700 return NULL;
2701 }
2702
2703 if (NCH(n) == 4) {
2704 s = ast_for_suite(c, CHILD(n, 3));
2705 if (!s)
2706 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002707 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2708 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 }
2710 /* check for empty base list */
2711 if (TYPE(CHILD(n,3)) == RPAR) {
2712 s = ast_for_suite(c, CHILD(n,5));
2713 if (!s)
2714 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002715 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
2716 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 }
2718
2719 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002720 bases = ast_for_class_bases(c, CHILD(n, 3));
2721 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723
2724 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002725 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002727 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
2728 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729}
2730
2731static stmt_ty
2732ast_for_stmt(struct compiling *c, const node *n)
2733{
2734 if (TYPE(n) == stmt) {
2735 assert(NCH(n) == 1);
2736 n = CHILD(n, 0);
2737 }
2738 if (TYPE(n) == simple_stmt) {
2739 assert(num_stmts(n) == 1);
2740 n = CHILD(n, 0);
2741 }
2742 if (TYPE(n) == small_stmt) {
2743 REQ(n, small_stmt);
2744 n = CHILD(n, 0);
2745 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2746 | flow_stmt | import_stmt | global_stmt | exec_stmt
2747 | assert_stmt
2748 */
2749 switch (TYPE(n)) {
2750 case expr_stmt:
2751 return ast_for_expr_stmt(c, n);
2752 case print_stmt:
2753 return ast_for_print_stmt(c, n);
2754 case del_stmt:
2755 return ast_for_del_stmt(c, n);
2756 case pass_stmt:
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002757 return Pass(LINENO(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 case flow_stmt:
2759 return ast_for_flow_stmt(c, n);
2760 case import_stmt:
2761 return ast_for_import_stmt(c, n);
2762 case global_stmt:
2763 return ast_for_global_stmt(c, n);
2764 case exec_stmt:
2765 return ast_for_exec_stmt(c, n);
2766 case assert_stmt:
2767 return ast_for_assert_stmt(c, n);
2768 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002769 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2771 TYPE(n), NCH(n));
2772 return NULL;
2773 }
2774 }
2775 else {
2776 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2777 | funcdef | classdef
2778 */
2779 node *ch = CHILD(n, 0);
2780 REQ(n, compound_stmt);
2781 switch (TYPE(ch)) {
2782 case if_stmt:
2783 return ast_for_if_stmt(c, ch);
2784 case while_stmt:
2785 return ast_for_while_stmt(c, ch);
2786 case for_stmt:
2787 return ast_for_for_stmt(c, ch);
2788 case try_stmt:
2789 return ast_for_try_stmt(c, ch);
2790 case funcdef:
2791 return ast_for_funcdef(c, ch);
2792 case classdef:
2793 return ast_for_classdef(c, ch);
2794 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002795 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2797 TYPE(n), NCH(n));
2798 return NULL;
2799 }
2800 }
2801}
2802
2803static PyObject *
2804parsenumber(const char *s)
2805{
2806 const char *end;
2807 long x;
2808 double dx;
2809#ifndef WITHOUT_COMPLEX
2810 Py_complex c;
2811 int imflag;
2812#endif
2813
2814 errno = 0;
2815 end = s + strlen(s) - 1;
2816#ifndef WITHOUT_COMPLEX
2817 imflag = *end == 'j' || *end == 'J';
2818#endif
2819 if (*end == 'l' || *end == 'L')
2820 return PyLong_FromString((char *)s, (char **)0, 0);
2821 if (s[0] == '0') {
2822 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2823 if (x < 0 && errno == 0) {
2824 return PyLong_FromString((char *)s,
2825 (char **)0,
2826 0);
2827 }
2828 }
2829 else
2830 x = PyOS_strtol((char *)s, (char **)&end, 0);
2831 if (*end == '\0') {
2832 if (errno != 0)
2833 return PyLong_FromString((char *)s, (char **)0, 0);
2834 return PyInt_FromLong(x);
2835 }
2836 /* XXX Huge floats may silently fail */
2837#ifndef WITHOUT_COMPLEX
2838 if (imflag) {
2839 c.real = 0.;
2840 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002841 c.imag = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 PyFPE_END_PROTECT(c)
2843 return PyComplex_FromCComplex(c);
2844 }
2845 else
2846#endif
2847 {
2848 PyFPE_START_PROTECT("atof", return 0)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +00002849 dx = PyOS_ascii_atof(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850 PyFPE_END_PROTECT(dx)
2851 return PyFloat_FromDouble(dx);
2852 }
2853}
2854
2855static PyObject *
2856decode_utf8(const char **sPtr, const char *end, char* encoding)
2857{
2858#ifndef Py_USING_UNICODE
2859 Py_FatalError("decode_utf8 should not be called in this build.");
2860 return NULL;
2861#else
2862 PyObject *u, *v;
2863 char *s, *t;
2864 t = s = (char *)*sPtr;
2865 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2866 while (s < end && (*s & 0x80)) s++;
2867 *sPtr = s;
2868 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2869 if (u == NULL)
2870 return NULL;
2871 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2872 Py_DECREF(u);
2873 return v;
2874#endif
2875}
2876
2877static PyObject *
2878decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2879{
2880 PyObject *v, *u;
2881 char *buf;
2882 char *p;
2883 const char *end;
2884 if (encoding == NULL) {
2885 buf = (char *)s;
2886 u = NULL;
2887 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2888 buf = (char *)s;
2889 u = NULL;
2890 } else {
2891 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2892 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2893 if (u == NULL)
2894 return NULL;
2895 p = buf = PyString_AsString(u);
2896 end = s + len;
2897 while (s < end) {
2898 if (*s == '\\') {
2899 *p++ = *s++;
2900 if (*s & 0x80) {
2901 strcpy(p, "u005c");
2902 p += 5;
2903 }
2904 }
2905 if (*s & 0x80) { /* XXX inefficient */
2906 PyObject *w;
2907 char *r;
2908 int rn, i;
2909 w = decode_utf8(&s, end, "utf-16-be");
2910 if (w == NULL) {
2911 Py_DECREF(u);
2912 return NULL;
2913 }
2914 r = PyString_AsString(w);
2915 rn = PyString_Size(w);
2916 assert(rn % 2 == 0);
2917 for (i = 0; i < rn; i += 2) {
2918 sprintf(p, "\\u%02x%02x",
2919 r[i + 0] & 0xFF,
2920 r[i + 1] & 0xFF);
2921 p += 6;
2922 }
2923 Py_DECREF(w);
2924 } else {
2925 *p++ = *s++;
2926 }
2927 }
2928 len = p - buf;
2929 s = buf;
2930 }
2931 if (rawmode)
2932 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
2933 else
2934 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
2935 Py_XDECREF(u);
2936 return v;
2937}
2938
2939/* s is a Python string literal, including the bracketing quote characters,
2940 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
2941 * parsestr parses it, and returns the decoded Python string object.
2942 */
2943static PyObject *
2944parsestr(const char *s, const char *encoding)
2945{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 size_t len;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00002947 int quote = Py_CHARMASK(*s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 int rawmode = 0;
2949 int need_encoding;
2950 int unicode = 0;
2951
2952 if (isalpha(quote) || quote == '_') {
2953 if (quote == 'u' || quote == 'U') {
2954 quote = *++s;
2955 unicode = 1;
2956 }
2957 if (quote == 'r' || quote == 'R') {
2958 quote = *++s;
2959 rawmode = 1;
2960 }
2961 }
2962 if (quote != '\'' && quote != '\"') {
2963 PyErr_BadInternalCall();
2964 return NULL;
2965 }
2966 s++;
2967 len = strlen(s);
2968 if (len > INT_MAX) {
2969 PyErr_SetString(PyExc_OverflowError,
2970 "string to parse is too long");
2971 return NULL;
2972 }
2973 if (s[--len] != quote) {
2974 PyErr_BadInternalCall();
2975 return NULL;
2976 }
2977 if (len >= 4 && s[0] == quote && s[1] == quote) {
2978 s += 2;
2979 len -= 2;
2980 if (s[--len] != quote || s[--len] != quote) {
2981 PyErr_BadInternalCall();
2982 return NULL;
2983 }
2984 }
2985#ifdef Py_USING_UNICODE
2986 if (unicode || Py_UnicodeFlag) {
2987 return decode_unicode(s, len, rawmode, encoding);
2988 }
2989#endif
2990 need_encoding = (encoding != NULL &&
2991 strcmp(encoding, "utf-8") != 0 &&
2992 strcmp(encoding, "iso-8859-1") != 0);
2993 if (rawmode || strchr(s, '\\') == NULL) {
2994 if (need_encoding) {
2995#ifndef Py_USING_UNICODE
2996 /* This should not happen - we never see any other
2997 encoding. */
2998 Py_FatalError("cannot deal with encodings in this build.");
2999#else
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003000 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001 if (u == NULL)
3002 return NULL;
3003 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3004 Py_DECREF(u);
3005 return v;
3006#endif
3007 } else {
3008 return PyString_FromStringAndSize(s, len);
3009 }
3010 }
3011
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003012 return PyString_DecodeEscape(s, len, NULL, unicode,
3013 need_encoding ? encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014}
3015
3016/* Build a Python string object out of a STRING atom. This takes care of
3017 * compile-time literal catenation, calling parsestr() on each piece, and
3018 * pasting the intermediate results together.
3019 */
3020static PyObject *
3021parsestrplus(struct compiling *c, const node *n)
3022{
3023 PyObject *v;
3024 int i;
3025 REQ(CHILD(n, 0), STRING);
3026 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3027 /* String literal concatenation */
3028 for (i = 1; i < NCH(n); i++) {
3029 PyObject *s;
3030 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3031 if (s == NULL)
3032 goto onError;
3033 if (PyString_Check(v) && PyString_Check(s)) {
3034 PyString_ConcatAndDel(&v, s);
3035 if (v == NULL)
3036 goto onError;
3037 }
3038#ifdef Py_USING_UNICODE
3039 else {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003040 PyObject *temp = PyUnicode_Concat(v, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 Py_DECREF(s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 Py_DECREF(v);
3043 v = temp;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003044 if (v == NULL)
3045 goto onError;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046 }
3047#endif
3048 }
3049 }
3050 return v;
3051
3052 onError:
3053 Py_XDECREF(v);
3054 return NULL;
3055}