blob: edcd18b9b8ffd7219f5bc4d21ce6d951e28ad9c8 [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"
10#include "ast.h"
11#include "token.h"
12#include "parsetok.h"
13#include "graminit.h"
14
15#include <assert.h>
16
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017/* Data structure used internally */
18struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +000019 char *c_encoding; /* source encoding */
20 PyArena *c_arena; /* arena for allocating memeory */
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000021 const char *c_filename; /* filename */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022};
23
24static asdl_seq *seq_for_testlist(struct compiling *, const node *);
25static expr_ty ast_for_expr(struct compiling *, const node *);
26static stmt_ty ast_for_stmt(struct compiling *, const node *);
27static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +000028static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
29 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000030static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +000031static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032
33/* Note different signature for ast_for_call */
34static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
35
Christian Heimes81ee3ef2008-05-04 22:42:01 +000036static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes4d6ec852008-03-26 22:34:47 +000037static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode);
Thomas Wouters00e41de2007-02-23 19:56:57 +000038static PyObject *parsestrplus(struct compiling *, const node *n,
39 int *bytesmode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041#ifndef LINENO
Thomas Wouters89f507f2006-12-13 04:49:30 +000042#define LINENO(n) ((n)->n_lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043#endif
44
Nick Coghlan650f0d02007-04-15 12:05:43 +000045#define COMP_GENEXP 0
46#define COMP_LISTCOMP 1
47#define COMP_SETCOMP 2
48
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049static identifier
Benjamin Petersonc7dedb02012-09-02 16:36:01 -040050new_identifier(const char *n, PyArena *arena)
Martin v. Löwis47383402007-08-15 07:32:56 +000051{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -040052 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Thomas Heller50d5a1c2008-11-25 12:35:58 +000053 Py_UNICODE *u;
Benjamin Peterson30760062008-11-25 04:02:28 +000054 if (!id)
55 return NULL;
Thomas Heller50d5a1c2008-11-25 12:35:58 +000056 u = PyUnicode_AS_UNICODE(id);
Martin v. Löwis47383402007-08-15 07:32:56 +000057 /* Check whether there are non-ASCII characters in the
58 identifier; if so, normalize to NFKC. */
59 for (; *u; u++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 if (*u >= 128) {
61 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
62 PyObject *id2;
63 if (!m)
64 return NULL;
65 id2 = PyObject_CallMethod(m, "normalize", "sO", "NFKC", id);
66 Py_DECREF(m);
67 if (!id2)
68 return NULL;
69 Py_DECREF(id);
70 id = id2;
71 break;
72 }
Martin v. Löwis47383402007-08-15 07:32:56 +000073 }
Martin v. Löwis5b222132007-06-10 09:51:05 +000074 PyUnicode_InternInPlace(&id);
Neal Norwitzadb69fc2005-12-17 20:54:49 +000075 PyArena_AddPyObject(arena, id);
76 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077}
78
Neal Norwitzadb69fc2005-12-17 20:54:49 +000079#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080
81/* This routine provides an invalid object for the syntax error.
82 The outermost routine must unpack this error and create the
83 proper object. We do this so that we don't have to pass
84 the filename to everything function.
85
86 XXX Maybe we should just pass the filename...
87*/
88
89static int
90ast_error(const node *n, const char *errstr)
91{
Benjamin Petersonc5d75182012-09-02 14:38:08 -040092 PyObject *u = Py_BuildValue("zii", errstr, LINENO(n), n->n_col_offset), *save;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000093 if (!u)
Thomas Wouters89f507f2006-12-13 04:49:30 +000094 return 0;
Benjamin Peterson2e2c9032012-09-02 14:23:15 -040095 /*
96 * Prevent the error from being chained. PyErr_SetObject will normalize the
97 * exception in order to chain it. ast_error_finish, however, requires the
98 * error not to be normalized.
99 */
Benjamin Petersonc5d75182012-09-02 14:38:08 -0400100 save = PyThreadState_GET()->exc_value;
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400101 PyThreadState_GET()->exc_value = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102 PyErr_SetObject(PyExc_SyntaxError, u);
Benjamin Peterson2e2c9032012-09-02 14:23:15 -0400103 PyThreadState_GET()->exc_value = save;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104 Py_DECREF(u);
105 return 0;
106}
107
108static void
109ast_error_finish(const char *filename)
110{
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000111 PyObject *type, *value, *tback, *errstr, *offset, *loc, *tmp;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000112 PyObject *filename_obj;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000113 long lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114
115 assert(PyErr_Occurred());
116 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000117 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118
119 PyErr_Fetch(&type, &value, &tback);
120 errstr = PyTuple_GetItem(value, 0);
121 if (!errstr)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123 Py_INCREF(errstr);
Christian Heimes217cfd12007-12-02 14:31:20 +0000124 lineno = PyLong_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000125 if (lineno == -1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000126 Py_DECREF(errstr);
127 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000128 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000129 offset = PyTuple_GetItem(value, 2);
130 if (!offset) {
131 Py_DECREF(errstr);
132 return;
133 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000134 Py_DECREF(value);
135
136 loc = PyErr_ProgramText(filename, lineno);
137 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000138 Py_INCREF(Py_None);
139 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140 }
Victor Stinner2f2ed1f2010-10-16 13:42:53 +0000141 if (filename != NULL)
142 filename_obj = PyUnicode_DecodeFSDefault(filename);
143 else {
144 Py_INCREF(Py_None);
145 filename_obj = Py_None;
146 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000147 if (filename_obj != NULL)
148 tmp = Py_BuildValue("(NlOO)", filename_obj, lineno, offset, loc);
149 else
150 tmp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000152 if (!tmp) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000153 Py_DECREF(errstr);
154 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000155 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000156 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157 Py_DECREF(errstr);
158 Py_DECREF(tmp);
159 if (!value)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000160 return;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161 PyErr_Restore(type, value, tback);
162}
163
164/* num_stmts() returns number of contained statements.
165
166 Use this routine to determine how big a sequence is needed for
167 the statements in a parse tree. Its raison d'etre is this bit of
168 grammar:
169
170 stmt: simple_stmt | compound_stmt
171 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
172
173 A simple_stmt can contain multiple small_stmt elements joined
174 by semicolons. If the arg is a simple_stmt, the number of
175 small_stmt elements is returned.
176*/
177
178static int
179num_stmts(const node *n)
180{
181 int i, l;
182 node *ch;
183
184 switch (TYPE(n)) {
185 case single_input:
186 if (TYPE(CHILD(n, 0)) == NEWLINE)
187 return 0;
188 else
189 return num_stmts(CHILD(n, 0));
190 case file_input:
191 l = 0;
192 for (i = 0; i < NCH(n); i++) {
193 ch = CHILD(n, i);
194 if (TYPE(ch) == stmt)
195 l += num_stmts(ch);
196 }
197 return l;
198 case stmt:
199 return num_stmts(CHILD(n, 0));
200 case compound_stmt:
201 return 1;
202 case simple_stmt:
203 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
204 case suite:
205 if (NCH(n) == 1)
206 return num_stmts(CHILD(n, 0));
207 else {
208 l = 0;
209 for (i = 2; i < (NCH(n) - 1); i++)
210 l += num_stmts(CHILD(n, i));
211 return l;
212 }
213 default: {
214 char buf[128];
215
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000216 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 TYPE(n), NCH(n));
218 Py_FatalError(buf);
219 }
220 }
221 assert(0);
222 return 0;
223}
224
225/* Transform the CST rooted at node * to the appropriate AST
226*/
227
228mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000229PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
230 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000232 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 asdl_seq *stmts = NULL;
234 stmt_ty s;
235 node *ch;
236 struct compiling c;
237
238 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000239 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000241#if 0
Guido van Rossumd8faa362007-04-27 19:54:29 +0000242 ast_error(n, "encoding declaration in Unicode string");
243 goto error;
Guido van Rossum53970392007-06-12 00:28:30 +0000244#endif
245 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247 } else if (TYPE(n) == encoding_decl) {
248 c.c_encoding = STR(n);
249 n = CHILD(n, 0);
250 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000252 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000254 c.c_arena = arena;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000255 c.c_filename = filename;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256
Jeremy Hyltona8293132006-02-28 17:58:27 +0000257 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 switch (TYPE(n)) {
259 case file_input:
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000260 stmts = asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 if (!stmts)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000262 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263 for (i = 0; i < NCH(n) - 1; i++) {
264 ch = CHILD(n, i);
265 if (TYPE(ch) == NEWLINE)
266 continue;
267 REQ(ch, stmt);
268 num = num_stmts(ch);
269 if (num == 1) {
270 s = ast_for_stmt(&c, ch);
271 if (!s)
272 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000273 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 }
275 else {
276 ch = CHILD(ch, 0);
277 REQ(ch, simple_stmt);
278 for (j = 0; j < num; j++) {
279 s = ast_for_stmt(&c, CHILD(ch, j * 2));
280 if (!s)
281 goto error;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000282 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 }
284 }
285 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000286 return Module(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287 case eval_input: {
288 expr_ty testlist_ast;
289
Nick Coghlan650f0d02007-04-15 12:05:43 +0000290 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000291 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292 if (!testlist_ast)
293 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000294 return Expression(testlist_ast, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295 }
296 case single_input:
297 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000298 stmts = asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 if (!stmts)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000300 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000301 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
302 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000303 if (!asdl_seq_GET(stmts, 0))
304 goto error;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000305 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306 }
307 else {
308 n = CHILD(n, 0);
309 num = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000310 stmts = asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311 if (!stmts)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000312 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000314 s = ast_for_stmt(&c, n);
315 if (!s)
316 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317 asdl_seq_SET(stmts, 0, s);
318 }
319 else {
320 /* Only a simple_stmt can contain multiple statements. */
321 REQ(n, simple_stmt);
322 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323 if (TYPE(CHILD(n, i)) == NEWLINE)
324 break;
325 s = ast_for_stmt(&c, CHILD(n, i));
326 if (!s)
327 goto error;
328 asdl_seq_SET(stmts, i / 2, s);
329 }
330 }
331
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000332 return Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333 }
334 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000335 PyErr_Format(PyExc_SystemError,
336 "invalid node %d for PyAST_FromNode", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337 goto error;
338 }
339 error:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340 ast_error_finish(filename);
341 return NULL;
342}
343
344/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
345*/
346
347static operator_ty
348get_operator(const node *n)
349{
350 switch (TYPE(n)) {
351 case VBAR:
352 return BitOr;
353 case CIRCUMFLEX:
354 return BitXor;
355 case AMPER:
356 return BitAnd;
357 case LEFTSHIFT:
358 return LShift;
359 case RIGHTSHIFT:
360 return RShift;
361 case PLUS:
362 return Add;
363 case MINUS:
364 return Sub;
365 case STAR:
366 return Mult;
367 case SLASH:
368 return Div;
369 case DOUBLESLASH:
370 return FloorDiv;
371 case PERCENT:
372 return Mod;
373 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375 }
376}
377
Guido van Rossume7ba4952007-06-06 23:52:48 +0000378static const char* FORBIDDEN[] = {
379 "None",
380 "True",
381 "False",
382 NULL,
383};
384
385static int
Benjamin Peterson70f52762009-06-28 23:32:44 +0000386forbidden_name(identifier name, const node *n, int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000387{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000388 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000389 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
390 ast_error(n, "assignment to keyword");
391 return 1;
392 }
393 if (full_checks) {
394 const char **p;
395 for (p = FORBIDDEN; *p; p++) {
396 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
397 ast_error(n, "assignment to keyword");
398 return 1;
399 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000400 }
401 }
402 return 0;
403}
404
Jeremy Hyltona8293132006-02-28 17:58:27 +0000405/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406
407 Only sets context for expr kinds that "can appear in assignment context"
408 (according to ../Parser/Python.asdl). For other expr kinds, it sets
409 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410*/
411
412static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000413set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414{
415 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000416 /* If a particular expression type can't be used for assign / delete,
417 set expr_name to its name and an error message will be generated.
418 */
419 const char* expr_name = NULL;
420
421 /* The ast defines augmented store and load contexts, but the
422 implementation here doesn't actually use them. The code may be
423 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000424 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000425 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000426 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000427 */
428 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429
430 switch (e->kind) {
431 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000432 e->v.Attribute.ctx = ctx;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000433 if (ctx == Store && forbidden_name(e->v.Attribute.attr, n, 1))
434 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000435 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000437 e->v.Subscript.ctx = ctx;
438 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000439 case Starred_kind:
440 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000441 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000442 return 0;
443 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000445 if (ctx == Store) {
Benjamin Peterson70f52762009-06-28 23:32:44 +0000446 if (forbidden_name(e->v.Name.id, n, 1))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000447 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000448 }
449 e->v.Name.ctx = ctx;
450 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000452 e->v.List.ctx = ctx;
453 s = e->v.List.elts;
454 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000456 if (asdl_seq_LEN(e->v.Tuple.elts)) {
457 e->v.Tuple.ctx = ctx;
458 s = e->v.Tuple.elts;
459 }
460 else {
461 expr_name = "()";
462 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000463 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000464 case Lambda_kind:
465 expr_name = "lambda";
466 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000468 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000469 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000470 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000472 case UnaryOp_kind:
473 expr_name = "operator";
474 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000476 expr_name = "generator expression";
477 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478 case Yield_kind:
479 expr_name = "yield expression";
480 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000481 case ListComp_kind:
482 expr_name = "list comprehension";
483 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000484 case SetComp_kind:
485 expr_name = "set comprehension";
486 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000487 case DictComp_kind:
488 expr_name = "dict comprehension";
489 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000490 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000491 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492 case Num_kind:
493 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500494 case Bytes_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000495 expr_name = "literal";
496 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000497 case Ellipsis_kind:
498 expr_name = "Ellipsis";
499 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000500 case Compare_kind:
501 expr_name = "comparison";
502 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000503 case IfExp_kind:
504 expr_name = "conditional expression";
505 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000506 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 PyErr_Format(PyExc_SystemError,
508 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000509 e->kind, e->lineno);
510 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000512 /* Check for error string set by switch */
513 if (expr_name) {
514 char buf[300];
515 PyOS_snprintf(buf, sizeof(buf),
516 "can't %s %s",
517 ctx == Store ? "assign to" : "delete",
518 expr_name);
519 return ast_error(n, buf);
520 }
521
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 */
525 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000526 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527
Thomas Wouters89f507f2006-12-13 04:49:30 +0000528 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000529 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000530 return 0;
531 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532 }
533 return 1;
534}
535
536static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000537ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538{
539 REQ(n, augassign);
540 n = CHILD(n, 0);
541 switch (STR(n)[0]) {
542 case '+':
543 return Add;
544 case '-':
545 return Sub;
546 case '/':
547 if (STR(n)[1] == '/')
548 return FloorDiv;
549 else
550 return Div;
551 case '%':
552 return Mod;
553 case '<':
554 return LShift;
555 case '>':
556 return RShift;
557 case '&':
558 return BitAnd;
559 case '^':
560 return BitXor;
561 case '|':
562 return BitOr;
563 case '*':
564 if (STR(n)[1] == '*')
565 return Pow;
566 else
567 return Mult;
568 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000569 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000570 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 }
572}
573
574static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000575ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576{
Guido van Rossumb053cd82006-08-24 03:53:23 +0000577 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 |'is' 'not'
579 */
580 REQ(n, comp_op);
581 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000582 n = CHILD(n, 0);
583 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584 case LESS:
585 return Lt;
586 case GREATER:
587 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000588 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589 return Eq;
590 case LESSEQUAL:
591 return LtE;
592 case GREATEREQUAL:
593 return GtE;
594 case NOTEQUAL:
595 return NotEq;
596 case NAME:
597 if (strcmp(STR(n), "in") == 0)
598 return In;
599 if (strcmp(STR(n), "is") == 0)
600 return Is;
601 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000602 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 }
607 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000608 /* handle "not in" and "is not" */
609 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 case NAME:
611 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
612 return NotIn;
613 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
614 return IsNot;
615 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000616 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000618 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000619 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 }
Neal Norwitz79792652005-11-14 04:25:03 +0000621 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624}
625
626static asdl_seq *
627seq_for_testlist(struct compiling *c, const node *n)
628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +0000630 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
631 */
Armin Rigo31441302005-10-21 12:57:31 +0000632 asdl_seq *seq;
633 expr_ty expression;
634 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +0000635 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000637 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 if (!seq)
639 return NULL;
640
641 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +0000643 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644
Benjamin Peterson4905e802009-09-27 02:43:28 +0000645 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000646 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648
649 assert(i / 2 < seq->size);
650 asdl_seq_SET(seq, i / 2, expression);
651 }
652 return seq;
653}
654
Neal Norwitzc1505362006-12-28 06:47:50 +0000655static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -0700656ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +0000657{
658 identifier name;
659 expr_ty annotation = NULL;
660 node *ch;
661
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000662 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +0000663 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +0000664 name = NEW_IDENTIFIER(ch);
665 if (!name)
666 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000667 if (forbidden_name(name, ch, 0))
668 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000669
670 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
671 annotation = ast_for_expr(c, CHILD(n, 2));
672 if (!annotation)
673 return NULL;
674 }
675
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000676 return arg(name, annotation, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677}
678
Guido van Rossum4f72a782006-10-27 23:31:49 +0000679/* returns -1 if failed to handle keyword only arguments
680 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000681 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +0000682 ^^^
683 start pointing here
684 */
685static int
686handle_keywordonly_args(struct compiling *c, const node *n, int start,
687 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
688{
Benjamin Petersond951e7b2008-11-25 22:19:53 +0000689 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000690 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +0000691 expr_ty expression, annotation;
692 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000693 int i = start;
694 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +0000695
696 if (kwonlyargs == NULL) {
697 ast_error(CHILD(n, start), "named arguments must follow bare *");
698 return -1;
699 }
700 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000701 while (i < NCH(n)) {
702 ch = CHILD(n, i);
703 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000704 case vfpdef:
705 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +0000706 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000707 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000708 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000709 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000710 asdl_seq_SET(kwdefaults, j, expression);
711 i += 2; /* '=' and test */
712 }
713 else { /* setting NULL if no default value exists */
714 asdl_seq_SET(kwdefaults, j, NULL);
715 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000716 if (NCH(ch) == 3) {
717 /* ch is NAME ':' test */
718 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000719 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +0000720 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +0000721 }
722 else {
723 annotation = NULL;
724 }
725 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +0000726 argname = NEW_IDENTIFIER(ch);
727 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +0000728 goto error;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000729 if (forbidden_name(argname, ch, 0))
730 goto error;
Benjamin Petersond951e7b2008-11-25 22:19:53 +0000731 arg = arg(argname, annotation, c->c_arena);
732 if (!arg)
733 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +0000734 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000735 i += 2; /* the name and the comma */
736 break;
737 case DOUBLESTAR:
738 return i;
739 default:
740 ast_error(ch, "unexpected node");
741 goto error;
742 }
743 }
744 return i;
745 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000747}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748
Jeremy Hyltona8293132006-02-28 17:58:27 +0000749/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750
751static arguments_ty
752ast_for_arguments(struct compiling *c, const node *n)
753{
Neal Norwitzc1505362006-12-28 06:47:50 +0000754 /* This function handles both typedargslist (function definition)
755 and varargslist (lambda definition).
756
757 parameters: '(' [typedargslist] ')'
758 typedargslist: ((tfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000760 | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +0000761 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000762 tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +0000763 varargslist: ((vfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000765 | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +0000766 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000767 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000769 int i, j, k, nposargs = 0, nkwonlyargs = 0;
770 int nposdefaults = 0, found_default = 0;
771 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 identifier vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000773 arg_ty arg;
774 expr_ty varargannotation = NULL, kwargannotation = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 node *ch;
776
777 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000778 if (NCH(n) == 2) /* () as argument list */
Neal Norwitzc1505362006-12-28 06:47:50 +0000779 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
780 NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000781 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000783 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Jeremy Hyltone921e022008-07-17 16:37:17 +0000785 /* First count the number of positional args & defaults. The
786 variable i is the loop index for this for loop and the next.
787 The next loop picks up where the first leaves off.
788 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000790 ch = CHILD(n, i);
791 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +0000792 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000793 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +0000794 if (i < NCH(n) && /* skip argument following star */
795 (TYPE(CHILD(n, i)) == tfpdef ||
796 TYPE(CHILD(n, i)) == vfpdef)) {
797 i++;
798 }
799 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000800 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000801 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000802 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000803 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +0000806 defaults for keyword only args */
807 for ( ; i < NCH(n); ++i) {
808 ch = CHILD(n, i);
809 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000810 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000811 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000812 posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL);
813 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000814 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000815 kwonlyargs = (nkwonlyargs ?
816 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
817 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000818 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 posdefaults = (nposdefaults ?
Guido van Rossum4f72a782006-10-27 23:31:49 +0000820 asdl_seq_new(nposdefaults, c->c_arena) : NULL);
821 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000822 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +0000824 since we set NULL as default for keyword only argument w/o default
825 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +0000826 kwdefaults = (nkwonlyargs ?
Guido van Rossum4f72a782006-10-27 23:31:49 +0000827 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
828 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000829 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000830
831 if (nposargs + nkwonlyargs > 255) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000832 ast_error(n, "more than 255 arguments");
833 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000834 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000836 /* tfpdef: NAME [':' test]
837 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 */
839 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000840 j = 0; /* index for defaults */
841 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000843 ch = CHILD(n, i);
844 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +0000845 case tfpdef:
846 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
848 anything other than EQUAL or a comma? */
849 /* XXX Should NCH(n) check be made a separate check? */
850 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000851 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
852 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000853 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000854 assert(posdefaults != NULL);
855 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000857 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000859 else if (found_default) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 ast_error(n,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000861 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000862 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000863 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -0700864 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +0000865 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000866 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000867 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 i += 2; /* the name and the comma */
869 break;
870 case STAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +0000871 if (i+1 >= NCH(n)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 ast_error(CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +0000873 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000874 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000875 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000876 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +0000877 if (TYPE(ch) == COMMA) {
878 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000879 i += 2; /* now follows keyword only arguments */
880 res = handle_keywordonly_args(c, n, i,
881 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000882 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000883 i = res; /* res has new position to process */
884 }
885 else {
Neal Norwitzc1505362006-12-28 06:47:50 +0000886 vararg = NEW_IDENTIFIER(CHILD(ch, 0));
Benjamin Peterson30760062008-11-25 04:02:28 +0000887 if (!vararg)
888 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000889 if (forbidden_name(vararg, CHILD(ch, 0), 0))
890 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000891 if (NCH(ch) > 1) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000892 /* there is an annotation on the vararg */
893 varargannotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000894 if (!varargannotation)
895 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000896 }
Guido van Rossum4f72a782006-10-27 23:31:49 +0000897 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000898 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
899 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +0000900 int res = 0;
901 res = handle_keywordonly_args(c, n, i,
902 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000903 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000904 i = res; /* res has new position to process */
905 }
906 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907 break;
908 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000909 ch = CHILD(n, i+1); /* tfpdef */
910 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +0000911 kwarg = NEW_IDENTIFIER(CHILD(ch, 0));
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000912 if (!kwarg)
913 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000914 if (NCH(ch) > 1) {
915 /* there is an annotation on the kwarg */
916 kwargannotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000917 if (!kwargannotation)
918 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000919 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000920 if (forbidden_name(kwarg, CHILD(ch, 0), 0))
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000921 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 i += 3;
923 break;
924 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000925 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 "unexpected node in varargslist: %d @ %d",
927 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +0000928 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000929 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 }
Neal Norwitzc1505362006-12-28 06:47:50 +0000931 return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg,
932 kwargannotation, posdefaults, kwdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933}
934
935static expr_ty
936ast_for_dotted_name(struct compiling *c, const node *n)
937{
Neal Norwitz84456bd2005-12-18 03:16:20 +0000938 expr_ty e;
939 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000940 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 int i;
942
943 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000944
945 lineno = LINENO(n);
946 col_offset = n->n_col_offset;
947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 id = NEW_IDENTIFIER(CHILD(n, 0));
949 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000950 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000951 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
955 for (i = 2; i < NCH(n); i+=2) {
956 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000957 if (!id)
958 return NULL;
959 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
960 if (!e)
961 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 }
963
964 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965}
966
967static expr_ty
968ast_for_decorator(struct compiling *c, const node *n)
969{
970 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
971 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +0000972 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 REQ(CHILD(n, 0), AT);
976 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
979 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000980 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000983 d = name_expr;
984 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 }
986 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000987 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000988 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000989 if (!d)
990 return NULL;
991 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 }
993 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000994 d = ast_for_call(c, CHILD(n, 3), name_expr);
995 if (!d)
996 return NULL;
997 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 }
999
1000 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001}
1002
1003static asdl_seq*
1004ast_for_decorators(struct compiling *c, const node *n)
1005{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001006 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001007 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 REQ(n, decorators);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001011 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 if (!decorator_seq)
1013 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001016 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001017 if (!d)
1018 return NULL;
1019 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 }
1021 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022}
1023
1024static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001025ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001027 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001028 identifier name;
1029 arguments_ty args;
1030 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001031 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001032 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033
1034 REQ(n, funcdef);
1035
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 name = NEW_IDENTIFIER(CHILD(n, name_i));
1037 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001038 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001039 if (forbidden_name(name, CHILD(n, name_i), 0))
1040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1042 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001043 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001044 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1045 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1046 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001047 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001048 name_i += 2;
1049 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 body = ast_for_suite(c, CHILD(n, name_i + 3));
1051 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001052 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053
Neal Norwitzc1505362006-12-28 06:47:50 +00001054 return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056}
1057
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001058static stmt_ty
1059ast_for_decorated(struct compiling *c, const node *n)
1060{
1061 /* decorated: decorators (classdef | funcdef) */
1062 stmt_ty thing = NULL;
1063 asdl_seq *decorator_seq = NULL;
1064
1065 REQ(n, decorated);
1066
1067 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1068 if (!decorator_seq)
1069 return NULL;
1070
1071 assert(TYPE(CHILD(n, 1)) == funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001073
1074 if (TYPE(CHILD(n, 1)) == funcdef) {
1075 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1076 } else if (TYPE(CHILD(n, 1)) == classdef) {
1077 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
1078 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001079 /* we count the decorators in when talking about the class' or
1080 * function's line number */
1081 if (thing) {
1082 thing->lineno = LINENO(n);
1083 thing->col_offset = n->n_col_offset;
1084 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001085 return thing;
1086}
1087
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088static expr_ty
1089ast_for_lambdef(struct compiling *c, const node *n)
1090{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001091 /* lambdef: 'lambda' [varargslist] ':' test
1092 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 arguments_ty args;
1094 expr_ty expression;
1095
1096 if (NCH(n) == 3) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001097 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1098 NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 if (!args)
1100 return NULL;
1101 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001102 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 }
1105 else {
1106 args = ast_for_arguments(c, CHILD(n, 1));
1107 if (!args)
1108 return NULL;
1109 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001110 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 }
1113
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001114 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115}
1116
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001117static expr_ty
1118ast_for_ifexpr(struct compiling *c, const node *n)
1119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001121 expr_ty expression, body, orelse;
1122
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001123 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001124 body = ast_for_expr(c, CHILD(n, 0));
1125 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001126 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001127 expression = ast_for_expr(c, CHILD(n, 2));
1128 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001129 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001130 orelse = ast_for_expr(c, CHILD(n, 4));
1131 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001132 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001133 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1134 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001135}
1136
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001138 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139
Nick Coghlan650f0d02007-04-15 12:05:43 +00001140 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141*/
1142
1143static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001144count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001146 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147
Guido van Rossumd8faa362007-04-27 19:54:29 +00001148 count_comp_for:
1149 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001150 REQ(n, comp_for);
1151 if (NCH(n) == 5)
1152 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001153 else
1154 return n_fors;
1155 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001156 REQ(n, comp_iter);
1157 n = CHILD(n, 0);
1158 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001159 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001160 else if (TYPE(n) == comp_if) {
1161 if (NCH(n) == 3) {
1162 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001163 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001164 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001165 else
1166 return n_fors;
1167 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001168
Guido van Rossumd8faa362007-04-27 19:54:29 +00001169 /* Should never be reached */
1170 PyErr_SetString(PyExc_SystemError,
1171 "logic error in count_comp_fors");
1172 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173}
1174
Nick Coghlan650f0d02007-04-15 12:05:43 +00001175/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176
Nick Coghlan650f0d02007-04-15 12:05:43 +00001177 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178*/
1179
1180static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001181count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001183 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184
Guido van Rossumd8faa362007-04-27 19:54:29 +00001185 while (1) {
1186 REQ(n, comp_iter);
1187 if (TYPE(CHILD(n, 0)) == comp_for)
1188 return n_ifs;
1189 n = CHILD(n, 0);
1190 REQ(n, comp_if);
1191 n_ifs++;
1192 if (NCH(n) == 2)
1193 return n_ifs;
1194 n = CHILD(n, 2);
1195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196}
1197
Guido van Rossum992d4a32007-07-11 13:09:30 +00001198static asdl_seq *
1199ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001202 asdl_seq *comps;
1203
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001204 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (n_fors == -1)
1206 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001207
Nick Coghlan650f0d02007-04-15 12:05:43 +00001208 comps = asdl_seq_new(n_fors, c->c_arena);
1209 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001211
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001213 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001215 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001216 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217
Guido van Rossum992d4a32007-07-11 13:09:30 +00001218 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219
Guido van Rossum992d4a32007-07-11 13:09:30 +00001220 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001221 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001222 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001224 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001225 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227
Thomas Wouters89f507f2006-12-13 04:49:30 +00001228 /* Check the # of children rather than the length of t, since
1229 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001230 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001231 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001232 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001234 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1235 c->c_arena),
1236 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001237 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239
Guido van Rossum992d4a32007-07-11 13:09:30 +00001240 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 int j, n_ifs;
1242 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243
Guido van Rossum992d4a32007-07-11 13:09:30 +00001244 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001245 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001246 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248
1249 ifs = asdl_seq_new(n_ifs, c->c_arena);
1250 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001254 REQ(n, comp_iter);
1255 n = CHILD(n, 0);
1256 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257
Guido van Rossum992d4a32007-07-11 13:09:30 +00001258 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001259 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001260 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001261 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001262 if (NCH(n) == 3)
1263 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001265 /* on exit, must guarantee that n is a comp_for */
1266 if (TYPE(n) == comp_iter)
1267 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001268 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001270 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001272 return comps;
1273}
1274
1275static expr_ty
1276ast_for_itercomp(struct compiling *c, const node *n, int type)
1277{
1278 /* testlist_comp: test ( comp_for | (',' test)* [','] )
1279 argument: [test '='] test [comp_for] # Really [keyword '='] test */
1280 expr_ty elt;
1281 asdl_seq *comps;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282
Guido van Rossum992d4a32007-07-11 13:09:30 +00001283 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284
Guido van Rossum992d4a32007-07-11 13:09:30 +00001285 elt = ast_for_expr(c, CHILD(n, 0));
1286 if (!elt)
1287 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288
Guido van Rossum992d4a32007-07-11 13:09:30 +00001289 comps = ast_for_comprehension(c, CHILD(n, 1));
1290 if (!comps)
1291 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001292
1293 if (type == COMP_GENEXP)
1294 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1295 else if (type == COMP_LISTCOMP)
1296 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1297 else if (type == COMP_SETCOMP)
1298 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1299 else
1300 /* Should never happen */
1301 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302}
1303
1304static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001305ast_for_dictcomp(struct compiling *c, const node *n)
1306{
1307 expr_ty key, value;
1308 asdl_seq *comps;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309
Guido van Rossum992d4a32007-07-11 13:09:30 +00001310 assert(NCH(n) > 3);
1311 REQ(CHILD(n, 1), COLON);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312
Guido van Rossum992d4a32007-07-11 13:09:30 +00001313 key = ast_for_expr(c, CHILD(n, 0));
1314 if (!key)
1315 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001316 value = ast_for_expr(c, CHILD(n, 2));
1317 if (!value)
1318 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319
Guido van Rossum992d4a32007-07-11 13:09:30 +00001320 comps = ast_for_comprehension(c, CHILD(n, 3));
1321 if (!comps)
1322 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323
Guido van Rossum992d4a32007-07-11 13:09:30 +00001324 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1325}
1326
1327static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001328ast_for_genexp(struct compiling *c, const node *n)
1329{
1330 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001331 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001332}
1333
1334static expr_ty
1335ast_for_listcomp(struct compiling *c, const node *n)
1336{
1337 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001338 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001339}
1340
1341static expr_ty
1342ast_for_setcomp(struct compiling *c, const node *n)
1343{
1344 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001345 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001346}
1347
1348
1349static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350ast_for_atom(struct compiling *c, const node *n)
1351{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001352 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1353 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00001354 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 */
1356 node *ch = CHILD(n, 0);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001357 int bytesmode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00001360 case NAME: {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001361 /* All names start in Load context, but may later be
1362 changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00001363 PyObject *name = NEW_IDENTIFIER(ch);
1364 if (!name)
1365 return NULL;
1366 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1367 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 case STRING: {
Thomas Wouters00e41de2007-02-23 19:56:57 +00001369 PyObject *str = parsestrplus(c, n, &bytesmode);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001370 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001371 const char *errtype = NULL;
1372 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
1373 errtype = "unicode error";
1374 else if (PyErr_ExceptionMatches(PyExc_ValueError))
1375 errtype = "value error";
1376 if (errtype) {
1377 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001378 PyObject *type, *value, *tback, *errstr;
1379 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00001380 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001381 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001382 char *s = _PyUnicode_AsString(errstr);
1383 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00001384 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001385 } else {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001386 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001387 }
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001388 ast_error(n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001389 Py_DECREF(type);
1390 Py_DECREF(value);
1391 Py_XDECREF(tback);
1392 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001393 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001394 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001395 PyArena_AddPyObject(c->c_arena, str);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001396 if (bytesmode)
1397 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
1398 else
1399 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 }
1401 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001402 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001403 if (!pynum)
1404 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001405
Thomas Wouters89f507f2006-12-13 04:49:30 +00001406 PyArena_AddPyObject(c->c_arena, pynum);
1407 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 }
Georg Brandldde00282007-03-18 19:01:53 +00001409 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00001410 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001412 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413
Thomas Wouters89f507f2006-12-13 04:49:30 +00001414 if (TYPE(ch) == RPAR)
1415 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416
Thomas Wouters89f507f2006-12-13 04:49:30 +00001417 if (TYPE(ch) == yield_expr)
1418 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00001421 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001422 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00001423
Nick Coghlan650f0d02007-04-15 12:05:43 +00001424 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001426 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427
Thomas Wouters89f507f2006-12-13 04:49:30 +00001428 if (TYPE(ch) == RSQB)
1429 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430
Nick Coghlan650f0d02007-04-15 12:05:43 +00001431 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001432 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1433 asdl_seq *elts = seq_for_testlist(c, ch);
1434 if (!elts)
1435 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001436
Thomas Wouters89f507f2006-12-13 04:49:30 +00001437 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1438 }
1439 else
1440 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 case LBRACE: {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001442 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
1443 * test (gen_for | (',' test)* [',']) */
Neal Norwitzc1505362006-12-28 06:47:50 +00001444 int i, size;
1445 asdl_seq *keys, *values;
1446
1447 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001448 if (TYPE(ch) == RBRACE) {
1449 /* it's an empty dict */
1450 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
1451 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1452 /* it's a simple set */
Guido van Rossum4d2adcc2007-04-17 21:58:50 +00001453 asdl_seq *elts;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001454 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
Guido van Rossum4d2adcc2007-04-17 21:58:50 +00001455 elts = asdl_seq_new(size, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001456 if (!elts)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001457 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001458 for (i = 0; i < NCH(ch); i += 2) {
1459 expr_ty expression;
1460 expression = ast_for_expr(c, CHILD(ch, i));
1461 if (!expression)
1462 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001463 asdl_seq_SET(elts, i / 2, expression);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001464 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001465 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1466 } else if (TYPE(CHILD(ch, 1)) == comp_for) {
1467 /* it's a set comprehension */
1468 return ast_for_setcomp(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001469 } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {
1470 return ast_for_dictcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001471 } else {
1472 /* it's a dict */
1473 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1474 keys = asdl_seq_new(size, c->c_arena);
1475 if (!keys)
1476 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477
Guido van Rossum86e58e22006-08-28 15:27:34 +00001478 values = asdl_seq_new(size, c->c_arena);
1479 if (!values)
1480 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481
Guido van Rossum86e58e22006-08-28 15:27:34 +00001482 for (i = 0; i < NCH(ch); i += 4) {
1483 expr_ty expression;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484
Guido van Rossum86e58e22006-08-28 15:27:34 +00001485 expression = ast_for_expr(c, CHILD(ch, i));
1486 if (!expression)
1487 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001488
Guido van Rossum86e58e22006-08-28 15:27:34 +00001489 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitze76adcd2005-11-15 05:04:31 +00001490
Guido van Rossum86e58e22006-08-28 15:27:34 +00001491 expression = ast_for_expr(c, CHILD(ch, i + 2));
1492 if (!expression)
1493 return NULL;
1494
1495 asdl_seq_SET(values, i / 4, expression);
1496 }
1497 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1498 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001501 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1502 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 }
1504}
1505
1506static slice_ty
1507ast_for_slice(struct compiling *c, const node *n)
1508{
1509 node *ch;
1510 expr_ty lower = NULL, upper = NULL, step = NULL;
1511
1512 REQ(n, subscript);
1513
1514 /*
Georg Brandl52318d62006-09-06 07:06:08 +00001515 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 sliceop: ':' [test]
1517 */
1518 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 if (NCH(n) == 1 && TYPE(ch) == test) {
1520 /* 'step' variable hold no significance in terms of being used over
1521 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 if (!step)
1524 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525
Thomas Wouters89f507f2006-12-13 04:49:30 +00001526 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 }
1528
1529 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001530 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 if (!lower)
1532 return NULL;
1533 }
1534
1535 /* If there's an upper bound it's in the second or third position. */
1536 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001537 if (NCH(n) > 1) {
1538 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 if (TYPE(n2) == test) {
1541 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542 if (!upper)
1543 return NULL;
1544 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001545 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001547 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548
Thomas Wouters89f507f2006-12-13 04:49:30 +00001549 if (TYPE(n2) == test) {
1550 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 if (!upper)
1552 return NULL;
1553 }
1554 }
1555
1556 ch = CHILD(n, NCH(n) - 1);
1557 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00001558 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001559 ch = CHILD(ch, 1);
1560 if (TYPE(ch) == test) {
1561 step = ast_for_expr(c, ch);
1562 if (!step)
1563 return NULL;
1564 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 }
1566 }
1567
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569}
1570
1571static expr_ty
1572ast_for_binop(struct compiling *c, const node *n)
1573{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001574 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00001576 BinOp(BinOp(A, op, B), op, C).
1577 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578
Guido van Rossumd8faa362007-04-27 19:54:29 +00001579 int i, nops;
1580 expr_ty expr1, expr2, result;
1581 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582
Guido van Rossumd8faa362007-04-27 19:54:29 +00001583 expr1 = ast_for_expr(c, CHILD(n, 0));
1584 if (!expr1)
1585 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586
Guido van Rossumd8faa362007-04-27 19:54:29 +00001587 expr2 = ast_for_expr(c, CHILD(n, 2));
1588 if (!expr2)
1589 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590
Guido van Rossumd8faa362007-04-27 19:54:29 +00001591 newoperator = get_operator(CHILD(n, 1));
1592 if (!newoperator)
1593 return NULL;
1594
1595 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1596 c->c_arena);
1597 if (!result)
1598 return NULL;
1599
1600 nops = (NCH(n) - 1) / 2;
1601 for (i = 1; i < nops; i++) {
1602 expr_ty tmp_result, tmp;
1603 const node* next_oper = CHILD(n, i * 2 + 1);
1604
1605 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001606 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607 return NULL;
1608
Guido van Rossumd8faa362007-04-27 19:54:29 +00001609 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1610 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611 return NULL;
1612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001614 LINENO(next_oper), next_oper->n_col_offset,
1615 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001617 return NULL;
1618 result = tmp_result;
1619 }
1620 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621}
1622
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001623static expr_ty
1624ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001627 subscriptlist: subscript (',' subscript)* [',']
1628 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1629 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001630 REQ(n, trailer);
1631 if (TYPE(CHILD(n, 0)) == LPAR) {
1632 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001633 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1634 n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001635 else
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001636 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001637 }
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001638 else if (TYPE(CHILD(n, 0)) == DOT ) {
Benjamin Peterson30760062008-11-25 04:02:28 +00001639 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
1640 if (!attr_id)
1641 return NULL;
1642 return Attribute(left_expr, attr_id, Load,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001643 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00001644 }
1645 else {
1646 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001647 REQ(CHILD(n, 2), RSQB);
1648 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001649 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001650 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1651 if (!slc)
1652 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001653 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1654 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001655 }
1656 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001658 by treating the sequence as a tuple literal if there are
1659 no slice features.
1660 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001661 int j;
1662 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001663 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001664 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001665 asdl_seq *slices, *elts;
1666 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001667 if (!slices)
1668 return NULL;
1669 for (j = 0; j < NCH(n); j += 2) {
1670 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001671 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001672 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001673 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001674 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001675 asdl_seq_SET(slices, j / 2, slc);
1676 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001677 if (!simple) {
1678 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001679 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001680 }
1681 /* extract Index values and put them in a Tuple */
1682 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001683 if (!elts)
1684 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001685 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1686 slc = (slice_ty)asdl_seq_GET(slices, j);
1687 assert(slc->kind == Index_kind && slc->v.Index.value);
1688 asdl_seq_SET(elts, j, slc->v.Index.value);
1689 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001690 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00001691 if (!e)
1692 return NULL;
1693 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001694 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001695 }
1696 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001697}
1698
1699static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001700ast_for_factor(struct compiling *c, const node *n)
1701{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001702 expr_ty expression;
1703
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001704 expression = ast_for_expr(c, CHILD(n, 1));
1705 if (!expression)
1706 return NULL;
1707
1708 switch (TYPE(CHILD(n, 0))) {
1709 case PLUS:
1710 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1711 c->c_arena);
1712 case MINUS:
1713 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1714 c->c_arena);
1715 case TILDE:
1716 return UnaryOp(Invert, expression, LINENO(n),
1717 n->n_col_offset, c->c_arena);
1718 }
1719 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1720 TYPE(CHILD(n, 0)));
1721 return NULL;
1722}
1723
1724static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001725ast_for_power(struct compiling *c, const node *n)
1726{
1727 /* power: atom trailer* ('**' factor)*
1728 */
1729 int i;
1730 expr_ty e, tmp;
1731 REQ(n, power);
1732 e = ast_for_atom(c, CHILD(n, 0));
1733 if (!e)
1734 return NULL;
1735 if (NCH(n) == 1)
1736 return e;
1737 for (i = 1; i < NCH(n); i++) {
1738 node *ch = CHILD(n, i);
1739 if (TYPE(ch) != trailer)
1740 break;
1741 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001742 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001743 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001744 tmp->lineno = e->lineno;
1745 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001746 e = tmp;
1747 }
1748 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1749 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001750 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001751 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001752 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001753 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001754 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001755 e = tmp;
1756 }
1757 return e;
1758}
1759
Guido van Rossum0368b722007-05-11 16:50:42 +00001760static expr_ty
1761ast_for_starred(struct compiling *c, const node *n)
1762{
1763 expr_ty tmp;
1764 REQ(n, star_expr);
1765
1766 tmp = ast_for_expr(c, CHILD(n, 1));
1767 if (!tmp)
1768 return NULL;
1769
1770 /* The Load context is changed later. */
1771 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
1772}
1773
1774
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775/* Do not name a variable 'expr'! Will cause a compile error.
1776*/
1777
1778static expr_ty
1779ast_for_expr(struct compiling *c, const node *n)
1780{
1781 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001782 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00001783 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 and_test: not_test ('and' not_test)*
1786 not_test: 'not' not_test | comparison
1787 comparison: expr (comp_op expr)*
1788 expr: xor_expr ('|' xor_expr)*
1789 xor_expr: and_expr ('^' and_expr)*
1790 and_expr: shift_expr ('&' shift_expr)*
1791 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1792 arith_expr: term (('+'|'-') term)*
1793 term: factor (('*'|'/'|'%'|'//') factor)*
1794 factor: ('+'|'-'|'~') factor | power
1795 power: atom trailer* ('**' factor)*
1796 */
1797
1798 asdl_seq *seq;
1799 int i;
1800
1801 loop:
1802 switch (TYPE(n)) {
1803 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00001804 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001805 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00001806 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001808 else if (NCH(n) > 1)
1809 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001810 /* Fallthrough */
1811 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 case and_test:
1813 if (NCH(n) == 1) {
1814 n = CHILD(n, 0);
1815 goto loop;
1816 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 if (!seq)
1819 return NULL;
1820 for (i = 0; i < NCH(n); i += 2) {
1821 expr_ty e = ast_for_expr(c, CHILD(n, i));
1822 if (!e)
1823 return NULL;
1824 asdl_seq_SET(seq, i / 2, e);
1825 }
1826 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001827 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1828 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00001829 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001830 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 case not_test:
1832 if (NCH(n) == 1) {
1833 n = CHILD(n, 0);
1834 goto loop;
1835 }
1836 else {
1837 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1838 if (!expression)
1839 return NULL;
1840
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001841 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1842 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 }
1844 case comparison:
1845 if (NCH(n) == 1) {
1846 n = CHILD(n, 0);
1847 goto loop;
1848 }
1849 else {
1850 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001851 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001852 asdl_seq *cmps;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001853 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 if (!ops)
1855 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 return NULL;
1859 }
1860 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001861 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001863 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001864 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001866 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867
1868 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001869 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001871 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001873 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 asdl_seq_SET(cmps, i / 2, expression);
1875 }
1876 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001877 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001879 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001881 return Compare(expression, ops, cmps, LINENO(n),
1882 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 }
1884 break;
1885
Guido van Rossum0368b722007-05-11 16:50:42 +00001886 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 /* The next five cases all handle BinOps. The main body of code
1889 is the same in each case, but the switch turned inside out to
1890 reuse the code for each type of operator.
1891 */
1892 case expr:
1893 case xor_expr:
1894 case and_expr:
1895 case shift_expr:
1896 case arith_expr:
1897 case term:
1898 if (NCH(n) == 1) {
1899 n = CHILD(n, 0);
1900 goto loop;
1901 }
1902 return ast_for_binop(c, n);
1903 case yield_expr: {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001904 expr_ty exp = NULL;
1905 if (NCH(n) == 2) {
1906 exp = ast_for_testlist(c, CHILD(n, 1));
1907 if (!exp)
1908 return NULL;
1909 }
1910 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1911 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001912 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 if (NCH(n) == 1) {
1914 n = CHILD(n, 0);
1915 goto loop;
1916 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001917 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001918 case power:
1919 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001921 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922 return NULL;
1923 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001924 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 return NULL;
1926}
1927
1928static expr_ty
1929ast_for_call(struct compiling *c, const node *n, expr_ty func)
1930{
1931 /*
1932 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1933 | '**' test)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001934 argument: [test '='] test [comp_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 */
1936
1937 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001938 asdl_seq *args;
1939 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 expr_ty vararg = NULL, kwarg = NULL;
1941
1942 REQ(n, arglist);
1943
1944 nargs = 0;
1945 nkeywords = 0;
1946 ngens = 0;
1947 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001948 node *ch = CHILD(n, i);
1949 if (TYPE(ch) == argument) {
1950 if (NCH(ch) == 1)
1951 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001952 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001953 ngens++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 else
Thomas Wouters89f507f2006-12-13 04:49:30 +00001955 nkeywords++;
1956 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 }
1958 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001959 ast_error(n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00001960 "if not sole argument");
1961 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 }
1963
1964 if (nargs + nkeywords + ngens > 255) {
1965 ast_error(n, "more than 255 arguments");
1966 return NULL;
1967 }
1968
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001969 args = asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001971 return NULL;
1972 keywords = asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 nargs = 0;
1976 nkeywords = 0;
1977 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001978 node *ch = CHILD(n, i);
1979 if (TYPE(ch) == argument) {
1980 expr_ty e;
1981 if (NCH(ch) == 1) {
1982 if (nkeywords) {
1983 ast_error(CHILD(ch, 0),
1984 "non-keyword arg after keyword arg");
1985 return NULL;
1986 }
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00001987 if (vararg) {
1988 ast_error(CHILD(ch, 0),
1989 "only named arguments may follow *expression");
1990 return NULL;
1991 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001992 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001994 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001995 asdl_seq_SET(args, nargs++, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001997 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001998 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002000 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002001 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002003 else {
2004 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002005 identifier key, tmp;
2006 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 /* CHILD(ch, 0) is test, but must be an identifier? */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002009 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002011 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 /* f(lambda x: x[0] = 3) ends up getting parsed with
2013 * LHS test = lambda x: x[0], and RHS test = 3.
2014 * SF bug 132313 points out that complaining about a keyword
2015 * then is very confusing.
2016 */
2017 if (e->kind == Lambda_kind) {
2018 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002019 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 } else if (e->kind != Name_kind) {
2021 ast_error(CHILD(ch, 0), "keyword can't be an expression");
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002022 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00002023 } else if (forbidden_name(e->v.Name.id, ch, 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 return NULL;
2025 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002026 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002027 for (k = 0; k < nkeywords; k++) {
2028 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
2029 if (!PyUnicode_Compare(tmp, key)) {
2030 ast_error(CHILD(ch, 0), "keyword argument repeated");
2031 return NULL;
2032 }
2033 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002034 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002036 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002037 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002039 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002040 asdl_seq_SET(keywords, nkeywords++, kw);
2041 }
2042 }
2043 else if (TYPE(ch) == STAR) {
2044 vararg = ast_for_expr(c, CHILD(n, i+1));
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002045 if (!vararg)
2046 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002047 i++;
2048 }
2049 else if (TYPE(ch) == DOUBLESTAR) {
2050 kwarg = ast_for_expr(c, CHILD(n, i+1));
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002051 if (!kwarg)
2052 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002053 i++;
2054 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 }
2056
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002057 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058}
2059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002061ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002063 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002064 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002066 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002067 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002068 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002069 }
2070 else {
2071 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002072 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002073 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002075 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 else {
2077 asdl_seq *tmp = seq_for_testlist(c, n);
2078 if (!tmp)
2079 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002080 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002082}
2083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084static stmt_ty
2085ast_for_expr_stmt(struct compiling *c, const node *n)
2086{
2087 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002090 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002092 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 test: ... here starts the operator precendence dance
2094 */
2095
2096 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002097 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 if (!e)
2099 return NULL;
2100
Thomas Wouters89f507f2006-12-13 04:49:30 +00002101 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 }
2103 else if (TYPE(CHILD(n, 1)) == augassign) {
2104 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002105 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002106 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107
Thomas Wouters89f507f2006-12-13 04:49:30 +00002108 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 if (!expr1)
2110 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002111 if(!set_context(c, expr1, Store, ch))
2112 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002113 /* set_context checks that most expressions are not the left side.
2114 Augmented assignments can only have a name, a subscript, or an
2115 attribute on the left, though, so we have to explicitly check for
2116 those. */
2117 switch (expr1->kind) {
2118 case Name_kind:
2119 case Attribute_kind:
2120 case Subscript_kind:
2121 break;
2122 default:
2123 ast_error(ch, "illegal expression for augmented assignment");
2124 return NULL;
2125 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126
Thomas Wouters89f507f2006-12-13 04:49:30 +00002127 ch = CHILD(n, 2);
2128 if (TYPE(ch) == testlist)
2129 expr2 = ast_for_testlist(c, ch);
2130 else
2131 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002132 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 return NULL;
2134
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002135 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002136 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 return NULL;
2138
Thomas Wouters89f507f2006-12-13 04:49:30 +00002139 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 }
2141 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002142 int i;
2143 asdl_seq *targets;
2144 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 expr_ty expression;
2146
Thomas Wouters89f507f2006-12-13 04:49:30 +00002147 /* a normal assignment */
2148 REQ(CHILD(n, 1), EQUAL);
2149 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2150 if (!targets)
2151 return NULL;
2152 for (i = 0; i < NCH(n) - 2; i += 2) {
2153 expr_ty e;
2154 node *ch = CHILD(n, i);
2155 if (TYPE(ch) == yield_expr) {
2156 ast_error(ch, "assignment to yield expression not possible");
2157 return NULL;
2158 }
2159 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002161 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002163 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002164 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002165 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 asdl_seq_SET(targets, i / 2, e);
2168 }
2169 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002170 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002171 expression = ast_for_testlist(c, value);
2172 else
2173 expression = ast_for_expr(c, value);
2174 if (!expression)
2175 return NULL;
2176 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178}
2179
Benjamin Peterson78565b22009-06-28 19:19:51 +00002180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002182ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183{
2184 asdl_seq *seq;
2185 int i;
2186 expr_ty e;
2187
2188 REQ(n, exprlist);
2189
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002190 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002192 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002194 e = ast_for_expr(c, CHILD(n, i));
2195 if (!e)
2196 return NULL;
2197 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002198 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 }
2201 return seq;
2202}
2203
2204static stmt_ty
2205ast_for_del_stmt(struct compiling *c, const node *n)
2206{
2207 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 /* del_stmt: 'del' exprlist */
2210 REQ(n, del_stmt);
2211
2212 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2213 if (!expr_list)
2214 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002215 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216}
2217
2218static stmt_ty
2219ast_for_flow_stmt(struct compiling *c, const node *n)
2220{
2221 /*
2222 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2223 | yield_stmt
2224 break_stmt: 'break'
2225 continue_stmt: 'continue'
2226 return_stmt: 'return' [testlist]
2227 yield_stmt: yield_expr
2228 yield_expr: 'yield' testlist
2229 raise_stmt: 'raise' [test [',' test [',' test]]]
2230 */
2231 node *ch;
2232
2233 REQ(n, flow_stmt);
2234 ch = CHILD(n, 0);
2235 switch (TYPE(ch)) {
2236 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002237 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002239 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002241 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2242 if (!exp)
2243 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002244 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 }
2246 case return_stmt:
2247 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002248 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002250 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 if (!expression)
2252 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002253 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 }
2255 case raise_stmt:
2256 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002257 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2258 else if (NCH(ch) >= 2) {
2259 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2261 if (!expression)
2262 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002263 if (NCH(ch) == 4) {
2264 cause = ast_for_expr(c, CHILD(ch, 3));
2265 if (!cause)
2266 return NULL;
2267 }
2268 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 }
2270 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002271 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 "unexpected flow_stmt: %d", TYPE(ch));
2273 return NULL;
2274 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002275
2276 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2277 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278}
2279
2280static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00002281alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282{
2283 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002284 import_as_name: NAME ['as' NAME]
2285 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 dotted_name: NAME ('.' NAME)*
2287 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00002288 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002289
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 loop:
2291 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05002292 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002293 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002294 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002295 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002296 if (!name)
2297 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002298 if (NCH(n) == 3) {
2299 node *str_node = CHILD(n, 2);
2300 str = NEW_IDENTIFIER(str_node);
2301 if (!str)
2302 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00002303 if (store && forbidden_name(str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002304 return NULL;
2305 }
2306 else {
Benjamin Peterson70f52762009-06-28 23:32:44 +00002307 if (forbidden_name(name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002308 return NULL;
2309 }
Benjamin Peterson30760062008-11-25 04:02:28 +00002310 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002311 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 case dotted_as_name:
2313 if (NCH(n) == 1) {
2314 n = CHILD(n, 0);
2315 goto loop;
2316 }
2317 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002318 node *asname_node = CHILD(n, 2);
2319 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002320 if (!a)
2321 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002323 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002324 if (!a->asname)
2325 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00002326 if (forbidden_name(a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002327 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 return a;
2329 }
2330 break;
2331 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00002332 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002333 node *name_node = CHILD(n, 0);
2334 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002335 if (!name)
2336 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00002337 if (store && forbidden_name(name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002338 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00002339 return alias(name, NULL, c->c_arena);
2340 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 else {
2342 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002343 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002344 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347
2348 len = 0;
2349 for (i = 0; i < NCH(n); i += 2)
2350 /* length of string plus one for the dot */
2351 len += strlen(STR(CHILD(n, i))) + 1;
2352 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00002353 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 if (!str)
2355 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00002356 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 if (!s)
2358 return NULL;
2359 for (i = 0; i < NCH(n); i += 2) {
2360 char *sch = STR(CHILD(n, i));
2361 strcpy(s, STR(CHILD(n, i)));
2362 s += strlen(sch);
2363 *s++ = '.';
2364 }
2365 --s;
2366 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
2368 PyBytes_GET_SIZE(str),
2369 NULL);
2370 Py_DECREF(str);
2371 if (!uni)
2372 return NULL;
2373 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002374 PyUnicode_InternInPlace(&str);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002375 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002376 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 }
2378 break;
2379 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00002380 str = PyUnicode_InternFromString("*");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002381 PyArena_AddPyObject(c->c_arena, str);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002382 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002384 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 "unexpected import name: %d", TYPE(n));
2386 return NULL;
2387 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002388
2389 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 return NULL;
2391}
2392
2393static stmt_ty
2394ast_for_import_stmt(struct compiling *c, const node *n)
2395{
2396 /*
2397 import_stmt: import_name | import_from
2398 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00002399 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
2400 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002402 int lineno;
2403 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 int i;
2405 asdl_seq *aliases;
2406
2407 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002408 lineno = LINENO(n);
2409 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002411 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002413 REQ(n, dotted_as_names);
2414 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2415 if (!aliases)
2416 return NULL;
2417 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002418 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002419 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002421 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002423 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002425 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002427 int idx, ndots = 0;
2428 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002429 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002431 /* Count the number of dots (for relative imports) and check for the
2432 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002433 for (idx = 1; idx < NCH(n); idx++) {
2434 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002435 mod = alias_for_import_name(c, CHILD(n, idx), 0);
2436 if (!mod)
2437 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002438 idx++;
2439 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00002440 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00002442 ndots += 3;
2443 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002444 } else if (TYPE(CHILD(n, idx)) != DOT) {
2445 break;
2446 }
2447 ndots++;
2448 }
2449 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002450 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002451 case STAR:
2452 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002453 n = CHILD(n, idx);
2454 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002455 break;
2456 case LPAR:
2457 /* from ... import (x, y, z) */
2458 n = CHILD(n, idx + 1);
2459 n_children = NCH(n);
2460 break;
2461 case import_as_names:
2462 /* from ... import x, y, z */
2463 n = CHILD(n, idx);
2464 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00002465 if (n_children % 2 == 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 ast_error(n, "trailing comma not allowed without"
2467 " surrounding parentheses");
2468 return NULL;
2469 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002470 break;
2471 default:
2472 ast_error(n, "Unexpected node-type in from-import");
2473 return NULL;
2474 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475
Thomas Wouters89f507f2006-12-13 04:49:30 +00002476 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2477 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479
2480 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002481 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002482 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002483 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002485 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002487 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002488 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002489 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002490 if (!import_alias)
2491 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002492 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002493 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002495 if (mod != NULL)
2496 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002497 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002498 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 }
Neal Norwitz79792652005-11-14 04:25:03 +00002500 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 "unknown import statement: starts with command '%s'",
2502 STR(CHILD(n, 0)));
2503 return NULL;
2504}
2505
2506static stmt_ty
2507ast_for_global_stmt(struct compiling *c, const node *n)
2508{
2509 /* global_stmt: 'global' NAME (',' NAME)* */
2510 identifier name;
2511 asdl_seq *s;
2512 int i;
2513
2514 REQ(n, global_stmt);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002515 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002517 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002519 name = NEW_IDENTIFIER(CHILD(n, i));
2520 if (!name)
2521 return NULL;
2522 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002524 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525}
2526
2527static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00002528ast_for_nonlocal_stmt(struct compiling *c, const node *n)
2529{
2530 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
2531 identifier name;
2532 asdl_seq *s;
2533 int i;
2534
2535 REQ(n, nonlocal_stmt);
2536 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
2537 if (!s)
2538 return NULL;
2539 for (i = 1; i < NCH(n); i += 2) {
2540 name = NEW_IDENTIFIER(CHILD(n, i));
2541 if (!name)
2542 return NULL;
2543 asdl_seq_SET(s, i / 2, name);
2544 }
2545 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
2546}
2547
2548static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549ast_for_assert_stmt(struct compiling *c, const node *n)
2550{
2551 /* assert_stmt: 'assert' test [',' test] */
2552 REQ(n, assert_stmt);
2553 if (NCH(n) == 2) {
2554 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2555 if (!expression)
2556 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002557 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 }
2559 else if (NCH(n) == 4) {
2560 expr_ty expr1, expr2;
2561
2562 expr1 = ast_for_expr(c, CHILD(n, 1));
2563 if (!expr1)
2564 return NULL;
2565 expr2 = ast_for_expr(c, CHILD(n, 3));
2566 if (!expr2)
2567 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568
Thomas Wouters89f507f2006-12-13 04:49:30 +00002569 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 }
Neal Norwitz79792652005-11-14 04:25:03 +00002571 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 "improper number of parts to 'assert' statement: %d",
2573 NCH(n));
2574 return NULL;
2575}
2576
2577static asdl_seq *
2578ast_for_suite(struct compiling *c, const node *n)
2579{
2580 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00002581 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 stmt_ty s;
2583 int i, total, num, end, pos = 0;
2584 node *ch;
2585
2586 REQ(n, suite);
2587
2588 total = num_stmts(n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002589 seq = asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002591 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002593 n = CHILD(n, 0);
2594 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00002596 */
2597 end = NCH(n) - 1;
2598 if (TYPE(CHILD(n, end - 1)) == SEMI)
2599 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002601 for (i = 0; i < end; i += 2) {
2602 ch = CHILD(n, i);
2603 s = ast_for_stmt(c, ch);
2604 if (!s)
2605 return NULL;
2606 asdl_seq_SET(seq, pos++, s);
2607 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 }
2609 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002610 for (i = 2; i < (NCH(n) - 1); i++) {
2611 ch = CHILD(n, i);
2612 REQ(ch, stmt);
2613 num = num_stmts(ch);
2614 if (num == 1) {
2615 /* small_stmt or compound_stmt with only one child */
2616 s = ast_for_stmt(c, ch);
2617 if (!s)
2618 return NULL;
2619 asdl_seq_SET(seq, pos++, s);
2620 }
2621 else {
2622 int j;
2623 ch = CHILD(ch, 0);
2624 REQ(ch, simple_stmt);
2625 for (j = 0; j < NCH(ch); j += 2) {
2626 /* statement terminates with a semi-colon ';' */
2627 if (NCH(CHILD(ch, j)) == 0) {
2628 assert((j + 1) == NCH(ch));
2629 break;
2630 }
2631 s = ast_for_stmt(c, CHILD(ch, j));
2632 if (!s)
2633 return NULL;
2634 asdl_seq_SET(seq, pos++, s);
2635 }
2636 }
2637 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 }
2639 assert(pos == seq->size);
2640 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641}
2642
2643static stmt_ty
2644ast_for_if_stmt(struct compiling *c, const node *n)
2645{
2646 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2647 ['else' ':' suite]
2648 */
2649 char *s;
2650
2651 REQ(n, if_stmt);
2652
2653 if (NCH(n) == 4) {
2654 expr_ty expression;
2655 asdl_seq *suite_seq;
2656
2657 expression = ast_for_expr(c, CHILD(n, 1));
2658 if (!expression)
2659 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002661 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663
Guido van Rossumd8faa362007-04-27 19:54:29 +00002664 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2665 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002667
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 s = STR(CHILD(n, 4));
2669 /* s[2], the third character in the string, will be
2670 's' for el_s_e, or
2671 'i' for el_i_f
2672 */
2673 if (s[2] == 's') {
2674 expr_ty expression;
2675 asdl_seq *seq1, *seq2;
2676
2677 expression = ast_for_expr(c, CHILD(n, 1));
2678 if (!expression)
2679 return NULL;
2680 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002681 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 return NULL;
2683 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002684 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685 return NULL;
2686
Guido van Rossumd8faa362007-04-27 19:54:29 +00002687 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2688 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 }
2690 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002691 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002692 expr_ty expression;
2693 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002694 asdl_seq *orelse = NULL;
2695 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 /* must reference the child n_elif+1 since 'else' token is third,
2697 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002698 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2699 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2700 has_else = 1;
2701 n_elif -= 3;
2702 }
2703 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704
Thomas Wouters89f507f2006-12-13 04:49:30 +00002705 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00002706 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707
Thomas Wouters89f507f2006-12-13 04:49:30 +00002708 orelse = asdl_seq_new(1, c->c_arena);
2709 if (!orelse)
2710 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002712 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002714 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2715 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002717 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2718 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 asdl_seq_SET(orelse, 0,
2722 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002723 LINENO(CHILD(n, NCH(n) - 6)),
2724 CHILD(n, NCH(n) - 6)->n_col_offset,
2725 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002726 /* the just-created orelse handled the last elif */
2727 n_elif--;
2728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729
Thomas Wouters89f507f2006-12-13 04:49:30 +00002730 for (i = 0; i < n_elif; i++) {
2731 int off = 5 + (n_elif - i - 1) * 4;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002732 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2733 if (!newobj)
2734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002736 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002739 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741
Thomas Wouters89f507f2006-12-13 04:49:30 +00002742 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002744 LINENO(CHILD(n, off)),
2745 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002746 orelse = newobj;
2747 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002748 expression = ast_for_expr(c, CHILD(n, 1));
2749 if (!expression)
2750 return NULL;
2751 suite_seq = ast_for_suite(c, CHILD(n, 3));
2752 if (!suite_seq)
2753 return NULL;
2754 return If(expression, suite_seq, orelse,
2755 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002757
2758 PyErr_Format(PyExc_SystemError,
2759 "unexpected token in 'if' statement: %s", s);
2760 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761}
2762
2763static stmt_ty
2764ast_for_while_stmt(struct compiling *c, const node *n)
2765{
2766 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2767 REQ(n, while_stmt);
2768
2769 if (NCH(n) == 4) {
2770 expr_ty expression;
2771 asdl_seq *suite_seq;
2772
2773 expression = ast_for_expr(c, CHILD(n, 1));
2774 if (!expression)
2775 return NULL;
2776 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002777 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002779 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 }
2781 else if (NCH(n) == 7) {
2782 expr_ty expression;
2783 asdl_seq *seq1, *seq2;
2784
2785 expression = ast_for_expr(c, CHILD(n, 1));
2786 if (!expression)
2787 return NULL;
2788 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002789 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 return NULL;
2791 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002792 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 return NULL;
2794
Thomas Wouters89f507f2006-12-13 04:49:30 +00002795 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002797
2798 PyErr_Format(PyExc_SystemError,
2799 "wrong number of tokens for 'while' statement: %d",
2800 NCH(n));
2801 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802}
2803
2804static stmt_ty
2805ast_for_for_stmt(struct compiling *c, const node *n)
2806{
Neal Norwitz84456bd2005-12-18 03:16:20 +00002807 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002809 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002810 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2812 REQ(n, for_stmt);
2813
2814 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 if (!seq)
2817 return NULL;
2818 }
2819
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002820 node_target = CHILD(n, 1);
2821 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002822 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002824 /* Check the # of children rather than the length of _target, since
2825 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002826 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002827 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002828 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002830 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002832 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002833 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 return NULL;
2835 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002836 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 return NULL;
2838
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002839 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2840 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841}
2842
2843static excepthandler_ty
2844ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2845{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002846 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 REQ(exc, except_clause);
2848 REQ(body, suite);
2849
2850 if (NCH(exc) == 1) {
2851 asdl_seq *suite_seq = ast_for_suite(c, body);
2852 if (!suite_seq)
2853 return NULL;
2854
Neal Norwitzad74aa82008-03-31 05:14:30 +00002855 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002856 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 }
2858 else if (NCH(exc) == 2) {
2859 expr_ty expression;
2860 asdl_seq *suite_seq;
2861
2862 expression = ast_for_expr(c, CHILD(exc, 1));
2863 if (!expression)
2864 return NULL;
2865 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002866 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 return NULL;
2868
Neal Norwitzad74aa82008-03-31 05:14:30 +00002869 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002870 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 }
2872 else if (NCH(exc) == 4) {
2873 asdl_seq *suite_seq;
2874 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00002875 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002876 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00002878 if (forbidden_name(e, CHILD(exc, 3), 0))
2879 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00002881 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 return NULL;
2883 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002884 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 return NULL;
2886
Neal Norwitzad74aa82008-03-31 05:14:30 +00002887 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002888 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002890
2891 PyErr_Format(PyExc_SystemError,
2892 "wrong number of children for 'except' clause: %d",
2893 NCH(exc));
2894 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895}
2896
2897static stmt_ty
2898ast_for_try_stmt(struct compiling *c, const node *n)
2899{
Neal Norwitzf599f422005-12-17 21:33:47 +00002900 const int nch = NCH(n);
2901 int n_except = (nch - 3)/3;
2902 asdl_seq *body, *orelse = NULL, *finally = NULL;
2903
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 REQ(n, try_stmt);
2905
Neal Norwitzf599f422005-12-17 21:33:47 +00002906 body = ast_for_suite(c, CHILD(n, 2));
2907 if (body == NULL)
2908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909
Neal Norwitzf599f422005-12-17 21:33:47 +00002910 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2911 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2912 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2913 /* we can assume it's an "else",
2914 because nch >= 9 for try-else-finally and
2915 it would otherwise have a type of except_clause */
2916 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2917 if (orelse == NULL)
2918 return NULL;
2919 n_except--;
2920 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921
Neal Norwitzf599f422005-12-17 21:33:47 +00002922 finally = ast_for_suite(c, CHILD(n, nch - 1));
2923 if (finally == NULL)
2924 return NULL;
2925 n_except--;
2926 }
2927 else {
2928 /* we can assume it's an "else",
2929 otherwise it would have a type of except_clause */
2930 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2931 if (orelse == NULL)
2932 return NULL;
2933 n_except--;
2934 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935 }
Neal Norwitzf599f422005-12-17 21:33:47 +00002936 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002937 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 return NULL;
2939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940
Neal Norwitzf599f422005-12-17 21:33:47 +00002941 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002942 int i;
2943 stmt_ty except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002944 /* process except statements to create a try ... except */
2945 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2946 if (handlers == NULL)
2947 return NULL;
2948
2949 for (i = 0; i < n_except; i++) {
2950 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2951 CHILD(n, 5 + i * 3));
2952 if (!e)
2953 return NULL;
2954 asdl_seq_SET(handlers, i, e);
2955 }
2956
Thomas Wouters89f507f2006-12-13 04:49:30 +00002957 except_st = TryExcept(body, handlers, orelse, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002958 n->n_col_offset, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00002959 if (!finally)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002960 return except_st;
Neal Norwitzf599f422005-12-17 21:33:47 +00002961
2962 /* if a 'finally' is present too, we nest the TryExcept within a
2963 TryFinally to emulate try ... except ... finally */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002964 body = asdl_seq_new(1, c->c_arena);
2965 if (body == NULL)
2966 return NULL;
2967 asdl_seq_SET(body, 0, except_st);
Neal Norwitzf599f422005-12-17 21:33:47 +00002968 }
2969
2970 /* must be a try ... finally (except clauses are in body, if any exist) */
2971 assert(finally != NULL);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002972 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973}
2974
Georg Brandl0c315622009-05-25 21:10:36 +00002975/* with_item: test ['as' expr] */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002976static stmt_ty
Georg Brandl0c315622009-05-25 21:10:36 +00002977ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002978{
2979 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002980
Georg Brandl0c315622009-05-25 21:10:36 +00002981 REQ(n, with_item);
2982 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002983 if (!context_expr)
2984 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00002985 if (NCH(n) == 3) {
2986 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00002987
2988 if (!optional_vars) {
2989 return NULL;
2990 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002991 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002992 return NULL;
2993 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002994 }
2995
Georg Brandl0c315622009-05-25 21:10:36 +00002996 return With(context_expr, optional_vars, content, LINENO(n),
Thomas Wouters89f507f2006-12-13 04:49:30 +00002997 n->n_col_offset, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002998}
2999
Georg Brandl0c315622009-05-25 21:10:36 +00003000/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3001static stmt_ty
3002ast_for_with_stmt(struct compiling *c, const node *n)
3003{
3004 int i;
3005 stmt_ty ret;
3006 asdl_seq *inner;
3007
3008 REQ(n, with_stmt);
3009
3010 /* process the with items inside-out */
3011 i = NCH(n) - 1;
3012 /* the suite of the innermost with item is the suite of the with stmt */
3013 inner = ast_for_suite(c, CHILD(n, i));
3014 if (!inner)
3015 return NULL;
3016
3017 for (;;) {
3018 i -= 2;
3019 ret = ast_for_with_item(c, CHILD(n, i), inner);
3020 if (!ret)
3021 return NULL;
3022 /* was this the last item? */
3023 if (i == 1)
3024 break;
3025 /* if not, wrap the result so far in a new sequence */
3026 inner = asdl_seq_new(1, c->c_arena);
3027 if (!inner)
3028 return NULL;
3029 asdl_seq_SET(inner, 0, ret);
3030 }
3031
3032 return ret;
3033}
3034
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003036ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003038 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003039 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003040 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003041 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043 REQ(n, classdef);
3044
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003045 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046 s = ast_for_suite(c, CHILD(n, 3));
3047 if (!s)
3048 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003049 classname = NEW_IDENTIFIER(CHILD(n, 1));
3050 if (!classname)
3051 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00003052 if (forbidden_name(classname, CHILD(n, 3), 0))
3053 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003054 return ClassDef(classname, NULL, NULL, NULL, NULL, s, decorator_seq,
3055 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003057
3058 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003059 s = ast_for_suite(c, CHILD(n,5));
3060 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003061 return NULL;
3062 classname = NEW_IDENTIFIER(CHILD(n, 1));
3063 if (!classname)
3064 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00003065 if (forbidden_name(classname, CHILD(n, 3), 0))
3066 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003067 return ClassDef(classname, NULL, NULL, NULL, NULL, s, decorator_seq,
3068 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003069 }
3070
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003071 /* class NAME '(' arglist ')' ':' suite */
3072 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003073 {
3074 PyObject *dummy_name;
3075 expr_ty dummy;
3076 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3077 if (!dummy_name)
3078 return NULL;
3079 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3080 call = ast_for_call(c, CHILD(n, 3), dummy);
3081 if (!call)
3082 return NULL;
3083 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003085 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003087 classname = NEW_IDENTIFIER(CHILD(n, 1));
3088 if (!classname)
3089 return NULL;
Benjamin Peterson70f52762009-06-28 23:32:44 +00003090 if (forbidden_name(classname, CHILD(n, 1), 0))
3091 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003092
Benjamin Peterson30760062008-11-25 04:02:28 +00003093 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords,
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003094 call->v.Call.starargs, call->v.Call.kwargs, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003095 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096}
3097
3098static stmt_ty
3099ast_for_stmt(struct compiling *c, const node *n)
3100{
3101 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003102 assert(NCH(n) == 1);
3103 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 }
3105 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003106 assert(num_stmts(n) == 1);
3107 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 }
3109 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003110 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003111 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3112 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003113 */
3114 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 case expr_stmt:
3116 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 case del_stmt:
3118 return ast_for_del_stmt(c, n);
3119 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003120 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 case flow_stmt:
3122 return ast_for_flow_stmt(c, n);
3123 case import_stmt:
3124 return ast_for_import_stmt(c, n);
3125 case global_stmt:
3126 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003127 case nonlocal_stmt:
3128 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 case assert_stmt:
3130 return ast_for_assert_stmt(c, n);
3131 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003132 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3134 TYPE(n), NCH(n));
3135 return NULL;
3136 }
3137 }
3138 else {
3139 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003140 | funcdef | classdef | decorated
Thomas Wouters89f507f2006-12-13 04:49:30 +00003141 */
3142 node *ch = CHILD(n, 0);
3143 REQ(n, compound_stmt);
3144 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 case if_stmt:
3146 return ast_for_if_stmt(c, ch);
3147 case while_stmt:
3148 return ast_for_while_stmt(c, ch);
3149 case for_stmt:
3150 return ast_for_for_stmt(c, ch);
3151 case try_stmt:
3152 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003153 case with_stmt:
3154 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003156 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003158 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 case decorated:
3160 return ast_for_decorated(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003162 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3164 TYPE(n), NCH(n));
3165 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003166 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 }
3168}
3169
3170static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003171parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003173 const char *end;
3174 long x;
3175 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003176 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003177 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003179 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003180 errno = 0;
3181 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003182 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003183 if (s[0] == '0') {
3184 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3185 if (x < 0 && errno == 0) {
3186 return PyLong_FromString((char *)s,
3187 (char **)0,
3188 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003189 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003190 }
3191 else
3192 x = PyOS_strtol((char *)s, (char **)&end, 0);
3193 if (*end == '\0') {
3194 if (errno != 0)
3195 return PyLong_FromString((char *)s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003196 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003197 }
3198 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003199 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003200 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003201 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3202 if (compl.imag == -1.0 && PyErr_Occurred())
3203 return NULL;
3204 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003205 }
3206 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003207 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003208 dx = PyOS_string_to_double(s, NULL, NULL);
3209 if (dx == -1.0 && PyErr_Occurred())
3210 return NULL;
3211 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003212 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213}
3214
3215static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003216decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003218 PyObject *u, *v;
3219 char *s, *t;
3220 t = s = (char *)*sPtr;
3221 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3222 while (s < end && (*s & 0x80)) s++;
3223 *sPtr = s;
3224 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3225 if (u == NULL)
3226 return NULL;
3227 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3228 Py_DECREF(u);
3229 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230}
3231
3232static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003233decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003235 PyObject *v, *u;
3236 char *buf;
3237 char *p;
3238 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003239
Guido van Rossumd8faa362007-04-27 19:54:29 +00003240 if (encoding == NULL) {
3241 buf = (char *)s;
3242 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003243 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003244 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003245 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003246 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003247 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3248 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3249 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003250 if (u == NULL)
3251 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003252 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003253 end = s + len;
3254 while (s < end) {
3255 if (*s == '\\') {
3256 *p++ = *s++;
3257 if (*s & 0x80) {
3258 strcpy(p, "u005c");
3259 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003260 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003261 }
3262 if (*s & 0x80) { /* XXX inefficient */
3263 PyObject *w;
3264 char *r;
3265 Py_ssize_t rn, i;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003266 w = decode_utf8(c, &s, end, "utf-32-be");
Guido van Rossumd8faa362007-04-27 19:54:29 +00003267 if (w == NULL) {
3268 Py_DECREF(u);
3269 return NULL;
3270 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003271 r = PyBytes_AS_STRING(w);
Christian Heimes90aa7642007-12-19 02:45:37 +00003272 rn = Py_SIZE(w);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003273 assert(rn % 4 == 0);
3274 for (i = 0; i < rn; i += 4) {
3275 sprintf(p, "\\U%02x%02x%02x%02x",
Guido van Rossumd8faa362007-04-27 19:54:29 +00003276 r[i + 0] & 0xFF,
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003277 r[i + 1] & 0xFF,
3278 r[i + 2] & 0xFF,
3279 r[i + 3] & 0xFF);
3280 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003281 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003282 /* Should be impossible to overflow */
3283 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003284 Py_DECREF(w);
3285 } else {
3286 *p++ = *s++;
3287 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003288 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003289 len = p - buf;
3290 s = buf;
3291 }
3292 if (rawmode)
3293 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3294 else
3295 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3296 Py_XDECREF(u);
3297 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298}
3299
3300/* s is a Python string literal, including the bracketing quote characters,
Guido van Rossum98297ee2007-11-06 21:34:58 +00003301 * and r &/or b prefixes (if any), and embedded escape sequences (if any).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 * parsestr parses it, and returns the decoded Python string object.
3303 */
3304static PyObject *
Christian Heimes4d6ec852008-03-26 22:34:47 +00003305parsestr(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003307 size_t len;
3308 const char *s = STR(n);
3309 int quote = Py_CHARMASK(*s);
3310 int rawmode = 0;
3311 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01003312 if (Py_ISALPHA(quote)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003313 if (quote == 'b' || quote == 'B') {
3314 quote = *++s;
3315 *bytesmode = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003317 if (quote == 'r' || quote == 'R') {
3318 quote = *++s;
3319 rawmode = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003321 }
3322 if (quote != '\'' && quote != '\"') {
3323 PyErr_BadInternalCall();
3324 return NULL;
3325 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003326 s++;
3327 len = strlen(s);
3328 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003330 "string to parse is too long");
3331 return NULL;
3332 }
3333 if (s[--len] != quote) {
3334 PyErr_BadInternalCall();
3335 return NULL;
3336 }
3337 if (len >= 4 && s[0] == quote && s[1] == quote) {
3338 s += 2;
3339 len -= 2;
3340 if (s[--len] != quote || s[--len] != quote) {
3341 PyErr_BadInternalCall();
3342 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003343 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003344 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00003345 if (!*bytesmode && !rawmode) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003346 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003347 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003348 if (*bytesmode) {
3349 /* Disallow non-ascii characters (but not escapes) */
3350 const char *c;
3351 for (c = s; *c; c++) {
3352 if (Py_CHARMASK(*c) >= 0x80) {
3353 ast_error(n, "bytes can only contain ASCII "
3354 "literal characters.");
3355 return NULL;
3356 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00003357 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003358 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00003359 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00003360 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003361 if (rawmode || strchr(s, '\\') == NULL) {
3362 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003363 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00003364 if (u == NULL || !*bytesmode)
3365 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00003366 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003367 Py_DECREF(u);
3368 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003369 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00003370 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00003371 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00003372 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00003374 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003375 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003376 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003377 return PyBytes_DecodeEscape(s, len, NULL, 1,
Christian Heimes4d6ec852008-03-26 22:34:47 +00003378 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379}
3380
Guido van Rossum29fd7122007-11-12 01:13:56 +00003381/* Build a Python string object out of a STRING+ atom. This takes care of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382 * compile-time literal catenation, calling parsestr() on each piece, and
3383 * pasting the intermediate results together.
3384 */
3385static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00003386parsestrplus(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003388 PyObject *v;
3389 int i;
3390 REQ(CHILD(n, 0), STRING);
Christian Heimes4d6ec852008-03-26 22:34:47 +00003391 v = parsestr(c, CHILD(n, 0), bytesmode);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003392 if (v != NULL) {
3393 /* String literal concatenation */
3394 for (i = 1; i < NCH(n); i++) {
3395 PyObject *s;
3396 int subbm = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00003397 s = parsestr(c, CHILD(n, i), &subbm);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003398 if (s == NULL)
3399 goto onError;
3400 if (*bytesmode != subbm) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00003401 ast_error(n, "cannot mix bytes and nonbytes literals");
Christian Heimes3d463392012-09-10 16:52:42 +02003402 Py_DECREF(s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003403 goto onError;
3404 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003405 if (PyBytes_Check(v) && PyBytes_Check(s)) {
3406 PyBytes_ConcatAndDel(&v, s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003407 if (v == NULL)
3408 goto onError;
3409 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003410 else {
3411 PyObject *temp = PyUnicode_Concat(v, s);
3412 Py_DECREF(s);
3413 Py_DECREF(v);
3414 v = temp;
3415 if (v == NULL)
3416 goto onError;
3417 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003418 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003419 }
3420 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421
Guido van Rossumd8faa362007-04-27 19:54:29 +00003422 onError:
3423 Py_XDECREF(v);
3424 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425}