]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | /*\r |
2 | * This file includes functions to transform a concrete syntax tree (CST) to\r | |
3 | * an abstract syntax tree (AST). The main function is PyAST_FromNode().\r | |
4 | *\r | |
5 | */\r | |
6 | #include "Python.h"\r | |
7 | #include "Python-ast.h"\r | |
8 | #include "grammar.h"\r | |
9 | #include "node.h"\r | |
10 | #include "pyarena.h"\r | |
11 | #include "ast.h"\r | |
12 | #include "token.h"\r | |
13 | #include "parsetok.h"\r | |
14 | #include "graminit.h"\r | |
15 | \r | |
16 | #include <assert.h>\r | |
17 | \r | |
18 | /* Data structure used internally */\r | |
19 | struct compiling {\r | |
20 | char *c_encoding; /* source encoding */\r | |
21 | int c_future_unicode; /* __future__ unicode literals flag */\r | |
22 | PyArena *c_arena; /* arena for allocating memeory */\r | |
23 | const char *c_filename; /* filename */\r | |
24 | };\r | |
25 | \r | |
26 | static asdl_seq *seq_for_testlist(struct compiling *, const node *);\r | |
27 | static expr_ty ast_for_expr(struct compiling *, const node *);\r | |
28 | static stmt_ty ast_for_stmt(struct compiling *, const node *);\r | |
29 | static asdl_seq *ast_for_suite(struct compiling *, const node *);\r | |
30 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *,\r | |
31 | expr_context_ty);\r | |
32 | static expr_ty ast_for_testlist(struct compiling *, const node *);\r | |
33 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);\r | |
34 | static expr_ty ast_for_testlist_comp(struct compiling *, const node *);\r | |
35 | \r | |
36 | /* Note different signature for ast_for_call */\r | |
37 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);\r | |
38 | \r | |
39 | static PyObject *parsenumber(struct compiling *, const char *);\r | |
40 | static PyObject *parsestr(struct compiling *, const char *);\r | |
41 | static PyObject *parsestrplus(struct compiling *, const node *n);\r | |
42 | \r | |
43 | #ifndef LINENO\r | |
44 | #define LINENO(n) ((n)->n_lineno)\r | |
45 | #endif\r | |
46 | \r | |
47 | #define COMP_GENEXP 0\r | |
48 | #define COMP_SETCOMP 1\r | |
49 | \r | |
50 | static identifier\r | |
51 | new_identifier(const char* n, PyArena *arena) {\r | |
52 | PyObject* id = PyString_InternFromString(n);\r | |
53 | if (id != NULL)\r | |
54 | PyArena_AddPyObject(arena, id);\r | |
55 | return id;\r | |
56 | }\r | |
57 | \r | |
58 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)\r | |
59 | \r | |
60 | /* This routine provides an invalid object for the syntax error.\r | |
61 | The outermost routine must unpack this error and create the\r | |
62 | proper object. We do this so that we don't have to pass\r | |
63 | the filename to everything function.\r | |
64 | \r | |
65 | XXX Maybe we should just pass the filename...\r | |
66 | */\r | |
67 | \r | |
68 | static int\r | |
69 | ast_error(const node *n, const char *errstr)\r | |
70 | {\r | |
71 | PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));\r | |
72 | if (!u)\r | |
73 | return 0;\r | |
74 | PyErr_SetObject(PyExc_SyntaxError, u);\r | |
75 | Py_DECREF(u);\r | |
76 | return 0;\r | |
77 | }\r | |
78 | \r | |
79 | static void\r | |
80 | ast_error_finish(const char *filename)\r | |
81 | {\r | |
82 | PyObject *type, *value, *tback, *errstr, *loc, *tmp;\r | |
83 | long lineno;\r | |
84 | \r | |
85 | assert(PyErr_Occurred());\r | |
86 | if (!PyErr_ExceptionMatches(PyExc_SyntaxError))\r | |
87 | return;\r | |
88 | \r | |
89 | PyErr_Fetch(&type, &value, &tback);\r | |
90 | errstr = PyTuple_GetItem(value, 0);\r | |
91 | if (!errstr)\r | |
92 | return;\r | |
93 | Py_INCREF(errstr);\r | |
94 | lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));\r | |
95 | if (lineno == -1) {\r | |
96 | Py_DECREF(errstr);\r | |
97 | return;\r | |
98 | }\r | |
99 | Py_DECREF(value);\r | |
100 | \r | |
101 | loc = PyErr_ProgramText(filename, lineno);\r | |
102 | if (!loc) {\r | |
103 | Py_INCREF(Py_None);\r | |
104 | loc = Py_None;\r | |
105 | }\r | |
106 | tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);\r | |
107 | Py_DECREF(loc);\r | |
108 | if (!tmp) {\r | |
109 | Py_DECREF(errstr);\r | |
110 | return;\r | |
111 | }\r | |
112 | value = PyTuple_Pack(2, errstr, tmp);\r | |
113 | Py_DECREF(errstr);\r | |
114 | Py_DECREF(tmp);\r | |
115 | if (!value)\r | |
116 | return;\r | |
117 | PyErr_Restore(type, value, tback);\r | |
118 | }\r | |
119 | \r | |
120 | static int\r | |
121 | ast_warn(struct compiling *c, const node *n, char *msg)\r | |
122 | {\r | |
123 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),\r | |
124 | NULL, NULL) < 0) {\r | |
125 | /* if -Werr, change it to a SyntaxError */\r | |
126 | if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))\r | |
127 | ast_error(n, msg);\r | |
128 | return 0;\r | |
129 | }\r | |
130 | return 1;\r | |
131 | }\r | |
132 | \r | |
133 | static int\r | |
134 | forbidden_check(struct compiling *c, const node *n, const char *x)\r | |
135 | {\r | |
136 | if (!strcmp(x, "None"))\r | |
137 | return ast_error(n, "cannot assign to None");\r | |
138 | if (!strcmp(x, "__debug__"))\r | |
139 | return ast_error(n, "cannot assign to __debug__");\r | |
140 | if (Py_Py3kWarningFlag) {\r | |
141 | if (!(strcmp(x, "True") && strcmp(x, "False")) &&\r | |
142 | !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))\r | |
143 | return 0;\r | |
144 | if (!strcmp(x, "nonlocal") &&\r | |
145 | !ast_warn(c, n, "nonlocal is a keyword in 3.x"))\r | |
146 | return 0;\r | |
147 | }\r | |
148 | return 1;\r | |
149 | }\r | |
150 | \r | |
151 | /* num_stmts() returns number of contained statements.\r | |
152 | \r | |
153 | Use this routine to determine how big a sequence is needed for\r | |
154 | the statements in a parse tree. Its raison d'etre is this bit of\r | |
155 | grammar:\r | |
156 | \r | |
157 | stmt: simple_stmt | compound_stmt\r | |
158 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE\r | |
159 | \r | |
160 | A simple_stmt can contain multiple small_stmt elements joined\r | |
161 | by semicolons. If the arg is a simple_stmt, the number of\r | |
162 | small_stmt elements is returned.\r | |
163 | */\r | |
164 | \r | |
165 | static int\r | |
166 | num_stmts(const node *n)\r | |
167 | {\r | |
168 | int i, l;\r | |
169 | node *ch;\r | |
170 | \r | |
171 | switch (TYPE(n)) {\r | |
172 | case single_input:\r | |
173 | if (TYPE(CHILD(n, 0)) == NEWLINE)\r | |
174 | return 0;\r | |
175 | else\r | |
176 | return num_stmts(CHILD(n, 0));\r | |
177 | case file_input:\r | |
178 | l = 0;\r | |
179 | for (i = 0; i < NCH(n); i++) {\r | |
180 | ch = CHILD(n, i);\r | |
181 | if (TYPE(ch) == stmt)\r | |
182 | l += num_stmts(ch);\r | |
183 | }\r | |
184 | return l;\r | |
185 | case stmt:\r | |
186 | return num_stmts(CHILD(n, 0));\r | |
187 | case compound_stmt:\r | |
188 | return 1;\r | |
189 | case simple_stmt:\r | |
190 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */\r | |
191 | case suite:\r | |
192 | if (NCH(n) == 1)\r | |
193 | return num_stmts(CHILD(n, 0));\r | |
194 | else {\r | |
195 | l = 0;\r | |
196 | for (i = 2; i < (NCH(n) - 1); i++)\r | |
197 | l += num_stmts(CHILD(n, i));\r | |
198 | return l;\r | |
199 | }\r | |
200 | default: {\r | |
201 | char buf[128];\r | |
202 | \r | |
203 | sprintf(buf, "Non-statement found: %d %d",\r | |
204 | TYPE(n), NCH(n));\r | |
205 | Py_FatalError(buf);\r | |
206 | }\r | |
207 | }\r | |
208 | assert(0);\r | |
209 | return 0;\r | |
210 | }\r | |
211 | \r | |
212 | /* Transform the CST rooted at node * to the appropriate AST\r | |
213 | */\r | |
214 | \r | |
215 | mod_ty\r | |
216 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,\r | |
217 | PyArena *arena)\r | |
218 | {\r | |
219 | int i, j, k, num;\r | |
220 | asdl_seq *stmts = NULL;\r | |
221 | stmt_ty s;\r | |
222 | node *ch;\r | |
223 | struct compiling c;\r | |
224 | \r | |
225 | if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {\r | |
226 | c.c_encoding = "utf-8";\r | |
227 | if (TYPE(n) == encoding_decl) {\r | |
228 | ast_error(n, "encoding declaration in Unicode string");\r | |
229 | goto error;\r | |
230 | }\r | |
231 | } else if (TYPE(n) == encoding_decl) {\r | |
232 | c.c_encoding = STR(n);\r | |
233 | n = CHILD(n, 0);\r | |
234 | } else {\r | |
235 | c.c_encoding = NULL;\r | |
236 | }\r | |
237 | c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;\r | |
238 | c.c_arena = arena;\r | |
239 | c.c_filename = filename;\r | |
240 | \r | |
241 | k = 0;\r | |
242 | switch (TYPE(n)) {\r | |
243 | case file_input:\r | |
244 | stmts = asdl_seq_new(num_stmts(n), arena);\r | |
245 | if (!stmts)\r | |
246 | return NULL;\r | |
247 | for (i = 0; i < NCH(n) - 1; i++) {\r | |
248 | ch = CHILD(n, i);\r | |
249 | if (TYPE(ch) == NEWLINE)\r | |
250 | continue;\r | |
251 | REQ(ch, stmt);\r | |
252 | num = num_stmts(ch);\r | |
253 | if (num == 1) {\r | |
254 | s = ast_for_stmt(&c, ch);\r | |
255 | if (!s)\r | |
256 | goto error;\r | |
257 | asdl_seq_SET(stmts, k++, s);\r | |
258 | }\r | |
259 | else {\r | |
260 | ch = CHILD(ch, 0);\r | |
261 | REQ(ch, simple_stmt);\r | |
262 | for (j = 0; j < num; j++) {\r | |
263 | s = ast_for_stmt(&c, CHILD(ch, j * 2));\r | |
264 | if (!s)\r | |
265 | goto error;\r | |
266 | asdl_seq_SET(stmts, k++, s);\r | |
267 | }\r | |
268 | }\r | |
269 | }\r | |
270 | return Module(stmts, arena);\r | |
271 | case eval_input: {\r | |
272 | expr_ty testlist_ast;\r | |
273 | \r | |
274 | /* XXX Why not comp_for here? */\r | |
275 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0));\r | |
276 | if (!testlist_ast)\r | |
277 | goto error;\r | |
278 | return Expression(testlist_ast, arena);\r | |
279 | }\r | |
280 | case single_input:\r | |
281 | if (TYPE(CHILD(n, 0)) == NEWLINE) {\r | |
282 | stmts = asdl_seq_new(1, arena);\r | |
283 | if (!stmts)\r | |
284 | goto error;\r | |
285 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,\r | |
286 | arena));\r | |
287 | if (!asdl_seq_GET(stmts, 0))\r | |
288 | goto error;\r | |
289 | return Interactive(stmts, arena);\r | |
290 | }\r | |
291 | else {\r | |
292 | n = CHILD(n, 0);\r | |
293 | num = num_stmts(n);\r | |
294 | stmts = asdl_seq_new(num, arena);\r | |
295 | if (!stmts)\r | |
296 | goto error;\r | |
297 | if (num == 1) {\r | |
298 | s = ast_for_stmt(&c, n);\r | |
299 | if (!s)\r | |
300 | goto error;\r | |
301 | asdl_seq_SET(stmts, 0, s);\r | |
302 | }\r | |
303 | else {\r | |
304 | /* Only a simple_stmt can contain multiple statements. */\r | |
305 | REQ(n, simple_stmt);\r | |
306 | for (i = 0; i < NCH(n); i += 2) {\r | |
307 | if (TYPE(CHILD(n, i)) == NEWLINE)\r | |
308 | break;\r | |
309 | s = ast_for_stmt(&c, CHILD(n, i));\r | |
310 | if (!s)\r | |
311 | goto error;\r | |
312 | asdl_seq_SET(stmts, i / 2, s);\r | |
313 | }\r | |
314 | }\r | |
315 | \r | |
316 | return Interactive(stmts, arena);\r | |
317 | }\r | |
318 | default:\r | |
319 | PyErr_Format(PyExc_SystemError,\r | |
320 | "invalid node %d for PyAST_FromNode", TYPE(n));\r | |
321 | goto error;\r | |
322 | }\r | |
323 | error:\r | |
324 | ast_error_finish(filename);\r | |
325 | return NULL;\r | |
326 | }\r | |
327 | \r | |
328 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)\r | |
329 | */\r | |
330 | \r | |
331 | static operator_ty\r | |
332 | get_operator(const node *n)\r | |
333 | {\r | |
334 | switch (TYPE(n)) {\r | |
335 | case VBAR:\r | |
336 | return BitOr;\r | |
337 | case CIRCUMFLEX:\r | |
338 | return BitXor;\r | |
339 | case AMPER:\r | |
340 | return BitAnd;\r | |
341 | case LEFTSHIFT:\r | |
342 | return LShift;\r | |
343 | case RIGHTSHIFT:\r | |
344 | return RShift;\r | |
345 | case PLUS:\r | |
346 | return Add;\r | |
347 | case MINUS:\r | |
348 | return Sub;\r | |
349 | case STAR:\r | |
350 | return Mult;\r | |
351 | case SLASH:\r | |
352 | return Div;\r | |
353 | case DOUBLESLASH:\r | |
354 | return FloorDiv;\r | |
355 | case PERCENT:\r | |
356 | return Mod;\r | |
357 | default:\r | |
358 | return (operator_ty)0;\r | |
359 | }\r | |
360 | }\r | |
361 | \r | |
362 | /* Set the context ctx for expr_ty e, recursively traversing e.\r | |
363 | \r | |
364 | Only sets context for expr kinds that "can appear in assignment context"\r | |
365 | (according to ../Parser/Python.asdl). For other expr kinds, it sets\r | |
366 | an appropriate syntax error and returns false.\r | |
367 | */\r | |
368 | \r | |
369 | static int\r | |
370 | set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)\r | |
371 | {\r | |
372 | asdl_seq *s = NULL;\r | |
373 | /* If a particular expression type can't be used for assign / delete,\r | |
374 | set expr_name to its name and an error message will be generated.\r | |
375 | */\r | |
376 | const char* expr_name = NULL;\r | |
377 | \r | |
378 | /* The ast defines augmented store and load contexts, but the\r | |
379 | implementation here doesn't actually use them. The code may be\r | |
380 | a little more complex than necessary as a result. It also means\r | |
381 | that expressions in an augmented assignment have a Store context.\r | |
382 | Consider restructuring so that augmented assignment uses\r | |
383 | set_context(), too.\r | |
384 | */\r | |
385 | assert(ctx != AugStore && ctx != AugLoad);\r | |
386 | \r | |
387 | switch (e->kind) {\r | |
388 | case Attribute_kind:\r | |
389 | if (ctx == Store && !forbidden_check(c, n,\r | |
390 | PyBytes_AS_STRING(e->v.Attribute.attr)))\r | |
391 | return 0;\r | |
392 | e->v.Attribute.ctx = ctx;\r | |
393 | break;\r | |
394 | case Subscript_kind:\r | |
395 | e->v.Subscript.ctx = ctx;\r | |
396 | break;\r | |
397 | case Name_kind:\r | |
398 | if (ctx == Store && !forbidden_check(c, n,\r | |
399 | PyBytes_AS_STRING(e->v.Name.id)))\r | |
400 | return 0;\r | |
401 | e->v.Name.ctx = ctx;\r | |
402 | break;\r | |
403 | case List_kind:\r | |
404 | e->v.List.ctx = ctx;\r | |
405 | s = e->v.List.elts;\r | |
406 | break;\r | |
407 | case Tuple_kind:\r | |
408 | if (asdl_seq_LEN(e->v.Tuple.elts)) {\r | |
409 | e->v.Tuple.ctx = ctx;\r | |
410 | s = e->v.Tuple.elts;\r | |
411 | }\r | |
412 | else {\r | |
413 | expr_name = "()";\r | |
414 | }\r | |
415 | break;\r | |
416 | case Lambda_kind:\r | |
417 | expr_name = "lambda";\r | |
418 | break;\r | |
419 | case Call_kind:\r | |
420 | expr_name = "function call";\r | |
421 | break;\r | |
422 | case BoolOp_kind:\r | |
423 | case BinOp_kind:\r | |
424 | case UnaryOp_kind:\r | |
425 | expr_name = "operator";\r | |
426 | break;\r | |
427 | case GeneratorExp_kind:\r | |
428 | expr_name = "generator expression";\r | |
429 | break;\r | |
430 | case Yield_kind:\r | |
431 | expr_name = "yield expression";\r | |
432 | break;\r | |
433 | case ListComp_kind:\r | |
434 | expr_name = "list comprehension";\r | |
435 | break;\r | |
436 | case SetComp_kind:\r | |
437 | expr_name = "set comprehension";\r | |
438 | break;\r | |
439 | case DictComp_kind:\r | |
440 | expr_name = "dict comprehension";\r | |
441 | break;\r | |
442 | case Dict_kind:\r | |
443 | case Set_kind:\r | |
444 | case Num_kind:\r | |
445 | case Str_kind:\r | |
446 | expr_name = "literal";\r | |
447 | break;\r | |
448 | case Compare_kind:\r | |
449 | expr_name = "comparison";\r | |
450 | break;\r | |
451 | case Repr_kind:\r | |
452 | expr_name = "repr";\r | |
453 | break;\r | |
454 | case IfExp_kind:\r | |
455 | expr_name = "conditional expression";\r | |
456 | break;\r | |
457 | default:\r | |
458 | PyErr_Format(PyExc_SystemError,\r | |
459 | "unexpected expression in assignment %d (line %d)",\r | |
460 | e->kind, e->lineno);\r | |
461 | return 0;\r | |
462 | }\r | |
463 | /* Check for error string set by switch */\r | |
464 | if (expr_name) {\r | |
465 | char buf[300];\r | |
466 | PyOS_snprintf(buf, sizeof(buf),\r | |
467 | "can't %s %s",\r | |
468 | ctx == Store ? "assign to" : "delete",\r | |
469 | expr_name);\r | |
470 | return ast_error(n, buf);\r | |
471 | }\r | |
472 | \r | |
473 | /* If the LHS is a list or tuple, we need to set the assignment\r | |
474 | context for all the contained elements.\r | |
475 | */\r | |
476 | if (s) {\r | |
477 | int i;\r | |
478 | \r | |
479 | for (i = 0; i < asdl_seq_LEN(s); i++) {\r | |
480 | if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))\r | |
481 | return 0;\r | |
482 | }\r | |
483 | }\r | |
484 | return 1;\r | |
485 | }\r | |
486 | \r | |
487 | static operator_ty\r | |
488 | ast_for_augassign(struct compiling *c, const node *n)\r | |
489 | {\r | |
490 | REQ(n, augassign);\r | |
491 | n = CHILD(n, 0);\r | |
492 | switch (STR(n)[0]) {\r | |
493 | case '+':\r | |
494 | return Add;\r | |
495 | case '-':\r | |
496 | return Sub;\r | |
497 | case '/':\r | |
498 | if (STR(n)[1] == '/')\r | |
499 | return FloorDiv;\r | |
500 | else\r | |
501 | return Div;\r | |
502 | case '%':\r | |
503 | return Mod;\r | |
504 | case '<':\r | |
505 | return LShift;\r | |
506 | case '>':\r | |
507 | return RShift;\r | |
508 | case '&':\r | |
509 | return BitAnd;\r | |
510 | case '^':\r | |
511 | return BitXor;\r | |
512 | case '|':\r | |
513 | return BitOr;\r | |
514 | case '*':\r | |
515 | if (STR(n)[1] == '*')\r | |
516 | return Pow;\r | |
517 | else\r | |
518 | return Mult;\r | |
519 | default:\r | |
520 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));\r | |
521 | return (operator_ty)0;\r | |
522 | }\r | |
523 | }\r | |
524 | \r | |
525 | static cmpop_ty\r | |
526 | ast_for_comp_op(struct compiling *c, const node *n)\r | |
527 | {\r | |
528 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'\r | |
529 | |'is' 'not'\r | |
530 | */\r | |
531 | REQ(n, comp_op);\r | |
532 | if (NCH(n) == 1) {\r | |
533 | n = CHILD(n, 0);\r | |
534 | switch (TYPE(n)) {\r | |
535 | case LESS:\r | |
536 | return Lt;\r | |
537 | case GREATER:\r | |
538 | return Gt;\r | |
539 | case EQEQUAL: /* == */\r | |
540 | return Eq;\r | |
541 | case LESSEQUAL:\r | |
542 | return LtE;\r | |
543 | case GREATEREQUAL:\r | |
544 | return GtE;\r | |
545 | case NOTEQUAL:\r | |
546 | return NotEq;\r | |
547 | case NAME:\r | |
548 | if (strcmp(STR(n), "in") == 0)\r | |
549 | return In;\r | |
550 | if (strcmp(STR(n), "is") == 0)\r | |
551 | return Is;\r | |
552 | default:\r | |
553 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",\r | |
554 | STR(n));\r | |
555 | return (cmpop_ty)0;\r | |
556 | }\r | |
557 | }\r | |
558 | else if (NCH(n) == 2) {\r | |
559 | /* handle "not in" and "is not" */\r | |
560 | switch (TYPE(CHILD(n, 0))) {\r | |
561 | case NAME:\r | |
562 | if (strcmp(STR(CHILD(n, 1)), "in") == 0)\r | |
563 | return NotIn;\r | |
564 | if (strcmp(STR(CHILD(n, 0)), "is") == 0)\r | |
565 | return IsNot;\r | |
566 | default:\r | |
567 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",\r | |
568 | STR(CHILD(n, 0)), STR(CHILD(n, 1)));\r | |
569 | return (cmpop_ty)0;\r | |
570 | }\r | |
571 | }\r | |
572 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",\r | |
573 | NCH(n));\r | |
574 | return (cmpop_ty)0;\r | |
575 | }\r | |
576 | \r | |
577 | static asdl_seq *\r | |
578 | seq_for_testlist(struct compiling *c, const node *n)\r | |
579 | {\r | |
580 | /* testlist: test (',' test)* [','] */\r | |
581 | asdl_seq *seq;\r | |
582 | expr_ty expression;\r | |
583 | int i;\r | |
584 | assert(TYPE(n) == testlist ||\r | |
585 | TYPE(n) == listmaker ||\r | |
586 | TYPE(n) == testlist_comp ||\r | |
587 | TYPE(n) == testlist_safe ||\r | |
588 | TYPE(n) == testlist1);\r | |
589 | \r | |
590 | seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);\r | |
591 | if (!seq)\r | |
592 | return NULL;\r | |
593 | \r | |
594 | for (i = 0; i < NCH(n); i += 2) {\r | |
595 | assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);\r | |
596 | \r | |
597 | expression = ast_for_expr(c, CHILD(n, i));\r | |
598 | if (!expression)\r | |
599 | return NULL;\r | |
600 | \r | |
601 | assert(i / 2 < seq->size);\r | |
602 | asdl_seq_SET(seq, i / 2, expression);\r | |
603 | }\r | |
604 | return seq;\r | |
605 | }\r | |
606 | \r | |
607 | static expr_ty\r | |
608 | compiler_complex_args(struct compiling *c, const node *n)\r | |
609 | {\r | |
610 | int i, len = (NCH(n) + 1) / 2;\r | |
611 | expr_ty result;\r | |
612 | asdl_seq *args = asdl_seq_new(len, c->c_arena);\r | |
613 | if (!args)\r | |
614 | return NULL;\r | |
615 | \r | |
616 | /* fpdef: NAME | '(' fplist ')'\r | |
617 | fplist: fpdef (',' fpdef)* [',']\r | |
618 | */\r | |
619 | REQ(n, fplist);\r | |
620 | for (i = 0; i < len; i++) {\r | |
621 | PyObject *arg_id;\r | |
622 | const node *fpdef_node = CHILD(n, 2*i);\r | |
623 | const node *child;\r | |
624 | expr_ty arg;\r | |
625 | set_name:\r | |
626 | /* fpdef_node is either a NAME or an fplist */\r | |
627 | child = CHILD(fpdef_node, 0);\r | |
628 | if (TYPE(child) == NAME) {\r | |
629 | if (!forbidden_check(c, n, STR(child)))\r | |
630 | return NULL;\r | |
631 | arg_id = NEW_IDENTIFIER(child);\r | |
632 | if (!arg_id)\r | |
633 | return NULL;\r | |
634 | arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,\r | |
635 | c->c_arena);\r | |
636 | }\r | |
637 | else {\r | |
638 | assert(TYPE(fpdef_node) == fpdef);\r | |
639 | /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */\r | |
640 | child = CHILD(fpdef_node, 1);\r | |
641 | assert(TYPE(child) == fplist);\r | |
642 | /* NCH == 1 means we have (x), we need to elide the extra parens */\r | |
643 | if (NCH(child) == 1) {\r | |
644 | fpdef_node = CHILD(child, 0);\r | |
645 | assert(TYPE(fpdef_node) == fpdef);\r | |
646 | goto set_name;\r | |
647 | }\r | |
648 | arg = compiler_complex_args(c, child);\r | |
649 | }\r | |
650 | asdl_seq_SET(args, i, arg);\r | |
651 | }\r | |
652 | \r | |
653 | result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);\r | |
654 | if (!set_context(c, result, Store, n))\r | |
655 | return NULL;\r | |
656 | return result;\r | |
657 | }\r | |
658 | \r | |
659 | \r | |
660 | /* Create AST for argument list. */\r | |
661 | \r | |
662 | static arguments_ty\r | |
663 | ast_for_arguments(struct compiling *c, const node *n)\r | |
664 | {\r | |
665 | /* parameters: '(' [varargslist] ')'\r | |
666 | varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]\r | |
667 | | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']\r | |
668 | */\r | |
669 | int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;\r | |
670 | asdl_seq *args, *defaults;\r | |
671 | identifier vararg = NULL, kwarg = NULL;\r | |
672 | node *ch;\r | |
673 | \r | |
674 | if (TYPE(n) == parameters) {\r | |
675 | if (NCH(n) == 2) /* () as argument list */\r | |
676 | return arguments(NULL, NULL, NULL, NULL, c->c_arena);\r | |
677 | n = CHILD(n, 1);\r | |
678 | }\r | |
679 | REQ(n, varargslist);\r | |
680 | \r | |
681 | /* first count the number of normal args & defaults */\r | |
682 | for (i = 0; i < NCH(n); i++) {\r | |
683 | ch = CHILD(n, i);\r | |
684 | if (TYPE(ch) == fpdef)\r | |
685 | n_args++;\r | |
686 | if (TYPE(ch) == EQUAL)\r | |
687 | n_defaults++;\r | |
688 | }\r | |
689 | args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);\r | |
690 | if (!args && n_args)\r | |
691 | return NULL;\r | |
692 | defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);\r | |
693 | if (!defaults && n_defaults)\r | |
694 | return NULL;\r | |
695 | \r | |
696 | /* fpdef: NAME | '(' fplist ')'\r | |
697 | fplist: fpdef (',' fpdef)* [',']\r | |
698 | */\r | |
699 | i = 0;\r | |
700 | j = 0; /* index for defaults */\r | |
701 | k = 0; /* index for args */\r | |
702 | while (i < NCH(n)) {\r | |
703 | ch = CHILD(n, i);\r | |
704 | switch (TYPE(ch)) {\r | |
705 | case fpdef: {\r | |
706 | int complex_args = 0, parenthesized = 0;\r | |
707 | handle_fpdef:\r | |
708 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is\r | |
709 | anything other than EQUAL or a comma? */\r | |
710 | /* XXX Should NCH(n) check be made a separate check? */\r | |
711 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {\r | |
712 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));\r | |
713 | if (!expression)\r | |
714 | return NULL;\r | |
715 | assert(defaults != NULL);\r | |
716 | asdl_seq_SET(defaults, j++, expression);\r | |
717 | i += 2;\r | |
718 | found_default = 1;\r | |
719 | }\r | |
720 | else if (found_default) {\r | |
721 | /* def f((x)=4): pass should raise an error.\r | |
722 | def f((x, (y))): pass will just incur the tuple unpacking warning. */\r | |
723 | if (parenthesized && !complex_args) {\r | |
724 | ast_error(n, "parenthesized arg with default");\r | |
725 | return NULL;\r | |
726 | }\r | |
727 | ast_error(n,\r | |
728 | "non-default argument follows default argument");\r | |
729 | return NULL;\r | |
730 | }\r | |
731 | if (NCH(ch) == 3) {\r | |
732 | ch = CHILD(ch, 1);\r | |
733 | /* def foo((x)): is not complex, special case. */\r | |
734 | if (NCH(ch) != 1) {\r | |
735 | /* We have complex arguments, setup for unpacking. */\r | |
736 | if (Py_Py3kWarningFlag && !ast_warn(c, ch,\r | |
737 | "tuple parameter unpacking has been removed in 3.x"))\r | |
738 | return NULL;\r | |
739 | complex_args = 1;\r | |
740 | asdl_seq_SET(args, k++, compiler_complex_args(c, ch));\r | |
741 | if (!asdl_seq_GET(args, k-1))\r | |
742 | return NULL;\r | |
743 | } else {\r | |
744 | /* def foo((x)): setup for checking NAME below. */\r | |
745 | /* Loop because there can be many parens and tuple\r | |
746 | unpacking mixed in. */\r | |
747 | parenthesized = 1;\r | |
748 | ch = CHILD(ch, 0);\r | |
749 | assert(TYPE(ch) == fpdef);\r | |
750 | goto handle_fpdef;\r | |
751 | }\r | |
752 | }\r | |
753 | if (TYPE(CHILD(ch, 0)) == NAME) {\r | |
754 | PyObject *id;\r | |
755 | expr_ty name;\r | |
756 | if (!forbidden_check(c, n, STR(CHILD(ch, 0))))\r | |
757 | return NULL;\r | |
758 | id = NEW_IDENTIFIER(CHILD(ch, 0));\r | |
759 | if (!id)\r | |
760 | return NULL;\r | |
761 | name = Name(id, Param, LINENO(ch), ch->n_col_offset,\r | |
762 | c->c_arena);\r | |
763 | if (!name)\r | |
764 | return NULL;\r | |
765 | asdl_seq_SET(args, k++, name);\r | |
766 | \r | |
767 | }\r | |
768 | i += 2; /* the name and the comma */\r | |
769 | if (parenthesized && Py_Py3kWarningFlag &&\r | |
770 | !ast_warn(c, ch, "parenthesized argument names "\r | |
771 | "are invalid in 3.x"))\r | |
772 | return NULL;\r | |
773 | \r | |
774 | break;\r | |
775 | }\r | |
776 | case STAR:\r | |
777 | if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))\r | |
778 | return NULL;\r | |
779 | vararg = NEW_IDENTIFIER(CHILD(n, i+1));\r | |
780 | if (!vararg)\r | |
781 | return NULL;\r | |
782 | i += 3;\r | |
783 | break;\r | |
784 | case DOUBLESTAR:\r | |
785 | if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))\r | |
786 | return NULL;\r | |
787 | kwarg = NEW_IDENTIFIER(CHILD(n, i+1));\r | |
788 | if (!kwarg)\r | |
789 | return NULL;\r | |
790 | i += 3;\r | |
791 | break;\r | |
792 | default:\r | |
793 | PyErr_Format(PyExc_SystemError,\r | |
794 | "unexpected node in varargslist: %d @ %d",\r | |
795 | TYPE(ch), i);\r | |
796 | return NULL;\r | |
797 | }\r | |
798 | }\r | |
799 | \r | |
800 | return arguments(args, vararg, kwarg, defaults, c->c_arena);\r | |
801 | }\r | |
802 | \r | |
803 | static expr_ty\r | |
804 | ast_for_dotted_name(struct compiling *c, const node *n)\r | |
805 | {\r | |
806 | expr_ty e;\r | |
807 | identifier id;\r | |
808 | int lineno, col_offset;\r | |
809 | int i;\r | |
810 | \r | |
811 | REQ(n, dotted_name);\r | |
812 | \r | |
813 | lineno = LINENO(n);\r | |
814 | col_offset = n->n_col_offset;\r | |
815 | \r | |
816 | id = NEW_IDENTIFIER(CHILD(n, 0));\r | |
817 | if (!id)\r | |
818 | return NULL;\r | |
819 | e = Name(id, Load, lineno, col_offset, c->c_arena);\r | |
820 | if (!e)\r | |
821 | return NULL;\r | |
822 | \r | |
823 | for (i = 2; i < NCH(n); i+=2) {\r | |
824 | id = NEW_IDENTIFIER(CHILD(n, i));\r | |
825 | if (!id)\r | |
826 | return NULL;\r | |
827 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);\r | |
828 | if (!e)\r | |
829 | return NULL;\r | |
830 | }\r | |
831 | \r | |
832 | return e;\r | |
833 | }\r | |
834 | \r | |
835 | static expr_ty\r | |
836 | ast_for_decorator(struct compiling *c, const node *n)\r | |
837 | {\r | |
838 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */\r | |
839 | expr_ty d = NULL;\r | |
840 | expr_ty name_expr;\r | |
841 | \r | |
842 | REQ(n, decorator);\r | |
843 | REQ(CHILD(n, 0), AT);\r | |
844 | REQ(RCHILD(n, -1), NEWLINE);\r | |
845 | \r | |
846 | name_expr = ast_for_dotted_name(c, CHILD(n, 1));\r | |
847 | if (!name_expr)\r | |
848 | return NULL;\r | |
849 | \r | |
850 | if (NCH(n) == 3) { /* No arguments */\r | |
851 | d = name_expr;\r | |
852 | name_expr = NULL;\r | |
853 | }\r | |
854 | else if (NCH(n) == 5) { /* Call with no arguments */\r | |
855 | d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),\r | |
856 | n->n_col_offset, c->c_arena);\r | |
857 | if (!d)\r | |
858 | return NULL;\r | |
859 | name_expr = NULL;\r | |
860 | }\r | |
861 | else {\r | |
862 | d = ast_for_call(c, CHILD(n, 3), name_expr);\r | |
863 | if (!d)\r | |
864 | return NULL;\r | |
865 | name_expr = NULL;\r | |
866 | }\r | |
867 | \r | |
868 | return d;\r | |
869 | }\r | |
870 | \r | |
871 | static asdl_seq*\r | |
872 | ast_for_decorators(struct compiling *c, const node *n)\r | |
873 | {\r | |
874 | asdl_seq* decorator_seq;\r | |
875 | expr_ty d;\r | |
876 | int i;\r | |
877 | \r | |
878 | REQ(n, decorators);\r | |
879 | decorator_seq = asdl_seq_new(NCH(n), c->c_arena);\r | |
880 | if (!decorator_seq)\r | |
881 | return NULL;\r | |
882 | \r | |
883 | for (i = 0; i < NCH(n); i++) {\r | |
884 | d = ast_for_decorator(c, CHILD(n, i));\r | |
885 | if (!d)\r | |
886 | return NULL;\r | |
887 | asdl_seq_SET(decorator_seq, i, d);\r | |
888 | }\r | |
889 | return decorator_seq;\r | |
890 | }\r | |
891 | \r | |
892 | static stmt_ty\r | |
893 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)\r | |
894 | {\r | |
895 | /* funcdef: 'def' NAME parameters ':' suite */\r | |
896 | identifier name;\r | |
897 | arguments_ty args;\r | |
898 | asdl_seq *body;\r | |
899 | int name_i = 1;\r | |
900 | \r | |
901 | REQ(n, funcdef);\r | |
902 | \r | |
903 | name = NEW_IDENTIFIER(CHILD(n, name_i));\r | |
904 | if (!name)\r | |
905 | return NULL;\r | |
906 | else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))\r | |
907 | return NULL;\r | |
908 | args = ast_for_arguments(c, CHILD(n, name_i + 1));\r | |
909 | if (!args)\r | |
910 | return NULL;\r | |
911 | body = ast_for_suite(c, CHILD(n, name_i + 3));\r | |
912 | if (!body)\r | |
913 | return NULL;\r | |
914 | \r | |
915 | return FunctionDef(name, args, body, decorator_seq, LINENO(n),\r | |
916 | n->n_col_offset, c->c_arena);\r | |
917 | }\r | |
918 | \r | |
919 | static stmt_ty\r | |
920 | ast_for_decorated(struct compiling *c, const node *n)\r | |
921 | {\r | |
922 | /* decorated: decorators (classdef | funcdef) */\r | |
923 | stmt_ty thing = NULL;\r | |
924 | asdl_seq *decorator_seq = NULL;\r | |
925 | \r | |
926 | REQ(n, decorated);\r | |
927 | \r | |
928 | decorator_seq = ast_for_decorators(c, CHILD(n, 0));\r | |
929 | if (!decorator_seq)\r | |
930 | return NULL;\r | |
931 | \r | |
932 | assert(TYPE(CHILD(n, 1)) == funcdef ||\r | |
933 | TYPE(CHILD(n, 1)) == classdef);\r | |
934 | \r | |
935 | if (TYPE(CHILD(n, 1)) == funcdef) {\r | |
936 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);\r | |
937 | } else if (TYPE(CHILD(n, 1)) == classdef) {\r | |
938 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);\r | |
939 | }\r | |
940 | /* we count the decorators in when talking about the class' or\r | |
941 | function's line number */\r | |
942 | if (thing) {\r | |
943 | thing->lineno = LINENO(n);\r | |
944 | thing->col_offset = n->n_col_offset;\r | |
945 | }\r | |
946 | return thing;\r | |
947 | }\r | |
948 | \r | |
949 | static expr_ty\r | |
950 | ast_for_lambdef(struct compiling *c, const node *n)\r | |
951 | {\r | |
952 | /* lambdef: 'lambda' [varargslist] ':' test */\r | |
953 | arguments_ty args;\r | |
954 | expr_ty expression;\r | |
955 | \r | |
956 | if (NCH(n) == 3) {\r | |
957 | args = arguments(NULL, NULL, NULL, NULL, c->c_arena);\r | |
958 | if (!args)\r | |
959 | return NULL;\r | |
960 | expression = ast_for_expr(c, CHILD(n, 2));\r | |
961 | if (!expression)\r | |
962 | return NULL;\r | |
963 | }\r | |
964 | else {\r | |
965 | args = ast_for_arguments(c, CHILD(n, 1));\r | |
966 | if (!args)\r | |
967 | return NULL;\r | |
968 | expression = ast_for_expr(c, CHILD(n, 3));\r | |
969 | if (!expression)\r | |
970 | return NULL;\r | |
971 | }\r | |
972 | \r | |
973 | return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);\r | |
974 | }\r | |
975 | \r | |
976 | static expr_ty\r | |
977 | ast_for_ifexpr(struct compiling *c, const node *n)\r | |
978 | {\r | |
979 | /* test: or_test 'if' or_test 'else' test */\r | |
980 | expr_ty expression, body, orelse;\r | |
981 | \r | |
982 | assert(NCH(n) == 5);\r | |
983 | body = ast_for_expr(c, CHILD(n, 0));\r | |
984 | if (!body)\r | |
985 | return NULL;\r | |
986 | expression = ast_for_expr(c, CHILD(n, 2));\r | |
987 | if (!expression)\r | |
988 | return NULL;\r | |
989 | orelse = ast_for_expr(c, CHILD(n, 4));\r | |
990 | if (!orelse)\r | |
991 | return NULL;\r | |
992 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,\r | |
993 | c->c_arena);\r | |
994 | }\r | |
995 | \r | |
996 | /* XXX(nnorwitz): the listcomp and genexpr code should be refactored\r | |
997 | so there is only a single version. Possibly for loops can also re-use\r | |
998 | the code.\r | |
999 | */\r | |
1000 | \r | |
1001 | /* Count the number of 'for' loop in a list comprehension.\r | |
1002 | \r | |
1003 | Helper for ast_for_listcomp().\r | |
1004 | */\r | |
1005 | \r | |
1006 | static int\r | |
1007 | count_list_fors(struct compiling *c, const node *n)\r | |
1008 | {\r | |
1009 | int n_fors = 0;\r | |
1010 | node *ch = CHILD(n, 1);\r | |
1011 | \r | |
1012 | count_list_for:\r | |
1013 | n_fors++;\r | |
1014 | REQ(ch, list_for);\r | |
1015 | if (NCH(ch) == 5)\r | |
1016 | ch = CHILD(ch, 4);\r | |
1017 | else\r | |
1018 | return n_fors;\r | |
1019 | count_list_iter:\r | |
1020 | REQ(ch, list_iter);\r | |
1021 | ch = CHILD(ch, 0);\r | |
1022 | if (TYPE(ch) == list_for)\r | |
1023 | goto count_list_for;\r | |
1024 | else if (TYPE(ch) == list_if) {\r | |
1025 | if (NCH(ch) == 3) {\r | |
1026 | ch = CHILD(ch, 2);\r | |
1027 | goto count_list_iter;\r | |
1028 | }\r | |
1029 | else\r | |
1030 | return n_fors;\r | |
1031 | }\r | |
1032 | \r | |
1033 | /* Should never be reached */\r | |
1034 | PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");\r | |
1035 | return -1;\r | |
1036 | }\r | |
1037 | \r | |
1038 | /* Count the number of 'if' statements in a list comprehension.\r | |
1039 | \r | |
1040 | Helper for ast_for_listcomp().\r | |
1041 | */\r | |
1042 | \r | |
1043 | static int\r | |
1044 | count_list_ifs(struct compiling *c, const node *n)\r | |
1045 | {\r | |
1046 | int n_ifs = 0;\r | |
1047 | \r | |
1048 | count_list_iter:\r | |
1049 | REQ(n, list_iter);\r | |
1050 | if (TYPE(CHILD(n, 0)) == list_for)\r | |
1051 | return n_ifs;\r | |
1052 | n = CHILD(n, 0);\r | |
1053 | REQ(n, list_if);\r | |
1054 | n_ifs++;\r | |
1055 | if (NCH(n) == 2)\r | |
1056 | return n_ifs;\r | |
1057 | n = CHILD(n, 2);\r | |
1058 | goto count_list_iter;\r | |
1059 | }\r | |
1060 | \r | |
1061 | static expr_ty\r | |
1062 | ast_for_listcomp(struct compiling *c, const node *n)\r | |
1063 | {\r | |
1064 | /* listmaker: test ( list_for | (',' test)* [','] )\r | |
1065 | list_for: 'for' exprlist 'in' testlist_safe [list_iter]\r | |
1066 | list_iter: list_for | list_if\r | |
1067 | list_if: 'if' test [list_iter]\r | |
1068 | testlist_safe: test [(',' test)+ [',']]\r | |
1069 | */\r | |
1070 | expr_ty elt, first;\r | |
1071 | asdl_seq *listcomps;\r | |
1072 | int i, n_fors;\r | |
1073 | node *ch;\r | |
1074 | \r | |
1075 | REQ(n, listmaker);\r | |
1076 | assert(NCH(n) > 1);\r | |
1077 | \r | |
1078 | elt = ast_for_expr(c, CHILD(n, 0));\r | |
1079 | if (!elt)\r | |
1080 | return NULL;\r | |
1081 | \r | |
1082 | n_fors = count_list_fors(c, n);\r | |
1083 | if (n_fors == -1)\r | |
1084 | return NULL;\r | |
1085 | \r | |
1086 | listcomps = asdl_seq_new(n_fors, c->c_arena);\r | |
1087 | if (!listcomps)\r | |
1088 | return NULL;\r | |
1089 | \r | |
1090 | ch = CHILD(n, 1);\r | |
1091 | for (i = 0; i < n_fors; i++) {\r | |
1092 | comprehension_ty lc;\r | |
1093 | asdl_seq *t;\r | |
1094 | expr_ty expression;\r | |
1095 | node *for_ch;\r | |
1096 | \r | |
1097 | REQ(ch, list_for);\r | |
1098 | \r | |
1099 | for_ch = CHILD(ch, 1);\r | |
1100 | t = ast_for_exprlist(c, for_ch, Store);\r | |
1101 | if (!t)\r | |
1102 | return NULL;\r | |
1103 | expression = ast_for_testlist(c, CHILD(ch, 3));\r | |
1104 | if (!expression)\r | |
1105 | return NULL;\r | |
1106 | \r | |
1107 | /* Check the # of children rather than the length of t, since\r | |
1108 | [x for x, in ... ] has 1 element in t, but still requires a Tuple.\r | |
1109 | */\r | |
1110 | first = (expr_ty)asdl_seq_GET(t, 0);\r | |
1111 | if (NCH(for_ch) == 1)\r | |
1112 | lc = comprehension(first, expression, NULL, c->c_arena);\r | |
1113 | else\r | |
1114 | lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset,\r | |
1115 | c->c_arena),\r | |
1116 | expression, NULL, c->c_arena);\r | |
1117 | if (!lc)\r | |
1118 | return NULL;\r | |
1119 | \r | |
1120 | if (NCH(ch) == 5) {\r | |
1121 | int j, n_ifs;\r | |
1122 | asdl_seq *ifs;\r | |
1123 | expr_ty list_for_expr;\r | |
1124 | \r | |
1125 | ch = CHILD(ch, 4);\r | |
1126 | n_ifs = count_list_ifs(c, ch);\r | |
1127 | if (n_ifs == -1)\r | |
1128 | return NULL;\r | |
1129 | \r | |
1130 | ifs = asdl_seq_new(n_ifs, c->c_arena);\r | |
1131 | if (!ifs)\r | |
1132 | return NULL;\r | |
1133 | \r | |
1134 | for (j = 0; j < n_ifs; j++) {\r | |
1135 | REQ(ch, list_iter);\r | |
1136 | ch = CHILD(ch, 0);\r | |
1137 | REQ(ch, list_if);\r | |
1138 | \r | |
1139 | list_for_expr = ast_for_expr(c, CHILD(ch, 1));\r | |
1140 | if (!list_for_expr)\r | |
1141 | return NULL;\r | |
1142 | \r | |
1143 | asdl_seq_SET(ifs, j, list_for_expr);\r | |
1144 | if (NCH(ch) == 3)\r | |
1145 | ch = CHILD(ch, 2);\r | |
1146 | }\r | |
1147 | /* on exit, must guarantee that ch is a list_for */\r | |
1148 | if (TYPE(ch) == list_iter)\r | |
1149 | ch = CHILD(ch, 0);\r | |
1150 | lc->ifs = ifs;\r | |
1151 | }\r | |
1152 | asdl_seq_SET(listcomps, i, lc);\r | |
1153 | }\r | |
1154 | \r | |
1155 | return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1156 | }\r | |
1157 | \r | |
1158 | /*\r | |
1159 | Count the number of 'for' loops in a comprehension.\r | |
1160 | \r | |
1161 | Helper for ast_for_comprehension().\r | |
1162 | */\r | |
1163 | \r | |
1164 | static int\r | |
1165 | count_comp_fors(struct compiling *c, const node *n)\r | |
1166 | {\r | |
1167 | int n_fors = 0;\r | |
1168 | \r | |
1169 | count_comp_for:\r | |
1170 | n_fors++;\r | |
1171 | REQ(n, comp_for);\r | |
1172 | if (NCH(n) == 5)\r | |
1173 | n = CHILD(n, 4);\r | |
1174 | else\r | |
1175 | return n_fors;\r | |
1176 | count_comp_iter:\r | |
1177 | REQ(n, comp_iter);\r | |
1178 | n = CHILD(n, 0);\r | |
1179 | if (TYPE(n) == comp_for)\r | |
1180 | goto count_comp_for;\r | |
1181 | else if (TYPE(n) == comp_if) {\r | |
1182 | if (NCH(n) == 3) {\r | |
1183 | n = CHILD(n, 2);\r | |
1184 | goto count_comp_iter;\r | |
1185 | }\r | |
1186 | else\r | |
1187 | return n_fors;\r | |
1188 | }\r | |
1189 | \r | |
1190 | /* Should never be reached */\r | |
1191 | PyErr_SetString(PyExc_SystemError,\r | |
1192 | "logic error in count_comp_fors");\r | |
1193 | return -1;\r | |
1194 | }\r | |
1195 | \r | |
1196 | /* Count the number of 'if' statements in a comprehension.\r | |
1197 | \r | |
1198 | Helper for ast_for_comprehension().\r | |
1199 | */\r | |
1200 | \r | |
1201 | static int\r | |
1202 | count_comp_ifs(struct compiling *c, const node *n)\r | |
1203 | {\r | |
1204 | int n_ifs = 0;\r | |
1205 | \r | |
1206 | while (1) {\r | |
1207 | REQ(n, comp_iter);\r | |
1208 | if (TYPE(CHILD(n, 0)) == comp_for)\r | |
1209 | return n_ifs;\r | |
1210 | n = CHILD(n, 0);\r | |
1211 | REQ(n, comp_if);\r | |
1212 | n_ifs++;\r | |
1213 | if (NCH(n) == 2)\r | |
1214 | return n_ifs;\r | |
1215 | n = CHILD(n, 2);\r | |
1216 | }\r | |
1217 | }\r | |
1218 | \r | |
1219 | static asdl_seq *\r | |
1220 | ast_for_comprehension(struct compiling *c, const node *n)\r | |
1221 | {\r | |
1222 | int i, n_fors;\r | |
1223 | asdl_seq *comps;\r | |
1224 | \r | |
1225 | n_fors = count_comp_fors(c, n);\r | |
1226 | if (n_fors == -1)\r | |
1227 | return NULL;\r | |
1228 | \r | |
1229 | comps = asdl_seq_new(n_fors, c->c_arena);\r | |
1230 | if (!comps)\r | |
1231 | return NULL;\r | |
1232 | \r | |
1233 | for (i = 0; i < n_fors; i++) {\r | |
1234 | comprehension_ty comp;\r | |
1235 | asdl_seq *t;\r | |
1236 | expr_ty expression, first;\r | |
1237 | node *for_ch;\r | |
1238 | \r | |
1239 | REQ(n, comp_for);\r | |
1240 | \r | |
1241 | for_ch = CHILD(n, 1);\r | |
1242 | t = ast_for_exprlist(c, for_ch, Store);\r | |
1243 | if (!t)\r | |
1244 | return NULL;\r | |
1245 | expression = ast_for_expr(c, CHILD(n, 3));\r | |
1246 | if (!expression)\r | |
1247 | return NULL;\r | |
1248 | \r | |
1249 | /* Check the # of children rather than the length of t, since\r | |
1250 | (x for x, in ...) has 1 element in t, but still requires a Tuple. */\r | |
1251 | first = (expr_ty)asdl_seq_GET(t, 0);\r | |
1252 | if (NCH(for_ch) == 1)\r | |
1253 | comp = comprehension(first, expression, NULL, c->c_arena);\r | |
1254 | else\r | |
1255 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,\r | |
1256 | c->c_arena),\r | |
1257 | expression, NULL, c->c_arena);\r | |
1258 | if (!comp)\r | |
1259 | return NULL;\r | |
1260 | \r | |
1261 | if (NCH(n) == 5) {\r | |
1262 | int j, n_ifs;\r | |
1263 | asdl_seq *ifs;\r | |
1264 | \r | |
1265 | n = CHILD(n, 4);\r | |
1266 | n_ifs = count_comp_ifs(c, n);\r | |
1267 | if (n_ifs == -1)\r | |
1268 | return NULL;\r | |
1269 | \r | |
1270 | ifs = asdl_seq_new(n_ifs, c->c_arena);\r | |
1271 | if (!ifs)\r | |
1272 | return NULL;\r | |
1273 | \r | |
1274 | for (j = 0; j < n_ifs; j++) {\r | |
1275 | REQ(n, comp_iter);\r | |
1276 | n = CHILD(n, 0);\r | |
1277 | REQ(n, comp_if);\r | |
1278 | \r | |
1279 | expression = ast_for_expr(c, CHILD(n, 1));\r | |
1280 | if (!expression)\r | |
1281 | return NULL;\r | |
1282 | asdl_seq_SET(ifs, j, expression);\r | |
1283 | if (NCH(n) == 3)\r | |
1284 | n = CHILD(n, 2);\r | |
1285 | }\r | |
1286 | /* on exit, must guarantee that n is a comp_for */\r | |
1287 | if (TYPE(n) == comp_iter)\r | |
1288 | n = CHILD(n, 0);\r | |
1289 | comp->ifs = ifs;\r | |
1290 | }\r | |
1291 | asdl_seq_SET(comps, i, comp);\r | |
1292 | }\r | |
1293 | return comps;\r | |
1294 | }\r | |
1295 | \r | |
1296 | static expr_ty\r | |
1297 | ast_for_itercomp(struct compiling *c, const node *n, int type)\r | |
1298 | {\r | |
1299 | expr_ty elt;\r | |
1300 | asdl_seq *comps;\r | |
1301 | \r | |
1302 | assert(NCH(n) > 1);\r | |
1303 | \r | |
1304 | elt = ast_for_expr(c, CHILD(n, 0));\r | |
1305 | if (!elt)\r | |
1306 | return NULL;\r | |
1307 | \r | |
1308 | comps = ast_for_comprehension(c, CHILD(n, 1));\r | |
1309 | if (!comps)\r | |
1310 | return NULL;\r | |
1311 | \r | |
1312 | if (type == COMP_GENEXP)\r | |
1313 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1314 | else if (type == COMP_SETCOMP)\r | |
1315 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1316 | else\r | |
1317 | /* Should never happen */\r | |
1318 | return NULL;\r | |
1319 | }\r | |
1320 | \r | |
1321 | static expr_ty\r | |
1322 | ast_for_dictcomp(struct compiling *c, const node *n)\r | |
1323 | {\r | |
1324 | expr_ty key, value;\r | |
1325 | asdl_seq *comps;\r | |
1326 | \r | |
1327 | assert(NCH(n) > 3);\r | |
1328 | REQ(CHILD(n, 1), COLON);\r | |
1329 | \r | |
1330 | key = ast_for_expr(c, CHILD(n, 0));\r | |
1331 | if (!key)\r | |
1332 | return NULL;\r | |
1333 | \r | |
1334 | value = ast_for_expr(c, CHILD(n, 2));\r | |
1335 | if (!value)\r | |
1336 | return NULL;\r | |
1337 | \r | |
1338 | comps = ast_for_comprehension(c, CHILD(n, 3));\r | |
1339 | if (!comps)\r | |
1340 | return NULL;\r | |
1341 | \r | |
1342 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1343 | }\r | |
1344 | \r | |
1345 | static expr_ty\r | |
1346 | ast_for_genexp(struct compiling *c, const node *n)\r | |
1347 | {\r | |
1348 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));\r | |
1349 | return ast_for_itercomp(c, n, COMP_GENEXP);\r | |
1350 | }\r | |
1351 | \r | |
1352 | static expr_ty\r | |
1353 | ast_for_setcomp(struct compiling *c, const node *n)\r | |
1354 | {\r | |
1355 | assert(TYPE(n) == (dictorsetmaker));\r | |
1356 | return ast_for_itercomp(c, n, COMP_SETCOMP);\r | |
1357 | }\r | |
1358 | \r | |
1359 | static expr_ty\r | |
1360 | ast_for_atom(struct compiling *c, const node *n)\r | |
1361 | {\r | |
1362 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']'\r | |
1363 | | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+\r | |
1364 | */\r | |
1365 | node *ch = CHILD(n, 0);\r | |
1366 | \r | |
1367 | switch (TYPE(ch)) {\r | |
1368 | case NAME: {\r | |
1369 | /* All names start in Load context, but may later be\r | |
1370 | changed. */\r | |
1371 | PyObject *name = NEW_IDENTIFIER(ch);\r | |
1372 | if (!name)\r | |
1373 | return NULL;\r | |
1374 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1375 | }\r | |
1376 | case STRING: {\r | |
1377 | PyObject *str = parsestrplus(c, n);\r | |
1378 | if (!str) {\r | |
1379 | #ifdef Py_USING_UNICODE\r | |
1380 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)){\r | |
1381 | PyObject *type, *value, *tback, *errstr;\r | |
1382 | PyErr_Fetch(&type, &value, &tback);\r | |
1383 | errstr = PyObject_Str(value);\r | |
1384 | if (errstr) {\r | |
1385 | char *s = "";\r | |
1386 | char buf[128];\r | |
1387 | s = PyString_AsString(errstr);\r | |
1388 | PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);\r | |
1389 | ast_error(n, buf);\r | |
1390 | Py_DECREF(errstr);\r | |
1391 | } else {\r | |
1392 | ast_error(n, "(unicode error) unknown error");\r | |
1393 | }\r | |
1394 | Py_DECREF(type);\r | |
1395 | Py_DECREF(value);\r | |
1396 | Py_XDECREF(tback);\r | |
1397 | }\r | |
1398 | #endif\r | |
1399 | return NULL;\r | |
1400 | }\r | |
1401 | PyArena_AddPyObject(c->c_arena, str);\r | |
1402 | return Str(str, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1403 | }\r | |
1404 | case NUMBER: {\r | |
1405 | PyObject *pynum = parsenumber(c, STR(ch));\r | |
1406 | if (!pynum)\r | |
1407 | return NULL;\r | |
1408 | \r | |
1409 | PyArena_AddPyObject(c->c_arena, pynum);\r | |
1410 | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1411 | }\r | |
1412 | case LPAR: /* some parenthesized expressions */\r | |
1413 | ch = CHILD(n, 1);\r | |
1414 | \r | |
1415 | if (TYPE(ch) == RPAR)\r | |
1416 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1417 | \r | |
1418 | if (TYPE(ch) == yield_expr)\r | |
1419 | return ast_for_expr(c, ch);\r | |
1420 | \r | |
1421 | return ast_for_testlist_comp(c, ch);\r | |
1422 | case LSQB: /* list (or list comprehension) */\r | |
1423 | ch = CHILD(n, 1);\r | |
1424 | \r | |
1425 | if (TYPE(ch) == RSQB)\r | |
1426 | return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1427 | \r | |
1428 | REQ(ch, listmaker);\r | |
1429 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {\r | |
1430 | asdl_seq *elts = seq_for_testlist(c, ch);\r | |
1431 | if (!elts)\r | |
1432 | return NULL;\r | |
1433 | \r | |
1434 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1435 | }\r | |
1436 | else\r | |
1437 | return ast_for_listcomp(c, ch);\r | |
1438 | case LBRACE: {\r | |
1439 | /* dictorsetmaker:\r | |
1440 | * (test ':' test (comp_for | (',' test ':' test)* [','])) |\r | |
1441 | * (test (comp_for | (',' test)* [',']))\r | |
1442 | */\r | |
1443 | int i, size;\r | |
1444 | asdl_seq *keys, *values;\r | |
1445 | \r | |
1446 | ch = CHILD(n, 1);\r | |
1447 | if (TYPE(ch) == RBRACE) {\r | |
1448 | /* it's an empty dict */\r | |
1449 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1450 | } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {\r | |
1451 | /* it's a simple set */\r | |
1452 | asdl_seq *elts;\r | |
1453 | size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */\r | |
1454 | elts = asdl_seq_new(size, c->c_arena);\r | |
1455 | if (!elts)\r | |
1456 | return NULL;\r | |
1457 | for (i = 0; i < NCH(ch); i += 2) {\r | |
1458 | expr_ty expression;\r | |
1459 | expression = ast_for_expr(c, CHILD(ch, i));\r | |
1460 | if (!expression)\r | |
1461 | return NULL;\r | |
1462 | asdl_seq_SET(elts, i / 2, expression);\r | |
1463 | }\r | |
1464 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1465 | } else if (TYPE(CHILD(ch, 1)) == comp_for) {\r | |
1466 | /* it's a set comprehension */\r | |
1467 | return ast_for_setcomp(c, ch);\r | |
1468 | } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {\r | |
1469 | return ast_for_dictcomp(c, ch);\r | |
1470 | } else {\r | |
1471 | /* it's a dict */\r | |
1472 | size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */\r | |
1473 | keys = asdl_seq_new(size, c->c_arena);\r | |
1474 | if (!keys)\r | |
1475 | return NULL;\r | |
1476 | \r | |
1477 | values = asdl_seq_new(size, c->c_arena);\r | |
1478 | if (!values)\r | |
1479 | return NULL;\r | |
1480 | \r | |
1481 | for (i = 0; i < NCH(ch); i += 4) {\r | |
1482 | expr_ty expression;\r | |
1483 | \r | |
1484 | expression = ast_for_expr(c, CHILD(ch, i));\r | |
1485 | if (!expression)\r | |
1486 | return NULL;\r | |
1487 | \r | |
1488 | asdl_seq_SET(keys, i / 4, expression);\r | |
1489 | \r | |
1490 | expression = ast_for_expr(c, CHILD(ch, i + 2));\r | |
1491 | if (!expression)\r | |
1492 | return NULL;\r | |
1493 | \r | |
1494 | asdl_seq_SET(values, i / 4, expression);\r | |
1495 | }\r | |
1496 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1497 | }\r | |
1498 | }\r | |
1499 | case BACKQUOTE: { /* repr */\r | |
1500 | expr_ty expression;\r | |
1501 | if (Py_Py3kWarningFlag &&\r | |
1502 | !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))\r | |
1503 | return NULL;\r | |
1504 | expression = ast_for_testlist(c, CHILD(n, 1));\r | |
1505 | if (!expression)\r | |
1506 | return NULL;\r | |
1507 | \r | |
1508 | return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1509 | }\r | |
1510 | default:\r | |
1511 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));\r | |
1512 | return NULL;\r | |
1513 | }\r | |
1514 | }\r | |
1515 | \r | |
1516 | static slice_ty\r | |
1517 | ast_for_slice(struct compiling *c, const node *n)\r | |
1518 | {\r | |
1519 | node *ch;\r | |
1520 | expr_ty lower = NULL, upper = NULL, step = NULL;\r | |
1521 | \r | |
1522 | REQ(n, subscript);\r | |
1523 | \r | |
1524 | /*\r | |
1525 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]\r | |
1526 | sliceop: ':' [test]\r | |
1527 | */\r | |
1528 | ch = CHILD(n, 0);\r | |
1529 | if (TYPE(ch) == DOT)\r | |
1530 | return Ellipsis(c->c_arena);\r | |
1531 | \r | |
1532 | if (NCH(n) == 1 && TYPE(ch) == test) {\r | |
1533 | /* 'step' variable hold no significance in terms of being used over\r | |
1534 | other vars */\r | |
1535 | step = ast_for_expr(c, ch);\r | |
1536 | if (!step)\r | |
1537 | return NULL;\r | |
1538 | \r | |
1539 | return Index(step, c->c_arena);\r | |
1540 | }\r | |
1541 | \r | |
1542 | if (TYPE(ch) == test) {\r | |
1543 | lower = ast_for_expr(c, ch);\r | |
1544 | if (!lower)\r | |
1545 | return NULL;\r | |
1546 | }\r | |
1547 | \r | |
1548 | /* If there's an upper bound it's in the second or third position. */\r | |
1549 | if (TYPE(ch) == COLON) {\r | |
1550 | if (NCH(n) > 1) {\r | |
1551 | node *n2 = CHILD(n, 1);\r | |
1552 | \r | |
1553 | if (TYPE(n2) == test) {\r | |
1554 | upper = ast_for_expr(c, n2);\r | |
1555 | if (!upper)\r | |
1556 | return NULL;\r | |
1557 | }\r | |
1558 | }\r | |
1559 | } else if (NCH(n) > 2) {\r | |
1560 | node *n2 = CHILD(n, 2);\r | |
1561 | \r | |
1562 | if (TYPE(n2) == test) {\r | |
1563 | upper = ast_for_expr(c, n2);\r | |
1564 | if (!upper)\r | |
1565 | return NULL;\r | |
1566 | }\r | |
1567 | }\r | |
1568 | \r | |
1569 | ch = CHILD(n, NCH(n) - 1);\r | |
1570 | if (TYPE(ch) == sliceop) {\r | |
1571 | if (NCH(ch) == 1) {\r | |
1572 | /*\r | |
1573 | This is an extended slice (ie "x[::]") with no expression in the\r | |
1574 | step field. We set this literally to "None" in order to\r | |
1575 | disambiguate it from x[:]. (The interpreter might have to call\r | |
1576 | __getslice__ for x[:], but it must call __getitem__ for x[::].)\r | |
1577 | */\r | |
1578 | identifier none = new_identifier("None", c->c_arena);\r | |
1579 | if (!none)\r | |
1580 | return NULL;\r | |
1581 | ch = CHILD(ch, 0);\r | |
1582 | step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);\r | |
1583 | if (!step)\r | |
1584 | return NULL;\r | |
1585 | } else {\r | |
1586 | ch = CHILD(ch, 1);\r | |
1587 | if (TYPE(ch) == test) {\r | |
1588 | step = ast_for_expr(c, ch);\r | |
1589 | if (!step)\r | |
1590 | return NULL;\r | |
1591 | }\r | |
1592 | }\r | |
1593 | }\r | |
1594 | \r | |
1595 | return Slice(lower, upper, step, c->c_arena);\r | |
1596 | }\r | |
1597 | \r | |
1598 | static expr_ty\r | |
1599 | ast_for_binop(struct compiling *c, const node *n)\r | |
1600 | {\r | |
1601 | /* Must account for a sequence of expressions.\r | |
1602 | How should A op B op C by represented?\r | |
1603 | BinOp(BinOp(A, op, B), op, C).\r | |
1604 | */\r | |
1605 | \r | |
1606 | int i, nops;\r | |
1607 | expr_ty expr1, expr2, result;\r | |
1608 | operator_ty newoperator;\r | |
1609 | \r | |
1610 | expr1 = ast_for_expr(c, CHILD(n, 0));\r | |
1611 | if (!expr1)\r | |
1612 | return NULL;\r | |
1613 | \r | |
1614 | expr2 = ast_for_expr(c, CHILD(n, 2));\r | |
1615 | if (!expr2)\r | |
1616 | return NULL;\r | |
1617 | \r | |
1618 | newoperator = get_operator(CHILD(n, 1));\r | |
1619 | if (!newoperator)\r | |
1620 | return NULL;\r | |
1621 | \r | |
1622 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,\r | |
1623 | c->c_arena);\r | |
1624 | if (!result)\r | |
1625 | return NULL;\r | |
1626 | \r | |
1627 | nops = (NCH(n) - 1) / 2;\r | |
1628 | for (i = 1; i < nops; i++) {\r | |
1629 | expr_ty tmp_result, tmp;\r | |
1630 | const node* next_oper = CHILD(n, i * 2 + 1);\r | |
1631 | \r | |
1632 | newoperator = get_operator(next_oper);\r | |
1633 | if (!newoperator)\r | |
1634 | return NULL;\r | |
1635 | \r | |
1636 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));\r | |
1637 | if (!tmp)\r | |
1638 | return NULL;\r | |
1639 | \r | |
1640 | tmp_result = BinOp(result, newoperator, tmp,\r | |
1641 | LINENO(next_oper), next_oper->n_col_offset,\r | |
1642 | c->c_arena);\r | |
1643 | if (!tmp_result)\r | |
1644 | return NULL;\r | |
1645 | result = tmp_result;\r | |
1646 | }\r | |
1647 | return result;\r | |
1648 | }\r | |
1649 | \r | |
1650 | static expr_ty\r | |
1651 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)\r | |
1652 | {\r | |
1653 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME\r | |
1654 | subscriptlist: subscript (',' subscript)* [',']\r | |
1655 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]\r | |
1656 | */\r | |
1657 | REQ(n, trailer);\r | |
1658 | if (TYPE(CHILD(n, 0)) == LPAR) {\r | |
1659 | if (NCH(n) == 2)\r | |
1660 | return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),\r | |
1661 | n->n_col_offset, c->c_arena);\r | |
1662 | else\r | |
1663 | return ast_for_call(c, CHILD(n, 1), left_expr);\r | |
1664 | }\r | |
1665 | else if (TYPE(CHILD(n, 0)) == DOT ) {\r | |
1666 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));\r | |
1667 | if (!attr_id)\r | |
1668 | return NULL;\r | |
1669 | return Attribute(left_expr, attr_id, Load,\r | |
1670 | LINENO(n), n->n_col_offset, c->c_arena);\r | |
1671 | }\r | |
1672 | else {\r | |
1673 | REQ(CHILD(n, 0), LSQB);\r | |
1674 | REQ(CHILD(n, 2), RSQB);\r | |
1675 | n = CHILD(n, 1);\r | |
1676 | if (NCH(n) == 1) {\r | |
1677 | slice_ty slc = ast_for_slice(c, CHILD(n, 0));\r | |
1678 | if (!slc)\r | |
1679 | return NULL;\r | |
1680 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,\r | |
1681 | c->c_arena);\r | |
1682 | }\r | |
1683 | else {\r | |
1684 | /* The grammar is ambiguous here. The ambiguity is resolved\r | |
1685 | by treating the sequence as a tuple literal if there are\r | |
1686 | no slice features.\r | |
1687 | */\r | |
1688 | int j;\r | |
1689 | slice_ty slc;\r | |
1690 | expr_ty e;\r | |
1691 | bool simple = true;\r | |
1692 | asdl_seq *slices, *elts;\r | |
1693 | slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);\r | |
1694 | if (!slices)\r | |
1695 | return NULL;\r | |
1696 | for (j = 0; j < NCH(n); j += 2) {\r | |
1697 | slc = ast_for_slice(c, CHILD(n, j));\r | |
1698 | if (!slc)\r | |
1699 | return NULL;\r | |
1700 | if (slc->kind != Index_kind)\r | |
1701 | simple = false;\r | |
1702 | asdl_seq_SET(slices, j / 2, slc);\r | |
1703 | }\r | |
1704 | if (!simple) {\r | |
1705 | return Subscript(left_expr, ExtSlice(slices, c->c_arena),\r | |
1706 | Load, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1707 | }\r | |
1708 | /* extract Index values and put them in a Tuple */\r | |
1709 | elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);\r | |
1710 | if (!elts)\r | |
1711 | return NULL;\r | |
1712 | for (j = 0; j < asdl_seq_LEN(slices); ++j) {\r | |
1713 | slc = (slice_ty)asdl_seq_GET(slices, j);\r | |
1714 | assert(slc->kind == Index_kind && slc->v.Index.value);\r | |
1715 | asdl_seq_SET(elts, j, slc->v.Index.value);\r | |
1716 | }\r | |
1717 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1718 | if (!e)\r | |
1719 | return NULL;\r | |
1720 | return Subscript(left_expr, Index(e, c->c_arena),\r | |
1721 | Load, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1722 | }\r | |
1723 | }\r | |
1724 | }\r | |
1725 | \r | |
1726 | static expr_ty\r | |
1727 | ast_for_factor(struct compiling *c, const node *n)\r | |
1728 | {\r | |
1729 | node *pfactor, *ppower, *patom, *pnum;\r | |
1730 | expr_ty expression;\r | |
1731 | \r | |
1732 | /* If the unary - operator is applied to a constant, don't generate\r | |
1733 | a UNARY_NEGATIVE opcode. Just store the approriate value as a\r | |
1734 | constant. The peephole optimizer already does something like\r | |
1735 | this but it doesn't handle the case where the constant is\r | |
1736 | (sys.maxint - 1). In that case, we want a PyIntObject, not a\r | |
1737 | PyLongObject.\r | |
1738 | */\r | |
1739 | if (TYPE(CHILD(n, 0)) == MINUS &&\r | |
1740 | NCH(n) == 2 &&\r | |
1741 | TYPE((pfactor = CHILD(n, 1))) == factor &&\r | |
1742 | NCH(pfactor) == 1 &&\r | |
1743 | TYPE((ppower = CHILD(pfactor, 0))) == power &&\r | |
1744 | NCH(ppower) == 1 &&\r | |
1745 | TYPE((patom = CHILD(ppower, 0))) == atom &&\r | |
1746 | TYPE((pnum = CHILD(patom, 0))) == NUMBER) {\r | |
1747 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);\r | |
1748 | if (s == NULL)\r | |
1749 | return NULL;\r | |
1750 | s[0] = '-';\r | |
1751 | strcpy(s + 1, STR(pnum));\r | |
1752 | PyObject_FREE(STR(pnum));\r | |
1753 | STR(pnum) = s;\r | |
1754 | return ast_for_atom(c, patom);\r | |
1755 | }\r | |
1756 | \r | |
1757 | expression = ast_for_expr(c, CHILD(n, 1));\r | |
1758 | if (!expression)\r | |
1759 | return NULL;\r | |
1760 | \r | |
1761 | switch (TYPE(CHILD(n, 0))) {\r | |
1762 | case PLUS:\r | |
1763 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,\r | |
1764 | c->c_arena);\r | |
1765 | case MINUS:\r | |
1766 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,\r | |
1767 | c->c_arena);\r | |
1768 | case TILDE:\r | |
1769 | return UnaryOp(Invert, expression, LINENO(n),\r | |
1770 | n->n_col_offset, c->c_arena);\r | |
1771 | }\r | |
1772 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d",\r | |
1773 | TYPE(CHILD(n, 0)));\r | |
1774 | return NULL;\r | |
1775 | }\r | |
1776 | \r | |
1777 | static expr_ty\r | |
1778 | ast_for_power(struct compiling *c, const node *n)\r | |
1779 | {\r | |
1780 | /* power: atom trailer* ('**' factor)*\r | |
1781 | */\r | |
1782 | int i;\r | |
1783 | expr_ty e, tmp;\r | |
1784 | REQ(n, power);\r | |
1785 | e = ast_for_atom(c, CHILD(n, 0));\r | |
1786 | if (!e)\r | |
1787 | return NULL;\r | |
1788 | if (NCH(n) == 1)\r | |
1789 | return e;\r | |
1790 | for (i = 1; i < NCH(n); i++) {\r | |
1791 | node *ch = CHILD(n, i);\r | |
1792 | if (TYPE(ch) != trailer)\r | |
1793 | break;\r | |
1794 | tmp = ast_for_trailer(c, ch, e);\r | |
1795 | if (!tmp)\r | |
1796 | return NULL;\r | |
1797 | tmp->lineno = e->lineno;\r | |
1798 | tmp->col_offset = e->col_offset;\r | |
1799 | e = tmp;\r | |
1800 | }\r | |
1801 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {\r | |
1802 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));\r | |
1803 | if (!f)\r | |
1804 | return NULL;\r | |
1805 | tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1806 | if (!tmp)\r | |
1807 | return NULL;\r | |
1808 | e = tmp;\r | |
1809 | }\r | |
1810 | return e;\r | |
1811 | }\r | |
1812 | \r | |
1813 | /* Do not name a variable 'expr'! Will cause a compile error.\r | |
1814 | */\r | |
1815 | \r | |
1816 | static expr_ty\r | |
1817 | ast_for_expr(struct compiling *c, const node *n)\r | |
1818 | {\r | |
1819 | /* handle the full range of simple expressions\r | |
1820 | test: or_test ['if' or_test 'else' test] | lambdef\r | |
1821 | or_test: and_test ('or' and_test)*\r | |
1822 | and_test: not_test ('and' not_test)*\r | |
1823 | not_test: 'not' not_test | comparison\r | |
1824 | comparison: expr (comp_op expr)*\r | |
1825 | expr: xor_expr ('|' xor_expr)*\r | |
1826 | xor_expr: and_expr ('^' and_expr)*\r | |
1827 | and_expr: shift_expr ('&' shift_expr)*\r | |
1828 | shift_expr: arith_expr (('<<'|'>>') arith_expr)*\r | |
1829 | arith_expr: term (('+'|'-') term)*\r | |
1830 | term: factor (('*'|'/'|'%'|'//') factor)*\r | |
1831 | factor: ('+'|'-'|'~') factor | power\r | |
1832 | power: atom trailer* ('**' factor)*\r | |
1833 | \r | |
1834 | As well as modified versions that exist for backward compatibility,\r | |
1835 | to explicitly allow:\r | |
1836 | [ x for x in lambda: 0, lambda: 1 ]\r | |
1837 | (which would be ambiguous without these extra rules)\r | |
1838 | \r | |
1839 | old_test: or_test | old_lambdef\r | |
1840 | old_lambdef: 'lambda' [vararglist] ':' old_test\r | |
1841 | \r | |
1842 | */\r | |
1843 | \r | |
1844 | asdl_seq *seq;\r | |
1845 | int i;\r | |
1846 | \r | |
1847 | loop:\r | |
1848 | switch (TYPE(n)) {\r | |
1849 | case test:\r | |
1850 | case old_test:\r | |
1851 | if (TYPE(CHILD(n, 0)) == lambdef ||\r | |
1852 | TYPE(CHILD(n, 0)) == old_lambdef)\r | |
1853 | return ast_for_lambdef(c, CHILD(n, 0));\r | |
1854 | else if (NCH(n) > 1)\r | |
1855 | return ast_for_ifexpr(c, n);\r | |
1856 | /* Fallthrough */\r | |
1857 | case or_test:\r | |
1858 | case and_test:\r | |
1859 | if (NCH(n) == 1) {\r | |
1860 | n = CHILD(n, 0);\r | |
1861 | goto loop;\r | |
1862 | }\r | |
1863 | seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);\r | |
1864 | if (!seq)\r | |
1865 | return NULL;\r | |
1866 | for (i = 0; i < NCH(n); i += 2) {\r | |
1867 | expr_ty e = ast_for_expr(c, CHILD(n, i));\r | |
1868 | if (!e)\r | |
1869 | return NULL;\r | |
1870 | asdl_seq_SET(seq, i / 2, e);\r | |
1871 | }\r | |
1872 | if (!strcmp(STR(CHILD(n, 1)), "and"))\r | |
1873 | return BoolOp(And, seq, LINENO(n), n->n_col_offset,\r | |
1874 | c->c_arena);\r | |
1875 | assert(!strcmp(STR(CHILD(n, 1)), "or"));\r | |
1876 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1877 | case not_test:\r | |
1878 | if (NCH(n) == 1) {\r | |
1879 | n = CHILD(n, 0);\r | |
1880 | goto loop;\r | |
1881 | }\r | |
1882 | else {\r | |
1883 | expr_ty expression = ast_for_expr(c, CHILD(n, 1));\r | |
1884 | if (!expression)\r | |
1885 | return NULL;\r | |
1886 | \r | |
1887 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,\r | |
1888 | c->c_arena);\r | |
1889 | }\r | |
1890 | case comparison:\r | |
1891 | if (NCH(n) == 1) {\r | |
1892 | n = CHILD(n, 0);\r | |
1893 | goto loop;\r | |
1894 | }\r | |
1895 | else {\r | |
1896 | expr_ty expression;\r | |
1897 | asdl_int_seq *ops;\r | |
1898 | asdl_seq *cmps;\r | |
1899 | ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);\r | |
1900 | if (!ops)\r | |
1901 | return NULL;\r | |
1902 | cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);\r | |
1903 | if (!cmps) {\r | |
1904 | return NULL;\r | |
1905 | }\r | |
1906 | for (i = 1; i < NCH(n); i += 2) {\r | |
1907 | cmpop_ty newoperator;\r | |
1908 | \r | |
1909 | newoperator = ast_for_comp_op(c, CHILD(n, i));\r | |
1910 | if (!newoperator) {\r | |
1911 | return NULL;\r | |
1912 | }\r | |
1913 | \r | |
1914 | expression = ast_for_expr(c, CHILD(n, i + 1));\r | |
1915 | if (!expression) {\r | |
1916 | return NULL;\r | |
1917 | }\r | |
1918 | \r | |
1919 | asdl_seq_SET(ops, i / 2, newoperator);\r | |
1920 | asdl_seq_SET(cmps, i / 2, expression);\r | |
1921 | }\r | |
1922 | expression = ast_for_expr(c, CHILD(n, 0));\r | |
1923 | if (!expression) {\r | |
1924 | return NULL;\r | |
1925 | }\r | |
1926 | \r | |
1927 | return Compare(expression, ops, cmps, LINENO(n),\r | |
1928 | n->n_col_offset, c->c_arena);\r | |
1929 | }\r | |
1930 | break;\r | |
1931 | \r | |
1932 | /* The next five cases all handle BinOps. The main body of code\r | |
1933 | is the same in each case, but the switch turned inside out to\r | |
1934 | reuse the code for each type of operator.\r | |
1935 | */\r | |
1936 | case expr:\r | |
1937 | case xor_expr:\r | |
1938 | case and_expr:\r | |
1939 | case shift_expr:\r | |
1940 | case arith_expr:\r | |
1941 | case term:\r | |
1942 | if (NCH(n) == 1) {\r | |
1943 | n = CHILD(n, 0);\r | |
1944 | goto loop;\r | |
1945 | }\r | |
1946 | return ast_for_binop(c, n);\r | |
1947 | case yield_expr: {\r | |
1948 | expr_ty exp = NULL;\r | |
1949 | if (NCH(n) == 2) {\r | |
1950 | exp = ast_for_testlist(c, CHILD(n, 1));\r | |
1951 | if (!exp)\r | |
1952 | return NULL;\r | |
1953 | }\r | |
1954 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);\r | |
1955 | }\r | |
1956 | case factor:\r | |
1957 | if (NCH(n) == 1) {\r | |
1958 | n = CHILD(n, 0);\r | |
1959 | goto loop;\r | |
1960 | }\r | |
1961 | return ast_for_factor(c, n);\r | |
1962 | case power:\r | |
1963 | return ast_for_power(c, n);\r | |
1964 | default:\r | |
1965 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));\r | |
1966 | return NULL;\r | |
1967 | }\r | |
1968 | /* should never get here unless if error is set */\r | |
1969 | return NULL;\r | |
1970 | }\r | |
1971 | \r | |
1972 | static expr_ty\r | |
1973 | ast_for_call(struct compiling *c, const node *n, expr_ty func)\r | |
1974 | {\r | |
1975 | /*\r | |
1976 | arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]\r | |
1977 | | '**' test)\r | |
1978 | argument: [test '='] test [comp_for] # Really [keyword '='] test\r | |
1979 | */\r | |
1980 | \r | |
1981 | int i, nargs, nkeywords, ngens;\r | |
1982 | asdl_seq *args;\r | |
1983 | asdl_seq *keywords;\r | |
1984 | expr_ty vararg = NULL, kwarg = NULL;\r | |
1985 | \r | |
1986 | REQ(n, arglist);\r | |
1987 | \r | |
1988 | nargs = 0;\r | |
1989 | nkeywords = 0;\r | |
1990 | ngens = 0;\r | |
1991 | for (i = 0; i < NCH(n); i++) {\r | |
1992 | node *ch = CHILD(n, i);\r | |
1993 | if (TYPE(ch) == argument) {\r | |
1994 | if (NCH(ch) == 1)\r | |
1995 | nargs++;\r | |
1996 | else if (TYPE(CHILD(ch, 1)) == comp_for)\r | |
1997 | ngens++;\r | |
1998 | else\r | |
1999 | nkeywords++;\r | |
2000 | }\r | |
2001 | }\r | |
2002 | if (ngens > 1 || (ngens && (nargs || nkeywords))) {\r | |
2003 | ast_error(n, "Generator expression must be parenthesized "\r | |
2004 | "if not sole argument");\r | |
2005 | return NULL;\r | |
2006 | }\r | |
2007 | \r | |
2008 | if (nargs + nkeywords + ngens > 255) {\r | |
2009 | ast_error(n, "more than 255 arguments");\r | |
2010 | return NULL;\r | |
2011 | }\r | |
2012 | \r | |
2013 | args = asdl_seq_new(nargs + ngens, c->c_arena);\r | |
2014 | if (!args)\r | |
2015 | return NULL;\r | |
2016 | keywords = asdl_seq_new(nkeywords, c->c_arena);\r | |
2017 | if (!keywords)\r | |
2018 | return NULL;\r | |
2019 | nargs = 0;\r | |
2020 | nkeywords = 0;\r | |
2021 | for (i = 0; i < NCH(n); i++) {\r | |
2022 | node *ch = CHILD(n, i);\r | |
2023 | if (TYPE(ch) == argument) {\r | |
2024 | expr_ty e;\r | |
2025 | if (NCH(ch) == 1) {\r | |
2026 | if (nkeywords) {\r | |
2027 | ast_error(CHILD(ch, 0),\r | |
2028 | "non-keyword arg after keyword arg");\r | |
2029 | return NULL;\r | |
2030 | }\r | |
2031 | if (vararg) {\r | |
2032 | ast_error(CHILD(ch, 0),\r | |
2033 | "only named arguments may follow *expression");\r | |
2034 | return NULL;\r | |
2035 | }\r | |
2036 | e = ast_for_expr(c, CHILD(ch, 0));\r | |
2037 | if (!e)\r | |
2038 | return NULL;\r | |
2039 | asdl_seq_SET(args, nargs++, e);\r | |
2040 | }\r | |
2041 | else if (TYPE(CHILD(ch, 1)) == comp_for) {\r | |
2042 | e = ast_for_genexp(c, ch);\r | |
2043 | if (!e)\r | |
2044 | return NULL;\r | |
2045 | asdl_seq_SET(args, nargs++, e);\r | |
2046 | }\r | |
2047 | else {\r | |
2048 | keyword_ty kw;\r | |
2049 | identifier key;\r | |
2050 | int k;\r | |
2051 | char *tmp;\r | |
2052 | \r | |
2053 | /* CHILD(ch, 0) is test, but must be an identifier? */\r | |
2054 | e = ast_for_expr(c, CHILD(ch, 0));\r | |
2055 | if (!e)\r | |
2056 | return NULL;\r | |
2057 | /* f(lambda x: x[0] = 3) ends up getting parsed with\r | |
2058 | * LHS test = lambda x: x[0], and RHS test = 3.\r | |
2059 | * SF bug 132313 points out that complaining about a keyword\r | |
2060 | * then is very confusing.\r | |
2061 | */\r | |
2062 | if (e->kind == Lambda_kind) {\r | |
2063 | ast_error(CHILD(ch, 0),\r | |
2064 | "lambda cannot contain assignment");\r | |
2065 | return NULL;\r | |
2066 | } else if (e->kind != Name_kind) {\r | |
2067 | ast_error(CHILD(ch, 0), "keyword can't be an expression");\r | |
2068 | return NULL;\r | |
2069 | }\r | |
2070 | key = e->v.Name.id;\r | |
2071 | if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))\r | |
2072 | return NULL;\r | |
2073 | for (k = 0; k < nkeywords; k++) {\r | |
2074 | tmp = PyString_AS_STRING(\r | |
2075 | ((keyword_ty)asdl_seq_GET(keywords, k))->arg);\r | |
2076 | if (!strcmp(tmp, PyString_AS_STRING(key))) {\r | |
2077 | ast_error(CHILD(ch, 0), "keyword argument repeated");\r | |
2078 | return NULL;\r | |
2079 | }\r | |
2080 | }\r | |
2081 | e = ast_for_expr(c, CHILD(ch, 2));\r | |
2082 | if (!e)\r | |
2083 | return NULL;\r | |
2084 | kw = keyword(key, e, c->c_arena);\r | |
2085 | if (!kw)\r | |
2086 | return NULL;\r | |
2087 | asdl_seq_SET(keywords, nkeywords++, kw);\r | |
2088 | }\r | |
2089 | }\r | |
2090 | else if (TYPE(ch) == STAR) {\r | |
2091 | vararg = ast_for_expr(c, CHILD(n, i+1));\r | |
2092 | if (!vararg)\r | |
2093 | return NULL;\r | |
2094 | i++;\r | |
2095 | }\r | |
2096 | else if (TYPE(ch) == DOUBLESTAR) {\r | |
2097 | kwarg = ast_for_expr(c, CHILD(n, i+1));\r | |
2098 | if (!kwarg)\r | |
2099 | return NULL;\r | |
2100 | i++;\r | |
2101 | }\r | |
2102 | }\r | |
2103 | \r | |
2104 | return Call(func, args, keywords, vararg, kwarg, func->lineno,\r | |
2105 | func->col_offset, c->c_arena);\r | |
2106 | }\r | |
2107 | \r | |
2108 | static expr_ty\r | |
2109 | ast_for_testlist(struct compiling *c, const node* n)\r | |
2110 | {\r | |
2111 | /* testlist_comp: test (',' test)* [','] */\r | |
2112 | /* testlist: test (',' test)* [','] */\r | |
2113 | /* testlist_safe: test (',' test)+ [','] */\r | |
2114 | /* testlist1: test (',' test)* */\r | |
2115 | assert(NCH(n) > 0);\r | |
2116 | if (TYPE(n) == testlist_comp) {\r | |
2117 | if (NCH(n) > 1)\r | |
2118 | assert(TYPE(CHILD(n, 1)) != comp_for);\r | |
2119 | }\r | |
2120 | else {\r | |
2121 | assert(TYPE(n) == testlist ||\r | |
2122 | TYPE(n) == testlist_safe ||\r | |
2123 | TYPE(n) == testlist1);\r | |
2124 | }\r | |
2125 | if (NCH(n) == 1)\r | |
2126 | return ast_for_expr(c, CHILD(n, 0));\r | |
2127 | else {\r | |
2128 | asdl_seq *tmp = seq_for_testlist(c, n);\r | |
2129 | if (!tmp)\r | |
2130 | return NULL;\r | |
2131 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);\r | |
2132 | }\r | |
2133 | }\r | |
2134 | \r | |
2135 | static expr_ty\r | |
2136 | ast_for_testlist_comp(struct compiling *c, const node* n)\r | |
2137 | {\r | |
2138 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */\r | |
2139 | /* argument: test [ comp_for ] */\r | |
2140 | assert(TYPE(n) == testlist_comp || TYPE(n) == argument);\r | |
2141 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == comp_for)\r | |
2142 | return ast_for_genexp(c, n);\r | |
2143 | return ast_for_testlist(c, n);\r | |
2144 | }\r | |
2145 | \r | |
2146 | /* like ast_for_testlist() but returns a sequence */\r | |
2147 | static asdl_seq*\r | |
2148 | ast_for_class_bases(struct compiling *c, const node* n)\r | |
2149 | {\r | |
2150 | /* testlist: test (',' test)* [','] */\r | |
2151 | assert(NCH(n) > 0);\r | |
2152 | REQ(n, testlist);\r | |
2153 | if (NCH(n) == 1) {\r | |
2154 | expr_ty base;\r | |
2155 | asdl_seq *bases = asdl_seq_new(1, c->c_arena);\r | |
2156 | if (!bases)\r | |
2157 | return NULL;\r | |
2158 | base = ast_for_expr(c, CHILD(n, 0));\r | |
2159 | if (!base)\r | |
2160 | return NULL;\r | |
2161 | asdl_seq_SET(bases, 0, base);\r | |
2162 | return bases;\r | |
2163 | }\r | |
2164 | \r | |
2165 | return seq_for_testlist(c, n);\r | |
2166 | }\r | |
2167 | \r | |
2168 | static stmt_ty\r | |
2169 | ast_for_expr_stmt(struct compiling *c, const node *n)\r | |
2170 | {\r | |
2171 | REQ(n, expr_stmt);\r | |
2172 | /* expr_stmt: testlist (augassign (yield_expr|testlist)\r | |
2173 | | ('=' (yield_expr|testlist))*)\r | |
2174 | testlist: test (',' test)* [',']\r | |
2175 | augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='\r | |
2176 | | '<<=' | '>>=' | '**=' | '//='\r | |
2177 | test: ... here starts the operator precendence dance\r | |
2178 | */\r | |
2179 | \r | |
2180 | if (NCH(n) == 1) {\r | |
2181 | expr_ty e = ast_for_testlist(c, CHILD(n, 0));\r | |
2182 | if (!e)\r | |
2183 | return NULL;\r | |
2184 | \r | |
2185 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);\r | |
2186 | }\r | |
2187 | else if (TYPE(CHILD(n, 1)) == augassign) {\r | |
2188 | expr_ty expr1, expr2;\r | |
2189 | operator_ty newoperator;\r | |
2190 | node *ch = CHILD(n, 0);\r | |
2191 | \r | |
2192 | expr1 = ast_for_testlist(c, ch);\r | |
2193 | if (!expr1)\r | |
2194 | return NULL;\r | |
2195 | if(!set_context(c, expr1, Store, ch))\r | |
2196 | return NULL;\r | |
2197 | /* set_context checks that most expressions are not the left side.\r | |
2198 | Augmented assignments can only have a name, a subscript, or an\r | |
2199 | attribute on the left, though, so we have to explicitly check for\r | |
2200 | those. */\r | |
2201 | switch (expr1->kind) {\r | |
2202 | case Name_kind:\r | |
2203 | case Attribute_kind:\r | |
2204 | case Subscript_kind:\r | |
2205 | break;\r | |
2206 | default:\r | |
2207 | ast_error(ch, "illegal expression for augmented assignment");\r | |
2208 | return NULL;\r | |
2209 | }\r | |
2210 | \r | |
2211 | ch = CHILD(n, 2);\r | |
2212 | if (TYPE(ch) == testlist)\r | |
2213 | expr2 = ast_for_testlist(c, ch);\r | |
2214 | else\r | |
2215 | expr2 = ast_for_expr(c, ch);\r | |
2216 | if (!expr2)\r | |
2217 | return NULL;\r | |
2218 | \r | |
2219 | newoperator = ast_for_augassign(c, CHILD(n, 1));\r | |
2220 | if (!newoperator)\r | |
2221 | return NULL;\r | |
2222 | \r | |
2223 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,\r | |
2224 | c->c_arena);\r | |
2225 | }\r | |
2226 | else {\r | |
2227 | int i;\r | |
2228 | asdl_seq *targets;\r | |
2229 | node *value;\r | |
2230 | expr_ty expression;\r | |
2231 | \r | |
2232 | /* a normal assignment */\r | |
2233 | REQ(CHILD(n, 1), EQUAL);\r | |
2234 | targets = asdl_seq_new(NCH(n) / 2, c->c_arena);\r | |
2235 | if (!targets)\r | |
2236 | return NULL;\r | |
2237 | for (i = 0; i < NCH(n) - 2; i += 2) {\r | |
2238 | expr_ty e;\r | |
2239 | node *ch = CHILD(n, i);\r | |
2240 | if (TYPE(ch) == yield_expr) {\r | |
2241 | ast_error(ch, "assignment to yield expression not possible");\r | |
2242 | return NULL;\r | |
2243 | }\r | |
2244 | e = ast_for_testlist(c, ch);\r | |
2245 | if (!e)\r | |
2246 | return NULL;\r | |
2247 | \r | |
2248 | /* set context to assign */\r | |
2249 | if (!set_context(c, e, Store, CHILD(n, i)))\r | |
2250 | return NULL;\r | |
2251 | \r | |
2252 | asdl_seq_SET(targets, i / 2, e);\r | |
2253 | }\r | |
2254 | value = CHILD(n, NCH(n) - 1);\r | |
2255 | if (TYPE(value) == testlist)\r | |
2256 | expression = ast_for_testlist(c, value);\r | |
2257 | else\r | |
2258 | expression = ast_for_expr(c, value);\r | |
2259 | if (!expression)\r | |
2260 | return NULL;\r | |
2261 | return Assign(targets, expression, LINENO(n), n->n_col_offset,\r | |
2262 | c->c_arena);\r | |
2263 | }\r | |
2264 | }\r | |
2265 | \r | |
2266 | static stmt_ty\r | |
2267 | ast_for_print_stmt(struct compiling *c, const node *n)\r | |
2268 | {\r | |
2269 | /* print_stmt: 'print' ( [ test (',' test)* [','] ]\r | |
2270 | | '>>' test [ (',' test)+ [','] ] )\r | |
2271 | */\r | |
2272 | expr_ty dest = NULL, expression;\r | |
2273 | asdl_seq *seq = NULL;\r | |
2274 | bool nl;\r | |
2275 | int i, j, values_count, start = 1;\r | |
2276 | \r | |
2277 | REQ(n, print_stmt);\r | |
2278 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {\r | |
2279 | dest = ast_for_expr(c, CHILD(n, 2));\r | |
2280 | if (!dest)\r | |
2281 | return NULL;\r | |
2282 | start = 4;\r | |
2283 | }\r | |
2284 | values_count = (NCH(n) + 1 - start) / 2;\r | |
2285 | if (values_count) {\r | |
2286 | seq = asdl_seq_new(values_count, c->c_arena);\r | |
2287 | if (!seq)\r | |
2288 | return NULL;\r | |
2289 | for (i = start, j = 0; i < NCH(n); i += 2, ++j) {\r | |
2290 | expression = ast_for_expr(c, CHILD(n, i));\r | |
2291 | if (!expression)\r | |
2292 | return NULL;\r | |
2293 | asdl_seq_SET(seq, j, expression);\r | |
2294 | }\r | |
2295 | }\r | |
2296 | nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;\r | |
2297 | return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);\r | |
2298 | }\r | |
2299 | \r | |
2300 | static asdl_seq *\r | |
2301 | ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)\r | |
2302 | {\r | |
2303 | asdl_seq *seq;\r | |
2304 | int i;\r | |
2305 | expr_ty e;\r | |
2306 | \r | |
2307 | REQ(n, exprlist);\r | |
2308 | \r | |
2309 | seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);\r | |
2310 | if (!seq)\r | |
2311 | return NULL;\r | |
2312 | for (i = 0; i < NCH(n); i += 2) {\r | |
2313 | e = ast_for_expr(c, CHILD(n, i));\r | |
2314 | if (!e)\r | |
2315 | return NULL;\r | |
2316 | asdl_seq_SET(seq, i / 2, e);\r | |
2317 | if (context && !set_context(c, e, context, CHILD(n, i)))\r | |
2318 | return NULL;\r | |
2319 | }\r | |
2320 | return seq;\r | |
2321 | }\r | |
2322 | \r | |
2323 | static stmt_ty\r | |
2324 | ast_for_del_stmt(struct compiling *c, const node *n)\r | |
2325 | {\r | |
2326 | asdl_seq *expr_list;\r | |
2327 | \r | |
2328 | /* del_stmt: 'del' exprlist */\r | |
2329 | REQ(n, del_stmt);\r | |
2330 | \r | |
2331 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);\r | |
2332 | if (!expr_list)\r | |
2333 | return NULL;\r | |
2334 | return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);\r | |
2335 | }\r | |
2336 | \r | |
2337 | static stmt_ty\r | |
2338 | ast_for_flow_stmt(struct compiling *c, const node *n)\r | |
2339 | {\r | |
2340 | /*\r | |
2341 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt\r | |
2342 | | yield_stmt\r | |
2343 | break_stmt: 'break'\r | |
2344 | continue_stmt: 'continue'\r | |
2345 | return_stmt: 'return' [testlist]\r | |
2346 | yield_stmt: yield_expr\r | |
2347 | yield_expr: 'yield' testlist\r | |
2348 | raise_stmt: 'raise' [test [',' test [',' test]]]\r | |
2349 | */\r | |
2350 | node *ch;\r | |
2351 | \r | |
2352 | REQ(n, flow_stmt);\r | |
2353 | ch = CHILD(n, 0);\r | |
2354 | switch (TYPE(ch)) {\r | |
2355 | case break_stmt:\r | |
2356 | return Break(LINENO(n), n->n_col_offset, c->c_arena);\r | |
2357 | case continue_stmt:\r | |
2358 | return Continue(LINENO(n), n->n_col_offset, c->c_arena);\r | |
2359 | case yield_stmt: { /* will reduce to yield_expr */\r | |
2360 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0));\r | |
2361 | if (!exp)\r | |
2362 | return NULL;\r | |
2363 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);\r | |
2364 | }\r | |
2365 | case return_stmt:\r | |
2366 | if (NCH(ch) == 1)\r | |
2367 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);\r | |
2368 | else {\r | |
2369 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));\r | |
2370 | if (!expression)\r | |
2371 | return NULL;\r | |
2372 | return Return(expression, LINENO(n), n->n_col_offset,\r | |
2373 | c->c_arena);\r | |
2374 | }\r | |
2375 | case raise_stmt:\r | |
2376 | if (NCH(ch) == 1)\r | |
2377 | return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,\r | |
2378 | c->c_arena);\r | |
2379 | else if (NCH(ch) == 2) {\r | |
2380 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1));\r | |
2381 | if (!expression)\r | |
2382 | return NULL;\r | |
2383 | return Raise(expression, NULL, NULL, LINENO(n),\r | |
2384 | n->n_col_offset, c->c_arena);\r | |
2385 | }\r | |
2386 | else if (NCH(ch) == 4) {\r | |
2387 | expr_ty expr1, expr2;\r | |
2388 | \r | |
2389 | expr1 = ast_for_expr(c, CHILD(ch, 1));\r | |
2390 | if (!expr1)\r | |
2391 | return NULL;\r | |
2392 | expr2 = ast_for_expr(c, CHILD(ch, 3));\r | |
2393 | if (!expr2)\r | |
2394 | return NULL;\r | |
2395 | \r | |
2396 | return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,\r | |
2397 | c->c_arena);\r | |
2398 | }\r | |
2399 | else if (NCH(ch) == 6) {\r | |
2400 | expr_ty expr1, expr2, expr3;\r | |
2401 | \r | |
2402 | expr1 = ast_for_expr(c, CHILD(ch, 1));\r | |
2403 | if (!expr1)\r | |
2404 | return NULL;\r | |
2405 | expr2 = ast_for_expr(c, CHILD(ch, 3));\r | |
2406 | if (!expr2)\r | |
2407 | return NULL;\r | |
2408 | expr3 = ast_for_expr(c, CHILD(ch, 5));\r | |
2409 | if (!expr3)\r | |
2410 | return NULL;\r | |
2411 | \r | |
2412 | return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,\r | |
2413 | c->c_arena);\r | |
2414 | }\r | |
2415 | default:\r | |
2416 | PyErr_Format(PyExc_SystemError,\r | |
2417 | "unexpected flow_stmt: %d", TYPE(ch));\r | |
2418 | return NULL;\r | |
2419 | }\r | |
2420 | \r | |
2421 | PyErr_SetString(PyExc_SystemError, "unhandled flow statement");\r | |
2422 | return NULL;\r | |
2423 | }\r | |
2424 | \r | |
2425 | static alias_ty\r | |
2426 | alias_for_import_name(struct compiling *c, const node *n, int store)\r | |
2427 | {\r | |
2428 | /*\r | |
2429 | import_as_name: NAME ['as' NAME]\r | |
2430 | dotted_as_name: dotted_name ['as' NAME]\r | |
2431 | dotted_name: NAME ('.' NAME)*\r | |
2432 | */\r | |
2433 | PyObject *str, *name;\r | |
2434 | \r | |
2435 | loop:\r | |
2436 | switch (TYPE(n)) {\r | |
2437 | case import_as_name: {\r | |
2438 | node *name_node = CHILD(n, 0);\r | |
2439 | str = NULL;\r | |
2440 | if (NCH(n) == 3) {\r | |
2441 | node *str_node = CHILD(n, 2);\r | |
2442 | if (store && !forbidden_check(c, str_node, STR(str_node)))\r | |
2443 | return NULL;\r | |
2444 | str = NEW_IDENTIFIER(str_node);\r | |
2445 | if (!str)\r | |
2446 | return NULL;\r | |
2447 | }\r | |
2448 | else {\r | |
2449 | if (!forbidden_check(c, name_node, STR(name_node)))\r | |
2450 | return NULL;\r | |
2451 | }\r | |
2452 | name = NEW_IDENTIFIER(name_node);\r | |
2453 | if (!name)\r | |
2454 | return NULL;\r | |
2455 | return alias(name, str, c->c_arena);\r | |
2456 | }\r | |
2457 | case dotted_as_name:\r | |
2458 | if (NCH(n) == 1) {\r | |
2459 | n = CHILD(n, 0);\r | |
2460 | goto loop;\r | |
2461 | }\r | |
2462 | else {\r | |
2463 | node *asname_node = CHILD(n, 2);\r | |
2464 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);\r | |
2465 | if (!a)\r | |
2466 | return NULL;\r | |
2467 | assert(!a->asname);\r | |
2468 | if (!forbidden_check(c, asname_node, STR(asname_node)))\r | |
2469 | return NULL;\r | |
2470 | a->asname = NEW_IDENTIFIER(asname_node);\r | |
2471 | if (!a->asname)\r | |
2472 | return NULL;\r | |
2473 | return a;\r | |
2474 | }\r | |
2475 | break;\r | |
2476 | case dotted_name:\r | |
2477 | if (NCH(n) == 1) {\r | |
2478 | node *name_node = CHILD(n, 0);\r | |
2479 | if (store && !forbidden_check(c, name_node, STR(name_node)))\r | |
2480 | return NULL;\r | |
2481 | name = NEW_IDENTIFIER(name_node);\r | |
2482 | if (!name)\r | |
2483 | return NULL;\r | |
2484 | return alias(name, NULL, c->c_arena);\r | |
2485 | }\r | |
2486 | else {\r | |
2487 | /* Create a string of the form "a.b.c" */\r | |
2488 | int i;\r | |
2489 | size_t len;\r | |
2490 | char *s;\r | |
2491 | \r | |
2492 | len = 0;\r | |
2493 | for (i = 0; i < NCH(n); i += 2)\r | |
2494 | /* length of string plus one for the dot */\r | |
2495 | len += strlen(STR(CHILD(n, i))) + 1;\r | |
2496 | len--; /* the last name doesn't have a dot */\r | |
2497 | str = PyString_FromStringAndSize(NULL, len);\r | |
2498 | if (!str)\r | |
2499 | return NULL;\r | |
2500 | s = PyString_AS_STRING(str);\r | |
2501 | if (!s)\r | |
2502 | return NULL;\r | |
2503 | for (i = 0; i < NCH(n); i += 2) {\r | |
2504 | char *sch = STR(CHILD(n, i));\r | |
2505 | strcpy(s, STR(CHILD(n, i)));\r | |
2506 | s += strlen(sch);\r | |
2507 | *s++ = '.';\r | |
2508 | }\r | |
2509 | --s;\r | |
2510 | *s = '\0';\r | |
2511 | PyString_InternInPlace(&str);\r | |
2512 | PyArena_AddPyObject(c->c_arena, str);\r | |
2513 | return alias(str, NULL, c->c_arena);\r | |
2514 | }\r | |
2515 | break;\r | |
2516 | case STAR:\r | |
2517 | str = PyString_InternFromString("*");\r | |
2518 | PyArena_AddPyObject(c->c_arena, str);\r | |
2519 | return alias(str, NULL, c->c_arena);\r | |
2520 | default:\r | |
2521 | PyErr_Format(PyExc_SystemError,\r | |
2522 | "unexpected import name: %d", TYPE(n));\r | |
2523 | return NULL;\r | |
2524 | }\r | |
2525 | \r | |
2526 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition");\r | |
2527 | return NULL;\r | |
2528 | }\r | |
2529 | \r | |
2530 | static stmt_ty\r | |
2531 | ast_for_import_stmt(struct compiling *c, const node *n)\r | |
2532 | {\r | |
2533 | /*\r | |
2534 | import_stmt: import_name | import_from\r | |
2535 | import_name: 'import' dotted_as_names\r | |
2536 | import_from: 'from' ('.'* dotted_name | '.') 'import'\r | |
2537 | ('*' | '(' import_as_names ')' | import_as_names)\r | |
2538 | */\r | |
2539 | int lineno;\r | |
2540 | int col_offset;\r | |
2541 | int i;\r | |
2542 | asdl_seq *aliases;\r | |
2543 | \r | |
2544 | REQ(n, import_stmt);\r | |
2545 | lineno = LINENO(n);\r | |
2546 | col_offset = n->n_col_offset;\r | |
2547 | n = CHILD(n, 0);\r | |
2548 | if (TYPE(n) == import_name) {\r | |
2549 | n = CHILD(n, 1);\r | |
2550 | REQ(n, dotted_as_names);\r | |
2551 | aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);\r | |
2552 | if (!aliases)\r | |
2553 | return NULL;\r | |
2554 | for (i = 0; i < NCH(n); i += 2) {\r | |
2555 | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);\r | |
2556 | if (!import_alias)\r | |
2557 | return NULL;\r | |
2558 | asdl_seq_SET(aliases, i / 2, import_alias);\r | |
2559 | }\r | |
2560 | return Import(aliases, lineno, col_offset, c->c_arena);\r | |
2561 | }\r | |
2562 | else if (TYPE(n) == import_from) {\r | |
2563 | int n_children;\r | |
2564 | int idx, ndots = 0;\r | |
2565 | alias_ty mod = NULL;\r | |
2566 | identifier modname = NULL;\r | |
2567 | \r | |
2568 | /* Count the number of dots (for relative imports) and check for the\r | |
2569 | optional module name */\r | |
2570 | for (idx = 1; idx < NCH(n); idx++) {\r | |
2571 | if (TYPE(CHILD(n, idx)) == dotted_name) {\r | |
2572 | mod = alias_for_import_name(c, CHILD(n, idx), 0);\r | |
2573 | if (!mod)\r | |
2574 | return NULL;\r | |
2575 | idx++;\r | |
2576 | break;\r | |
2577 | } else if (TYPE(CHILD(n, idx)) != DOT) {\r | |
2578 | break;\r | |
2579 | }\r | |
2580 | ndots++;\r | |
2581 | }\r | |
2582 | idx++; /* skip over the 'import' keyword */\r | |
2583 | switch (TYPE(CHILD(n, idx))) {\r | |
2584 | case STAR:\r | |
2585 | /* from ... import * */\r | |
2586 | n = CHILD(n, idx);\r | |
2587 | n_children = 1;\r | |
2588 | break;\r | |
2589 | case LPAR:\r | |
2590 | /* from ... import (x, y, z) */\r | |
2591 | n = CHILD(n, idx + 1);\r | |
2592 | n_children = NCH(n);\r | |
2593 | break;\r | |
2594 | case import_as_names:\r | |
2595 | /* from ... import x, y, z */\r | |
2596 | n = CHILD(n, idx);\r | |
2597 | n_children = NCH(n);\r | |
2598 | if (n_children % 2 == 0) {\r | |
2599 | ast_error(n, "trailing comma not allowed without"\r | |
2600 | " surrounding parentheses");\r | |
2601 | return NULL;\r | |
2602 | }\r | |
2603 | break;\r | |
2604 | default:\r | |
2605 | ast_error(n, "Unexpected node-type in from-import");\r | |
2606 | return NULL;\r | |
2607 | }\r | |
2608 | \r | |
2609 | aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);\r | |
2610 | if (!aliases)\r | |
2611 | return NULL;\r | |
2612 | \r | |
2613 | /* handle "from ... import *" special b/c there's no children */\r | |
2614 | if (TYPE(n) == STAR) {\r | |
2615 | alias_ty import_alias = alias_for_import_name(c, n, 1);\r | |
2616 | if (!import_alias)\r | |
2617 | return NULL;\r | |
2618 | asdl_seq_SET(aliases, 0, import_alias);\r | |
2619 | }\r | |
2620 | else {\r | |
2621 | for (i = 0; i < NCH(n); i += 2) {\r | |
2622 | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);\r | |
2623 | if (!import_alias)\r | |
2624 | return NULL;\r | |
2625 | asdl_seq_SET(aliases, i / 2, import_alias);\r | |
2626 | }\r | |
2627 | }\r | |
2628 | if (mod != NULL)\r | |
2629 | modname = mod->name;\r | |
2630 | return ImportFrom(modname, aliases, ndots, lineno, col_offset,\r | |
2631 | c->c_arena);\r | |
2632 | }\r | |
2633 | PyErr_Format(PyExc_SystemError,\r | |
2634 | "unknown import statement: starts with command '%s'",\r | |
2635 | STR(CHILD(n, 0)));\r | |
2636 | return NULL;\r | |
2637 | }\r | |
2638 | \r | |
2639 | static stmt_ty\r | |
2640 | ast_for_global_stmt(struct compiling *c, const node *n)\r | |
2641 | {\r | |
2642 | /* global_stmt: 'global' NAME (',' NAME)* */\r | |
2643 | identifier name;\r | |
2644 | asdl_seq *s;\r | |
2645 | int i;\r | |
2646 | \r | |
2647 | REQ(n, global_stmt);\r | |
2648 | s = asdl_seq_new(NCH(n) / 2, c->c_arena);\r | |
2649 | if (!s)\r | |
2650 | return NULL;\r | |
2651 | for (i = 1; i < NCH(n); i += 2) {\r | |
2652 | name = NEW_IDENTIFIER(CHILD(n, i));\r | |
2653 | if (!name)\r | |
2654 | return NULL;\r | |
2655 | asdl_seq_SET(s, i / 2, name);\r | |
2656 | }\r | |
2657 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena);\r | |
2658 | }\r | |
2659 | \r | |
2660 | static stmt_ty\r | |
2661 | ast_for_exec_stmt(struct compiling *c, const node *n)\r | |
2662 | {\r | |
2663 | expr_ty expr1, globals = NULL, locals = NULL;\r | |
2664 | int n_children = NCH(n);\r | |
2665 | if (n_children != 2 && n_children != 4 && n_children != 6) {\r | |
2666 | PyErr_Format(PyExc_SystemError,\r | |
2667 | "poorly formed 'exec' statement: %d parts to statement",\r | |
2668 | n_children);\r | |
2669 | return NULL;\r | |
2670 | }\r | |
2671 | \r | |
2672 | /* exec_stmt: 'exec' expr ['in' test [',' test]] */\r | |
2673 | REQ(n, exec_stmt);\r | |
2674 | expr1 = ast_for_expr(c, CHILD(n, 1));\r | |
2675 | if (!expr1)\r | |
2676 | return NULL;\r | |
2677 | if (n_children >= 4) {\r | |
2678 | globals = ast_for_expr(c, CHILD(n, 3));\r | |
2679 | if (!globals)\r | |
2680 | return NULL;\r | |
2681 | }\r | |
2682 | if (n_children == 6) {\r | |
2683 | locals = ast_for_expr(c, CHILD(n, 5));\r | |
2684 | if (!locals)\r | |
2685 | return NULL;\r | |
2686 | }\r | |
2687 | \r | |
2688 | return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,\r | |
2689 | c->c_arena);\r | |
2690 | }\r | |
2691 | \r | |
2692 | static stmt_ty\r | |
2693 | ast_for_assert_stmt(struct compiling *c, const node *n)\r | |
2694 | {\r | |
2695 | /* assert_stmt: 'assert' test [',' test] */\r | |
2696 | REQ(n, assert_stmt);\r | |
2697 | if (NCH(n) == 2) {\r | |
2698 | expr_ty expression = ast_for_expr(c, CHILD(n, 1));\r | |
2699 | if (!expression)\r | |
2700 | return NULL;\r | |
2701 | return Assert(expression, NULL, LINENO(n), n->n_col_offset,\r | |
2702 | c->c_arena);\r | |
2703 | }\r | |
2704 | else if (NCH(n) == 4) {\r | |
2705 | expr_ty expr1, expr2;\r | |
2706 | \r | |
2707 | expr1 = ast_for_expr(c, CHILD(n, 1));\r | |
2708 | if (!expr1)\r | |
2709 | return NULL;\r | |
2710 | expr2 = ast_for_expr(c, CHILD(n, 3));\r | |
2711 | if (!expr2)\r | |
2712 | return NULL;\r | |
2713 | \r | |
2714 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);\r | |
2715 | }\r | |
2716 | PyErr_Format(PyExc_SystemError,\r | |
2717 | "improper number of parts to 'assert' statement: %d",\r | |
2718 | NCH(n));\r | |
2719 | return NULL;\r | |
2720 | }\r | |
2721 | \r | |
2722 | static asdl_seq *\r | |
2723 | ast_for_suite(struct compiling *c, const node *n)\r | |
2724 | {\r | |
2725 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */\r | |
2726 | asdl_seq *seq;\r | |
2727 | stmt_ty s;\r | |
2728 | int i, total, num, end, pos = 0;\r | |
2729 | node *ch;\r | |
2730 | \r | |
2731 | REQ(n, suite);\r | |
2732 | \r | |
2733 | total = num_stmts(n);\r | |
2734 | seq = asdl_seq_new(total, c->c_arena);\r | |
2735 | if (!seq)\r | |
2736 | return NULL;\r | |
2737 | if (TYPE(CHILD(n, 0)) == simple_stmt) {\r | |
2738 | n = CHILD(n, 0);\r | |
2739 | /* simple_stmt always ends with a NEWLINE,\r | |
2740 | and may have a trailing SEMI\r | |
2741 | */\r | |
2742 | end = NCH(n) - 1;\r | |
2743 | if (TYPE(CHILD(n, end - 1)) == SEMI)\r | |
2744 | end--;\r | |
2745 | /* loop by 2 to skip semi-colons */\r | |
2746 | for (i = 0; i < end; i += 2) {\r | |
2747 | ch = CHILD(n, i);\r | |
2748 | s = ast_for_stmt(c, ch);\r | |
2749 | if (!s)\r | |
2750 | return NULL;\r | |
2751 | asdl_seq_SET(seq, pos++, s);\r | |
2752 | }\r | |
2753 | }\r | |
2754 | else {\r | |
2755 | for (i = 2; i < (NCH(n) - 1); i++) {\r | |
2756 | ch = CHILD(n, i);\r | |
2757 | REQ(ch, stmt);\r | |
2758 | num = num_stmts(ch);\r | |
2759 | if (num == 1) {\r | |
2760 | /* small_stmt or compound_stmt with only one child */\r | |
2761 | s = ast_for_stmt(c, ch);\r | |
2762 | if (!s)\r | |
2763 | return NULL;\r | |
2764 | asdl_seq_SET(seq, pos++, s);\r | |
2765 | }\r | |
2766 | else {\r | |
2767 | int j;\r | |
2768 | ch = CHILD(ch, 0);\r | |
2769 | REQ(ch, simple_stmt);\r | |
2770 | for (j = 0; j < NCH(ch); j += 2) {\r | |
2771 | /* statement terminates with a semi-colon ';' */\r | |
2772 | if (NCH(CHILD(ch, j)) == 0) {\r | |
2773 | assert((j + 1) == NCH(ch));\r | |
2774 | break;\r | |
2775 | }\r | |
2776 | s = ast_for_stmt(c, CHILD(ch, j));\r | |
2777 | if (!s)\r | |
2778 | return NULL;\r | |
2779 | asdl_seq_SET(seq, pos++, s);\r | |
2780 | }\r | |
2781 | }\r | |
2782 | }\r | |
2783 | }\r | |
2784 | assert(pos == seq->size);\r | |
2785 | return seq;\r | |
2786 | }\r | |
2787 | \r | |
2788 | static stmt_ty\r | |
2789 | ast_for_if_stmt(struct compiling *c, const node *n)\r | |
2790 | {\r | |
2791 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*\r | |
2792 | ['else' ':' suite]\r | |
2793 | */\r | |
2794 | char *s;\r | |
2795 | \r | |
2796 | REQ(n, if_stmt);\r | |
2797 | \r | |
2798 | if (NCH(n) == 4) {\r | |
2799 | expr_ty expression;\r | |
2800 | asdl_seq *suite_seq;\r | |
2801 | \r | |
2802 | expression = ast_for_expr(c, CHILD(n, 1));\r | |
2803 | if (!expression)\r | |
2804 | return NULL;\r | |
2805 | suite_seq = ast_for_suite(c, CHILD(n, 3));\r | |
2806 | if (!suite_seq)\r | |
2807 | return NULL;\r | |
2808 | \r | |
2809 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,\r | |
2810 | c->c_arena);\r | |
2811 | }\r | |
2812 | \r | |
2813 | s = STR(CHILD(n, 4));\r | |
2814 | /* s[2], the third character in the string, will be\r | |
2815 | 's' for el_s_e, or\r | |
2816 | 'i' for el_i_f\r | |
2817 | */\r | |
2818 | if (s[2] == 's') {\r | |
2819 | expr_ty expression;\r | |
2820 | asdl_seq *seq1, *seq2;\r | |
2821 | \r | |
2822 | expression = ast_for_expr(c, CHILD(n, 1));\r | |
2823 | if (!expression)\r | |
2824 | return NULL;\r | |
2825 | seq1 = ast_for_suite(c, CHILD(n, 3));\r | |
2826 | if (!seq1)\r | |
2827 | return NULL;\r | |
2828 | seq2 = ast_for_suite(c, CHILD(n, 6));\r | |
2829 | if (!seq2)\r | |
2830 | return NULL;\r | |
2831 | \r | |
2832 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,\r | |
2833 | c->c_arena);\r | |
2834 | }\r | |
2835 | else if (s[2] == 'i') {\r | |
2836 | int i, n_elif, has_else = 0;\r | |
2837 | expr_ty expression;\r | |
2838 | asdl_seq *suite_seq;\r | |
2839 | asdl_seq *orelse = NULL;\r | |
2840 | n_elif = NCH(n) - 4;\r | |
2841 | /* must reference the child n_elif+1 since 'else' token is third,\r | |
2842 | not fourth, child from the end. */\r | |
2843 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME\r | |
2844 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') {\r | |
2845 | has_else = 1;\r | |
2846 | n_elif -= 3;\r | |
2847 | }\r | |
2848 | n_elif /= 4;\r | |
2849 | \r | |
2850 | if (has_else) {\r | |
2851 | asdl_seq *suite_seq2;\r | |
2852 | \r | |
2853 | orelse = asdl_seq_new(1, c->c_arena);\r | |
2854 | if (!orelse)\r | |
2855 | return NULL;\r | |
2856 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));\r | |
2857 | if (!expression)\r | |
2858 | return NULL;\r | |
2859 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));\r | |
2860 | if (!suite_seq)\r | |
2861 | return NULL;\r | |
2862 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));\r | |
2863 | if (!suite_seq2)\r | |
2864 | return NULL;\r | |
2865 | \r | |
2866 | asdl_seq_SET(orelse, 0,\r | |
2867 | If(expression, suite_seq, suite_seq2,\r | |
2868 | LINENO(CHILD(n, NCH(n) - 6)),\r | |
2869 | CHILD(n, NCH(n) - 6)->n_col_offset,\r | |
2870 | c->c_arena));\r | |
2871 | /* the just-created orelse handled the last elif */\r | |
2872 | n_elif--;\r | |
2873 | }\r | |
2874 | \r | |
2875 | for (i = 0; i < n_elif; i++) {\r | |
2876 | int off = 5 + (n_elif - i - 1) * 4;\r | |
2877 | asdl_seq *newobj = asdl_seq_new(1, c->c_arena);\r | |
2878 | if (!newobj)\r | |
2879 | return NULL;\r | |
2880 | expression = ast_for_expr(c, CHILD(n, off));\r | |
2881 | if (!expression)\r | |
2882 | return NULL;\r | |
2883 | suite_seq = ast_for_suite(c, CHILD(n, off + 2));\r | |
2884 | if (!suite_seq)\r | |
2885 | return NULL;\r | |
2886 | \r | |
2887 | asdl_seq_SET(newobj, 0,\r | |
2888 | If(expression, suite_seq, orelse,\r | |
2889 | LINENO(CHILD(n, off)),\r | |
2890 | CHILD(n, off)->n_col_offset, c->c_arena));\r | |
2891 | orelse = newobj;\r | |
2892 | }\r | |
2893 | expression = ast_for_expr(c, CHILD(n, 1));\r | |
2894 | if (!expression)\r | |
2895 | return NULL;\r | |
2896 | suite_seq = ast_for_suite(c, CHILD(n, 3));\r | |
2897 | if (!suite_seq)\r | |
2898 | return NULL;\r | |
2899 | return If(expression, suite_seq, orelse,\r | |
2900 | LINENO(n), n->n_col_offset, c->c_arena);\r | |
2901 | }\r | |
2902 | \r | |
2903 | PyErr_Format(PyExc_SystemError,\r | |
2904 | "unexpected token in 'if' statement: %s", s);\r | |
2905 | return NULL;\r | |
2906 | }\r | |
2907 | \r | |
2908 | static stmt_ty\r | |
2909 | ast_for_while_stmt(struct compiling *c, const node *n)\r | |
2910 | {\r | |
2911 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */\r | |
2912 | REQ(n, while_stmt);\r | |
2913 | \r | |
2914 | if (NCH(n) == 4) {\r | |
2915 | expr_ty expression;\r | |
2916 | asdl_seq *suite_seq;\r | |
2917 | \r | |
2918 | expression = ast_for_expr(c, CHILD(n, 1));\r | |
2919 | if (!expression)\r | |
2920 | return NULL;\r | |
2921 | suite_seq = ast_for_suite(c, CHILD(n, 3));\r | |
2922 | if (!suite_seq)\r | |
2923 | return NULL;\r | |
2924 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,\r | |
2925 | c->c_arena);\r | |
2926 | }\r | |
2927 | else if (NCH(n) == 7) {\r | |
2928 | expr_ty expression;\r | |
2929 | asdl_seq *seq1, *seq2;\r | |
2930 | \r | |
2931 | expression = ast_for_expr(c, CHILD(n, 1));\r | |
2932 | if (!expression)\r | |
2933 | return NULL;\r | |
2934 | seq1 = ast_for_suite(c, CHILD(n, 3));\r | |
2935 | if (!seq1)\r | |
2936 | return NULL;\r | |
2937 | seq2 = ast_for_suite(c, CHILD(n, 6));\r | |
2938 | if (!seq2)\r | |
2939 | return NULL;\r | |
2940 | \r | |
2941 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,\r | |
2942 | c->c_arena);\r | |
2943 | }\r | |
2944 | \r | |
2945 | PyErr_Format(PyExc_SystemError,\r | |
2946 | "wrong number of tokens for 'while' statement: %d",\r | |
2947 | NCH(n));\r | |
2948 | return NULL;\r | |
2949 | }\r | |
2950 | \r | |
2951 | static stmt_ty\r | |
2952 | ast_for_for_stmt(struct compiling *c, const node *n)\r | |
2953 | {\r | |
2954 | asdl_seq *_target, *seq = NULL, *suite_seq;\r | |
2955 | expr_ty expression;\r | |
2956 | expr_ty target, first;\r | |
2957 | const node *node_target;\r | |
2958 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */\r | |
2959 | REQ(n, for_stmt);\r | |
2960 | \r | |
2961 | if (NCH(n) == 9) {\r | |
2962 | seq = ast_for_suite(c, CHILD(n, 8));\r | |
2963 | if (!seq)\r | |
2964 | return NULL;\r | |
2965 | }\r | |
2966 | \r | |
2967 | node_target = CHILD(n, 1);\r | |
2968 | _target = ast_for_exprlist(c, node_target, Store);\r | |
2969 | if (!_target)\r | |
2970 | return NULL;\r | |
2971 | /* Check the # of children rather than the length of _target, since\r | |
2972 | for x, in ... has 1 element in _target, but still requires a Tuple. */\r | |
2973 | first = (expr_ty)asdl_seq_GET(_target, 0);\r | |
2974 | if (NCH(node_target) == 1)\r | |
2975 | target = first;\r | |
2976 | else\r | |
2977 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);\r | |
2978 | \r | |
2979 | expression = ast_for_testlist(c, CHILD(n, 3));\r | |
2980 | if (!expression)\r | |
2981 | return NULL;\r | |
2982 | suite_seq = ast_for_suite(c, CHILD(n, 5));\r | |
2983 | if (!suite_seq)\r | |
2984 | return NULL;\r | |
2985 | \r | |
2986 | return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,\r | |
2987 | c->c_arena);\r | |
2988 | }\r | |
2989 | \r | |
2990 | static excepthandler_ty\r | |
2991 | ast_for_except_clause(struct compiling *c, const node *exc, node *body)\r | |
2992 | {\r | |
2993 | /* except_clause: 'except' [test [(',' | 'as') test]] */\r | |
2994 | REQ(exc, except_clause);\r | |
2995 | REQ(body, suite);\r | |
2996 | \r | |
2997 | if (NCH(exc) == 1) {\r | |
2998 | asdl_seq *suite_seq = ast_for_suite(c, body);\r | |
2999 | if (!suite_seq)\r | |
3000 | return NULL;\r | |
3001 | \r | |
3002 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),\r | |
3003 | exc->n_col_offset, c->c_arena);\r | |
3004 | }\r | |
3005 | else if (NCH(exc) == 2) {\r | |
3006 | expr_ty expression;\r | |
3007 | asdl_seq *suite_seq;\r | |
3008 | \r | |
3009 | expression = ast_for_expr(c, CHILD(exc, 1));\r | |
3010 | if (!expression)\r | |
3011 | return NULL;\r | |
3012 | suite_seq = ast_for_suite(c, body);\r | |
3013 | if (!suite_seq)\r | |
3014 | return NULL;\r | |
3015 | \r | |
3016 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),\r | |
3017 | exc->n_col_offset, c->c_arena);\r | |
3018 | }\r | |
3019 | else if (NCH(exc) == 4) {\r | |
3020 | asdl_seq *suite_seq;\r | |
3021 | expr_ty expression;\r | |
3022 | expr_ty e = ast_for_expr(c, CHILD(exc, 3));\r | |
3023 | if (!e)\r | |
3024 | return NULL;\r | |
3025 | if (!set_context(c, e, Store, CHILD(exc, 3)))\r | |
3026 | return NULL;\r | |
3027 | expression = ast_for_expr(c, CHILD(exc, 1));\r | |
3028 | if (!expression)\r | |
3029 | return NULL;\r | |
3030 | suite_seq = ast_for_suite(c, body);\r | |
3031 | if (!suite_seq)\r | |
3032 | return NULL;\r | |
3033 | \r | |
3034 | return ExceptHandler(expression, e, suite_seq, LINENO(exc),\r | |
3035 | exc->n_col_offset, c->c_arena);\r | |
3036 | }\r | |
3037 | \r | |
3038 | PyErr_Format(PyExc_SystemError,\r | |
3039 | "wrong number of children for 'except' clause: %d",\r | |
3040 | NCH(exc));\r | |
3041 | return NULL;\r | |
3042 | }\r | |
3043 | \r | |
3044 | static stmt_ty\r | |
3045 | ast_for_try_stmt(struct compiling *c, const node *n)\r | |
3046 | {\r | |
3047 | const int nch = NCH(n);\r | |
3048 | int n_except = (nch - 3)/3;\r | |
3049 | asdl_seq *body, *orelse = NULL, *finally = NULL;\r | |
3050 | \r | |
3051 | REQ(n, try_stmt);\r | |
3052 | \r | |
3053 | body = ast_for_suite(c, CHILD(n, 2));\r | |
3054 | if (body == NULL)\r | |
3055 | return NULL;\r | |
3056 | \r | |
3057 | if (TYPE(CHILD(n, nch - 3)) == NAME) {\r | |
3058 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {\r | |
3059 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {\r | |
3060 | /* we can assume it's an "else",\r | |
3061 | because nch >= 9 for try-else-finally and\r | |
3062 | it would otherwise have a type of except_clause */\r | |
3063 | orelse = ast_for_suite(c, CHILD(n, nch - 4));\r | |
3064 | if (orelse == NULL)\r | |
3065 | return NULL;\r | |
3066 | n_except--;\r | |
3067 | }\r | |
3068 | \r | |
3069 | finally = ast_for_suite(c, CHILD(n, nch - 1));\r | |
3070 | if (finally == NULL)\r | |
3071 | return NULL;\r | |
3072 | n_except--;\r | |
3073 | }\r | |
3074 | else {\r | |
3075 | /* we can assume it's an "else",\r | |
3076 | otherwise it would have a type of except_clause */\r | |
3077 | orelse = ast_for_suite(c, CHILD(n, nch - 1));\r | |
3078 | if (orelse == NULL)\r | |
3079 | return NULL;\r | |
3080 | n_except--;\r | |
3081 | }\r | |
3082 | }\r | |
3083 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) {\r | |
3084 | ast_error(n, "malformed 'try' statement");\r | |
3085 | return NULL;\r | |
3086 | }\r | |
3087 | \r | |
3088 | if (n_except > 0) {\r | |
3089 | int i;\r | |
3090 | stmt_ty except_st;\r | |
3091 | /* process except statements to create a try ... except */\r | |
3092 | asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);\r | |
3093 | if (handlers == NULL)\r | |
3094 | return NULL;\r | |
3095 | \r | |
3096 | for (i = 0; i < n_except; i++) {\r | |
3097 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),\r | |
3098 | CHILD(n, 5 + i * 3));\r | |
3099 | if (!e)\r | |
3100 | return NULL;\r | |
3101 | asdl_seq_SET(handlers, i, e);\r | |
3102 | }\r | |
3103 | \r | |
3104 | except_st = TryExcept(body, handlers, orelse, LINENO(n),\r | |
3105 | n->n_col_offset, c->c_arena);\r | |
3106 | if (!finally)\r | |
3107 | return except_st;\r | |
3108 | \r | |
3109 | /* if a 'finally' is present too, we nest the TryExcept within a\r | |
3110 | TryFinally to emulate try ... except ... finally */\r | |
3111 | body = asdl_seq_new(1, c->c_arena);\r | |
3112 | if (body == NULL)\r | |
3113 | return NULL;\r | |
3114 | asdl_seq_SET(body, 0, except_st);\r | |
3115 | }\r | |
3116 | \r | |
3117 | /* must be a try ... finally (except clauses are in body, if any exist) */\r | |
3118 | assert(finally != NULL);\r | |
3119 | return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);\r | |
3120 | }\r | |
3121 | \r | |
3122 | /* with_item: test ['as' expr] */\r | |
3123 | static stmt_ty\r | |
3124 | ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)\r | |
3125 | {\r | |
3126 | expr_ty context_expr, optional_vars = NULL;\r | |
3127 | \r | |
3128 | REQ(n, with_item);\r | |
3129 | context_expr = ast_for_expr(c, CHILD(n, 0));\r | |
3130 | if (!context_expr)\r | |
3131 | return NULL;\r | |
3132 | if (NCH(n) == 3) {\r | |
3133 | optional_vars = ast_for_expr(c, CHILD(n, 2));\r | |
3134 | \r | |
3135 | if (!optional_vars) {\r | |
3136 | return NULL;\r | |
3137 | }\r | |
3138 | if (!set_context(c, optional_vars, Store, n)) {\r | |
3139 | return NULL;\r | |
3140 | }\r | |
3141 | }\r | |
3142 | \r | |
3143 | return With(context_expr, optional_vars, content, LINENO(n),\r | |
3144 | n->n_col_offset, c->c_arena);\r | |
3145 | }\r | |
3146 | \r | |
3147 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */\r | |
3148 | static stmt_ty\r | |
3149 | ast_for_with_stmt(struct compiling *c, const node *n)\r | |
3150 | {\r | |
3151 | int i;\r | |
3152 | stmt_ty ret;\r | |
3153 | asdl_seq *inner;\r | |
3154 | \r | |
3155 | REQ(n, with_stmt);\r | |
3156 | \r | |
3157 | /* process the with items inside-out */\r | |
3158 | i = NCH(n) - 1;\r | |
3159 | /* the suite of the innermost with item is the suite of the with stmt */\r | |
3160 | inner = ast_for_suite(c, CHILD(n, i));\r | |
3161 | if (!inner)\r | |
3162 | return NULL;\r | |
3163 | \r | |
3164 | for (;;) {\r | |
3165 | i -= 2;\r | |
3166 | ret = ast_for_with_item(c, CHILD(n, i), inner);\r | |
3167 | if (!ret)\r | |
3168 | return NULL;\r | |
3169 | /* was this the last item? */\r | |
3170 | if (i == 1)\r | |
3171 | break;\r | |
3172 | /* if not, wrap the result so far in a new sequence */\r | |
3173 | inner = asdl_seq_new(1, c->c_arena);\r | |
3174 | if (!inner)\r | |
3175 | return NULL;\r | |
3176 | asdl_seq_SET(inner, 0, ret);\r | |
3177 | }\r | |
3178 | \r | |
3179 | return ret;\r | |
3180 | }\r | |
3181 | \r | |
3182 | static stmt_ty\r | |
3183 | ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)\r | |
3184 | {\r | |
3185 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */\r | |
3186 | PyObject *classname;\r | |
3187 | asdl_seq *bases, *s;\r | |
3188 | \r | |
3189 | REQ(n, classdef);\r | |
3190 | \r | |
3191 | if (!forbidden_check(c, n, STR(CHILD(n, 1))))\r | |
3192 | return NULL;\r | |
3193 | \r | |
3194 | if (NCH(n) == 4) {\r | |
3195 | s = ast_for_suite(c, CHILD(n, 3));\r | |
3196 | if (!s)\r | |
3197 | return NULL;\r | |
3198 | classname = NEW_IDENTIFIER(CHILD(n, 1));\r | |
3199 | if (!classname)\r | |
3200 | return NULL;\r | |
3201 | return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),\r | |
3202 | n->n_col_offset, c->c_arena);\r | |
3203 | }\r | |
3204 | /* check for empty base list */\r | |
3205 | if (TYPE(CHILD(n,3)) == RPAR) {\r | |
3206 | s = ast_for_suite(c, CHILD(n,5));\r | |
3207 | if (!s)\r | |
3208 | return NULL;\r | |
3209 | classname = NEW_IDENTIFIER(CHILD(n, 1));\r | |
3210 | if (!classname)\r | |
3211 | return NULL;\r | |
3212 | return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),\r | |
3213 | n->n_col_offset, c->c_arena);\r | |
3214 | }\r | |
3215 | \r | |
3216 | /* else handle the base class list */\r | |
3217 | bases = ast_for_class_bases(c, CHILD(n, 3));\r | |
3218 | if (!bases)\r | |
3219 | return NULL;\r | |
3220 | \r | |
3221 | s = ast_for_suite(c, CHILD(n, 6));\r | |
3222 | if (!s)\r | |
3223 | return NULL;\r | |
3224 | classname = NEW_IDENTIFIER(CHILD(n, 1));\r | |
3225 | if (!classname)\r | |
3226 | return NULL;\r | |
3227 | return ClassDef(classname, bases, s, decorator_seq,\r | |
3228 | LINENO(n), n->n_col_offset, c->c_arena);\r | |
3229 | }\r | |
3230 | \r | |
3231 | static stmt_ty\r | |
3232 | ast_for_stmt(struct compiling *c, const node *n)\r | |
3233 | {\r | |
3234 | if (TYPE(n) == stmt) {\r | |
3235 | assert(NCH(n) == 1);\r | |
3236 | n = CHILD(n, 0);\r | |
3237 | }\r | |
3238 | if (TYPE(n) == simple_stmt) {\r | |
3239 | assert(num_stmts(n) == 1);\r | |
3240 | n = CHILD(n, 0);\r | |
3241 | }\r | |
3242 | if (TYPE(n) == small_stmt) {\r | |
3243 | n = CHILD(n, 0);\r | |
3244 | /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt\r | |
3245 | | flow_stmt | import_stmt | global_stmt | exec_stmt\r | |
3246 | | assert_stmt\r | |
3247 | */\r | |
3248 | switch (TYPE(n)) {\r | |
3249 | case expr_stmt:\r | |
3250 | return ast_for_expr_stmt(c, n);\r | |
3251 | case print_stmt:\r | |
3252 | return ast_for_print_stmt(c, n);\r | |
3253 | case del_stmt:\r | |
3254 | return ast_for_del_stmt(c, n);\r | |
3255 | case pass_stmt:\r | |
3256 | return Pass(LINENO(n), n->n_col_offset, c->c_arena);\r | |
3257 | case flow_stmt:\r | |
3258 | return ast_for_flow_stmt(c, n);\r | |
3259 | case import_stmt:\r | |
3260 | return ast_for_import_stmt(c, n);\r | |
3261 | case global_stmt:\r | |
3262 | return ast_for_global_stmt(c, n);\r | |
3263 | case exec_stmt:\r | |
3264 | return ast_for_exec_stmt(c, n);\r | |
3265 | case assert_stmt:\r | |
3266 | return ast_for_assert_stmt(c, n);\r | |
3267 | default:\r | |
3268 | PyErr_Format(PyExc_SystemError,\r | |
3269 | "unhandled small_stmt: TYPE=%d NCH=%d\n",\r | |
3270 | TYPE(n), NCH(n));\r | |
3271 | return NULL;\r | |
3272 | }\r | |
3273 | }\r | |
3274 | else {\r | |
3275 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt\r | |
3276 | | funcdef | classdef | decorated\r | |
3277 | */\r | |
3278 | node *ch = CHILD(n, 0);\r | |
3279 | REQ(n, compound_stmt);\r | |
3280 | switch (TYPE(ch)) {\r | |
3281 | case if_stmt:\r | |
3282 | return ast_for_if_stmt(c, ch);\r | |
3283 | case while_stmt:\r | |
3284 | return ast_for_while_stmt(c, ch);\r | |
3285 | case for_stmt:\r | |
3286 | return ast_for_for_stmt(c, ch);\r | |
3287 | case try_stmt:\r | |
3288 | return ast_for_try_stmt(c, ch);\r | |
3289 | case with_stmt:\r | |
3290 | return ast_for_with_stmt(c, ch);\r | |
3291 | case funcdef:\r | |
3292 | return ast_for_funcdef(c, ch, NULL);\r | |
3293 | case classdef:\r | |
3294 | return ast_for_classdef(c, ch, NULL);\r | |
3295 | case decorated:\r | |
3296 | return ast_for_decorated(c, ch);\r | |
3297 | default:\r | |
3298 | PyErr_Format(PyExc_SystemError,\r | |
3299 | "unhandled small_stmt: TYPE=%d NCH=%d\n",\r | |
3300 | TYPE(n), NCH(n));\r | |
3301 | return NULL;\r | |
3302 | }\r | |
3303 | }\r | |
3304 | }\r | |
3305 | \r | |
3306 | static PyObject *\r | |
3307 | parsenumber(struct compiling *c, const char *s)\r | |
3308 | {\r | |
3309 | const char *end;\r | |
3310 | long x;\r | |
3311 | double dx;\r | |
3312 | #ifndef WITHOUT_COMPLEX\r | |
3313 | Py_complex complex;\r | |
3314 | int imflag;\r | |
3315 | #endif\r | |
3316 | \r | |
3317 | assert(s != NULL);\r | |
3318 | errno = 0;\r | |
3319 | end = s + strlen(s) - 1;\r | |
3320 | #ifndef WITHOUT_COMPLEX\r | |
3321 | imflag = *end == 'j' || *end == 'J';\r | |
3322 | #endif\r | |
3323 | if (*end == 'l' || *end == 'L')\r | |
3324 | return PyLong_FromString((char *)s, (char **)0, 0);\r | |
3325 | x = PyOS_strtol((char *)s, (char **)&end, 0);\r | |
3326 | if (*end == '\0') {\r | |
3327 | if (errno != 0)\r | |
3328 | return PyLong_FromString((char *)s, (char **)0, 0);\r | |
3329 | return PyInt_FromLong(x);\r | |
3330 | }\r | |
3331 | /* XXX Huge floats may silently fail */\r | |
3332 | #ifndef WITHOUT_COMPLEX\r | |
3333 | if (imflag) {\r | |
3334 | complex.real = 0.;\r | |
3335 | complex.imag = PyOS_string_to_double(s, (char **)&end, NULL);\r | |
3336 | if (complex.imag == -1.0 && PyErr_Occurred())\r | |
3337 | return NULL;\r | |
3338 | return PyComplex_FromCComplex(complex);\r | |
3339 | }\r | |
3340 | else\r | |
3341 | #endif\r | |
3342 | {\r | |
3343 | dx = PyOS_string_to_double(s, NULL, NULL);\r | |
3344 | if (dx == -1.0 && PyErr_Occurred())\r | |
3345 | return NULL;\r | |
3346 | return PyFloat_FromDouble(dx);\r | |
3347 | }\r | |
3348 | }\r | |
3349 | \r | |
3350 | static PyObject *\r | |
3351 | decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)\r | |
3352 | {\r | |
3353 | #ifndef Py_USING_UNICODE\r | |
3354 | Py_FatalError("decode_utf8 should not be called in this build.");\r | |
3355 | return NULL;\r | |
3356 | #else\r | |
3357 | PyObject *u, *v;\r | |
3358 | char *s, *t;\r | |
3359 | t = s = (char *)*sPtr;\r | |
3360 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */\r | |
3361 | while (s < end && (*s & 0x80)) s++;\r | |
3362 | *sPtr = s;\r | |
3363 | u = PyUnicode_DecodeUTF8(t, s - t, NULL);\r | |
3364 | if (u == NULL)\r | |
3365 | return NULL;\r | |
3366 | v = PyUnicode_AsEncodedString(u, encoding, NULL);\r | |
3367 | Py_DECREF(u);\r | |
3368 | return v;\r | |
3369 | #endif\r | |
3370 | }\r | |
3371 | \r | |
3372 | #ifdef Py_USING_UNICODE\r | |
3373 | static PyObject *\r | |
3374 | decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)\r | |
3375 | {\r | |
3376 | PyObject *v;\r | |
3377 | PyObject *u = NULL;\r | |
3378 | char *buf;\r | |
3379 | char *p;\r | |
3380 | const char *end;\r | |
3381 | if (encoding != NULL && strcmp(encoding, "iso-8859-1")) {\r | |
3382 | /* check for integer overflow */\r | |
3383 | if (len > PY_SIZE_MAX / 6)\r | |
3384 | return NULL;\r | |
3385 | /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5\r | |
3386 | "\รค" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */\r | |
3387 | u = PyString_FromStringAndSize((char *)NULL, len * 6);\r | |
3388 | if (u == NULL)\r | |
3389 | return NULL;\r | |
3390 | p = buf = PyString_AsString(u);\r | |
3391 | end = s + len;\r | |
3392 | while (s < end) {\r | |
3393 | if (*s == '\\') {\r | |
3394 | *p++ = *s++;\r | |
3395 | if (*s & 0x80) {\r | |
3396 | strcpy(p, "u005c");\r | |
3397 | p += 5;\r | |
3398 | }\r | |
3399 | }\r | |
3400 | if (*s & 0x80) { /* XXX inefficient */\r | |
3401 | PyObject *w;\r | |
3402 | char *r;\r | |
3403 | Py_ssize_t rn, i;\r | |
3404 | w = decode_utf8(c, &s, end, "utf-32-be");\r | |
3405 | if (w == NULL) {\r | |
3406 | Py_DECREF(u);\r | |
3407 | return NULL;\r | |
3408 | }\r | |
3409 | r = PyString_AsString(w);\r | |
3410 | rn = PyString_Size(w);\r | |
3411 | assert(rn % 4 == 0);\r | |
3412 | for (i = 0; i < rn; i += 4) {\r | |
3413 | sprintf(p, "\\U%02x%02x%02x%02x",\r | |
3414 | r[i + 0] & 0xFF,\r | |
3415 | r[i + 1] & 0xFF,\r | |
3416 | r[i + 2] & 0xFF,\r | |
3417 | r[i + 3] & 0xFF);\r | |
3418 | p += 10;\r | |
3419 | }\r | |
3420 | Py_DECREF(w);\r | |
3421 | } else {\r | |
3422 | *p++ = *s++;\r | |
3423 | }\r | |
3424 | }\r | |
3425 | len = p - buf;\r | |
3426 | s = buf;\r | |
3427 | }\r | |
3428 | if (rawmode)\r | |
3429 | v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);\r | |
3430 | else\r | |
3431 | v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);\r | |
3432 | Py_XDECREF(u);\r | |
3433 | return v;\r | |
3434 | }\r | |
3435 | #endif\r | |
3436 | \r | |
3437 | /* s is a Python string literal, including the bracketing quote characters,\r | |
3438 | * and r &/or u prefixes (if any), and embedded escape sequences (if any).\r | |
3439 | * parsestr parses it, and returns the decoded Python string object.\r | |
3440 | */\r | |
3441 | static PyObject *\r | |
3442 | parsestr(struct compiling *c, const char *s)\r | |
3443 | {\r | |
3444 | size_t len;\r | |
3445 | int quote = Py_CHARMASK(*s);\r | |
3446 | int rawmode = 0;\r | |
3447 | int need_encoding;\r | |
3448 | int unicode = c->c_future_unicode;\r | |
3449 | \r | |
3450 | if (isalpha(quote) || quote == '_') {\r | |
3451 | if (quote == 'u' || quote == 'U') {\r | |
3452 | quote = *++s;\r | |
3453 | unicode = 1;\r | |
3454 | }\r | |
3455 | if (quote == 'b' || quote == 'B') {\r | |
3456 | quote = *++s;\r | |
3457 | unicode = 0;\r | |
3458 | }\r | |
3459 | if (quote == 'r' || quote == 'R') {\r | |
3460 | quote = *++s;\r | |
3461 | rawmode = 1;\r | |
3462 | }\r | |
3463 | }\r | |
3464 | if (quote != '\'' && quote != '\"') {\r | |
3465 | PyErr_BadInternalCall();\r | |
3466 | return NULL;\r | |
3467 | }\r | |
3468 | s++;\r | |
3469 | len = strlen(s);\r | |
3470 | if (len > INT_MAX) {\r | |
3471 | PyErr_SetString(PyExc_OverflowError,\r | |
3472 | "string to parse is too long");\r | |
3473 | return NULL;\r | |
3474 | }\r | |
3475 | if (s[--len] != quote) {\r | |
3476 | PyErr_BadInternalCall();\r | |
3477 | return NULL;\r | |
3478 | }\r | |
3479 | if (len >= 4 && s[0] == quote && s[1] == quote) {\r | |
3480 | s += 2;\r | |
3481 | len -= 2;\r | |
3482 | if (s[--len] != quote || s[--len] != quote) {\r | |
3483 | PyErr_BadInternalCall();\r | |
3484 | return NULL;\r | |
3485 | }\r | |
3486 | }\r | |
3487 | #ifdef Py_USING_UNICODE\r | |
3488 | if (unicode || Py_UnicodeFlag) {\r | |
3489 | return decode_unicode(c, s, len, rawmode, c->c_encoding);\r | |
3490 | }\r | |
3491 | #endif\r | |
3492 | need_encoding = (c->c_encoding != NULL &&\r | |
3493 | strcmp(c->c_encoding, "utf-8") != 0 &&\r | |
3494 | strcmp(c->c_encoding, "iso-8859-1") != 0);\r | |
3495 | if (rawmode || strchr(s, '\\') == NULL) {\r | |
3496 | if (need_encoding) {\r | |
3497 | #ifndef Py_USING_UNICODE\r | |
3498 | /* This should not happen - we never see any other\r | |
3499 | encoding. */\r | |
3500 | Py_FatalError(\r | |
3501 | "cannot deal with encodings in this build.");\r | |
3502 | #else\r | |
3503 | PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);\r | |
3504 | if (u == NULL)\r | |
3505 | return NULL;\r | |
3506 | v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);\r | |
3507 | Py_DECREF(u);\r | |
3508 | return v;\r | |
3509 | #endif\r | |
3510 | } else {\r | |
3511 | return PyString_FromStringAndSize(s, len);\r | |
3512 | }\r | |
3513 | }\r | |
3514 | \r | |
3515 | return PyString_DecodeEscape(s, len, NULL, unicode,\r | |
3516 | need_encoding ? c->c_encoding : NULL);\r | |
3517 | }\r | |
3518 | \r | |
3519 | /* Build a Python string object out of a STRING atom. This takes care of\r | |
3520 | * compile-time literal catenation, calling parsestr() on each piece, and\r | |
3521 | * pasting the intermediate results together.\r | |
3522 | */\r | |
3523 | static PyObject *\r | |
3524 | parsestrplus(struct compiling *c, const node *n)\r | |
3525 | {\r | |
3526 | PyObject *v;\r | |
3527 | int i;\r | |
3528 | REQ(CHILD(n, 0), STRING);\r | |
3529 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {\r | |
3530 | /* String literal concatenation */\r | |
3531 | for (i = 1; i < NCH(n); i++) {\r | |
3532 | PyObject *s;\r | |
3533 | s = parsestr(c, STR(CHILD(n, i)));\r | |
3534 | if (s == NULL)\r | |
3535 | goto onError;\r | |
3536 | if (PyString_Check(v) && PyString_Check(s)) {\r | |
3537 | PyString_ConcatAndDel(&v, s);\r | |
3538 | if (v == NULL)\r | |
3539 | goto onError;\r | |
3540 | }\r | |
3541 | #ifdef Py_USING_UNICODE\r | |
3542 | else {\r | |
3543 | PyObject *temp = PyUnicode_Concat(v, s);\r | |
3544 | Py_DECREF(s);\r | |
3545 | Py_DECREF(v);\r | |
3546 | v = temp;\r | |
3547 | if (v == NULL)\r | |
3548 | goto onError;\r | |
3549 | }\r | |
3550 | #endif\r | |
3551 | }\r | |
3552 | }\r | |
3553 | return v;\r | |
3554 | \r | |
3555 | onError:\r | |
3556 | Py_XDECREF(v);\r | |
3557 | return NULL;\r | |
3558 | }\r |