blob: 02aa2a772aa6c4fa80ca32413edee9e5eee0dc5b [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);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001297 errstr = PyObject_Str(value);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001298 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);
Benjamin Petersonc078f922008-11-21 22:27:24 +00001304 Py_DECREF(errstr);
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001305 } else {
1306 ast_error(n, "(unicode error) unknown error");
1307 }
1308 Py_DECREF(type);
1309 Py_DECREF(value);
1310 Py_XDECREF(tback);
1311 }
Georg Brandldfe5dc82008-01-07 18:16:36 +00001312#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001313 return NULL;
Guido van Rossumb6ac23c2007-07-18 17:19:14 +00001314 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001315 PyArena_AddPyObject(c->c_arena, str);
1316 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 }
1318 case NUMBER: {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001319 PyObject *pynum = parsenumber(c, STR(ch));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001320 if (!pynum)
1321 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001322
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001323 PyArena_AddPyObject(c->c_arena, pynum);
1324 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 }
1326 case LPAR: /* some parenthesized expressions */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001327 ch = CHILD(n, 1);
1328
1329 if (TYPE(ch) == RPAR)
1330 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1331
1332 if (TYPE(ch) == yield_expr)
1333 return ast_for_expr(c, ch);
1334
1335 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1336 return ast_for_genexp(c, ch);
1337
1338 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 case LSQB: /* list (or list comprehension) */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001340 ch = CHILD(n, 1);
1341
1342 if (TYPE(ch) == RSQB)
1343 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1344
1345 REQ(ch, listmaker);
1346 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1347 asdl_seq *elts = seq_for_testlist(c, ch);
1348 if (!elts)
1349 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001350
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001351 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1352 }
1353 else
1354 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 case LBRACE: {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001356 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1357 int i, size;
1358 asdl_seq *keys, *values;
1359
1360 ch = CHILD(n, 1);
1361 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1362 keys = asdl_seq_new(size, c->c_arena);
1363 if (!keys)
1364 return NULL;
1365
1366 values = asdl_seq_new(size, c->c_arena);
1367 if (!values)
1368 return NULL;
1369
1370 for (i = 0; i < NCH(ch); i += 4) {
1371 expr_ty expression;
1372
1373 expression = ast_for_expr(c, CHILD(ch, i));
1374 if (!expression)
1375 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001376
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001377 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001378
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001379 expression = ast_for_expr(c, CHILD(ch, i + 2));
1380 if (!expression)
1381 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001382
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001383 asdl_seq_SET(values, i / 4, expression);
1384 }
1385 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 }
1387 case BACKQUOTE: { /* repr */
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001388 expr_ty expression;
Benjamin Petersoncbd78132008-06-08 15:45:23 +00001389 if (Py_Py3kWarningFlag &&
1390 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001391 return NULL;
Christian Heimes6d8fb1a2007-11-23 13:25:31 +00001392 expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001393 if (!expression)
1394 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001395
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001396 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 }
1398 default:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001399 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1400 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 }
1402}
1403
1404static slice_ty
1405ast_for_slice(struct compiling *c, const node *n)
1406{
1407 node *ch;
1408 expr_ty lower = NULL, upper = NULL, step = NULL;
1409
1410 REQ(n, subscript);
1411
1412 /*
1413 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1414 sliceop: ':' [test]
1415 */
1416 ch = CHILD(n, 0);
1417 if (TYPE(ch) == DOT)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001418 return Ellipsis(c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419
1420 if (NCH(n) == 1 && TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001421 /* 'step' variable hold no significance in terms of being used over
1422 other vars */
1423 step = ast_for_expr(c, ch);
1424 if (!step)
1425 return NULL;
1426
1427 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 }
1429
1430 if (TYPE(ch) == test) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001431 lower = ast_for_expr(c, ch);
1432 if (!lower)
1433 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 }
1435
1436 /* If there's an upper bound it's in the second or third position. */
1437 if (TYPE(ch) == COLON) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001438 if (NCH(n) > 1) {
1439 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001441 if (TYPE(n2) == test) {
1442 upper = ast_for_expr(c, n2);
1443 if (!upper)
1444 return NULL;
1445 }
1446 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 } else if (NCH(n) > 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001448 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001450 if (TYPE(n2) == test) {
1451 upper = ast_for_expr(c, n2);
1452 if (!upper)
1453 return NULL;
1454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 }
1456
1457 ch = CHILD(n, NCH(n) - 1);
1458 if (TYPE(ch) == sliceop) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001459 if (NCH(ch) == 1) {
1460 /* No expression, so step is None */
1461 ch = CHILD(ch, 0);
1462 step = Name(new_identifier("None", c->c_arena), Load,
1463 LINENO(ch), ch->n_col_offset, c->c_arena);
1464 if (!step)
1465 return NULL;
1466 } else {
1467 ch = CHILD(ch, 1);
1468 if (TYPE(ch) == test) {
1469 step = ast_for_expr(c, ch);
1470 if (!step)
1471 return NULL;
1472 }
1473 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 }
1475
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001476 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477}
1478
1479static expr_ty
1480ast_for_binop(struct compiling *c, const node *n)
1481{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001482 /* Must account for a sequence of expressions.
1483 How should A op B op C by represented?
1484 BinOp(BinOp(A, op, B), op, C).
1485 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001487 int i, nops;
1488 expr_ty expr1, expr2, result;
1489 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001491 expr1 = ast_for_expr(c, CHILD(n, 0));
1492 if (!expr1)
1493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001495 expr2 = ast_for_expr(c, CHILD(n, 2));
1496 if (!expr2)
1497 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001499 newoperator = get_operator(CHILD(n, 1));
1500 if (!newoperator)
1501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001503 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1504 c->c_arena);
1505 if (!result)
1506 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001508 nops = (NCH(n) - 1) / 2;
1509 for (i = 1; i < nops; i++) {
1510 expr_ty tmp_result, tmp;
1511 const node* next_oper = CHILD(n, i * 2 + 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001513 newoperator = get_operator(next_oper);
1514 if (!newoperator)
1515 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001517 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1518 if (!tmp)
1519 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001521 tmp_result = BinOp(result, newoperator, tmp,
1522 LINENO(next_oper), next_oper->n_col_offset,
1523 c->c_arena);
Neal Norwitz3adac212007-10-05 03:41:19 +00001524 if (!tmp_result)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001525 return NULL;
1526 result = tmp_result;
1527 }
1528 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529}
1530
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001531static expr_ty
1532ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1533{
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001534 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1535 subscriptlist: subscript (',' subscript)* [',']
1536 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1537 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001538 REQ(n, trailer);
1539 if (TYPE(CHILD(n, 0)) == LPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001540 if (NCH(n) == 2)
1541 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1542 n->n_col_offset, c->c_arena);
1543 else
1544 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001545 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001546 else if (TYPE(CHILD(n, 0)) == DOT ) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001547 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1548 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001549 }
1550 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001551 REQ(CHILD(n, 0), LSQB);
1552 REQ(CHILD(n, 2), RSQB);
1553 n = CHILD(n, 1);
1554 if (NCH(n) == 1) {
1555 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1556 if (!slc)
1557 return NULL;
1558 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1559 c->c_arena);
1560 }
1561 else {
1562 /* The grammar is ambiguous here. The ambiguity is resolved
1563 by treating the sequence as a tuple literal if there are
1564 no slice features.
1565 */
1566 int j;
1567 slice_ty slc;
1568 expr_ty e;
1569 bool simple = true;
1570 asdl_seq *slices, *elts;
1571 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1572 if (!slices)
1573 return NULL;
1574 for (j = 0; j < NCH(n); j += 2) {
1575 slc = ast_for_slice(c, CHILD(n, j));
1576 if (!slc)
1577 return NULL;
1578 if (slc->kind != Index_kind)
1579 simple = false;
1580 asdl_seq_SET(slices, j / 2, slc);
1581 }
1582 if (!simple) {
1583 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1584 Load, LINENO(n), n->n_col_offset, c->c_arena);
1585 }
1586 /* extract Index values and put them in a Tuple */
1587 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1588 if (!elts)
1589 return NULL;
1590 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1591 slc = (slice_ty)asdl_seq_GET(slices, j);
1592 assert(slc->kind == Index_kind && slc->v.Index.value);
1593 asdl_seq_SET(elts, j, slc->v.Index.value);
1594 }
1595 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1596 if (!e)
1597 return NULL;
1598 return Subscript(left_expr, Index(e, c->c_arena),
1599 Load, LINENO(n), n->n_col_offset, c->c_arena);
1600 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001601 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001602}
1603
1604static expr_ty
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001605ast_for_factor(struct compiling *c, const node *n)
1606{
1607 node *pfactor, *ppower, *patom, *pnum;
1608 expr_ty expression;
1609
1610 /* If the unary - operator is applied to a constant, don't generate
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001611 a UNARY_NEGATIVE opcode. Just store the approriate value as a
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001612 constant. The peephole optimizer already does something like
1613 this but it doesn't handle the case where the constant is
1614 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1615 PyLongObject.
1616 */
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001617 if (TYPE(CHILD(n, 0)) == MINUS &&
1618 NCH(n) == 2 &&
1619 TYPE((pfactor = CHILD(n, 1))) == factor &&
1620 NCH(pfactor) == 1 &&
1621 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1622 NCH(ppower) == 1 &&
1623 TYPE((patom = CHILD(ppower, 0))) == atom &&
1624 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1625 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1626 if (s == NULL)
1627 return NULL;
1628 s[0] = '-';
1629 strcpy(s + 1, STR(pnum));
1630 PyObject_FREE(STR(pnum));
1631 STR(pnum) = s;
1632 return ast_for_atom(c, patom);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001633 }
1634
1635 expression = ast_for_expr(c, CHILD(n, 1));
1636 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001637 return NULL;
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001638
1639 switch (TYPE(CHILD(n, 0))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001640 case PLUS:
1641 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1642 c->c_arena);
1643 case MINUS:
1644 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1645 c->c_arena);
1646 case TILDE:
1647 return UnaryOp(Invert, expression, LINENO(n),
1648 n->n_col_offset, c->c_arena);
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001649 }
1650 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001651 TYPE(CHILD(n, 0)));
Neil Schemenauer6ec6ab02006-07-09 21:19:29 +00001652 return NULL;
1653}
1654
1655static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001656ast_for_power(struct compiling *c, const node *n)
1657{
1658 /* power: atom trailer* ('**' factor)*
1659 */
1660 int i;
1661 expr_ty e, tmp;
1662 REQ(n, power);
1663 e = ast_for_atom(c, CHILD(n, 0));
1664 if (!e)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001665 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001666 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001667 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001668 for (i = 1; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001669 node *ch = CHILD(n, i);
1670 if (TYPE(ch) != trailer)
1671 break;
1672 tmp = ast_for_trailer(c, ch, e);
1673 if (!tmp)
1674 return NULL;
1675 tmp->lineno = e->lineno;
1676 tmp->col_offset = e->col_offset;
1677 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001678 }
1679 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001680 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1681 if (!f)
1682 return NULL;
1683 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1684 if (!tmp)
1685 return NULL;
1686 e = tmp;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001687 }
1688 return e;
1689}
1690
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691/* Do not name a variable 'expr'! Will cause a compile error.
1692*/
1693
1694static expr_ty
1695ast_for_expr(struct compiling *c, const node *n)
1696{
1697 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001698 test: or_test ['if' or_test 'else' test] | lambdef
1699 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 and_test: not_test ('and' not_test)*
1701 not_test: 'not' not_test | comparison
1702 comparison: expr (comp_op expr)*
1703 expr: xor_expr ('|' xor_expr)*
1704 xor_expr: and_expr ('^' and_expr)*
1705 and_expr: shift_expr ('&' shift_expr)*
1706 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1707 arith_expr: term (('+'|'-') term)*
1708 term: factor (('*'|'/'|'%'|'//') factor)*
1709 factor: ('+'|'-'|'~') factor | power
1710 power: atom trailer* ('**' factor)*
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001711
1712 As well as modified versions that exist for backward compatibility,
1713 to explicitly allow:
1714 [ x for x in lambda: 0, lambda: 1 ]
1715 (which would be ambiguous without these extra rules)
1716
1717 old_test: or_test | old_lambdef
1718 old_lambdef: 'lambda' [vararglist] ':' old_test
1719
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 */
1721
1722 asdl_seq *seq;
1723 int i;
1724
1725 loop:
1726 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001727 case test:
1728 case old_test:
1729 if (TYPE(CHILD(n, 0)) == lambdef ||
1730 TYPE(CHILD(n, 0)) == old_lambdef)
1731 return ast_for_lambdef(c, CHILD(n, 0));
1732 else if (NCH(n) > 1)
1733 return ast_for_ifexpr(c, n);
1734 /* Fallthrough */
1735 case or_test:
1736 case and_test:
1737 if (NCH(n) == 1) {
1738 n = CHILD(n, 0);
1739 goto loop;
1740 }
1741 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1742 if (!seq)
1743 return NULL;
1744 for (i = 0; i < NCH(n); i += 2) {
1745 expr_ty e = ast_for_expr(c, CHILD(n, i));
1746 if (!e)
1747 return NULL;
1748 asdl_seq_SET(seq, i / 2, e);
1749 }
1750 if (!strcmp(STR(CHILD(n, 1)), "and"))
1751 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1752 c->c_arena);
1753 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1754 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1755 case not_test:
1756 if (NCH(n) == 1) {
1757 n = CHILD(n, 0);
1758 goto loop;
1759 }
1760 else {
1761 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1762 if (!expression)
1763 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001765 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1766 c->c_arena);
1767 }
1768 case comparison:
1769 if (NCH(n) == 1) {
1770 n = CHILD(n, 0);
1771 goto loop;
1772 }
1773 else {
1774 expr_ty expression;
1775 asdl_int_seq *ops;
1776 asdl_seq *cmps;
1777 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1778 if (!ops)
1779 return NULL;
1780 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1781 if (!cmps) {
1782 return NULL;
1783 }
1784 for (i = 1; i < NCH(n); i += 2) {
1785 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00001787 newoperator = ast_for_comp_op(c, CHILD(n, i));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001788 if (!newoperator) {
1789 return NULL;
1790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001792 expression = ast_for_expr(c, CHILD(n, i + 1));
1793 if (!expression) {
1794 return NULL;
1795 }
1796
1797 asdl_seq_SET(ops, i / 2, newoperator);
1798 asdl_seq_SET(cmps, i / 2, expression);
1799 }
1800 expression = ast_for_expr(c, CHILD(n, 0));
1801 if (!expression) {
1802 return NULL;
1803 }
1804
1805 return Compare(expression, ops, cmps, LINENO(n),
1806 n->n_col_offset, c->c_arena);
1807 }
1808 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001810 /* The next five cases all handle BinOps. The main body of code
1811 is the same in each case, but the switch turned inside out to
1812 reuse the code for each type of operator.
1813 */
1814 case expr:
1815 case xor_expr:
1816 case and_expr:
1817 case shift_expr:
1818 case arith_expr:
1819 case term:
1820 if (NCH(n) == 1) {
1821 n = CHILD(n, 0);
1822 goto loop;
1823 }
1824 return ast_for_binop(c, n);
1825 case yield_expr: {
1826 expr_ty exp = NULL;
1827 if (NCH(n) == 2) {
1828 exp = ast_for_testlist(c, CHILD(n, 1));
1829 if (!exp)
1830 return NULL;
1831 }
1832 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1833 }
1834 case factor:
1835 if (NCH(n) == 1) {
1836 n = CHILD(n, 0);
1837 goto loop;
1838 }
1839 return ast_for_factor(c, n);
1840 case power:
1841 return ast_for_power(c, n);
1842 default:
1843 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1844 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001846 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 return NULL;
1848}
1849
1850static expr_ty
1851ast_for_call(struct compiling *c, const node *n, expr_ty func)
1852{
1853 /*
1854 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001855 | '**' test)
1856 argument: [test '='] test [gen_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 */
1858
1859 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001860 asdl_seq *args;
1861 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 expr_ty vararg = NULL, kwarg = NULL;
1863
1864 REQ(n, arglist);
1865
1866 nargs = 0;
1867 nkeywords = 0;
1868 ngens = 0;
1869 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001870 node *ch = CHILD(n, i);
1871 if (TYPE(ch) == argument) {
1872 if (NCH(ch) == 1)
1873 nargs++;
1874 else if (TYPE(CHILD(ch, 1)) == gen_for)
1875 ngens++;
1876 else
1877 nkeywords++;
1878 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 }
1880 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001881 ast_error(n, "Generator expression must be parenthesized "
1882 "if not sole argument");
1883 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 }
1885
1886 if (nargs + nkeywords + ngens > 255) {
1887 ast_error(n, "more than 255 arguments");
1888 return NULL;
1889 }
1890
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 if (!args)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001893 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 if (!keywords)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001896 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 nargs = 0;
1898 nkeywords = 0;
1899 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001900 node *ch = CHILD(n, i);
1901 if (TYPE(ch) == argument) {
1902 expr_ty e;
1903 if (NCH(ch) == 1) {
1904 if (nkeywords) {
1905 ast_error(CHILD(ch, 0),
1906 "non-keyword arg after keyword arg");
1907 return NULL;
1908 }
Benjamin Peterson80f0ed52008-08-19 19:52:46 +00001909 if (vararg) {
1910 ast_error(CHILD(ch, 0),
1911 "only named arguments may follow *expression");
1912 return NULL;
1913 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001914 e = ast_for_expr(c, CHILD(ch, 0));
1915 if (!e)
1916 return NULL;
1917 asdl_seq_SET(args, nargs++, e);
1918 }
1919 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1920 e = ast_for_genexp(c, ch);
1921 if (!e)
1922 return NULL;
1923 asdl_seq_SET(args, nargs++, e);
1924 }
1925 else {
1926 keyword_ty kw;
1927 identifier key;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001928 int k;
1929 char *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001931 /* CHILD(ch, 0) is test, but must be an identifier? */
1932 e = ast_for_expr(c, CHILD(ch, 0));
1933 if (!e)
1934 return NULL;
1935 /* f(lambda x: x[0] = 3) ends up getting parsed with
1936 * LHS test = lambda x: x[0], and RHS test = 3.
1937 * SF bug 132313 points out that complaining about a keyword
1938 * then is very confusing.
1939 */
1940 if (e->kind == Lambda_kind) {
1941 ast_error(CHILD(ch, 0),
1942 "lambda cannot contain assignment");
1943 return NULL;
1944 } else if (e->kind != Name_kind) {
1945 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1946 return NULL;
1947 }
1948 key = e->v.Name.id;
Benjamin Petersond5efd202008-06-08 22:52:37 +00001949 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
Georg Brandle06cf452007-06-07 13:23:24 +00001950 return NULL;
Benjamin Peterson175e4d92008-07-01 19:34:52 +00001951 for (k = 0; k < nkeywords; k++) {
1952 tmp = PyString_AS_STRING(
1953 ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
1954 if (!strcmp(tmp, PyString_AS_STRING(key))) {
1955 ast_error(CHILD(ch, 0), "keyword argument repeated");
1956 return NULL;
1957 }
1958 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001959 e = ast_for_expr(c, CHILD(ch, 2));
1960 if (!e)
1961 return NULL;
1962 kw = keyword(key, e, c->c_arena);
1963 if (!kw)
1964 return NULL;
1965 asdl_seq_SET(keywords, nkeywords++, kw);
1966 }
1967 }
1968 else if (TYPE(ch) == STAR) {
1969 vararg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001970 if (!vararg)
1971 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001972 i++;
1973 }
1974 else if (TYPE(ch) == DOUBLESTAR) {
1975 kwarg = ast_for_expr(c, CHILD(n, i+1));
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +00001976 if (!kwarg)
1977 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001978 i++;
1979 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980 }
1981
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001982 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1983 func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984}
1985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001987ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001989 /* testlist_gexp: test (',' test)* [','] */
1990 /* testlist: test (',' test)* [','] */
1991 /* testlist_safe: test (',' test)+ [','] */
1992 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001994 if (TYPE(n) == testlist_gexp) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001995 if (NCH(n) > 1)
1996 assert(TYPE(CHILD(n, 1)) != gen_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001997 }
1998 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00001999 assert(TYPE(n) == testlist ||
2000 TYPE(n) == testlist_safe ||
2001 TYPE(n) == testlist1);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002002 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 if (NCH(n) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002004 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002006 asdl_seq *tmp = seq_for_testlist(c, n);
2007 if (!tmp)
2008 return NULL;
2009 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002011}
2012
2013static expr_ty
2014ast_for_testlist_gexp(struct compiling *c, const node* n)
2015{
2016 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2017 /* argument: test [ gen_for ] */
2018 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002019 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002020 return ast_for_genexp(c, n);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002021 return ast_for_testlist(c, n);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002022}
2023
2024/* like ast_for_testlist() but returns a sequence */
2025static asdl_seq*
2026ast_for_class_bases(struct compiling *c, const node* n)
2027{
2028 /* testlist: test (',' test)* [','] */
2029 assert(NCH(n) > 0);
2030 REQ(n, testlist);
2031 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002032 expr_ty base;
2033 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2034 if (!bases)
2035 return NULL;
2036 base = ast_for_expr(c, CHILD(n, 0));
2037 if (!base)
2038 return NULL;
2039 asdl_seq_SET(bases, 0, base);
2040 return bases;
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002041 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002042
2043 return seq_for_testlist(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044}
2045
2046static stmt_ty
2047ast_for_expr_stmt(struct compiling *c, const node *n)
2048{
2049 REQ(n, expr_stmt);
2050 /* expr_stmt: testlist (augassign (yield_expr|testlist)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002051 | ('=' (yield_expr|testlist))*)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 testlist: test (',' test)* [',']
2053 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002054 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 test: ... here starts the operator precendence dance
2056 */
2057
2058 if (NCH(n) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002059 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2060 if (!e)
2061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002063 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 }
2065 else if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002066 expr_ty expr1, expr2;
2067 operator_ty newoperator;
2068 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002070 expr1 = ast_for_testlist(c, ch);
2071 if (!expr1)
2072 return NULL;
2073 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2074 switch (expr1->kind) {
2075 case GeneratorExp_kind:
2076 ast_error(ch, "augmented assignment to generator "
2077 "expression not possible");
2078 return NULL;
2079 case Yield_kind:
2080 ast_error(ch, "augmented assignment to yield "
2081 "expression not possible");
2082 return NULL;
2083 case Name_kind: {
Christian Heimes593daf52008-05-26 12:51:38 +00002084 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
Benjamin Petersond5efd202008-06-08 22:52:37 +00002085 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
2086 !forbidden_check(c, ch, var_name))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002087 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002088 break;
2089 }
2090 case Attribute_kind:
2091 case Subscript_kind:
2092 break;
2093 default:
2094 ast_error(ch, "illegal expression for augmented "
2095 "assignment");
2096 return NULL;
2097 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002098 if(!set_context(c, expr1, Store, ch))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002099 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002101 ch = CHILD(n, 2);
2102 if (TYPE(ch) == testlist)
2103 expr2 = ast_for_testlist(c, ch);
2104 else
2105 expr2 = ast_for_expr(c, ch);
2106 if (!expr2)
2107 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002109 newoperator = ast_for_augassign(c, CHILD(n, 1));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002110 if (!newoperator)
2111 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002113 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2114 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 }
2116 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002117 int i;
2118 asdl_seq *targets;
2119 node *value;
2120 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002122 /* a normal assignment */
2123 REQ(CHILD(n, 1), EQUAL);
2124 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2125 if (!targets)
2126 return NULL;
2127 for (i = 0; i < NCH(n) - 2; i += 2) {
2128 expr_ty e;
2129 node *ch = CHILD(n, i);
2130 if (TYPE(ch) == yield_expr) {
2131 ast_error(ch, "assignment to yield expression not possible");
2132 return NULL;
2133 }
2134 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002136 /* set context to assign */
2137 if (!e)
2138 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002140 if (!set_context(c, e, Store, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002141 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002143 asdl_seq_SET(targets, i / 2, e);
2144 }
2145 value = CHILD(n, NCH(n) - 1);
2146 if (TYPE(value) == testlist)
2147 expression = ast_for_testlist(c, value);
2148 else
2149 expression = ast_for_expr(c, value);
2150 if (!expression)
2151 return NULL;
2152 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2153 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155}
2156
2157static stmt_ty
2158ast_for_print_stmt(struct compiling *c, const node *n)
2159{
2160 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002161 | '>>' test [ (',' test)+ [','] ] )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 */
2163 expr_ty dest = NULL, expression;
2164 asdl_seq *seq;
2165 bool nl;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002166 int i, j, start = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167
2168 REQ(n, print_stmt);
2169 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002170 dest = ast_for_expr(c, CHILD(n, 2));
2171 if (!dest)
2172 return NULL;
2173 start = 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002175 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002177 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002178 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002179 expression = ast_for_expr(c, CHILD(n, i));
2180 if (!expression)
2181 return NULL;
2182 asdl_seq_SET(seq, j, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 }
2184 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002185 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186}
2187
2188static asdl_seq *
Martin v. Löwis28457502006-04-11 09:17:27 +00002189ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190{
2191 asdl_seq *seq;
2192 int i;
2193 expr_ty e;
2194
2195 REQ(n, exprlist);
2196
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002197 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002201 e = ast_for_expr(c, CHILD(n, i));
2202 if (!e)
2203 return NULL;
2204 asdl_seq_SET(seq, i / 2, e);
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002205 if (context && !set_context(c, e, context, CHILD(n, i)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 }
2208 return seq;
2209}
2210
2211static stmt_ty
2212ast_for_del_stmt(struct compiling *c, const node *n)
2213{
2214 asdl_seq *expr_list;
2215
2216 /* del_stmt: 'del' exprlist */
2217 REQ(n, del_stmt);
2218
2219 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2220 if (!expr_list)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002221 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002222 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223}
2224
2225static stmt_ty
2226ast_for_flow_stmt(struct compiling *c, const node *n)
2227{
2228 /*
2229 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002230 | yield_stmt
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231 break_stmt: 'break'
2232 continue_stmt: 'continue'
2233 return_stmt: 'return' [testlist]
2234 yield_stmt: yield_expr
2235 yield_expr: 'yield' testlist
2236 raise_stmt: 'raise' [test [',' test [',' test]]]
2237 */
2238 node *ch;
2239
2240 REQ(n, flow_stmt);
2241 ch = CHILD(n, 0);
2242 switch (TYPE(ch)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002243 case break_stmt:
2244 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2245 case continue_stmt:
2246 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2247 case yield_stmt: { /* will reduce to yield_expr */
2248 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2249 if (!exp)
2250 return NULL;
2251 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2252 }
2253 case return_stmt:
2254 if (NCH(ch) == 1)
2255 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2256 else {
2257 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2258 if (!expression)
2259 return NULL;
2260 return Return(expression, LINENO(n), n->n_col_offset,
2261 c->c_arena);
2262 }
2263 case raise_stmt:
2264 if (NCH(ch) == 1)
2265 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2266 c->c_arena);
2267 else if (NCH(ch) == 2) {
2268 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2269 if (!expression)
2270 return NULL;
2271 return Raise(expression, NULL, NULL, LINENO(n),
2272 n->n_col_offset, c->c_arena);
2273 }
2274 else if (NCH(ch) == 4) {
2275 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002277 expr1 = ast_for_expr(c, CHILD(ch, 1));
2278 if (!expr1)
2279 return NULL;
2280 expr2 = ast_for_expr(c, CHILD(ch, 3));
2281 if (!expr2)
2282 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002284 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2285 c->c_arena);
2286 }
2287 else if (NCH(ch) == 6) {
2288 expr_ty expr1, expr2, expr3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002290 expr1 = ast_for_expr(c, CHILD(ch, 1));
2291 if (!expr1)
2292 return NULL;
2293 expr2 = ast_for_expr(c, CHILD(ch, 3));
2294 if (!expr2)
2295 return NULL;
2296 expr3 = ast_for_expr(c, CHILD(ch, 5));
2297 if (!expr3)
2298 return NULL;
2299
2300 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2301 c->c_arena);
2302 }
2303 default:
2304 PyErr_Format(PyExc_SystemError,
2305 "unexpected flow_stmt: %d", TYPE(ch));
2306 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002308
2309 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2310 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311}
2312
2313static alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002314alias_for_import_name(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315{
2316 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002317 import_as_name: NAME ['as' NAME]
2318 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319 dotted_name: NAME ('.' NAME)*
2320 */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002321 PyObject *str;
2322
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323 loop:
2324 switch (TYPE(n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002325 case import_as_name:
2326 str = NULL;
2327 if (NCH(n) == 3) {
2328 str = NEW_IDENTIFIER(CHILD(n, 2));
2329 }
2330 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2331 case dotted_as_name:
2332 if (NCH(n) == 1) {
2333 n = CHILD(n, 0);
2334 goto loop;
2335 }
2336 else {
2337 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2338 if (!a)
2339 return NULL;
2340 assert(!a->asname);
2341 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2342 return a;
2343 }
2344 break;
2345 case dotted_name:
2346 if (NCH(n) == 1)
2347 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2348 else {
2349 /* Create a string of the form "a.b.c" */
2350 int i;
2351 size_t len;
2352 char *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002353
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002354 len = 0;
2355 for (i = 0; i < NCH(n); i += 2)
2356 /* length of string plus one for the dot */
2357 len += strlen(STR(CHILD(n, i))) + 1;
2358 len--; /* the last name doesn't have a dot */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002359 str = PyString_FromStringAndSize(NULL, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002360 if (!str)
2361 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002362 s = PyString_AS_STRING(str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002363 if (!s)
2364 return NULL;
2365 for (i = 0; i < NCH(n); i += 2) {
2366 char *sch = STR(CHILD(n, i));
2367 strcpy(s, STR(CHILD(n, i)));
2368 s += strlen(sch);
2369 *s++ = '.';
2370 }
2371 --s;
2372 *s = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002373 PyString_InternInPlace(&str);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002374 PyArena_AddPyObject(c->c_arena, str);
2375 return alias(str, NULL, c->c_arena);
2376 }
2377 break;
2378 case STAR:
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002379 str = PyString_InternFromString("*");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002380 PyArena_AddPyObject(c->c_arena, str);
2381 return alias(str, NULL, c->c_arena);
2382 default:
2383 PyErr_Format(PyExc_SystemError,
2384 "unexpected import name: %d", TYPE(n));
2385 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002387
2388 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 return NULL;
2390}
2391
2392static stmt_ty
2393ast_for_import_stmt(struct compiling *c, const node *n)
2394{
2395 /*
2396 import_stmt: import_name | import_from
2397 import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002398 import_from: 'from' ('.'* dotted_name | '.') 'import'
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002399 ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002401 int lineno;
2402 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 int i;
2404 asdl_seq *aliases;
2405
2406 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002407 lineno = LINENO(n);
2408 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002410 if (TYPE(n) == import_name) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002411 n = CHILD(n, 1);
2412 REQ(n, dotted_as_names);
2413 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2414 if (!aliases)
2415 return NULL;
2416 for (i = 0; i < NCH(n); i += 2) {
2417 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2418 if (!import_alias)
2419 return NULL;
2420 asdl_seq_SET(aliases, i / 2, import_alias);
2421 }
2422 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002424 else if (TYPE(n) == import_from) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002425 int n_children;
2426 int idx, ndots = 0;
2427 alias_ty mod = NULL;
2428 identifier modname;
2429
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002430 /* Count the number of dots (for relative imports) and check for the
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002431 optional module name */
2432 for (idx = 1; idx < NCH(n); idx++) {
2433 if (TYPE(CHILD(n, idx)) == dotted_name) {
2434 mod = alias_for_import_name(c, CHILD(n, idx));
2435 idx++;
2436 break;
2437 } else if (TYPE(CHILD(n, idx)) != DOT) {
2438 break;
2439 }
2440 ndots++;
2441 }
2442 idx++; /* skip over the 'import' keyword */
2443 switch (TYPE(CHILD(n, idx))) {
2444 case STAR:
2445 /* from ... import * */
2446 n = CHILD(n, idx);
2447 n_children = 1;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002448 break;
2449 case LPAR:
2450 /* from ... import (x, y, z) */
2451 n = CHILD(n, idx + 1);
2452 n_children = NCH(n);
2453 break;
2454 case import_as_names:
2455 /* from ... import x, y, z */
2456 n = CHILD(n, idx);
2457 n_children = NCH(n);
2458 if (n_children % 2 == 0) {
2459 ast_error(n, "trailing comma not allowed without"
2460 " surrounding parentheses");
2461 return NULL;
2462 }
2463 break;
2464 default:
2465 ast_error(n, "Unexpected node-type in from-import");
2466 return NULL;
2467 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002469 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2470 if (!aliases)
2471 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002473 /* handle "from ... import *" special b/c there's no children */
2474 if (TYPE(n) == STAR) {
2475 alias_ty import_alias = alias_for_import_name(c, n);
2476 if (!import_alias)
2477 return NULL;
2478 asdl_seq_SET(aliases, 0, import_alias);
2479 }
2480 else {
2481 for (i = 0; i < NCH(n); i += 2) {
2482 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2483 if (!import_alias)
2484 return NULL;
2485 asdl_seq_SET(aliases, i / 2, import_alias);
2486 }
2487 }
2488 if (mod != NULL)
2489 modname = mod->name;
2490 else
2491 modname = new_identifier("", c->c_arena);
2492 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2493 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 }
Neal Norwitz79792652005-11-14 04:25:03 +00002495 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002496 "unknown import statement: starts with command '%s'",
2497 STR(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 return NULL;
2499}
2500
2501static stmt_ty
2502ast_for_global_stmt(struct compiling *c, const node *n)
2503{
2504 /* global_stmt: 'global' NAME (',' NAME)* */
2505 identifier name;
2506 asdl_seq *s;
2507 int i;
2508
2509 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002510 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002514 name = NEW_IDENTIFIER(CHILD(n, i));
2515 if (!name)
2516 return NULL;
2517 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002519 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520}
2521
2522static stmt_ty
2523ast_for_exec_stmt(struct compiling *c, const node *n)
2524{
2525 expr_ty expr1, globals = NULL, locals = NULL;
2526 int n_children = NCH(n);
2527 if (n_children != 2 && n_children != 4 && n_children != 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002528 PyErr_Format(PyExc_SystemError,
2529 "poorly formed 'exec' statement: %d parts to statement",
2530 n_children);
2531 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 }
2533
2534 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2535 REQ(n, exec_stmt);
2536 expr1 = ast_for_expr(c, CHILD(n, 1));
2537 if (!expr1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002538 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 if (n_children >= 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002540 globals = ast_for_expr(c, CHILD(n, 3));
2541 if (!globals)
2542 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 }
2544 if (n_children == 6) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002545 locals = ast_for_expr(c, CHILD(n, 5));
2546 if (!locals)
2547 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 }
2549
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002550 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2551 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552}
2553
2554static stmt_ty
2555ast_for_assert_stmt(struct compiling *c, const node *n)
2556{
2557 /* assert_stmt: 'assert' test [',' test] */
2558 REQ(n, assert_stmt);
2559 if (NCH(n) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002560 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2561 if (!expression)
2562 return NULL;
2563 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2564 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 }
2566 else if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002567 expr_ty expr1, expr2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002569 expr1 = ast_for_expr(c, CHILD(n, 1));
2570 if (!expr1)
2571 return NULL;
2572 expr2 = ast_for_expr(c, CHILD(n, 3));
2573 if (!expr2)
2574 return NULL;
2575
2576 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 }
Neal Norwitz79792652005-11-14 04:25:03 +00002578 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002579 "improper number of parts to 'assert' statement: %d",
2580 NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 return NULL;
2582}
2583
2584static asdl_seq *
2585ast_for_suite(struct compiling *c, const node *n)
2586{
2587 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002588 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 stmt_ty s;
2590 int i, total, num, end, pos = 0;
2591 node *ch;
2592
2593 REQ(n, suite);
2594
2595 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002596 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 if (!seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002598 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002600 n = CHILD(n, 0);
2601 /* simple_stmt always ends with a NEWLINE,
2602 and may have a trailing SEMI
2603 */
2604 end = NCH(n) - 1;
2605 if (TYPE(CHILD(n, end - 1)) == SEMI)
2606 end--;
2607 /* loop by 2 to skip semi-colons */
2608 for (i = 0; i < end; i += 2) {
2609 ch = CHILD(n, i);
2610 s = ast_for_stmt(c, ch);
2611 if (!s)
2612 return NULL;
2613 asdl_seq_SET(seq, pos++, s);
2614 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 }
2616 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002617 for (i = 2; i < (NCH(n) - 1); i++) {
2618 ch = CHILD(n, i);
2619 REQ(ch, stmt);
2620 num = num_stmts(ch);
2621 if (num == 1) {
2622 /* small_stmt or compound_stmt with only one child */
2623 s = ast_for_stmt(c, ch);
2624 if (!s)
2625 return NULL;
2626 asdl_seq_SET(seq, pos++, s);
2627 }
2628 else {
2629 int j;
2630 ch = CHILD(ch, 0);
2631 REQ(ch, simple_stmt);
2632 for (j = 0; j < NCH(ch); j += 2) {
2633 /* statement terminates with a semi-colon ';' */
2634 if (NCH(CHILD(ch, j)) == 0) {
2635 assert((j + 1) == NCH(ch));
2636 break;
2637 }
2638 s = ast_for_stmt(c, CHILD(ch, j));
2639 if (!s)
2640 return NULL;
2641 asdl_seq_SET(seq, pos++, s);
2642 }
2643 }
2644 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 }
2646 assert(pos == seq->size);
2647 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648}
2649
2650static stmt_ty
2651ast_for_if_stmt(struct compiling *c, const node *n)
2652{
2653 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2654 ['else' ':' suite]
2655 */
2656 char *s;
2657
2658 REQ(n, if_stmt);
2659
2660 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002661 expr_ty expression;
2662 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002664 expression = ast_for_expr(c, CHILD(n, 1));
2665 if (!expression)
2666 return NULL;
2667 suite_seq = ast_for_suite(c, CHILD(n, 3));
2668 if (!suite_seq)
2669 return NULL;
2670
2671 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2672 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002674
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 s = STR(CHILD(n, 4));
2676 /* s[2], the third character in the string, will be
2677 's' for el_s_e, or
2678 'i' for el_i_f
2679 */
2680 if (s[2] == 's') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002681 expr_ty expression;
2682 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002684 expression = ast_for_expr(c, CHILD(n, 1));
2685 if (!expression)
2686 return NULL;
2687 seq1 = ast_for_suite(c, CHILD(n, 3));
2688 if (!seq1)
2689 return NULL;
2690 seq2 = ast_for_suite(c, CHILD(n, 6));
2691 if (!seq2)
2692 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002694 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2695 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 }
2697 else if (s[2] == 'i') {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002698 int i, n_elif, has_else = 0;
2699 expr_ty expression;
2700 asdl_seq *suite_seq;
2701 asdl_seq *orelse = NULL;
2702 n_elif = NCH(n) - 4;
2703 /* must reference the child n_elif+1 since 'else' token is third,
2704 not fourth, child from the end. */
2705 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2706 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2707 has_else = 1;
2708 n_elif -= 3;
2709 }
2710 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002712 if (has_else) {
2713 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002715 orelse = asdl_seq_new(1, c->c_arena);
2716 if (!orelse)
2717 return NULL;
2718 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2719 if (!expression)
2720 return NULL;
2721 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2722 if (!suite_seq)
2723 return NULL;
2724 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2725 if (!suite_seq2)
2726 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002728 asdl_seq_SET(orelse, 0,
2729 If(expression, suite_seq, suite_seq2,
2730 LINENO(CHILD(n, NCH(n) - 6)),
2731 CHILD(n, NCH(n) - 6)->n_col_offset,
2732 c->c_arena));
2733 /* the just-created orelse handled the last elif */
2734 n_elif--;
2735 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002737 for (i = 0; i < n_elif; i++) {
2738 int off = 5 + (n_elif - i - 1) * 4;
2739 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2740 if (!newobj)
2741 return NULL;
2742 expression = ast_for_expr(c, CHILD(n, off));
2743 if (!expression)
2744 return NULL;
2745 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2746 if (!suite_seq)
2747 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002749 asdl_seq_SET(newobj, 0,
2750 If(expression, suite_seq, orelse,
2751 LINENO(CHILD(n, off)),
2752 CHILD(n, off)->n_col_offset, c->c_arena));
2753 orelse = newobj;
2754 }
2755 expression = ast_for_expr(c, CHILD(n, 1));
2756 if (!expression)
2757 return NULL;
2758 suite_seq = ast_for_suite(c, CHILD(n, 3));
2759 if (!suite_seq)
2760 return NULL;
2761 return If(expression, suite_seq, orelse,
2762 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002764
2765 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002766 "unexpected token in 'if' statement: %s", s);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002767 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768}
2769
2770static stmt_ty
2771ast_for_while_stmt(struct compiling *c, const node *n)
2772{
2773 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2774 REQ(n, while_stmt);
2775
2776 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002777 expr_ty expression;
2778 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002780 expression = ast_for_expr(c, CHILD(n, 1));
2781 if (!expression)
2782 return NULL;
2783 suite_seq = ast_for_suite(c, CHILD(n, 3));
2784 if (!suite_seq)
2785 return NULL;
2786 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2787 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 }
2789 else if (NCH(n) == 7) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002790 expr_ty expression;
2791 asdl_seq *seq1, *seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002793 expression = ast_for_expr(c, CHILD(n, 1));
2794 if (!expression)
2795 return NULL;
2796 seq1 = ast_for_suite(c, CHILD(n, 3));
2797 if (!seq1)
2798 return NULL;
2799 seq2 = ast_for_suite(c, CHILD(n, 6));
2800 if (!seq2)
2801 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002803 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2804 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002806
2807 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002808 "wrong number of tokens for 'while' statement: %d",
2809 NCH(n));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002810 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811}
2812
2813static stmt_ty
2814ast_for_for_stmt(struct compiling *c, const node *n)
2815{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002816 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 expr_ty expression;
2818 expr_ty target;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002819 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2821 REQ(n, for_stmt);
2822
2823 if (NCH(n) == 9) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002824 seq = ast_for_suite(c, CHILD(n, 8));
2825 if (!seq)
2826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 }
2828
Neal Norwitzedef2be2006-07-12 05:26:17 +00002829 node_target = CHILD(n, 1);
2830 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002831 if (!_target)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002832 return NULL;
Neal Norwitzedef2be2006-07-12 05:26:17 +00002833 /* Check the # of children rather than the length of _target, since
2834 for x, in ... has 1 element in _target, but still requires a Tuple. */
2835 if (NCH(node_target) == 1)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002836 target = (expr_ty)asdl_seq_GET(_target, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002838 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002840 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002841 if (!expression)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002842 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002844 if (!suite_seq)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002845 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002847 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002848 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849}
2850
2851static excepthandler_ty
2852ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2853{
Collin Winter62903052007-05-18 23:11:24 +00002854 /* except_clause: 'except' [test [(',' | 'as') test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 REQ(exc, except_clause);
2856 REQ(body, suite);
2857
2858 if (NCH(exc) == 1) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002859 asdl_seq *suite_seq = ast_for_suite(c, body);
2860 if (!suite_seq)
2861 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862
Georg Brandla48f3ab2008-03-30 06:40:17 +00002863 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002864 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 }
2866 else if (NCH(exc) == 2) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002867 expr_ty expression;
2868 asdl_seq *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002870 expression = ast_for_expr(c, CHILD(exc, 1));
2871 if (!expression)
2872 return NULL;
2873 suite_seq = ast_for_suite(c, body);
2874 if (!suite_seq)
2875 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876
Georg Brandla48f3ab2008-03-30 06:40:17 +00002877 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002878 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 }
2880 else if (NCH(exc) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002881 asdl_seq *suite_seq;
2882 expr_ty expression;
2883 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2884 if (!e)
2885 return NULL;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00002886 if (!set_context(c, e, Store, CHILD(exc, 3)))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002887 return NULL;
2888 expression = ast_for_expr(c, CHILD(exc, 1));
2889 if (!expression)
2890 return NULL;
2891 suite_seq = ast_for_suite(c, body);
2892 if (!suite_seq)
2893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894
Georg Brandla48f3ab2008-03-30 06:40:17 +00002895 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002896 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002898
2899 PyErr_Format(PyExc_SystemError,
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002900 "wrong number of children for 'except' clause: %d",
2901 NCH(exc));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002902 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903}
2904
2905static stmt_ty
2906ast_for_try_stmt(struct compiling *c, const node *n)
2907{
Neal Norwitzf599f422005-12-17 21:33:47 +00002908 const int nch = NCH(n);
2909 int n_except = (nch - 3)/3;
2910 asdl_seq *body, *orelse = NULL, *finally = NULL;
2911
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 REQ(n, try_stmt);
2913
Neal Norwitzf599f422005-12-17 21:33:47 +00002914 body = ast_for_suite(c, CHILD(n, 2));
2915 if (body == NULL)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002916 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917
Neal Norwitzf599f422005-12-17 21:33:47 +00002918 if (TYPE(CHILD(n, nch - 3)) == NAME) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002919 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2920 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2921 /* we can assume it's an "else",
2922 because nch >= 9 for try-else-finally and
2923 it would otherwise have a type of except_clause */
2924 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2925 if (orelse == NULL)
2926 return NULL;
2927 n_except--;
2928 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002930 finally = ast_for_suite(c, CHILD(n, nch - 1));
2931 if (finally == NULL)
2932 return NULL;
2933 n_except--;
2934 }
2935 else {
2936 /* we can assume it's an "else",
2937 otherwise it would have a type of except_clause */
2938 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2939 if (orelse == NULL)
2940 return NULL;
2941 n_except--;
2942 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002944 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002945 ast_error(n, "malformed 'try' statement");
2946 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002948
2949 if (n_except > 0) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002950 int i;
2951 stmt_ty except_st;
2952 /* process except statements to create a try ... except */
2953 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2954 if (handlers == NULL)
2955 return NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00002956
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002957 for (i = 0; i < n_except; i++) {
2958 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2959 CHILD(n, 5 + i * 3));
2960 if (!e)
2961 return NULL;
2962 asdl_seq_SET(handlers, i, e);
2963 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002964
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002965 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2966 n->n_col_offset, c->c_arena);
2967 if (!finally)
2968 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002969
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00002970 /* if a 'finally' is present too, we nest the TryExcept within a
2971 TryFinally to emulate try ... except ... finally */
2972 body = asdl_seq_new(1, c->c_arena);
2973 if (body == NULL)
2974 return NULL;
2975 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002976 }
2977
2978 /* must be a try ... finally (except clauses are in body, if any exist) */
2979 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002980 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981}
2982
Guido van Rossumc2e20742006-02-27 22:32:47 +00002983static expr_ty
2984ast_for_with_var(struct compiling *c, const node *n)
2985{
2986 REQ(n, with_var);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002987 return ast_for_expr(c, CHILD(n, 1));
2988}
2989
2990/* with_stmt: 'with' test [ with_var ] ':' suite */
2991static stmt_ty
2992ast_for_with_stmt(struct compiling *c, const node *n)
2993{
2994 expr_ty context_expr, optional_vars = NULL;
2995 int suite_index = 3; /* skip 'with', test, and ':' */
2996 asdl_seq *suite_seq;
2997
2998 assert(TYPE(n) == with_stmt);
2999 context_expr = ast_for_expr(c, CHILD(n, 1));
Collin Winter77c67bd2007-03-16 04:11:30 +00003000 if (!context_expr)
3001 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003002 if (TYPE(CHILD(n, 2)) == with_var) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003003 optional_vars = ast_for_with_var(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003004
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003005 if (!optional_vars) {
3006 return NULL;
3007 }
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003008 if (!set_context(c, optional_vars, Store, n)) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003009 return NULL;
3010 }
3011 suite_index = 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003012 }
3013
3014 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
3015 if (!suite_seq) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003016 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003017 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003018 return With(context_expr, optional_vars, suite_seq, LINENO(n),
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003019 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003020}
3021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022static stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00003023ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024{
3025 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 asdl_seq *bases, *s;
3027
3028 REQ(n, classdef);
3029
Benjamin Petersond5efd202008-06-08 22:52:37 +00003030 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003031 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032
3033 if (NCH(n) == 4) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003034 s = ast_for_suite(c, CHILD(n, 3));
3035 if (!s)
3036 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003037 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3038 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 }
3040 /* check for empty base list */
3041 if (TYPE(CHILD(n,3)) == RPAR) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003042 s = ast_for_suite(c, CHILD(n,5));
3043 if (!s)
3044 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003045 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3046 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047 }
3048
3049 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003050 bases = ast_for_class_bases(c, CHILD(n, 3));
3051 if (!bases)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003052 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053
3054 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003055 if (!s)
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003056 return NULL;
Christian Heimes5224d282008-02-23 15:01:05 +00003057 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3058 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059}
3060
3061static stmt_ty
3062ast_for_stmt(struct compiling *c, const node *n)
3063{
3064 if (TYPE(n) == stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003065 assert(NCH(n) == 1);
3066 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003067 }
3068 if (TYPE(n) == simple_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003069 assert(num_stmts(n) == 1);
3070 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 }
3072 if (TYPE(n) == small_stmt) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003073 REQ(n, small_stmt);
3074 n = CHILD(n, 0);
3075 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3076 | flow_stmt | import_stmt | global_stmt | exec_stmt
3077 | assert_stmt
3078 */
3079 switch (TYPE(n)) {
3080 case expr_stmt:
3081 return ast_for_expr_stmt(c, n);
3082 case print_stmt:
3083 return ast_for_print_stmt(c, n);
3084 case del_stmt:
3085 return ast_for_del_stmt(c, n);
3086 case pass_stmt:
3087 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3088 case flow_stmt:
3089 return ast_for_flow_stmt(c, n);
3090 case import_stmt:
3091 return ast_for_import_stmt(c, n);
3092 case global_stmt:
3093 return ast_for_global_stmt(c, n);
3094 case exec_stmt:
3095 return ast_for_exec_stmt(c, n);
3096 case assert_stmt:
3097 return ast_for_assert_stmt(c, n);
3098 default:
3099 PyErr_Format(PyExc_SystemError,
3100 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3101 TYPE(n), NCH(n));
3102 return NULL;
3103 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 }
3105 else {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003106 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Christian Heimes5224d282008-02-23 15:01:05 +00003107 | funcdef | classdef | decorated
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003108 */
3109 node *ch = CHILD(n, 0);
3110 REQ(n, compound_stmt);
3111 switch (TYPE(ch)) {
3112 case if_stmt:
3113 return ast_for_if_stmt(c, ch);
3114 case while_stmt:
3115 return ast_for_while_stmt(c, ch);
3116 case for_stmt:
3117 return ast_for_for_stmt(c, ch);
3118 case try_stmt:
3119 return ast_for_try_stmt(c, ch);
3120 case with_stmt:
3121 return ast_for_with_stmt(c, ch);
3122 case funcdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003123 return ast_for_funcdef(c, ch, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003124 case classdef:
Christian Heimes5224d282008-02-23 15:01:05 +00003125 return ast_for_classdef(c, ch, NULL);
3126 case decorated:
3127 return ast_for_decorated(c, ch);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003128 default:
3129 PyErr_Format(PyExc_SystemError,
3130 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3131 TYPE(n), NCH(n));
3132 return NULL;
3133 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 }
3135}
3136
3137static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003138parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003140 const char *end;
3141 long x;
3142 double dx;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143#ifndef WITHOUT_COMPLEX
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003144 Py_complex complex;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003145 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146#endif
3147
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003148 errno = 0;
3149 end = s + strlen(s) - 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003151 imflag = *end == 'j' || *end == 'J';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003153 if (*end == 'l' || *end == 'L')
3154 return PyLong_FromString((char *)s, (char **)0, 0);
Mark Dickinson64b7e502008-07-16 09:40:03 +00003155 x = PyOS_strtol((char *)s, (char **)&end, 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003156 if (*end == '\0') {
3157 if (errno != 0)
3158 return PyLong_FromString((char *)s, (char **)0, 0);
3159 return PyInt_FromLong(x);
3160 }
3161 /* XXX Huge floats may silently fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162#ifndef WITHOUT_COMPLEX
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003163 if (imflag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003164 complex.real = 0.;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003165 PyFPE_START_PROTECT("atof", return 0)
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003166 complex.imag = PyOS_ascii_atof(s);
3167 PyFPE_END_PROTECT(complex)
3168 return PyComplex_FromCComplex(complex);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003169 }
3170 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003172 {
3173 PyFPE_START_PROTECT("atof", return 0)
3174 dx = PyOS_ascii_atof(s);
3175 PyFPE_END_PROTECT(dx)
3176 return PyFloat_FromDouble(dx);
3177 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178}
3179
3180static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003181decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182{
3183#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003184 Py_FatalError("decode_utf8 should not be called in this build.");
3185 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003187 PyObject *u, *v;
3188 char *s, *t;
3189 t = s = (char *)*sPtr;
3190 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3191 while (s < end && (*s & 0x80)) s++;
3192 *sPtr = s;
3193 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3194 if (u == NULL)
3195 return NULL;
3196 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3197 Py_DECREF(u);
3198 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199#endif
3200}
3201
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003202#ifdef Py_USING_UNICODE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203static PyObject *
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003204decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003206 PyObject *v, *u;
3207 char *buf;
3208 char *p;
3209 const char *end;
3210 if (encoding == NULL) {
3211 buf = (char *)s;
3212 u = NULL;
3213 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3214 buf = (char *)s;
3215 u = NULL;
3216 } else {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003217 /* check for integer overflow */
3218 if (len > PY_SIZE_MAX / 4)
3219 return NULL;
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003220 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003221 u = PyString_FromStringAndSize((char *)NULL, len * 4);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003222 if (u == NULL)
3223 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003224 p = buf = PyString_AsString(u);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003225 end = s + len;
3226 while (s < end) {
3227 if (*s == '\\') {
3228 *p++ = *s++;
3229 if (*s & 0x80) {
3230 strcpy(p, "u005c");
3231 p += 5;
3232 }
3233 }
3234 if (*s & 0x80) { /* XXX inefficient */
3235 PyObject *w;
3236 char *r;
3237 Py_ssize_t rn, i;
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003238 w = decode_utf8(c, &s, end, "utf-16-be");
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003239 if (w == NULL) {
3240 Py_DECREF(u);
3241 return NULL;
3242 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003243 r = PyString_AsString(w);
3244 rn = PyString_Size(w);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003245 assert(rn % 2 == 0);
3246 for (i = 0; i < rn; i += 2) {
3247 sprintf(p, "\\u%02x%02x",
3248 r[i + 0] & 0xFF,
3249 r[i + 1] & 0xFF);
3250 p += 6;
3251 }
3252 Py_DECREF(w);
3253 } else {
3254 *p++ = *s++;
3255 }
3256 }
3257 len = p - buf;
3258 s = buf;
3259 }
3260 if (rawmode)
3261 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3262 else
3263 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3264 Py_XDECREF(u);
3265 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266}
Georg Brandl69ff5ac2007-08-06 07:37:58 +00003267#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268
3269/* s is a Python string literal, including the bracketing quote characters,
3270 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3271 * parsestr parses it, and returns the decoded Python string object.
3272 */
3273static PyObject *
Christian Heimes3c608332008-03-26 22:01:37 +00003274parsestr(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003276 size_t len;
3277 int quote = Py_CHARMASK(*s);
3278 int rawmode = 0;
3279 int need_encoding;
Christian Heimes3c608332008-03-26 22:01:37 +00003280 int unicode = c->c_future_unicode;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003282 if (isalpha(quote) || quote == '_') {
3283 if (quote == 'u' || quote == 'U') {
3284 quote = *++s;
3285 unicode = 1;
3286 }
Christian Heimes288e89a2008-01-18 18:24:07 +00003287 if (quote == 'b' || quote == 'B') {
3288 quote = *++s;
Christian Heimes3c608332008-03-26 22:01:37 +00003289 unicode = 0;
Christian Heimes288e89a2008-01-18 18:24:07 +00003290 }
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003291 if (quote == 'r' || quote == 'R') {
3292 quote = *++s;
3293 rawmode = 1;
3294 }
3295 }
3296 if (quote != '\'' && quote != '\"') {
3297 PyErr_BadInternalCall();
3298 return NULL;
3299 }
3300 s++;
3301 len = strlen(s);
3302 if (len > INT_MAX) {
3303 PyErr_SetString(PyExc_OverflowError,
3304 "string to parse is too long");
3305 return NULL;
3306 }
3307 if (s[--len] != quote) {
3308 PyErr_BadInternalCall();
3309 return NULL;
3310 }
3311 if (len >= 4 && s[0] == quote && s[1] == quote) {
3312 s += 2;
3313 len -= 2;
3314 if (s[--len] != quote || s[--len] != quote) {
3315 PyErr_BadInternalCall();
3316 return NULL;
3317 }
3318 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003320 if (unicode || Py_UnicodeFlag) {
Benjamin Peterson2b30ea02008-05-03 15:56:42 +00003321 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003322 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323#endif
Christian Heimes3c608332008-03-26 22:01:37 +00003324 need_encoding = (c->c_encoding != NULL &&
3325 strcmp(c->c_encoding, "utf-8") != 0 &&
3326 strcmp(c->c_encoding, "iso-8859-1") != 0);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003327 if (rawmode || strchr(s, '\\') == NULL) {
3328 if (need_encoding) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329#ifndef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003330 /* This should not happen - we never see any other
3331 encoding. */
3332 Py_FatalError(
3333 "cannot deal with encodings in this build.");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334#else
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003335 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3336 if (u == NULL)
3337 return NULL;
Christian Heimes3c608332008-03-26 22:01:37 +00003338 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003339 Py_DECREF(u);
3340 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003342 } else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003343 return PyString_FromStringAndSize(s, len);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003344 }
3345 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003346
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003347 return PyString_DecodeEscape(s, len, NULL, unicode,
Christian Heimes3c608332008-03-26 22:01:37 +00003348 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349}
3350
3351/* Build a Python string object out of a STRING atom. This takes care of
3352 * compile-time literal catenation, calling parsestr() on each piece, and
3353 * pasting the intermediate results together.
3354 */
3355static PyObject *
3356parsestrplus(struct compiling *c, const node *n)
3357{
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003358 PyObject *v;
3359 int i;
3360 REQ(CHILD(n, 0), STRING);
Christian Heimes3c608332008-03-26 22:01:37 +00003361 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003362 /* String literal concatenation */
3363 for (i = 1; i < NCH(n); i++) {
3364 PyObject *s;
Christian Heimes3c608332008-03-26 22:01:37 +00003365 s = parsestr(c, STR(CHILD(n, i)));
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003366 if (s == NULL)
3367 goto onError;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003368 if (PyString_Check(v) && PyString_Check(s)) {
3369 PyString_ConcatAndDel(&v, s);
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003370 if (v == NULL)
3371 goto onError;
3372 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373#ifdef Py_USING_UNICODE
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003374 else {
3375 PyObject *temp = PyUnicode_Concat(v, s);
3376 Py_DECREF(s);
3377 Py_DECREF(v);
3378 v = temp;
3379 if (v == NULL)
3380 goto onError;
3381 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382#endif
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003383 }
3384 }
3385 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386
3387 onError:
Jeremy Hyltondd2cf1c2007-03-16 15:59:47 +00003388 Py_XDECREF(v);
3389 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390}