blob: ab166e816ab8306dcf60c4f530ab6460d1c3595b [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
Benjamin Petersond5efd202008-06-08 22:52:37 +0000129static int
130forbidden_check(struct compiling *c, const node *n, const char *x)
131{
132 if (!strcmp(x, "None"))
133 return ast_error(n, "assignment to None");
134 if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) &&
135 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
136 return 0;
137 return 1;
138}
139
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140/* num_stmts() returns number of contained statements.
141
142 Use this routine to determine how big a sequence is needed for
143 the statements in a parse tree. Its raison d'etre is this bit of
144 grammar:
145
146 stmt: simple_stmt | compound_stmt
147 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
148
149 A simple_stmt can contain multiple small_stmt elements joined
150 by semicolons. If the arg is a simple_stmt, the number of
151 small_stmt elements is returned.
152*/
153
154static int
155num_stmts(const node *n)
156{
157 int i, l;
158 node *ch;
159
160 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000161 case single_input:
162 if (TYPE(CHILD(n, 0)) == NEWLINE)
163 return 0;
164 else
165 return num_stmts(CHILD(n, 0));
166 case file_input:
167 l = 0;
168 for (i = 0; i < NCH(n); i++) {
169 ch = CHILD(n, i);
170 if (TYPE(ch) == stmt)
171 l += num_stmts(ch);
172 }
173 return l;
174 case stmt:
175 return num_stmts(CHILD(n, 0));
176 case compound_stmt:
177 return 1;
178 case simple_stmt:
179 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
180 case suite:
181 if (NCH(n) == 1)
182 return num_stmts(CHILD(n, 0));
183 else {
184 l = 0;
185 for (i = 2; i < (NCH(n) - 1); i++)
186 l += num_stmts(CHILD(n, i));
187 return l;
188 }
189 default: {
190 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000192 sprintf(buf, "Non-statement found: %d %d\n",
193 TYPE(n), NCH(n));
194 Py_FatalError(buf);
195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 }
197 assert(0);
198 return 0;
199}
200
201/* Transform the CST rooted at node * to the appropriate AST
202*/
203
204mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000205PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000206 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000208 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 asdl_seq *stmts = NULL;
210 stmt_ty s;
211 node *ch;
212 struct compiling c;
213
214 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000215 c.c_encoding = "utf-8";
216 if (TYPE(n) == encoding_decl) {
217 ast_error(n, "encoding declaration in Unicode string");
218 goto error;
219 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000221 c.c_encoding = STR(n);
222 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000224 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225 }
Christian Heimes3c608332008-03-26 22:01:37 +0000226 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000227 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000228 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
Jeremy Hyltona8293132006-02-28 17:58:27 +0000230 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000232 case file_input:
233 stmts = asdl_seq_new(num_stmts(n), arena);
234 if (!stmts)
235 return NULL;
236 for (i = 0; i < NCH(n) - 1; i++) {
237 ch = CHILD(n, i);
238 if (TYPE(ch) == NEWLINE)
239 continue;
240 REQ(ch, stmt);
241 num = num_stmts(ch);
242 if (num == 1) {
243 s = ast_for_stmt(&c, ch);
244 if (!s)
245 goto error;
246 asdl_seq_SET(stmts, k++, s);
247 }
248 else {
249 ch = CHILD(ch, 0);
250 REQ(ch, simple_stmt);
251 for (j = 0; j < num; j++) {
252 s = ast_for_stmt(&c, CHILD(ch, j * 2));
253 if (!s)
254 goto error;
255 asdl_seq_SET(stmts, k++, s);
256 }
257 }
258 }
259 return Module(stmts, arena);
260 case eval_input: {
261 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000263 /* XXX Why not gen_for here? */
264 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
265 if (!testlist_ast)
266 goto error;
267 return Expression(testlist_ast, arena);
268 }
269 case single_input:
270 if (TYPE(CHILD(n, 0)) == NEWLINE) {
271 stmts = asdl_seq_new(1, arena);
272 if (!stmts)
273 goto error;
274 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
275 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000276 if (!asdl_seq_GET(stmts, 0))
277 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000278 return Interactive(stmts, arena);
279 }
280 else {
281 n = CHILD(n, 0);
282 num = num_stmts(n);
283 stmts = asdl_seq_new(num, arena);
284 if (!stmts)
285 goto error;
286 if (num == 1) {
287 s = ast_for_stmt(&c, n);
288 if (!s)
289 goto error;
290 asdl_seq_SET(stmts, 0, s);
291 }
292 else {
293 /* Only a simple_stmt can contain multiple statements. */
294 REQ(n, simple_stmt);
295 for (i = 0; i < NCH(n); i += 2) {
296 if (TYPE(CHILD(n, i)) == NEWLINE)
297 break;
298 s = ast_for_stmt(&c, CHILD(n, i));
299 if (!s)
300 goto error;
301 asdl_seq_SET(stmts, i / 2, s);
302 }
303 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000305 return Interactive(stmts, arena);
306 }
307 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000308 PyErr_Format(PyExc_SystemError,
309 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000310 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311 }
312 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313 ast_error_finish(filename);
314 return NULL;
315}
316
317/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
318*/
319
320static operator_ty
321get_operator(const node *n)
322{
323 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000324 case VBAR:
325 return BitOr;
326 case CIRCUMFLEX:
327 return BitXor;
328 case AMPER:
329 return BitAnd;
330 case LEFTSHIFT:
331 return LShift;
332 case RIGHTSHIFT:
333 return RShift;
334 case PLUS:
335 return Add;
336 case MINUS:
337 return Sub;
338 case STAR:
339 return Mult;
340 case SLASH:
341 return Div;
342 case DOUBLESLASH:
343 return FloorDiv;
344 case PERCENT:
345 return Mod;
346 default:
347 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348 }
349}
350
Jeremy Hyltona8293132006-02-28 17:58:27 +0000351/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352
353 Only sets context for expr kinds that "can appear in assignment context"
354 (according to ../Parser/Python.asdl). For other expr kinds, it sets
355 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356*/
357
358static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000359set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360{
361 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000362 /* If a particular expression type can't be used for assign / delete,
363 set expr_name to its name and an error message will be generated.
364 */
365 const char* expr_name = NULL;
366
367 /* The ast defines augmented store and load contexts, but the
368 implementation here doesn't actually use them. The code may be
369 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000370 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000371 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000372 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000373 */
374 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375
376 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000377 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000378 if (ctx == Store && !forbidden_check(c, n,
379 PyBytes_AS_STRING(e->v.Attribute.attr)))
380 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000381 e->v.Attribute.ctx = ctx;
382 break;
383 case Subscript_kind:
384 e->v.Subscript.ctx = ctx;
385 break;
386 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000387 if (ctx == Store && !forbidden_check(c, n,
388 PyBytes_AS_STRING(e->v.Name.id)))
389 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000390 e->v.Name.ctx = ctx;
391 break;
392 case List_kind:
393 e->v.List.ctx = ctx;
394 s = e->v.List.elts;
395 break;
396 case Tuple_kind:
397 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
398 return ast_error(n, "can't assign to ()");
399 e->v.Tuple.ctx = ctx;
400 s = e->v.Tuple.elts;
401 break;
402 case Lambda_kind:
403 expr_name = "lambda";
404 break;
405 case Call_kind:
406 expr_name = "function call";
407 break;
408 case BoolOp_kind:
409 case BinOp_kind:
410 case UnaryOp_kind:
411 expr_name = "operator";
412 break;
413 case GeneratorExp_kind:
414 expr_name = "generator expression";
415 break;
416 case Yield_kind:
417 expr_name = "yield expression";
418 break;
419 case ListComp_kind:
420 expr_name = "list comprehension";
421 break;
422 case Dict_kind:
423 case Num_kind:
424 case Str_kind:
425 expr_name = "literal";
426 break;
427 case Compare_kind:
428 expr_name = "comparison";
429 break;
430 case Repr_kind:
431 expr_name = "repr";
432 break;
433 case IfExp_kind:
434 expr_name = "conditional expression";
435 break;
436 default:
437 PyErr_Format(PyExc_SystemError,
438 "unexpected expression in assignment %d (line %d)",
439 e->kind, e->lineno);
440 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000442 /* Check for error string set by switch */
443 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000444 char buf[300];
445 PyOS_snprintf(buf, sizeof(buf),
446 "can't %s %s",
447 ctx == Store ? "assign to" : "delete",
448 expr_name);
449 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000450 }
451
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000453 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454 */
455 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000456 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000458 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000459 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000460 return 0;
461 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462 }
463 return 1;
464}
465
466static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000467ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468{
469 REQ(n, augassign);
470 n = CHILD(n, 0);
471 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000472 case '+':
473 return Add;
474 case '-':
475 return Sub;
476 case '/':
477 if (STR(n)[1] == '/')
478 return FloorDiv;
479 else
480 return Div;
481 case '%':
482 return Mod;
483 case '<':
484 return LShift;
485 case '>':
486 return RShift;
487 case '&':
488 return BitAnd;
489 case '^':
490 return BitXor;
491 case '|':
492 return BitOr;
493 case '*':
494 if (STR(n)[1] == '*')
495 return Pow;
496 else
497 return Mult;
498 default:
499 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
500 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 }
502}
503
504static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000505ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506{
507 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000508 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 */
510 REQ(n, comp_op);
511 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000512 n = CHILD(n, 0);
513 switch (TYPE(n)) {
514 case LESS:
515 return Lt;
516 case GREATER:
517 return Gt;
518 case EQEQUAL: /* == */
519 return Eq;
520 case LESSEQUAL:
521 return LtE;
522 case GREATEREQUAL:
523 return GtE;
524 case NOTEQUAL:
525 return NotEq;
526 case NAME:
527 if (strcmp(STR(n), "in") == 0)
528 return In;
529 if (strcmp(STR(n), "is") == 0)
530 return Is;
531 default:
532 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
533 STR(n));
534 return (cmpop_ty)0;
535 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536 }
537 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000538 /* handle "not in" and "is not" */
539 switch (TYPE(CHILD(n, 0))) {
540 case NAME:
541 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
542 return NotIn;
543 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
544 return IsNot;
545 default:
546 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
547 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
548 return (cmpop_ty)0;
549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550 }
Neal Norwitz79792652005-11-14 04:25:03 +0000551 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000552 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000553 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554}
555
556static asdl_seq *
557seq_for_testlist(struct compiling *c, const node *n)
558{
559 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000560 asdl_seq *seq;
561 expr_ty expression;
562 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000563 assert(TYPE(n) == testlist ||
564 TYPE(n) == listmaker ||
565 TYPE(n) == testlist_gexp ||
566 TYPE(n) == testlist_safe ||
567 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000569 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000571 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572
573 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000574 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000576 expression = ast_for_expr(c, CHILD(n, i));
577 if (!expression)
578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000580 assert(i / 2 < seq->size);
581 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582 }
583 return seq;
584}
585
586static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000587compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588{
589 int i, len = (NCH(n) + 1) / 2;
590 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000591 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594
Neal Norwitz3a230172006-09-22 08:18:10 +0000595 /* fpdef: NAME | '(' fplist ')'
596 fplist: fpdef (',' fpdef)* [',']
597 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000600 const node *fpdef_node = CHILD(n, 2*i);
601 const node *child;
602 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000603set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000604 /* fpdef_node is either a NAME or an fplist */
605 child = CHILD(fpdef_node, 0);
606 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000607 if (!forbidden_check(c, n, STR(child)))
608 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000609 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
610 child->n_col_offset, c->c_arena);
611 }
612 else {
613 assert(TYPE(fpdef_node) == fpdef);
614 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
615 child = CHILD(fpdef_node, 1);
616 assert(TYPE(child) == fplist);
617 /* NCH == 1 means we have (x), we need to elide the extra parens */
618 if (NCH(child) == 1) {
619 fpdef_node = CHILD(child, 0);
620 assert(TYPE(fpdef_node) == fpdef);
621 goto set_name;
622 }
623 arg = compiler_complex_args(c, child);
624 }
625 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626 }
627
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000628 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000629 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 return result;
632}
633
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634
Jeremy Hyltona8293132006-02-28 17:58:27 +0000635/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636
637static arguments_ty
638ast_for_arguments(struct compiling *c, const node *n)
639{
640 /* parameters: '(' [varargslist] ')'
641 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000642 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000644 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 asdl_seq *args, *defaults;
646 identifier vararg = NULL, kwarg = NULL;
647 node *ch;
648
649 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000650 if (NCH(n) == 2) /* () as argument list */
651 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
652 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 }
654 REQ(n, varargslist);
655
656 /* first count the number of normal args & defaults */
657 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000658 ch = CHILD(n, i);
659 if (TYPE(ch) == fpdef)
660 n_args++;
661 if (TYPE(ch) == EQUAL)
662 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000664 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000666 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000667 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000669 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670
671 /* fpdef: NAME | '(' fplist ')'
672 fplist: fpdef (',' fpdef)* [',']
673 */
674 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000675 j = 0; /* index for defaults */
676 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000678 ch = CHILD(n, i);
679 switch (TYPE(ch)) {
680 case fpdef:
681 handle_fpdef:
682 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
683 anything other than EQUAL or a comma? */
684 /* XXX Should NCH(n) check be made a separate check? */
685 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
686 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
687 if (!expression)
688 goto error;
689 assert(defaults != NULL);
690 asdl_seq_SET(defaults, j++, expression);
691 i += 2;
692 found_default = 1;
693 }
694 else if (found_default) {
695 ast_error(n,
696 "non-default argument follows default argument");
697 goto error;
698 }
699 if (NCH(ch) == 3) {
700 ch = CHILD(ch, 1);
701 /* def foo((x)): is not complex, special case. */
702 if (NCH(ch) != 1) {
703 /* We have complex arguments, setup for unpacking. */
704 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000705 if (!asdl_seq_GET(args, k-1))
706 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000707 } else {
708 /* def foo((x)): setup for checking NAME below. */
709 /* Loop because there can be many parens and tuple
710 unpacking mixed in. */
711 ch = CHILD(ch, 0);
712 assert(TYPE(ch) == fpdef);
713 goto handle_fpdef;
714 }
715 }
716 if (TYPE(CHILD(ch, 0)) == NAME) {
717 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000718 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000719 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000720 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
721 Param, LINENO(ch), ch->n_col_offset,
722 c->c_arena);
723 if (!name)
724 goto error;
725 asdl_seq_SET(args, k++, name);
726
727 }
728 i += 2; /* the name and the comma */
729 break;
730 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000731 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000732 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000733 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
734 i += 3;
735 break;
736 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000737 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000738 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000739 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
740 i += 3;
741 break;
742 default:
743 PyErr_Format(PyExc_SystemError,
744 "unexpected node in varargslist: %d @ %d",
745 TYPE(ch), i);
746 goto error;
747 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 }
749
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000750 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751
752 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000753 Py_XDECREF(vararg);
754 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 return NULL;
756}
757
758static expr_ty
759ast_for_dotted_name(struct compiling *c, const node *n)
760{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000761 expr_ty e;
762 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000763 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 int i;
765
766 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000767
768 lineno = LINENO(n);
769 col_offset = n->n_col_offset;
770
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 id = NEW_IDENTIFIER(CHILD(n, 0));
772 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000773 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000774 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000776 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
778 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000779 id = NEW_IDENTIFIER(CHILD(n, i));
780 if (!id)
781 return NULL;
782 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
783 if (!e)
784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 }
786
787 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788}
789
790static expr_ty
791ast_for_decorator(struct compiling *c, const node *n)
792{
793 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
794 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000795 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796
797 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000798 REQ(CHILD(n, 0), AT);
799 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800
801 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
802 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000803 return NULL;
804
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000806 d = name_expr;
807 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 }
809 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000810 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
811 n->n_col_offset, c->c_arena);
812 if (!d)
813 return NULL;
814 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 }
816 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000817 d = ast_for_call(c, CHILD(n, 3), name_expr);
818 if (!d)
819 return NULL;
820 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 }
822
823 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824}
825
826static asdl_seq*
827ast_for_decorators(struct compiling *c, const node *n)
828{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000829 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000830 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 int i;
832
833 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000834 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000836 return NULL;
837
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000839 d = ast_for_decorator(c, CHILD(n, i));
840 if (!d)
841 return NULL;
842 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 }
844 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845}
846
847static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000848ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849{
Christian Heimes5224d282008-02-23 15:01:05 +0000850 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000851 identifier name;
852 arguments_ty args;
853 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000854 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855
856 REQ(n, funcdef);
857
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 name = NEW_IDENTIFIER(CHILD(n, name_i));
859 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000860 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000861 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 args = ast_for_arguments(c, CHILD(n, name_i + 1));
864 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 body = ast_for_suite(c, CHILD(n, name_i + 3));
867 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000868 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000870 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000871 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872}
873
Christian Heimes5224d282008-02-23 15:01:05 +0000874static stmt_ty
875ast_for_decorated(struct compiling *c, const node *n)
876{
877 /* decorated: decorators (classdef | funcdef) */
878 stmt_ty thing = NULL;
879 asdl_seq *decorator_seq = NULL;
880
881 REQ(n, decorated);
882
883 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
884 if (!decorator_seq)
885 return NULL;
886
887 assert(TYPE(CHILD(n, 1)) == funcdef ||
888 TYPE(CHILD(n, 1)) == classdef);
889
890 if (TYPE(CHILD(n, 1)) == funcdef) {
891 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
892 } else if (TYPE(CHILD(n, 1)) == classdef) {
893 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
894 }
895 /* we count the decorators in when talking about the class' or
896 function's line number */
897 if (thing) {
898 thing->lineno = LINENO(n);
899 thing->col_offset = n->n_col_offset;
900 }
901 return thing;
902}
903
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904static expr_ty
905ast_for_lambdef(struct compiling *c, const node *n)
906{
907 /* lambdef: 'lambda' [varargslist] ':' test */
908 arguments_ty args;
909 expr_ty expression;
910
911 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000912 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
913 if (!args)
914 return NULL;
915 expression = ast_for_expr(c, CHILD(n, 2));
916 if (!expression)
917 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918 }
919 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000920 args = ast_for_arguments(c, CHILD(n, 1));
921 if (!args)
922 return NULL;
923 expression = ast_for_expr(c, CHILD(n, 3));
924 if (!expression)
925 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 }
927
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000928 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929}
930
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000931static expr_ty
932ast_for_ifexpr(struct compiling *c, const node *n)
933{
934 /* test: or_test 'if' or_test 'else' test */
935 expr_ty expression, body, orelse;
936
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000937 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000938 body = ast_for_expr(c, CHILD(n, 0));
939 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000940 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000941 expression = ast_for_expr(c, CHILD(n, 2));
942 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000943 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000944 orelse = ast_for_expr(c, CHILD(n, 4));
945 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000946 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000947 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000948 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000949}
950
Neal Norwitze4d4f002006-09-05 03:58:26 +0000951/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
952 so there is only a single version. Possibly for loops can also re-use
953 the code.
954*/
955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956/* Count the number of 'for' loop in a list comprehension.
957
958 Helper for ast_for_listcomp().
959*/
960
961static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000962count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963{
964 int n_fors = 0;
965 node *ch = CHILD(n, 1);
966
967 count_list_for:
968 n_fors++;
969 REQ(ch, list_for);
970 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000971 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000973 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 count_list_iter:
975 REQ(ch, list_iter);
976 ch = CHILD(ch, 0);
977 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000978 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000980 if (NCH(ch) == 3) {
981 ch = CHILD(ch, 2);
982 goto count_list_iter;
983 }
984 else
985 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000987
988 /* Should never be reached */
989 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
990 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991}
992
993/* Count the number of 'if' statements in a list comprehension.
994
995 Helper for ast_for_listcomp().
996*/
997
998static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000999count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000{
1001 int n_ifs = 0;
1002
1003 count_list_iter:
1004 REQ(n, list_iter);
1005 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001006 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 n = CHILD(n, 0);
1008 REQ(n, list_if);
1009 n_ifs++;
1010 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001011 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 n = CHILD(n, 2);
1013 goto count_list_iter;
1014}
1015
1016static expr_ty
1017ast_for_listcomp(struct compiling *c, const node *n)
1018{
1019 /* listmaker: test ( list_for | (',' test)* [','] )
1020 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1021 list_iter: list_for | list_if
1022 list_if: 'if' test [list_iter]
1023 testlist_safe: test [(',' test)+ [',']]
1024 */
1025 expr_ty elt;
1026 asdl_seq *listcomps;
1027 int i, n_fors;
1028 node *ch;
1029
1030 REQ(n, listmaker);
1031 assert(NCH(n) > 1);
1032
1033 elt = ast_for_expr(c, CHILD(n, 0));
1034 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001037 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001039 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001041 listcomps = asdl_seq_new(n_fors, c->c_arena);
1042 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001043 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001044
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 ch = CHILD(n, 1);
1046 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001047 comprehension_ty lc;
1048 asdl_seq *t;
1049 expr_ty expression;
1050 node *for_ch;
1051
1052 REQ(ch, list_for);
1053
1054 for_ch = CHILD(ch, 1);
1055 t = ast_for_exprlist(c, for_ch, Store);
1056 if (!t)
1057 return NULL;
1058 expression = ast_for_testlist(c, CHILD(ch, 3));
1059 if (!expression)
1060 return NULL;
1061
1062 /* Check the # of children rather than the length of t, since
1063 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1064 */
1065 if (NCH(for_ch) == 1)
1066 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1067 c->c_arena);
1068 else
1069 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1070 c->c_arena),
1071 expression, NULL, c->c_arena);
1072 if (!lc)
1073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001075 if (NCH(ch) == 5) {
1076 int j, n_ifs;
1077 asdl_seq *ifs;
1078 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001080 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001081 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001082 if (n_ifs == -1)
1083 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001085 ifs = asdl_seq_new(n_ifs, c->c_arena);
1086 if (!ifs)
1087 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001089 for (j = 0; j < n_ifs; j++) {
1090 REQ(ch, list_iter);
1091 ch = CHILD(ch, 0);
1092 REQ(ch, list_if);
1093
1094 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1095 if (!list_for_expr)
1096 return NULL;
1097
1098 asdl_seq_SET(ifs, j, list_for_expr);
1099 if (NCH(ch) == 3)
1100 ch = CHILD(ch, 2);
1101 }
1102 /* on exit, must guarantee that ch is a list_for */
1103 if (TYPE(ch) == list_iter)
1104 ch = CHILD(ch, 0);
1105 lc->ifs = ifs;
1106 }
1107 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108 }
1109
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001110 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111}
1112
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001113/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114
1115 Helper for ast_for_genexp().
1116*/
1117
1118static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001119count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001121 int n_fors = 0;
1122 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123
1124 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001125 n_fors++;
1126 REQ(ch, gen_for);
1127 if (NCH(ch) == 5)
1128 ch = CHILD(ch, 4);
1129 else
1130 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001132 REQ(ch, gen_iter);
1133 ch = CHILD(ch, 0);
1134 if (TYPE(ch) == gen_for)
1135 goto count_gen_for;
1136 else if (TYPE(ch) == gen_if) {
1137 if (NCH(ch) == 3) {
1138 ch = CHILD(ch, 2);
1139 goto count_gen_iter;
1140 }
1141 else
1142 return n_fors;
1143 }
1144
1145 /* Should never be reached */
1146 PyErr_SetString(PyExc_SystemError,
1147 "logic error in count_gen_fors");
1148 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149}
1150
1151/* Count the number of 'if' statements in a generator expression.
1152
1153 Helper for ast_for_genexp().
1154*/
1155
1156static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001157count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001159 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001161 while (1) {
1162 REQ(n, gen_iter);
1163 if (TYPE(CHILD(n, 0)) == gen_for)
1164 return n_ifs;
1165 n = CHILD(n, 0);
1166 REQ(n, gen_if);
1167 n_ifs++;
1168 if (NCH(n) == 2)
1169 return n_ifs;
1170 n = CHILD(n, 2);
1171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172}
1173
Jeremy Hyltona8293132006-02-28 17:58:27 +00001174/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175static expr_ty
1176ast_for_genexp(struct compiling *c, const node *n)
1177{
1178 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001179 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 expr_ty elt;
1181 asdl_seq *genexps;
1182 int i, n_fors;
1183 node *ch;
1184
1185 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1186 assert(NCH(n) > 1);
1187
1188 elt = ast_for_expr(c, CHILD(n, 0));
1189 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001190 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001192 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001194 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001195
1196 genexps = asdl_seq_new(n_fors, c->c_arena);
1197 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001198 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 ch = CHILD(n, 1);
1201 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001202 comprehension_ty ge;
1203 asdl_seq *t;
1204 expr_ty expression;
1205 node *for_ch;
1206
1207 REQ(ch, gen_for);
1208
1209 for_ch = CHILD(ch, 1);
1210 t = ast_for_exprlist(c, for_ch, Store);
1211 if (!t)
1212 return NULL;
1213 expression = ast_for_expr(c, CHILD(ch, 3));
1214 if (!expression)
1215 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001216
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001217 /* Check the # of children rather than the length of t, since
1218 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1219 if (NCH(for_ch) == 1)
1220 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1221 NULL, c->c_arena);
1222 else
1223 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1224 c->c_arena),
1225 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001226
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001227 if (!ge)
1228 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001229
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001230 if (NCH(ch) == 5) {
1231 int j, n_ifs;
1232 asdl_seq *ifs;
1233
1234 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001235 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001236 if (n_ifs == -1)
1237 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001238
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001239 ifs = asdl_seq_new(n_ifs, c->c_arena);
1240 if (!ifs)
1241 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001243 for (j = 0; j < n_ifs; j++) {
1244 REQ(ch, gen_iter);
1245 ch = CHILD(ch, 0);
1246 REQ(ch, gen_if);
1247
1248 expression = ast_for_expr(c, CHILD(ch, 1));
1249 if (!expression)
1250 return NULL;
1251 asdl_seq_SET(ifs, j, expression);
1252 if (NCH(ch) == 3)
1253 ch = CHILD(ch, 2);
1254 }
1255 /* on exit, must guarantee that ch is a gen_for */
1256 if (TYPE(ch) == gen_iter)
1257 ch = CHILD(ch, 0);
1258 ge->ifs = ifs;
1259 }
1260 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 }
1262
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001263 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264}
1265
1266static expr_ty
1267ast_for_atom(struct compiling *c, const node *n)
1268{
1269 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1270 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1271 */
1272 node *ch = CHILD(n, 0);
1273
1274 switch (TYPE(ch)) {
1275 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001276 /* All names start in Load context, but may later be
1277 changed. */
1278 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1279 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001281 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001282 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001283#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001284 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1285 PyObject *type, *value, *tback, *errstr;
1286 PyErr_Fetch(&type, &value, &tback);
1287 errstr = ((PyUnicodeErrorObject *)value)->reason;
1288 if (errstr) {
1289 char *s = "";
1290 char buf[128];
Christian Heimes593daf52008-05-26 12:51:38 +00001291 s = PyBytes_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001292 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1293 ast_error(n, buf);
1294 } else {
1295 ast_error(n, "(unicode error) unknown error");
1296 }
1297 Py_DECREF(type);
1298 Py_DECREF(value);
1299 Py_XDECREF(tback);
1300 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001301#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001302 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001303 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001304 PyArena_AddPyObject(c->c_arena, str);
1305 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 }
1307 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001308 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001309 if (!pynum)
1310 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001311
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001312 PyArena_AddPyObject(c->c_arena, pynum);
1313 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 }
1315 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001316 ch = CHILD(n, 1);
1317
1318 if (TYPE(ch) == RPAR)
1319 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1320
1321 if (TYPE(ch) == yield_expr)
1322 return ast_for_expr(c, ch);
1323
1324 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1325 return ast_for_genexp(c, ch);
1326
1327 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001329 ch = CHILD(n, 1);
1330
1331 if (TYPE(ch) == RSQB)
1332 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1333
1334 REQ(ch, listmaker);
1335 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1336 asdl_seq *elts = seq_for_testlist(c, ch);
1337 if (!elts)
1338 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001339
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001340 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1341 }
1342 else
1343 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001345 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1346 int i, size;
1347 asdl_seq *keys, *values;
1348
1349 ch = CHILD(n, 1);
1350 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1351 keys = asdl_seq_new(size, c->c_arena);
1352 if (!keys)
1353 return NULL;
1354
1355 values = asdl_seq_new(size, c->c_arena);
1356 if (!values)
1357 return NULL;
1358
1359 for (i = 0; i < NCH(ch); i += 4) {
1360 expr_ty expression;
1361
1362 expression = ast_for_expr(c, CHILD(ch, i));
1363 if (!expression)
1364 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001365
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001366 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001367
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001368 expression = ast_for_expr(c, CHILD(ch, i + 2));
1369 if (!expression)
1370 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001371
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001372 asdl_seq_SET(values, i / 4, expression);
1373 }
1374 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 }
1376 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001377 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001378 if (Py_Py3kWarningFlag &&
1379 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001380 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001381 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001382 if (!expression)
1383 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001384
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001385 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 }
1387 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001388 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1389 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 }
1391}
1392
1393static slice_ty
1394ast_for_slice(struct compiling *c, const node *n)
1395{
1396 node *ch;
1397 expr_ty lower = NULL, upper = NULL, step = NULL;
1398
1399 REQ(n, subscript);
1400
1401 /*
1402 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1403 sliceop: ':' [test]
1404 */
1405 ch = CHILD(n, 0);
1406 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001407 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408
1409 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001410 /* 'step' variable hold no significance in terms of being used over
1411 other vars */
1412 step = ast_for_expr(c, ch);
1413 if (!step)
1414 return NULL;
1415
1416 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 }
1418
1419 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001420 lower = ast_for_expr(c, ch);
1421 if (!lower)
1422 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 }
1424
1425 /* If there's an upper bound it's in the second or third position. */
1426 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001427 if (NCH(n) > 1) {
1428 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001430 if (TYPE(n2) == test) {
1431 upper = ast_for_expr(c, n2);
1432 if (!upper)
1433 return NULL;
1434 }
1435 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001437 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001439 if (TYPE(n2) == test) {
1440 upper = ast_for_expr(c, n2);
1441 if (!upper)
1442 return NULL;
1443 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 }
1445
1446 ch = CHILD(n, NCH(n) - 1);
1447 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001448 if (NCH(ch) == 1) {
1449 /* No expression, so step is None */
1450 ch = CHILD(ch, 0);
1451 step = Name(new_identifier("None", c->c_arena), Load,
1452 LINENO(ch), ch->n_col_offset, c->c_arena);
1453 if (!step)
1454 return NULL;
1455 } else {
1456 ch = CHILD(ch, 1);
1457 if (TYPE(ch) == test) {
1458 step = ast_for_expr(c, ch);
1459 if (!step)
1460 return NULL;
1461 }
1462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 }
1464
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001465 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466}
1467
1468static expr_ty
1469ast_for_binop(struct compiling *c, const node *n)
1470{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001471 /* Must account for a sequence of expressions.
1472 How should A op B op C by represented?
1473 BinOp(BinOp(A, op, B), op, C).
1474 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001476 int i, nops;
1477 expr_ty expr1, expr2, result;
1478 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001480 expr1 = ast_for_expr(c, CHILD(n, 0));
1481 if (!expr1)
1482 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001484 expr2 = ast_for_expr(c, CHILD(n, 2));
1485 if (!expr2)
1486 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001488 newoperator = get_operator(CHILD(n, 1));
1489 if (!newoperator)
1490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001492 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1493 c->c_arena);
1494 if (!result)
1495 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001497 nops = (NCH(n) - 1) / 2;
1498 for (i = 1; i < nops; i++) {
1499 expr_ty tmp_result, tmp;
1500 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001502 newoperator = get_operator(next_oper);
1503 if (!newoperator)
1504 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001506 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1507 if (!tmp)
1508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001510 tmp_result = BinOp(result, newoperator, tmp,
1511 LINENO(next_oper), next_oper->n_col_offset,
1512 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001513 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001514 return NULL;
1515 result = tmp_result;
1516 }
1517 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518}
1519
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001520static expr_ty
1521ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1522{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001523 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1524 subscriptlist: subscript (',' subscript)* [',']
1525 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1526 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001527 REQ(n, trailer);
1528 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001529 if (NCH(n) == 2)
1530 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1531 n->n_col_offset, c->c_arena);
1532 else
1533 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001534 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001535 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001536 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1537 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001538 }
1539 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001540 REQ(CHILD(n, 0), LSQB);
1541 REQ(CHILD(n, 2), RSQB);
1542 n = CHILD(n, 1);
1543 if (NCH(n) == 1) {
1544 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1545 if (!slc)
1546 return NULL;
1547 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1548 c->c_arena);
1549 }
1550 else {
1551 /* The grammar is ambiguous here. The ambiguity is resolved
1552 by treating the sequence as a tuple literal if there are
1553 no slice features.
1554 */
1555 int j;
1556 slice_ty slc;
1557 expr_ty e;
1558 bool simple = true;
1559 asdl_seq *slices, *elts;
1560 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1561 if (!slices)
1562 return NULL;
1563 for (j = 0; j < NCH(n); j += 2) {
1564 slc = ast_for_slice(c, CHILD(n, j));
1565 if (!slc)
1566 return NULL;
1567 if (slc->kind != Index_kind)
1568 simple = false;
1569 asdl_seq_SET(slices, j / 2, slc);
1570 }
1571 if (!simple) {
1572 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1573 Load, LINENO(n), n->n_col_offset, c->c_arena);
1574 }
1575 /* extract Index values and put them in a Tuple */
1576 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1577 if (!elts)
1578 return NULL;
1579 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1580 slc = (slice_ty)asdl_seq_GET(slices, j);
1581 assert(slc->kind == Index_kind && slc->v.Index.value);
1582 asdl_seq_SET(elts, j, slc->v.Index.value);
1583 }
1584 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1585 if (!e)
1586 return NULL;
1587 return Subscript(left_expr, Index(e, c->c_arena),
1588 Load, LINENO(n), n->n_col_offset, c->c_arena);
1589 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001590 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001591}
1592
1593static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001594ast_for_factor(struct compiling *c, const node *n)
1595{
1596 node *pfactor, *ppower, *patom, *pnum;
1597 expr_ty expression;
1598
1599 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001600 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001601 constant. The peephole optimizer already does something like
1602 this but it doesn't handle the case where the constant is
1603 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1604 PyLongObject.
1605 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001606 if (TYPE(CHILD(n, 0)) == MINUS &&
1607 NCH(n) == 2 &&
1608 TYPE((pfactor = CHILD(n, 1))) == factor &&
1609 NCH(pfactor) == 1 &&
1610 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1611 NCH(ppower) == 1 &&
1612 TYPE((patom = CHILD(ppower, 0))) == atom &&
1613 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1614 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1615 if (s == NULL)
1616 return NULL;
1617 s[0] = '-';
1618 strcpy(s + 1, STR(pnum));
1619 PyObject_FREE(STR(pnum));
1620 STR(pnum) = s;
1621 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001622 }
1623
1624 expression = ast_for_expr(c, CHILD(n, 1));
1625 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001626 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001627
1628 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001629 case PLUS:
1630 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1631 c->c_arena);
1632 case MINUS:
1633 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1634 c->c_arena);
1635 case TILDE:
1636 return UnaryOp(Invert, expression, LINENO(n),
1637 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001638 }
1639 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001640 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001641 return NULL;
1642}
1643
1644static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001645ast_for_power(struct compiling *c, const node *n)
1646{
1647 /* power: atom trailer* ('**' factor)*
1648 */
1649 int i;
1650 expr_ty e, tmp;
1651 REQ(n, power);
1652 e = ast_for_atom(c, CHILD(n, 0));
1653 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001654 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001655 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001656 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001657 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001658 node *ch = CHILD(n, i);
1659 if (TYPE(ch) != trailer)
1660 break;
1661 tmp = ast_for_trailer(c, ch, e);
1662 if (!tmp)
1663 return NULL;
1664 tmp->lineno = e->lineno;
1665 tmp->col_offset = e->col_offset;
1666 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001667 }
1668 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001669 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1670 if (!f)
1671 return NULL;
1672 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1673 if (!tmp)
1674 return NULL;
1675 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001676 }
1677 return e;
1678}
1679
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680/* Do not name a variable 'expr'! Will cause a compile error.
1681*/
1682
1683static expr_ty
1684ast_for_expr(struct compiling *c, const node *n)
1685{
1686 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001687 test: or_test ['if' or_test 'else' test] | lambdef
1688 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 and_test: not_test ('and' not_test)*
1690 not_test: 'not' not_test | comparison
1691 comparison: expr (comp_op expr)*
1692 expr: xor_expr ('|' xor_expr)*
1693 xor_expr: and_expr ('^' and_expr)*
1694 and_expr: shift_expr ('&' shift_expr)*
1695 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1696 arith_expr: term (('+'|'-') term)*
1697 term: factor (('*'|'/'|'%'|'//') factor)*
1698 factor: ('+'|'-'|'~') factor | power
1699 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001700
1701 As well as modified versions that exist for backward compatibility,
1702 to explicitly allow:
1703 [ x for x in lambda: 0, lambda: 1 ]
1704 (which would be ambiguous without these extra rules)
1705
1706 old_test: or_test | old_lambdef
1707 old_lambdef: 'lambda' [vararglist] ':' old_test
1708
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 */
1710
1711 asdl_seq *seq;
1712 int i;
1713
1714 loop:
1715 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001716 case test:
1717 case old_test:
1718 if (TYPE(CHILD(n, 0)) == lambdef ||
1719 TYPE(CHILD(n, 0)) == old_lambdef)
1720 return ast_for_lambdef(c, CHILD(n, 0));
1721 else if (NCH(n) > 1)
1722 return ast_for_ifexpr(c, n);
1723 /* Fallthrough */
1724 case or_test:
1725 case and_test:
1726 if (NCH(n) == 1) {
1727 n = CHILD(n, 0);
1728 goto loop;
1729 }
1730 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1731 if (!seq)
1732 return NULL;
1733 for (i = 0; i < NCH(n); i += 2) {
1734 expr_ty e = ast_for_expr(c, CHILD(n, i));
1735 if (!e)
1736 return NULL;
1737 asdl_seq_SET(seq, i / 2, e);
1738 }
1739 if (!strcmp(STR(CHILD(n, 1)), "and"))
1740 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1741 c->c_arena);
1742 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1743 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1744 case not_test:
1745 if (NCH(n) == 1) {
1746 n = CHILD(n, 0);
1747 goto loop;
1748 }
1749 else {
1750 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1751 if (!expression)
1752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001754 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1755 c->c_arena);
1756 }
1757 case comparison:
1758 if (NCH(n) == 1) {
1759 n = CHILD(n, 0);
1760 goto loop;
1761 }
1762 else {
1763 expr_ty expression;
1764 asdl_int_seq *ops;
1765 asdl_seq *cmps;
1766 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1767 if (!ops)
1768 return NULL;
1769 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1770 if (!cmps) {
1771 return NULL;
1772 }
1773 for (i = 1; i < NCH(n); i += 2) {
1774 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001776 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001777 if (!newoperator) {
1778 return NULL;
1779 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001781 expression = ast_for_expr(c, CHILD(n, i + 1));
1782 if (!expression) {
1783 return NULL;
1784 }
1785
1786 asdl_seq_SET(ops, i / 2, newoperator);
1787 asdl_seq_SET(cmps, i / 2, expression);
1788 }
1789 expression = ast_for_expr(c, CHILD(n, 0));
1790 if (!expression) {
1791 return NULL;
1792 }
1793
1794 return Compare(expression, ops, cmps, LINENO(n),
1795 n->n_col_offset, c->c_arena);
1796 }
1797 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001799 /* The next five cases all handle BinOps. The main body of code
1800 is the same in each case, but the switch turned inside out to
1801 reuse the code for each type of operator.
1802 */
1803 case expr:
1804 case xor_expr:
1805 case and_expr:
1806 case shift_expr:
1807 case arith_expr:
1808 case term:
1809 if (NCH(n) == 1) {
1810 n = CHILD(n, 0);
1811 goto loop;
1812 }
1813 return ast_for_binop(c, n);
1814 case yield_expr: {
1815 expr_ty exp = NULL;
1816 if (NCH(n) == 2) {
1817 exp = ast_for_testlist(c, CHILD(n, 1));
1818 if (!exp)
1819 return NULL;
1820 }
1821 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1822 }
1823 case factor:
1824 if (NCH(n) == 1) {
1825 n = CHILD(n, 0);
1826 goto loop;
1827 }
1828 return ast_for_factor(c, n);
1829 case power:
1830 return ast_for_power(c, n);
1831 default:
1832 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001835 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 return NULL;
1837}
1838
1839static expr_ty
1840ast_for_call(struct compiling *c, const node *n, expr_ty func)
1841{
1842 /*
1843 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001844 | '**' test)
1845 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 */
1847
1848 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001849 asdl_seq *args;
1850 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 expr_ty vararg = NULL, kwarg = NULL;
1852
1853 REQ(n, arglist);
1854
1855 nargs = 0;
1856 nkeywords = 0;
1857 ngens = 0;
1858 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001859 node *ch = CHILD(n, i);
1860 if (TYPE(ch) == argument) {
1861 if (NCH(ch) == 1)
1862 nargs++;
1863 else if (TYPE(CHILD(ch, 1)) == gen_for)
1864 ngens++;
1865 else
1866 nkeywords++;
1867 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 }
1869 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001870 ast_error(n, "Generator expression must be parenthesized "
1871 "if not sole argument");
1872 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 }
1874
1875 if (nargs + nkeywords + ngens > 255) {
1876 ast_error(n, "more than 255 arguments");
1877 return NULL;
1878 }
1879
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001880 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001882 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001883 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 nargs = 0;
1887 nkeywords = 0;
1888 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001889 node *ch = CHILD(n, i);
1890 if (TYPE(ch) == argument) {
1891 expr_ty e;
1892 if (NCH(ch) == 1) {
1893 if (nkeywords) {
1894 ast_error(CHILD(ch, 0),
1895 "non-keyword arg after keyword arg");
1896 return NULL;
1897 }
1898 e = ast_for_expr(c, CHILD(ch, 0));
1899 if (!e)
1900 return NULL;
1901 asdl_seq_SET(args, nargs++, e);
1902 }
1903 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1904 e = ast_for_genexp(c, ch);
1905 if (!e)
1906 return NULL;
1907 asdl_seq_SET(args, nargs++, e);
1908 }
1909 else {
1910 keyword_ty kw;
1911 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001913 /* CHILD(ch, 0) is test, but must be an identifier? */
1914 e = ast_for_expr(c, CHILD(ch, 0));
1915 if (!e)
1916 return NULL;
1917 /* f(lambda x: x[0] = 3) ends up getting parsed with
1918 * LHS test = lambda x: x[0], and RHS test = 3.
1919 * SF bug 132313 points out that complaining about a keyword
1920 * then is very confusing.
1921 */
1922 if (e->kind == Lambda_kind) {
1923 ast_error(CHILD(ch, 0),
1924 "lambda cannot contain assignment");
1925 return NULL;
1926 } else if (e->kind != Name_kind) {
1927 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1928 return NULL;
1929 }
1930 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001931 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001932 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001933 e = ast_for_expr(c, CHILD(ch, 2));
1934 if (!e)
1935 return NULL;
1936 kw = keyword(key, e, c->c_arena);
1937 if (!kw)
1938 return NULL;
1939 asdl_seq_SET(keywords, nkeywords++, kw);
1940 }
1941 }
1942 else if (TYPE(ch) == STAR) {
1943 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001944 if (!vararg)
1945 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001946 i++;
1947 }
1948 else if (TYPE(ch) == DOUBLESTAR) {
1949 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001950 if (!kwarg)
1951 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001952 i++;
1953 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 }
1955
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001956 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1957 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958}
1959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001961ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001963 /* testlist_gexp: test (',' test)* [','] */
1964 /* testlist: test (',' test)* [','] */
1965 /* testlist_safe: test (',' test)+ [','] */
1966 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001968 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001969 if (NCH(n) > 1)
1970 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001971 }
1972 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001973 assert(TYPE(n) == testlist ||
1974 TYPE(n) == testlist_safe ||
1975 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001976 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001978 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001980 asdl_seq *tmp = seq_for_testlist(c, n);
1981 if (!tmp)
1982 return NULL;
1983 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001985}
1986
1987static expr_ty
1988ast_for_testlist_gexp(struct compiling *c, const node* n)
1989{
1990 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1991 /* argument: test [ gen_for ] */
1992 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001993 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001994 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001995 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001996}
1997
1998/* like ast_for_testlist() but returns a sequence */
1999static asdl_seq*
2000ast_for_class_bases(struct compiling *c, const node* n)
2001{
2002 /* testlist: test (',' test)* [','] */
2003 assert(NCH(n) > 0);
2004 REQ(n, testlist);
2005 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002006 expr_ty base;
2007 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2008 if (!bases)
2009 return NULL;
2010 base = ast_for_expr(c, CHILD(n, 0));
2011 if (!base)
2012 return NULL;
2013 asdl_seq_SET(bases, 0, base);
2014 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002015 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002016
2017 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018}
2019
2020static stmt_ty
2021ast_for_expr_stmt(struct compiling *c, const node *n)
2022{
2023 REQ(n, expr_stmt);
2024 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002025 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026 testlist: test (',' test)* [',']
2027 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002028 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 test: ... here starts the operator precendence dance
2030 */
2031
2032 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002033 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2034 if (!e)
2035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002037 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 }
2039 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002040 expr_ty expr1, expr2;
2041 operator_ty newoperator;
2042 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002044 expr1 = ast_for_testlist(c, ch);
2045 if (!expr1)
2046 return NULL;
2047 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2048 switch (expr1->kind) {
2049 case GeneratorExp_kind:
2050 ast_error(ch, "augmented assignment to generator "
2051 "expression not possible");
2052 return NULL;
2053 case Yield_kind:
2054 ast_error(ch, "augmented assignment to yield "
2055 "expression not possible");
2056 return NULL;
2057 case Name_kind: {
Christian Heimes593daf52008-05-26 12:51:38 +00002058 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
Benjamin Petersond5efd202008-06-08 22:52:37 +00002059 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
2060 !forbidden_check(c, ch, var_name))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002061 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002062 break;
2063 }
2064 case Attribute_kind:
2065 case Subscript_kind:
2066 break;
2067 default:
2068 ast_error(ch, "illegal expression for augmented "
2069 "assignment");
2070 return NULL;
2071 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002072 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002075 ch = CHILD(n, 2);
2076 if (TYPE(ch) == testlist)
2077 expr2 = ast_for_testlist(c, ch);
2078 else
2079 expr2 = ast_for_expr(c, ch);
2080 if (!expr2)
2081 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002083 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002084 if (!newoperator)
2085 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002087 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2088 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 }
2090 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002091 int i;
2092 asdl_seq *targets;
2093 node *value;
2094 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002096 /* a normal assignment */
2097 REQ(CHILD(n, 1), EQUAL);
2098 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2099 if (!targets)
2100 return NULL;
2101 for (i = 0; i < NCH(n) - 2; i += 2) {
2102 expr_ty e;
2103 node *ch = CHILD(n, i);
2104 if (TYPE(ch) == yield_expr) {
2105 ast_error(ch, "assignment to yield expression not possible");
2106 return NULL;
2107 }
2108 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002110 /* set context to assign */
2111 if (!e)
2112 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002114 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002115 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002117 asdl_seq_SET(targets, i / 2, e);
2118 }
2119 value = CHILD(n, NCH(n) - 1);
2120 if (TYPE(value) == testlist)
2121 expression = ast_for_testlist(c, value);
2122 else
2123 expression = ast_for_expr(c, value);
2124 if (!expression)
2125 return NULL;
2126 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2127 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129}
2130
2131static stmt_ty
2132ast_for_print_stmt(struct compiling *c, const node *n)
2133{
2134 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002135 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 */
2137 expr_ty dest = NULL, expression;
2138 asdl_seq *seq;
2139 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002140 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141
2142 REQ(n, print_stmt);
2143 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002144 dest = ast_for_expr(c, CHILD(n, 2));
2145 if (!dest)
2146 return NULL;
2147 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002149 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002151 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002152 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002153 expression = ast_for_expr(c, CHILD(n, i));
2154 if (!expression)
2155 return NULL;
2156 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 }
2158 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002159 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160}
2161
2162static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002163ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164{
2165 asdl_seq *seq;
2166 int i;
2167 expr_ty e;
2168
2169 REQ(n, exprlist);
2170
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002171 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002173 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002175 e = ast_for_expr(c, CHILD(n, i));
2176 if (!e)
2177 return NULL;
2178 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002179 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002180 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 }
2182 return seq;
2183}
2184
2185static stmt_ty
2186ast_for_del_stmt(struct compiling *c, const node *n)
2187{
2188 asdl_seq *expr_list;
2189
2190 /* del_stmt: 'del' exprlist */
2191 REQ(n, del_stmt);
2192
2193 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2194 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002195 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002196 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197}
2198
2199static stmt_ty
2200ast_for_flow_stmt(struct compiling *c, const node *n)
2201{
2202 /*
2203 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002204 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205 break_stmt: 'break'
2206 continue_stmt: 'continue'
2207 return_stmt: 'return' [testlist]
2208 yield_stmt: yield_expr
2209 yield_expr: 'yield' testlist
2210 raise_stmt: 'raise' [test [',' test [',' test]]]
2211 */
2212 node *ch;
2213
2214 REQ(n, flow_stmt);
2215 ch = CHILD(n, 0);
2216 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002217 case break_stmt:
2218 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2219 case continue_stmt:
2220 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2221 case yield_stmt: { /* will reduce to yield_expr */
2222 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2223 if (!exp)
2224 return NULL;
2225 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2226 }
2227 case return_stmt:
2228 if (NCH(ch) == 1)
2229 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2230 else {
2231 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2232 if (!expression)
2233 return NULL;
2234 return Return(expression, LINENO(n), n->n_col_offset,
2235 c->c_arena);
2236 }
2237 case raise_stmt:
2238 if (NCH(ch) == 1)
2239 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2240 c->c_arena);
2241 else if (NCH(ch) == 2) {
2242 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2243 if (!expression)
2244 return NULL;
2245 return Raise(expression, NULL, NULL, LINENO(n),
2246 n->n_col_offset, c->c_arena);
2247 }
2248 else if (NCH(ch) == 4) {
2249 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002251 expr1 = ast_for_expr(c, CHILD(ch, 1));
2252 if (!expr1)
2253 return NULL;
2254 expr2 = ast_for_expr(c, CHILD(ch, 3));
2255 if (!expr2)
2256 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002258 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2259 c->c_arena);
2260 }
2261 else if (NCH(ch) == 6) {
2262 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002264 expr1 = ast_for_expr(c, CHILD(ch, 1));
2265 if (!expr1)
2266 return NULL;
2267 expr2 = ast_for_expr(c, CHILD(ch, 3));
2268 if (!expr2)
2269 return NULL;
2270 expr3 = ast_for_expr(c, CHILD(ch, 5));
2271 if (!expr3)
2272 return NULL;
2273
2274 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2275 c->c_arena);
2276 }
2277 default:
2278 PyErr_Format(PyExc_SystemError,
2279 "unexpected flow_stmt: %d", TYPE(ch));
2280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002282
2283 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2284 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285}
2286
2287static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002288alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289{
2290 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002291 import_as_name: NAME ['as' NAME]
2292 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 dotted_name: NAME ('.' NAME)*
2294 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002295 PyObject *str;
2296
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 loop:
2298 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002299 case import_as_name:
2300 str = NULL;
2301 if (NCH(n) == 3) {
2302 str = NEW_IDENTIFIER(CHILD(n, 2));
2303 }
2304 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2305 case dotted_as_name:
2306 if (NCH(n) == 1) {
2307 n = CHILD(n, 0);
2308 goto loop;
2309 }
2310 else {
2311 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2312 if (!a)
2313 return NULL;
2314 assert(!a->asname);
2315 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2316 return a;
2317 }
2318 break;
2319 case dotted_name:
2320 if (NCH(n) == 1)
2321 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2322 else {
2323 /* Create a string of the form "a.b.c" */
2324 int i;
2325 size_t len;
2326 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002328 len = 0;
2329 for (i = 0; i < NCH(n); i += 2)
2330 /* length of string plus one for the dot */
2331 len += strlen(STR(CHILD(n, i))) + 1;
2332 len--; /* the last name doesn't have a dot */
Christian Heimes593daf52008-05-26 12:51:38 +00002333 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002334 if (!str)
2335 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +00002336 s = PyBytes_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002337 if (!s)
2338 return NULL;
2339 for (i = 0; i < NCH(n); i += 2) {
2340 char *sch = STR(CHILD(n, i));
2341 strcpy(s, STR(CHILD(n, i)));
2342 s += strlen(sch);
2343 *s++ = '.';
2344 }
2345 --s;
2346 *s = '\0';
Christian Heimes593daf52008-05-26 12:51:38 +00002347 PyBytes_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002348 PyArena_AddPyObject(c->c_arena, str);
2349 return alias(str, NULL, c->c_arena);
2350 }
2351 break;
2352 case STAR:
Christian Heimes593daf52008-05-26 12:51:38 +00002353 str = PyBytes_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002354 PyArena_AddPyObject(c->c_arena, str);
2355 return alias(str, NULL, c->c_arena);
2356 default:
2357 PyErr_Format(PyExc_SystemError,
2358 "unexpected import name: %d", TYPE(n));
2359 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002361
2362 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002363 return NULL;
2364}
2365
2366static stmt_ty
2367ast_for_import_stmt(struct compiling *c, const node *n)
2368{
2369 /*
2370 import_stmt: import_name | import_from
2371 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002372 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002373 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002375 int lineno;
2376 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 int i;
2378 asdl_seq *aliases;
2379
2380 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002381 lineno = LINENO(n);
2382 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002384 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002385 n = CHILD(n, 1);
2386 REQ(n, dotted_as_names);
2387 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2388 if (!aliases)
2389 return NULL;
2390 for (i = 0; i < NCH(n); i += 2) {
2391 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2392 if (!import_alias)
2393 return NULL;
2394 asdl_seq_SET(aliases, i / 2, import_alias);
2395 }
2396 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002398 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002399 int n_children;
2400 int idx, ndots = 0;
2401 alias_ty mod = NULL;
2402 identifier modname;
2403
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002404 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002405 optional module name */
2406 for (idx = 1; idx < NCH(n); idx++) {
2407 if (TYPE(CHILD(n, idx)) == dotted_name) {
2408 mod = alias_for_import_name(c, CHILD(n, idx));
2409 idx++;
2410 break;
2411 } else if (TYPE(CHILD(n, idx)) != DOT) {
2412 break;
2413 }
2414 ndots++;
2415 }
2416 idx++; /* skip over the 'import' keyword */
2417 switch (TYPE(CHILD(n, idx))) {
2418 case STAR:
2419 /* from ... import * */
2420 n = CHILD(n, idx);
2421 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002422 break;
2423 case LPAR:
2424 /* from ... import (x, y, z) */
2425 n = CHILD(n, idx + 1);
2426 n_children = NCH(n);
2427 break;
2428 case import_as_names:
2429 /* from ... import x, y, z */
2430 n = CHILD(n, idx);
2431 n_children = NCH(n);
2432 if (n_children % 2 == 0) {
2433 ast_error(n, "trailing comma not allowed without"
2434 " surrounding parentheses");
2435 return NULL;
2436 }
2437 break;
2438 default:
2439 ast_error(n, "Unexpected node-type in from-import");
2440 return NULL;
2441 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002443 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2444 if (!aliases)
2445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002447 /* handle "from ... import *" special b/c there's no children */
2448 if (TYPE(n) == STAR) {
2449 alias_ty import_alias = alias_for_import_name(c, n);
2450 if (!import_alias)
2451 return NULL;
2452 asdl_seq_SET(aliases, 0, import_alias);
2453 }
2454 else {
2455 for (i = 0; i < NCH(n); i += 2) {
2456 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2457 if (!import_alias)
2458 return NULL;
2459 asdl_seq_SET(aliases, i / 2, import_alias);
2460 }
2461 }
2462 if (mod != NULL)
2463 modname = mod->name;
2464 else
2465 modname = new_identifier("", c->c_arena);
2466 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2467 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 }
Neal Norwitz79792652005-11-14 04:25:03 +00002469 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002470 "unknown import statement: starts with command '%s'",
2471 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 return NULL;
2473}
2474
2475static stmt_ty
2476ast_for_global_stmt(struct compiling *c, const node *n)
2477{
2478 /* global_stmt: 'global' NAME (',' NAME)* */
2479 identifier name;
2480 asdl_seq *s;
2481 int i;
2482
2483 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002484 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002486 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002488 name = NEW_IDENTIFIER(CHILD(n, i));
2489 if (!name)
2490 return NULL;
2491 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002493 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494}
2495
2496static stmt_ty
2497ast_for_exec_stmt(struct compiling *c, const node *n)
2498{
2499 expr_ty expr1, globals = NULL, locals = NULL;
2500 int n_children = NCH(n);
2501 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002502 PyErr_Format(PyExc_SystemError,
2503 "poorly formed 'exec' statement: %d parts to statement",
2504 n_children);
2505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 }
2507
2508 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2509 REQ(n, exec_stmt);
2510 expr1 = ast_for_expr(c, CHILD(n, 1));
2511 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002514 globals = ast_for_expr(c, CHILD(n, 3));
2515 if (!globals)
2516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 }
2518 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002519 locals = ast_for_expr(c, CHILD(n, 5));
2520 if (!locals)
2521 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 }
2523
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002524 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2525 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526}
2527
2528static stmt_ty
2529ast_for_assert_stmt(struct compiling *c, const node *n)
2530{
2531 /* assert_stmt: 'assert' test [',' test] */
2532 REQ(n, assert_stmt);
2533 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002534 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2535 if (!expression)
2536 return NULL;
2537 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2538 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 }
2540 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002541 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002543 expr1 = ast_for_expr(c, CHILD(n, 1));
2544 if (!expr1)
2545 return NULL;
2546 expr2 = ast_for_expr(c, CHILD(n, 3));
2547 if (!expr2)
2548 return NULL;
2549
2550 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 }
Neal Norwitz79792652005-11-14 04:25:03 +00002552 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002553 "improper number of parts to 'assert' statement: %d",
2554 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 return NULL;
2556}
2557
2558static asdl_seq *
2559ast_for_suite(struct compiling *c, const node *n)
2560{
2561 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002562 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 stmt_ty s;
2564 int i, total, num, end, pos = 0;
2565 node *ch;
2566
2567 REQ(n, suite);
2568
2569 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002570 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002572 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002574 n = CHILD(n, 0);
2575 /* simple_stmt always ends with a NEWLINE,
2576 and may have a trailing SEMI
2577 */
2578 end = NCH(n) - 1;
2579 if (TYPE(CHILD(n, end - 1)) == SEMI)
2580 end--;
2581 /* loop by 2 to skip semi-colons */
2582 for (i = 0; i < end; i += 2) {
2583 ch = CHILD(n, i);
2584 s = ast_for_stmt(c, ch);
2585 if (!s)
2586 return NULL;
2587 asdl_seq_SET(seq, pos++, s);
2588 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 }
2590 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002591 for (i = 2; i < (NCH(n) - 1); i++) {
2592 ch = CHILD(n, i);
2593 REQ(ch, stmt);
2594 num = num_stmts(ch);
2595 if (num == 1) {
2596 /* small_stmt or compound_stmt with only one child */
2597 s = ast_for_stmt(c, ch);
2598 if (!s)
2599 return NULL;
2600 asdl_seq_SET(seq, pos++, s);
2601 }
2602 else {
2603 int j;
2604 ch = CHILD(ch, 0);
2605 REQ(ch, simple_stmt);
2606 for (j = 0; j < NCH(ch); j += 2) {
2607 /* statement terminates with a semi-colon ';' */
2608 if (NCH(CHILD(ch, j)) == 0) {
2609 assert((j + 1) == NCH(ch));
2610 break;
2611 }
2612 s = ast_for_stmt(c, CHILD(ch, j));
2613 if (!s)
2614 return NULL;
2615 asdl_seq_SET(seq, pos++, s);
2616 }
2617 }
2618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 }
2620 assert(pos == seq->size);
2621 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622}
2623
2624static stmt_ty
2625ast_for_if_stmt(struct compiling *c, const node *n)
2626{
2627 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2628 ['else' ':' suite]
2629 */
2630 char *s;
2631
2632 REQ(n, if_stmt);
2633
2634 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002635 expr_ty expression;
2636 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002638 expression = ast_for_expr(c, CHILD(n, 1));
2639 if (!expression)
2640 return NULL;
2641 suite_seq = ast_for_suite(c, CHILD(n, 3));
2642 if (!suite_seq)
2643 return NULL;
2644
2645 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2646 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002648
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 s = STR(CHILD(n, 4));
2650 /* s[2], the third character in the string, will be
2651 's' for el_s_e, or
2652 'i' for el_i_f
2653 */
2654 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002655 expr_ty expression;
2656 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002658 expression = ast_for_expr(c, CHILD(n, 1));
2659 if (!expression)
2660 return NULL;
2661 seq1 = ast_for_suite(c, CHILD(n, 3));
2662 if (!seq1)
2663 return NULL;
2664 seq2 = ast_for_suite(c, CHILD(n, 6));
2665 if (!seq2)
2666 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002668 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2669 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 }
2671 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002672 int i, n_elif, has_else = 0;
2673 expr_ty expression;
2674 asdl_seq *suite_seq;
2675 asdl_seq *orelse = NULL;
2676 n_elif = NCH(n) - 4;
2677 /* must reference the child n_elif+1 since 'else' token is third,
2678 not fourth, child from the end. */
2679 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2680 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2681 has_else = 1;
2682 n_elif -= 3;
2683 }
2684 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002686 if (has_else) {
2687 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002689 orelse = asdl_seq_new(1, c->c_arena);
2690 if (!orelse)
2691 return NULL;
2692 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2693 if (!expression)
2694 return NULL;
2695 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2696 if (!suite_seq)
2697 return NULL;
2698 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2699 if (!suite_seq2)
2700 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002702 asdl_seq_SET(orelse, 0,
2703 If(expression, suite_seq, suite_seq2,
2704 LINENO(CHILD(n, NCH(n) - 6)),
2705 CHILD(n, NCH(n) - 6)->n_col_offset,
2706 c->c_arena));
2707 /* the just-created orelse handled the last elif */
2708 n_elif--;
2709 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002711 for (i = 0; i < n_elif; i++) {
2712 int off = 5 + (n_elif - i - 1) * 4;
2713 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2714 if (!newobj)
2715 return NULL;
2716 expression = ast_for_expr(c, CHILD(n, off));
2717 if (!expression)
2718 return NULL;
2719 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2720 if (!suite_seq)
2721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002723 asdl_seq_SET(newobj, 0,
2724 If(expression, suite_seq, orelse,
2725 LINENO(CHILD(n, off)),
2726 CHILD(n, off)->n_col_offset, c->c_arena));
2727 orelse = newobj;
2728 }
2729 expression = ast_for_expr(c, CHILD(n, 1));
2730 if (!expression)
2731 return NULL;
2732 suite_seq = ast_for_suite(c, CHILD(n, 3));
2733 if (!suite_seq)
2734 return NULL;
2735 return If(expression, suite_seq, orelse,
2736 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002738
2739 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002740 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742}
2743
2744static stmt_ty
2745ast_for_while_stmt(struct compiling *c, const node *n)
2746{
2747 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2748 REQ(n, while_stmt);
2749
2750 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002751 expr_ty expression;
2752 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002754 expression = ast_for_expr(c, CHILD(n, 1));
2755 if (!expression)
2756 return NULL;
2757 suite_seq = ast_for_suite(c, CHILD(n, 3));
2758 if (!suite_seq)
2759 return NULL;
2760 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2761 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 }
2763 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002764 expr_ty expression;
2765 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002767 expression = ast_for_expr(c, CHILD(n, 1));
2768 if (!expression)
2769 return NULL;
2770 seq1 = ast_for_suite(c, CHILD(n, 3));
2771 if (!seq1)
2772 return NULL;
2773 seq2 = ast_for_suite(c, CHILD(n, 6));
2774 if (!seq2)
2775 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002777 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2778 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002780
2781 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002782 "wrong number of tokens for 'while' statement: %d",
2783 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785}
2786
2787static stmt_ty
2788ast_for_for_stmt(struct compiling *c, const node *n)
2789{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002790 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 expr_ty expression;
2792 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002793 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2795 REQ(n, for_stmt);
2796
2797 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002798 seq = ast_for_suite(c, CHILD(n, 8));
2799 if (!seq)
2800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 }
2802
Neal Norwitzedef2be2006-07-12 05:26:17 +00002803 node_target = CHILD(n, 1);
2804 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002805 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002806 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002807 /* Check the # of children rather than the length of _target, since
2808 for x, in ... has 1 element in _target, but still requires a Tuple. */
2809 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002810 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002812 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002814 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002815 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002818 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002819 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002821 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002822 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823}
2824
2825static excepthandler_ty
2826ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2827{
Collin Winter62903052007-05-18 23:11:24 +00002828 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 REQ(exc, except_clause);
2830 REQ(body, suite);
2831
2832 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002833 asdl_seq *suite_seq = ast_for_suite(c, body);
2834 if (!suite_seq)
2835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836
Georg Brandla48f3ab2008-03-30 06:40:17 +00002837 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002838 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 }
2840 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002841 expr_ty expression;
2842 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002844 expression = ast_for_expr(c, CHILD(exc, 1));
2845 if (!expression)
2846 return NULL;
2847 suite_seq = ast_for_suite(c, body);
2848 if (!suite_seq)
2849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850
Georg Brandla48f3ab2008-03-30 06:40:17 +00002851 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002852 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 }
2854 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002855 asdl_seq *suite_seq;
2856 expr_ty expression;
2857 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2858 if (!e)
2859 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002860 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002861 return NULL;
2862 expression = ast_for_expr(c, CHILD(exc, 1));
2863 if (!expression)
2864 return NULL;
2865 suite_seq = ast_for_suite(c, body);
2866 if (!suite_seq)
2867 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868
Georg Brandla48f3ab2008-03-30 06:40:17 +00002869 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002870 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002872
2873 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002874 "wrong number of children for 'except' clause: %d",
2875 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002876 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877}
2878
2879static stmt_ty
2880ast_for_try_stmt(struct compiling *c, const node *n)
2881{
Neal Norwitzf599f422005-12-17 21:33:47 +00002882 const int nch = NCH(n);
2883 int n_except = (nch - 3)/3;
2884 asdl_seq *body, *orelse = NULL, *finally = NULL;
2885
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 REQ(n, try_stmt);
2887
Neal Norwitzf599f422005-12-17 21:33:47 +00002888 body = ast_for_suite(c, CHILD(n, 2));
2889 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891
Neal Norwitzf599f422005-12-17 21:33:47 +00002892 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002893 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2894 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2895 /* we can assume it's an "else",
2896 because nch >= 9 for try-else-finally and
2897 it would otherwise have a type of except_clause */
2898 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2899 if (orelse == NULL)
2900 return NULL;
2901 n_except--;
2902 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002904 finally = ast_for_suite(c, CHILD(n, nch - 1));
2905 if (finally == NULL)
2906 return NULL;
2907 n_except--;
2908 }
2909 else {
2910 /* we can assume it's an "else",
2911 otherwise it would have a type of except_clause */
2912 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2913 if (orelse == NULL)
2914 return NULL;
2915 n_except--;
2916 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002918 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002919 ast_error(n, "malformed 'try' statement");
2920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002922
2923 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002924 int i;
2925 stmt_ty except_st;
2926 /* process except statements to create a try ... except */
2927 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2928 if (handlers == NULL)
2929 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002930
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002931 for (i = 0; i < n_except; i++) {
2932 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2933 CHILD(n, 5 + i * 3));
2934 if (!e)
2935 return NULL;
2936 asdl_seq_SET(handlers, i, e);
2937 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002938
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002939 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2940 n->n_col_offset, c->c_arena);
2941 if (!finally)
2942 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002943
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002944 /* if a 'finally' is present too, we nest the TryExcept within a
2945 TryFinally to emulate try ... except ... finally */
2946 body = asdl_seq_new(1, c->c_arena);
2947 if (body == NULL)
2948 return NULL;
2949 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002950 }
2951
2952 /* must be a try ... finally (except clauses are in body, if any exist) */
2953 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002954 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955}
2956
Guido van Rossumc2e20742006-02-27 22:32:47 +00002957static expr_ty
2958ast_for_with_var(struct compiling *c, const node *n)
2959{
2960 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002961 return ast_for_expr(c, CHILD(n, 1));
2962}
2963
2964/* with_stmt: 'with' test [ with_var ] ':' suite */
2965static stmt_ty
2966ast_for_with_stmt(struct compiling *c, const node *n)
2967{
2968 expr_ty context_expr, optional_vars = NULL;
2969 int suite_index = 3; /* skip 'with', test, and ':' */
2970 asdl_seq *suite_seq;
2971
2972 assert(TYPE(n) == with_stmt);
2973 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002974 if (!context_expr)
2975 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002976 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002977 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002978
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002979 if (!optional_vars) {
2980 return NULL;
2981 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002982 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002983 return NULL;
2984 }
2985 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002986 }
2987
2988 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2989 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002990 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002991 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002992 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002993 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002994}
2995
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00002997ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998{
2999 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 asdl_seq *bases, *s;
3001
3002 REQ(n, classdef);
3003
Benjamin Petersond5efd202008-06-08 22:52:37 +00003004 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006
3007 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003008 s = ast_for_suite(c, CHILD(n, 3));
3009 if (!s)
3010 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003011 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3012 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 }
3014 /* check for empty base list */
3015 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003016 s = ast_for_suite(c, CHILD(n,5));
3017 if (!s)
3018 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003019 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3020 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 }
3022
3023 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003024 bases = ast_for_class_bases(c, CHILD(n, 3));
3025 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003026 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027
3028 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003029 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003030 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003031 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3032 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033}
3034
3035static stmt_ty
3036ast_for_stmt(struct compiling *c, const node *n)
3037{
3038 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003039 assert(NCH(n) == 1);
3040 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 }
3042 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003043 assert(num_stmts(n) == 1);
3044 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 }
3046 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003047 REQ(n, small_stmt);
3048 n = CHILD(n, 0);
3049 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3050 | flow_stmt | import_stmt | global_stmt | exec_stmt
3051 | assert_stmt
3052 */
3053 switch (TYPE(n)) {
3054 case expr_stmt:
3055 return ast_for_expr_stmt(c, n);
3056 case print_stmt:
3057 return ast_for_print_stmt(c, n);
3058 case del_stmt:
3059 return ast_for_del_stmt(c, n);
3060 case pass_stmt:
3061 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3062 case flow_stmt:
3063 return ast_for_flow_stmt(c, n);
3064 case import_stmt:
3065 return ast_for_import_stmt(c, n);
3066 case global_stmt:
3067 return ast_for_global_stmt(c, n);
3068 case exec_stmt:
3069 return ast_for_exec_stmt(c, n);
3070 case assert_stmt:
3071 return ast_for_assert_stmt(c, n);
3072 default:
3073 PyErr_Format(PyExc_SystemError,
3074 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3075 TYPE(n), NCH(n));
3076 return NULL;
3077 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078 }
3079 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003080 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003081 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003082 */
3083 node *ch = CHILD(n, 0);
3084 REQ(n, compound_stmt);
3085 switch (TYPE(ch)) {
3086 case if_stmt:
3087 return ast_for_if_stmt(c, ch);
3088 case while_stmt:
3089 return ast_for_while_stmt(c, ch);
3090 case for_stmt:
3091 return ast_for_for_stmt(c, ch);
3092 case try_stmt:
3093 return ast_for_try_stmt(c, ch);
3094 case with_stmt:
3095 return ast_for_with_stmt(c, ch);
3096 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003097 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003098 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003099 return ast_for_classdef(c, ch, NULL);
3100 case decorated:
3101 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003102 default:
3103 PyErr_Format(PyExc_SystemError,
3104 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3105 TYPE(n), NCH(n));
3106 return NULL;
3107 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 }
3109}
3110
3111static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003112parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003114 const char *end;
3115 long x;
3116 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003118 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003119 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120#endif
3121
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003122 errno = 0;
3123 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003125 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003127 if (*end == 'l' || *end == 'L')
3128 return PyLong_FromString((char *)s, (char **)0, 0);
3129 if (s[0] == '0') {
3130 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3131 if (x < 0 && errno == 0) {
3132 return PyLong_FromString((char *)s,
3133 (char **)0,
3134 0);
3135 }
3136 }
3137 else
3138 x = PyOS_strtol((char *)s, (char **)&end, 0);
3139 if (*end == '\0') {
3140 if (errno != 0)
3141 return PyLong_FromString((char *)s, (char **)0, 0);
3142 return PyInt_FromLong(x);
3143 }
3144 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003146 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003147 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003148 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003149 complex.imag = PyOS_ascii_atof(s);
3150 PyFPE_END_PROTECT(complex)
3151 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003152 }
3153 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003155 {
3156 PyFPE_START_PROTECT("atof", return 0)
3157 dx = PyOS_ascii_atof(s);
3158 PyFPE_END_PROTECT(dx)
3159 return PyFloat_FromDouble(dx);
3160 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161}
3162
3163static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003164decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165{
3166#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003167 Py_FatalError("decode_utf8 should not be called in this build.");
3168 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003170 PyObject *u, *v;
3171 char *s, *t;
3172 t = s = (char *)*sPtr;
3173 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3174 while (s < end && (*s & 0x80)) s++;
3175 *sPtr = s;
3176 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3177 if (u == NULL)
3178 return NULL;
3179 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3180 Py_DECREF(u);
3181 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182#endif
3183}
3184
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003185#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003187decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003189 PyObject *v, *u;
3190 char *buf;
3191 char *p;
3192 const char *end;
3193 if (encoding == NULL) {
3194 buf = (char *)s;
3195 u = NULL;
3196 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3197 buf = (char *)s;
3198 u = NULL;
3199 } else {
3200 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Christian Heimes593daf52008-05-26 12:51:38 +00003201 u = PyBytes_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003202 if (u == NULL)
3203 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +00003204 p = buf = PyBytes_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003205 end = s + len;
3206 while (s < end) {
3207 if (*s == '\\') {
3208 *p++ = *s++;
3209 if (*s & 0x80) {
3210 strcpy(p, "u005c");
3211 p += 5;
3212 }
3213 }
3214 if (*s & 0x80) { /* XXX inefficient */
3215 PyObject *w;
3216 char *r;
3217 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003218 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003219 if (w == NULL) {
3220 Py_DECREF(u);
3221 return NULL;
3222 }
Christian Heimes593daf52008-05-26 12:51:38 +00003223 r = PyBytes_AsString(w);
3224 rn = PyBytes_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003225 assert(rn % 2 == 0);
3226 for (i = 0; i < rn; i += 2) {
3227 sprintf(p, "\\u%02x%02x",
3228 r[i + 0] & 0xFF,
3229 r[i + 1] & 0xFF);
3230 p += 6;
3231 }
3232 Py_DECREF(w);
3233 } else {
3234 *p++ = *s++;
3235 }
3236 }
3237 len = p - buf;
3238 s = buf;
3239 }
3240 if (rawmode)
3241 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3242 else
3243 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3244 Py_XDECREF(u);
3245 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003247#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248
3249/* s is a Python string literal, including the bracketing quote characters,
3250 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3251 * parsestr parses it, and returns the decoded Python string object.
3252 */
3253static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003254parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003256 size_t len;
3257 int quote = Py_CHARMASK(*s);
3258 int rawmode = 0;
3259 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003260 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003262 if (isalpha(quote) || quote == '_') {
3263 if (quote == 'u' || quote == 'U') {
3264 quote = *++s;
3265 unicode = 1;
3266 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003267 if (quote == 'b' || quote == 'B') {
3268 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003269 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003270 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003271 if (quote == 'r' || quote == 'R') {
3272 quote = *++s;
3273 rawmode = 1;
3274 }
3275 }
3276 if (quote != '\'' && quote != '\"') {
3277 PyErr_BadInternalCall();
3278 return NULL;
3279 }
3280 s++;
3281 len = strlen(s);
3282 if (len > INT_MAX) {
3283 PyErr_SetString(PyExc_OverflowError,
3284 "string to parse is too long");
3285 return NULL;
3286 }
3287 if (s[--len] != quote) {
3288 PyErr_BadInternalCall();
3289 return NULL;
3290 }
3291 if (len >= 4 && s[0] == quote && s[1] == quote) {
3292 s += 2;
3293 len -= 2;
3294 if (s[--len] != quote || s[--len] != quote) {
3295 PyErr_BadInternalCall();
3296 return NULL;
3297 }
3298 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003300 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003301 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003304 need_encoding = (c->c_encoding != NULL &&
3305 strcmp(c->c_encoding, "utf-8") != 0 &&
3306 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003307 if (rawmode || strchr(s, '\\') == NULL) {
3308 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003310 /* This should not happen - we never see any other
3311 encoding. */
3312 Py_FatalError(
3313 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003315 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3316 if (u == NULL)
3317 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003318 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003319 Py_DECREF(u);
3320 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003322 } else {
Christian Heimes593daf52008-05-26 12:51:38 +00003323 return PyBytes_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003324 }
3325 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326
Christian Heimes593daf52008-05-26 12:51:38 +00003327 return PyBytes_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003328 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329}
3330
3331/* Build a Python string object out of a STRING atom. This takes care of
3332 * compile-time literal catenation, calling parsestr() on each piece, and
3333 * pasting the intermediate results together.
3334 */
3335static PyObject *
3336parsestrplus(struct compiling *c, const node *n)
3337{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003338 PyObject *v;
3339 int i;
3340 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003341 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003342 /* String literal concatenation */
3343 for (i = 1; i < NCH(n); i++) {
3344 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003345 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003346 if (s == NULL)
3347 goto onError;
Christian Heimes593daf52008-05-26 12:51:38 +00003348 if (PyBytes_Check(v) && PyBytes_Check(s)) {
3349 PyBytes_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003350 if (v == NULL)
3351 goto onError;
3352 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003354 else {
3355 PyObject *temp = PyUnicode_Concat(v, s);
3356 Py_DECREF(s);
3357 Py_DECREF(v);
3358 v = temp;
3359 if (v == NULL)
3360 goto onError;
3361 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003363 }
3364 }
3365 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366
3367 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003368 Py_XDECREF(v);
3369 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370}