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