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