blob: 6eb3aa411006b0026999b1045f58d17babe9a0ff [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) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000049 PyObject* id = PyString_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"))
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000133 return ast_error(n, "cannot assign to None");
134 if (!strcmp(x, "__debug__"))
135 return ast_error(n, "cannot assign to __debug__");
Benjamin Peterson399b1fe2008-10-25 02:53:28 +0000136 if (Py_Py3kWarningFlag) {
137 if (!(strcmp(x, "True") && strcmp(x, "False")) &&
138 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
139 return 0;
140 if (!strcmp(x, "nonlocal") &&
141 !ast_warn(c, n, "nonlocal is a keyword in 3.x"))
142 return 0;
143 }
Benjamin Petersond5efd202008-06-08 22:52:37 +0000144 return 1;
145}
146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147/* num_stmts() returns number of contained statements.
148
149 Use this routine to determine how big a sequence is needed for
150 the statements in a parse tree. Its raison d'etre is this bit of
151 grammar:
152
153 stmt: simple_stmt | compound_stmt
154 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
155
156 A simple_stmt can contain multiple small_stmt elements joined
157 by semicolons. If the arg is a simple_stmt, the number of
158 small_stmt elements is returned.
159*/
160
161static int
162num_stmts(const node *n)
163{
164 int i, l;
165 node *ch;
166
167 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000168 case single_input:
169 if (TYPE(CHILD(n, 0)) == NEWLINE)
170 return 0;
171 else
172 return num_stmts(CHILD(n, 0));
173 case file_input:
174 l = 0;
175 for (i = 0; i < NCH(n); i++) {
176 ch = CHILD(n, i);
177 if (TYPE(ch) == stmt)
178 l += num_stmts(ch);
179 }
180 return l;
181 case stmt:
182 return num_stmts(CHILD(n, 0));
183 case compound_stmt:
184 return 1;
185 case simple_stmt:
186 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
187 case suite:
188 if (NCH(n) == 1)
189 return num_stmts(CHILD(n, 0));
190 else {
191 l = 0;
192 for (i = 2; i < (NCH(n) - 1); i++)
193 l += num_stmts(CHILD(n, i));
194 return l;
195 }
196 default: {
197 char buf[128];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000199 sprintf(buf, "Non-statement found: %d %d\n",
200 TYPE(n), NCH(n));
201 Py_FatalError(buf);
202 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 }
204 assert(0);
205 return 0;
206}
207
208/* Transform the CST rooted at node * to the appropriate AST
209*/
210
211mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000212PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000213 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000215 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 asdl_seq *stmts = NULL;
217 stmt_ty s;
218 node *ch;
219 struct compiling c;
220
221 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000222 c.c_encoding = "utf-8";
223 if (TYPE(n) == encoding_decl) {
224 ast_error(n, "encoding declaration in Unicode string");
225 goto error;
226 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227 } else if (TYPE(n) == encoding_decl) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000228 c.c_encoding = STR(n);
229 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230 } else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000231 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 }
Christian Heimes3c608332008-03-26 22:01:37 +0000233 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000234 c.c_arena = arena;
Christian Heimesffcd1e12007-11-24 01:36:02 +0000235 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236
Jeremy Hyltona8293132006-02-28 17:58:27 +0000237 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000239 case file_input:
240 stmts = asdl_seq_new(num_stmts(n), arena);
241 if (!stmts)
242 return NULL;
243 for (i = 0; i < NCH(n) - 1; i++) {
244 ch = CHILD(n, i);
245 if (TYPE(ch) == NEWLINE)
246 continue;
247 REQ(ch, stmt);
248 num = num_stmts(ch);
249 if (num == 1) {
250 s = ast_for_stmt(&c, ch);
251 if (!s)
252 goto error;
253 asdl_seq_SET(stmts, k++, s);
254 }
255 else {
256 ch = CHILD(ch, 0);
257 REQ(ch, simple_stmt);
258 for (j = 0; j < num; j++) {
259 s = ast_for_stmt(&c, CHILD(ch, j * 2));
260 if (!s)
261 goto error;
262 asdl_seq_SET(stmts, k++, s);
263 }
264 }
265 }
266 return Module(stmts, arena);
267 case eval_input: {
268 expr_ty testlist_ast;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000270 /* XXX Why not gen_for here? */
271 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
272 if (!testlist_ast)
273 goto error;
274 return Expression(testlist_ast, arena);
275 }
276 case single_input:
277 if (TYPE(CHILD(n, 0)) == NEWLINE) {
278 stmts = asdl_seq_new(1, arena);
279 if (!stmts)
280 goto error;
281 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
282 arena));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000283 if (!asdl_seq_GET(stmts, 0))
284 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000285 return Interactive(stmts, arena);
286 }
287 else {
288 n = CHILD(n, 0);
289 num = num_stmts(n);
290 stmts = asdl_seq_new(num, arena);
291 if (!stmts)
292 goto error;
293 if (num == 1) {
294 s = ast_for_stmt(&c, n);
295 if (!s)
296 goto error;
297 asdl_seq_SET(stmts, 0, s);
298 }
299 else {
300 /* Only a simple_stmt can contain multiple statements. */
301 REQ(n, simple_stmt);
302 for (i = 0; i < NCH(n); i += 2) {
303 if (TYPE(CHILD(n, i)) == NEWLINE)
304 break;
305 s = ast_for_stmt(&c, CHILD(n, i));
306 if (!s)
307 goto error;
308 asdl_seq_SET(stmts, i / 2, s);
309 }
310 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000312 return Interactive(stmts, arena);
313 }
314 default:
Georg Brandlb8ae3d02007-05-02 20:02:29 +0000315 PyErr_Format(PyExc_SystemError,
316 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000317 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318 }
319 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 ast_error_finish(filename);
321 return NULL;
322}
323
324/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
325*/
326
327static operator_ty
328get_operator(const node *n)
329{
330 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000331 case VBAR:
332 return BitOr;
333 case CIRCUMFLEX:
334 return BitXor;
335 case AMPER:
336 return BitAnd;
337 case LEFTSHIFT:
338 return LShift;
339 case RIGHTSHIFT:
340 return RShift;
341 case PLUS:
342 return Add;
343 case MINUS:
344 return Sub;
345 case STAR:
346 return Mult;
347 case SLASH:
348 return Div;
349 case DOUBLESLASH:
350 return FloorDiv;
351 case PERCENT:
352 return Mod;
353 default:
354 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355 }
356}
357
Jeremy Hyltona8293132006-02-28 17:58:27 +0000358/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359
360 Only sets context for expr kinds that "can appear in assignment context"
361 (according to ../Parser/Python.asdl). For other expr kinds, it sets
362 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363*/
364
365static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000366set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367{
368 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000369 /* If a particular expression type can't be used for assign / delete,
370 set expr_name to its name and an error message will be generated.
371 */
372 const char* expr_name = NULL;
373
374 /* The ast defines augmented store and load contexts, but the
375 implementation here doesn't actually use them. The code may be
376 a little more complex than necessary as a result. It also means
Neil Schemenauer0e07b602006-07-09 16:16:34 +0000377 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000378 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000379 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000380 */
381 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382
383 switch (e->kind) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000384 case Attribute_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000385 if (ctx == Store && !forbidden_check(c, n,
386 PyBytes_AS_STRING(e->v.Attribute.attr)))
387 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000388 e->v.Attribute.ctx = ctx;
389 break;
390 case Subscript_kind:
391 e->v.Subscript.ctx = ctx;
392 break;
393 case Name_kind:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000394 if (ctx == Store && !forbidden_check(c, n,
395 PyBytes_AS_STRING(e->v.Name.id)))
396 return 0;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000397 e->v.Name.ctx = ctx;
398 break;
399 case List_kind:
400 e->v.List.ctx = ctx;
401 s = e->v.List.elts;
402 break;
403 case Tuple_kind:
404 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
405 return ast_error(n, "can't assign to ()");
406 e->v.Tuple.ctx = ctx;
407 s = e->v.Tuple.elts;
408 break;
409 case Lambda_kind:
410 expr_name = "lambda";
411 break;
412 case Call_kind:
413 expr_name = "function call";
414 break;
415 case BoolOp_kind:
416 case BinOp_kind:
417 case UnaryOp_kind:
418 expr_name = "operator";
419 break;
420 case GeneratorExp_kind:
421 expr_name = "generator expression";
422 break;
423 case Yield_kind:
424 expr_name = "yield expression";
425 break;
426 case ListComp_kind:
427 expr_name = "list comprehension";
428 break;
429 case Dict_kind:
430 case Num_kind:
431 case Str_kind:
432 expr_name = "literal";
433 break;
434 case Compare_kind:
435 expr_name = "comparison";
436 break;
437 case Repr_kind:
438 expr_name = "repr";
439 break;
440 case IfExp_kind:
441 expr_name = "conditional expression";
442 break;
443 default:
444 PyErr_Format(PyExc_SystemError,
445 "unexpected expression in assignment %d (line %d)",
446 e->kind, e->lineno);
447 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000449 /* Check for error string set by switch */
450 if (expr_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000451 char buf[300];
452 PyOS_snprintf(buf, sizeof(buf),
453 "can't %s %s",
454 ctx == Store ? "assign to" : "delete",
455 expr_name);
456 return ast_error(n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000457 }
458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000459 /* If the LHS is a list or tuple, we need to set the assignment
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000460 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461 */
462 if (s) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000463 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000465 for (i = 0; i < asdl_seq_LEN(s); i++) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000466 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000467 return 0;
468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469 }
470 return 1;
471}
472
473static operator_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000474ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475{
476 REQ(n, augassign);
477 n = CHILD(n, 0);
478 switch (STR(n)[0]) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000479 case '+':
480 return Add;
481 case '-':
482 return Sub;
483 case '/':
484 if (STR(n)[1] == '/')
485 return FloorDiv;
486 else
487 return Div;
488 case '%':
489 return Mod;
490 case '<':
491 return LShift;
492 case '>':
493 return RShift;
494 case '&':
495 return BitAnd;
496 case '^':
497 return BitXor;
498 case '|':
499 return BitOr;
500 case '*':
501 if (STR(n)[1] == '*')
502 return Pow;
503 else
504 return Mult;
505 default:
506 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
507 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508 }
509}
510
511static cmpop_ty
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000512ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513{
514 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000515 |'is' 'not'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516 */
517 REQ(n, comp_op);
518 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000519 n = CHILD(n, 0);
520 switch (TYPE(n)) {
521 case LESS:
522 return Lt;
523 case GREATER:
524 return Gt;
525 case EQEQUAL: /* == */
526 return Eq;
527 case LESSEQUAL:
528 return LtE;
529 case GREATEREQUAL:
530 return GtE;
531 case NOTEQUAL:
532 return NotEq;
533 case NAME:
534 if (strcmp(STR(n), "in") == 0)
535 return In;
536 if (strcmp(STR(n), "is") == 0)
537 return Is;
538 default:
539 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
540 STR(n));
541 return (cmpop_ty)0;
542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 }
544 else if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000545 /* handle "not in" and "is not" */
546 switch (TYPE(CHILD(n, 0))) {
547 case NAME:
548 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
549 return NotIn;
550 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
551 return IsNot;
552 default:
553 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
554 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
555 return (cmpop_ty)0;
556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557 }
Neal Norwitz79792652005-11-14 04:25:03 +0000558 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000559 NCH(n));
Martin v. Löwis28457502006-04-11 09:17:27 +0000560 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561}
562
563static asdl_seq *
564seq_for_testlist(struct compiling *c, const node *n)
565{
566 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000567 asdl_seq *seq;
568 expr_ty expression;
569 int i;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000570 assert(TYPE(n) == testlist ||
571 TYPE(n) == listmaker ||
572 TYPE(n) == testlist_gexp ||
573 TYPE(n) == testlist_safe ||
574 TYPE(n) == testlist1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000576 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
580 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000581 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000583 expression = ast_for_expr(c, CHILD(n, i));
584 if (!expression)
585 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000587 assert(i / 2 < seq->size);
588 asdl_seq_SET(seq, i / 2, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589 }
590 return seq;
591}
592
593static expr_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000594compiler_complex_args(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595{
596 int i, len = (NCH(n) + 1) / 2;
597 expr_ty result;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000598 asdl_seq *args = asdl_seq_new(len, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000600 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601
Neal Norwitz3a230172006-09-22 08:18:10 +0000602 /* fpdef: NAME | '(' fplist ')'
603 fplist: fpdef (',' fpdef)* [',']
604 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 REQ(n, fplist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 for (i = 0; i < len; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000607 const node *fpdef_node = CHILD(n, 2*i);
608 const node *child;
609 expr_ty arg;
Neal Norwitz3a230172006-09-22 08:18:10 +0000610set_name:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000611 /* fpdef_node is either a NAME or an fplist */
612 child = CHILD(fpdef_node, 0);
613 if (TYPE(child) == NAME) {
Benjamin Petersond5efd202008-06-08 22:52:37 +0000614 if (!forbidden_check(c, n, STR(child)))
615 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000616 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
617 child->n_col_offset, c->c_arena);
618 }
619 else {
620 assert(TYPE(fpdef_node) == fpdef);
621 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
622 child = CHILD(fpdef_node, 1);
623 assert(TYPE(child) == fplist);
624 /* NCH == 1 means we have (x), we need to elide the extra parens */
625 if (NCH(child) == 1) {
626 fpdef_node = CHILD(child, 0);
627 assert(TYPE(fpdef_node) == fpdef);
628 goto set_name;
629 }
630 arg = compiler_complex_args(c, child);
631 }
632 asdl_seq_SET(args, i, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 }
634
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000635 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000636 if (!set_context(c, result, Store, n))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000637 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 return result;
639}
640
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641
Jeremy Hyltona8293132006-02-28 17:58:27 +0000642/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643
644static arguments_ty
645ast_for_arguments(struct compiling *c, const node *n)
646{
647 /* parameters: '(' [varargslist] ')'
648 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000649 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 */
Jeremy Hyltona8293132006-02-28 17:58:27 +0000651 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 asdl_seq *args, *defaults;
653 identifier vararg = NULL, kwarg = NULL;
654 node *ch;
655
656 if (TYPE(n) == parameters) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000657 if (NCH(n) == 2) /* () as argument list */
658 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
659 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660 }
661 REQ(n, varargslist);
662
663 /* first count the number of normal args & defaults */
664 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000665 ch = CHILD(n, i);
666 if (TYPE(ch) == fpdef)
667 n_args++;
668 if (TYPE(ch) == EQUAL)
669 n_defaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000671 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 if (!args && n_args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000673 return NULL; /* Don't need to goto error; no objects allocated */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000674 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 if (!defaults && n_defaults)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000676 return NULL; /* Don't need to goto error; no objects allocated */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677
678 /* fpdef: NAME | '(' fplist ')'
679 fplist: fpdef (',' fpdef)* [',']
680 */
681 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000682 j = 0; /* index for defaults */
683 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 while (i < NCH(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000685 ch = CHILD(n, i);
686 switch (TYPE(ch)) {
687 case fpdef:
688 handle_fpdef:
689 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
690 anything other than EQUAL or a comma? */
691 /* XXX Should NCH(n) check be made a separate check? */
692 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
693 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
694 if (!expression)
695 goto error;
696 assert(defaults != NULL);
697 asdl_seq_SET(defaults, j++, expression);
698 i += 2;
699 found_default = 1;
700 }
701 else if (found_default) {
702 ast_error(n,
703 "non-default argument follows default argument");
704 goto error;
705 }
706 if (NCH(ch) == 3) {
707 ch = CHILD(ch, 1);
708 /* def foo((x)): is not complex, special case. */
709 if (NCH(ch) != 1) {
710 /* We have complex arguments, setup for unpacking. */
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000711 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
712 "tuple parameter unpacking has been removed in 3.x"))
713 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000714 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
Neal Norwitzdc9b32e2007-05-03 06:47:18 +0000715 if (!asdl_seq_GET(args, k-1))
716 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000717 } else {
718 /* def foo((x)): setup for checking NAME below. */
719 /* Loop because there can be many parens and tuple
720 unpacking mixed in. */
721 ch = CHILD(ch, 0);
722 assert(TYPE(ch) == fpdef);
723 goto handle_fpdef;
724 }
725 }
726 if (TYPE(CHILD(ch, 0)) == NAME) {
727 expr_ty name;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000728 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000729 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000730 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
731 Param, LINENO(ch), ch->n_col_offset,
732 c->c_arena);
733 if (!name)
734 goto error;
735 asdl_seq_SET(args, k++, name);
736
737 }
738 i += 2; /* the name and the comma */
739 break;
740 case STAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000741 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000742 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000743 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
744 i += 3;
745 break;
746 case DOUBLESTAR:
Benjamin Petersond5efd202008-06-08 22:52:37 +0000747 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000748 goto error;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000749 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
750 i += 3;
751 break;
752 default:
753 PyErr_Format(PyExc_SystemError,
754 "unexpected node in varargslist: %d @ %d",
755 TYPE(ch), i);
756 goto error;
757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 }
759
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000760 return arguments(args, vararg, kwarg, defaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761
762 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000763 Py_XDECREF(vararg);
764 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 return NULL;
766}
767
768static expr_ty
769ast_for_dotted_name(struct compiling *c, const node *n)
770{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000771 expr_ty e;
772 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000773 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 int i;
775
776 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000777
778 lineno = LINENO(n);
779 col_offset = n->n_col_offset;
780
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 id = NEW_IDENTIFIER(CHILD(n, 0));
782 if (!id)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000783 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000784 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000786 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787
788 for (i = 2; i < NCH(n); i+=2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000789 id = NEW_IDENTIFIER(CHILD(n, i));
790 if (!id)
791 return NULL;
792 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
793 if (!e)
794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 }
796
797 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798}
799
800static expr_ty
801ast_for_decorator(struct compiling *c, const node *n)
802{
803 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
804 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000805 expr_ty name_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806
807 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000808 REQ(CHILD(n, 0), AT);
809 REQ(RCHILD(n, -1), NEWLINE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810
811 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
812 if (!name_expr)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000813 return NULL;
814
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 if (NCH(n) == 3) { /* No arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000816 d = name_expr;
817 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 }
819 else if (NCH(n) == 5) { /* Call with no arguments */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000820 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
821 n->n_col_offset, c->c_arena);
822 if (!d)
823 return NULL;
824 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 }
826 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000827 d = ast_for_call(c, CHILD(n, 3), name_expr);
828 if (!d)
829 return NULL;
830 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 }
832
833 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834}
835
836static asdl_seq*
837ast_for_decorators(struct compiling *c, const node *n)
838{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000839 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000840 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 int i;
842
843 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000844 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 if (!decorator_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000846 return NULL;
847
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000849 d = ast_for_decorator(c, CHILD(n, i));
850 if (!d)
851 return NULL;
852 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
854 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855}
856
857static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000858ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859{
Christian Heimes5224d282008-02-23 15:01:05 +0000860 /* funcdef: 'def' NAME parameters ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +0000861 identifier name;
862 arguments_ty args;
863 asdl_seq *body;
Christian Heimes5224d282008-02-23 15:01:05 +0000864 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865
866 REQ(n, funcdef);
867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 name = NEW_IDENTIFIER(CHILD(n, name_i));
869 if (!name)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000870 return NULL;
Benjamin Petersond5efd202008-06-08 22:52:37 +0000871 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000872 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 args = ast_for_arguments(c, CHILD(n, name_i + 1));
874 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000875 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 body = ast_for_suite(c, CHILD(n, name_i + 3));
877 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000878 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000880 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000881 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882}
883
Christian Heimes5224d282008-02-23 15:01:05 +0000884static stmt_ty
885ast_for_decorated(struct compiling *c, const node *n)
886{
887 /* decorated: decorators (classdef | funcdef) */
888 stmt_ty thing = NULL;
889 asdl_seq *decorator_seq = NULL;
890
891 REQ(n, decorated);
892
893 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
894 if (!decorator_seq)
895 return NULL;
896
897 assert(TYPE(CHILD(n, 1)) == funcdef ||
898 TYPE(CHILD(n, 1)) == classdef);
899
900 if (TYPE(CHILD(n, 1)) == funcdef) {
901 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
902 } else if (TYPE(CHILD(n, 1)) == classdef) {
903 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
904 }
905 /* we count the decorators in when talking about the class' or
906 function's line number */
907 if (thing) {
908 thing->lineno = LINENO(n);
909 thing->col_offset = n->n_col_offset;
910 }
911 return thing;
912}
913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914static expr_ty
915ast_for_lambdef(struct compiling *c, const node *n)
916{
917 /* lambdef: 'lambda' [varargslist] ':' test */
918 arguments_ty args;
919 expr_ty expression;
920
921 if (NCH(n) == 3) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000922 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
923 if (!args)
924 return NULL;
925 expression = ast_for_expr(c, CHILD(n, 2));
926 if (!expression)
927 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 }
929 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000930 args = ast_for_arguments(c, CHILD(n, 1));
931 if (!args)
932 return NULL;
933 expression = ast_for_expr(c, CHILD(n, 3));
934 if (!expression)
935 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 }
937
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000938 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939}
940
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000941static expr_ty
942ast_for_ifexpr(struct compiling *c, const node *n)
943{
944 /* test: or_test 'if' or_test 'else' test */
945 expr_ty expression, body, orelse;
946
Thomas Woutersaa8b6c52006-02-27 16:46:22 +0000947 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000948 body = ast_for_expr(c, CHILD(n, 0));
949 if (!body)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000950 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000951 expression = ast_for_expr(c, CHILD(n, 2));
952 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000953 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000954 orelse = ast_for_expr(c, CHILD(n, 4));
955 if (!orelse)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000956 return NULL;
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000957 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000958 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000959}
960
Neal Norwitze4d4f002006-09-05 03:58:26 +0000961/* XXX(nnorwitz): the listcomp and genexpr code should be refactored
962 so there is only a single version. Possibly for loops can also re-use
963 the code.
964*/
965
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966/* Count the number of 'for' loop in a list comprehension.
967
968 Helper for ast_for_listcomp().
969*/
970
971static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +0000972count_list_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973{
974 int n_fors = 0;
975 node *ch = CHILD(n, 1);
976
977 count_list_for:
978 n_fors++;
979 REQ(ch, list_for);
980 if (NCH(ch) == 5)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000981 ch = CHILD(ch, 4);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000983 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 count_list_iter:
985 REQ(ch, list_iter);
986 ch = CHILD(ch, 0);
987 if (TYPE(ch) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000988 goto count_list_for;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 else if (TYPE(ch) == list_if) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +0000990 if (NCH(ch) == 3) {
991 ch = CHILD(ch, 2);
992 goto count_list_iter;
993 }
994 else
995 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 }
Neal Norwitz84456bd2005-12-18 03:16:20 +0000997
998 /* Should never be reached */
999 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
1000 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001}
1002
1003/* Count the number of 'if' statements in a list comprehension.
1004
1005 Helper for ast_for_listcomp().
1006*/
1007
1008static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001009count_list_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010{
1011 int n_ifs = 0;
1012
1013 count_list_iter:
1014 REQ(n, list_iter);
1015 if (TYPE(CHILD(n, 0)) == list_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001016 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 n = CHILD(n, 0);
1018 REQ(n, list_if);
1019 n_ifs++;
1020 if (NCH(n) == 2)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001021 return n_ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 n = CHILD(n, 2);
1023 goto count_list_iter;
1024}
1025
1026static expr_ty
1027ast_for_listcomp(struct compiling *c, const node *n)
1028{
1029 /* listmaker: test ( list_for | (',' test)* [','] )
1030 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1031 list_iter: list_for | list_if
1032 list_if: 'if' test [list_iter]
1033 testlist_safe: test [(',' test)+ [',']]
1034 */
1035 expr_ty elt;
1036 asdl_seq *listcomps;
1037 int i, n_fors;
1038 node *ch;
1039
1040 REQ(n, listmaker);
1041 assert(NCH(n) > 1);
1042
1043 elt = ast_for_expr(c, CHILD(n, 0));
1044 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001047 n_fors = count_list_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001051 listcomps = asdl_seq_new(n_fors, c->c_arena);
1052 if (!listcomps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001053 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 ch = CHILD(n, 1);
1056 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001057 comprehension_ty lc;
1058 asdl_seq *t;
1059 expr_ty expression;
1060 node *for_ch;
1061
1062 REQ(ch, list_for);
1063
1064 for_ch = CHILD(ch, 1);
1065 t = ast_for_exprlist(c, for_ch, Store);
1066 if (!t)
1067 return NULL;
1068 expression = ast_for_testlist(c, CHILD(ch, 3));
1069 if (!expression)
1070 return NULL;
1071
1072 /* Check the # of children rather than the length of t, since
1073 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1074 */
1075 if (NCH(for_ch) == 1)
1076 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1077 c->c_arena);
1078 else
1079 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1080 c->c_arena),
1081 expression, NULL, c->c_arena);
1082 if (!lc)
1083 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001085 if (NCH(ch) == 5) {
1086 int j, n_ifs;
1087 asdl_seq *ifs;
1088 expr_ty list_for_expr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001090 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001091 n_ifs = count_list_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001092 if (n_ifs == -1)
1093 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001095 ifs = asdl_seq_new(n_ifs, c->c_arena);
1096 if (!ifs)
1097 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001099 for (j = 0; j < n_ifs; j++) {
1100 REQ(ch, list_iter);
1101 ch = CHILD(ch, 0);
1102 REQ(ch, list_if);
1103
1104 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1105 if (!list_for_expr)
1106 return NULL;
1107
1108 asdl_seq_SET(ifs, j, list_for_expr);
1109 if (NCH(ch) == 3)
1110 ch = CHILD(ch, 2);
1111 }
1112 /* on exit, must guarantee that ch is a list_for */
1113 if (TYPE(ch) == list_iter)
1114 ch = CHILD(ch, 0);
1115 lc->ifs = ifs;
1116 }
1117 asdl_seq_SET(listcomps, i, lc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 }
1119
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001120 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121}
1122
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001123/* Count the number of 'for' loops in a generator expression.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124
1125 Helper for ast_for_genexp().
1126*/
1127
1128static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001129count_gen_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001131 int n_fors = 0;
1132 node *ch = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
1134 count_gen_for:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001135 n_fors++;
1136 REQ(ch, gen_for);
1137 if (NCH(ch) == 5)
1138 ch = CHILD(ch, 4);
1139 else
1140 return n_fors;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 count_gen_iter:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001142 REQ(ch, gen_iter);
1143 ch = CHILD(ch, 0);
1144 if (TYPE(ch) == gen_for)
1145 goto count_gen_for;
1146 else if (TYPE(ch) == gen_if) {
1147 if (NCH(ch) == 3) {
1148 ch = CHILD(ch, 2);
1149 goto count_gen_iter;
1150 }
1151 else
1152 return n_fors;
1153 }
1154
1155 /* Should never be reached */
1156 PyErr_SetString(PyExc_SystemError,
1157 "logic error in count_gen_fors");
1158 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159}
1160
1161/* Count the number of 'if' statements in a generator expression.
1162
1163 Helper for ast_for_genexp().
1164*/
1165
1166static int
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001167count_gen_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001169 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001171 while (1) {
1172 REQ(n, gen_iter);
1173 if (TYPE(CHILD(n, 0)) == gen_for)
1174 return n_ifs;
1175 n = CHILD(n, 0);
1176 REQ(n, gen_if);
1177 n_ifs++;
1178 if (NCH(n) == 2)
1179 return n_ifs;
1180 n = CHILD(n, 2);
1181 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182}
1183
Jeremy Hyltona8293132006-02-28 17:58:27 +00001184/* TODO(jhylton): Combine with list comprehension code? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185static expr_ty
1186ast_for_genexp(struct compiling *c, const node *n)
1187{
1188 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001189 argument: [test '='] test [gen_for] # Really [keyword '='] test */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 expr_ty elt;
1191 asdl_seq *genexps;
1192 int i, n_fors;
1193 node *ch;
1194
1195 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1196 assert(NCH(n) > 1);
1197
1198 elt = ast_for_expr(c, CHILD(n, 0));
1199 if (!elt)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001202 n_fors = count_gen_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 if (n_fors == -1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001204 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001205
1206 genexps = asdl_seq_new(n_fors, c->c_arena);
1207 if (!genexps)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001208 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 ch = CHILD(n, 1);
1211 for (i = 0; i < n_fors; i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001212 comprehension_ty ge;
1213 asdl_seq *t;
1214 expr_ty expression;
1215 node *for_ch;
1216
1217 REQ(ch, gen_for);
1218
1219 for_ch = CHILD(ch, 1);
1220 t = ast_for_exprlist(c, for_ch, Store);
1221 if (!t)
1222 return NULL;
1223 expression = ast_for_expr(c, CHILD(ch, 3));
1224 if (!expression)
1225 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001226
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001227 /* Check the # of children rather than the length of t, since
1228 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1229 if (NCH(for_ch) == 1)
1230 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1231 NULL, c->c_arena);
1232 else
1233 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1234 c->c_arena),
1235 expression, NULL, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001236
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001237 if (!ge)
1238 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001240 if (NCH(ch) == 5) {
1241 int j, n_ifs;
1242 asdl_seq *ifs;
1243
1244 ch = CHILD(ch, 4);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001245 n_ifs = count_gen_ifs(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001246 if (n_ifs == -1)
1247 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001249 ifs = asdl_seq_new(n_ifs, c->c_arena);
1250 if (!ifs)
1251 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001253 for (j = 0; j < n_ifs; j++) {
1254 REQ(ch, gen_iter);
1255 ch = CHILD(ch, 0);
1256 REQ(ch, gen_if);
1257
1258 expression = ast_for_expr(c, CHILD(ch, 1));
1259 if (!expression)
1260 return NULL;
1261 asdl_seq_SET(ifs, j, expression);
1262 if (NCH(ch) == 3)
1263 ch = CHILD(ch, 2);
1264 }
1265 /* on exit, must guarantee that ch is a gen_for */
1266 if (TYPE(ch) == gen_iter)
1267 ch = CHILD(ch, 0);
1268 ge->ifs = ifs;
1269 }
1270 asdl_seq_SET(genexps, i, ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 }
1272
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001273 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274}
1275
1276static expr_ty
1277ast_for_atom(struct compiling *c, const node *n)
1278{
1279 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1280 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1281 */
1282 node *ch = CHILD(n, 0);
1283
1284 switch (TYPE(ch)) {
1285 case NAME:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001286 /* All names start in Load context, but may later be
1287 changed. */
1288 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1289 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 case STRING: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001291 PyObject *str = parsestrplus(c, n);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001292 if (!str) {
Georg Brandldfe5dc82008-01-07 18:16:36 +00001293#ifdef Py_USING_UNICODE
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001294 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1295 PyObject *type, *value, *tback, *errstr;
1296 PyErr_Fetch(&type, &value, &tback);
1297 errstr = ((PyUnicodeErrorObject *)value)->reason;
1298 if (errstr) {
1299 char *s = "";
1300 char buf[128];
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001301 s = PyString_AsString(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001302 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1303 ast_error(n, buf);
1304 } else {
1305 ast_error(n, "(unicode error) unknown error");
1306 }
1307 Py_DECREF(type);
1308 Py_DECREF(value);
1309 Py_XDECREF(tback);
1310 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001311#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001312 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001313 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001314 PyArena_AddPyObject(c->c_arena, str);
1315 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 }
1317 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001318 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001319 if (!pynum)
1320 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001321
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001322 PyArena_AddPyObject(c->c_arena, pynum);
1323 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 }
1325 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001326 ch = CHILD(n, 1);
1327
1328 if (TYPE(ch) == RPAR)
1329 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1330
1331 if (TYPE(ch) == yield_expr)
1332 return ast_for_expr(c, ch);
1333
1334 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1335 return ast_for_genexp(c, ch);
1336
1337 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001339 ch = CHILD(n, 1);
1340
1341 if (TYPE(ch) == RSQB)
1342 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1343
1344 REQ(ch, listmaker);
1345 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1346 asdl_seq *elts = seq_for_testlist(c, ch);
1347 if (!elts)
1348 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001349
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001350 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1351 }
1352 else
1353 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001355 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1356 int i, size;
1357 asdl_seq *keys, *values;
1358
1359 ch = CHILD(n, 1);
1360 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1361 keys = asdl_seq_new(size, c->c_arena);
1362 if (!keys)
1363 return NULL;
1364
1365 values = asdl_seq_new(size, c->c_arena);
1366 if (!values)
1367 return NULL;
1368
1369 for (i = 0; i < NCH(ch); i += 4) {
1370 expr_ty expression;
1371
1372 expression = ast_for_expr(c, CHILD(ch, i));
1373 if (!expression)
1374 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001375
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001376 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001377
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001378 expression = ast_for_expr(c, CHILD(ch, i + 2));
1379 if (!expression)
1380 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001381
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001382 asdl_seq_SET(values, i / 4, expression);
1383 }
1384 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 }
1386 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001387 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001388 if (Py_Py3kWarningFlag &&
1389 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001390 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001391 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001392 if (!expression)
1393 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001394
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001395 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 }
1397 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001398 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1399 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 }
1401}
1402
1403static slice_ty
1404ast_for_slice(struct compiling *c, const node *n)
1405{
1406 node *ch;
1407 expr_ty lower = NULL, upper = NULL, step = NULL;
1408
1409 REQ(n, subscript);
1410
1411 /*
1412 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1413 sliceop: ':' [test]
1414 */
1415 ch = CHILD(n, 0);
1416 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001417 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418
1419 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001420 /* 'step' variable hold no significance in terms of being used over
1421 other vars */
1422 step = ast_for_expr(c, ch);
1423 if (!step)
1424 return NULL;
1425
1426 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 }
1428
1429 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001430 lower = ast_for_expr(c, ch);
1431 if (!lower)
1432 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 }
1434
1435 /* If there's an upper bound it's in the second or third position. */
1436 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001437 if (NCH(n) > 1) {
1438 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001440 if (TYPE(n2) == test) {
1441 upper = ast_for_expr(c, n2);
1442 if (!upper)
1443 return NULL;
1444 }
1445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001447 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001449 if (TYPE(n2) == test) {
1450 upper = ast_for_expr(c, n2);
1451 if (!upper)
1452 return NULL;
1453 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 }
1455
1456 ch = CHILD(n, NCH(n) - 1);
1457 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001458 if (NCH(ch) == 1) {
1459 /* No expression, so step is None */
1460 ch = CHILD(ch, 0);
1461 step = Name(new_identifier("None", c->c_arena), Load,
1462 LINENO(ch), ch->n_col_offset, c->c_arena);
1463 if (!step)
1464 return NULL;
1465 } else {
1466 ch = CHILD(ch, 1);
1467 if (TYPE(ch) == test) {
1468 step = ast_for_expr(c, ch);
1469 if (!step)
1470 return NULL;
1471 }
1472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 }
1474
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001475 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476}
1477
1478static expr_ty
1479ast_for_binop(struct compiling *c, const node *n)
1480{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001481 /* Must account for a sequence of expressions.
1482 How should A op B op C by represented?
1483 BinOp(BinOp(A, op, B), op, C).
1484 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001486 int i, nops;
1487 expr_ty expr1, expr2, result;
1488 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001490 expr1 = ast_for_expr(c, CHILD(n, 0));
1491 if (!expr1)
1492 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001494 expr2 = ast_for_expr(c, CHILD(n, 2));
1495 if (!expr2)
1496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001498 newoperator = get_operator(CHILD(n, 1));
1499 if (!newoperator)
1500 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001502 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1503 c->c_arena);
1504 if (!result)
1505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001507 nops = (NCH(n) - 1) / 2;
1508 for (i = 1; i < nops; i++) {
1509 expr_ty tmp_result, tmp;
1510 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001512 newoperator = get_operator(next_oper);
1513 if (!newoperator)
1514 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001516 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1517 if (!tmp)
1518 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001520 tmp_result = BinOp(result, newoperator, tmp,
1521 LINENO(next_oper), next_oper->n_col_offset,
1522 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001523 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001524 return NULL;
1525 result = tmp_result;
1526 }
1527 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528}
1529
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001530static expr_ty
1531ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1532{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001533 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1534 subscriptlist: subscript (',' subscript)* [',']
1535 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1536 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001537 REQ(n, trailer);
1538 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001539 if (NCH(n) == 2)
1540 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1541 n->n_col_offset, c->c_arena);
1542 else
1543 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001544 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001545 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001546 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1547 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001548 }
1549 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001550 REQ(CHILD(n, 0), LSQB);
1551 REQ(CHILD(n, 2), RSQB);
1552 n = CHILD(n, 1);
1553 if (NCH(n) == 1) {
1554 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1555 if (!slc)
1556 return NULL;
1557 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1558 c->c_arena);
1559 }
1560 else {
1561 /* The grammar is ambiguous here. The ambiguity is resolved
1562 by treating the sequence as a tuple literal if there are
1563 no slice features.
1564 */
1565 int j;
1566 slice_ty slc;
1567 expr_ty e;
1568 bool simple = true;
1569 asdl_seq *slices, *elts;
1570 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1571 if (!slices)
1572 return NULL;
1573 for (j = 0; j < NCH(n); j += 2) {
1574 slc = ast_for_slice(c, CHILD(n, j));
1575 if (!slc)
1576 return NULL;
1577 if (slc->kind != Index_kind)
1578 simple = false;
1579 asdl_seq_SET(slices, j / 2, slc);
1580 }
1581 if (!simple) {
1582 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1583 Load, LINENO(n), n->n_col_offset, c->c_arena);
1584 }
1585 /* extract Index values and put them in a Tuple */
1586 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1587 if (!elts)
1588 return NULL;
1589 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1590 slc = (slice_ty)asdl_seq_GET(slices, j);
1591 assert(slc->kind == Index_kind && slc->v.Index.value);
1592 asdl_seq_SET(elts, j, slc->v.Index.value);
1593 }
1594 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1595 if (!e)
1596 return NULL;
1597 return Subscript(left_expr, Index(e, c->c_arena),
1598 Load, LINENO(n), n->n_col_offset, c->c_arena);
1599 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001600 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001601}
1602
1603static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001604ast_for_factor(struct compiling *c, const node *n)
1605{
1606 node *pfactor, *ppower, *patom, *pnum;
1607 expr_ty expression;
1608
1609 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001610 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001611 constant. The peephole optimizer already does something like
1612 this but it doesn't handle the case where the constant is
1613 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1614 PyLongObject.
1615 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001616 if (TYPE(CHILD(n, 0)) == MINUS &&
1617 NCH(n) == 2 &&
1618 TYPE((pfactor = CHILD(n, 1))) == factor &&
1619 NCH(pfactor) == 1 &&
1620 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1621 NCH(ppower) == 1 &&
1622 TYPE((patom = CHILD(ppower, 0))) == atom &&
1623 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1624 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1625 if (s == NULL)
1626 return NULL;
1627 s[0] = '-';
1628 strcpy(s + 1, STR(pnum));
1629 PyObject_FREE(STR(pnum));
1630 STR(pnum) = s;
1631 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001632 }
1633
1634 expression = ast_for_expr(c, CHILD(n, 1));
1635 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001636 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001637
1638 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001639 case PLUS:
1640 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1641 c->c_arena);
1642 case MINUS:
1643 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1644 c->c_arena);
1645 case TILDE:
1646 return UnaryOp(Invert, expression, LINENO(n),
1647 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001648 }
1649 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001650 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001651 return NULL;
1652}
1653
1654static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001655ast_for_power(struct compiling *c, const node *n)
1656{
1657 /* power: atom trailer* ('**' factor)*
1658 */
1659 int i;
1660 expr_ty e, tmp;
1661 REQ(n, power);
1662 e = ast_for_atom(c, CHILD(n, 0));
1663 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001664 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001665 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001666 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001667 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001668 node *ch = CHILD(n, i);
1669 if (TYPE(ch) != trailer)
1670 break;
1671 tmp = ast_for_trailer(c, ch, e);
1672 if (!tmp)
1673 return NULL;
1674 tmp->lineno = e->lineno;
1675 tmp->col_offset = e->col_offset;
1676 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001677 }
1678 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001679 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1680 if (!f)
1681 return NULL;
1682 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1683 if (!tmp)
1684 return NULL;
1685 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001686 }
1687 return e;
1688}
1689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690/* Do not name a variable 'expr'! Will cause a compile error.
1691*/
1692
1693static expr_ty
1694ast_for_expr(struct compiling *c, const node *n)
1695{
1696 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001697 test: or_test ['if' or_test 'else' test] | lambdef
1698 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 and_test: not_test ('and' not_test)*
1700 not_test: 'not' not_test | comparison
1701 comparison: expr (comp_op expr)*
1702 expr: xor_expr ('|' xor_expr)*
1703 xor_expr: and_expr ('^' and_expr)*
1704 and_expr: shift_expr ('&' shift_expr)*
1705 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1706 arith_expr: term (('+'|'-') term)*
1707 term: factor (('*'|'/'|'%'|'//') factor)*
1708 factor: ('+'|'-'|'~') factor | power
1709 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001710
1711 As well as modified versions that exist for backward compatibility,
1712 to explicitly allow:
1713 [ x for x in lambda: 0, lambda: 1 ]
1714 (which would be ambiguous without these extra rules)
1715
1716 old_test: or_test | old_lambdef
1717 old_lambdef: 'lambda' [vararglist] ':' old_test
1718
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 */
1720
1721 asdl_seq *seq;
1722 int i;
1723
1724 loop:
1725 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001726 case test:
1727 case old_test:
1728 if (TYPE(CHILD(n, 0)) == lambdef ||
1729 TYPE(CHILD(n, 0)) == old_lambdef)
1730 return ast_for_lambdef(c, CHILD(n, 0));
1731 else if (NCH(n) > 1)
1732 return ast_for_ifexpr(c, n);
1733 /* Fallthrough */
1734 case or_test:
1735 case and_test:
1736 if (NCH(n) == 1) {
1737 n = CHILD(n, 0);
1738 goto loop;
1739 }
1740 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1741 if (!seq)
1742 return NULL;
1743 for (i = 0; i < NCH(n); i += 2) {
1744 expr_ty e = ast_for_expr(c, CHILD(n, i));
1745 if (!e)
1746 return NULL;
1747 asdl_seq_SET(seq, i / 2, e);
1748 }
1749 if (!strcmp(STR(CHILD(n, 1)), "and"))
1750 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1751 c->c_arena);
1752 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1753 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1754 case not_test:
1755 if (NCH(n) == 1) {
1756 n = CHILD(n, 0);
1757 goto loop;
1758 }
1759 else {
1760 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1761 if (!expression)
1762 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001764 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1765 c->c_arena);
1766 }
1767 case comparison:
1768 if (NCH(n) == 1) {
1769 n = CHILD(n, 0);
1770 goto loop;
1771 }
1772 else {
1773 expr_ty expression;
1774 asdl_int_seq *ops;
1775 asdl_seq *cmps;
1776 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1777 if (!ops)
1778 return NULL;
1779 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1780 if (!cmps) {
1781 return NULL;
1782 }
1783 for (i = 1; i < NCH(n); i += 2) {
1784 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001786 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001787 if (!newoperator) {
1788 return NULL;
1789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001791 expression = ast_for_expr(c, CHILD(n, i + 1));
1792 if (!expression) {
1793 return NULL;
1794 }
1795
1796 asdl_seq_SET(ops, i / 2, newoperator);
1797 asdl_seq_SET(cmps, i / 2, expression);
1798 }
1799 expression = ast_for_expr(c, CHILD(n, 0));
1800 if (!expression) {
1801 return NULL;
1802 }
1803
1804 return Compare(expression, ops, cmps, LINENO(n),
1805 n->n_col_offset, c->c_arena);
1806 }
1807 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001809 /* The next five cases all handle BinOps. The main body of code
1810 is the same in each case, but the switch turned inside out to
1811 reuse the code for each type of operator.
1812 */
1813 case expr:
1814 case xor_expr:
1815 case and_expr:
1816 case shift_expr:
1817 case arith_expr:
1818 case term:
1819 if (NCH(n) == 1) {
1820 n = CHILD(n, 0);
1821 goto loop;
1822 }
1823 return ast_for_binop(c, n);
1824 case yield_expr: {
1825 expr_ty exp = NULL;
1826 if (NCH(n) == 2) {
1827 exp = ast_for_testlist(c, CHILD(n, 1));
1828 if (!exp)
1829 return NULL;
1830 }
1831 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1832 }
1833 case factor:
1834 if (NCH(n) == 1) {
1835 n = CHILD(n, 0);
1836 goto loop;
1837 }
1838 return ast_for_factor(c, n);
1839 case power:
1840 return ast_for_power(c, n);
1841 default:
1842 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001845 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 return NULL;
1847}
1848
1849static expr_ty
1850ast_for_call(struct compiling *c, const node *n, expr_ty func)
1851{
1852 /*
1853 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001854 | '**' test)
1855 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 */
1857
1858 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001859 asdl_seq *args;
1860 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 expr_ty vararg = NULL, kwarg = NULL;
1862
1863 REQ(n, arglist);
1864
1865 nargs = 0;
1866 nkeywords = 0;
1867 ngens = 0;
1868 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001869 node *ch = CHILD(n, i);
1870 if (TYPE(ch) == argument) {
1871 if (NCH(ch) == 1)
1872 nargs++;
1873 else if (TYPE(CHILD(ch, 1)) == gen_for)
1874 ngens++;
1875 else
1876 nkeywords++;
1877 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 }
1879 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001880 ast_error(n, "Generator expression must be parenthesized "
1881 "if not sole argument");
1882 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 }
1884
1885 if (nargs + nkeywords + ngens > 255) {
1886 ast_error(n, "more than 255 arguments");
1887 return NULL;
1888 }
1889
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001890 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001892 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001893 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001895 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 nargs = 0;
1897 nkeywords = 0;
1898 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001899 node *ch = CHILD(n, i);
1900 if (TYPE(ch) == argument) {
1901 expr_ty e;
1902 if (NCH(ch) == 1) {
1903 if (nkeywords) {
1904 ast_error(CHILD(ch, 0),
1905 "non-keyword arg after keyword arg");
1906 return NULL;
1907 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001908 if (vararg) {
1909 ast_error(CHILD(ch, 0),
1910 "only named arguments may follow *expression");
1911 return NULL;
1912 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001913 e = ast_for_expr(c, CHILD(ch, 0));
1914 if (!e)
1915 return NULL;
1916 asdl_seq_SET(args, nargs++, e);
1917 }
1918 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1919 e = ast_for_genexp(c, ch);
1920 if (!e)
1921 return NULL;
1922 asdl_seq_SET(args, nargs++, e);
1923 }
1924 else {
1925 keyword_ty kw;
1926 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001927 int k;
1928 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001930 /* CHILD(ch, 0) is test, but must be an identifier? */
1931 e = ast_for_expr(c, CHILD(ch, 0));
1932 if (!e)
1933 return NULL;
1934 /* f(lambda x: x[0] = 3) ends up getting parsed with
1935 * LHS test = lambda x: x[0], and RHS test = 3.
1936 * SF bug 132313 points out that complaining about a keyword
1937 * then is very confusing.
1938 */
1939 if (e->kind == Lambda_kind) {
1940 ast_error(CHILD(ch, 0),
1941 "lambda cannot contain assignment");
1942 return NULL;
1943 } else if (e->kind != Name_kind) {
1944 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1945 return NULL;
1946 }
1947 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001948 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001949 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001950 for (k = 0; k < nkeywords; k++) {
1951 tmp = PyString_AS_STRING(
1952 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1953 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1954 ast_error(CHILD(ch, 0), "keyword argument repeated");
1955 return NULL;
1956 }
1957 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001958 e = ast_for_expr(c, CHILD(ch, 2));
1959 if (!e)
1960 return NULL;
1961 kw = keyword(key, e, c->c_arena);
1962 if (!kw)
1963 return NULL;
1964 asdl_seq_SET(keywords, nkeywords++, kw);
1965 }
1966 }
1967 else if (TYPE(ch) == STAR) {
1968 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001969 if (!vararg)
1970 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001971 i++;
1972 }
1973 else if (TYPE(ch) == DOUBLESTAR) {
1974 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001975 if (!kwarg)
1976 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001977 i++;
1978 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 }
1980
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001981 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1982 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983}
1984
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001986ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001988 /* testlist_gexp: test (',' test)* [','] */
1989 /* testlist: test (',' test)* [','] */
1990 /* testlist_safe: test (',' test)+ [','] */
1991 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001993 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001994 if (NCH(n) > 1)
1995 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001996 }
1997 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001998 assert(TYPE(n) == testlist ||
1999 TYPE(n) == testlist_safe ||
2000 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002001 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002003 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002005 asdl_seq *tmp = seq_for_testlist(c, n);
2006 if (!tmp)
2007 return NULL;
2008 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002010}
2011
2012static expr_ty
2013ast_for_testlist_gexp(struct compiling *c, const node* n)
2014{
2015 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2016 /* argument: test [ gen_for ] */
2017 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002018 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002019 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002020 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002021}
2022
2023/* like ast_for_testlist() but returns a sequence */
2024static asdl_seq*
2025ast_for_class_bases(struct compiling *c, const node* n)
2026{
2027 /* testlist: test (',' test)* [','] */
2028 assert(NCH(n) > 0);
2029 REQ(n, testlist);
2030 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002031 expr_ty base;
2032 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2033 if (!bases)
2034 return NULL;
2035 base = ast_for_expr(c, CHILD(n, 0));
2036 if (!base)
2037 return NULL;
2038 asdl_seq_SET(bases, 0, base);
2039 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002040 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002041
2042 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043}
2044
2045static stmt_ty
2046ast_for_expr_stmt(struct compiling *c, const node *n)
2047{
2048 REQ(n, expr_stmt);
2049 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002050 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 testlist: test (',' test)* [',']
2052 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002053 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 test: ... here starts the operator precendence dance
2055 */
2056
2057 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002058 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2059 if (!e)
2060 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002062 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 }
2064 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002065 expr_ty expr1, expr2;
2066 operator_ty newoperator;
2067 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002069 expr1 = ast_for_testlist(c, ch);
2070 if (!expr1)
2071 return NULL;
2072 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2073 switch (expr1->kind) {
2074 case GeneratorExp_kind:
2075 ast_error(ch, "augmented assignment to generator "
2076 "expression not possible");
2077 return NULL;
2078 case Yield_kind:
2079 ast_error(ch, "augmented assignment to yield "
2080 "expression not possible");
2081 return NULL;
2082 case Name_kind: {
Christian Heimes593daf52008-05-26 12:51:38 +00002083 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
Benjamin Petersond5efd202008-06-08 22:52:37 +00002084 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
2085 !forbidden_check(c, ch, var_name))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002086 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002087 break;
2088 }
2089 case Attribute_kind:
2090 case Subscript_kind:
2091 break;
2092 default:
2093 ast_error(ch, "illegal expression for augmented "
2094 "assignment");
2095 return NULL;
2096 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002097 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002098 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002100 ch = CHILD(n, 2);
2101 if (TYPE(ch) == testlist)
2102 expr2 = ast_for_testlist(c, ch);
2103 else
2104 expr2 = ast_for_expr(c, ch);
2105 if (!expr2)
2106 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002108 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002109 if (!newoperator)
2110 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002112 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2113 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 }
2115 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002116 int i;
2117 asdl_seq *targets;
2118 node *value;
2119 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002121 /* a normal assignment */
2122 REQ(CHILD(n, 1), EQUAL);
2123 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2124 if (!targets)
2125 return NULL;
2126 for (i = 0; i < NCH(n) - 2; i += 2) {
2127 expr_ty e;
2128 node *ch = CHILD(n, i);
2129 if (TYPE(ch) == yield_expr) {
2130 ast_error(ch, "assignment to yield expression not possible");
2131 return NULL;
2132 }
2133 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002135 /* set context to assign */
2136 if (!e)
2137 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002139 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002140 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002142 asdl_seq_SET(targets, i / 2, e);
2143 }
2144 value = CHILD(n, NCH(n) - 1);
2145 if (TYPE(value) == testlist)
2146 expression = ast_for_testlist(c, value);
2147 else
2148 expression = ast_for_expr(c, value);
2149 if (!expression)
2150 return NULL;
2151 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2152 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154}
2155
2156static stmt_ty
2157ast_for_print_stmt(struct compiling *c, const node *n)
2158{
2159 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002160 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 */
2162 expr_ty dest = NULL, expression;
2163 asdl_seq *seq;
2164 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002165 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166
2167 REQ(n, print_stmt);
2168 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002169 dest = ast_for_expr(c, CHILD(n, 2));
2170 if (!dest)
2171 return NULL;
2172 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002174 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002176 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002177 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002178 expression = ast_for_expr(c, CHILD(n, i));
2179 if (!expression)
2180 return NULL;
2181 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 }
2183 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002184 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185}
2186
2187static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002188ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189{
2190 asdl_seq *seq;
2191 int i;
2192 expr_ty e;
2193
2194 REQ(n, exprlist);
2195
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002196 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002200 e = ast_for_expr(c, CHILD(n, i));
2201 if (!e)
2202 return NULL;
2203 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002204 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 }
2207 return seq;
2208}
2209
2210static stmt_ty
2211ast_for_del_stmt(struct compiling *c, const node *n)
2212{
2213 asdl_seq *expr_list;
2214
2215 /* del_stmt: 'del' exprlist */
2216 REQ(n, del_stmt);
2217
2218 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2219 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002220 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002221 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222}
2223
2224static stmt_ty
2225ast_for_flow_stmt(struct compiling *c, const node *n)
2226{
2227 /*
2228 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002229 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 break_stmt: 'break'
2231 continue_stmt: 'continue'
2232 return_stmt: 'return' [testlist]
2233 yield_stmt: yield_expr
2234 yield_expr: 'yield' testlist
2235 raise_stmt: 'raise' [test [',' test [',' test]]]
2236 */
2237 node *ch;
2238
2239 REQ(n, flow_stmt);
2240 ch = CHILD(n, 0);
2241 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002242 case break_stmt:
2243 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2244 case continue_stmt:
2245 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2246 case yield_stmt: { /* will reduce to yield_expr */
2247 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2248 if (!exp)
2249 return NULL;
2250 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2251 }
2252 case return_stmt:
2253 if (NCH(ch) == 1)
2254 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2255 else {
2256 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2257 if (!expression)
2258 return NULL;
2259 return Return(expression, LINENO(n), n->n_col_offset,
2260 c->c_arena);
2261 }
2262 case raise_stmt:
2263 if (NCH(ch) == 1)
2264 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2265 c->c_arena);
2266 else if (NCH(ch) == 2) {
2267 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2268 if (!expression)
2269 return NULL;
2270 return Raise(expression, NULL, NULL, LINENO(n),
2271 n->n_col_offset, c->c_arena);
2272 }
2273 else if (NCH(ch) == 4) {
2274 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002276 expr1 = ast_for_expr(c, CHILD(ch, 1));
2277 if (!expr1)
2278 return NULL;
2279 expr2 = ast_for_expr(c, CHILD(ch, 3));
2280 if (!expr2)
2281 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002283 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2284 c->c_arena);
2285 }
2286 else if (NCH(ch) == 6) {
2287 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002289 expr1 = ast_for_expr(c, CHILD(ch, 1));
2290 if (!expr1)
2291 return NULL;
2292 expr2 = ast_for_expr(c, CHILD(ch, 3));
2293 if (!expr2)
2294 return NULL;
2295 expr3 = ast_for_expr(c, CHILD(ch, 5));
2296 if (!expr3)
2297 return NULL;
2298
2299 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2300 c->c_arena);
2301 }
2302 default:
2303 PyErr_Format(PyExc_SystemError,
2304 "unexpected flow_stmt: %d", TYPE(ch));
2305 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002307
2308 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2309 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310}
2311
2312static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002313alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314{
2315 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002316 import_as_name: NAME ['as' NAME]
2317 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318 dotted_name: NAME ('.' NAME)*
2319 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002320 PyObject *str;
2321
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 loop:
2323 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002324 case import_as_name:
2325 str = NULL;
2326 if (NCH(n) == 3) {
2327 str = NEW_IDENTIFIER(CHILD(n, 2));
2328 }
2329 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2330 case dotted_as_name:
2331 if (NCH(n) == 1) {
2332 n = CHILD(n, 0);
2333 goto loop;
2334 }
2335 else {
2336 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2337 if (!a)
2338 return NULL;
2339 assert(!a->asname);
2340 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2341 return a;
2342 }
2343 break;
2344 case dotted_name:
2345 if (NCH(n) == 1)
2346 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2347 else {
2348 /* Create a string of the form "a.b.c" */
2349 int i;
2350 size_t len;
2351 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002353 len = 0;
2354 for (i = 0; i < NCH(n); i += 2)
2355 /* length of string plus one for the dot */
2356 len += strlen(STR(CHILD(n, i))) + 1;
2357 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002358 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002359 if (!str)
2360 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002361 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002362 if (!s)
2363 return NULL;
2364 for (i = 0; i < NCH(n); i += 2) {
2365 char *sch = STR(CHILD(n, i));
2366 strcpy(s, STR(CHILD(n, i)));
2367 s += strlen(sch);
2368 *s++ = '.';
2369 }
2370 --s;
2371 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002372 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002373 PyArena_AddPyObject(c->c_arena, str);
2374 return alias(str, NULL, c->c_arena);
2375 }
2376 break;
2377 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002378 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002379 PyArena_AddPyObject(c->c_arena, str);
2380 return alias(str, NULL, c->c_arena);
2381 default:
2382 PyErr_Format(PyExc_SystemError,
2383 "unexpected import name: %d", TYPE(n));
2384 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002386
2387 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 return NULL;
2389}
2390
2391static stmt_ty
2392ast_for_import_stmt(struct compiling *c, const node *n)
2393{
2394 /*
2395 import_stmt: import_name | import_from
2396 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002397 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002398 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002400 int lineno;
2401 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 int i;
2403 asdl_seq *aliases;
2404
2405 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002406 lineno = LINENO(n);
2407 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002409 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002410 n = CHILD(n, 1);
2411 REQ(n, dotted_as_names);
2412 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2413 if (!aliases)
2414 return NULL;
2415 for (i = 0; i < NCH(n); i += 2) {
2416 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2417 if (!import_alias)
2418 return NULL;
2419 asdl_seq_SET(aliases, i / 2, import_alias);
2420 }
2421 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002423 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002424 int n_children;
2425 int idx, ndots = 0;
2426 alias_ty mod = NULL;
2427 identifier modname;
2428
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002429 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002430 optional module name */
2431 for (idx = 1; idx < NCH(n); idx++) {
2432 if (TYPE(CHILD(n, idx)) == dotted_name) {
2433 mod = alias_for_import_name(c, CHILD(n, idx));
2434 idx++;
2435 break;
2436 } else if (TYPE(CHILD(n, idx)) != DOT) {
2437 break;
2438 }
2439 ndots++;
2440 }
2441 idx++; /* skip over the 'import' keyword */
2442 switch (TYPE(CHILD(n, idx))) {
2443 case STAR:
2444 /* from ... import * */
2445 n = CHILD(n, idx);
2446 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002447 break;
2448 case LPAR:
2449 /* from ... import (x, y, z) */
2450 n = CHILD(n, idx + 1);
2451 n_children = NCH(n);
2452 break;
2453 case import_as_names:
2454 /* from ... import x, y, z */
2455 n = CHILD(n, idx);
2456 n_children = NCH(n);
2457 if (n_children % 2 == 0) {
2458 ast_error(n, "trailing comma not allowed without"
2459 " surrounding parentheses");
2460 return NULL;
2461 }
2462 break;
2463 default:
2464 ast_error(n, "Unexpected node-type in from-import");
2465 return NULL;
2466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002468 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2469 if (!aliases)
2470 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002472 /* handle "from ... import *" special b/c there's no children */
2473 if (TYPE(n) == STAR) {
2474 alias_ty import_alias = alias_for_import_name(c, n);
2475 if (!import_alias)
2476 return NULL;
2477 asdl_seq_SET(aliases, 0, import_alias);
2478 }
2479 else {
2480 for (i = 0; i < NCH(n); i += 2) {
2481 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2482 if (!import_alias)
2483 return NULL;
2484 asdl_seq_SET(aliases, i / 2, import_alias);
2485 }
2486 }
2487 if (mod != NULL)
2488 modname = mod->name;
2489 else
2490 modname = new_identifier("", c->c_arena);
2491 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2492 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 }
Neal Norwitz79792652005-11-14 04:25:03 +00002494 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002495 "unknown import statement: starts with command '%s'",
2496 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 return NULL;
2498}
2499
2500static stmt_ty
2501ast_for_global_stmt(struct compiling *c, const node *n)
2502{
2503 /* global_stmt: 'global' NAME (',' NAME)* */
2504 identifier name;
2505 asdl_seq *s;
2506 int i;
2507
2508 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002509 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002511 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002513 name = NEW_IDENTIFIER(CHILD(n, i));
2514 if (!name)
2515 return NULL;
2516 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002518 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519}
2520
2521static stmt_ty
2522ast_for_exec_stmt(struct compiling *c, const node *n)
2523{
2524 expr_ty expr1, globals = NULL, locals = NULL;
2525 int n_children = NCH(n);
2526 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002527 PyErr_Format(PyExc_SystemError,
2528 "poorly formed 'exec' statement: %d parts to statement",
2529 n_children);
2530 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 }
2532
2533 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2534 REQ(n, exec_stmt);
2535 expr1 = ast_for_expr(c, CHILD(n, 1));
2536 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002537 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002539 globals = ast_for_expr(c, CHILD(n, 3));
2540 if (!globals)
2541 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 }
2543 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002544 locals = ast_for_expr(c, CHILD(n, 5));
2545 if (!locals)
2546 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 }
2548
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002549 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2550 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551}
2552
2553static stmt_ty
2554ast_for_assert_stmt(struct compiling *c, const node *n)
2555{
2556 /* assert_stmt: 'assert' test [',' test] */
2557 REQ(n, assert_stmt);
2558 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002559 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2560 if (!expression)
2561 return NULL;
2562 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2563 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 }
2565 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002566 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002568 expr1 = ast_for_expr(c, CHILD(n, 1));
2569 if (!expr1)
2570 return NULL;
2571 expr2 = ast_for_expr(c, CHILD(n, 3));
2572 if (!expr2)
2573 return NULL;
2574
2575 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 }
Neal Norwitz79792652005-11-14 04:25:03 +00002577 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002578 "improper number of parts to 'assert' statement: %d",
2579 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 return NULL;
2581}
2582
2583static asdl_seq *
2584ast_for_suite(struct compiling *c, const node *n)
2585{
2586 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002587 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 stmt_ty s;
2589 int i, total, num, end, pos = 0;
2590 node *ch;
2591
2592 REQ(n, suite);
2593
2594 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002595 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002597 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002599 n = CHILD(n, 0);
2600 /* simple_stmt always ends with a NEWLINE,
2601 and may have a trailing SEMI
2602 */
2603 end = NCH(n) - 1;
2604 if (TYPE(CHILD(n, end - 1)) == SEMI)
2605 end--;
2606 /* loop by 2 to skip semi-colons */
2607 for (i = 0; i < end; i += 2) {
2608 ch = CHILD(n, i);
2609 s = ast_for_stmt(c, ch);
2610 if (!s)
2611 return NULL;
2612 asdl_seq_SET(seq, pos++, s);
2613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 }
2615 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002616 for (i = 2; i < (NCH(n) - 1); i++) {
2617 ch = CHILD(n, i);
2618 REQ(ch, stmt);
2619 num = num_stmts(ch);
2620 if (num == 1) {
2621 /* small_stmt or compound_stmt with only one child */
2622 s = ast_for_stmt(c, ch);
2623 if (!s)
2624 return NULL;
2625 asdl_seq_SET(seq, pos++, s);
2626 }
2627 else {
2628 int j;
2629 ch = CHILD(ch, 0);
2630 REQ(ch, simple_stmt);
2631 for (j = 0; j < NCH(ch); j += 2) {
2632 /* statement terminates with a semi-colon ';' */
2633 if (NCH(CHILD(ch, j)) == 0) {
2634 assert((j + 1) == NCH(ch));
2635 break;
2636 }
2637 s = ast_for_stmt(c, CHILD(ch, j));
2638 if (!s)
2639 return NULL;
2640 asdl_seq_SET(seq, pos++, s);
2641 }
2642 }
2643 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
2645 assert(pos == seq->size);
2646 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647}
2648
2649static stmt_ty
2650ast_for_if_stmt(struct compiling *c, const node *n)
2651{
2652 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2653 ['else' ':' suite]
2654 */
2655 char *s;
2656
2657 REQ(n, if_stmt);
2658
2659 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002660 expr_ty expression;
2661 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002663 expression = ast_for_expr(c, CHILD(n, 1));
2664 if (!expression)
2665 return NULL;
2666 suite_seq = ast_for_suite(c, CHILD(n, 3));
2667 if (!suite_seq)
2668 return NULL;
2669
2670 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2671 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002673
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 s = STR(CHILD(n, 4));
2675 /* s[2], the third character in the string, will be
2676 's' for el_s_e, or
2677 'i' for el_i_f
2678 */
2679 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002680 expr_ty expression;
2681 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002683 expression = ast_for_expr(c, CHILD(n, 1));
2684 if (!expression)
2685 return NULL;
2686 seq1 = ast_for_suite(c, CHILD(n, 3));
2687 if (!seq1)
2688 return NULL;
2689 seq2 = ast_for_suite(c, CHILD(n, 6));
2690 if (!seq2)
2691 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002693 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2694 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 }
2696 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002697 int i, n_elif, has_else = 0;
2698 expr_ty expression;
2699 asdl_seq *suite_seq;
2700 asdl_seq *orelse = NULL;
2701 n_elif = NCH(n) - 4;
2702 /* must reference the child n_elif+1 since 'else' token is third,
2703 not fourth, child from the end. */
2704 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2705 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2706 has_else = 1;
2707 n_elif -= 3;
2708 }
2709 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002711 if (has_else) {
2712 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002714 orelse = asdl_seq_new(1, c->c_arena);
2715 if (!orelse)
2716 return NULL;
2717 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2718 if (!expression)
2719 return NULL;
2720 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2721 if (!suite_seq)
2722 return NULL;
2723 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2724 if (!suite_seq2)
2725 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002727 asdl_seq_SET(orelse, 0,
2728 If(expression, suite_seq, suite_seq2,
2729 LINENO(CHILD(n, NCH(n) - 6)),
2730 CHILD(n, NCH(n) - 6)->n_col_offset,
2731 c->c_arena));
2732 /* the just-created orelse handled the last elif */
2733 n_elif--;
2734 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002736 for (i = 0; i < n_elif; i++) {
2737 int off = 5 + (n_elif - i - 1) * 4;
2738 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2739 if (!newobj)
2740 return NULL;
2741 expression = ast_for_expr(c, CHILD(n, off));
2742 if (!expression)
2743 return NULL;
2744 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2745 if (!suite_seq)
2746 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002748 asdl_seq_SET(newobj, 0,
2749 If(expression, suite_seq, orelse,
2750 LINENO(CHILD(n, off)),
2751 CHILD(n, off)->n_col_offset, c->c_arena));
2752 orelse = newobj;
2753 }
2754 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 If(expression, suite_seq, orelse,
2761 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002763
2764 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002765 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002766 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767}
2768
2769static stmt_ty
2770ast_for_while_stmt(struct compiling *c, const node *n)
2771{
2772 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2773 REQ(n, while_stmt);
2774
2775 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002776 expr_ty expression;
2777 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002779 expression = ast_for_expr(c, CHILD(n, 1));
2780 if (!expression)
2781 return NULL;
2782 suite_seq = ast_for_suite(c, CHILD(n, 3));
2783 if (!suite_seq)
2784 return NULL;
2785 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2786 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787 }
2788 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002789 expr_ty expression;
2790 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002792 expression = ast_for_expr(c, CHILD(n, 1));
2793 if (!expression)
2794 return NULL;
2795 seq1 = ast_for_suite(c, CHILD(n, 3));
2796 if (!seq1)
2797 return NULL;
2798 seq2 = ast_for_suite(c, CHILD(n, 6));
2799 if (!seq2)
2800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002802 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2803 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002805
2806 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002807 "wrong number of tokens for 'while' statement: %d",
2808 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810}
2811
2812static stmt_ty
2813ast_for_for_stmt(struct compiling *c, const node *n)
2814{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002815 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 expr_ty expression;
2817 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002818 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2820 REQ(n, for_stmt);
2821
2822 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002823 seq = ast_for_suite(c, CHILD(n, 8));
2824 if (!seq)
2825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 }
2827
Neal Norwitzedef2be2006-07-12 05:26:17 +00002828 node_target = CHILD(n, 1);
2829 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002830 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002831 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002832 /* Check the # of children rather than the length of _target, since
2833 for x, in ... has 1 element in _target, but still requires a Tuple. */
2834 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002835 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002837 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002839 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002840 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002841 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002843 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002844 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002846 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002847 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848}
2849
2850static excepthandler_ty
2851ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2852{
Collin Winter62903052007-05-18 23:11:24 +00002853 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 REQ(exc, except_clause);
2855 REQ(body, suite);
2856
2857 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002858 asdl_seq *suite_seq = ast_for_suite(c, body);
2859 if (!suite_seq)
2860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861
Georg Brandla48f3ab2008-03-30 06:40:17 +00002862 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002863 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 }
2865 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002866 expr_ty expression;
2867 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002869 expression = ast_for_expr(c, CHILD(exc, 1));
2870 if (!expression)
2871 return NULL;
2872 suite_seq = ast_for_suite(c, body);
2873 if (!suite_seq)
2874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875
Georg Brandla48f3ab2008-03-30 06:40:17 +00002876 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002877 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 }
2879 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002880 asdl_seq *suite_seq;
2881 expr_ty expression;
2882 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2883 if (!e)
2884 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002885 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002886 return NULL;
2887 expression = ast_for_expr(c, CHILD(exc, 1));
2888 if (!expression)
2889 return NULL;
2890 suite_seq = ast_for_suite(c, body);
2891 if (!suite_seq)
2892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893
Georg Brandla48f3ab2008-03-30 06:40:17 +00002894 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002895 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002897
2898 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002899 "wrong number of children for 'except' clause: %d",
2900 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002901 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902}
2903
2904static stmt_ty
2905ast_for_try_stmt(struct compiling *c, const node *n)
2906{
Neal Norwitzf599f422005-12-17 21:33:47 +00002907 const int nch = NCH(n);
2908 int n_except = (nch - 3)/3;
2909 asdl_seq *body, *orelse = NULL, *finally = NULL;
2910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911 REQ(n, try_stmt);
2912
Neal Norwitzf599f422005-12-17 21:33:47 +00002913 body = ast_for_suite(c, CHILD(n, 2));
2914 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916
Neal Norwitzf599f422005-12-17 21:33:47 +00002917 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002918 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2919 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2920 /* we can assume it's an "else",
2921 because nch >= 9 for try-else-finally and
2922 it would otherwise have a type of except_clause */
2923 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2924 if (orelse == NULL)
2925 return NULL;
2926 n_except--;
2927 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002928
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002929 finally = ast_for_suite(c, CHILD(n, nch - 1));
2930 if (finally == NULL)
2931 return NULL;
2932 n_except--;
2933 }
2934 else {
2935 /* we can assume it's an "else",
2936 otherwise it would have a type of except_clause */
2937 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2938 if (orelse == NULL)
2939 return NULL;
2940 n_except--;
2941 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002943 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002944 ast_error(n, "malformed 'try' statement");
2945 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002947
2948 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002949 int i;
2950 stmt_ty except_st;
2951 /* process except statements to create a try ... except */
2952 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2953 if (handlers == NULL)
2954 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002955
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002956 for (i = 0; i < n_except; i++) {
2957 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2958 CHILD(n, 5 + i * 3));
2959 if (!e)
2960 return NULL;
2961 asdl_seq_SET(handlers, i, e);
2962 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002963
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002964 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2965 n->n_col_offset, c->c_arena);
2966 if (!finally)
2967 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002968
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002969 /* if a 'finally' is present too, we nest the TryExcept within a
2970 TryFinally to emulate try ... except ... finally */
2971 body = asdl_seq_new(1, c->c_arena);
2972 if (body == NULL)
2973 return NULL;
2974 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002975 }
2976
2977 /* must be a try ... finally (except clauses are in body, if any exist) */
2978 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002979 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980}
2981
Guido van Rossumc2e20742006-02-27 22:32:47 +00002982static expr_ty
2983ast_for_with_var(struct compiling *c, const node *n)
2984{
2985 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002986 return ast_for_expr(c, CHILD(n, 1));
2987}
2988
2989/* with_stmt: 'with' test [ with_var ] ':' suite */
2990static stmt_ty
2991ast_for_with_stmt(struct compiling *c, const node *n)
2992{
2993 expr_ty context_expr, optional_vars = NULL;
2994 int suite_index = 3; /* skip 'with', test, and ':' */
2995 asdl_seq *suite_seq;
2996
2997 assert(TYPE(n) == with_stmt);
2998 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00002999 if (!context_expr)
3000 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003001 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003002 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003003
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003004 if (!optional_vars) {
3005 return NULL;
3006 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003007 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003008 return NULL;
3009 }
3010 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003011 }
3012
3013 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
3014 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003015 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003016 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003017 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003018 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003019}
3020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003022ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023{
3024 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 asdl_seq *bases, *s;
3026
3027 REQ(n, classdef);
3028
Benjamin Petersond5efd202008-06-08 22:52:37 +00003029 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003030 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031
3032 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003033 s = ast_for_suite(c, CHILD(n, 3));
3034 if (!s)
3035 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003036 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3037 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038 }
3039 /* check for empty base list */
3040 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003041 s = ast_for_suite(c, CHILD(n,5));
3042 if (!s)
3043 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003044 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3045 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046 }
3047
3048 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003049 bases = ast_for_class_bases(c, CHILD(n, 3));
3050 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003051 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052
3053 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003054 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003055 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003056 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3057 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058}
3059
3060static stmt_ty
3061ast_for_stmt(struct compiling *c, const node *n)
3062{
3063 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003064 assert(NCH(n) == 1);
3065 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066 }
3067 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003068 assert(num_stmts(n) == 1);
3069 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 }
3071 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003072 REQ(n, small_stmt);
3073 n = CHILD(n, 0);
3074 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3075 | flow_stmt | import_stmt | global_stmt | exec_stmt
3076 | assert_stmt
3077 */
3078 switch (TYPE(n)) {
3079 case expr_stmt:
3080 return ast_for_expr_stmt(c, n);
3081 case print_stmt:
3082 return ast_for_print_stmt(c, n);
3083 case del_stmt:
3084 return ast_for_del_stmt(c, n);
3085 case pass_stmt:
3086 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3087 case flow_stmt:
3088 return ast_for_flow_stmt(c, n);
3089 case import_stmt:
3090 return ast_for_import_stmt(c, n);
3091 case global_stmt:
3092 return ast_for_global_stmt(c, n);
3093 case exec_stmt:
3094 return ast_for_exec_stmt(c, n);
3095 case assert_stmt:
3096 return ast_for_assert_stmt(c, n);
3097 default:
3098 PyErr_Format(PyExc_SystemError,
3099 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3100 TYPE(n), NCH(n));
3101 return NULL;
3102 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103 }
3104 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003105 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003106 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003107 */
3108 node *ch = CHILD(n, 0);
3109 REQ(n, compound_stmt);
3110 switch (TYPE(ch)) {
3111 case if_stmt:
3112 return ast_for_if_stmt(c, ch);
3113 case while_stmt:
3114 return ast_for_while_stmt(c, ch);
3115 case for_stmt:
3116 return ast_for_for_stmt(c, ch);
3117 case try_stmt:
3118 return ast_for_try_stmt(c, ch);
3119 case with_stmt:
3120 return ast_for_with_stmt(c, ch);
3121 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003122 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003123 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003124 return ast_for_classdef(c, ch, NULL);
3125 case decorated:
3126 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003127 default:
3128 PyErr_Format(PyExc_SystemError,
3129 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3130 TYPE(n), NCH(n));
3131 return NULL;
3132 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 }
3134}
3135
3136static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003137parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003139 const char *end;
3140 long x;
3141 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003143 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003144 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145#endif
3146
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003147 errno = 0;
3148 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003150 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003152 if (*end == 'l' || *end == 'L')
3153 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003154 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003155 if (*end == '\0') {
3156 if (errno != 0)
3157 return PyLong_FromString((char *)s, (char **)0, 0);
3158 return PyInt_FromLong(x);
3159 }
3160 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003162 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003163 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003164 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003165 complex.imag = PyOS_ascii_atof(s);
3166 PyFPE_END_PROTECT(complex)
3167 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003168 }
3169 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003171 {
3172 PyFPE_START_PROTECT("atof", return 0)
3173 dx = PyOS_ascii_atof(s);
3174 PyFPE_END_PROTECT(dx)
3175 return PyFloat_FromDouble(dx);
3176 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177}
3178
3179static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003180decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181{
3182#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003183 Py_FatalError("decode_utf8 should not be called in this build.");
3184 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003186 PyObject *u, *v;
3187 char *s, *t;
3188 t = s = (char *)*sPtr;
3189 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3190 while (s < end && (*s & 0x80)) s++;
3191 *sPtr = s;
3192 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3193 if (u == NULL)
3194 return NULL;
3195 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3196 Py_DECREF(u);
3197 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198#endif
3199}
3200
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003201#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003203decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003205 PyObject *v, *u;
3206 char *buf;
3207 char *p;
3208 const char *end;
3209 if (encoding == NULL) {
3210 buf = (char *)s;
3211 u = NULL;
3212 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3213 buf = (char *)s;
3214 u = NULL;
3215 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003216 /* check for integer overflow */
3217 if (len > PY_SIZE_MAX / 4)
3218 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003219 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003220 u = PyString_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003221 if (u == NULL)
3222 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003223 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003224 end = s + len;
3225 while (s < end) {
3226 if (*s == '\\') {
3227 *p++ = *s++;
3228 if (*s & 0x80) {
3229 strcpy(p, "u005c");
3230 p += 5;
3231 }
3232 }
3233 if (*s & 0x80) { /* XXX inefficient */
3234 PyObject *w;
3235 char *r;
3236 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003237 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003238 if (w == NULL) {
3239 Py_DECREF(u);
3240 return NULL;
3241 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003242 r = PyString_AsString(w);
3243 rn = PyString_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003244 assert(rn % 2 == 0);
3245 for (i = 0; i < rn; i += 2) {
3246 sprintf(p, "\\u%02x%02x",
3247 r[i + 0] & 0xFF,
3248 r[i + 1] & 0xFF);
3249 p += 6;
3250 }
3251 Py_DECREF(w);
3252 } else {
3253 *p++ = *s++;
3254 }
3255 }
3256 len = p - buf;
3257 s = buf;
3258 }
3259 if (rawmode)
3260 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3261 else
3262 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3263 Py_XDECREF(u);
3264 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003266#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267
3268/* s is a Python string literal, including the bracketing quote characters,
3269 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3270 * parsestr parses it, and returns the decoded Python string object.
3271 */
3272static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003273parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003275 size_t len;
3276 int quote = Py_CHARMASK(*s);
3277 int rawmode = 0;
3278 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003279 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003281 if (isalpha(quote) || quote == '_') {
3282 if (quote == 'u' || quote == 'U') {
3283 quote = *++s;
3284 unicode = 1;
3285 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003286 if (quote == 'b' || quote == 'B') {
3287 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003288 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003289 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003290 if (quote == 'r' || quote == 'R') {
3291 quote = *++s;
3292 rawmode = 1;
3293 }
3294 }
3295 if (quote != '\'' && quote != '\"') {
3296 PyErr_BadInternalCall();
3297 return NULL;
3298 }
3299 s++;
3300 len = strlen(s);
3301 if (len > INT_MAX) {
3302 PyErr_SetString(PyExc_OverflowError,
3303 "string to parse is too long");
3304 return NULL;
3305 }
3306 if (s[--len] != quote) {
3307 PyErr_BadInternalCall();
3308 return NULL;
3309 }
3310 if (len >= 4 && s[0] == quote && s[1] == quote) {
3311 s += 2;
3312 len -= 2;
3313 if (s[--len] != quote || s[--len] != quote) {
3314 PyErr_BadInternalCall();
3315 return NULL;
3316 }
3317 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003319 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003320 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003321 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003323 need_encoding = (c->c_encoding != NULL &&
3324 strcmp(c->c_encoding, "utf-8") != 0 &&
3325 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003326 if (rawmode || strchr(s, '\\') == NULL) {
3327 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003329 /* This should not happen - we never see any other
3330 encoding. */
3331 Py_FatalError(
3332 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003334 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3335 if (u == NULL)
3336 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003337 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003338 Py_DECREF(u);
3339 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003341 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003342 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003343 }
3344 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003346 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003347 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348}
3349
3350/* Build a Python string object out of a STRING atom. This takes care of
3351 * compile-time literal catenation, calling parsestr() on each piece, and
3352 * pasting the intermediate results together.
3353 */
3354static PyObject *
3355parsestrplus(struct compiling *c, const node *n)
3356{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003357 PyObject *v;
3358 int i;
3359 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003360 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003361 /* String literal concatenation */
3362 for (i = 1; i < NCH(n); i++) {
3363 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003364 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003365 if (s == NULL)
3366 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003367 if (PyString_Check(v) && PyString_Check(s)) {
3368 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003369 if (v == NULL)
3370 goto onError;
3371 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003373 else {
3374 PyObject *temp = PyUnicode_Concat(v, s);
3375 Py_DECREF(s);
3376 Py_DECREF(v);
3377 v = temp;
3378 if (v == NULL)
3379 goto onError;
3380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003382 }
3383 }
3384 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385
3386 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003387 Py_XDECREF(v);
3388 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389}