blob: 06f5fdfb3de95ede9dd5593828f1d79948139cbc [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/* Data structure used internally */
19struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000020 char *c_encoding; /* source encoding */
Christian Heimes3c608332008-03-26 22:01:37 +000021 int c_future_unicode; /* __future__ unicode literals flag */
Neal Norwitzadb69fc2005-12-17 20:54:49 +000022 PyArena *c_arena; /* arena for allocating memeory */
Christian Heimesffcd1e12007-11-24 01:36:02 +000023 const char *c_filename; /* filename */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024};
25
26static asdl_seq *seq_for_testlist(struct compiling *, const node *);
27static expr_ty ast_for_expr(struct compiling *, const node *);
28static stmt_ty ast_for_stmt(struct compiling *, const node *);
29static asdl_seq *ast_for_suite(struct compiling *, const node *);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000030static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
31 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000032static expr_ty ast_for_testlist(struct compiling *, const node *);
Christian Heimes5224d282008-02-23 15:01:05 +000033static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000034static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035
36/* Note different signature for ast_for_call */
37static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
38
Benjamin Peterson2b30ea02008-05-03 15:56:42 +000039static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes3c608332008-03-26 22:01:37 +000040static PyObject *parsestr(struct compiling *, const char *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041static PyObject *parsestrplus(struct compiling *, const node *n);
42
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043#ifndef LINENO
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000044#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045#endif
46
Neal Norwitzadb69fc2005-12-17 20:54:49 +000047static identifier
48new_identifier(const char* n, PyArena *arena) {
49 PyObject* id = PyString_InternFromString(n);
50 PyArena_AddPyObject(arena, id);
51 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052}
53
Neal Norwitzadb69fc2005-12-17 20:54:49 +000054#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055
56/* This routine provides an invalid object for the syntax error.
57 The outermost routine must unpack this error and create the
58 proper object. We do this so that we don't have to pass
59 the filename to everything function.
60
61 XXX Maybe we should just pass the filename...
62*/
63
64static int
65ast_error(const node *n, const char *errstr)
66{
67 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
68 if (!u)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000069 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070 PyErr_SetObject(PyExc_SyntaxError, u);
71 Py_DECREF(u);
72 return 0;
73}
74
75static void
76ast_error_finish(const char *filename)
77{
78 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
Neal Norwitz46b7bda2006-01-08 01:06:06 +000079 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080
81 assert(PyErr_Occurred());
82 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000083 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084
85 PyErr_Fetch(&type, &value, &tback);
86 errstr = PyTuple_GetItem(value, 0);
87 if (!errstr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000088 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089 Py_INCREF(errstr);
90 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000091 if (lineno == -1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000092 Py_DECREF(errstr);
93 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +000094 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095 Py_DECREF(value);
96
97 loc = PyErr_ProgramText(filename, lineno);
98 if (!loc) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +000099 Py_INCREF(Py_None);
100 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000101 }
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000102 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000104 if (!tmp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000105 Py_DECREF(errstr);
106 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000107 }
Georg Brandl7784f122006-05-26 20:04:44 +0000108 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109 Py_DECREF(errstr);
110 Py_DECREF(tmp);
111 if (!value)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000112 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113 PyErr_Restore(type, value, tback);
114}
115
116/* num_stmts() returns number of contained statements.
117
118 Use this routine to determine how big a sequence is needed for
119 the statements in a parse tree. Its raison d'etre is this bit of
120 grammar:
121
122 stmt: simple_stmt | compound_stmt
123 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
124
125 A simple_stmt can contain multiple small_stmt elements joined
126 by semicolons. If the arg is a simple_stmt, the number of
127 small_stmt elements is returned.
128*/
129
130static int
131num_stmts(const node *n)
132{
133 int i, l;
134 node *ch;
135
136 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000137 case single_input:
138 if (TYPE(CHILD(n, 0)) == NEWLINE)
139 return 0;
140 else
141 return num_stmts(CHILD(n, 0));
142 case file_input:
143 l = 0;
144 for (i = 0; i < NCH(n); i++) {
145 ch = CHILD(n, i);
146 if (TYPE(ch) == stmt)
147 l += num_stmts(ch);
148 }
149 return l;
150 case stmt:
151 return num_stmts(CHILD(n, 0));
152 case compound_stmt:
153 return 1;
154 case simple_stmt:
155 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
156 case suite:
157 if (NCH(n) == 1)
158 return num_stmts(CHILD(n, 0));
159 else {
160 l = 0;
161 for (i = 2; i < (NCH(n) - 1); i++)
162 l += num_stmts(CHILD(n, i));
163 return l;
164 }
165 default: {
166 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000168 sprintf(buf, "Non-statement found: %d %d\n",
169 TYPE(n), NCH(n));
170 Py_FatalError(buf);
171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172 }
173 assert(0);
174 return 0;
175}
176
177/* Transform the CST rooted at node * to the appropriate AST
178*/
179
180mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000181PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000182 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000184 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185 asdl_seq *stmts = NULL;
186 stmt_ty s;
187 node *ch;
188 struct compiling c;
189
190 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000191 c.c_encoding = "utf-8";
192 if (TYPE(n) == encoding_decl) {
193 ast_error(n, "encoding declaration in Unicode string");
194 goto error;
195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000197 c.c_encoding = STR(n);
198 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000200 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201 }
Christian Heimes3c608332008-03-26 22:01:37 +0000202 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000203 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000204 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltona8293132006-02-28 17:58:27 +0000206 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000208 case file_input:
209 stmts = asdl_seq_new(num_stmts(n), arena);
210 if (!stmts)
211 return NULL;
212 for (i = 0; i < NCH(n) - 1; i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == NEWLINE)
215 continue;
216 REQ(ch, stmt);
217 num = num_stmts(ch);
218 if (num == 1) {
219 s = ast_for_stmt(&c, ch);
220 if (!s)
221 goto error;
222 asdl_seq_SET(stmts, k++, s);
223 }
224 else {
225 ch = CHILD(ch, 0);
226 REQ(ch, simple_stmt);
227 for (j = 0; j < num; j++) {
228 s = ast_for_stmt(&c, CHILD(ch, j * 2));
229 if (!s)
230 goto error;
231 asdl_seq_SET(stmts, k++, s);
232 }
233 }
234 }
235 return Module(stmts, arena);
236 case eval_input: {
237 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000239 /* XXX Why not gen_for here? */
240 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
241 if (!testlist_ast)
242 goto error;
243 return Expression(testlist_ast, arena);
244 }
245 case single_input:
246 if (TYPE(CHILD(n, 0)) == NEWLINE) {
247 stmts = asdl_seq_new(1, arena);
248 if (!stmts)
249 goto error;
250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000252 if (!asdl_seq_GET(stmts, 0))
253 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000254 return Interactive(stmts, arena);
255 }
256 else {
257 n = CHILD(n, 0);
258 num = num_stmts(n);
259 stmts = asdl_seq_new(num, arena);
260 if (!stmts)
261 goto error;
262 if (num == 1) {
263 s = ast_for_stmt(&c, n);
264 if (!s)
265 goto error;
266 asdl_seq_SET(stmts, 0, s);
267 }
268 else {
269 /* Only a simple_stmt can contain multiple statements. */
270 REQ(n, simple_stmt);
271 for (i = 0; i < NCH(n); i += 2) {
272 if (TYPE(CHILD(n, i)) == NEWLINE)
273 break;
274 s = ast_for_stmt(&c, CHILD(n, i));
275 if (!s)
276 goto error;
277 asdl_seq_SET(stmts, i / 2, s);
278 }
279 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000281 return Interactive(stmts, arena);
282 }
283 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000284 PyErr_Format(PyExc_SystemError,
285 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000286 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287 }
288 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 ast_error_finish(filename);
290 return NULL;
291}
292
293/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
294*/
295
296static operator_ty
297get_operator(const node *n)
298{
299 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000300 case VBAR:
301 return BitOr;
302 case CIRCUMFLEX:
303 return BitXor;
304 case AMPER:
305 return BitAnd;
306 case LEFTSHIFT:
307 return LShift;
308 case RIGHTSHIFT:
309 return RShift;
310 case PLUS:
311 return Add;
312 case MINUS:
313 return Sub;
314 case STAR:
315 return Mult;
316 case SLASH:
317 return Div;
318 case DOUBLESLASH:
319 return FloorDiv;
320 case PERCENT:
321 return Mod;
322 default:
323 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 }
325}
326
Jeremy Hyltona8293132006-02-28 17:58:27 +0000327/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328
329 Only sets context for expr kinds that "can appear in assignment context"
330 (according to ../Parser/Python.asdl). For other expr kinds, it sets
331 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332*/
333
334static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000335set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336{
337 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000338 /* If a particular expression type can't be used for assign / delete,
339 set expr_name to its name and an error message will be generated.
340 */
341 const char* expr_name = NULL;
342
343 /* The ast defines augmented store and load contexts, but the
344 implementation here doesn't actually use them. The code may be
345 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000346 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000347 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000348 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000349 */
350 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351
352 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000353 case Attribute_kind:
354 if (ctx == Store &&
355 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
356 return ast_error(n, "assignment to None");
357 }
358 e->v.Attribute.ctx = ctx;
359 break;
360 case Subscript_kind:
361 e->v.Subscript.ctx = ctx;
362 break;
363 case Name_kind:
364 if (ctx == Store &&
365 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
366 return ast_error(n, "assignment to None");
367 }
368 e->v.Name.ctx = ctx;
369 break;
370 case List_kind:
371 e->v.List.ctx = ctx;
372 s = e->v.List.elts;
373 break;
374 case Tuple_kind:
375 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
376 return ast_error(n, "can't assign to ()");
377 e->v.Tuple.ctx = ctx;
378 s = e->v.Tuple.elts;
379 break;
380 case Lambda_kind:
381 expr_name = "lambda";
382 break;
383 case Call_kind:
384 expr_name = "function call";
385 break;
386 case BoolOp_kind:
387 case BinOp_kind:
388 case UnaryOp_kind:
389 expr_name = "operator";
390 break;
391 case GeneratorExp_kind:
392 expr_name = "generator expression";
393 break;
394 case Yield_kind:
395 expr_name = "yield expression";
396 break;
397 case ListComp_kind:
398 expr_name = "list comprehension";
399 break;
400 case Dict_kind:
401 case Num_kind:
402 case Str_kind:
403 expr_name = "literal";
404 break;
405 case Compare_kind:
406 expr_name = "comparison";
407 break;
408 case Repr_kind:
409 expr_name = "repr";
410 break;
411 case IfExp_kind:
412 expr_name = "conditional expression";
413 break;
414 default:
415 PyErr_Format(PyExc_SystemError,
416 "unexpected expression in assignment %d (line %d)",
417 e->kind, e->lineno);
418 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000419 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000420 /* Check for error string set by switch */
421 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000422 char buf[300];
423 PyOS_snprintf(buf, sizeof(buf),
424 "can't %s %s",
425 ctx == Store ? "assign to" : "delete",
426 expr_name);
427 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000428 }
429
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000431 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 */
433 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000434 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000436 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000437 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000438 return 0;
439 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440 }
441 return 1;
442}
443
444static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000445ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446{
447 REQ(n, augassign);
448 n = CHILD(n, 0);
449 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000450 case '+':
451 return Add;
452 case '-':
453 return Sub;
454 case '/':
455 if (STR(n)[1] == '/')
456 return FloorDiv;
457 else
458 return Div;
459 case '%':
460 return Mod;
461 case '<':
462 return LShift;
463 case '>':
464 return RShift;
465 case '&':
466 return BitAnd;
467 case '^':
468 return BitXor;
469 case '|':
470 return BitOr;
471 case '*':
472 if (STR(n)[1] == '*')
473 return Pow;
474 else
475 return Mult;
476 default:
477 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
478 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479 }
480}
481
482static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000483ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484{
485 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000486 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 */
488 REQ(n, comp_op);
489 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000490 n = CHILD(n, 0);
491 switch (TYPE(n)) {
492 case LESS:
493 return Lt;
494 case GREATER:
495 return Gt;
496 case EQEQUAL: /* == */
497 return Eq;
498 case LESSEQUAL:
499 return LtE;
500 case GREATEREQUAL:
501 return GtE;
502 case NOTEQUAL:
503 return NotEq;
504 case NAME:
505 if (strcmp(STR(n), "in") == 0)
506 return In;
507 if (strcmp(STR(n), "is") == 0)
508 return Is;
509 default:
510 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
511 STR(n));
512 return (cmpop_ty)0;
513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 }
515 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000516 /* handle "not in" and "is not" */
517 switch (TYPE(CHILD(n, 0))) {
518 case NAME:
519 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
520 return NotIn;
521 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
522 return IsNot;
523 default:
524 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
525 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
526 return (cmpop_ty)0;
527 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528 }
Neal Norwitz79792652005-11-14 04:25:03 +0000529 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000530 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000531 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532}
533
534static asdl_seq *
535seq_for_testlist(struct compiling *c, const node *n)
536{
537 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000538 asdl_seq *seq;
539 expr_ty expression;
540 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000541 assert(TYPE(n) == testlist ||
542 TYPE(n) == listmaker ||
543 TYPE(n) == testlist_gexp ||
544 TYPE(n) == testlist_safe ||
545 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000547 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
551 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000552 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000554 expression = ast_for_expr(c, CHILD(n, i));
555 if (!expression)
556 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000558 assert(i / 2 < seq->size);
559 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560 }
561 return seq;
562}
563
564static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000565compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566{
567 int i, len = (NCH(n) + 1) / 2;
568 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000569 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000571 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572
Neal Norwitz3a230172006-09-22 08:18:10 +0000573 /* fpdef: NAME | '(' fplist ')'
574 fplist: fpdef (',' fpdef)* [',']
575 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000578 const node *fpdef_node = CHILD(n, 2*i);
579 const node *child;
580 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000581set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000582 /* fpdef_node is either a NAME or an fplist */
583 child = CHILD(fpdef_node, 0);
584 if (TYPE(child) == NAME) {
585 if (!strcmp(STR(child), "None")) {
586 ast_error(child, "assignment to None");
587 return NULL;
588 }
589 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
590 child->n_col_offset, c->c_arena);
591 }
592 else {
593 assert(TYPE(fpdef_node) == fpdef);
594 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
595 child = CHILD(fpdef_node, 1);
596 assert(TYPE(child) == fplist);
597 /* NCH == 1 means we have (x), we need to elide the extra parens */
598 if (NCH(child) == 1) {
599 fpdef_node = CHILD(child, 0);
600 assert(TYPE(fpdef_node) == fpdef);
601 goto set_name;
602 }
603 arg = compiler_complex_args(c, child);
604 }
605 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 }
607
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000608 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000609 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000610 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611 return result;
612}
613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614
Jeremy Hyltona8293132006-02-28 17:58:27 +0000615/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616
617static arguments_ty
618ast_for_arguments(struct compiling *c, const node *n)
619{
620 /* parameters: '(' [varargslist] ')'
621 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000622 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000624 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 asdl_seq *args, *defaults;
626 identifier vararg = NULL, kwarg = NULL;
627 node *ch;
628
629 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000630 if (NCH(n) == 2) /* () as argument list */
631 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
632 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 }
634 REQ(n, varargslist);
635
636 /* first count the number of normal args & defaults */
637 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000638 ch = CHILD(n, i);
639 if (TYPE(ch) == fpdef)
640 n_args++;
641 if (TYPE(ch) == EQUAL)
642 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000644 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000646 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000647 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000649 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650
651 /* fpdef: NAME | '(' fplist ')'
652 fplist: fpdef (',' fpdef)* [',']
653 */
654 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000655 j = 0; /* index for defaults */
656 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000658 ch = CHILD(n, i);
659 switch (TYPE(ch)) {
660 case fpdef:
661 handle_fpdef:
662 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
663 anything other than EQUAL or a comma? */
664 /* XXX Should NCH(n) check be made a separate check? */
665 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
666 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
667 if (!expression)
668 goto error;
669 assert(defaults != NULL);
670 asdl_seq_SET(defaults, j++, expression);
671 i += 2;
672 found_default = 1;
673 }
674 else if (found_default) {
675 ast_error(n,
676 "non-default argument follows default argument");
677 goto error;
678 }
679 if (NCH(ch) == 3) {
680 ch = CHILD(ch, 1);
681 /* def foo((x)): is not complex, special case. */
682 if (NCH(ch) != 1) {
683 /* We have complex arguments, setup for unpacking. */
684 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000685 if (!asdl_seq_GET(args, k-1))
686 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000687 } else {
688 /* def foo((x)): setup for checking NAME below. */
689 /* Loop because there can be many parens and tuple
690 unpacking mixed in. */
691 ch = CHILD(ch, 0);
692 assert(TYPE(ch) == fpdef);
693 goto handle_fpdef;
694 }
695 }
696 if (TYPE(CHILD(ch, 0)) == NAME) {
697 expr_ty name;
698 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
699 ast_error(CHILD(ch, 0), "assignment to None");
700 goto error;
701 }
702 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
703 Param, LINENO(ch), ch->n_col_offset,
704 c->c_arena);
705 if (!name)
706 goto error;
707 asdl_seq_SET(args, k++, name);
708
709 }
710 i += 2; /* the name and the comma */
711 break;
712 case STAR:
713 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
714 ast_error(CHILD(n, i+1), "assignment to None");
715 goto error;
716 }
717 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
718 i += 3;
719 break;
720 case DOUBLESTAR:
721 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
722 ast_error(CHILD(n, i+1), "assignment to None");
723 goto error;
724 }
725 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
726 i += 3;
727 break;
728 default:
729 PyErr_Format(PyExc_SystemError,
730 "unexpected node in varargslist: %d @ %d",
731 TYPE(ch), i);
732 goto error;
733 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 }
735
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000736 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737
738 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000739 Py_XDECREF(vararg);
740 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 return NULL;
742}
743
744static expr_ty
745ast_for_dotted_name(struct compiling *c, const node *n)
746{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000747 expr_ty e;
748 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000749 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 int i;
751
752 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000753
754 lineno = LINENO(n);
755 col_offset = n->n_col_offset;
756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 id = NEW_IDENTIFIER(CHILD(n, 0));
758 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000759 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000760 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000762 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
764 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000765 id = NEW_IDENTIFIER(CHILD(n, i));
766 if (!id)
767 return NULL;
768 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
769 if (!e)
770 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 }
772
773 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774}
775
776static expr_ty
777ast_for_decorator(struct compiling *c, const node *n)
778{
779 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
780 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000781 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782
783 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000784 REQ(CHILD(n, 0), AT);
785 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786
787 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
788 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000789 return NULL;
790
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000792 d = name_expr;
793 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 }
795 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000796 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
797 n->n_col_offset, c->c_arena);
798 if (!d)
799 return NULL;
800 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 }
802 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000803 d = ast_for_call(c, CHILD(n, 3), name_expr);
804 if (!d)
805 return NULL;
806 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808
809 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810}
811
812static asdl_seq*
813ast_for_decorators(struct compiling *c, const node *n)
814{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000815 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000816 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 int i;
818
819 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000820 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000822 return NULL;
823
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000825 d = ast_for_decorator(c, CHILD(n, i));
826 if (!d)
827 return NULL;
828 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 }
830 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831}
832
833static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000834ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835{
Christian Heimes5224d282008-02-23 15:01:05 +0000836 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000837 identifier name;
838 arguments_ty args;
839 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000840 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841
842 REQ(n, funcdef);
843
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 name = NEW_IDENTIFIER(CHILD(n, name_i));
845 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000848 ast_error(CHILD(n, name_i), "assignment to None");
849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 }
851 args = ast_for_arguments(c, CHILD(n, name_i + 1));
852 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000853 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 body = ast_for_suite(c, CHILD(n, name_i + 3));
855 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000858 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000859 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860}
861
Christian Heimes5224d282008-02-23 15:01:05 +0000862static stmt_ty
863ast_for_decorated(struct compiling *c, const node *n)
864{
865 /* decorated: decorators (classdef | funcdef) */
866 stmt_ty thing = NULL;
867 asdl_seq *decorator_seq = NULL;
868
869 REQ(n, decorated);
870
871 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
872 if (!decorator_seq)
873 return NULL;
874
875 assert(TYPE(CHILD(n, 1)) == funcdef ||
876 TYPE(CHILD(n, 1)) == classdef);
877
878 if (TYPE(CHILD(n, 1)) == funcdef) {
879 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
880 } else if (TYPE(CHILD(n, 1)) == classdef) {
881 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
882 }
883 /* we count the decorators in when talking about the class' or
884 function's line number */
885 if (thing) {
886 thing->lineno = LINENO(n);
887 thing->col_offset = n->n_col_offset;
888 }
889 return thing;
890}
891
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892static expr_ty
893ast_for_lambdef(struct compiling *c, const node *n)
894{
895 /* lambdef: 'lambda' [varargslist] ':' test */
896 arguments_ty args;
897 expr_ty expression;
898
899 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000900 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
901 if (!args)
902 return NULL;
903 expression = ast_for_expr(c, CHILD(n, 2));
904 if (!expression)
905 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 }
907 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000908 args = ast_for_arguments(c, CHILD(n, 1));
909 if (!args)
910 return NULL;
911 expression = ast_for_expr(c, CHILD(n, 3));
912 if (!expression)
913 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 }
915
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000916 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917}
918
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000919static expr_ty
920ast_for_ifexpr(struct compiling *c, const node *n)
921{
922 /* test: or_test 'if' or_test 'else' test */
923 expr_ty expression, body, orelse;
924
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000925 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000926 body = ast_for_expr(c, CHILD(n, 0));
927 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000928 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000929 expression = ast_for_expr(c, CHILD(n, 2));
930 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000931 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000932 orelse = ast_for_expr(c, CHILD(n, 4));
933 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000934 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000935 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000936 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000937}
938
Neal Norwitze4d4f002006-09-05 03:58:26 +0000939/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
940 so there is only a single version. Possibly for loops can also re-use
941 the code.
942*/
943
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944/* Count the number of 'for' loop in a list comprehension.
945
946 Helper for ast_for_listcomp().
947*/
948
949static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000950count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951{
952 int n_fors = 0;
953 node *ch = CHILD(n, 1);
954
955 count_list_for:
956 n_fors++;
957 REQ(ch, list_for);
958 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000959 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000961 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 count_list_iter:
963 REQ(ch, list_iter);
964 ch = CHILD(ch, 0);
965 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000966 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000968 if (NCH(ch) == 3) {
969 ch = CHILD(ch, 2);
970 goto count_list_iter;
971 }
972 else
973 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000975
976 /* Should never be reached */
977 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
978 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979}
980
981/* Count the number of 'if' statements in a list comprehension.
982
983 Helper for ast_for_listcomp().
984*/
985
986static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000987count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988{
989 int n_ifs = 0;
990
991 count_list_iter:
992 REQ(n, list_iter);
993 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000994 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 n = CHILD(n, 0);
996 REQ(n, list_if);
997 n_ifs++;
998 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000999 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 n = CHILD(n, 2);
1001 goto count_list_iter;
1002}
1003
1004static expr_ty
1005ast_for_listcomp(struct compiling *c, const node *n)
1006{
1007 /* listmaker: test ( list_for | (',' test)* [','] )
1008 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1009 list_iter: list_for | list_if
1010 list_if: 'if' test [list_iter]
1011 testlist_safe: test [(',' test)+ [',']]
1012 */
1013 expr_ty elt;
1014 asdl_seq *listcomps;
1015 int i, n_fors;
1016 node *ch;
1017
1018 REQ(n, listmaker);
1019 assert(NCH(n) > 1);
1020
1021 elt = ast_for_expr(c, CHILD(n, 0));
1022 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001023 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001025 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001027 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001029 listcomps = asdl_seq_new(n_fors, c->c_arena);
1030 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001031 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 ch = CHILD(n, 1);
1034 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001035 comprehension_ty lc;
1036 asdl_seq *t;
1037 expr_ty expression;
1038 node *for_ch;
1039
1040 REQ(ch, list_for);
1041
1042 for_ch = CHILD(ch, 1);
1043 t = ast_for_exprlist(c, for_ch, Store);
1044 if (!t)
1045 return NULL;
1046 expression = ast_for_testlist(c, CHILD(ch, 3));
1047 if (!expression)
1048 return NULL;
1049
1050 /* Check the # of children rather than the length of t, since
1051 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1052 */
1053 if (NCH(for_ch) == 1)
1054 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1055 c->c_arena);
1056 else
1057 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1058 c->c_arena),
1059 expression, NULL, c->c_arena);
1060 if (!lc)
1061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001063 if (NCH(ch) == 5) {
1064 int j, n_ifs;
1065 asdl_seq *ifs;
1066 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001068 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001069 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001070 if (n_ifs == -1)
1071 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001073 ifs = asdl_seq_new(n_ifs, c->c_arena);
1074 if (!ifs)
1075 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001077 for (j = 0; j < n_ifs; j++) {
1078 REQ(ch, list_iter);
1079 ch = CHILD(ch, 0);
1080 REQ(ch, list_if);
1081
1082 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1083 if (!list_for_expr)
1084 return NULL;
1085
1086 asdl_seq_SET(ifs, j, list_for_expr);
1087 if (NCH(ch) == 3)
1088 ch = CHILD(ch, 2);
1089 }
1090 /* on exit, must guarantee that ch is a list_for */
1091 if (TYPE(ch) == list_iter)
1092 ch = CHILD(ch, 0);
1093 lc->ifs = ifs;
1094 }
1095 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 }
1097
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001098 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099}
1100
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001101/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102
1103 Helper for ast_for_genexp().
1104*/
1105
1106static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001107count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001109 int n_fors = 0;
1110 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111
1112 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001113 n_fors++;
1114 REQ(ch, gen_for);
1115 if (NCH(ch) == 5)
1116 ch = CHILD(ch, 4);
1117 else
1118 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001120 REQ(ch, gen_iter);
1121 ch = CHILD(ch, 0);
1122 if (TYPE(ch) == gen_for)
1123 goto count_gen_for;
1124 else if (TYPE(ch) == gen_if) {
1125 if (NCH(ch) == 3) {
1126 ch = CHILD(ch, 2);
1127 goto count_gen_iter;
1128 }
1129 else
1130 return n_fors;
1131 }
1132
1133 /* Should never be reached */
1134 PyErr_SetString(PyExc_SystemError,
1135 "logic error in count_gen_fors");
1136 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137}
1138
1139/* Count the number of 'if' statements in a generator expression.
1140
1141 Helper for ast_for_genexp().
1142*/
1143
1144static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001145count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001147 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001149 while (1) {
1150 REQ(n, gen_iter);
1151 if (TYPE(CHILD(n, 0)) == gen_for)
1152 return n_ifs;
1153 n = CHILD(n, 0);
1154 REQ(n, gen_if);
1155 n_ifs++;
1156 if (NCH(n) == 2)
1157 return n_ifs;
1158 n = CHILD(n, 2);
1159 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160}
1161
Jeremy Hyltona8293132006-02-28 17:58:27 +00001162/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163static expr_ty
1164ast_for_genexp(struct compiling *c, const node *n)
1165{
1166 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001167 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 expr_ty elt;
1169 asdl_seq *genexps;
1170 int i, n_fors;
1171 node *ch;
1172
1173 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1174 assert(NCH(n) > 1);
1175
1176 elt = ast_for_expr(c, CHILD(n, 0));
1177 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001178 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001180 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001182 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001183
1184 genexps = asdl_seq_new(n_fors, c->c_arena);
1185 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001186 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 ch = CHILD(n, 1);
1189 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001190 comprehension_ty ge;
1191 asdl_seq *t;
1192 expr_ty expression;
1193 node *for_ch;
1194
1195 REQ(ch, gen_for);
1196
1197 for_ch = CHILD(ch, 1);
1198 t = ast_for_exprlist(c, for_ch, Store);
1199 if (!t)
1200 return NULL;
1201 expression = ast_for_expr(c, CHILD(ch, 3));
1202 if (!expression)
1203 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001205 /* Check the # of children rather than the length of t, since
1206 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1207 if (NCH(for_ch) == 1)
1208 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1209 NULL, c->c_arena);
1210 else
1211 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1212 c->c_arena),
1213 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001214
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001215 if (!ge)
1216 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001217
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001218 if (NCH(ch) == 5) {
1219 int j, n_ifs;
1220 asdl_seq *ifs;
1221
1222 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001223 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001224 if (n_ifs == -1)
1225 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001226
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001227 ifs = asdl_seq_new(n_ifs, c->c_arena);
1228 if (!ifs)
1229 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001230
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001231 for (j = 0; j < n_ifs; j++) {
1232 REQ(ch, gen_iter);
1233 ch = CHILD(ch, 0);
1234 REQ(ch, gen_if);
1235
1236 expression = ast_for_expr(c, CHILD(ch, 1));
1237 if (!expression)
1238 return NULL;
1239 asdl_seq_SET(ifs, j, expression);
1240 if (NCH(ch) == 3)
1241 ch = CHILD(ch, 2);
1242 }
1243 /* on exit, must guarantee that ch is a gen_for */
1244 if (TYPE(ch) == gen_iter)
1245 ch = CHILD(ch, 0);
1246 ge->ifs = ifs;
1247 }
1248 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 }
1250
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001251 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252}
1253
1254static expr_ty
1255ast_for_atom(struct compiling *c, const node *n)
1256{
1257 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1258 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1259 */
1260 node *ch = CHILD(n, 0);
1261
1262 switch (TYPE(ch)) {
1263 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001264 /* All names start in Load context, but may later be
1265 changed. */
1266 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1267 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001269 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001270 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001271#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001272 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1273 PyObject *type, *value, *tback, *errstr;
1274 PyErr_Fetch(&type, &value, &tback);
1275 errstr = ((PyUnicodeErrorObject *)value)->reason;
1276 if (errstr) {
1277 char *s = "";
1278 char buf[128];
1279 s = PyString_AsString(errstr);
1280 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1281 ast_error(n, buf);
1282 } else {
1283 ast_error(n, "(unicode error) unknown error");
1284 }
1285 Py_DECREF(type);
1286 Py_DECREF(value);
1287 Py_XDECREF(tback);
1288 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001289#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001290 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001291 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001292 PyArena_AddPyObject(c->c_arena, str);
1293 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 }
1295 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001296 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001297 if (!pynum)
1298 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001299
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001300 PyArena_AddPyObject(c->c_arena, pynum);
1301 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 }
1303 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001304 ch = CHILD(n, 1);
1305
1306 if (TYPE(ch) == RPAR)
1307 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1308
1309 if (TYPE(ch) == yield_expr)
1310 return ast_for_expr(c, ch);
1311
1312 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1313 return ast_for_genexp(c, ch);
1314
1315 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001317 ch = CHILD(n, 1);
1318
1319 if (TYPE(ch) == RSQB)
1320 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1321
1322 REQ(ch, listmaker);
1323 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1324 asdl_seq *elts = seq_for_testlist(c, ch);
1325 if (!elts)
1326 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001327
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001328 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1329 }
1330 else
1331 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001333 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1334 int i, size;
1335 asdl_seq *keys, *values;
1336
1337 ch = CHILD(n, 1);
1338 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1339 keys = asdl_seq_new(size, c->c_arena);
1340 if (!keys)
1341 return NULL;
1342
1343 values = asdl_seq_new(size, c->c_arena);
1344 if (!values)
1345 return NULL;
1346
1347 for (i = 0; i < NCH(ch); i += 4) {
1348 expr_ty expression;
1349
1350 expression = ast_for_expr(c, CHILD(ch, i));
1351 if (!expression)
1352 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001353
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001354 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001355
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001356 expression = ast_for_expr(c, CHILD(ch, i + 2));
1357 if (!expression)
1358 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001359
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001360 asdl_seq_SET(values, i / 4, expression);
1361 }
1362 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 }
1364 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001365 expr_ty expression;
Christian Heimes02c9ab52007-11-23 12:12:02 +00001366 if (Py_Py3kWarningFlag) {
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001367 if (PyErr_WarnExplicit(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00001368 "backquote not supported in 3.x; use repr()",
Christian Heimesffcd1e12007-11-24 01:36:02 +00001369 c->c_filename, LINENO(n),
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001370 NULL, NULL)) {
1371 return NULL;
1372 }
1373 }
1374 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001375 if (!expression)
1376 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001377
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001378 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 }
1380 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001381 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1382 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 }
1384}
1385
1386static slice_ty
1387ast_for_slice(struct compiling *c, const node *n)
1388{
1389 node *ch;
1390 expr_ty lower = NULL, upper = NULL, step = NULL;
1391
1392 REQ(n, subscript);
1393
1394 /*
1395 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1396 sliceop: ':' [test]
1397 */
1398 ch = CHILD(n, 0);
1399 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001400 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401
1402 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001403 /* 'step' variable hold no significance in terms of being used over
1404 other vars */
1405 step = ast_for_expr(c, ch);
1406 if (!step)
1407 return NULL;
1408
1409 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 }
1411
1412 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001413 lower = ast_for_expr(c, ch);
1414 if (!lower)
1415 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 }
1417
1418 /* If there's an upper bound it's in the second or third position. */
1419 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001420 if (NCH(n) > 1) {
1421 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001423 if (TYPE(n2) == test) {
1424 upper = ast_for_expr(c, n2);
1425 if (!upper)
1426 return NULL;
1427 }
1428 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001430 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001432 if (TYPE(n2) == test) {
1433 upper = ast_for_expr(c, n2);
1434 if (!upper)
1435 return NULL;
1436 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 }
1438
1439 ch = CHILD(n, NCH(n) - 1);
1440 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001441 if (NCH(ch) == 1) {
1442 /* No expression, so step is None */
1443 ch = CHILD(ch, 0);
1444 step = Name(new_identifier("None", c->c_arena), Load,
1445 LINENO(ch), ch->n_col_offset, c->c_arena);
1446 if (!step)
1447 return NULL;
1448 } else {
1449 ch = CHILD(ch, 1);
1450 if (TYPE(ch) == test) {
1451 step = ast_for_expr(c, ch);
1452 if (!step)
1453 return NULL;
1454 }
1455 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 }
1457
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001458 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459}
1460
1461static expr_ty
1462ast_for_binop(struct compiling *c, const node *n)
1463{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001464 /* Must account for a sequence of expressions.
1465 How should A op B op C by represented?
1466 BinOp(BinOp(A, op, B), op, C).
1467 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001469 int i, nops;
1470 expr_ty expr1, expr2, result;
1471 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001473 expr1 = ast_for_expr(c, CHILD(n, 0));
1474 if (!expr1)
1475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001477 expr2 = ast_for_expr(c, CHILD(n, 2));
1478 if (!expr2)
1479 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001481 newoperator = get_operator(CHILD(n, 1));
1482 if (!newoperator)
1483 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001485 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1486 c->c_arena);
1487 if (!result)
1488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001490 nops = (NCH(n) - 1) / 2;
1491 for (i = 1; i < nops; i++) {
1492 expr_ty tmp_result, tmp;
1493 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001495 newoperator = get_operator(next_oper);
1496 if (!newoperator)
1497 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001499 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1500 if (!tmp)
1501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001503 tmp_result = BinOp(result, newoperator, tmp,
1504 LINENO(next_oper), next_oper->n_col_offset,
1505 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001506 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001507 return NULL;
1508 result = tmp_result;
1509 }
1510 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511}
1512
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001513static expr_ty
1514ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1515{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001516 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1517 subscriptlist: subscript (',' subscript)* [',']
1518 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1519 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001520 REQ(n, trailer);
1521 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001522 if (NCH(n) == 2)
1523 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1524 n->n_col_offset, c->c_arena);
1525 else
1526 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001527 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001528 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001529 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1530 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001531 }
1532 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001533 REQ(CHILD(n, 0), LSQB);
1534 REQ(CHILD(n, 2), RSQB);
1535 n = CHILD(n, 1);
1536 if (NCH(n) == 1) {
1537 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1538 if (!slc)
1539 return NULL;
1540 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1541 c->c_arena);
1542 }
1543 else {
1544 /* The grammar is ambiguous here. The ambiguity is resolved
1545 by treating the sequence as a tuple literal if there are
1546 no slice features.
1547 */
1548 int j;
1549 slice_ty slc;
1550 expr_ty e;
1551 bool simple = true;
1552 asdl_seq *slices, *elts;
1553 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1554 if (!slices)
1555 return NULL;
1556 for (j = 0; j < NCH(n); j += 2) {
1557 slc = ast_for_slice(c, CHILD(n, j));
1558 if (!slc)
1559 return NULL;
1560 if (slc->kind != Index_kind)
1561 simple = false;
1562 asdl_seq_SET(slices, j / 2, slc);
1563 }
1564 if (!simple) {
1565 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1566 Load, LINENO(n), n->n_col_offset, c->c_arena);
1567 }
1568 /* extract Index values and put them in a Tuple */
1569 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1570 if (!elts)
1571 return NULL;
1572 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1573 slc = (slice_ty)asdl_seq_GET(slices, j);
1574 assert(slc->kind == Index_kind && slc->v.Index.value);
1575 asdl_seq_SET(elts, j, slc->v.Index.value);
1576 }
1577 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1578 if (!e)
1579 return NULL;
1580 return Subscript(left_expr, Index(e, c->c_arena),
1581 Load, LINENO(n), n->n_col_offset, c->c_arena);
1582 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001583 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001584}
1585
1586static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001587ast_for_factor(struct compiling *c, const node *n)
1588{
1589 node *pfactor, *ppower, *patom, *pnum;
1590 expr_ty expression;
1591
1592 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001593 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001594 constant. The peephole optimizer already does something like
1595 this but it doesn't handle the case where the constant is
1596 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1597 PyLongObject.
1598 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001599 if (TYPE(CHILD(n, 0)) == MINUS &&
1600 NCH(n) == 2 &&
1601 TYPE((pfactor = CHILD(n, 1))) == factor &&
1602 NCH(pfactor) == 1 &&
1603 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1604 NCH(ppower) == 1 &&
1605 TYPE((patom = CHILD(ppower, 0))) == atom &&
1606 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1607 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1608 if (s == NULL)
1609 return NULL;
1610 s[0] = '-';
1611 strcpy(s + 1, STR(pnum));
1612 PyObject_FREE(STR(pnum));
1613 STR(pnum) = s;
1614 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001615 }
1616
1617 expression = ast_for_expr(c, CHILD(n, 1));
1618 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001619 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001620
1621 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001622 case PLUS:
1623 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1624 c->c_arena);
1625 case MINUS:
1626 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1627 c->c_arena);
1628 case TILDE:
1629 return UnaryOp(Invert, expression, LINENO(n),
1630 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001631 }
1632 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001633 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001634 return NULL;
1635}
1636
1637static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001638ast_for_power(struct compiling *c, const node *n)
1639{
1640 /* power: atom trailer* ('**' factor)*
1641 */
1642 int i;
1643 expr_ty e, tmp;
1644 REQ(n, power);
1645 e = ast_for_atom(c, CHILD(n, 0));
1646 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001647 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001648 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001649 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001650 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001651 node *ch = CHILD(n, i);
1652 if (TYPE(ch) != trailer)
1653 break;
1654 tmp = ast_for_trailer(c, ch, e);
1655 if (!tmp)
1656 return NULL;
1657 tmp->lineno = e->lineno;
1658 tmp->col_offset = e->col_offset;
1659 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001660 }
1661 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001662 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1663 if (!f)
1664 return NULL;
1665 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1666 if (!tmp)
1667 return NULL;
1668 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001669 }
1670 return e;
1671}
1672
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673/* Do not name a variable 'expr'! Will cause a compile error.
1674*/
1675
1676static expr_ty
1677ast_for_expr(struct compiling *c, const node *n)
1678{
1679 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001680 test: or_test ['if' or_test 'else' test] | lambdef
1681 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 and_test: not_test ('and' not_test)*
1683 not_test: 'not' not_test | comparison
1684 comparison: expr (comp_op expr)*
1685 expr: xor_expr ('|' xor_expr)*
1686 xor_expr: and_expr ('^' and_expr)*
1687 and_expr: shift_expr ('&' shift_expr)*
1688 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1689 arith_expr: term (('+'|'-') term)*
1690 term: factor (('*'|'/'|'%'|'//') factor)*
1691 factor: ('+'|'-'|'~') factor | power
1692 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001693
1694 As well as modified versions that exist for backward compatibility,
1695 to explicitly allow:
1696 [ x for x in lambda: 0, lambda: 1 ]
1697 (which would be ambiguous without these extra rules)
1698
1699 old_test: or_test | old_lambdef
1700 old_lambdef: 'lambda' [vararglist] ':' old_test
1701
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 */
1703
1704 asdl_seq *seq;
1705 int i;
1706
1707 loop:
1708 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001709 case test:
1710 case old_test:
1711 if (TYPE(CHILD(n, 0)) == lambdef ||
1712 TYPE(CHILD(n, 0)) == old_lambdef)
1713 return ast_for_lambdef(c, CHILD(n, 0));
1714 else if (NCH(n) > 1)
1715 return ast_for_ifexpr(c, n);
1716 /* Fallthrough */
1717 case or_test:
1718 case and_test:
1719 if (NCH(n) == 1) {
1720 n = CHILD(n, 0);
1721 goto loop;
1722 }
1723 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1724 if (!seq)
1725 return NULL;
1726 for (i = 0; i < NCH(n); i += 2) {
1727 expr_ty e = ast_for_expr(c, CHILD(n, i));
1728 if (!e)
1729 return NULL;
1730 asdl_seq_SET(seq, i / 2, e);
1731 }
1732 if (!strcmp(STR(CHILD(n, 1)), "and"))
1733 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1734 c->c_arena);
1735 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1736 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1737 case not_test:
1738 if (NCH(n) == 1) {
1739 n = CHILD(n, 0);
1740 goto loop;
1741 }
1742 else {
1743 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1744 if (!expression)
1745 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001747 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1748 c->c_arena);
1749 }
1750 case comparison:
1751 if (NCH(n) == 1) {
1752 n = CHILD(n, 0);
1753 goto loop;
1754 }
1755 else {
1756 expr_ty expression;
1757 asdl_int_seq *ops;
1758 asdl_seq *cmps;
1759 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1760 if (!ops)
1761 return NULL;
1762 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1763 if (!cmps) {
1764 return NULL;
1765 }
1766 for (i = 1; i < NCH(n); i += 2) {
1767 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001769 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001770 if (!newoperator) {
1771 return NULL;
1772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001774 expression = ast_for_expr(c, CHILD(n, i + 1));
1775 if (!expression) {
1776 return NULL;
1777 }
1778
1779 asdl_seq_SET(ops, i / 2, newoperator);
1780 asdl_seq_SET(cmps, i / 2, expression);
1781 }
1782 expression = ast_for_expr(c, CHILD(n, 0));
1783 if (!expression) {
1784 return NULL;
1785 }
1786
1787 return Compare(expression, ops, cmps, LINENO(n),
1788 n->n_col_offset, c->c_arena);
1789 }
1790 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001792 /* The next five cases all handle BinOps. The main body of code
1793 is the same in each case, but the switch turned inside out to
1794 reuse the code for each type of operator.
1795 */
1796 case expr:
1797 case xor_expr:
1798 case and_expr:
1799 case shift_expr:
1800 case arith_expr:
1801 case term:
1802 if (NCH(n) == 1) {
1803 n = CHILD(n, 0);
1804 goto loop;
1805 }
1806 return ast_for_binop(c, n);
1807 case yield_expr: {
1808 expr_ty exp = NULL;
1809 if (NCH(n) == 2) {
1810 exp = ast_for_testlist(c, CHILD(n, 1));
1811 if (!exp)
1812 return NULL;
1813 }
1814 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1815 }
1816 case factor:
1817 if (NCH(n) == 1) {
1818 n = CHILD(n, 0);
1819 goto loop;
1820 }
1821 return ast_for_factor(c, n);
1822 case power:
1823 return ast_for_power(c, n);
1824 default:
1825 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001828 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 return NULL;
1830}
1831
1832static expr_ty
1833ast_for_call(struct compiling *c, const node *n, expr_ty func)
1834{
1835 /*
1836 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001837 | '**' test)
1838 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 */
1840
1841 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001842 asdl_seq *args;
1843 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 expr_ty vararg = NULL, kwarg = NULL;
1845
1846 REQ(n, arglist);
1847
1848 nargs = 0;
1849 nkeywords = 0;
1850 ngens = 0;
1851 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001852 node *ch = CHILD(n, i);
1853 if (TYPE(ch) == argument) {
1854 if (NCH(ch) == 1)
1855 nargs++;
1856 else if (TYPE(CHILD(ch, 1)) == gen_for)
1857 ngens++;
1858 else
1859 nkeywords++;
1860 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 }
1862 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001863 ast_error(n, "Generator expression must be parenthesized "
1864 "if not sole argument");
1865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 }
1867
1868 if (nargs + nkeywords + ngens > 255) {
1869 ast_error(n, "more than 255 arguments");
1870 return NULL;
1871 }
1872
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001873 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001875 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001876 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001878 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 nargs = 0;
1880 nkeywords = 0;
1881 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001882 node *ch = CHILD(n, i);
1883 if (TYPE(ch) == argument) {
1884 expr_ty e;
1885 if (NCH(ch) == 1) {
1886 if (nkeywords) {
1887 ast_error(CHILD(ch, 0),
1888 "non-keyword arg after keyword arg");
1889 return NULL;
1890 }
1891 e = ast_for_expr(c, CHILD(ch, 0));
1892 if (!e)
1893 return NULL;
1894 asdl_seq_SET(args, nargs++, e);
1895 }
1896 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1897 e = ast_for_genexp(c, ch);
1898 if (!e)
1899 return NULL;
1900 asdl_seq_SET(args, nargs++, e);
1901 }
1902 else {
1903 keyword_ty kw;
1904 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001906 /* CHILD(ch, 0) is test, but must be an identifier? */
1907 e = ast_for_expr(c, CHILD(ch, 0));
1908 if (!e)
1909 return NULL;
1910 /* f(lambda x: x[0] = 3) ends up getting parsed with
1911 * LHS test = lambda x: x[0], and RHS test = 3.
1912 * SF bug 132313 points out that complaining about a keyword
1913 * then is very confusing.
1914 */
1915 if (e->kind == Lambda_kind) {
1916 ast_error(CHILD(ch, 0),
1917 "lambda cannot contain assignment");
1918 return NULL;
1919 } else if (e->kind != Name_kind) {
1920 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1921 return NULL;
1922 }
1923 key = e->v.Name.id;
Georg Brandle06cf452007-06-07 13:23:24 +00001924 if (!strcmp(PyString_AS_STRING(key), "None")) {
1925 ast_error(CHILD(ch, 0), "assignment to None");
1926 return NULL;
1927 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001928 e = ast_for_expr(c, CHILD(ch, 2));
1929 if (!e)
1930 return NULL;
1931 kw = keyword(key, e, c->c_arena);
1932 if (!kw)
1933 return NULL;
1934 asdl_seq_SET(keywords, nkeywords++, kw);
1935 }
1936 }
1937 else if (TYPE(ch) == STAR) {
1938 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001939 if (!vararg)
1940 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001941 i++;
1942 }
1943 else if (TYPE(ch) == DOUBLESTAR) {
1944 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001945 if (!kwarg)
1946 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001947 i++;
1948 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 }
1950
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001951 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1952 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953}
1954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001956ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001958 /* testlist_gexp: test (',' test)* [','] */
1959 /* testlist: test (',' test)* [','] */
1960 /* testlist_safe: test (',' test)+ [','] */
1961 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001963 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001964 if (NCH(n) > 1)
1965 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001966 }
1967 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001968 assert(TYPE(n) == testlist ||
1969 TYPE(n) == testlist_safe ||
1970 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001971 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001973 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001975 asdl_seq *tmp = seq_for_testlist(c, n);
1976 if (!tmp)
1977 return NULL;
1978 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001980}
1981
1982static expr_ty
1983ast_for_testlist_gexp(struct compiling *c, const node* n)
1984{
1985 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1986 /* argument: test [ gen_for ] */
1987 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001988 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001989 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001990 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001991}
1992
1993/* like ast_for_testlist() but returns a sequence */
1994static asdl_seq*
1995ast_for_class_bases(struct compiling *c, const node* n)
1996{
1997 /* testlist: test (',' test)* [','] */
1998 assert(NCH(n) > 0);
1999 REQ(n, testlist);
2000 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002001 expr_ty base;
2002 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2003 if (!bases)
2004 return NULL;
2005 base = ast_for_expr(c, CHILD(n, 0));
2006 if (!base)
2007 return NULL;
2008 asdl_seq_SET(bases, 0, base);
2009 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002010 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002011
2012 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013}
2014
2015static stmt_ty
2016ast_for_expr_stmt(struct compiling *c, const node *n)
2017{
2018 REQ(n, expr_stmt);
2019 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002020 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 testlist: test (',' test)* [',']
2022 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002023 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 test: ... here starts the operator precendence dance
2025 */
2026
2027 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002028 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2029 if (!e)
2030 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002032 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 }
2034 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002035 expr_ty expr1, expr2;
2036 operator_ty newoperator;
2037 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002039 expr1 = ast_for_testlist(c, ch);
2040 if (!expr1)
2041 return NULL;
2042 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2043 switch (expr1->kind) {
2044 case GeneratorExp_kind:
2045 ast_error(ch, "augmented assignment to generator "
2046 "expression not possible");
2047 return NULL;
2048 case Yield_kind:
2049 ast_error(ch, "augmented assignment to yield "
2050 "expression not possible");
2051 return NULL;
2052 case Name_kind: {
2053 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2054 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2055 ast_error(ch, "assignment to None");
2056 return NULL;
2057 }
2058 break;
2059 }
2060 case Attribute_kind:
2061 case Subscript_kind:
2062 break;
2063 default:
2064 ast_error(ch, "illegal expression for augmented "
2065 "assignment");
2066 return NULL;
2067 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002068 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002069 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002071 ch = CHILD(n, 2);
2072 if (TYPE(ch) == testlist)
2073 expr2 = ast_for_testlist(c, ch);
2074 else
2075 expr2 = ast_for_expr(c, ch);
2076 if (!expr2)
2077 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002079 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002080 if (!newoperator)
2081 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002083 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2084 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 }
2086 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002087 int i;
2088 asdl_seq *targets;
2089 node *value;
2090 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002092 /* a normal assignment */
2093 REQ(CHILD(n, 1), EQUAL);
2094 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2095 if (!targets)
2096 return NULL;
2097 for (i = 0; i < NCH(n) - 2; i += 2) {
2098 expr_ty e;
2099 node *ch = CHILD(n, i);
2100 if (TYPE(ch) == yield_expr) {
2101 ast_error(ch, "assignment to yield expression not possible");
2102 return NULL;
2103 }
2104 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002106 /* set context to assign */
2107 if (!e)
2108 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002110 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002111 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002113 asdl_seq_SET(targets, i / 2, e);
2114 }
2115 value = CHILD(n, NCH(n) - 1);
2116 if (TYPE(value) == testlist)
2117 expression = ast_for_testlist(c, value);
2118 else
2119 expression = ast_for_expr(c, value);
2120 if (!expression)
2121 return NULL;
2122 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2123 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002124 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125}
2126
2127static stmt_ty
2128ast_for_print_stmt(struct compiling *c, const node *n)
2129{
2130 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002131 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 */
2133 expr_ty dest = NULL, expression;
2134 asdl_seq *seq;
2135 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002136 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137
2138 REQ(n, print_stmt);
2139 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002140 dest = ast_for_expr(c, CHILD(n, 2));
2141 if (!dest)
2142 return NULL;
2143 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002145 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002147 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002148 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002149 expression = ast_for_expr(c, CHILD(n, i));
2150 if (!expression)
2151 return NULL;
2152 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 }
2154 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002155 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156}
2157
2158static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002159ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160{
2161 asdl_seq *seq;
2162 int i;
2163 expr_ty e;
2164
2165 REQ(n, exprlist);
2166
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002167 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002169 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002171 e = ast_for_expr(c, CHILD(n, i));
2172 if (!e)
2173 return NULL;
2174 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002175 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002176 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 }
2178 return seq;
2179}
2180
2181static stmt_ty
2182ast_for_del_stmt(struct compiling *c, const node *n)
2183{
2184 asdl_seq *expr_list;
2185
2186 /* del_stmt: 'del' exprlist */
2187 REQ(n, del_stmt);
2188
2189 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2190 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002191 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002192 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193}
2194
2195static stmt_ty
2196ast_for_flow_stmt(struct compiling *c, const node *n)
2197{
2198 /*
2199 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002200 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 break_stmt: 'break'
2202 continue_stmt: 'continue'
2203 return_stmt: 'return' [testlist]
2204 yield_stmt: yield_expr
2205 yield_expr: 'yield' testlist
2206 raise_stmt: 'raise' [test [',' test [',' test]]]
2207 */
2208 node *ch;
2209
2210 REQ(n, flow_stmt);
2211 ch = CHILD(n, 0);
2212 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002213 case break_stmt:
2214 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2215 case continue_stmt:
2216 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2217 case yield_stmt: { /* will reduce to yield_expr */
2218 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2219 if (!exp)
2220 return NULL;
2221 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2222 }
2223 case return_stmt:
2224 if (NCH(ch) == 1)
2225 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2226 else {
2227 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2228 if (!expression)
2229 return NULL;
2230 return Return(expression, LINENO(n), n->n_col_offset,
2231 c->c_arena);
2232 }
2233 case raise_stmt:
2234 if (NCH(ch) == 1)
2235 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2236 c->c_arena);
2237 else if (NCH(ch) == 2) {
2238 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2239 if (!expression)
2240 return NULL;
2241 return Raise(expression, NULL, NULL, LINENO(n),
2242 n->n_col_offset, c->c_arena);
2243 }
2244 else if (NCH(ch) == 4) {
2245 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002247 expr1 = ast_for_expr(c, CHILD(ch, 1));
2248 if (!expr1)
2249 return NULL;
2250 expr2 = ast_for_expr(c, CHILD(ch, 3));
2251 if (!expr2)
2252 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002254 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2255 c->c_arena);
2256 }
2257 else if (NCH(ch) == 6) {
2258 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002260 expr1 = ast_for_expr(c, CHILD(ch, 1));
2261 if (!expr1)
2262 return NULL;
2263 expr2 = ast_for_expr(c, CHILD(ch, 3));
2264 if (!expr2)
2265 return NULL;
2266 expr3 = ast_for_expr(c, CHILD(ch, 5));
2267 if (!expr3)
2268 return NULL;
2269
2270 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2271 c->c_arena);
2272 }
2273 default:
2274 PyErr_Format(PyExc_SystemError,
2275 "unexpected flow_stmt: %d", TYPE(ch));
2276 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002278
2279 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281}
2282
2283static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002284alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285{
2286 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002287 import_as_name: NAME ['as' NAME]
2288 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 dotted_name: NAME ('.' NAME)*
2290 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002291 PyObject *str;
2292
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 loop:
2294 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002295 case import_as_name:
2296 str = NULL;
2297 if (NCH(n) == 3) {
2298 str = NEW_IDENTIFIER(CHILD(n, 2));
2299 }
2300 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2301 case dotted_as_name:
2302 if (NCH(n) == 1) {
2303 n = CHILD(n, 0);
2304 goto loop;
2305 }
2306 else {
2307 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2308 if (!a)
2309 return NULL;
2310 assert(!a->asname);
2311 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2312 return a;
2313 }
2314 break;
2315 case dotted_name:
2316 if (NCH(n) == 1)
2317 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2318 else {
2319 /* Create a string of the form "a.b.c" */
2320 int i;
2321 size_t len;
2322 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002324 len = 0;
2325 for (i = 0; i < NCH(n); i += 2)
2326 /* length of string plus one for the dot */
2327 len += strlen(STR(CHILD(n, i))) + 1;
2328 len--; /* the last name doesn't have a dot */
2329 str = PyString_FromStringAndSize(NULL, len);
2330 if (!str)
2331 return NULL;
2332 s = PyString_AS_STRING(str);
2333 if (!s)
2334 return NULL;
2335 for (i = 0; i < NCH(n); i += 2) {
2336 char *sch = STR(CHILD(n, i));
2337 strcpy(s, STR(CHILD(n, i)));
2338 s += strlen(sch);
2339 *s++ = '.';
2340 }
2341 --s;
2342 *s = '\0';
2343 PyString_InternInPlace(&str);
2344 PyArena_AddPyObject(c->c_arena, str);
2345 return alias(str, NULL, c->c_arena);
2346 }
2347 break;
2348 case STAR:
2349 str = PyString_InternFromString("*");
2350 PyArena_AddPyObject(c->c_arena, str);
2351 return alias(str, NULL, c->c_arena);
2352 default:
2353 PyErr_Format(PyExc_SystemError,
2354 "unexpected import name: %d", TYPE(n));
2355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002357
2358 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 return NULL;
2360}
2361
2362static stmt_ty
2363ast_for_import_stmt(struct compiling *c, const node *n)
2364{
2365 /*
2366 import_stmt: import_name | import_from
2367 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002368 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002369 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002371 int lineno;
2372 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 int i;
2374 asdl_seq *aliases;
2375
2376 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002377 lineno = LINENO(n);
2378 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002380 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002381 n = CHILD(n, 1);
2382 REQ(n, dotted_as_names);
2383 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2384 if (!aliases)
2385 return NULL;
2386 for (i = 0; i < NCH(n); i += 2) {
2387 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2388 if (!import_alias)
2389 return NULL;
2390 asdl_seq_SET(aliases, i / 2, import_alias);
2391 }
2392 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002394 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002395 int n_children;
2396 int idx, ndots = 0;
2397 alias_ty mod = NULL;
2398 identifier modname;
2399
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002400 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002401 optional module name */
2402 for (idx = 1; idx < NCH(n); idx++) {
2403 if (TYPE(CHILD(n, idx)) == dotted_name) {
2404 mod = alias_for_import_name(c, CHILD(n, idx));
2405 idx++;
2406 break;
2407 } else if (TYPE(CHILD(n, idx)) != DOT) {
2408 break;
2409 }
2410 ndots++;
2411 }
2412 idx++; /* skip over the 'import' keyword */
2413 switch (TYPE(CHILD(n, idx))) {
2414 case STAR:
2415 /* from ... import * */
2416 n = CHILD(n, idx);
2417 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002418 break;
2419 case LPAR:
2420 /* from ... import (x, y, z) */
2421 n = CHILD(n, idx + 1);
2422 n_children = NCH(n);
2423 break;
2424 case import_as_names:
2425 /* from ... import x, y, z */
2426 n = CHILD(n, idx);
2427 n_children = NCH(n);
2428 if (n_children % 2 == 0) {
2429 ast_error(n, "trailing comma not allowed without"
2430 " surrounding parentheses");
2431 return NULL;
2432 }
2433 break;
2434 default:
2435 ast_error(n, "Unexpected node-type in from-import");
2436 return NULL;
2437 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002439 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2440 if (!aliases)
2441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002443 /* handle "from ... import *" special b/c there's no children */
2444 if (TYPE(n) == STAR) {
2445 alias_ty import_alias = alias_for_import_name(c, n);
2446 if (!import_alias)
2447 return NULL;
2448 asdl_seq_SET(aliases, 0, import_alias);
2449 }
2450 else {
2451 for (i = 0; i < NCH(n); i += 2) {
2452 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2453 if (!import_alias)
2454 return NULL;
2455 asdl_seq_SET(aliases, i / 2, import_alias);
2456 }
2457 }
2458 if (mod != NULL)
2459 modname = mod->name;
2460 else
2461 modname = new_identifier("", c->c_arena);
2462 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2463 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 }
Neal Norwitz79792652005-11-14 04:25:03 +00002465 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002466 "unknown import statement: starts with command '%s'",
2467 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 return NULL;
2469}
2470
2471static stmt_ty
2472ast_for_global_stmt(struct compiling *c, const node *n)
2473{
2474 /* global_stmt: 'global' NAME (',' NAME)* */
2475 identifier name;
2476 asdl_seq *s;
2477 int i;
2478
2479 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002480 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002482 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002484 name = NEW_IDENTIFIER(CHILD(n, i));
2485 if (!name)
2486 return NULL;
2487 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002489 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490}
2491
2492static stmt_ty
2493ast_for_exec_stmt(struct compiling *c, const node *n)
2494{
2495 expr_ty expr1, globals = NULL, locals = NULL;
2496 int n_children = NCH(n);
2497 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002498 PyErr_Format(PyExc_SystemError,
2499 "poorly formed 'exec' statement: %d parts to statement",
2500 n_children);
2501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 }
2503
2504 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2505 REQ(n, exec_stmt);
2506 expr1 = ast_for_expr(c, CHILD(n, 1));
2507 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002510 globals = ast_for_expr(c, CHILD(n, 3));
2511 if (!globals)
2512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 }
2514 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002515 locals = ast_for_expr(c, CHILD(n, 5));
2516 if (!locals)
2517 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 }
2519
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002520 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2521 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522}
2523
2524static stmt_ty
2525ast_for_assert_stmt(struct compiling *c, const node *n)
2526{
2527 /* assert_stmt: 'assert' test [',' test] */
2528 REQ(n, assert_stmt);
2529 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002530 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2531 if (!expression)
2532 return NULL;
2533 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2534 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 }
2536 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002537 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002539 expr1 = ast_for_expr(c, CHILD(n, 1));
2540 if (!expr1)
2541 return NULL;
2542 expr2 = ast_for_expr(c, CHILD(n, 3));
2543 if (!expr2)
2544 return NULL;
2545
2546 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 }
Neal Norwitz79792652005-11-14 04:25:03 +00002548 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002549 "improper number of parts to 'assert' statement: %d",
2550 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 return NULL;
2552}
2553
2554static asdl_seq *
2555ast_for_suite(struct compiling *c, const node *n)
2556{
2557 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002558 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 stmt_ty s;
2560 int i, total, num, end, pos = 0;
2561 node *ch;
2562
2563 REQ(n, suite);
2564
2565 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002566 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002568 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002570 n = CHILD(n, 0);
2571 /* simple_stmt always ends with a NEWLINE,
2572 and may have a trailing SEMI
2573 */
2574 end = NCH(n) - 1;
2575 if (TYPE(CHILD(n, end - 1)) == SEMI)
2576 end--;
2577 /* loop by 2 to skip semi-colons */
2578 for (i = 0; i < end; i += 2) {
2579 ch = CHILD(n, i);
2580 s = ast_for_stmt(c, ch);
2581 if (!s)
2582 return NULL;
2583 asdl_seq_SET(seq, pos++, s);
2584 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 }
2586 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002587 for (i = 2; i < (NCH(n) - 1); i++) {
2588 ch = CHILD(n, i);
2589 REQ(ch, stmt);
2590 num = num_stmts(ch);
2591 if (num == 1) {
2592 /* small_stmt or compound_stmt with only one child */
2593 s = ast_for_stmt(c, ch);
2594 if (!s)
2595 return NULL;
2596 asdl_seq_SET(seq, pos++, s);
2597 }
2598 else {
2599 int j;
2600 ch = CHILD(ch, 0);
2601 REQ(ch, simple_stmt);
2602 for (j = 0; j < NCH(ch); j += 2) {
2603 /* statement terminates with a semi-colon ';' */
2604 if (NCH(CHILD(ch, j)) == 0) {
2605 assert((j + 1) == NCH(ch));
2606 break;
2607 }
2608 s = ast_for_stmt(c, CHILD(ch, j));
2609 if (!s)
2610 return NULL;
2611 asdl_seq_SET(seq, pos++, s);
2612 }
2613 }
2614 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 }
2616 assert(pos == seq->size);
2617 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618}
2619
2620static stmt_ty
2621ast_for_if_stmt(struct compiling *c, const node *n)
2622{
2623 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2624 ['else' ':' suite]
2625 */
2626 char *s;
2627
2628 REQ(n, if_stmt);
2629
2630 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002631 expr_ty expression;
2632 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002634 expression = ast_for_expr(c, CHILD(n, 1));
2635 if (!expression)
2636 return NULL;
2637 suite_seq = ast_for_suite(c, CHILD(n, 3));
2638 if (!suite_seq)
2639 return NULL;
2640
2641 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2642 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002644
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 s = STR(CHILD(n, 4));
2646 /* s[2], the third character in the string, will be
2647 's' for el_s_e, or
2648 'i' for el_i_f
2649 */
2650 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002651 expr_ty expression;
2652 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002654 expression = ast_for_expr(c, CHILD(n, 1));
2655 if (!expression)
2656 return NULL;
2657 seq1 = ast_for_suite(c, CHILD(n, 3));
2658 if (!seq1)
2659 return NULL;
2660 seq2 = ast_for_suite(c, CHILD(n, 6));
2661 if (!seq2)
2662 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002664 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2665 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 }
2667 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002668 int i, n_elif, has_else = 0;
2669 expr_ty expression;
2670 asdl_seq *suite_seq;
2671 asdl_seq *orelse = NULL;
2672 n_elif = NCH(n) - 4;
2673 /* must reference the child n_elif+1 since 'else' token is third,
2674 not fourth, child from the end. */
2675 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2676 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2677 has_else = 1;
2678 n_elif -= 3;
2679 }
2680 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002682 if (has_else) {
2683 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002685 orelse = asdl_seq_new(1, c->c_arena);
2686 if (!orelse)
2687 return NULL;
2688 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2689 if (!expression)
2690 return NULL;
2691 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2692 if (!suite_seq)
2693 return NULL;
2694 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2695 if (!suite_seq2)
2696 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002698 asdl_seq_SET(orelse, 0,
2699 If(expression, suite_seq, suite_seq2,
2700 LINENO(CHILD(n, NCH(n) - 6)),
2701 CHILD(n, NCH(n) - 6)->n_col_offset,
2702 c->c_arena));
2703 /* the just-created orelse handled the last elif */
2704 n_elif--;
2705 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002707 for (i = 0; i < n_elif; i++) {
2708 int off = 5 + (n_elif - i - 1) * 4;
2709 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2710 if (!newobj)
2711 return NULL;
2712 expression = ast_for_expr(c, CHILD(n, off));
2713 if (!expression)
2714 return NULL;
2715 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2716 if (!suite_seq)
2717 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002719 asdl_seq_SET(newobj, 0,
2720 If(expression, suite_seq, orelse,
2721 LINENO(CHILD(n, off)),
2722 CHILD(n, off)->n_col_offset, c->c_arena));
2723 orelse = newobj;
2724 }
2725 expression = ast_for_expr(c, CHILD(n, 1));
2726 if (!expression)
2727 return NULL;
2728 suite_seq = ast_for_suite(c, CHILD(n, 3));
2729 if (!suite_seq)
2730 return NULL;
2731 return If(expression, suite_seq, orelse,
2732 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002734
2735 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002736 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738}
2739
2740static stmt_ty
2741ast_for_while_stmt(struct compiling *c, const node *n)
2742{
2743 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2744 REQ(n, while_stmt);
2745
2746 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002747 expr_ty expression;
2748 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002750 expression = ast_for_expr(c, CHILD(n, 1));
2751 if (!expression)
2752 return NULL;
2753 suite_seq = ast_for_suite(c, CHILD(n, 3));
2754 if (!suite_seq)
2755 return NULL;
2756 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2757 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 }
2759 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002760 expr_ty expression;
2761 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002763 expression = ast_for_expr(c, CHILD(n, 1));
2764 if (!expression)
2765 return NULL;
2766 seq1 = ast_for_suite(c, CHILD(n, 3));
2767 if (!seq1)
2768 return NULL;
2769 seq2 = ast_for_suite(c, CHILD(n, 6));
2770 if (!seq2)
2771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002773 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2774 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002776
2777 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002778 "wrong number of tokens for 'while' statement: %d",
2779 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781}
2782
2783static stmt_ty
2784ast_for_for_stmt(struct compiling *c, const node *n)
2785{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002786 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787 expr_ty expression;
2788 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002789 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2791 REQ(n, for_stmt);
2792
2793 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002794 seq = ast_for_suite(c, CHILD(n, 8));
2795 if (!seq)
2796 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 }
2798
Neal Norwitzedef2be2006-07-12 05:26:17 +00002799 node_target = CHILD(n, 1);
2800 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002801 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002802 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002803 /* Check the # of children rather than the length of _target, since
2804 for x, in ... has 1 element in _target, but still requires a Tuple. */
2805 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002806 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002808 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002810 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002811 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002812 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002814 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002817 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002818 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819}
2820
2821static excepthandler_ty
2822ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2823{
Collin Winter62903052007-05-18 23:11:24 +00002824 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 REQ(exc, except_clause);
2826 REQ(body, suite);
2827
2828 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002829 asdl_seq *suite_seq = ast_for_suite(c, body);
2830 if (!suite_seq)
2831 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832
Georg Brandla48f3ab2008-03-30 06:40:17 +00002833 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002834 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 }
2836 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002837 expr_ty expression;
2838 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002840 expression = ast_for_expr(c, CHILD(exc, 1));
2841 if (!expression)
2842 return NULL;
2843 suite_seq = ast_for_suite(c, body);
2844 if (!suite_seq)
2845 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846
Georg Brandla48f3ab2008-03-30 06:40:17 +00002847 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002848 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 }
2850 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002851 asdl_seq *suite_seq;
2852 expr_ty expression;
2853 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2854 if (!e)
2855 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002856 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002857 return NULL;
2858 expression = ast_for_expr(c, CHILD(exc, 1));
2859 if (!expression)
2860 return NULL;
2861 suite_seq = ast_for_suite(c, body);
2862 if (!suite_seq)
2863 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864
Georg Brandla48f3ab2008-03-30 06:40:17 +00002865 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002866 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002868
2869 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002870 "wrong number of children for 'except' clause: %d",
2871 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002872 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873}
2874
2875static stmt_ty
2876ast_for_try_stmt(struct compiling *c, const node *n)
2877{
Neal Norwitzf599f422005-12-17 21:33:47 +00002878 const int nch = NCH(n);
2879 int n_except = (nch - 3)/3;
2880 asdl_seq *body, *orelse = NULL, *finally = NULL;
2881
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 REQ(n, try_stmt);
2883
Neal Norwitzf599f422005-12-17 21:33:47 +00002884 body = ast_for_suite(c, CHILD(n, 2));
2885 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002886 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887
Neal Norwitzf599f422005-12-17 21:33:47 +00002888 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002889 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2890 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2891 /* we can assume it's an "else",
2892 because nch >= 9 for try-else-finally and
2893 it would otherwise have a type of except_clause */
2894 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2895 if (orelse == NULL)
2896 return NULL;
2897 n_except--;
2898 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002900 finally = ast_for_suite(c, CHILD(n, nch - 1));
2901 if (finally == NULL)
2902 return NULL;
2903 n_except--;
2904 }
2905 else {
2906 /* we can assume it's an "else",
2907 otherwise it would have a type of except_clause */
2908 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2909 if (orelse == NULL)
2910 return NULL;
2911 n_except--;
2912 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002914 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002915 ast_error(n, "malformed 'try' statement");
2916 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002918
2919 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002920 int i;
2921 stmt_ty except_st;
2922 /* process except statements to create a try ... except */
2923 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2924 if (handlers == NULL)
2925 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002926
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002927 for (i = 0; i < n_except; i++) {
2928 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2929 CHILD(n, 5 + i * 3));
2930 if (!e)
2931 return NULL;
2932 asdl_seq_SET(handlers, i, e);
2933 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002934
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002935 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2936 n->n_col_offset, c->c_arena);
2937 if (!finally)
2938 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002939
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002940 /* if a 'finally' is present too, we nest the TryExcept within a
2941 TryFinally to emulate try ... except ... finally */
2942 body = asdl_seq_new(1, c->c_arena);
2943 if (body == NULL)
2944 return NULL;
2945 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002946 }
2947
2948 /* must be a try ... finally (except clauses are in body, if any exist) */
2949 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002950 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951}
2952
Guido van Rossumc2e20742006-02-27 22:32:47 +00002953static expr_ty
2954ast_for_with_var(struct compiling *c, const node *n)
2955{
2956 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002957 return ast_for_expr(c, CHILD(n, 1));
2958}
2959
2960/* with_stmt: 'with' test [ with_var ] ':' suite */
2961static stmt_ty
2962ast_for_with_stmt(struct compiling *c, const node *n)
2963{
2964 expr_ty context_expr, optional_vars = NULL;
2965 int suite_index = 3; /* skip 'with', test, and ':' */
2966 asdl_seq *suite_seq;
2967
2968 assert(TYPE(n) == with_stmt);
2969 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002970 if (!context_expr)
2971 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002972 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002973 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002974
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002975 if (!optional_vars) {
2976 return NULL;
2977 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002978 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002979 return NULL;
2980 }
2981 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002982 }
2983
2984 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2985 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002986 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002987 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002988 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002989 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002990}
2991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00002993ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994{
2995 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 asdl_seq *bases, *s;
2997
2998 REQ(n, classdef);
2999
3000 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003001 ast_error(n, "assignment to None");
3002 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003 }
3004
3005 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003006 s = ast_for_suite(c, CHILD(n, 3));
3007 if (!s)
3008 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003009 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3010 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011 }
3012 /* check for empty base list */
3013 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003014 s = ast_for_suite(c, CHILD(n,5));
3015 if (!s)
3016 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003017 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3018 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019 }
3020
3021 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003022 bases = ast_for_class_bases(c, CHILD(n, 3));
3023 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003024 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025
3026 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003027 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003028 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003029 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3030 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031}
3032
3033static stmt_ty
3034ast_for_stmt(struct compiling *c, const node *n)
3035{
3036 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003037 assert(NCH(n) == 1);
3038 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 }
3040 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003041 assert(num_stmts(n) == 1);
3042 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043 }
3044 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003045 REQ(n, small_stmt);
3046 n = CHILD(n, 0);
3047 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3048 | flow_stmt | import_stmt | global_stmt | exec_stmt
3049 | assert_stmt
3050 */
3051 switch (TYPE(n)) {
3052 case expr_stmt:
3053 return ast_for_expr_stmt(c, n);
3054 case print_stmt:
3055 return ast_for_print_stmt(c, n);
3056 case del_stmt:
3057 return ast_for_del_stmt(c, n);
3058 case pass_stmt:
3059 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3060 case flow_stmt:
3061 return ast_for_flow_stmt(c, n);
3062 case import_stmt:
3063 return ast_for_import_stmt(c, n);
3064 case global_stmt:
3065 return ast_for_global_stmt(c, n);
3066 case exec_stmt:
3067 return ast_for_exec_stmt(c, n);
3068 case assert_stmt:
3069 return ast_for_assert_stmt(c, n);
3070 default:
3071 PyErr_Format(PyExc_SystemError,
3072 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3073 TYPE(n), NCH(n));
3074 return NULL;
3075 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 }
3077 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003078 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003079 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003080 */
3081 node *ch = CHILD(n, 0);
3082 REQ(n, compound_stmt);
3083 switch (TYPE(ch)) {
3084 case if_stmt:
3085 return ast_for_if_stmt(c, ch);
3086 case while_stmt:
3087 return ast_for_while_stmt(c, ch);
3088 case for_stmt:
3089 return ast_for_for_stmt(c, ch);
3090 case try_stmt:
3091 return ast_for_try_stmt(c, ch);
3092 case with_stmt:
3093 return ast_for_with_stmt(c, ch);
3094 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003095 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003096 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003097 return ast_for_classdef(c, ch, NULL);
3098 case decorated:
3099 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003100 default:
3101 PyErr_Format(PyExc_SystemError,
3102 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3103 TYPE(n), NCH(n));
3104 return NULL;
3105 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 }
3107}
3108
3109static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003110parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003112 const char *end;
3113 long x;
3114 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003116 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003117 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118#endif
3119
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003120 errno = 0;
3121 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003123 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003125 if (*end == 'l' || *end == 'L')
3126 return PyLong_FromString((char *)s, (char **)0, 0);
3127 if (s[0] == '0') {
3128 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3129 if (x < 0 && errno == 0) {
3130 return PyLong_FromString((char *)s,
3131 (char **)0,
3132 0);
3133 }
3134 }
3135 else
3136 x = PyOS_strtol((char *)s, (char **)&end, 0);
3137 if (*end == '\0') {
3138 if (errno != 0)
3139 return PyLong_FromString((char *)s, (char **)0, 0);
3140 return PyInt_FromLong(x);
3141 }
3142 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003144 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003145 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003146 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003147 complex.imag = PyOS_ascii_atof(s);
3148 PyFPE_END_PROTECT(complex)
3149 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003150 }
3151 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003153 {
3154 PyFPE_START_PROTECT("atof", return 0)
3155 dx = PyOS_ascii_atof(s);
3156 PyFPE_END_PROTECT(dx)
3157 return PyFloat_FromDouble(dx);
3158 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159}
3160
3161static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003162decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163{
3164#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003165 Py_FatalError("decode_utf8 should not be called in this build.");
3166 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003168 PyObject *u, *v;
3169 char *s, *t;
3170 t = s = (char *)*sPtr;
3171 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3172 while (s < end && (*s & 0x80)) s++;
3173 *sPtr = s;
3174 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3175 if (u == NULL)
3176 return NULL;
3177 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3178 Py_DECREF(u);
3179 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180#endif
3181}
3182
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003183#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003185decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003187 PyObject *v, *u;
3188 char *buf;
3189 char *p;
3190 const char *end;
3191 if (encoding == NULL) {
3192 buf = (char *)s;
3193 u = NULL;
3194 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3195 buf = (char *)s;
3196 u = NULL;
3197 } else {
3198 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3199 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3200 if (u == NULL)
3201 return NULL;
3202 p = buf = PyString_AsString(u);
3203 end = s + len;
3204 while (s < end) {
3205 if (*s == '\\') {
3206 *p++ = *s++;
3207 if (*s & 0x80) {
3208 strcpy(p, "u005c");
3209 p += 5;
3210 }
3211 }
3212 if (*s & 0x80) { /* XXX inefficient */
3213 PyObject *w;
3214 char *r;
3215 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003216 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003217 if (w == NULL) {
3218 Py_DECREF(u);
3219 return NULL;
3220 }
3221 r = PyString_AsString(w);
3222 rn = PyString_Size(w);
3223 assert(rn % 2 == 0);
3224 for (i = 0; i < rn; i += 2) {
3225 sprintf(p, "\\u%02x%02x",
3226 r[i + 0] & 0xFF,
3227 r[i + 1] & 0xFF);
3228 p += 6;
3229 }
3230 Py_DECREF(w);
3231 } else {
3232 *p++ = *s++;
3233 }
3234 }
3235 len = p - buf;
3236 s = buf;
3237 }
3238 if (rawmode)
3239 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3240 else
3241 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3242 Py_XDECREF(u);
3243 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003245#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246
3247/* s is a Python string literal, including the bracketing quote characters,
3248 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3249 * parsestr parses it, and returns the decoded Python string object.
3250 */
3251static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003252parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003254 size_t len;
3255 int quote = Py_CHARMASK(*s);
3256 int rawmode = 0;
3257 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003258 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003260 if (isalpha(quote) || quote == '_') {
3261 if (quote == 'u' || quote == 'U') {
3262 quote = *++s;
3263 unicode = 1;
3264 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003265 if (quote == 'b' || quote == 'B') {
3266 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003267 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003268 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003269 if (quote == 'r' || quote == 'R') {
3270 quote = *++s;
3271 rawmode = 1;
3272 }
3273 }
3274 if (quote != '\'' && quote != '\"') {
3275 PyErr_BadInternalCall();
3276 return NULL;
3277 }
3278 s++;
3279 len = strlen(s);
3280 if (len > INT_MAX) {
3281 PyErr_SetString(PyExc_OverflowError,
3282 "string to parse is too long");
3283 return NULL;
3284 }
3285 if (s[--len] != quote) {
3286 PyErr_BadInternalCall();
3287 return NULL;
3288 }
3289 if (len >= 4 && s[0] == quote && s[1] == quote) {
3290 s += 2;
3291 len -= 2;
3292 if (s[--len] != quote || s[--len] != quote) {
3293 PyErr_BadInternalCall();
3294 return NULL;
3295 }
3296 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003298 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003299 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003302 need_encoding = (c->c_encoding != NULL &&
3303 strcmp(c->c_encoding, "utf-8") != 0 &&
3304 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003305 if (rawmode || strchr(s, '\\') == NULL) {
3306 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003308 /* This should not happen - we never see any other
3309 encoding. */
3310 Py_FatalError(
3311 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003313 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3314 if (u == NULL)
3315 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003316 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003317 Py_DECREF(u);
3318 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003320 } else {
3321 return PyString_FromStringAndSize(s, len);
3322 }
3323 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003325 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003326 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327}
3328
3329/* Build a Python string object out of a STRING atom. This takes care of
3330 * compile-time literal catenation, calling parsestr() on each piece, and
3331 * pasting the intermediate results together.
3332 */
3333static PyObject *
3334parsestrplus(struct compiling *c, const node *n)
3335{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003336 PyObject *v;
3337 int i;
3338 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003339 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003340 /* String literal concatenation */
3341 for (i = 1; i < NCH(n); i++) {
3342 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003343 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003344 if (s == NULL)
3345 goto onError;
3346 if (PyString_Check(v) && PyString_Check(s)) {
3347 PyString_ConcatAndDel(&v, s);
3348 if (v == NULL)
3349 goto onError;
3350 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003352 else {
3353 PyObject *temp = PyUnicode_Concat(v, s);
3354 Py_DECREF(s);
3355 Py_DECREF(v);
3356 v = temp;
3357 if (v == NULL)
3358 goto onError;
3359 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003361 }
3362 }
3363 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364
3365 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003366 Py_XDECREF(v);
3367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368}