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