source: vendor/python/2.5/Python/ast.c

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

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