Updated to reflect change in logging.config to remove out-of-date comment in _install...
[python.git] / Python / ast.c
blob4d874af5c24f54ba9961362144b045904c29a556
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 PyArena_AddPyObject(arena, id);
51 return id;
54 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
56 /* This routine provides an invalid object for the syntax error.
57 The outermost routine must unpack this error and create the
58 proper object. We do this so that we don't have to pass
59 the filename to everything function.
61 XXX Maybe we should just pass the filename...
64 static int
65 ast_error(const node *n, const char *errstr)
67 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
68 if (!u)
69 return 0;
70 PyErr_SetObject(PyExc_SyntaxError, u);
71 Py_DECREF(u);
72 return 0;
75 static void
76 ast_error_finish(const char *filename)
78 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
79 long lineno;
81 assert(PyErr_Occurred());
82 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
83 return;
85 PyErr_Fetch(&type, &value, &tback);
86 errstr = PyTuple_GetItem(value, 0);
87 if (!errstr)
88 return;
89 Py_INCREF(errstr);
90 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
91 if (lineno == -1) {
92 Py_DECREF(errstr);
93 return;
95 Py_DECREF(value);
97 loc = PyErr_ProgramText(filename, lineno);
98 if (!loc) {
99 Py_INCREF(Py_None);
100 loc = Py_None;
102 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
103 Py_DECREF(loc);
104 if (!tmp) {
105 Py_DECREF(errstr);
106 return;
108 value = PyTuple_Pack(2, errstr, tmp);
109 Py_DECREF(errstr);
110 Py_DECREF(tmp);
111 if (!value)
112 return;
113 PyErr_Restore(type, value, tback);
116 static int
117 ast_warn(struct compiling *c, const node *n, char *msg)
119 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
120 NULL, NULL) < 0) {
121 /* if -Werr, change it to a SyntaxError */
122 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
123 ast_error(n, msg);
124 return 0;
126 return 1;
129 static int
130 forbidden_check(struct compiling *c, const node *n, const char *x)
132 if (!strcmp(x, "None"))
133 return ast_error(n, "assignment to None");
134 if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) &&
135 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
136 return 0;
137 return 1;
140 /* num_stmts() returns number of contained statements.
142 Use this routine to determine how big a sequence is needed for
143 the statements in a parse tree. Its raison d'etre is this bit of
144 grammar:
146 stmt: simple_stmt | compound_stmt
147 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
149 A simple_stmt can contain multiple small_stmt elements joined
150 by semicolons. If the arg is a simple_stmt, the number of
151 small_stmt elements is returned.
154 static int
155 num_stmts(const node *n)
157 int i, l;
158 node *ch;
160 switch (TYPE(n)) {
161 case single_input:
162 if (TYPE(CHILD(n, 0)) == NEWLINE)
163 return 0;
164 else
165 return num_stmts(CHILD(n, 0));
166 case file_input:
167 l = 0;
168 for (i = 0; i < NCH(n); i++) {
169 ch = CHILD(n, i);
170 if (TYPE(ch) == stmt)
171 l += num_stmts(ch);
173 return l;
174 case stmt:
175 return num_stmts(CHILD(n, 0));
176 case compound_stmt:
177 return 1;
178 case simple_stmt:
179 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
180 case suite:
181 if (NCH(n) == 1)
182 return num_stmts(CHILD(n, 0));
183 else {
184 l = 0;
185 for (i = 2; i < (NCH(n) - 1); i++)
186 l += num_stmts(CHILD(n, i));
187 return l;
189 default: {
190 char buf[128];
192 sprintf(buf, "Non-statement found: %d %d\n",
193 TYPE(n), NCH(n));
194 Py_FatalError(buf);
197 assert(0);
198 return 0;
201 /* Transform the CST rooted at node * to the appropriate AST
204 mod_ty
205 PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
206 PyArena *arena)
208 int i, j, k, num;
209 asdl_seq *stmts = NULL;
210 stmt_ty s;
211 node *ch;
212 struct compiling c;
214 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
215 c.c_encoding = "utf-8";
216 if (TYPE(n) == encoding_decl) {
217 ast_error(n, "encoding declaration in Unicode string");
218 goto error;
220 } else if (TYPE(n) == encoding_decl) {
221 c.c_encoding = STR(n);
222 n = CHILD(n, 0);
223 } else {
224 c.c_encoding = NULL;
226 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
227 c.c_arena = arena;
228 c.c_filename = filename;
230 k = 0;
231 switch (TYPE(n)) {
232 case file_input:
233 stmts = asdl_seq_new(num_stmts(n), arena);
234 if (!stmts)
235 return NULL;
236 for (i = 0; i < NCH(n) - 1; i++) {
237 ch = CHILD(n, i);
238 if (TYPE(ch) == NEWLINE)
239 continue;
240 REQ(ch, stmt);
241 num = num_stmts(ch);
242 if (num == 1) {
243 s = ast_for_stmt(&c, ch);
244 if (!s)
245 goto error;
246 asdl_seq_SET(stmts, k++, s);
248 else {
249 ch = CHILD(ch, 0);
250 REQ(ch, simple_stmt);
251 for (j = 0; j < num; j++) {
252 s = ast_for_stmt(&c, CHILD(ch, j * 2));
253 if (!s)
254 goto error;
255 asdl_seq_SET(stmts, k++, s);
259 return Module(stmts, arena);
260 case eval_input: {
261 expr_ty testlist_ast;
263 /* XXX Why not gen_for here? */
264 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
265 if (!testlist_ast)
266 goto error;
267 return Expression(testlist_ast, arena);
269 case single_input:
270 if (TYPE(CHILD(n, 0)) == NEWLINE) {
271 stmts = asdl_seq_new(1, arena);
272 if (!stmts)
273 goto error;
274 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
275 arena));
276 if (!asdl_seq_GET(stmts, 0))
277 goto error;
278 return Interactive(stmts, arena);
280 else {
281 n = CHILD(n, 0);
282 num = num_stmts(n);
283 stmts = asdl_seq_new(num, arena);
284 if (!stmts)
285 goto error;
286 if (num == 1) {
287 s = ast_for_stmt(&c, n);
288 if (!s)
289 goto error;
290 asdl_seq_SET(stmts, 0, s);
292 else {
293 /* Only a simple_stmt can contain multiple statements. */
294 REQ(n, simple_stmt);
295 for (i = 0; i < NCH(n); i += 2) {
296 if (TYPE(CHILD(n, i)) == NEWLINE)
297 break;
298 s = ast_for_stmt(&c, CHILD(n, i));
299 if (!s)
300 goto error;
301 asdl_seq_SET(stmts, i / 2, s);
305 return Interactive(stmts, arena);
307 default:
308 PyErr_Format(PyExc_SystemError,
309 "invalid node %d for PyAST_FromNode", TYPE(n));
310 goto error;
312 error:
313 ast_error_finish(filename);
314 return NULL;
317 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
320 static operator_ty
321 get_operator(const node *n)
323 switch (TYPE(n)) {
324 case VBAR:
325 return BitOr;
326 case CIRCUMFLEX:
327 return BitXor;
328 case AMPER:
329 return BitAnd;
330 case LEFTSHIFT:
331 return LShift;
332 case RIGHTSHIFT:
333 return RShift;
334 case PLUS:
335 return Add;
336 case MINUS:
337 return Sub;
338 case STAR:
339 return Mult;
340 case SLASH:
341 return Div;
342 case DOUBLESLASH:
343 return FloorDiv;
344 case PERCENT:
345 return Mod;
346 default:
347 return (operator_ty)0;
351 /* Set the context ctx for expr_ty e, recursively traversing e.
353 Only sets context for expr kinds that "can appear in assignment context"
354 (according to ../Parser/Python.asdl). For other expr kinds, it sets
355 an appropriate syntax error and returns false.
358 static int
359 set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
361 asdl_seq *s = NULL;
362 /* If a particular expression type can't be used for assign / delete,
363 set expr_name to its name and an error message will be generated.
365 const char* expr_name = NULL;
367 /* The ast defines augmented store and load contexts, but the
368 implementation here doesn't actually use them. The code may be
369 a little more complex than necessary as a result. It also means
370 that expressions in an augmented assignment have a Store context.
371 Consider restructuring so that augmented assignment uses
372 set_context(), too.
374 assert(ctx != AugStore && ctx != AugLoad);
376 switch (e->kind) {
377 case Attribute_kind:
378 if (ctx == Store && !forbidden_check(c, n,
379 PyBytes_AS_STRING(e->v.Attribute.attr)))
380 return 0;
381 e->v.Attribute.ctx = ctx;
382 break;
383 case Subscript_kind:
384 e->v.Subscript.ctx = ctx;
385 break;
386 case Name_kind:
387 if (ctx == Store && !forbidden_check(c, n,
388 PyBytes_AS_STRING(e->v.Name.id)))
389 return 0;
390 e->v.Name.ctx = ctx;
391 break;
392 case List_kind:
393 e->v.List.ctx = ctx;
394 s = e->v.List.elts;
395 break;
396 case Tuple_kind:
397 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
398 return ast_error(n, "can't assign to ()");
399 e->v.Tuple.ctx = ctx;
400 s = e->v.Tuple.elts;
401 break;
402 case Lambda_kind:
403 expr_name = "lambda";
404 break;
405 case Call_kind:
406 expr_name = "function call";
407 break;
408 case BoolOp_kind:
409 case BinOp_kind:
410 case UnaryOp_kind:
411 expr_name = "operator";
412 break;
413 case GeneratorExp_kind:
414 expr_name = "generator expression";
415 break;
416 case Yield_kind:
417 expr_name = "yield expression";
418 break;
419 case ListComp_kind:
420 expr_name = "list comprehension";
421 break;
422 case Dict_kind:
423 case Num_kind:
424 case Str_kind:
425 expr_name = "literal";
426 break;
427 case Compare_kind:
428 expr_name = "comparison";
429 break;
430 case Repr_kind:
431 expr_name = "repr";
432 break;
433 case IfExp_kind:
434 expr_name = "conditional expression";
435 break;
436 default:
437 PyErr_Format(PyExc_SystemError,
438 "unexpected expression in assignment %d (line %d)",
439 e->kind, e->lineno);
440 return 0;
442 /* Check for error string set by switch */
443 if (expr_name) {
444 char buf[300];
445 PyOS_snprintf(buf, sizeof(buf),
446 "can't %s %s",
447 ctx == Store ? "assign to" : "delete",
448 expr_name);
449 return ast_error(n, buf);
452 /* If the LHS is a list or tuple, we need to set the assignment
453 context for all the contained elements.
455 if (s) {
456 int i;
458 for (i = 0; i < asdl_seq_LEN(s); i++) {
459 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
460 return 0;
463 return 1;
466 static operator_ty
467 ast_for_augassign(struct compiling *c, const node *n)
469 REQ(n, augassign);
470 n = CHILD(n, 0);
471 switch (STR(n)[0]) {
472 case '+':
473 return Add;
474 case '-':
475 return Sub;
476 case '/':
477 if (STR(n)[1] == '/')
478 return FloorDiv;
479 else
480 return Div;
481 case '%':
482 return Mod;
483 case '<':
484 return LShift;
485 case '>':
486 return RShift;
487 case '&':
488 return BitAnd;
489 case '^':
490 return BitXor;
491 case '|':
492 return BitOr;
493 case '*':
494 if (STR(n)[1] == '*')
495 return Pow;
496 else
497 return Mult;
498 default:
499 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
500 return (operator_ty)0;
504 static cmpop_ty
505 ast_for_comp_op(struct compiling *c, const node *n)
507 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
508 |'is' 'not'
510 REQ(n, comp_op);
511 if (NCH(n) == 1) {
512 n = CHILD(n, 0);
513 switch (TYPE(n)) {
514 case LESS:
515 return Lt;
516 case GREATER:
517 return Gt;
518 case EQEQUAL: /* == */
519 return Eq;
520 case LESSEQUAL:
521 return LtE;
522 case GREATEREQUAL:
523 return GtE;
524 case NOTEQUAL:
525 return NotEq;
526 case NAME:
527 if (strcmp(STR(n), "in") == 0)
528 return In;
529 if (strcmp(STR(n), "is") == 0)
530 return Is;
531 default:
532 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
533 STR(n));
534 return (cmpop_ty)0;
537 else if (NCH(n) == 2) {
538 /* handle "not in" and "is not" */
539 switch (TYPE(CHILD(n, 0))) {
540 case NAME:
541 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
542 return NotIn;
543 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
544 return IsNot;
545 default:
546 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
547 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
548 return (cmpop_ty)0;
551 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
552 NCH(n));
553 return (cmpop_ty)0;
556 static asdl_seq *
557 seq_for_testlist(struct compiling *c, const node *n)
559 /* testlist: test (',' test)* [','] */
560 asdl_seq *seq;
561 expr_ty expression;
562 int i;
563 assert(TYPE(n) == testlist ||
564 TYPE(n) == listmaker ||
565 TYPE(n) == testlist_gexp ||
566 TYPE(n) == testlist_safe ||
567 TYPE(n) == testlist1);
569 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
570 if (!seq)
571 return NULL;
573 for (i = 0; i < NCH(n); i += 2) {
574 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
576 expression = ast_for_expr(c, CHILD(n, i));
577 if (!expression)
578 return NULL;
580 assert(i / 2 < seq->size);
581 asdl_seq_SET(seq, i / 2, expression);
583 return seq;
586 static expr_ty
587 compiler_complex_args(struct compiling *c, const node *n)
589 int i, len = (NCH(n) + 1) / 2;
590 expr_ty result;
591 asdl_seq *args = asdl_seq_new(len, c->c_arena);
592 if (!args)
593 return NULL;
595 /* fpdef: NAME | '(' fplist ')'
596 fplist: fpdef (',' fpdef)* [',']
598 REQ(n, fplist);
599 for (i = 0; i < len; i++) {
600 const node *fpdef_node = CHILD(n, 2*i);
601 const node *child;
602 expr_ty arg;
603 set_name:
604 /* fpdef_node is either a NAME or an fplist */
605 child = CHILD(fpdef_node, 0);
606 if (TYPE(child) == NAME) {
607 if (!forbidden_check(c, n, STR(child)))
608 return NULL;
609 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
610 child->n_col_offset, c->c_arena);
612 else {
613 assert(TYPE(fpdef_node) == fpdef);
614 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
615 child = CHILD(fpdef_node, 1);
616 assert(TYPE(child) == fplist);
617 /* NCH == 1 means we have (x), we need to elide the extra parens */
618 if (NCH(child) == 1) {
619 fpdef_node = CHILD(child, 0);
620 assert(TYPE(fpdef_node) == fpdef);
621 goto set_name;
623 arg = compiler_complex_args(c, child);
625 asdl_seq_SET(args, i, arg);
628 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
629 if (!set_context(c, result, Store, n))
630 return NULL;
631 return result;
635 /* Create AST for argument list. */
637 static arguments_ty
638 ast_for_arguments(struct compiling *c, const node *n)
640 /* parameters: '(' [varargslist] ')'
641 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
642 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
644 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
645 asdl_seq *args, *defaults;
646 identifier vararg = NULL, kwarg = NULL;
647 node *ch;
649 if (TYPE(n) == parameters) {
650 if (NCH(n) == 2) /* () as argument list */
651 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
652 n = CHILD(n, 1);
654 REQ(n, varargslist);
656 /* first count the number of normal args & defaults */
657 for (i = 0; i < NCH(n); i++) {
658 ch = CHILD(n, i);
659 if (TYPE(ch) == fpdef)
660 n_args++;
661 if (TYPE(ch) == EQUAL)
662 n_defaults++;
664 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
665 if (!args && n_args)
666 return NULL; /* Don't need to goto error; no objects allocated */
667 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
668 if (!defaults && n_defaults)
669 return NULL; /* Don't need to goto error; no objects allocated */
671 /* fpdef: NAME | '(' fplist ')'
672 fplist: fpdef (',' fpdef)* [',']
674 i = 0;
675 j = 0; /* index for defaults */
676 k = 0; /* index for args */
677 while (i < NCH(n)) {
678 ch = CHILD(n, i);
679 switch (TYPE(ch)) {
680 case fpdef:
681 handle_fpdef:
682 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
683 anything other than EQUAL or a comma? */
684 /* XXX Should NCH(n) check be made a separate check? */
685 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
686 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
687 if (!expression)
688 goto error;
689 assert(defaults != NULL);
690 asdl_seq_SET(defaults, j++, expression);
691 i += 2;
692 found_default = 1;
694 else if (found_default) {
695 ast_error(n,
696 "non-default argument follows default argument");
697 goto error;
699 if (NCH(ch) == 3) {
700 ch = CHILD(ch, 1);
701 /* def foo((x)): is not complex, special case. */
702 if (NCH(ch) != 1) {
703 /* We have complex arguments, setup for unpacking. */
704 if (Py_Py3kWarningFlag && !ast_warn(c, ch,
705 "tuple parameter unpacking has been removed in 3.x"))
706 goto error;
707 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
708 if (!asdl_seq_GET(args, k-1))
709 goto error;
710 } else {
711 /* def foo((x)): setup for checking NAME below. */
712 /* Loop because there can be many parens and tuple
713 unpacking mixed in. */
714 ch = CHILD(ch, 0);
715 assert(TYPE(ch) == fpdef);
716 goto handle_fpdef;
719 if (TYPE(CHILD(ch, 0)) == NAME) {
720 expr_ty name;
721 if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
722 goto error;
723 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
724 Param, LINENO(ch), ch->n_col_offset,
725 c->c_arena);
726 if (!name)
727 goto error;
728 asdl_seq_SET(args, k++, name);
731 i += 2; /* the name and the comma */
732 break;
733 case STAR:
734 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
735 goto error;
736 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
737 i += 3;
738 break;
739 case DOUBLESTAR:
740 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
741 goto error;
742 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
743 i += 3;
744 break;
745 default:
746 PyErr_Format(PyExc_SystemError,
747 "unexpected node in varargslist: %d @ %d",
748 TYPE(ch), i);
749 goto error;
753 return arguments(args, vararg, kwarg, defaults, c->c_arena);
755 error:
756 Py_XDECREF(vararg);
757 Py_XDECREF(kwarg);
758 return NULL;
761 static expr_ty
762 ast_for_dotted_name(struct compiling *c, const node *n)
764 expr_ty e;
765 identifier id;
766 int lineno, col_offset;
767 int i;
769 REQ(n, dotted_name);
771 lineno = LINENO(n);
772 col_offset = n->n_col_offset;
774 id = NEW_IDENTIFIER(CHILD(n, 0));
775 if (!id)
776 return NULL;
777 e = Name(id, Load, lineno, col_offset, c->c_arena);
778 if (!e)
779 return NULL;
781 for (i = 2; i < NCH(n); i+=2) {
782 id = NEW_IDENTIFIER(CHILD(n, i));
783 if (!id)
784 return NULL;
785 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
786 if (!e)
787 return NULL;
790 return e;
793 static expr_ty
794 ast_for_decorator(struct compiling *c, const node *n)
796 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
797 expr_ty d = NULL;
798 expr_ty name_expr;
800 REQ(n, decorator);
801 REQ(CHILD(n, 0), AT);
802 REQ(RCHILD(n, -1), NEWLINE);
804 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
805 if (!name_expr)
806 return NULL;
808 if (NCH(n) == 3) { /* No arguments */
809 d = name_expr;
810 name_expr = NULL;
812 else if (NCH(n) == 5) { /* Call with no arguments */
813 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
814 n->n_col_offset, c->c_arena);
815 if (!d)
816 return NULL;
817 name_expr = NULL;
819 else {
820 d = ast_for_call(c, CHILD(n, 3), name_expr);
821 if (!d)
822 return NULL;
823 name_expr = NULL;
826 return d;
829 static asdl_seq*
830 ast_for_decorators(struct compiling *c, const node *n)
832 asdl_seq* decorator_seq;
833 expr_ty d;
834 int i;
836 REQ(n, decorators);
837 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
838 if (!decorator_seq)
839 return NULL;
841 for (i = 0; i < NCH(n); i++) {
842 d = ast_for_decorator(c, CHILD(n, i));
843 if (!d)
844 return NULL;
845 asdl_seq_SET(decorator_seq, i, d);
847 return decorator_seq;
850 static stmt_ty
851 ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
853 /* funcdef: 'def' NAME parameters ':' suite */
854 identifier name;
855 arguments_ty args;
856 asdl_seq *body;
857 int name_i = 1;
859 REQ(n, funcdef);
861 name = NEW_IDENTIFIER(CHILD(n, name_i));
862 if (!name)
863 return NULL;
864 else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
865 return NULL;
866 args = ast_for_arguments(c, CHILD(n, name_i + 1));
867 if (!args)
868 return NULL;
869 body = ast_for_suite(c, CHILD(n, name_i + 3));
870 if (!body)
871 return NULL;
873 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
874 n->n_col_offset, c->c_arena);
877 static stmt_ty
878 ast_for_decorated(struct compiling *c, const node *n)
880 /* decorated: decorators (classdef | funcdef) */
881 stmt_ty thing = NULL;
882 asdl_seq *decorator_seq = NULL;
884 REQ(n, decorated);
886 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
887 if (!decorator_seq)
888 return NULL;
890 assert(TYPE(CHILD(n, 1)) == funcdef ||
891 TYPE(CHILD(n, 1)) == classdef);
893 if (TYPE(CHILD(n, 1)) == funcdef) {
894 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
895 } else if (TYPE(CHILD(n, 1)) == classdef) {
896 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
898 /* we count the decorators in when talking about the class' or
899 function's line number */
900 if (thing) {
901 thing->lineno = LINENO(n);
902 thing->col_offset = n->n_col_offset;
904 return thing;
907 static expr_ty
908 ast_for_lambdef(struct compiling *c, const node *n)
910 /* lambdef: 'lambda' [varargslist] ':' test */
911 arguments_ty args;
912 expr_ty expression;
914 if (NCH(n) == 3) {
915 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
916 if (!args)
917 return NULL;
918 expression = ast_for_expr(c, CHILD(n, 2));
919 if (!expression)
920 return NULL;
922 else {
923 args = ast_for_arguments(c, CHILD(n, 1));
924 if (!args)
925 return NULL;
926 expression = ast_for_expr(c, CHILD(n, 3));
927 if (!expression)
928 return NULL;
931 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
934 static expr_ty
935 ast_for_ifexpr(struct compiling *c, const node *n)
937 /* test: or_test 'if' or_test 'else' test */
938 expr_ty expression, body, orelse;
940 assert(NCH(n) == 5);
941 body = ast_for_expr(c, CHILD(n, 0));
942 if (!body)
943 return NULL;
944 expression = ast_for_expr(c, CHILD(n, 2));
945 if (!expression)
946 return NULL;
947 orelse = ast_for_expr(c, CHILD(n, 4));
948 if (!orelse)
949 return NULL;
950 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
951 c->c_arena);
954 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
955 so there is only a single version. Possibly for loops can also re-use
956 the code.
959 /* Count the number of 'for' loop in a list comprehension.
961 Helper for ast_for_listcomp().
964 static int
965 count_list_fors(struct compiling *c, const node *n)
967 int n_fors = 0;
968 node *ch = CHILD(n, 1);
970 count_list_for:
971 n_fors++;
972 REQ(ch, list_for);
973 if (NCH(ch) == 5)
974 ch = CHILD(ch, 4);
975 else
976 return n_fors;
977 count_list_iter:
978 REQ(ch, list_iter);
979 ch = CHILD(ch, 0);
980 if (TYPE(ch) == list_for)
981 goto count_list_for;
982 else if (TYPE(ch) == list_if) {
983 if (NCH(ch) == 3) {
984 ch = CHILD(ch, 2);
985 goto count_list_iter;
987 else
988 return n_fors;
991 /* Should never be reached */
992 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
993 return -1;
996 /* Count the number of 'if' statements in a list comprehension.
998 Helper for ast_for_listcomp().
1001 static int
1002 count_list_ifs(struct compiling *c, const node *n)
1004 int n_ifs = 0;
1006 count_list_iter:
1007 REQ(n, list_iter);
1008 if (TYPE(CHILD(n, 0)) == list_for)
1009 return n_ifs;
1010 n = CHILD(n, 0);
1011 REQ(n, list_if);
1012 n_ifs++;
1013 if (NCH(n) == 2)
1014 return n_ifs;
1015 n = CHILD(n, 2);
1016 goto count_list_iter;
1019 static expr_ty
1020 ast_for_listcomp(struct compiling *c, const node *n)
1022 /* listmaker: test ( list_for | (',' test)* [','] )
1023 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1024 list_iter: list_for | list_if
1025 list_if: 'if' test [list_iter]
1026 testlist_safe: test [(',' test)+ [',']]
1028 expr_ty elt;
1029 asdl_seq *listcomps;
1030 int i, n_fors;
1031 node *ch;
1033 REQ(n, listmaker);
1034 assert(NCH(n) > 1);
1036 elt = ast_for_expr(c, CHILD(n, 0));
1037 if (!elt)
1038 return NULL;
1040 n_fors = count_list_fors(c, n);
1041 if (n_fors == -1)
1042 return NULL;
1044 listcomps = asdl_seq_new(n_fors, c->c_arena);
1045 if (!listcomps)
1046 return NULL;
1048 ch = CHILD(n, 1);
1049 for (i = 0; i < n_fors; i++) {
1050 comprehension_ty lc;
1051 asdl_seq *t;
1052 expr_ty expression;
1053 node *for_ch;
1055 REQ(ch, list_for);
1057 for_ch = CHILD(ch, 1);
1058 t = ast_for_exprlist(c, for_ch, Store);
1059 if (!t)
1060 return NULL;
1061 expression = ast_for_testlist(c, CHILD(ch, 3));
1062 if (!expression)
1063 return NULL;
1065 /* Check the # of children rather than the length of t, since
1066 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1068 if (NCH(for_ch) == 1)
1069 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1070 c->c_arena);
1071 else
1072 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1073 c->c_arena),
1074 expression, NULL, c->c_arena);
1075 if (!lc)
1076 return NULL;
1078 if (NCH(ch) == 5) {
1079 int j, n_ifs;
1080 asdl_seq *ifs;
1081 expr_ty list_for_expr;
1083 ch = CHILD(ch, 4);
1084 n_ifs = count_list_ifs(c, ch);
1085 if (n_ifs == -1)
1086 return NULL;
1088 ifs = asdl_seq_new(n_ifs, c->c_arena);
1089 if (!ifs)
1090 return NULL;
1092 for (j = 0; j < n_ifs; j++) {
1093 REQ(ch, list_iter);
1094 ch = CHILD(ch, 0);
1095 REQ(ch, list_if);
1097 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1098 if (!list_for_expr)
1099 return NULL;
1101 asdl_seq_SET(ifs, j, list_for_expr);
1102 if (NCH(ch) == 3)
1103 ch = CHILD(ch, 2);
1105 /* on exit, must guarantee that ch is a list_for */
1106 if (TYPE(ch) == list_iter)
1107 ch = CHILD(ch, 0);
1108 lc->ifs = ifs;
1110 asdl_seq_SET(listcomps, i, lc);
1113 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
1116 /* Count the number of 'for' loops in a generator expression.
1118 Helper for ast_for_genexp().
1121 static int
1122 count_gen_fors(struct compiling *c, const node *n)
1124 int n_fors = 0;
1125 node *ch = CHILD(n, 1);
1127 count_gen_for:
1128 n_fors++;
1129 REQ(ch, gen_for);
1130 if (NCH(ch) == 5)
1131 ch = CHILD(ch, 4);
1132 else
1133 return n_fors;
1134 count_gen_iter:
1135 REQ(ch, gen_iter);
1136 ch = CHILD(ch, 0);
1137 if (TYPE(ch) == gen_for)
1138 goto count_gen_for;
1139 else if (TYPE(ch) == gen_if) {
1140 if (NCH(ch) == 3) {
1141 ch = CHILD(ch, 2);
1142 goto count_gen_iter;
1144 else
1145 return n_fors;
1148 /* Should never be reached */
1149 PyErr_SetString(PyExc_SystemError,
1150 "logic error in count_gen_fors");
1151 return -1;
1154 /* Count the number of 'if' statements in a generator expression.
1156 Helper for ast_for_genexp().
1159 static int
1160 count_gen_ifs(struct compiling *c, const node *n)
1162 int n_ifs = 0;
1164 while (1) {
1165 REQ(n, gen_iter);
1166 if (TYPE(CHILD(n, 0)) == gen_for)
1167 return n_ifs;
1168 n = CHILD(n, 0);
1169 REQ(n, gen_if);
1170 n_ifs++;
1171 if (NCH(n) == 2)
1172 return n_ifs;
1173 n = CHILD(n, 2);
1177 /* TODO(jhylton): Combine with list comprehension code? */
1178 static expr_ty
1179 ast_for_genexp(struct compiling *c, const node *n)
1181 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1182 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1183 expr_ty elt;
1184 asdl_seq *genexps;
1185 int i, n_fors;
1186 node *ch;
1188 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1189 assert(NCH(n) > 1);
1191 elt = ast_for_expr(c, CHILD(n, 0));
1192 if (!elt)
1193 return NULL;
1195 n_fors = count_gen_fors(c, n);
1196 if (n_fors == -1)
1197 return NULL;
1199 genexps = asdl_seq_new(n_fors, c->c_arena);
1200 if (!genexps)
1201 return NULL;
1203 ch = CHILD(n, 1);
1204 for (i = 0; i < n_fors; i++) {
1205 comprehension_ty ge;
1206 asdl_seq *t;
1207 expr_ty expression;
1208 node *for_ch;
1210 REQ(ch, gen_for);
1212 for_ch = CHILD(ch, 1);
1213 t = ast_for_exprlist(c, for_ch, Store);
1214 if (!t)
1215 return NULL;
1216 expression = ast_for_expr(c, CHILD(ch, 3));
1217 if (!expression)
1218 return NULL;
1220 /* Check the # of children rather than the length of t, since
1221 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1222 if (NCH(for_ch) == 1)
1223 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1224 NULL, c->c_arena);
1225 else
1226 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1227 c->c_arena),
1228 expression, NULL, c->c_arena);
1230 if (!ge)
1231 return NULL;
1233 if (NCH(ch) == 5) {
1234 int j, n_ifs;
1235 asdl_seq *ifs;
1237 ch = CHILD(ch, 4);
1238 n_ifs = count_gen_ifs(c, ch);
1239 if (n_ifs == -1)
1240 return NULL;
1242 ifs = asdl_seq_new(n_ifs, c->c_arena);
1243 if (!ifs)
1244 return NULL;
1246 for (j = 0; j < n_ifs; j++) {
1247 REQ(ch, gen_iter);
1248 ch = CHILD(ch, 0);
1249 REQ(ch, gen_if);
1251 expression = ast_for_expr(c, CHILD(ch, 1));
1252 if (!expression)
1253 return NULL;
1254 asdl_seq_SET(ifs, j, expression);
1255 if (NCH(ch) == 3)
1256 ch = CHILD(ch, 2);
1258 /* on exit, must guarantee that ch is a gen_for */
1259 if (TYPE(ch) == gen_iter)
1260 ch = CHILD(ch, 0);
1261 ge->ifs = ifs;
1263 asdl_seq_SET(genexps, i, ge);
1266 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
1269 static expr_ty
1270 ast_for_atom(struct compiling *c, const node *n)
1272 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1273 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1275 node *ch = CHILD(n, 0);
1277 switch (TYPE(ch)) {
1278 case NAME:
1279 /* All names start in Load context, but may later be
1280 changed. */
1281 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1282 c->c_arena);
1283 case STRING: {
1284 PyObject *str = parsestrplus(c, n);
1285 if (!str) {
1286 #ifdef Py_USING_UNICODE
1287 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1288 PyObject *type, *value, *tback, *errstr;
1289 PyErr_Fetch(&type, &value, &tback);
1290 errstr = ((PyUnicodeErrorObject *)value)->reason;
1291 if (errstr) {
1292 char *s = "";
1293 char buf[128];
1294 s = PyString_AsString(errstr);
1295 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1296 ast_error(n, buf);
1297 } else {
1298 ast_error(n, "(unicode error) unknown error");
1300 Py_DECREF(type);
1301 Py_DECREF(value);
1302 Py_XDECREF(tback);
1304 #endif
1305 return NULL;
1307 PyArena_AddPyObject(c->c_arena, str);
1308 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
1310 case NUMBER: {
1311 PyObject *pynum = parsenumber(c, STR(ch));
1312 if (!pynum)
1313 return NULL;
1315 PyArena_AddPyObject(c->c_arena, pynum);
1316 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
1318 case LPAR: /* some parenthesized expressions */
1319 ch = CHILD(n, 1);
1321 if (TYPE(ch) == RPAR)
1322 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1324 if (TYPE(ch) == yield_expr)
1325 return ast_for_expr(c, ch);
1327 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1328 return ast_for_genexp(c, ch);
1330 return ast_for_testlist_gexp(c, ch);
1331 case LSQB: /* list (or list comprehension) */
1332 ch = CHILD(n, 1);
1334 if (TYPE(ch) == RSQB)
1335 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1337 REQ(ch, listmaker);
1338 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1339 asdl_seq *elts = seq_for_testlist(c, ch);
1340 if (!elts)
1341 return NULL;
1343 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1345 else
1346 return ast_for_listcomp(c, ch);
1347 case LBRACE: {
1348 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1349 int i, size;
1350 asdl_seq *keys, *values;
1352 ch = CHILD(n, 1);
1353 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1354 keys = asdl_seq_new(size, c->c_arena);
1355 if (!keys)
1356 return NULL;
1358 values = asdl_seq_new(size, c->c_arena);
1359 if (!values)
1360 return NULL;
1362 for (i = 0; i < NCH(ch); i += 4) {
1363 expr_ty expression;
1365 expression = ast_for_expr(c, CHILD(ch, i));
1366 if (!expression)
1367 return NULL;
1369 asdl_seq_SET(keys, i / 4, expression);
1371 expression = ast_for_expr(c, CHILD(ch, i + 2));
1372 if (!expression)
1373 return NULL;
1375 asdl_seq_SET(values, i / 4, expression);
1377 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1379 case BACKQUOTE: { /* repr */
1380 expr_ty expression;
1381 if (Py_Py3kWarningFlag &&
1382 !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
1383 return NULL;
1384 expression = ast_for_testlist(c, CHILD(n, 1));
1385 if (!expression)
1386 return NULL;
1388 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
1390 default:
1391 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1392 return NULL;
1396 static slice_ty
1397 ast_for_slice(struct compiling *c, const node *n)
1399 node *ch;
1400 expr_ty lower = NULL, upper = NULL, step = NULL;
1402 REQ(n, subscript);
1405 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1406 sliceop: ':' [test]
1408 ch = CHILD(n, 0);
1409 if (TYPE(ch) == DOT)
1410 return Ellipsis(c->c_arena);
1412 if (NCH(n) == 1 && TYPE(ch) == test) {
1413 /* 'step' variable hold no significance in terms of being used over
1414 other vars */
1415 step = ast_for_expr(c, ch);
1416 if (!step)
1417 return NULL;
1419 return Index(step, c->c_arena);
1422 if (TYPE(ch) == test) {
1423 lower = ast_for_expr(c, ch);
1424 if (!lower)
1425 return NULL;
1428 /* If there's an upper bound it's in the second or third position. */
1429 if (TYPE(ch) == COLON) {
1430 if (NCH(n) > 1) {
1431 node *n2 = CHILD(n, 1);
1433 if (TYPE(n2) == test) {
1434 upper = ast_for_expr(c, n2);
1435 if (!upper)
1436 return NULL;
1439 } else if (NCH(n) > 2) {
1440 node *n2 = CHILD(n, 2);
1442 if (TYPE(n2) == test) {
1443 upper = ast_for_expr(c, n2);
1444 if (!upper)
1445 return NULL;
1449 ch = CHILD(n, NCH(n) - 1);
1450 if (TYPE(ch) == sliceop) {
1451 if (NCH(ch) == 1) {
1452 /* No expression, so step is None */
1453 ch = CHILD(ch, 0);
1454 step = Name(new_identifier("None", c->c_arena), Load,
1455 LINENO(ch), ch->n_col_offset, c->c_arena);
1456 if (!step)
1457 return NULL;
1458 } else {
1459 ch = CHILD(ch, 1);
1460 if (TYPE(ch) == test) {
1461 step = ast_for_expr(c, ch);
1462 if (!step)
1463 return NULL;
1468 return Slice(lower, upper, step, c->c_arena);
1471 static expr_ty
1472 ast_for_binop(struct compiling *c, const node *n)
1474 /* Must account for a sequence of expressions.
1475 How should A op B op C by represented?
1476 BinOp(BinOp(A, op, B), op, C).
1479 int i, nops;
1480 expr_ty expr1, expr2, result;
1481 operator_ty newoperator;
1483 expr1 = ast_for_expr(c, CHILD(n, 0));
1484 if (!expr1)
1485 return NULL;
1487 expr2 = ast_for_expr(c, CHILD(n, 2));
1488 if (!expr2)
1489 return NULL;
1491 newoperator = get_operator(CHILD(n, 1));
1492 if (!newoperator)
1493 return NULL;
1495 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1496 c->c_arena);
1497 if (!result)
1498 return NULL;
1500 nops = (NCH(n) - 1) / 2;
1501 for (i = 1; i < nops; i++) {
1502 expr_ty tmp_result, tmp;
1503 const node* next_oper = CHILD(n, i * 2 + 1);
1505 newoperator = get_operator(next_oper);
1506 if (!newoperator)
1507 return NULL;
1509 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1510 if (!tmp)
1511 return NULL;
1513 tmp_result = BinOp(result, newoperator, tmp,
1514 LINENO(next_oper), next_oper->n_col_offset,
1515 c->c_arena);
1516 if (!tmp_result)
1517 return NULL;
1518 result = tmp_result;
1520 return result;
1523 static expr_ty
1524 ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1526 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1527 subscriptlist: subscript (',' subscript)* [',']
1528 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1530 REQ(n, trailer);
1531 if (TYPE(CHILD(n, 0)) == LPAR) {
1532 if (NCH(n) == 2)
1533 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1534 n->n_col_offset, c->c_arena);
1535 else
1536 return ast_for_call(c, CHILD(n, 1), left_expr);
1538 else if (TYPE(CHILD(n, 0)) == DOT ) {
1539 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1540 LINENO(n), n->n_col_offset, c->c_arena);
1542 else {
1543 REQ(CHILD(n, 0), LSQB);
1544 REQ(CHILD(n, 2), RSQB);
1545 n = CHILD(n, 1);
1546 if (NCH(n) == 1) {
1547 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1548 if (!slc)
1549 return NULL;
1550 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1551 c->c_arena);
1553 else {
1554 /* The grammar is ambiguous here. The ambiguity is resolved
1555 by treating the sequence as a tuple literal if there are
1556 no slice features.
1558 int j;
1559 slice_ty slc;
1560 expr_ty e;
1561 bool simple = true;
1562 asdl_seq *slices, *elts;
1563 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1564 if (!slices)
1565 return NULL;
1566 for (j = 0; j < NCH(n); j += 2) {
1567 slc = ast_for_slice(c, CHILD(n, j));
1568 if (!slc)
1569 return NULL;
1570 if (slc->kind != Index_kind)
1571 simple = false;
1572 asdl_seq_SET(slices, j / 2, slc);
1574 if (!simple) {
1575 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1576 Load, LINENO(n), n->n_col_offset, c->c_arena);
1578 /* extract Index values and put them in a Tuple */
1579 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1580 if (!elts)
1581 return NULL;
1582 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1583 slc = (slice_ty)asdl_seq_GET(slices, j);
1584 assert(slc->kind == Index_kind && slc->v.Index.value);
1585 asdl_seq_SET(elts, j, slc->v.Index.value);
1587 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1588 if (!e)
1589 return NULL;
1590 return Subscript(left_expr, Index(e, c->c_arena),
1591 Load, LINENO(n), n->n_col_offset, c->c_arena);
1596 static expr_ty
1597 ast_for_factor(struct compiling *c, const node *n)
1599 node *pfactor, *ppower, *patom, *pnum;
1600 expr_ty expression;
1602 /* If the unary - operator is applied to a constant, don't generate
1603 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1604 constant. The peephole optimizer already does something like
1605 this but it doesn't handle the case where the constant is
1606 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1607 PyLongObject.
1609 if (TYPE(CHILD(n, 0)) == MINUS &&
1610 NCH(n) == 2 &&
1611 TYPE((pfactor = CHILD(n, 1))) == factor &&
1612 NCH(pfactor) == 1 &&
1613 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1614 NCH(ppower) == 1 &&
1615 TYPE((patom = CHILD(ppower, 0))) == atom &&
1616 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1617 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1618 if (s == NULL)
1619 return NULL;
1620 s[0] = '-';
1621 strcpy(s + 1, STR(pnum));
1622 PyObject_FREE(STR(pnum));
1623 STR(pnum) = s;
1624 return ast_for_atom(c, patom);
1627 expression = ast_for_expr(c, CHILD(n, 1));
1628 if (!expression)
1629 return NULL;
1631 switch (TYPE(CHILD(n, 0))) {
1632 case PLUS:
1633 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1634 c->c_arena);
1635 case MINUS:
1636 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1637 c->c_arena);
1638 case TILDE:
1639 return UnaryOp(Invert, expression, LINENO(n),
1640 n->n_col_offset, c->c_arena);
1642 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1643 TYPE(CHILD(n, 0)));
1644 return NULL;
1647 static expr_ty
1648 ast_for_power(struct compiling *c, const node *n)
1650 /* power: atom trailer* ('**' factor)*
1652 int i;
1653 expr_ty e, tmp;
1654 REQ(n, power);
1655 e = ast_for_atom(c, CHILD(n, 0));
1656 if (!e)
1657 return NULL;
1658 if (NCH(n) == 1)
1659 return e;
1660 for (i = 1; i < NCH(n); i++) {
1661 node *ch = CHILD(n, i);
1662 if (TYPE(ch) != trailer)
1663 break;
1664 tmp = ast_for_trailer(c, ch, e);
1665 if (!tmp)
1666 return NULL;
1667 tmp->lineno = e->lineno;
1668 tmp->col_offset = e->col_offset;
1669 e = tmp;
1671 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1672 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1673 if (!f)
1674 return NULL;
1675 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1676 if (!tmp)
1677 return NULL;
1678 e = tmp;
1680 return e;
1683 /* Do not name a variable 'expr'! Will cause a compile error.
1686 static expr_ty
1687 ast_for_expr(struct compiling *c, const node *n)
1689 /* handle the full range of simple expressions
1690 test: or_test ['if' or_test 'else' test] | lambdef
1691 or_test: and_test ('or' and_test)*
1692 and_test: not_test ('and' not_test)*
1693 not_test: 'not' not_test | comparison
1694 comparison: expr (comp_op expr)*
1695 expr: xor_expr ('|' xor_expr)*
1696 xor_expr: and_expr ('^' and_expr)*
1697 and_expr: shift_expr ('&' shift_expr)*
1698 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1699 arith_expr: term (('+'|'-') term)*
1700 term: factor (('*'|'/'|'%'|'//') factor)*
1701 factor: ('+'|'-'|'~') factor | power
1702 power: atom trailer* ('**' factor)*
1704 As well as modified versions that exist for backward compatibility,
1705 to explicitly allow:
1706 [ x for x in lambda: 0, lambda: 1 ]
1707 (which would be ambiguous without these extra rules)
1709 old_test: or_test | old_lambdef
1710 old_lambdef: 'lambda' [vararglist] ':' old_test
1714 asdl_seq *seq;
1715 int i;
1717 loop:
1718 switch (TYPE(n)) {
1719 case test:
1720 case old_test:
1721 if (TYPE(CHILD(n, 0)) == lambdef ||
1722 TYPE(CHILD(n, 0)) == old_lambdef)
1723 return ast_for_lambdef(c, CHILD(n, 0));
1724 else if (NCH(n) > 1)
1725 return ast_for_ifexpr(c, n);
1726 /* Fallthrough */
1727 case or_test:
1728 case and_test:
1729 if (NCH(n) == 1) {
1730 n = CHILD(n, 0);
1731 goto loop;
1733 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1734 if (!seq)
1735 return NULL;
1736 for (i = 0; i < NCH(n); i += 2) {
1737 expr_ty e = ast_for_expr(c, CHILD(n, i));
1738 if (!e)
1739 return NULL;
1740 asdl_seq_SET(seq, i / 2, e);
1742 if (!strcmp(STR(CHILD(n, 1)), "and"))
1743 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1744 c->c_arena);
1745 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1746 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1747 case not_test:
1748 if (NCH(n) == 1) {
1749 n = CHILD(n, 0);
1750 goto loop;
1752 else {
1753 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1754 if (!expression)
1755 return NULL;
1757 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1758 c->c_arena);
1760 case comparison:
1761 if (NCH(n) == 1) {
1762 n = CHILD(n, 0);
1763 goto loop;
1765 else {
1766 expr_ty expression;
1767 asdl_int_seq *ops;
1768 asdl_seq *cmps;
1769 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1770 if (!ops)
1771 return NULL;
1772 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1773 if (!cmps) {
1774 return NULL;
1776 for (i = 1; i < NCH(n); i += 2) {
1777 cmpop_ty newoperator;
1779 newoperator = ast_for_comp_op(c, CHILD(n, i));
1780 if (!newoperator) {
1781 return NULL;
1784 expression = ast_for_expr(c, CHILD(n, i + 1));
1785 if (!expression) {
1786 return NULL;
1789 asdl_seq_SET(ops, i / 2, newoperator);
1790 asdl_seq_SET(cmps, i / 2, expression);
1792 expression = ast_for_expr(c, CHILD(n, 0));
1793 if (!expression) {
1794 return NULL;
1797 return Compare(expression, ops, cmps, LINENO(n),
1798 n->n_col_offset, c->c_arena);
1800 break;
1802 /* The next five cases all handle BinOps. The main body of code
1803 is the same in each case, but the switch turned inside out to
1804 reuse the code for each type of operator.
1806 case expr:
1807 case xor_expr:
1808 case and_expr:
1809 case shift_expr:
1810 case arith_expr:
1811 case term:
1812 if (NCH(n) == 1) {
1813 n = CHILD(n, 0);
1814 goto loop;
1816 return ast_for_binop(c, n);
1817 case yield_expr: {
1818 expr_ty exp = NULL;
1819 if (NCH(n) == 2) {
1820 exp = ast_for_testlist(c, CHILD(n, 1));
1821 if (!exp)
1822 return NULL;
1824 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1826 case factor:
1827 if (NCH(n) == 1) {
1828 n = CHILD(n, 0);
1829 goto loop;
1831 return ast_for_factor(c, n);
1832 case power:
1833 return ast_for_power(c, n);
1834 default:
1835 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1836 return NULL;
1838 /* should never get here unless if error is set */
1839 return NULL;
1842 static expr_ty
1843 ast_for_call(struct compiling *c, const node *n, expr_ty func)
1846 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1847 | '**' test)
1848 argument: [test '='] test [gen_for] # Really [keyword '='] test
1851 int i, nargs, nkeywords, ngens;
1852 asdl_seq *args;
1853 asdl_seq *keywords;
1854 expr_ty vararg = NULL, kwarg = NULL;
1856 REQ(n, arglist);
1858 nargs = 0;
1859 nkeywords = 0;
1860 ngens = 0;
1861 for (i = 0; i < NCH(n); i++) {
1862 node *ch = CHILD(n, i);
1863 if (TYPE(ch) == argument) {
1864 if (NCH(ch) == 1)
1865 nargs++;
1866 else if (TYPE(CHILD(ch, 1)) == gen_for)
1867 ngens++;
1868 else
1869 nkeywords++;
1872 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1873 ast_error(n, "Generator expression must be parenthesized "
1874 "if not sole argument");
1875 return NULL;
1878 if (nargs + nkeywords + ngens > 255) {
1879 ast_error(n, "more than 255 arguments");
1880 return NULL;
1883 args = asdl_seq_new(nargs + ngens, c->c_arena);
1884 if (!args)
1885 return NULL;
1886 keywords = asdl_seq_new(nkeywords, c->c_arena);
1887 if (!keywords)
1888 return NULL;
1889 nargs = 0;
1890 nkeywords = 0;
1891 for (i = 0; i < NCH(n); i++) {
1892 node *ch = CHILD(n, i);
1893 if (TYPE(ch) == argument) {
1894 expr_ty e;
1895 if (NCH(ch) == 1) {
1896 if (nkeywords) {
1897 ast_error(CHILD(ch, 0),
1898 "non-keyword arg after keyword arg");
1899 return NULL;
1901 e = ast_for_expr(c, CHILD(ch, 0));
1902 if (!e)
1903 return NULL;
1904 asdl_seq_SET(args, nargs++, e);
1906 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1907 e = ast_for_genexp(c, ch);
1908 if (!e)
1909 return NULL;
1910 asdl_seq_SET(args, nargs++, e);
1912 else {
1913 keyword_ty kw;
1914 identifier key;
1916 /* CHILD(ch, 0) is test, but must be an identifier? */
1917 e = ast_for_expr(c, CHILD(ch, 0));
1918 if (!e)
1919 return NULL;
1920 /* f(lambda x: x[0] = 3) ends up getting parsed with
1921 * LHS test = lambda x: x[0], and RHS test = 3.
1922 * SF bug 132313 points out that complaining about a keyword
1923 * then is very confusing.
1925 if (e->kind == Lambda_kind) {
1926 ast_error(CHILD(ch, 0),
1927 "lambda cannot contain assignment");
1928 return NULL;
1929 } else if (e->kind != Name_kind) {
1930 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1931 return NULL;
1933 key = e->v.Name.id;
1934 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
1935 return NULL;
1936 e = ast_for_expr(c, CHILD(ch, 2));
1937 if (!e)
1938 return NULL;
1939 kw = keyword(key, e, c->c_arena);
1940 if (!kw)
1941 return NULL;
1942 asdl_seq_SET(keywords, nkeywords++, kw);
1945 else if (TYPE(ch) == STAR) {
1946 vararg = ast_for_expr(c, CHILD(n, i+1));
1947 if (!vararg)
1948 return NULL;
1949 i++;
1951 else if (TYPE(ch) == DOUBLESTAR) {
1952 kwarg = ast_for_expr(c, CHILD(n, i+1));
1953 if (!kwarg)
1954 return NULL;
1955 i++;
1959 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1960 func->col_offset, c->c_arena);
1963 static expr_ty
1964 ast_for_testlist(struct compiling *c, const node* n)
1966 /* testlist_gexp: test (',' test)* [','] */
1967 /* testlist: test (',' test)* [','] */
1968 /* testlist_safe: test (',' test)+ [','] */
1969 /* testlist1: test (',' test)* */
1970 assert(NCH(n) > 0);
1971 if (TYPE(n) == testlist_gexp) {
1972 if (NCH(n) > 1)
1973 assert(TYPE(CHILD(n, 1)) != gen_for);
1975 else {
1976 assert(TYPE(n) == testlist ||
1977 TYPE(n) == testlist_safe ||
1978 TYPE(n) == testlist1);
1980 if (NCH(n) == 1)
1981 return ast_for_expr(c, CHILD(n, 0));
1982 else {
1983 asdl_seq *tmp = seq_for_testlist(c, n);
1984 if (!tmp)
1985 return NULL;
1986 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
1990 static expr_ty
1991 ast_for_testlist_gexp(struct compiling *c, const node* n)
1993 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1994 /* argument: test [ gen_for ] */
1995 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
1996 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
1997 return ast_for_genexp(c, n);
1998 return ast_for_testlist(c, n);
2001 /* like ast_for_testlist() but returns a sequence */
2002 static asdl_seq*
2003 ast_for_class_bases(struct compiling *c, const node* n)
2005 /* testlist: test (',' test)* [','] */
2006 assert(NCH(n) > 0);
2007 REQ(n, testlist);
2008 if (NCH(n) == 1) {
2009 expr_ty base;
2010 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2011 if (!bases)
2012 return NULL;
2013 base = ast_for_expr(c, CHILD(n, 0));
2014 if (!base)
2015 return NULL;
2016 asdl_seq_SET(bases, 0, base);
2017 return bases;
2020 return seq_for_testlist(c, n);
2023 static stmt_ty
2024 ast_for_expr_stmt(struct compiling *c, const node *n)
2026 REQ(n, expr_stmt);
2027 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2028 | ('=' (yield_expr|testlist))*)
2029 testlist: test (',' test)* [',']
2030 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2031 | '<<=' | '>>=' | '**=' | '//='
2032 test: ... here starts the operator precendence dance
2035 if (NCH(n) == 1) {
2036 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2037 if (!e)
2038 return NULL;
2040 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
2042 else if (TYPE(CHILD(n, 1)) == augassign) {
2043 expr_ty expr1, expr2;
2044 operator_ty newoperator;
2045 node *ch = CHILD(n, 0);
2047 expr1 = ast_for_testlist(c, ch);
2048 if (!expr1)
2049 return NULL;
2050 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2051 switch (expr1->kind) {
2052 case GeneratorExp_kind:
2053 ast_error(ch, "augmented assignment to generator "
2054 "expression not possible");
2055 return NULL;
2056 case Yield_kind:
2057 ast_error(ch, "augmented assignment to yield "
2058 "expression not possible");
2059 return NULL;
2060 case Name_kind: {
2061 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
2062 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
2063 !forbidden_check(c, ch, var_name))
2064 return NULL;
2065 break;
2067 case Attribute_kind:
2068 case Subscript_kind:
2069 break;
2070 default:
2071 ast_error(ch, "illegal expression for augmented "
2072 "assignment");
2073 return NULL;
2075 if(!set_context(c, expr1, Store, ch))
2076 return NULL;
2078 ch = CHILD(n, 2);
2079 if (TYPE(ch) == testlist)
2080 expr2 = ast_for_testlist(c, ch);
2081 else
2082 expr2 = ast_for_expr(c, ch);
2083 if (!expr2)
2084 return NULL;
2086 newoperator = ast_for_augassign(c, CHILD(n, 1));
2087 if (!newoperator)
2088 return NULL;
2090 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2091 c->c_arena);
2093 else {
2094 int i;
2095 asdl_seq *targets;
2096 node *value;
2097 expr_ty expression;
2099 /* a normal assignment */
2100 REQ(CHILD(n, 1), EQUAL);
2101 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2102 if (!targets)
2103 return NULL;
2104 for (i = 0; i < NCH(n) - 2; i += 2) {
2105 expr_ty e;
2106 node *ch = CHILD(n, i);
2107 if (TYPE(ch) == yield_expr) {
2108 ast_error(ch, "assignment to yield expression not possible");
2109 return NULL;
2111 e = ast_for_testlist(c, ch);
2113 /* set context to assign */
2114 if (!e)
2115 return NULL;
2117 if (!set_context(c, e, Store, CHILD(n, i)))
2118 return NULL;
2120 asdl_seq_SET(targets, i / 2, e);
2122 value = CHILD(n, NCH(n) - 1);
2123 if (TYPE(value) == testlist)
2124 expression = ast_for_testlist(c, value);
2125 else
2126 expression = ast_for_expr(c, value);
2127 if (!expression)
2128 return NULL;
2129 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2130 c->c_arena);
2134 static stmt_ty
2135 ast_for_print_stmt(struct compiling *c, const node *n)
2137 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2138 | '>>' test [ (',' test)+ [','] ] )
2140 expr_ty dest = NULL, expression;
2141 asdl_seq *seq;
2142 bool nl;
2143 int i, j, start = 1;
2145 REQ(n, print_stmt);
2146 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2147 dest = ast_for_expr(c, CHILD(n, 2));
2148 if (!dest)
2149 return NULL;
2150 start = 4;
2152 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
2153 if (!seq)
2154 return NULL;
2155 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
2156 expression = ast_for_expr(c, CHILD(n, i));
2157 if (!expression)
2158 return NULL;
2159 asdl_seq_SET(seq, j, expression);
2161 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
2162 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
2165 static asdl_seq *
2166 ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
2168 asdl_seq *seq;
2169 int i;
2170 expr_ty e;
2172 REQ(n, exprlist);
2174 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2175 if (!seq)
2176 return NULL;
2177 for (i = 0; i < NCH(n); i += 2) {
2178 e = ast_for_expr(c, CHILD(n, i));
2179 if (!e)
2180 return NULL;
2181 asdl_seq_SET(seq, i / 2, e);
2182 if (context && !set_context(c, e, context, CHILD(n, i)))
2183 return NULL;
2185 return seq;
2188 static stmt_ty
2189 ast_for_del_stmt(struct compiling *c, const node *n)
2191 asdl_seq *expr_list;
2193 /* del_stmt: 'del' exprlist */
2194 REQ(n, del_stmt);
2196 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2197 if (!expr_list)
2198 return NULL;
2199 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
2202 static stmt_ty
2203 ast_for_flow_stmt(struct compiling *c, const node *n)
2206 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2207 | yield_stmt
2208 break_stmt: 'break'
2209 continue_stmt: 'continue'
2210 return_stmt: 'return' [testlist]
2211 yield_stmt: yield_expr
2212 yield_expr: 'yield' testlist
2213 raise_stmt: 'raise' [test [',' test [',' test]]]
2215 node *ch;
2217 REQ(n, flow_stmt);
2218 ch = CHILD(n, 0);
2219 switch (TYPE(ch)) {
2220 case break_stmt:
2221 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2222 case continue_stmt:
2223 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2224 case yield_stmt: { /* will reduce to yield_expr */
2225 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2226 if (!exp)
2227 return NULL;
2228 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2230 case return_stmt:
2231 if (NCH(ch) == 1)
2232 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2233 else {
2234 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2235 if (!expression)
2236 return NULL;
2237 return Return(expression, LINENO(n), n->n_col_offset,
2238 c->c_arena);
2240 case raise_stmt:
2241 if (NCH(ch) == 1)
2242 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2243 c->c_arena);
2244 else if (NCH(ch) == 2) {
2245 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2246 if (!expression)
2247 return NULL;
2248 return Raise(expression, NULL, NULL, LINENO(n),
2249 n->n_col_offset, c->c_arena);
2251 else if (NCH(ch) == 4) {
2252 expr_ty expr1, expr2;
2254 expr1 = ast_for_expr(c, CHILD(ch, 1));
2255 if (!expr1)
2256 return NULL;
2257 expr2 = ast_for_expr(c, CHILD(ch, 3));
2258 if (!expr2)
2259 return NULL;
2261 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2262 c->c_arena);
2264 else if (NCH(ch) == 6) {
2265 expr_ty expr1, expr2, expr3;
2267 expr1 = ast_for_expr(c, CHILD(ch, 1));
2268 if (!expr1)
2269 return NULL;
2270 expr2 = ast_for_expr(c, CHILD(ch, 3));
2271 if (!expr2)
2272 return NULL;
2273 expr3 = ast_for_expr(c, CHILD(ch, 5));
2274 if (!expr3)
2275 return NULL;
2277 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2278 c->c_arena);
2280 default:
2281 PyErr_Format(PyExc_SystemError,
2282 "unexpected flow_stmt: %d", TYPE(ch));
2283 return NULL;
2286 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2287 return NULL;
2290 static alias_ty
2291 alias_for_import_name(struct compiling *c, const node *n)
2294 import_as_name: NAME ['as' NAME]
2295 dotted_as_name: dotted_name ['as' NAME]
2296 dotted_name: NAME ('.' NAME)*
2298 PyObject *str;
2300 loop:
2301 switch (TYPE(n)) {
2302 case import_as_name:
2303 str = NULL;
2304 if (NCH(n) == 3) {
2305 str = NEW_IDENTIFIER(CHILD(n, 2));
2307 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2308 case dotted_as_name:
2309 if (NCH(n) == 1) {
2310 n = CHILD(n, 0);
2311 goto loop;
2313 else {
2314 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2315 if (!a)
2316 return NULL;
2317 assert(!a->asname);
2318 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2319 return a;
2321 break;
2322 case dotted_name:
2323 if (NCH(n) == 1)
2324 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2325 else {
2326 /* Create a string of the form "a.b.c" */
2327 int i;
2328 size_t len;
2329 char *s;
2331 len = 0;
2332 for (i = 0; i < NCH(n); i += 2)
2333 /* length of string plus one for the dot */
2334 len += strlen(STR(CHILD(n, i))) + 1;
2335 len--; /* the last name doesn't have a dot */
2336 str = PyString_FromStringAndSize(NULL, len);
2337 if (!str)
2338 return NULL;
2339 s = PyString_AS_STRING(str);
2340 if (!s)
2341 return NULL;
2342 for (i = 0; i < NCH(n); i += 2) {
2343 char *sch = STR(CHILD(n, i));
2344 strcpy(s, STR(CHILD(n, i)));
2345 s += strlen(sch);
2346 *s++ = '.';
2348 --s;
2349 *s = '\0';
2350 PyString_InternInPlace(&str);
2351 PyArena_AddPyObject(c->c_arena, str);
2352 return alias(str, NULL, c->c_arena);
2354 break;
2355 case STAR:
2356 str = PyString_InternFromString("*");
2357 PyArena_AddPyObject(c->c_arena, str);
2358 return alias(str, NULL, c->c_arena);
2359 default:
2360 PyErr_Format(PyExc_SystemError,
2361 "unexpected import name: %d", TYPE(n));
2362 return NULL;
2365 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
2366 return NULL;
2369 static stmt_ty
2370 ast_for_import_stmt(struct compiling *c, const node *n)
2373 import_stmt: import_name | import_from
2374 import_name: 'import' dotted_as_names
2375 import_from: 'from' ('.'* dotted_name | '.') 'import'
2376 ('*' | '(' import_as_names ')' | import_as_names)
2378 int lineno;
2379 int col_offset;
2380 int i;
2381 asdl_seq *aliases;
2383 REQ(n, import_stmt);
2384 lineno = LINENO(n);
2385 col_offset = n->n_col_offset;
2386 n = CHILD(n, 0);
2387 if (TYPE(n) == import_name) {
2388 n = CHILD(n, 1);
2389 REQ(n, dotted_as_names);
2390 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2391 if (!aliases)
2392 return NULL;
2393 for (i = 0; i < NCH(n); i += 2) {
2394 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2395 if (!import_alias)
2396 return NULL;
2397 asdl_seq_SET(aliases, i / 2, import_alias);
2399 return Import(aliases, lineno, col_offset, c->c_arena);
2401 else if (TYPE(n) == import_from) {
2402 int n_children;
2403 int idx, ndots = 0;
2404 alias_ty mod = NULL;
2405 identifier modname;
2407 /* Count the number of dots (for relative imports) and check for the
2408 optional module name */
2409 for (idx = 1; idx < NCH(n); idx++) {
2410 if (TYPE(CHILD(n, idx)) == dotted_name) {
2411 mod = alias_for_import_name(c, CHILD(n, idx));
2412 idx++;
2413 break;
2414 } else if (TYPE(CHILD(n, idx)) != DOT) {
2415 break;
2417 ndots++;
2419 idx++; /* skip over the 'import' keyword */
2420 switch (TYPE(CHILD(n, idx))) {
2421 case STAR:
2422 /* from ... import * */
2423 n = CHILD(n, idx);
2424 n_children = 1;
2425 break;
2426 case LPAR:
2427 /* from ... import (x, y, z) */
2428 n = CHILD(n, idx + 1);
2429 n_children = NCH(n);
2430 break;
2431 case import_as_names:
2432 /* from ... import x, y, z */
2433 n = CHILD(n, idx);
2434 n_children = NCH(n);
2435 if (n_children % 2 == 0) {
2436 ast_error(n, "trailing comma not allowed without"
2437 " surrounding parentheses");
2438 return NULL;
2440 break;
2441 default:
2442 ast_error(n, "Unexpected node-type in from-import");
2443 return NULL;
2446 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2447 if (!aliases)
2448 return NULL;
2450 /* handle "from ... import *" special b/c there's no children */
2451 if (TYPE(n) == STAR) {
2452 alias_ty import_alias = alias_for_import_name(c, n);
2453 if (!import_alias)
2454 return NULL;
2455 asdl_seq_SET(aliases, 0, import_alias);
2457 else {
2458 for (i = 0; i < NCH(n); i += 2) {
2459 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2460 if (!import_alias)
2461 return NULL;
2462 asdl_seq_SET(aliases, i / 2, import_alias);
2465 if (mod != NULL)
2466 modname = mod->name;
2467 else
2468 modname = new_identifier("", c->c_arena);
2469 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2470 c->c_arena);
2472 PyErr_Format(PyExc_SystemError,
2473 "unknown import statement: starts with command '%s'",
2474 STR(CHILD(n, 0)));
2475 return NULL;
2478 static stmt_ty
2479 ast_for_global_stmt(struct compiling *c, const node *n)
2481 /* global_stmt: 'global' NAME (',' NAME)* */
2482 identifier name;
2483 asdl_seq *s;
2484 int i;
2486 REQ(n, global_stmt);
2487 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
2488 if (!s)
2489 return NULL;
2490 for (i = 1; i < NCH(n); i += 2) {
2491 name = NEW_IDENTIFIER(CHILD(n, i));
2492 if (!name)
2493 return NULL;
2494 asdl_seq_SET(s, i / 2, name);
2496 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
2499 static stmt_ty
2500 ast_for_exec_stmt(struct compiling *c, const node *n)
2502 expr_ty expr1, globals = NULL, locals = NULL;
2503 int n_children = NCH(n);
2504 if (n_children != 2 && n_children != 4 && n_children != 6) {
2505 PyErr_Format(PyExc_SystemError,
2506 "poorly formed 'exec' statement: %d parts to statement",
2507 n_children);
2508 return NULL;
2511 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2512 REQ(n, exec_stmt);
2513 expr1 = ast_for_expr(c, CHILD(n, 1));
2514 if (!expr1)
2515 return NULL;
2516 if (n_children >= 4) {
2517 globals = ast_for_expr(c, CHILD(n, 3));
2518 if (!globals)
2519 return NULL;
2521 if (n_children == 6) {
2522 locals = ast_for_expr(c, CHILD(n, 5));
2523 if (!locals)
2524 return NULL;
2527 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2528 c->c_arena);
2531 static stmt_ty
2532 ast_for_assert_stmt(struct compiling *c, const node *n)
2534 /* assert_stmt: 'assert' test [',' test] */
2535 REQ(n, assert_stmt);
2536 if (NCH(n) == 2) {
2537 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2538 if (!expression)
2539 return NULL;
2540 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2541 c->c_arena);
2543 else if (NCH(n) == 4) {
2544 expr_ty expr1, expr2;
2546 expr1 = ast_for_expr(c, CHILD(n, 1));
2547 if (!expr1)
2548 return NULL;
2549 expr2 = ast_for_expr(c, CHILD(n, 3));
2550 if (!expr2)
2551 return NULL;
2553 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
2555 PyErr_Format(PyExc_SystemError,
2556 "improper number of parts to 'assert' statement: %d",
2557 NCH(n));
2558 return NULL;
2561 static asdl_seq *
2562 ast_for_suite(struct compiling *c, const node *n)
2564 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2565 asdl_seq *seq;
2566 stmt_ty s;
2567 int i, total, num, end, pos = 0;
2568 node *ch;
2570 REQ(n, suite);
2572 total = num_stmts(n);
2573 seq = asdl_seq_new(total, c->c_arena);
2574 if (!seq)
2575 return NULL;
2576 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2577 n = CHILD(n, 0);
2578 /* simple_stmt always ends with a NEWLINE,
2579 and may have a trailing SEMI
2581 end = NCH(n) - 1;
2582 if (TYPE(CHILD(n, end - 1)) == SEMI)
2583 end--;
2584 /* loop by 2 to skip semi-colons */
2585 for (i = 0; i < end; i += 2) {
2586 ch = CHILD(n, i);
2587 s = ast_for_stmt(c, ch);
2588 if (!s)
2589 return NULL;
2590 asdl_seq_SET(seq, pos++, s);
2593 else {
2594 for (i = 2; i < (NCH(n) - 1); i++) {
2595 ch = CHILD(n, i);
2596 REQ(ch, stmt);
2597 num = num_stmts(ch);
2598 if (num == 1) {
2599 /* small_stmt or compound_stmt with only one child */
2600 s = ast_for_stmt(c, ch);
2601 if (!s)
2602 return NULL;
2603 asdl_seq_SET(seq, pos++, s);
2605 else {
2606 int j;
2607 ch = CHILD(ch, 0);
2608 REQ(ch, simple_stmt);
2609 for (j = 0; j < NCH(ch); j += 2) {
2610 /* statement terminates with a semi-colon ';' */
2611 if (NCH(CHILD(ch, j)) == 0) {
2612 assert((j + 1) == NCH(ch));
2613 break;
2615 s = ast_for_stmt(c, CHILD(ch, j));
2616 if (!s)
2617 return NULL;
2618 asdl_seq_SET(seq, pos++, s);
2623 assert(pos == seq->size);
2624 return seq;
2627 static stmt_ty
2628 ast_for_if_stmt(struct compiling *c, const node *n)
2630 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2631 ['else' ':' suite]
2633 char *s;
2635 REQ(n, if_stmt);
2637 if (NCH(n) == 4) {
2638 expr_ty expression;
2639 asdl_seq *suite_seq;
2641 expression = ast_for_expr(c, CHILD(n, 1));
2642 if (!expression)
2643 return NULL;
2644 suite_seq = ast_for_suite(c, CHILD(n, 3));
2645 if (!suite_seq)
2646 return NULL;
2648 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2649 c->c_arena);
2652 s = STR(CHILD(n, 4));
2653 /* s[2], the third character in the string, will be
2654 's' for el_s_e, or
2655 'i' for el_i_f
2657 if (s[2] == 's') {
2658 expr_ty expression;
2659 asdl_seq *seq1, *seq2;
2661 expression = ast_for_expr(c, CHILD(n, 1));
2662 if (!expression)
2663 return NULL;
2664 seq1 = ast_for_suite(c, CHILD(n, 3));
2665 if (!seq1)
2666 return NULL;
2667 seq2 = ast_for_suite(c, CHILD(n, 6));
2668 if (!seq2)
2669 return NULL;
2671 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2672 c->c_arena);
2674 else if (s[2] == 'i') {
2675 int i, n_elif, has_else = 0;
2676 expr_ty expression;
2677 asdl_seq *suite_seq;
2678 asdl_seq *orelse = NULL;
2679 n_elif = NCH(n) - 4;
2680 /* must reference the child n_elif+1 since 'else' token is third,
2681 not fourth, child from the end. */
2682 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2683 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2684 has_else = 1;
2685 n_elif -= 3;
2687 n_elif /= 4;
2689 if (has_else) {
2690 asdl_seq *suite_seq2;
2692 orelse = asdl_seq_new(1, c->c_arena);
2693 if (!orelse)
2694 return NULL;
2695 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2696 if (!expression)
2697 return NULL;
2698 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2699 if (!suite_seq)
2700 return NULL;
2701 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2702 if (!suite_seq2)
2703 return NULL;
2705 asdl_seq_SET(orelse, 0,
2706 If(expression, suite_seq, suite_seq2,
2707 LINENO(CHILD(n, NCH(n) - 6)),
2708 CHILD(n, NCH(n) - 6)->n_col_offset,
2709 c->c_arena));
2710 /* the just-created orelse handled the last elif */
2711 n_elif--;
2714 for (i = 0; i < n_elif; i++) {
2715 int off = 5 + (n_elif - i - 1) * 4;
2716 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2717 if (!newobj)
2718 return NULL;
2719 expression = ast_for_expr(c, CHILD(n, off));
2720 if (!expression)
2721 return NULL;
2722 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2723 if (!suite_seq)
2724 return NULL;
2726 asdl_seq_SET(newobj, 0,
2727 If(expression, suite_seq, orelse,
2728 LINENO(CHILD(n, off)),
2729 CHILD(n, off)->n_col_offset, c->c_arena));
2730 orelse = newobj;
2732 expression = ast_for_expr(c, CHILD(n, 1));
2733 if (!expression)
2734 return NULL;
2735 suite_seq = ast_for_suite(c, CHILD(n, 3));
2736 if (!suite_seq)
2737 return NULL;
2738 return If(expression, suite_seq, orelse,
2739 LINENO(n), n->n_col_offset, c->c_arena);
2742 PyErr_Format(PyExc_SystemError,
2743 "unexpected token in 'if' statement: %s", s);
2744 return NULL;
2747 static stmt_ty
2748 ast_for_while_stmt(struct compiling *c, const node *n)
2750 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2751 REQ(n, while_stmt);
2753 if (NCH(n) == 4) {
2754 expr_ty expression;
2755 asdl_seq *suite_seq;
2757 expression = ast_for_expr(c, CHILD(n, 1));
2758 if (!expression)
2759 return NULL;
2760 suite_seq = ast_for_suite(c, CHILD(n, 3));
2761 if (!suite_seq)
2762 return NULL;
2763 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2764 c->c_arena);
2766 else if (NCH(n) == 7) {
2767 expr_ty expression;
2768 asdl_seq *seq1, *seq2;
2770 expression = ast_for_expr(c, CHILD(n, 1));
2771 if (!expression)
2772 return NULL;
2773 seq1 = ast_for_suite(c, CHILD(n, 3));
2774 if (!seq1)
2775 return NULL;
2776 seq2 = ast_for_suite(c, CHILD(n, 6));
2777 if (!seq2)
2778 return NULL;
2780 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2781 c->c_arena);
2784 PyErr_Format(PyExc_SystemError,
2785 "wrong number of tokens for 'while' statement: %d",
2786 NCH(n));
2787 return NULL;
2790 static stmt_ty
2791 ast_for_for_stmt(struct compiling *c, const node *n)
2793 asdl_seq *_target, *seq = NULL, *suite_seq;
2794 expr_ty expression;
2795 expr_ty target;
2796 const node *node_target;
2797 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2798 REQ(n, for_stmt);
2800 if (NCH(n) == 9) {
2801 seq = ast_for_suite(c, CHILD(n, 8));
2802 if (!seq)
2803 return NULL;
2806 node_target = CHILD(n, 1);
2807 _target = ast_for_exprlist(c, node_target, Store);
2808 if (!_target)
2809 return NULL;
2810 /* Check the # of children rather than the length of _target, since
2811 for x, in ... has 1 element in _target, but still requires a Tuple. */
2812 if (NCH(node_target) == 1)
2813 target = (expr_ty)asdl_seq_GET(_target, 0);
2814 else
2815 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
2817 expression = ast_for_testlist(c, CHILD(n, 3));
2818 if (!expression)
2819 return NULL;
2820 suite_seq = ast_for_suite(c, CHILD(n, 5));
2821 if (!suite_seq)
2822 return NULL;
2824 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2825 c->c_arena);
2828 static excepthandler_ty
2829 ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2831 /* except_clause: 'except' [test [(',' | 'as') test]] */
2832 REQ(exc, except_clause);
2833 REQ(body, suite);
2835 if (NCH(exc) == 1) {
2836 asdl_seq *suite_seq = ast_for_suite(c, body);
2837 if (!suite_seq)
2838 return NULL;
2840 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
2841 exc->n_col_offset, c->c_arena);
2843 else if (NCH(exc) == 2) {
2844 expr_ty expression;
2845 asdl_seq *suite_seq;
2847 expression = ast_for_expr(c, CHILD(exc, 1));
2848 if (!expression)
2849 return NULL;
2850 suite_seq = ast_for_suite(c, body);
2851 if (!suite_seq)
2852 return NULL;
2854 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
2855 exc->n_col_offset, c->c_arena);
2857 else if (NCH(exc) == 4) {
2858 asdl_seq *suite_seq;
2859 expr_ty expression;
2860 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2861 if (!e)
2862 return NULL;
2863 if (!set_context(c, e, Store, CHILD(exc, 3)))
2864 return NULL;
2865 expression = ast_for_expr(c, CHILD(exc, 1));
2866 if (!expression)
2867 return NULL;
2868 suite_seq = ast_for_suite(c, body);
2869 if (!suite_seq)
2870 return NULL;
2872 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
2873 exc->n_col_offset, c->c_arena);
2876 PyErr_Format(PyExc_SystemError,
2877 "wrong number of children for 'except' clause: %d",
2878 NCH(exc));
2879 return NULL;
2882 static stmt_ty
2883 ast_for_try_stmt(struct compiling *c, const node *n)
2885 const int nch = NCH(n);
2886 int n_except = (nch - 3)/3;
2887 asdl_seq *body, *orelse = NULL, *finally = NULL;
2889 REQ(n, try_stmt);
2891 body = ast_for_suite(c, CHILD(n, 2));
2892 if (body == NULL)
2893 return NULL;
2895 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2896 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2897 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2898 /* we can assume it's an "else",
2899 because nch >= 9 for try-else-finally and
2900 it would otherwise have a type of except_clause */
2901 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2902 if (orelse == NULL)
2903 return NULL;
2904 n_except--;
2907 finally = ast_for_suite(c, CHILD(n, nch - 1));
2908 if (finally == NULL)
2909 return NULL;
2910 n_except--;
2912 else {
2913 /* we can assume it's an "else",
2914 otherwise it would have a type of except_clause */
2915 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2916 if (orelse == NULL)
2917 return NULL;
2918 n_except--;
2921 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
2922 ast_error(n, "malformed 'try' statement");
2923 return NULL;
2926 if (n_except > 0) {
2927 int i;
2928 stmt_ty except_st;
2929 /* process except statements to create a try ... except */
2930 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2931 if (handlers == NULL)
2932 return NULL;
2934 for (i = 0; i < n_except; i++) {
2935 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2936 CHILD(n, 5 + i * 3));
2937 if (!e)
2938 return NULL;
2939 asdl_seq_SET(handlers, i, e);
2942 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2943 n->n_col_offset, c->c_arena);
2944 if (!finally)
2945 return except_st;
2947 /* if a 'finally' is present too, we nest the TryExcept within a
2948 TryFinally to emulate try ... except ... finally */
2949 body = asdl_seq_new(1, c->c_arena);
2950 if (body == NULL)
2951 return NULL;
2952 asdl_seq_SET(body, 0, except_st);
2955 /* must be a try ... finally (except clauses are in body, if any exist) */
2956 assert(finally != NULL);
2957 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
2960 static expr_ty
2961 ast_for_with_var(struct compiling *c, const node *n)
2963 REQ(n, with_var);
2964 return ast_for_expr(c, CHILD(n, 1));
2967 /* with_stmt: 'with' test [ with_var ] ':' suite */
2968 static stmt_ty
2969 ast_for_with_stmt(struct compiling *c, const node *n)
2971 expr_ty context_expr, optional_vars = NULL;
2972 int suite_index = 3; /* skip 'with', test, and ':' */
2973 asdl_seq *suite_seq;
2975 assert(TYPE(n) == with_stmt);
2976 context_expr = ast_for_expr(c, CHILD(n, 1));
2977 if (!context_expr)
2978 return NULL;
2979 if (TYPE(CHILD(n, 2)) == with_var) {
2980 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2982 if (!optional_vars) {
2983 return NULL;
2985 if (!set_context(c, optional_vars, Store, n)) {
2986 return NULL;
2988 suite_index = 4;
2991 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2992 if (!suite_seq) {
2993 return NULL;
2995 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2996 n->n_col_offset, c->c_arena);
2999 static stmt_ty
3000 ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
3002 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3003 asdl_seq *bases, *s;
3005 REQ(n, classdef);
3007 if (!forbidden_check(c, n, STR(CHILD(n, 1))))
3008 return NULL;
3010 if (NCH(n) == 4) {
3011 s = ast_for_suite(c, CHILD(n, 3));
3012 if (!s)
3013 return NULL;
3014 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3015 LINENO(n), n->n_col_offset, c->c_arena);
3017 /* check for empty base list */
3018 if (TYPE(CHILD(n,3)) == RPAR) {
3019 s = ast_for_suite(c, CHILD(n,5));
3020 if (!s)
3021 return NULL;
3022 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3023 LINENO(n), n->n_col_offset, c->c_arena);
3026 /* else handle the base class list */
3027 bases = ast_for_class_bases(c, CHILD(n, 3));
3028 if (!bases)
3029 return NULL;
3031 s = ast_for_suite(c, CHILD(n, 6));
3032 if (!s)
3033 return NULL;
3034 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3035 LINENO(n), n->n_col_offset, c->c_arena);
3038 static stmt_ty
3039 ast_for_stmt(struct compiling *c, const node *n)
3041 if (TYPE(n) == stmt) {
3042 assert(NCH(n) == 1);
3043 n = CHILD(n, 0);
3045 if (TYPE(n) == simple_stmt) {
3046 assert(num_stmts(n) == 1);
3047 n = CHILD(n, 0);
3049 if (TYPE(n) == small_stmt) {
3050 REQ(n, small_stmt);
3051 n = CHILD(n, 0);
3052 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3053 | flow_stmt | import_stmt | global_stmt | exec_stmt
3054 | assert_stmt
3056 switch (TYPE(n)) {
3057 case expr_stmt:
3058 return ast_for_expr_stmt(c, n);
3059 case print_stmt:
3060 return ast_for_print_stmt(c, n);
3061 case del_stmt:
3062 return ast_for_del_stmt(c, n);
3063 case pass_stmt:
3064 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3065 case flow_stmt:
3066 return ast_for_flow_stmt(c, n);
3067 case import_stmt:
3068 return ast_for_import_stmt(c, n);
3069 case global_stmt:
3070 return ast_for_global_stmt(c, n);
3071 case exec_stmt:
3072 return ast_for_exec_stmt(c, n);
3073 case assert_stmt:
3074 return ast_for_assert_stmt(c, n);
3075 default:
3076 PyErr_Format(PyExc_SystemError,
3077 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3078 TYPE(n), NCH(n));
3079 return NULL;
3082 else {
3083 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3084 | funcdef | classdef | decorated
3086 node *ch = CHILD(n, 0);
3087 REQ(n, compound_stmt);
3088 switch (TYPE(ch)) {
3089 case if_stmt:
3090 return ast_for_if_stmt(c, ch);
3091 case while_stmt:
3092 return ast_for_while_stmt(c, ch);
3093 case for_stmt:
3094 return ast_for_for_stmt(c, ch);
3095 case try_stmt:
3096 return ast_for_try_stmt(c, ch);
3097 case with_stmt:
3098 return ast_for_with_stmt(c, ch);
3099 case funcdef:
3100 return ast_for_funcdef(c, ch, NULL);
3101 case classdef:
3102 return ast_for_classdef(c, ch, NULL);
3103 case decorated:
3104 return ast_for_decorated(c, ch);
3105 default:
3106 PyErr_Format(PyExc_SystemError,
3107 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3108 TYPE(n), NCH(n));
3109 return NULL;
3114 static PyObject *
3115 parsenumber(struct compiling *c, const char *s)
3117 const char *end;
3118 long x;
3119 double dx;
3120 #ifndef WITHOUT_COMPLEX
3121 Py_complex complex;
3122 int imflag;
3123 #endif
3125 errno = 0;
3126 end = s + strlen(s) - 1;
3127 #ifndef WITHOUT_COMPLEX
3128 imflag = *end == 'j' || *end == 'J';
3129 #endif
3130 if (*end == 'l' || *end == 'L')
3131 return PyLong_FromString((char *)s, (char **)0, 0);
3132 if (s[0] == '0') {
3133 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3134 if (x < 0 && errno == 0) {
3135 return PyLong_FromString((char *)s,
3136 (char **)0,
3140 else
3141 x = PyOS_strtol((char *)s, (char **)&end, 0);
3142 if (*end == '\0') {
3143 if (errno != 0)
3144 return PyLong_FromString((char *)s, (char **)0, 0);
3145 return PyInt_FromLong(x);
3147 /* XXX Huge floats may silently fail */
3148 #ifndef WITHOUT_COMPLEX
3149 if (imflag) {
3150 complex.real = 0.;
3151 PyFPE_START_PROTECT("atof", return 0)
3152 complex.imag = PyOS_ascii_atof(s);
3153 PyFPE_END_PROTECT(complex)
3154 return PyComplex_FromCComplex(complex);
3156 else
3157 #endif
3159 PyFPE_START_PROTECT("atof", return 0)
3160 dx = PyOS_ascii_atof(s);
3161 PyFPE_END_PROTECT(dx)
3162 return PyFloat_FromDouble(dx);
3166 static PyObject *
3167 decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
3169 #ifndef Py_USING_UNICODE
3170 Py_FatalError("decode_utf8 should not be called in this build.");
3171 return NULL;
3172 #else
3173 PyObject *u, *v;
3174 char *s, *t;
3175 t = s = (char *)*sPtr;
3176 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3177 while (s < end && (*s & 0x80)) s++;
3178 *sPtr = s;
3179 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3180 if (u == NULL)
3181 return NULL;
3182 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3183 Py_DECREF(u);
3184 return v;
3185 #endif
3188 #ifdef Py_USING_UNICODE
3189 static PyObject *
3190 decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
3192 PyObject *v, *u;
3193 char *buf;
3194 char *p;
3195 const char *end;
3196 if (encoding == NULL) {
3197 buf = (char *)s;
3198 u = NULL;
3199 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3200 buf = (char *)s;
3201 u = NULL;
3202 } else {
3203 /* check for integer overflow */
3204 if (len > PY_SIZE_MAX / 4)
3205 return NULL;
3206 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3207 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3208 if (u == NULL)
3209 return NULL;
3210 p = buf = PyString_AsString(u);
3211 end = s + len;
3212 while (s < end) {
3213 if (*s == '\\') {
3214 *p++ = *s++;
3215 if (*s & 0x80) {
3216 strcpy(p, "u005c");
3217 p += 5;
3220 if (*s & 0x80) { /* XXX inefficient */
3221 PyObject *w;
3222 char *r;
3223 Py_ssize_t rn, i;
3224 w = decode_utf8(c, &s, end, "utf-16-be");
3225 if (w == NULL) {
3226 Py_DECREF(u);
3227 return NULL;
3229 r = PyString_AsString(w);
3230 rn = PyString_Size(w);
3231 assert(rn % 2 == 0);
3232 for (i = 0; i < rn; i += 2) {
3233 sprintf(p, "\\u%02x%02x",
3234 r[i + 0] & 0xFF,
3235 r[i + 1] & 0xFF);
3236 p += 6;
3238 Py_DECREF(w);
3239 } else {
3240 *p++ = *s++;
3243 len = p - buf;
3244 s = buf;
3246 if (rawmode)
3247 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3248 else
3249 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3250 Py_XDECREF(u);
3251 return v;
3253 #endif
3255 /* s is a Python string literal, including the bracketing quote characters,
3256 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3257 * parsestr parses it, and returns the decoded Python string object.
3259 static PyObject *
3260 parsestr(struct compiling *c, const char *s)
3262 size_t len;
3263 int quote = Py_CHARMASK(*s);
3264 int rawmode = 0;
3265 int need_encoding;
3266 int unicode = c->c_future_unicode;
3268 if (isalpha(quote) || quote == '_') {
3269 if (quote == 'u' || quote == 'U') {
3270 quote = *++s;
3271 unicode = 1;
3273 if (quote == 'b' || quote == 'B') {
3274 quote = *++s;
3275 unicode = 0;
3277 if (quote == 'r' || quote == 'R') {
3278 quote = *++s;
3279 rawmode = 1;
3282 if (quote != '\'' && quote != '\"') {
3283 PyErr_BadInternalCall();
3284 return NULL;
3286 s++;
3287 len = strlen(s);
3288 if (len > INT_MAX) {
3289 PyErr_SetString(PyExc_OverflowError,
3290 "string to parse is too long");
3291 return NULL;
3293 if (s[--len] != quote) {
3294 PyErr_BadInternalCall();
3295 return NULL;
3297 if (len >= 4 && s[0] == quote && s[1] == quote) {
3298 s += 2;
3299 len -= 2;
3300 if (s[--len] != quote || s[--len] != quote) {
3301 PyErr_BadInternalCall();
3302 return NULL;
3305 #ifdef Py_USING_UNICODE
3306 if (unicode || Py_UnicodeFlag) {
3307 return decode_unicode(c, s, len, rawmode, c->c_encoding);
3309 #endif
3310 need_encoding = (c->c_encoding != NULL &&
3311 strcmp(c->c_encoding, "utf-8") != 0 &&
3312 strcmp(c->c_encoding, "iso-8859-1") != 0);
3313 if (rawmode || strchr(s, '\\') == NULL) {
3314 if (need_encoding) {
3315 #ifndef Py_USING_UNICODE
3316 /* This should not happen - we never see any other
3317 encoding. */
3318 Py_FatalError(
3319 "cannot deal with encodings in this build.");
3320 #else
3321 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3322 if (u == NULL)
3323 return NULL;
3324 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
3325 Py_DECREF(u);
3326 return v;
3327 #endif
3328 } else {
3329 return PyString_FromStringAndSize(s, len);
3333 return PyString_DecodeEscape(s, len, NULL, unicode,
3334 need_encoding ? c->c_encoding : NULL);
3337 /* Build a Python string object out of a STRING atom. This takes care of
3338 * compile-time literal catenation, calling parsestr() on each piece, and
3339 * pasting the intermediate results together.
3341 static PyObject *
3342 parsestrplus(struct compiling *c, const node *n)
3344 PyObject *v;
3345 int i;
3346 REQ(CHILD(n, 0), STRING);
3347 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
3348 /* String literal concatenation */
3349 for (i = 1; i < NCH(n); i++) {
3350 PyObject *s;
3351 s = parsestr(c, STR(CHILD(n, i)));
3352 if (s == NULL)
3353 goto onError;
3354 if (PyString_Check(v) && PyString_Check(s)) {
3355 PyString_ConcatAndDel(&v, s);
3356 if (v == NULL)
3357 goto onError;
3359 #ifdef Py_USING_UNICODE
3360 else {
3361 PyObject *temp = PyUnicode_Concat(v, s);
3362 Py_DECREF(s);
3363 Py_DECREF(v);
3364 v = temp;
3365 if (v == NULL)
3366 goto onError;
3368 #endif
3371 return v;
3373 onError:
3374 Py_XDECREF(v);
3375 return NULL;