blob: ad78840c056ecd0cb55ffda104e684898991129a [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) {
Christian Heimes593daf52008-05-26 12:51:38 +000049 PyObject* id = PyBytes_InternFromString(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +000050 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
Benjamin Petersoncbd78132008-06-08 15:45:23 +0000116static int
117ast_warn(struct compiling *c, const node *n, char *msg)
118{
119 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
120 NULL, NULL) < 0) {
121 /* if -Werr, change it to a SyntaxError */
122 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
123 ast_error(n, msg);
124 return 0;
125 }
126 return 1;
127}
128
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129/* num_stmts() returns number of contained statements.
130
131 Use this routine to determine how big a sequence is needed for
132 the statements in a parse tree. Its raison d'etre is this bit of
133 grammar:
134
135 stmt: simple_stmt | compound_stmt
136 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
137
138 A simple_stmt can contain multiple small_stmt elements joined
139 by semicolons. If the arg is a simple_stmt, the number of
140 small_stmt elements is returned.
141*/
142
143static int
144num_stmts(const node *n)
145{
146 int i, l;
147 node *ch;
148
149 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000150 case single_input:
151 if (TYPE(CHILD(n, 0)) == NEWLINE)
152 return 0;
153 else
154 return num_stmts(CHILD(n, 0));
155 case file_input:
156 l = 0;
157 for (i = 0; i < NCH(n); i++) {
158 ch = CHILD(n, i);
159 if (TYPE(ch) == stmt)
160 l += num_stmts(ch);
161 }
162 return l;
163 case stmt:
164 return num_stmts(CHILD(n, 0));
165 case compound_stmt:
166 return 1;
167 case simple_stmt:
168 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
169 case suite:
170 if (NCH(n) == 1)
171 return num_stmts(CHILD(n, 0));
172 else {
173 l = 0;
174 for (i = 2; i < (NCH(n) - 1); i++)
175 l += num_stmts(CHILD(n, i));
176 return l;
177 }
178 default: {
179 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000181 sprintf(buf, "Non-statement found: %d %d\n",
182 TYPE(n), NCH(n));
183 Py_FatalError(buf);
184 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185 }
186 assert(0);
187 return 0;
188}
189
190/* Transform the CST rooted at node * to the appropriate AST
191*/
192
193mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000194PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000195 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000197 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 asdl_seq *stmts = NULL;
199 stmt_ty s;
200 node *ch;
201 struct compiling c;
202
203 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000204 c.c_encoding = "utf-8";
205 if (TYPE(n) == encoding_decl) {
206 ast_error(n, "encoding declaration in Unicode string");
207 goto error;
208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000210 c.c_encoding = STR(n);
211 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000213 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214 }
Christian Heimes3c608332008-03-26 22:01:37 +0000215 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000216 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000217 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218
Jeremy Hyltona8293132006-02-28 17:58:27 +0000219 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000221 case file_input:
222 stmts = asdl_seq_new(num_stmts(n), arena);
223 if (!stmts)
224 return NULL;
225 for (i = 0; i < NCH(n) - 1; i++) {
226 ch = CHILD(n, i);
227 if (TYPE(ch) == NEWLINE)
228 continue;
229 REQ(ch, stmt);
230 num = num_stmts(ch);
231 if (num == 1) {
232 s = ast_for_stmt(&c, ch);
233 if (!s)
234 goto error;
235 asdl_seq_SET(stmts, k++, s);
236 }
237 else {
238 ch = CHILD(ch, 0);
239 REQ(ch, simple_stmt);
240 for (j = 0; j < num; j++) {
241 s = ast_for_stmt(&c, CHILD(ch, j * 2));
242 if (!s)
243 goto error;
244 asdl_seq_SET(stmts, k++, s);
245 }
246 }
247 }
248 return Module(stmts, arena);
249 case eval_input: {
250 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000252 /* XXX Why not gen_for here? */
253 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
254 if (!testlist_ast)
255 goto error;
256 return Expression(testlist_ast, arena);
257 }
258 case single_input:
259 if (TYPE(CHILD(n, 0)) == NEWLINE) {
260 stmts = asdl_seq_new(1, arena);
261 if (!stmts)
262 goto error;
263 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
264 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000265 if (!asdl_seq_GET(stmts, 0))
266 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000267 return Interactive(stmts, arena);
268 }
269 else {
270 n = CHILD(n, 0);
271 num = num_stmts(n);
272 stmts = asdl_seq_new(num, arena);
273 if (!stmts)
274 goto error;
275 if (num == 1) {
276 s = ast_for_stmt(&c, n);
277 if (!s)
278 goto error;
279 asdl_seq_SET(stmts, 0, s);
280 }
281 else {
282 /* Only a simple_stmt can contain multiple statements. */
283 REQ(n, simple_stmt);
284 for (i = 0; i < NCH(n); i += 2) {
285 if (TYPE(CHILD(n, i)) == NEWLINE)
286 break;
287 s = ast_for_stmt(&c, CHILD(n, i));
288 if (!s)
289 goto error;
290 asdl_seq_SET(stmts, i / 2, s);
291 }
292 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000294 return Interactive(stmts, arena);
295 }
296 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000297 PyErr_Format(PyExc_SystemError,
298 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000299 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 }
301 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302 ast_error_finish(filename);
303 return NULL;
304}
305
306/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
307*/
308
309static operator_ty
310get_operator(const node *n)
311{
312 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000313 case VBAR:
314 return BitOr;
315 case CIRCUMFLEX:
316 return BitXor;
317 case AMPER:
318 return BitAnd;
319 case LEFTSHIFT:
320 return LShift;
321 case RIGHTSHIFT:
322 return RShift;
323 case PLUS:
324 return Add;
325 case MINUS:
326 return Sub;
327 case STAR:
328 return Mult;
329 case SLASH:
330 return Div;
331 case DOUBLESLASH:
332 return FloorDiv;
333 case PERCENT:
334 return Mod;
335 default:
336 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337 }
338}
339
Jeremy Hyltona8293132006-02-28 17:58:27 +0000340/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341
342 Only sets context for expr kinds that "can appear in assignment context"
343 (according to ../Parser/Python.asdl). For other expr kinds, it sets
344 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345*/
346
347static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000348set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349{
350 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000351 /* If a particular expression type can't be used for assign / delete,
352 set expr_name to its name and an error message will be generated.
353 */
354 const char* expr_name = NULL;
355
356 /* The ast defines augmented store and load contexts, but the
357 implementation here doesn't actually use them. The code may be
358 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000359 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000360 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000361 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000362 */
363 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364
365 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000366 case Attribute_kind:
367 if (ctx == Store &&
Christian Heimes593daf52008-05-26 12:51:38 +0000368 !strcmp(PyBytes_AS_STRING(e->v.Attribute.attr), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000369 return ast_error(n, "assignment to None");
370 }
371 e->v.Attribute.ctx = ctx;
372 break;
373 case Subscript_kind:
374 e->v.Subscript.ctx = ctx;
375 break;
376 case Name_kind:
377 if (ctx == Store &&
Christian Heimes593daf52008-05-26 12:51:38 +0000378 !strcmp(PyBytes_AS_STRING(e->v.Name.id), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000379 return ast_error(n, "assignment to None");
380 }
381 e->v.Name.ctx = ctx;
382 break;
383 case List_kind:
384 e->v.List.ctx = ctx;
385 s = e->v.List.elts;
386 break;
387 case Tuple_kind:
388 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
389 return ast_error(n, "can't assign to ()");
390 e->v.Tuple.ctx = ctx;
391 s = e->v.Tuple.elts;
392 break;
393 case Lambda_kind:
394 expr_name = "lambda";
395 break;
396 case Call_kind:
397 expr_name = "function call";
398 break;
399 case BoolOp_kind:
400 case BinOp_kind:
401 case UnaryOp_kind:
402 expr_name = "operator";
403 break;
404 case GeneratorExp_kind:
405 expr_name = "generator expression";
406 break;
407 case Yield_kind:
408 expr_name = "yield expression";
409 break;
410 case ListComp_kind:
411 expr_name = "list comprehension";
412 break;
413 case Dict_kind:
414 case Num_kind:
415 case Str_kind:
416 expr_name = "literal";
417 break;
418 case Compare_kind:
419 expr_name = "comparison";
420 break;
421 case Repr_kind:
422 expr_name = "repr";
423 break;
424 case IfExp_kind:
425 expr_name = "conditional expression";
426 break;
427 default:
428 PyErr_Format(PyExc_SystemError,
429 "unexpected expression in assignment %d (line %d)",
430 e->kind, e->lineno);
431 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000433 /* Check for error string set by switch */
434 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000435 char buf[300];
436 PyOS_snprintf(buf, sizeof(buf),
437 "can't %s %s",
438 ctx == Store ? "assign to" : "delete",
439 expr_name);
440 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000441 }
442
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000444 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445 */
446 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000447 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000449 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000450 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000451 return 0;
452 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 }
454 return 1;
455}
456
457static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000458ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000459{
460 REQ(n, augassign);
461 n = CHILD(n, 0);
462 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000463 case '+':
464 return Add;
465 case '-':
466 return Sub;
467 case '/':
468 if (STR(n)[1] == '/')
469 return FloorDiv;
470 else
471 return Div;
472 case '%':
473 return Mod;
474 case '<':
475 return LShift;
476 case '>':
477 return RShift;
478 case '&':
479 return BitAnd;
480 case '^':
481 return BitXor;
482 case '|':
483 return BitOr;
484 case '*':
485 if (STR(n)[1] == '*')
486 return Pow;
487 else
488 return Mult;
489 default:
490 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
491 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492 }
493}
494
495static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000496ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497{
498 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000499 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 */
501 REQ(n, comp_op);
502 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000503 n = CHILD(n, 0);
504 switch (TYPE(n)) {
505 case LESS:
506 return Lt;
507 case GREATER:
508 return Gt;
509 case EQEQUAL: /* == */
510 return Eq;
511 case LESSEQUAL:
512 return LtE;
513 case GREATEREQUAL:
514 return GtE;
515 case NOTEQUAL:
516 return NotEq;
517 case NAME:
518 if (strcmp(STR(n), "in") == 0)
519 return In;
520 if (strcmp(STR(n), "is") == 0)
521 return Is;
522 default:
523 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
524 STR(n));
525 return (cmpop_ty)0;
526 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527 }
528 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000529 /* handle "not in" and "is not" */
530 switch (TYPE(CHILD(n, 0))) {
531 case NAME:
532 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
533 return NotIn;
534 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
535 return IsNot;
536 default:
537 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
538 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
539 return (cmpop_ty)0;
540 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 }
Neal Norwitz79792652005-11-14 04:25:03 +0000542 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000543 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000544 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545}
546
547static asdl_seq *
548seq_for_testlist(struct compiling *c, const node *n)
549{
550 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000551 asdl_seq *seq;
552 expr_ty expression;
553 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000554 assert(TYPE(n) == testlist ||
555 TYPE(n) == listmaker ||
556 TYPE(n) == testlist_gexp ||
557 TYPE(n) == testlist_safe ||
558 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000560 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000562 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563
564 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000565 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000567 expression = ast_for_expr(c, CHILD(n, i));
568 if (!expression)
569 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000571 assert(i / 2 < seq->size);
572 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 }
574 return seq;
575}
576
577static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000578compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579{
580 int i, len = (NCH(n) + 1) / 2;
581 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000582 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000584 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585
Neal Norwitz3a230172006-09-22 08:18:10 +0000586 /* fpdef: NAME | '(' fplist ')'
587 fplist: fpdef (',' fpdef)* [',']
588 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000591 const node *fpdef_node = CHILD(n, 2*i);
592 const node *child;
593 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000594set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000595 /* fpdef_node is either a NAME or an fplist */
596 child = CHILD(fpdef_node, 0);
597 if (TYPE(child) == NAME) {
598 if (!strcmp(STR(child), "None")) {
599 ast_error(child, "assignment to None");
600 return NULL;
601 }
602 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
603 child->n_col_offset, c->c_arena);
604 }
605 else {
606 assert(TYPE(fpdef_node) == fpdef);
607 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
608 child = CHILD(fpdef_node, 1);
609 assert(TYPE(child) == fplist);
610 /* NCH == 1 means we have (x), we need to elide the extra parens */
611 if (NCH(child) == 1) {
612 fpdef_node = CHILD(child, 0);
613 assert(TYPE(fpdef_node) == fpdef);
614 goto set_name;
615 }
616 arg = compiler_complex_args(c, child);
617 }
618 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 }
620
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000621 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000622 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000623 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 return result;
625}
626
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627
Jeremy Hyltona8293132006-02-28 17:58:27 +0000628/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
630static arguments_ty
631ast_for_arguments(struct compiling *c, const node *n)
632{
633 /* parameters: '(' [varargslist] ')'
634 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000635 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000637 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 asdl_seq *args, *defaults;
639 identifier vararg = NULL, kwarg = NULL;
640 node *ch;
641
642 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000643 if (NCH(n) == 2) /* () as argument list */
644 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
645 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 }
647 REQ(n, varargslist);
648
649 /* first count the number of normal args & defaults */
650 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000651 ch = CHILD(n, i);
652 if (TYPE(ch) == fpdef)
653 n_args++;
654 if (TYPE(ch) == EQUAL)
655 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000657 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000659 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000660 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000662 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663
664 /* fpdef: NAME | '(' fplist ')'
665 fplist: fpdef (',' fpdef)* [',']
666 */
667 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000668 j = 0; /* index for defaults */
669 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000671 ch = CHILD(n, i);
672 switch (TYPE(ch)) {
673 case fpdef:
674 handle_fpdef:
675 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
676 anything other than EQUAL or a comma? */
677 /* XXX Should NCH(n) check be made a separate check? */
678 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
679 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
680 if (!expression)
681 goto error;
682 assert(defaults != NULL);
683 asdl_seq_SET(defaults, j++, expression);
684 i += 2;
685 found_default = 1;
686 }
687 else if (found_default) {
688 ast_error(n,
689 "non-default argument follows default argument");
690 goto error;
691 }
692 if (NCH(ch) == 3) {
693 ch = CHILD(ch, 1);
694 /* def foo((x)): is not complex, special case. */
695 if (NCH(ch) != 1) {
696 /* We have complex arguments, setup for unpacking. */
697 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000698 if (!asdl_seq_GET(args, k-1))
699 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000700 } else {
701 /* def foo((x)): setup for checking NAME below. */
702 /* Loop because there can be many parens and tuple
703 unpacking mixed in. */
704 ch = CHILD(ch, 0);
705 assert(TYPE(ch) == fpdef);
706 goto handle_fpdef;
707 }
708 }
709 if (TYPE(CHILD(ch, 0)) == NAME) {
710 expr_ty name;
711 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
712 ast_error(CHILD(ch, 0), "assignment to None");
713 goto error;
714 }
715 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
716 Param, LINENO(ch), ch->n_col_offset,
717 c->c_arena);
718 if (!name)
719 goto error;
720 asdl_seq_SET(args, k++, name);
721
722 }
723 i += 2; /* the name and the comma */
724 break;
725 case STAR:
726 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
727 ast_error(CHILD(n, i+1), "assignment to None");
728 goto error;
729 }
730 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
731 i += 3;
732 break;
733 case DOUBLESTAR:
734 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
735 ast_error(CHILD(n, i+1), "assignment to None");
736 goto error;
737 }
738 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
739 i += 3;
740 break;
741 default:
742 PyErr_Format(PyExc_SystemError,
743 "unexpected node in varargslist: %d @ %d",
744 TYPE(ch), i);
745 goto error;
746 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 }
748
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000749 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750
751 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000752 Py_XDECREF(vararg);
753 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 return NULL;
755}
756
757static expr_ty
758ast_for_dotted_name(struct compiling *c, const node *n)
759{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000760 expr_ty e;
761 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000762 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 int i;
764
765 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000766
767 lineno = LINENO(n);
768 col_offset = n->n_col_offset;
769
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 id = NEW_IDENTIFIER(CHILD(n, 0));
771 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000772 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000773 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000775 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776
777 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000778 id = NEW_IDENTIFIER(CHILD(n, i));
779 if (!id)
780 return NULL;
781 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
782 if (!e)
783 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 }
785
786 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787}
788
789static expr_ty
790ast_for_decorator(struct compiling *c, const node *n)
791{
792 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
793 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000794 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795
796 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000797 REQ(CHILD(n, 0), AT);
798 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
800 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
801 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000802 return NULL;
803
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000805 d = name_expr;
806 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000809 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
810 n->n_col_offset, c->c_arena);
811 if (!d)
812 return NULL;
813 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 }
815 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000816 d = ast_for_call(c, CHILD(n, 3), name_expr);
817 if (!d)
818 return NULL;
819 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821
822 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823}
824
825static asdl_seq*
826ast_for_decorators(struct compiling *c, const node *n)
827{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000828 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000829 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 int i;
831
832 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000835 return NULL;
836
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000838 d = ast_for_decorator(c, CHILD(n, i));
839 if (!d)
840 return NULL;
841 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 }
843 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844}
845
846static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000847ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848{
Christian Heimes5224d282008-02-23 15:01:05 +0000849 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000850 identifier name;
851 arguments_ty args;
852 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000853 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854
855 REQ(n, funcdef);
856
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 name = NEW_IDENTIFIER(CHILD(n, name_i));
858 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000859 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000861 ast_error(CHILD(n, name_i), "assignment to None");
862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 }
864 args = ast_for_arguments(c, CHILD(n, name_i + 1));
865 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000866 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 body = ast_for_suite(c, CHILD(n, name_i + 3));
868 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000869 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000871 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000872 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873}
874
Christian Heimes5224d282008-02-23 15:01:05 +0000875static stmt_ty
876ast_for_decorated(struct compiling *c, const node *n)
877{
878 /* decorated: decorators (classdef | funcdef) */
879 stmt_ty thing = NULL;
880 asdl_seq *decorator_seq = NULL;
881
882 REQ(n, decorated);
883
884 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
885 if (!decorator_seq)
886 return NULL;
887
888 assert(TYPE(CHILD(n, 1)) == funcdef ||
889 TYPE(CHILD(n, 1)) == classdef);
890
891 if (TYPE(CHILD(n, 1)) == funcdef) {
892 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
893 } else if (TYPE(CHILD(n, 1)) == classdef) {
894 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
895 }
896 /* we count the decorators in when talking about the class' or
897 function's line number */
898 if (thing) {
899 thing->lineno = LINENO(n);
900 thing->col_offset = n->n_col_offset;
901 }
902 return thing;
903}
904
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905static expr_ty
906ast_for_lambdef(struct compiling *c, const node *n)
907{
908 /* lambdef: 'lambda' [varargslist] ':' test */
909 arguments_ty args;
910 expr_ty expression;
911
912 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000913 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
914 if (!args)
915 return NULL;
916 expression = ast_for_expr(c, CHILD(n, 2));
917 if (!expression)
918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919 }
920 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000921 args = ast_for_arguments(c, CHILD(n, 1));
922 if (!args)
923 return NULL;
924 expression = ast_for_expr(c, CHILD(n, 3));
925 if (!expression)
926 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927 }
928
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000929 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930}
931
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000932static expr_ty
933ast_for_ifexpr(struct compiling *c, const node *n)
934{
935 /* test: or_test 'if' or_test 'else' test */
936 expr_ty expression, body, orelse;
937
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000938 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000939 body = ast_for_expr(c, CHILD(n, 0));
940 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000941 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000942 expression = ast_for_expr(c, CHILD(n, 2));
943 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000944 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000945 orelse = ast_for_expr(c, CHILD(n, 4));
946 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000947 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000948 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000949 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000950}
951
Neal Norwitze4d4f002006-09-05 03:58:26 +0000952/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
953 so there is only a single version. Possibly for loops can also re-use
954 the code.
955*/
956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957/* Count the number of 'for' loop in a list comprehension.
958
959 Helper for ast_for_listcomp().
960*/
961
962static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000963count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964{
965 int n_fors = 0;
966 node *ch = CHILD(n, 1);
967
968 count_list_for:
969 n_fors++;
970 REQ(ch, list_for);
971 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000972 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000974 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 count_list_iter:
976 REQ(ch, list_iter);
977 ch = CHILD(ch, 0);
978 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000979 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000981 if (NCH(ch) == 3) {
982 ch = CHILD(ch, 2);
983 goto count_list_iter;
984 }
985 else
986 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000988
989 /* Should never be reached */
990 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
991 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992}
993
994/* Count the number of 'if' statements in a list comprehension.
995
996 Helper for ast_for_listcomp().
997*/
998
999static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001000count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001{
1002 int n_ifs = 0;
1003
1004 count_list_iter:
1005 REQ(n, list_iter);
1006 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001007 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 n = CHILD(n, 0);
1009 REQ(n, list_if);
1010 n_ifs++;
1011 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001012 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 n = CHILD(n, 2);
1014 goto count_list_iter;
1015}
1016
1017static expr_ty
1018ast_for_listcomp(struct compiling *c, const node *n)
1019{
1020 /* listmaker: test ( list_for | (',' test)* [','] )
1021 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1022 list_iter: list_for | list_if
1023 list_if: 'if' test [list_iter]
1024 testlist_safe: test [(',' test)+ [',']]
1025 */
1026 expr_ty elt;
1027 asdl_seq *listcomps;
1028 int i, n_fors;
1029 node *ch;
1030
1031 REQ(n, listmaker);
1032 assert(NCH(n) > 1);
1033
1034 elt = ast_for_expr(c, CHILD(n, 0));
1035 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001038 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001042 listcomps = asdl_seq_new(n_fors, c->c_arena);
1043 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001044 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001045
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 ch = CHILD(n, 1);
1047 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001048 comprehension_ty lc;
1049 asdl_seq *t;
1050 expr_ty expression;
1051 node *for_ch;
1052
1053 REQ(ch, list_for);
1054
1055 for_ch = CHILD(ch, 1);
1056 t = ast_for_exprlist(c, for_ch, Store);
1057 if (!t)
1058 return NULL;
1059 expression = ast_for_testlist(c, CHILD(ch, 3));
1060 if (!expression)
1061 return NULL;
1062
1063 /* Check the # of children rather than the length of t, since
1064 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1065 */
1066 if (NCH(for_ch) == 1)
1067 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1068 c->c_arena);
1069 else
1070 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1071 c->c_arena),
1072 expression, NULL, c->c_arena);
1073 if (!lc)
1074 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001076 if (NCH(ch) == 5) {
1077 int j, n_ifs;
1078 asdl_seq *ifs;
1079 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001081 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001082 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001083 if (n_ifs == -1)
1084 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001086 ifs = asdl_seq_new(n_ifs, c->c_arena);
1087 if (!ifs)
1088 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001090 for (j = 0; j < n_ifs; j++) {
1091 REQ(ch, list_iter);
1092 ch = CHILD(ch, 0);
1093 REQ(ch, list_if);
1094
1095 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1096 if (!list_for_expr)
1097 return NULL;
1098
1099 asdl_seq_SET(ifs, j, list_for_expr);
1100 if (NCH(ch) == 3)
1101 ch = CHILD(ch, 2);
1102 }
1103 /* on exit, must guarantee that ch is a list_for */
1104 if (TYPE(ch) == list_iter)
1105 ch = CHILD(ch, 0);
1106 lc->ifs = ifs;
1107 }
1108 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 }
1110
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001111 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112}
1113
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001114/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115
1116 Helper for ast_for_genexp().
1117*/
1118
1119static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001120count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001122 int n_fors = 0;
1123 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124
1125 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001126 n_fors++;
1127 REQ(ch, gen_for);
1128 if (NCH(ch) == 5)
1129 ch = CHILD(ch, 4);
1130 else
1131 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001133 REQ(ch, gen_iter);
1134 ch = CHILD(ch, 0);
1135 if (TYPE(ch) == gen_for)
1136 goto count_gen_for;
1137 else if (TYPE(ch) == gen_if) {
1138 if (NCH(ch) == 3) {
1139 ch = CHILD(ch, 2);
1140 goto count_gen_iter;
1141 }
1142 else
1143 return n_fors;
1144 }
1145
1146 /* Should never be reached */
1147 PyErr_SetString(PyExc_SystemError,
1148 "logic error in count_gen_fors");
1149 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150}
1151
1152/* Count the number of 'if' statements in a generator expression.
1153
1154 Helper for ast_for_genexp().
1155*/
1156
1157static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001158count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001160 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001162 while (1) {
1163 REQ(n, gen_iter);
1164 if (TYPE(CHILD(n, 0)) == gen_for)
1165 return n_ifs;
1166 n = CHILD(n, 0);
1167 REQ(n, gen_if);
1168 n_ifs++;
1169 if (NCH(n) == 2)
1170 return n_ifs;
1171 n = CHILD(n, 2);
1172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173}
1174
Jeremy Hyltona8293132006-02-28 17:58:27 +00001175/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176static expr_ty
1177ast_for_genexp(struct compiling *c, const node *n)
1178{
1179 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001180 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 expr_ty elt;
1182 asdl_seq *genexps;
1183 int i, n_fors;
1184 node *ch;
1185
1186 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1187 assert(NCH(n) > 1);
1188
1189 elt = ast_for_expr(c, CHILD(n, 0));
1190 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001191 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001193 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001195 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001196
1197 genexps = asdl_seq_new(n_fors, c->c_arena);
1198 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001199 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 ch = CHILD(n, 1);
1202 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001203 comprehension_ty ge;
1204 asdl_seq *t;
1205 expr_ty expression;
1206 node *for_ch;
1207
1208 REQ(ch, gen_for);
1209
1210 for_ch = CHILD(ch, 1);
1211 t = ast_for_exprlist(c, for_ch, Store);
1212 if (!t)
1213 return NULL;
1214 expression = ast_for_expr(c, CHILD(ch, 3));
1215 if (!expression)
1216 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001217
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001218 /* Check the # of children rather than the length of t, since
1219 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1220 if (NCH(for_ch) == 1)
1221 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1222 NULL, c->c_arena);
1223 else
1224 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1225 c->c_arena),
1226 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001228 if (!ge)
1229 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001230
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001231 if (NCH(ch) == 5) {
1232 int j, n_ifs;
1233 asdl_seq *ifs;
1234
1235 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001236 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001237 if (n_ifs == -1)
1238 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001240 ifs = asdl_seq_new(n_ifs, c->c_arena);
1241 if (!ifs)
1242 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001243
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001244 for (j = 0; j < n_ifs; j++) {
1245 REQ(ch, gen_iter);
1246 ch = CHILD(ch, 0);
1247 REQ(ch, gen_if);
1248
1249 expression = ast_for_expr(c, CHILD(ch, 1));
1250 if (!expression)
1251 return NULL;
1252 asdl_seq_SET(ifs, j, expression);
1253 if (NCH(ch) == 3)
1254 ch = CHILD(ch, 2);
1255 }
1256 /* on exit, must guarantee that ch is a gen_for */
1257 if (TYPE(ch) == gen_iter)
1258 ch = CHILD(ch, 0);
1259 ge->ifs = ifs;
1260 }
1261 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 }
1263
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001264 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265}
1266
1267static expr_ty
1268ast_for_atom(struct compiling *c, const node *n)
1269{
1270 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1271 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1272 */
1273 node *ch = CHILD(n, 0);
1274
1275 switch (TYPE(ch)) {
1276 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001277 /* All names start in Load context, but may later be
1278 changed. */
1279 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1280 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001282 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001283 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001284#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001285 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1286 PyObject *type, *value, *tback, *errstr;
1287 PyErr_Fetch(&type, &value, &tback);
1288 errstr = ((PyUnicodeErrorObject *)value)->reason;
1289 if (errstr) {
1290 char *s = "";
1291 char buf[128];
Christian Heimes593daf52008-05-26 12:51:38 +00001292 s = PyBytes_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001293 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1294 ast_error(n, buf);
1295 } else {
1296 ast_error(n, "(unicode error) unknown error");
1297 }
1298 Py_DECREF(type);
1299 Py_DECREF(value);
1300 Py_XDECREF(tback);
1301 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001302#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001303 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001304 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001305 PyArena_AddPyObject(c->c_arena, str);
1306 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 }
1308 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001309 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001310 if (!pynum)
1311 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001312
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001313 PyArena_AddPyObject(c->c_arena, pynum);
1314 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 }
1316 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001317 ch = CHILD(n, 1);
1318
1319 if (TYPE(ch) == RPAR)
1320 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1321
1322 if (TYPE(ch) == yield_expr)
1323 return ast_for_expr(c, ch);
1324
1325 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1326 return ast_for_genexp(c, ch);
1327
1328 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001330 ch = CHILD(n, 1);
1331
1332 if (TYPE(ch) == RSQB)
1333 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1334
1335 REQ(ch, listmaker);
1336 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1337 asdl_seq *elts = seq_for_testlist(c, ch);
1338 if (!elts)
1339 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001340
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001341 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1342 }
1343 else
1344 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001346 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1347 int i, size;
1348 asdl_seq *keys, *values;
1349
1350 ch = CHILD(n, 1);
1351 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1352 keys = asdl_seq_new(size, c->c_arena);
1353 if (!keys)
1354 return NULL;
1355
1356 values = asdl_seq_new(size, c->c_arena);
1357 if (!values)
1358 return NULL;
1359
1360 for (i = 0; i < NCH(ch); i += 4) {
1361 expr_ty expression;
1362
1363 expression = ast_for_expr(c, CHILD(ch, i));
1364 if (!expression)
1365 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001366
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001367 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001368
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001369 expression = ast_for_expr(c, CHILD(ch, i + 2));
1370 if (!expression)
1371 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001372
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001373 asdl_seq_SET(values, i / 4, expression);
1374 }
1375 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 }
1377 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001378 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001379 if (Py_Py3kWarningFlag &&
1380 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001381 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001382 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001383 if (!expression)
1384 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001385
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001386 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 }
1388 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001389 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1390 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 }
1392}
1393
1394static slice_ty
1395ast_for_slice(struct compiling *c, const node *n)
1396{
1397 node *ch;
1398 expr_ty lower = NULL, upper = NULL, step = NULL;
1399
1400 REQ(n, subscript);
1401
1402 /*
1403 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1404 sliceop: ':' [test]
1405 */
1406 ch = CHILD(n, 0);
1407 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001408 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409
1410 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001411 /* 'step' variable hold no significance in terms of being used over
1412 other vars */
1413 step = ast_for_expr(c, ch);
1414 if (!step)
1415 return NULL;
1416
1417 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 }
1419
1420 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001421 lower = ast_for_expr(c, ch);
1422 if (!lower)
1423 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 }
1425
1426 /* If there's an upper bound it's in the second or third position. */
1427 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001428 if (NCH(n) > 1) {
1429 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001431 if (TYPE(n2) == test) {
1432 upper = ast_for_expr(c, n2);
1433 if (!upper)
1434 return NULL;
1435 }
1436 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001438 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001440 if (TYPE(n2) == test) {
1441 upper = ast_for_expr(c, n2);
1442 if (!upper)
1443 return NULL;
1444 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 }
1446
1447 ch = CHILD(n, NCH(n) - 1);
1448 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001449 if (NCH(ch) == 1) {
1450 /* No expression, so step is None */
1451 ch = CHILD(ch, 0);
1452 step = Name(new_identifier("None", c->c_arena), Load,
1453 LINENO(ch), ch->n_col_offset, c->c_arena);
1454 if (!step)
1455 return NULL;
1456 } else {
1457 ch = CHILD(ch, 1);
1458 if (TYPE(ch) == test) {
1459 step = ast_for_expr(c, ch);
1460 if (!step)
1461 return NULL;
1462 }
1463 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 }
1465
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001466 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467}
1468
1469static expr_ty
1470ast_for_binop(struct compiling *c, const node *n)
1471{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001472 /* Must account for a sequence of expressions.
1473 How should A op B op C by represented?
1474 BinOp(BinOp(A, op, B), op, C).
1475 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001477 int i, nops;
1478 expr_ty expr1, expr2, result;
1479 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001481 expr1 = ast_for_expr(c, CHILD(n, 0));
1482 if (!expr1)
1483 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001485 expr2 = ast_for_expr(c, CHILD(n, 2));
1486 if (!expr2)
1487 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001489 newoperator = get_operator(CHILD(n, 1));
1490 if (!newoperator)
1491 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001493 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1494 c->c_arena);
1495 if (!result)
1496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001498 nops = (NCH(n) - 1) / 2;
1499 for (i = 1; i < nops; i++) {
1500 expr_ty tmp_result, tmp;
1501 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001503 newoperator = get_operator(next_oper);
1504 if (!newoperator)
1505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001507 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1508 if (!tmp)
1509 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001511 tmp_result = BinOp(result, newoperator, tmp,
1512 LINENO(next_oper), next_oper->n_col_offset,
1513 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001514 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001515 return NULL;
1516 result = tmp_result;
1517 }
1518 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519}
1520
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001521static expr_ty
1522ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1523{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001524 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1525 subscriptlist: subscript (',' subscript)* [',']
1526 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1527 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001528 REQ(n, trailer);
1529 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001530 if (NCH(n) == 2)
1531 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1532 n->n_col_offset, c->c_arena);
1533 else
1534 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001535 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001536 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001537 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1538 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001539 }
1540 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001541 REQ(CHILD(n, 0), LSQB);
1542 REQ(CHILD(n, 2), RSQB);
1543 n = CHILD(n, 1);
1544 if (NCH(n) == 1) {
1545 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1546 if (!slc)
1547 return NULL;
1548 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1549 c->c_arena);
1550 }
1551 else {
1552 /* The grammar is ambiguous here. The ambiguity is resolved
1553 by treating the sequence as a tuple literal if there are
1554 no slice features.
1555 */
1556 int j;
1557 slice_ty slc;
1558 expr_ty e;
1559 bool simple = true;
1560 asdl_seq *slices, *elts;
1561 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1562 if (!slices)
1563 return NULL;
1564 for (j = 0; j < NCH(n); j += 2) {
1565 slc = ast_for_slice(c, CHILD(n, j));
1566 if (!slc)
1567 return NULL;
1568 if (slc->kind != Index_kind)
1569 simple = false;
1570 asdl_seq_SET(slices, j / 2, slc);
1571 }
1572 if (!simple) {
1573 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1574 Load, LINENO(n), n->n_col_offset, c->c_arena);
1575 }
1576 /* extract Index values and put them in a Tuple */
1577 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1578 if (!elts)
1579 return NULL;
1580 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1581 slc = (slice_ty)asdl_seq_GET(slices, j);
1582 assert(slc->kind == Index_kind && slc->v.Index.value);
1583 asdl_seq_SET(elts, j, slc->v.Index.value);
1584 }
1585 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1586 if (!e)
1587 return NULL;
1588 return Subscript(left_expr, Index(e, c->c_arena),
1589 Load, LINENO(n), n->n_col_offset, c->c_arena);
1590 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001591 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001592}
1593
1594static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001595ast_for_factor(struct compiling *c, const node *n)
1596{
1597 node *pfactor, *ppower, *patom, *pnum;
1598 expr_ty expression;
1599
1600 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001601 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001602 constant. The peephole optimizer already does something like
1603 this but it doesn't handle the case where the constant is
1604 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1605 PyLongObject.
1606 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001607 if (TYPE(CHILD(n, 0)) == MINUS &&
1608 NCH(n) == 2 &&
1609 TYPE((pfactor = CHILD(n, 1))) == factor &&
1610 NCH(pfactor) == 1 &&
1611 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1612 NCH(ppower) == 1 &&
1613 TYPE((patom = CHILD(ppower, 0))) == atom &&
1614 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1615 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1616 if (s == NULL)
1617 return NULL;
1618 s[0] = '-';
1619 strcpy(s + 1, STR(pnum));
1620 PyObject_FREE(STR(pnum));
1621 STR(pnum) = s;
1622 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001623 }
1624
1625 expression = ast_for_expr(c, CHILD(n, 1));
1626 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001627 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001628
1629 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001630 case PLUS:
1631 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1632 c->c_arena);
1633 case MINUS:
1634 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1635 c->c_arena);
1636 case TILDE:
1637 return UnaryOp(Invert, expression, LINENO(n),
1638 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001639 }
1640 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001641 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001642 return NULL;
1643}
1644
1645static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001646ast_for_power(struct compiling *c, const node *n)
1647{
1648 /* power: atom trailer* ('**' factor)*
1649 */
1650 int i;
1651 expr_ty e, tmp;
1652 REQ(n, power);
1653 e = ast_for_atom(c, CHILD(n, 0));
1654 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001655 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001656 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001657 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001658 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001659 node *ch = CHILD(n, i);
1660 if (TYPE(ch) != trailer)
1661 break;
1662 tmp = ast_for_trailer(c, ch, e);
1663 if (!tmp)
1664 return NULL;
1665 tmp->lineno = e->lineno;
1666 tmp->col_offset = e->col_offset;
1667 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001668 }
1669 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001670 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1671 if (!f)
1672 return NULL;
1673 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1674 if (!tmp)
1675 return NULL;
1676 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001677 }
1678 return e;
1679}
1680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681/* Do not name a variable 'expr'! Will cause a compile error.
1682*/
1683
1684static expr_ty
1685ast_for_expr(struct compiling *c, const node *n)
1686{
1687 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001688 test: or_test ['if' or_test 'else' test] | lambdef
1689 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 and_test: not_test ('and' not_test)*
1691 not_test: 'not' not_test | comparison
1692 comparison: expr (comp_op expr)*
1693 expr: xor_expr ('|' xor_expr)*
1694 xor_expr: and_expr ('^' and_expr)*
1695 and_expr: shift_expr ('&' shift_expr)*
1696 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1697 arith_expr: term (('+'|'-') term)*
1698 term: factor (('*'|'/'|'%'|'//') factor)*
1699 factor: ('+'|'-'|'~') factor | power
1700 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001701
1702 As well as modified versions that exist for backward compatibility,
1703 to explicitly allow:
1704 [ x for x in lambda: 0, lambda: 1 ]
1705 (which would be ambiguous without these extra rules)
1706
1707 old_test: or_test | old_lambdef
1708 old_lambdef: 'lambda' [vararglist] ':' old_test
1709
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 */
1711
1712 asdl_seq *seq;
1713 int i;
1714
1715 loop:
1716 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001717 case test:
1718 case old_test:
1719 if (TYPE(CHILD(n, 0)) == lambdef ||
1720 TYPE(CHILD(n, 0)) == old_lambdef)
1721 return ast_for_lambdef(c, CHILD(n, 0));
1722 else if (NCH(n) > 1)
1723 return ast_for_ifexpr(c, n);
1724 /* Fallthrough */
1725 case or_test:
1726 case and_test:
1727 if (NCH(n) == 1) {
1728 n = CHILD(n, 0);
1729 goto loop;
1730 }
1731 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1732 if (!seq)
1733 return NULL;
1734 for (i = 0; i < NCH(n); i += 2) {
1735 expr_ty e = ast_for_expr(c, CHILD(n, i));
1736 if (!e)
1737 return NULL;
1738 asdl_seq_SET(seq, i / 2, e);
1739 }
1740 if (!strcmp(STR(CHILD(n, 1)), "and"))
1741 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1742 c->c_arena);
1743 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1744 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1745 case not_test:
1746 if (NCH(n) == 1) {
1747 n = CHILD(n, 0);
1748 goto loop;
1749 }
1750 else {
1751 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1752 if (!expression)
1753 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001755 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1756 c->c_arena);
1757 }
1758 case comparison:
1759 if (NCH(n) == 1) {
1760 n = CHILD(n, 0);
1761 goto loop;
1762 }
1763 else {
1764 expr_ty expression;
1765 asdl_int_seq *ops;
1766 asdl_seq *cmps;
1767 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1768 if (!ops)
1769 return NULL;
1770 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1771 if (!cmps) {
1772 return NULL;
1773 }
1774 for (i = 1; i < NCH(n); i += 2) {
1775 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001777 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001778 if (!newoperator) {
1779 return NULL;
1780 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001782 expression = ast_for_expr(c, CHILD(n, i + 1));
1783 if (!expression) {
1784 return NULL;
1785 }
1786
1787 asdl_seq_SET(ops, i / 2, newoperator);
1788 asdl_seq_SET(cmps, i / 2, expression);
1789 }
1790 expression = ast_for_expr(c, CHILD(n, 0));
1791 if (!expression) {
1792 return NULL;
1793 }
1794
1795 return Compare(expression, ops, cmps, LINENO(n),
1796 n->n_col_offset, c->c_arena);
1797 }
1798 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001800 /* The next five cases all handle BinOps. The main body of code
1801 is the same in each case, but the switch turned inside out to
1802 reuse the code for each type of operator.
1803 */
1804 case expr:
1805 case xor_expr:
1806 case and_expr:
1807 case shift_expr:
1808 case arith_expr:
1809 case term:
1810 if (NCH(n) == 1) {
1811 n = CHILD(n, 0);
1812 goto loop;
1813 }
1814 return ast_for_binop(c, n);
1815 case yield_expr: {
1816 expr_ty exp = NULL;
1817 if (NCH(n) == 2) {
1818 exp = ast_for_testlist(c, CHILD(n, 1));
1819 if (!exp)
1820 return NULL;
1821 }
1822 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1823 }
1824 case factor:
1825 if (NCH(n) == 1) {
1826 n = CHILD(n, 0);
1827 goto loop;
1828 }
1829 return ast_for_factor(c, n);
1830 case power:
1831 return ast_for_power(c, n);
1832 default:
1833 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001836 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 return NULL;
1838}
1839
1840static expr_ty
1841ast_for_call(struct compiling *c, const node *n, expr_ty func)
1842{
1843 /*
1844 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001845 | '**' test)
1846 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 */
1848
1849 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001850 asdl_seq *args;
1851 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 expr_ty vararg = NULL, kwarg = NULL;
1853
1854 REQ(n, arglist);
1855
1856 nargs = 0;
1857 nkeywords = 0;
1858 ngens = 0;
1859 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001860 node *ch = CHILD(n, i);
1861 if (TYPE(ch) == argument) {
1862 if (NCH(ch) == 1)
1863 nargs++;
1864 else if (TYPE(CHILD(ch, 1)) == gen_for)
1865 ngens++;
1866 else
1867 nkeywords++;
1868 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 }
1870 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001871 ast_error(n, "Generator expression must be parenthesized "
1872 "if not sole argument");
1873 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 }
1875
1876 if (nargs + nkeywords + ngens > 255) {
1877 ast_error(n, "more than 255 arguments");
1878 return NULL;
1879 }
1880
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001881 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001883 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001886 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 nargs = 0;
1888 nkeywords = 0;
1889 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001890 node *ch = CHILD(n, i);
1891 if (TYPE(ch) == argument) {
1892 expr_ty e;
1893 if (NCH(ch) == 1) {
1894 if (nkeywords) {
1895 ast_error(CHILD(ch, 0),
1896 "non-keyword arg after keyword arg");
1897 return NULL;
1898 }
1899 e = ast_for_expr(c, CHILD(ch, 0));
1900 if (!e)
1901 return NULL;
1902 asdl_seq_SET(args, nargs++, e);
1903 }
1904 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1905 e = ast_for_genexp(c, ch);
1906 if (!e)
1907 return NULL;
1908 asdl_seq_SET(args, nargs++, e);
1909 }
1910 else {
1911 keyword_ty kw;
1912 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001914 /* CHILD(ch, 0) is test, but must be an identifier? */
1915 e = ast_for_expr(c, CHILD(ch, 0));
1916 if (!e)
1917 return NULL;
1918 /* f(lambda x: x[0] = 3) ends up getting parsed with
1919 * LHS test = lambda x: x[0], and RHS test = 3.
1920 * SF bug 132313 points out that complaining about a keyword
1921 * then is very confusing.
1922 */
1923 if (e->kind == Lambda_kind) {
1924 ast_error(CHILD(ch, 0),
1925 "lambda cannot contain assignment");
1926 return NULL;
1927 } else if (e->kind != Name_kind) {
1928 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1929 return NULL;
1930 }
1931 key = e->v.Name.id;
Christian Heimes593daf52008-05-26 12:51:38 +00001932 if (!strcmp(PyBytes_AS_STRING(key), "None")) {
Georg Brandle06cf452007-06-07 13:23:24 +00001933 ast_error(CHILD(ch, 0), "assignment to None");
1934 return NULL;
1935 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001936 e = ast_for_expr(c, CHILD(ch, 2));
1937 if (!e)
1938 return NULL;
1939 kw = keyword(key, e, c->c_arena);
1940 if (!kw)
1941 return NULL;
1942 asdl_seq_SET(keywords, nkeywords++, kw);
1943 }
1944 }
1945 else if (TYPE(ch) == STAR) {
1946 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001947 if (!vararg)
1948 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001949 i++;
1950 }
1951 else if (TYPE(ch) == DOUBLESTAR) {
1952 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001953 if (!kwarg)
1954 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001955 i++;
1956 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 }
1958
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001959 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1960 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961}
1962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001964ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001966 /* testlist_gexp: test (',' test)* [','] */
1967 /* testlist: test (',' test)* [','] */
1968 /* testlist_safe: test (',' test)+ [','] */
1969 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001971 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001972 if (NCH(n) > 1)
1973 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001974 }
1975 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001976 assert(TYPE(n) == testlist ||
1977 TYPE(n) == testlist_safe ||
1978 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001979 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001981 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001983 asdl_seq *tmp = seq_for_testlist(c, n);
1984 if (!tmp)
1985 return NULL;
1986 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001988}
1989
1990static expr_ty
1991ast_for_testlist_gexp(struct compiling *c, const node* n)
1992{
1993 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1994 /* argument: test [ gen_for ] */
1995 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001996 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001997 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001998 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001999}
2000
2001/* like ast_for_testlist() but returns a sequence */
2002static asdl_seq*
2003ast_for_class_bases(struct compiling *c, const node* n)
2004{
2005 /* testlist: test (',' test)* [','] */
2006 assert(NCH(n) > 0);
2007 REQ(n, testlist);
2008 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002009 expr_ty base;
2010 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2011 if (!bases)
2012 return NULL;
2013 base = ast_for_expr(c, CHILD(n, 0));
2014 if (!base)
2015 return NULL;
2016 asdl_seq_SET(bases, 0, base);
2017 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002018 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002019
2020 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021}
2022
2023static stmt_ty
2024ast_for_expr_stmt(struct compiling *c, const node *n)
2025{
2026 REQ(n, expr_stmt);
2027 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002028 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 testlist: test (',' test)* [',']
2030 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002031 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 test: ... here starts the operator precendence dance
2033 */
2034
2035 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002036 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2037 if (!e)
2038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002040 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 }
2042 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002043 expr_ty expr1, expr2;
2044 operator_ty newoperator;
2045 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002047 expr1 = ast_for_testlist(c, ch);
2048 if (!expr1)
2049 return NULL;
2050 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2051 switch (expr1->kind) {
2052 case GeneratorExp_kind:
2053 ast_error(ch, "augmented assignment to generator "
2054 "expression not possible");
2055 return NULL;
2056 case Yield_kind:
2057 ast_error(ch, "augmented assignment to yield "
2058 "expression not possible");
2059 return NULL;
2060 case Name_kind: {
Christian Heimes593daf52008-05-26 12:51:38 +00002061 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002062 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2063 ast_error(ch, "assignment to None");
2064 return NULL;
2065 }
2066 break;
2067 }
2068 case Attribute_kind:
2069 case Subscript_kind:
2070 break;
2071 default:
2072 ast_error(ch, "illegal expression for augmented "
2073 "assignment");
2074 return NULL;
2075 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002076 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002077 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002079 ch = CHILD(n, 2);
2080 if (TYPE(ch) == testlist)
2081 expr2 = ast_for_testlist(c, ch);
2082 else
2083 expr2 = ast_for_expr(c, ch);
2084 if (!expr2)
2085 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002087 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002088 if (!newoperator)
2089 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002091 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2092 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 }
2094 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002095 int i;
2096 asdl_seq *targets;
2097 node *value;
2098 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002100 /* a normal assignment */
2101 REQ(CHILD(n, 1), EQUAL);
2102 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2103 if (!targets)
2104 return NULL;
2105 for (i = 0; i < NCH(n) - 2; i += 2) {
2106 expr_ty e;
2107 node *ch = CHILD(n, i);
2108 if (TYPE(ch) == yield_expr) {
2109 ast_error(ch, "assignment to yield expression not possible");
2110 return NULL;
2111 }
2112 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002114 /* set context to assign */
2115 if (!e)
2116 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002118 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002119 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002121 asdl_seq_SET(targets, i / 2, e);
2122 }
2123 value = CHILD(n, NCH(n) - 1);
2124 if (TYPE(value) == testlist)
2125 expression = ast_for_testlist(c, value);
2126 else
2127 expression = ast_for_expr(c, value);
2128 if (!expression)
2129 return NULL;
2130 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2131 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133}
2134
2135static stmt_ty
2136ast_for_print_stmt(struct compiling *c, const node *n)
2137{
2138 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002139 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 */
2141 expr_ty dest = NULL, expression;
2142 asdl_seq *seq;
2143 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002144 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145
2146 REQ(n, print_stmt);
2147 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002148 dest = ast_for_expr(c, CHILD(n, 2));
2149 if (!dest)
2150 return NULL;
2151 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002153 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002155 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002156 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002157 expression = ast_for_expr(c, CHILD(n, i));
2158 if (!expression)
2159 return NULL;
2160 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 }
2162 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002163 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164}
2165
2166static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002167ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168{
2169 asdl_seq *seq;
2170 int i;
2171 expr_ty e;
2172
2173 REQ(n, exprlist);
2174
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002175 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002177 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002179 e = ast_for_expr(c, CHILD(n, i));
2180 if (!e)
2181 return NULL;
2182 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002183 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002184 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 }
2186 return seq;
2187}
2188
2189static stmt_ty
2190ast_for_del_stmt(struct compiling *c, const node *n)
2191{
2192 asdl_seq *expr_list;
2193
2194 /* del_stmt: 'del' exprlist */
2195 REQ(n, del_stmt);
2196
2197 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2198 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002199 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002200 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201}
2202
2203static stmt_ty
2204ast_for_flow_stmt(struct compiling *c, const node *n)
2205{
2206 /*
2207 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002208 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 break_stmt: 'break'
2210 continue_stmt: 'continue'
2211 return_stmt: 'return' [testlist]
2212 yield_stmt: yield_expr
2213 yield_expr: 'yield' testlist
2214 raise_stmt: 'raise' [test [',' test [',' test]]]
2215 */
2216 node *ch;
2217
2218 REQ(n, flow_stmt);
2219 ch = CHILD(n, 0);
2220 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002221 case break_stmt:
2222 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2223 case continue_stmt:
2224 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2225 case yield_stmt: { /* will reduce to yield_expr */
2226 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2227 if (!exp)
2228 return NULL;
2229 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2230 }
2231 case return_stmt:
2232 if (NCH(ch) == 1)
2233 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2234 else {
2235 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2236 if (!expression)
2237 return NULL;
2238 return Return(expression, LINENO(n), n->n_col_offset,
2239 c->c_arena);
2240 }
2241 case raise_stmt:
2242 if (NCH(ch) == 1)
2243 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2244 c->c_arena);
2245 else if (NCH(ch) == 2) {
2246 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2247 if (!expression)
2248 return NULL;
2249 return Raise(expression, NULL, NULL, LINENO(n),
2250 n->n_col_offset, c->c_arena);
2251 }
2252 else if (NCH(ch) == 4) {
2253 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002255 expr1 = ast_for_expr(c, CHILD(ch, 1));
2256 if (!expr1)
2257 return NULL;
2258 expr2 = ast_for_expr(c, CHILD(ch, 3));
2259 if (!expr2)
2260 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002262 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2263 c->c_arena);
2264 }
2265 else if (NCH(ch) == 6) {
2266 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002268 expr1 = ast_for_expr(c, CHILD(ch, 1));
2269 if (!expr1)
2270 return NULL;
2271 expr2 = ast_for_expr(c, CHILD(ch, 3));
2272 if (!expr2)
2273 return NULL;
2274 expr3 = ast_for_expr(c, CHILD(ch, 5));
2275 if (!expr3)
2276 return NULL;
2277
2278 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2279 c->c_arena);
2280 }
2281 default:
2282 PyErr_Format(PyExc_SystemError,
2283 "unexpected flow_stmt: %d", TYPE(ch));
2284 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002286
2287 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2288 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289}
2290
2291static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002292alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293{
2294 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002295 import_as_name: NAME ['as' NAME]
2296 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 dotted_name: NAME ('.' NAME)*
2298 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002299 PyObject *str;
2300
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 loop:
2302 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002303 case import_as_name:
2304 str = NULL;
2305 if (NCH(n) == 3) {
2306 str = NEW_IDENTIFIER(CHILD(n, 2));
2307 }
2308 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2309 case dotted_as_name:
2310 if (NCH(n) == 1) {
2311 n = CHILD(n, 0);
2312 goto loop;
2313 }
2314 else {
2315 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2316 if (!a)
2317 return NULL;
2318 assert(!a->asname);
2319 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2320 return a;
2321 }
2322 break;
2323 case dotted_name:
2324 if (NCH(n) == 1)
2325 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2326 else {
2327 /* Create a string of the form "a.b.c" */
2328 int i;
2329 size_t len;
2330 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002332 len = 0;
2333 for (i = 0; i < NCH(n); i += 2)
2334 /* length of string plus one for the dot */
2335 len += strlen(STR(CHILD(n, i))) + 1;
2336 len--; /* the last name doesn't have a dot */
Christian Heimes593daf52008-05-26 12:51:38 +00002337 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002338 if (!str)
2339 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +00002340 s = PyBytes_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002341 if (!s)
2342 return NULL;
2343 for (i = 0; i < NCH(n); i += 2) {
2344 char *sch = STR(CHILD(n, i));
2345 strcpy(s, STR(CHILD(n, i)));
2346 s += strlen(sch);
2347 *s++ = '.';
2348 }
2349 --s;
2350 *s = '\0';
Christian Heimes593daf52008-05-26 12:51:38 +00002351 PyBytes_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002352 PyArena_AddPyObject(c->c_arena, str);
2353 return alias(str, NULL, c->c_arena);
2354 }
2355 break;
2356 case STAR:
Christian Heimes593daf52008-05-26 12:51:38 +00002357 str = PyBytes_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002358 PyArena_AddPyObject(c->c_arena, str);
2359 return alias(str, NULL, c->c_arena);
2360 default:
2361 PyErr_Format(PyExc_SystemError,
2362 "unexpected import name: %d", TYPE(n));
2363 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002365
2366 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367 return NULL;
2368}
2369
2370static stmt_ty
2371ast_for_import_stmt(struct compiling *c, const node *n)
2372{
2373 /*
2374 import_stmt: import_name | import_from
2375 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002376 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002377 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002379 int lineno;
2380 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381 int i;
2382 asdl_seq *aliases;
2383
2384 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002385 lineno = LINENO(n);
2386 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002388 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002389 n = CHILD(n, 1);
2390 REQ(n, dotted_as_names);
2391 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2392 if (!aliases)
2393 return NULL;
2394 for (i = 0; i < NCH(n); i += 2) {
2395 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2396 if (!import_alias)
2397 return NULL;
2398 asdl_seq_SET(aliases, i / 2, import_alias);
2399 }
2400 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002402 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002403 int n_children;
2404 int idx, ndots = 0;
2405 alias_ty mod = NULL;
2406 identifier modname;
2407
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002408 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002409 optional module name */
2410 for (idx = 1; idx < NCH(n); idx++) {
2411 if (TYPE(CHILD(n, idx)) == dotted_name) {
2412 mod = alias_for_import_name(c, CHILD(n, idx));
2413 idx++;
2414 break;
2415 } else if (TYPE(CHILD(n, idx)) != DOT) {
2416 break;
2417 }
2418 ndots++;
2419 }
2420 idx++; /* skip over the 'import' keyword */
2421 switch (TYPE(CHILD(n, idx))) {
2422 case STAR:
2423 /* from ... import * */
2424 n = CHILD(n, idx);
2425 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002426 break;
2427 case LPAR:
2428 /* from ... import (x, y, z) */
2429 n = CHILD(n, idx + 1);
2430 n_children = NCH(n);
2431 break;
2432 case import_as_names:
2433 /* from ... import x, y, z */
2434 n = CHILD(n, idx);
2435 n_children = NCH(n);
2436 if (n_children % 2 == 0) {
2437 ast_error(n, "trailing comma not allowed without"
2438 " surrounding parentheses");
2439 return NULL;
2440 }
2441 break;
2442 default:
2443 ast_error(n, "Unexpected node-type in from-import");
2444 return NULL;
2445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002447 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2448 if (!aliases)
2449 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002451 /* handle "from ... import *" special b/c there's no children */
2452 if (TYPE(n) == STAR) {
2453 alias_ty import_alias = alias_for_import_name(c, n);
2454 if (!import_alias)
2455 return NULL;
2456 asdl_seq_SET(aliases, 0, import_alias);
2457 }
2458 else {
2459 for (i = 0; i < NCH(n); i += 2) {
2460 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2461 if (!import_alias)
2462 return NULL;
2463 asdl_seq_SET(aliases, i / 2, import_alias);
2464 }
2465 }
2466 if (mod != NULL)
2467 modname = mod->name;
2468 else
2469 modname = new_identifier("", c->c_arena);
2470 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2471 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 }
Neal Norwitz79792652005-11-14 04:25:03 +00002473 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002474 "unknown import statement: starts with command '%s'",
2475 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 return NULL;
2477}
2478
2479static stmt_ty
2480ast_for_global_stmt(struct compiling *c, const node *n)
2481{
2482 /* global_stmt: 'global' NAME (',' NAME)* */
2483 identifier name;
2484 asdl_seq *s;
2485 int i;
2486
2487 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002488 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002492 name = NEW_IDENTIFIER(CHILD(n, i));
2493 if (!name)
2494 return NULL;
2495 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002497 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498}
2499
2500static stmt_ty
2501ast_for_exec_stmt(struct compiling *c, const node *n)
2502{
2503 expr_ty expr1, globals = NULL, locals = NULL;
2504 int n_children = NCH(n);
2505 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002506 PyErr_Format(PyExc_SystemError,
2507 "poorly formed 'exec' statement: %d parts to statement",
2508 n_children);
2509 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 }
2511
2512 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2513 REQ(n, exec_stmt);
2514 expr1 = ast_for_expr(c, CHILD(n, 1));
2515 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002518 globals = ast_for_expr(c, CHILD(n, 3));
2519 if (!globals)
2520 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 }
2522 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002523 locals = ast_for_expr(c, CHILD(n, 5));
2524 if (!locals)
2525 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 }
2527
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002528 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2529 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530}
2531
2532static stmt_ty
2533ast_for_assert_stmt(struct compiling *c, const node *n)
2534{
2535 /* assert_stmt: 'assert' test [',' test] */
2536 REQ(n, assert_stmt);
2537 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002538 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2539 if (!expression)
2540 return NULL;
2541 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2542 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 }
2544 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002545 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002547 expr1 = ast_for_expr(c, CHILD(n, 1));
2548 if (!expr1)
2549 return NULL;
2550 expr2 = ast_for_expr(c, CHILD(n, 3));
2551 if (!expr2)
2552 return NULL;
2553
2554 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 }
Neal Norwitz79792652005-11-14 04:25:03 +00002556 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002557 "improper number of parts to 'assert' statement: %d",
2558 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 return NULL;
2560}
2561
2562static asdl_seq *
2563ast_for_suite(struct compiling *c, const node *n)
2564{
2565 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002566 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 stmt_ty s;
2568 int i, total, num, end, pos = 0;
2569 node *ch;
2570
2571 REQ(n, suite);
2572
2573 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002574 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002576 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002578 n = CHILD(n, 0);
2579 /* simple_stmt always ends with a NEWLINE,
2580 and may have a trailing SEMI
2581 */
2582 end = NCH(n) - 1;
2583 if (TYPE(CHILD(n, end - 1)) == SEMI)
2584 end--;
2585 /* loop by 2 to skip semi-colons */
2586 for (i = 0; i < end; i += 2) {
2587 ch = CHILD(n, i);
2588 s = ast_for_stmt(c, ch);
2589 if (!s)
2590 return NULL;
2591 asdl_seq_SET(seq, pos++, s);
2592 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 }
2594 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002595 for (i = 2; i < (NCH(n) - 1); i++) {
2596 ch = CHILD(n, i);
2597 REQ(ch, stmt);
2598 num = num_stmts(ch);
2599 if (num == 1) {
2600 /* small_stmt or compound_stmt with only one child */
2601 s = ast_for_stmt(c, ch);
2602 if (!s)
2603 return NULL;
2604 asdl_seq_SET(seq, pos++, s);
2605 }
2606 else {
2607 int j;
2608 ch = CHILD(ch, 0);
2609 REQ(ch, simple_stmt);
2610 for (j = 0; j < NCH(ch); j += 2) {
2611 /* statement terminates with a semi-colon ';' */
2612 if (NCH(CHILD(ch, j)) == 0) {
2613 assert((j + 1) == NCH(ch));
2614 break;
2615 }
2616 s = ast_for_stmt(c, CHILD(ch, j));
2617 if (!s)
2618 return NULL;
2619 asdl_seq_SET(seq, pos++, s);
2620 }
2621 }
2622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 }
2624 assert(pos == seq->size);
2625 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626}
2627
2628static stmt_ty
2629ast_for_if_stmt(struct compiling *c, const node *n)
2630{
2631 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2632 ['else' ':' suite]
2633 */
2634 char *s;
2635
2636 REQ(n, if_stmt);
2637
2638 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002639 expr_ty expression;
2640 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002642 expression = ast_for_expr(c, CHILD(n, 1));
2643 if (!expression)
2644 return NULL;
2645 suite_seq = ast_for_suite(c, CHILD(n, 3));
2646 if (!suite_seq)
2647 return NULL;
2648
2649 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2650 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002652
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 s = STR(CHILD(n, 4));
2654 /* s[2], the third character in the string, will be
2655 's' for el_s_e, or
2656 'i' for el_i_f
2657 */
2658 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002659 expr_ty expression;
2660 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002662 expression = ast_for_expr(c, CHILD(n, 1));
2663 if (!expression)
2664 return NULL;
2665 seq1 = ast_for_suite(c, CHILD(n, 3));
2666 if (!seq1)
2667 return NULL;
2668 seq2 = ast_for_suite(c, CHILD(n, 6));
2669 if (!seq2)
2670 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002672 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2673 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 }
2675 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002676 int i, n_elif, has_else = 0;
2677 expr_ty expression;
2678 asdl_seq *suite_seq;
2679 asdl_seq *orelse = NULL;
2680 n_elif = NCH(n) - 4;
2681 /* must reference the child n_elif+1 since 'else' token is third,
2682 not fourth, child from the end. */
2683 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2684 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2685 has_else = 1;
2686 n_elif -= 3;
2687 }
2688 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002690 if (has_else) {
2691 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002693 orelse = asdl_seq_new(1, c->c_arena);
2694 if (!orelse)
2695 return NULL;
2696 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2697 if (!expression)
2698 return NULL;
2699 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2700 if (!suite_seq)
2701 return NULL;
2702 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2703 if (!suite_seq2)
2704 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002706 asdl_seq_SET(orelse, 0,
2707 If(expression, suite_seq, suite_seq2,
2708 LINENO(CHILD(n, NCH(n) - 6)),
2709 CHILD(n, NCH(n) - 6)->n_col_offset,
2710 c->c_arena));
2711 /* the just-created orelse handled the last elif */
2712 n_elif--;
2713 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002715 for (i = 0; i < n_elif; i++) {
2716 int off = 5 + (n_elif - i - 1) * 4;
2717 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2718 if (!newobj)
2719 return NULL;
2720 expression = ast_for_expr(c, CHILD(n, off));
2721 if (!expression)
2722 return NULL;
2723 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2724 if (!suite_seq)
2725 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002727 asdl_seq_SET(newobj, 0,
2728 If(expression, suite_seq, orelse,
2729 LINENO(CHILD(n, off)),
2730 CHILD(n, off)->n_col_offset, c->c_arena));
2731 orelse = newobj;
2732 }
2733 expression = ast_for_expr(c, CHILD(n, 1));
2734 if (!expression)
2735 return NULL;
2736 suite_seq = ast_for_suite(c, CHILD(n, 3));
2737 if (!suite_seq)
2738 return NULL;
2739 return If(expression, suite_seq, orelse,
2740 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002742
2743 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002744 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002745 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746}
2747
2748static stmt_ty
2749ast_for_while_stmt(struct compiling *c, const node *n)
2750{
2751 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2752 REQ(n, while_stmt);
2753
2754 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002755 expr_ty expression;
2756 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002758 expression = ast_for_expr(c, CHILD(n, 1));
2759 if (!expression)
2760 return NULL;
2761 suite_seq = ast_for_suite(c, CHILD(n, 3));
2762 if (!suite_seq)
2763 return NULL;
2764 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2765 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 }
2767 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002768 expr_ty expression;
2769 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002771 expression = ast_for_expr(c, CHILD(n, 1));
2772 if (!expression)
2773 return NULL;
2774 seq1 = ast_for_suite(c, CHILD(n, 3));
2775 if (!seq1)
2776 return NULL;
2777 seq2 = ast_for_suite(c, CHILD(n, 6));
2778 if (!seq2)
2779 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002781 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2782 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002784
2785 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002786 "wrong number of tokens for 'while' statement: %d",
2787 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789}
2790
2791static stmt_ty
2792ast_for_for_stmt(struct compiling *c, const node *n)
2793{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002794 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 expr_ty expression;
2796 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002797 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2799 REQ(n, for_stmt);
2800
2801 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002802 seq = ast_for_suite(c, CHILD(n, 8));
2803 if (!seq)
2804 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 }
2806
Neal Norwitzedef2be2006-07-12 05:26:17 +00002807 node_target = CHILD(n, 1);
2808 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002809 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002810 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002811 /* Check the # of children rather than the length of _target, since
2812 for x, in ... has 1 element in _target, but still requires a Tuple. */
2813 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002814 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002816 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002818 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002819 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002822 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002825 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002826 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827}
2828
2829static excepthandler_ty
2830ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2831{
Collin Winter62903052007-05-18 23:11:24 +00002832 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 REQ(exc, except_clause);
2834 REQ(body, suite);
2835
2836 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002837 asdl_seq *suite_seq = ast_for_suite(c, body);
2838 if (!suite_seq)
2839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840
Georg Brandla48f3ab2008-03-30 06:40:17 +00002841 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002842 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 }
2844 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002845 expr_ty expression;
2846 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002848 expression = ast_for_expr(c, CHILD(exc, 1));
2849 if (!expression)
2850 return NULL;
2851 suite_seq = ast_for_suite(c, body);
2852 if (!suite_seq)
2853 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854
Georg Brandla48f3ab2008-03-30 06:40:17 +00002855 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002856 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 }
2858 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002859 asdl_seq *suite_seq;
2860 expr_ty expression;
2861 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2862 if (!e)
2863 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002864 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002865 return NULL;
2866 expression = ast_for_expr(c, CHILD(exc, 1));
2867 if (!expression)
2868 return NULL;
2869 suite_seq = ast_for_suite(c, body);
2870 if (!suite_seq)
2871 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872
Georg Brandla48f3ab2008-03-30 06:40:17 +00002873 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002874 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002876
2877 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002878 "wrong number of children for 'except' clause: %d",
2879 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002880 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881}
2882
2883static stmt_ty
2884ast_for_try_stmt(struct compiling *c, const node *n)
2885{
Neal Norwitzf599f422005-12-17 21:33:47 +00002886 const int nch = NCH(n);
2887 int n_except = (nch - 3)/3;
2888 asdl_seq *body, *orelse = NULL, *finally = NULL;
2889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 REQ(n, try_stmt);
2891
Neal Norwitzf599f422005-12-17 21:33:47 +00002892 body = ast_for_suite(c, CHILD(n, 2));
2893 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002894 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895
Neal Norwitzf599f422005-12-17 21:33:47 +00002896 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002897 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2898 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2899 /* we can assume it's an "else",
2900 because nch >= 9 for try-else-finally and
2901 it would otherwise have a type of except_clause */
2902 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2903 if (orelse == NULL)
2904 return NULL;
2905 n_except--;
2906 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002908 finally = ast_for_suite(c, CHILD(n, nch - 1));
2909 if (finally == NULL)
2910 return NULL;
2911 n_except--;
2912 }
2913 else {
2914 /* we can assume it's an "else",
2915 otherwise it would have a type of except_clause */
2916 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2917 if (orelse == NULL)
2918 return NULL;
2919 n_except--;
2920 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002922 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002923 ast_error(n, "malformed 'try' statement");
2924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002926
2927 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002928 int i;
2929 stmt_ty except_st;
2930 /* process except statements to create a try ... except */
2931 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2932 if (handlers == NULL)
2933 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002934
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002935 for (i = 0; i < n_except; i++) {
2936 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2937 CHILD(n, 5 + i * 3));
2938 if (!e)
2939 return NULL;
2940 asdl_seq_SET(handlers, i, e);
2941 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002942
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002943 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2944 n->n_col_offset, c->c_arena);
2945 if (!finally)
2946 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002947
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002948 /* if a 'finally' is present too, we nest the TryExcept within a
2949 TryFinally to emulate try ... except ... finally */
2950 body = asdl_seq_new(1, c->c_arena);
2951 if (body == NULL)
2952 return NULL;
2953 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002954 }
2955
2956 /* must be a try ... finally (except clauses are in body, if any exist) */
2957 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002958 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959}
2960
Guido van Rossumc2e20742006-02-27 22:32:47 +00002961static expr_ty
2962ast_for_with_var(struct compiling *c, const node *n)
2963{
2964 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002965 return ast_for_expr(c, CHILD(n, 1));
2966}
2967
2968/* with_stmt: 'with' test [ with_var ] ':' suite */
2969static stmt_ty
2970ast_for_with_stmt(struct compiling *c, const node *n)
2971{
2972 expr_ty context_expr, optional_vars = NULL;
2973 int suite_index = 3; /* skip 'with', test, and ':' */
2974 asdl_seq *suite_seq;
2975
2976 assert(TYPE(n) == with_stmt);
2977 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002978 if (!context_expr)
2979 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002980 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002981 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002982
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002983 if (!optional_vars) {
2984 return NULL;
2985 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002986 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002987 return NULL;
2988 }
2989 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002990 }
2991
2992 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2993 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002994 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002995 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002996 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002997 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002998}
2999
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003001ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002{
3003 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 asdl_seq *bases, *s;
3005
3006 REQ(n, classdef);
3007
3008 if (!strcmp(STR(CHILD(n, 1)), "None")) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003009 ast_error(n, "assignment to None");
3010 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011 }
3012
3013 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003014 s = ast_for_suite(c, CHILD(n, 3));
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 /* check for empty base list */
3021 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003022 s = ast_for_suite(c, CHILD(n,5));
3023 if (!s)
3024 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003025 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3026 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 }
3028
3029 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003030 bases = ast_for_class_bases(c, CHILD(n, 3));
3031 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003032 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033
3034 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003035 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003036 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003037 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3038 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039}
3040
3041static stmt_ty
3042ast_for_stmt(struct compiling *c, const node *n)
3043{
3044 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003045 assert(NCH(n) == 1);
3046 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047 }
3048 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003049 assert(num_stmts(n) == 1);
3050 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 }
3052 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003053 REQ(n, small_stmt);
3054 n = CHILD(n, 0);
3055 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3056 | flow_stmt | import_stmt | global_stmt | exec_stmt
3057 | assert_stmt
3058 */
3059 switch (TYPE(n)) {
3060 case expr_stmt:
3061 return ast_for_expr_stmt(c, n);
3062 case print_stmt:
3063 return ast_for_print_stmt(c, n);
3064 case del_stmt:
3065 return ast_for_del_stmt(c, n);
3066 case pass_stmt:
3067 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3068 case flow_stmt:
3069 return ast_for_flow_stmt(c, n);
3070 case import_stmt:
3071 return ast_for_import_stmt(c, n);
3072 case global_stmt:
3073 return ast_for_global_stmt(c, n);
3074 case exec_stmt:
3075 return ast_for_exec_stmt(c, n);
3076 case assert_stmt:
3077 return ast_for_assert_stmt(c, n);
3078 default:
3079 PyErr_Format(PyExc_SystemError,
3080 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3081 TYPE(n), NCH(n));
3082 return NULL;
3083 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 }
3085 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003086 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003087 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003088 */
3089 node *ch = CHILD(n, 0);
3090 REQ(n, compound_stmt);
3091 switch (TYPE(ch)) {
3092 case if_stmt:
3093 return ast_for_if_stmt(c, ch);
3094 case while_stmt:
3095 return ast_for_while_stmt(c, ch);
3096 case for_stmt:
3097 return ast_for_for_stmt(c, ch);
3098 case try_stmt:
3099 return ast_for_try_stmt(c, ch);
3100 case with_stmt:
3101 return ast_for_with_stmt(c, ch);
3102 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003103 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003104 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003105 return ast_for_classdef(c, ch, NULL);
3106 case decorated:
3107 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003108 default:
3109 PyErr_Format(PyExc_SystemError,
3110 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3111 TYPE(n), NCH(n));
3112 return NULL;
3113 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114 }
3115}
3116
3117static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003118parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003120 const char *end;
3121 long x;
3122 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003124 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003125 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126#endif
3127
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003128 errno = 0;
3129 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003131 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003133 if (*end == 'l' || *end == 'L')
3134 return PyLong_FromString((char *)s, (char **)0, 0);
3135 if (s[0] == '0') {
3136 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3137 if (x < 0 && errno == 0) {
3138 return PyLong_FromString((char *)s,
3139 (char **)0,
3140 0);
3141 }
3142 }
3143 else
3144 x = PyOS_strtol((char *)s, (char **)&end, 0);
3145 if (*end == '\0') {
3146 if (errno != 0)
3147 return PyLong_FromString((char *)s, (char **)0, 0);
3148 return PyInt_FromLong(x);
3149 }
3150 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003152 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003153 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003154 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003155 complex.imag = PyOS_ascii_atof(s);
3156 PyFPE_END_PROTECT(complex)
3157 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003158 }
3159 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003161 {
3162 PyFPE_START_PROTECT("atof", return 0)
3163 dx = PyOS_ascii_atof(s);
3164 PyFPE_END_PROTECT(dx)
3165 return PyFloat_FromDouble(dx);
3166 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167}
3168
3169static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003170decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171{
3172#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003173 Py_FatalError("decode_utf8 should not be called in this build.");
3174 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003176 PyObject *u, *v;
3177 char *s, *t;
3178 t = s = (char *)*sPtr;
3179 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3180 while (s < end && (*s & 0x80)) s++;
3181 *sPtr = s;
3182 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3183 if (u == NULL)
3184 return NULL;
3185 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3186 Py_DECREF(u);
3187 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188#endif
3189}
3190
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003191#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003193decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003195 PyObject *v, *u;
3196 char *buf;
3197 char *p;
3198 const char *end;
3199 if (encoding == NULL) {
3200 buf = (char *)s;
3201 u = NULL;
3202 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3203 buf = (char *)s;
3204 u = NULL;
3205 } else {
3206 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Christian Heimes593daf52008-05-26 12:51:38 +00003207 u = PyBytes_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003208 if (u == NULL)
3209 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +00003210 p = buf = PyBytes_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003211 end = s + len;
3212 while (s < end) {
3213 if (*s == '\\') {
3214 *p++ = *s++;
3215 if (*s & 0x80) {
3216 strcpy(p, "u005c");
3217 p += 5;
3218 }
3219 }
3220 if (*s & 0x80) { /* XXX inefficient */
3221 PyObject *w;
3222 char *r;
3223 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003224 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003225 if (w == NULL) {
3226 Py_DECREF(u);
3227 return NULL;
3228 }
Christian Heimes593daf52008-05-26 12:51:38 +00003229 r = PyBytes_AsString(w);
3230 rn = PyBytes_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003231 assert(rn % 2 == 0);
3232 for (i = 0; i < rn; i += 2) {
3233 sprintf(p, "\\u%02x%02x",
3234 r[i + 0] & 0xFF,
3235 r[i + 1] & 0xFF);
3236 p += 6;
3237 }
3238 Py_DECREF(w);
3239 } else {
3240 *p++ = *s++;
3241 }
3242 }
3243 len = p - buf;
3244 s = buf;
3245 }
3246 if (rawmode)
3247 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3248 else
3249 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3250 Py_XDECREF(u);
3251 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003253#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254
3255/* s is a Python string literal, including the bracketing quote characters,
3256 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3257 * parsestr parses it, and returns the decoded Python string object.
3258 */
3259static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003260parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003262 size_t len;
3263 int quote = Py_CHARMASK(*s);
3264 int rawmode = 0;
3265 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003266 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003268 if (isalpha(quote) || quote == '_') {
3269 if (quote == 'u' || quote == 'U') {
3270 quote = *++s;
3271 unicode = 1;
3272 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003273 if (quote == 'b' || quote == 'B') {
3274 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003275 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003276 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003277 if (quote == 'r' || quote == 'R') {
3278 quote = *++s;
3279 rawmode = 1;
3280 }
3281 }
3282 if (quote != '\'' && quote != '\"') {
3283 PyErr_BadInternalCall();
3284 return NULL;
3285 }
3286 s++;
3287 len = strlen(s);
3288 if (len > INT_MAX) {
3289 PyErr_SetString(PyExc_OverflowError,
3290 "string to parse is too long");
3291 return NULL;
3292 }
3293 if (s[--len] != quote) {
3294 PyErr_BadInternalCall();
3295 return NULL;
3296 }
3297 if (len >= 4 && s[0] == quote && s[1] == quote) {
3298 s += 2;
3299 len -= 2;
3300 if (s[--len] != quote || s[--len] != quote) {
3301 PyErr_BadInternalCall();
3302 return NULL;
3303 }
3304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003306 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003307 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003308 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003310 need_encoding = (c->c_encoding != NULL &&
3311 strcmp(c->c_encoding, "utf-8") != 0 &&
3312 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003313 if (rawmode || strchr(s, '\\') == NULL) {
3314 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003316 /* This should not happen - we never see any other
3317 encoding. */
3318 Py_FatalError(
3319 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003321 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3322 if (u == NULL)
3323 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003324 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003325 Py_DECREF(u);
3326 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003328 } else {
Christian Heimes593daf52008-05-26 12:51:38 +00003329 return PyBytes_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003330 }
3331 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332
Christian Heimes593daf52008-05-26 12:51:38 +00003333 return PyBytes_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003334 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003335}
3336
3337/* Build a Python string object out of a STRING atom. This takes care of
3338 * compile-time literal catenation, calling parsestr() on each piece, and
3339 * pasting the intermediate results together.
3340 */
3341static PyObject *
3342parsestrplus(struct compiling *c, const node *n)
3343{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003344 PyObject *v;
3345 int i;
3346 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003347 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003348 /* String literal concatenation */
3349 for (i = 1; i < NCH(n); i++) {
3350 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003351 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003352 if (s == NULL)
3353 goto onError;
Christian Heimes593daf52008-05-26 12:51:38 +00003354 if (PyBytes_Check(v) && PyBytes_Check(s)) {
3355 PyBytes_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003356 if (v == NULL)
3357 goto onError;
3358 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003360 else {
3361 PyObject *temp = PyUnicode_Concat(v, s);
3362 Py_DECREF(s);
3363 Py_DECREF(v);
3364 v = temp;
3365 if (v == NULL)
3366 goto onError;
3367 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003369 }
3370 }
3371 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372
3373 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003374 Py_XDECREF(v);
3375 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376}