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