blob: fbce7d71367fd428b317acde086a2b8502d19f2c [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)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000515 asdl_seq *seq;
516 expr_ty expression;
517 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 assert(TYPE(n) == testlist
519 || TYPE(n) == listmaker
520 || TYPE(n) == testlist_gexp
521 || TYPE(n) == testlist_safe
522 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523
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) {
Armin Rigo31441302005-10-21 12:57:31 +0000644 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
646 ast_error(CHILD(ch, 0), "assignment to None");
647 goto error;
648 }
Armin Rigo31441302005-10-21 12:57:31 +0000649 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
650 Param, LINENO(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 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) {
Armin Rigo31441302005-10-21 12:57:31 +00001901 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 node *ch = CHILD(n, i);
1903 if (TYPE(ch) == yield_expr) {
1904 ast_error(ch, "assignment to yield expression not possible");
1905 goto error;
1906 }
Armin Rigo31441302005-10-21 12:57:31 +00001907 e = ast_for_testlist(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908
1909 /* set context to assign */
1910 if (!e)
1911 goto error;
1912
1913 if (!set_context(e, Store, CHILD(n, i))) {
1914 free_expr(e);
1915 goto error;
1916 }
1917
1918 asdl_seq_SET(targets, i / 2, e);
1919 }
1920 value = CHILD(n, NCH(n) - 1);
1921 if (TYPE(value) == testlist)
1922 expression = ast_for_testlist(c, value, 0);
1923 else
1924 expression = ast_for_expr(c, value);
1925 if (!expression)
1926 return NULL;
1927 return Assign(targets, expression, LINENO(n));
1928 error:
1929 for (i = i / 2; i >= 0; i--)
1930 free_expr((expr_ty)asdl_seq_GET(targets, i));
1931 asdl_seq_free(targets);
1932 return NULL;
1933 }
1934 return NULL;
1935}
1936
1937static stmt_ty
1938ast_for_print_stmt(struct compiling *c, const node *n)
1939{
1940 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1941 | '>>' test [ (',' test)+ [','] ] )
1942 */
1943 expr_ty dest = NULL, expression;
1944 asdl_seq *seq;
1945 bool nl;
1946 int i, start = 1;
1947
1948 REQ(n, print_stmt);
1949 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1950 dest = ast_for_expr(c, CHILD(n, 2));
1951 if (!dest)
1952 return NULL;
1953 start = 4;
1954 }
1955 seq = asdl_seq_new((NCH(n) + 1 - start) / 2);
1956 if (!seq)
1957 return NULL;
1958 for (i = start; i < NCH(n); i += 2) {
1959 expression = ast_for_expr(c, CHILD(n, i));
1960 if (!expression) {
1961 asdl_seq_free(seq);
1962 return NULL;
1963 }
1964
1965 asdl_seq_APPEND(seq, expression);
1966 }
1967 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
1968 return Print(dest, seq, nl, LINENO(n));
1969}
1970
1971static asdl_seq *
1972ast_for_exprlist(struct compiling *c, const node *n, int context)
1973{
1974 asdl_seq *seq;
1975 int i;
1976 expr_ty e;
1977
1978 REQ(n, exprlist);
1979
1980 seq = asdl_seq_new((NCH(n) + 1) / 2);
1981 if (!seq)
1982 return NULL;
1983 for (i = 0; i < NCH(n); i += 2) {
1984 e = ast_for_expr(c, CHILD(n, i));
1985 if (!e) {
1986 asdl_seq_free(seq);
1987 return NULL;
1988 }
1989 if (context) {
1990 if (!set_context(e, context, CHILD(n, i)))
1991 return NULL;
1992 }
1993 asdl_seq_SET(seq, i / 2, e);
1994 }
1995 return seq;
1996}
1997
1998static stmt_ty
1999ast_for_del_stmt(struct compiling *c, const node *n)
2000{
2001 asdl_seq *expr_list;
2002
2003 /* del_stmt: 'del' exprlist */
2004 REQ(n, del_stmt);
2005
2006 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2007 if (!expr_list)
2008 return NULL;
2009 return Delete(expr_list, LINENO(n));
2010}
2011
2012static stmt_ty
2013ast_for_flow_stmt(struct compiling *c, const node *n)
2014{
2015 /*
2016 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2017 | yield_stmt
2018 break_stmt: 'break'
2019 continue_stmt: 'continue'
2020 return_stmt: 'return' [testlist]
2021 yield_stmt: yield_expr
2022 yield_expr: 'yield' testlist
2023 raise_stmt: 'raise' [test [',' test [',' test]]]
2024 */
2025 node *ch;
2026
2027 REQ(n, flow_stmt);
2028 ch = CHILD(n, 0);
2029 switch (TYPE(ch)) {
2030 case break_stmt:
2031 return Break(LINENO(n));
2032 case continue_stmt:
2033 return Continue(LINENO(n));
2034 case yield_stmt: { /* will reduce to yield_expr */
2035 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2036 if (!exp)
2037 return NULL;
2038 return Expr(exp, LINENO(n));
2039 }
2040 case return_stmt:
2041 if (NCH(ch) == 1)
2042 return Return(NULL, LINENO(n));
2043 else {
2044 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1), 0);
2045 if (!expression)
2046 return NULL;
2047 return Return(expression, LINENO(n));
2048 }
2049 case raise_stmt:
2050 if (NCH(ch) == 1)
2051 return Raise(NULL, NULL, NULL, LINENO(n));
2052 else if (NCH(ch) == 2) {
2053 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2054 if (!expression)
2055 return NULL;
2056 return Raise(expression, NULL, NULL, LINENO(n));
2057 }
2058 else if (NCH(ch) == 4) {
2059 expr_ty expr1, expr2;
2060
2061 expr1 = ast_for_expr(c, CHILD(ch, 1));
2062 if (!expr1)
2063 return NULL;
2064 expr2 = ast_for_expr(c, CHILD(ch, 3));
2065 if (!expr2)
2066 return NULL;
2067
2068 return Raise(expr1, expr2, NULL, LINENO(n));
2069 }
2070 else if (NCH(ch) == 6) {
2071 expr_ty expr1, expr2, expr3;
2072
2073 expr1 = ast_for_expr(c, CHILD(ch, 1));
2074 if (!expr1)
2075 return NULL;
2076 expr2 = ast_for_expr(c, CHILD(ch, 3));
2077 if (!expr2)
2078 return NULL;
2079 expr3 = ast_for_expr(c, CHILD(ch, 5));
2080 if (!expr3)
2081 return NULL;
2082
2083 return Raise(expr1, expr2, expr3, LINENO(n));
2084 }
2085 default:
2086 PyErr_Format(PyExc_Exception,
2087 "unexpected flow_stmt: %d", TYPE(ch));
2088 return NULL;
2089 }
2090}
2091
2092static alias_ty
2093alias_for_import_name(const node *n)
2094{
2095 /*
2096 import_as_name: NAME [NAME NAME]
2097 dotted_as_name: dotted_name [NAME NAME]
2098 dotted_name: NAME ('.' NAME)*
2099 */
2100 loop:
2101 switch (TYPE(n)) {
2102 case import_as_name:
2103 if (NCH(n) == 3)
2104 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
2105 NEW_IDENTIFIER(CHILD(n, 2)));
2106 else
2107 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
2108 NULL);
2109 break;
2110 case dotted_as_name:
2111 if (NCH(n) == 1) {
2112 n = CHILD(n, 0);
2113 goto loop;
2114 }
2115 else {
2116 alias_ty a = alias_for_import_name(CHILD(n, 0));
2117 assert(!a->asname);
2118 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2119 return a;
2120 }
2121 break;
2122 case dotted_name:
2123 if (NCH(n) == 1)
2124 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL);
2125 else {
2126 /* Create a string of the form "a.b.c" */
2127 int i, len;
2128 PyObject *str;
2129 char *s;
2130
2131 len = 0;
2132 for (i = 0; i < NCH(n); i += 2)
2133 /* length of string plus one for the dot */
2134 len += strlen(STR(CHILD(n, i))) + 1;
2135 len--; /* the last name doesn't have a dot */
2136 str = PyString_FromStringAndSize(NULL, len);
2137 if (!str)
2138 return NULL;
2139 s = PyString_AS_STRING(str);
2140 if (!s)
2141 return NULL;
2142 for (i = 0; i < NCH(n); i += 2) {
2143 char *sch = STR(CHILD(n, i));
2144 strcpy(s, STR(CHILD(n, i)));
2145 s += strlen(sch);
2146 *s++ = '.';
2147 }
2148 --s;
2149 *s = '\0';
2150 PyString_InternInPlace(&str);
2151 return alias(str, NULL);
2152 }
2153 break;
2154 case STAR:
2155 return alias(PyString_InternFromString("*"), NULL);
2156 default:
2157 PyErr_Format(PyExc_Exception,
2158 "unexpected import name: %d", TYPE(n));
2159 return NULL;
2160 }
2161 return NULL;
2162}
2163
2164static stmt_ty
2165ast_for_import_stmt(struct compiling *c, const node *n)
2166{
2167 /*
2168 import_stmt: import_name | import_from
2169 import_name: 'import' dotted_as_names
2170 import_from: 'from' dotted_name 'import' ('*' |
2171 '(' import_as_names ')' |
2172 import_as_names)
2173 */
2174 int i;
2175 asdl_seq *aliases;
2176
2177 REQ(n, import_stmt);
2178 n = CHILD(n, 0);
2179 if (STR(CHILD(n, 0))[0] == 'i') { /* import */
2180 n = CHILD(n, 1);
2181 aliases = asdl_seq_new((NCH(n) + 1) / 2);
2182 if (!aliases)
2183 return NULL;
2184 for (i = 0; i < NCH(n); i += 2) {
2185 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2186 if (!import_alias) {
2187 asdl_seq_free(aliases);
2188 return NULL;
2189 }
2190 asdl_seq_SET(aliases, i / 2, import_alias);
2191 }
2192 return Import(aliases, LINENO(n));
2193 }
2194 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
2195 stmt_ty import;
2196 int n_children;
2197 const char *from_modules;
2198 int lineno = LINENO(n);
2199 alias_ty mod = alias_for_import_name(CHILD(n, 1));
2200 if (!mod)
2201 return NULL;
2202
2203 /* XXX this needs to be cleaned up */
2204
2205 from_modules = STR(CHILD(n, 3));
2206 if (!from_modules) {
2207 n = CHILD(n, 3); /* from ... import x, y, z */
2208 if (NCH(n) % 2 == 0) {
2209 /* it ends with a comma, not valid but the parser allows it */
2210 ast_error(n, "trailing comma not allowed without"
2211 " surrounding parentheses");
2212 return NULL;
2213 }
2214 }
2215 else if (from_modules[0] == '*') {
2216 n = CHILD(n, 3); /* from ... import * */
2217 }
2218 else if (from_modules[0] == '(')
2219 n = CHILD(n, 4); /* from ... import (x, y, z) */
2220 else
2221 return NULL;
2222
2223 n_children = NCH(n);
2224 if (from_modules && from_modules[0] == '*')
2225 n_children = 1;
2226
2227 aliases = asdl_seq_new((n_children + 1) / 2);
2228 if (!aliases) {
2229 free_alias(mod);
2230 return NULL;
2231 }
2232
2233 /* handle "from ... import *" special b/c there's no children */
2234 if (from_modules && from_modules[0] == '*') {
2235 alias_ty import_alias = alias_for_import_name(n);
2236 if (!import_alias) {
2237 asdl_seq_free(aliases);
2238 free_alias(mod);
2239 return NULL;
2240 }
2241 asdl_seq_APPEND(aliases, import_alias);
2242 }
2243
2244 for (i = 0; i < NCH(n); i += 2) {
2245 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2246 if (!import_alias) {
2247 asdl_seq_free(aliases);
2248 free_alias(mod);
2249 return NULL;
2250 }
2251 asdl_seq_APPEND(aliases, import_alias);
2252 }
2253 Py_INCREF(mod->name);
2254 import = ImportFrom(mod->name, aliases, lineno);
2255 free_alias(mod);
2256 return import;
2257 }
2258 PyErr_Format(PyExc_Exception,
2259 "unknown import statement: starts with command '%s'",
2260 STR(CHILD(n, 0)));
2261 return NULL;
2262}
2263
2264static stmt_ty
2265ast_for_global_stmt(struct compiling *c, const node *n)
2266{
2267 /* global_stmt: 'global' NAME (',' NAME)* */
2268 identifier name;
2269 asdl_seq *s;
2270 int i;
2271
2272 REQ(n, global_stmt);
2273 s = asdl_seq_new(NCH(n) / 2);
2274 if (!s)
2275 return NULL;
2276 for (i = 1; i < NCH(n); i += 2) {
2277 name = NEW_IDENTIFIER(CHILD(n, i));
2278 if (!name) {
2279 asdl_seq_free(s);
2280 return NULL;
2281 }
2282 asdl_seq_SET(s, i / 2, name);
2283 }
2284 return Global(s, LINENO(n));
2285}
2286
2287static stmt_ty
2288ast_for_exec_stmt(struct compiling *c, const node *n)
2289{
2290 expr_ty expr1, globals = NULL, locals = NULL;
2291 int n_children = NCH(n);
2292 if (n_children != 2 && n_children != 4 && n_children != 6) {
2293 PyErr_Format(PyExc_Exception,
2294 "poorly formed 'exec' statement: %d parts to statement",
2295 n_children);
2296 return NULL;
2297 }
2298
2299 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2300 REQ(n, exec_stmt);
2301 expr1 = ast_for_expr(c, CHILD(n, 1));
2302 if (!expr1)
2303 return NULL;
2304 if (n_children >= 4) {
2305 globals = ast_for_expr(c, CHILD(n, 3));
2306 if (!globals)
2307 return NULL;
2308 }
2309 if (n_children == 6) {
2310 locals = ast_for_expr(c, CHILD(n, 5));
2311 if (!locals)
2312 return NULL;
2313 }
2314
2315 return Exec(expr1, globals, locals, LINENO(n));
2316}
2317
2318static stmt_ty
2319ast_for_assert_stmt(struct compiling *c, const node *n)
2320{
2321 /* assert_stmt: 'assert' test [',' test] */
2322 REQ(n, assert_stmt);
2323 if (NCH(n) == 2) {
2324 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2325 if (!expression)
2326 return NULL;
2327 return Assert(expression, NULL, LINENO(n));
2328 }
2329 else if (NCH(n) == 4) {
2330 expr_ty expr1, expr2;
2331
2332 expr1 = ast_for_expr(c, CHILD(n, 1));
2333 if (!expr1)
2334 return NULL;
2335 expr2 = ast_for_expr(c, CHILD(n, 3));
2336 if (!expr2)
2337 return NULL;
2338
2339 return Assert(expr1, expr2, LINENO(n));
2340 }
2341 PyErr_Format(PyExc_Exception,
2342 "improper number of parts to 'assert' statement: %d",
2343 NCH(n));
2344 return NULL;
2345}
2346
2347static asdl_seq *
2348ast_for_suite(struct compiling *c, const node *n)
2349{
2350 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2351 asdl_seq *seq = NULL;
2352 stmt_ty s;
2353 int i, total, num, end, pos = 0;
2354 node *ch;
2355
2356 REQ(n, suite);
2357
2358 total = num_stmts(n);
2359 seq = asdl_seq_new(total);
2360 if (!seq)
2361 return NULL;
2362 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2363 n = CHILD(n, 0);
2364 /* simple_stmt always ends with a NEWLINE,
2365 and may have a trailing SEMI
2366 */
2367 end = NCH(n) - 1;
2368 if (TYPE(CHILD(n, end - 1)) == SEMI)
2369 end--;
2370 /* loop by 2 to skip semi-colons */
2371 for (i = 0; i < end; i += 2) {
2372 ch = CHILD(n, i);
2373 s = ast_for_stmt(c, ch);
2374 if (!s)
2375 goto error;
2376 asdl_seq_SET(seq, pos++, s);
2377 }
2378 }
2379 else {
2380 for (i = 2; i < (NCH(n) - 1); i++) {
2381 ch = CHILD(n, i);
2382 REQ(ch, stmt);
2383 num = num_stmts(ch);
2384 if (num == 1) {
2385 /* small_stmt or compound_stmt with only one child */
2386 s = ast_for_stmt(c, ch);
2387 if (!s)
2388 goto error;
2389 asdl_seq_SET(seq, pos++, s);
2390 }
2391 else {
2392 int j;
2393 ch = CHILD(ch, 0);
2394 REQ(ch, simple_stmt);
2395 for (j = 0; j < NCH(ch); j += 2) {
2396 s = ast_for_stmt(c, CHILD(ch, j));
2397 if (!s)
2398 goto error;
2399 asdl_seq_SET(seq, pos++, s);
2400 }
2401 }
2402 }
2403 }
2404 assert(pos == seq->size);
2405 return seq;
2406 error:
2407 if (seq)
2408 asdl_seq_free(seq);
2409 return NULL;
2410}
2411
2412static stmt_ty
2413ast_for_if_stmt(struct compiling *c, const node *n)
2414{
2415 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2416 ['else' ':' suite]
2417 */
2418 char *s;
2419
2420 REQ(n, if_stmt);
2421
2422 if (NCH(n) == 4) {
2423 expr_ty expression;
2424 asdl_seq *suite_seq;
2425
2426 expression = ast_for_expr(c, CHILD(n, 1));
2427 if (!expression)
2428 return NULL;
2429 suite_seq = ast_for_suite(c, CHILD(n, 3));
2430 if (!suite_seq)
2431 return NULL;
2432
2433 return If(expression, suite_seq, NULL, LINENO(n));
2434 }
2435 s = STR(CHILD(n, 4));
2436 /* s[2], the third character in the string, will be
2437 's' for el_s_e, or
2438 'i' for el_i_f
2439 */
2440 if (s[2] == 's') {
2441 expr_ty expression;
2442 asdl_seq *seq1, *seq2;
2443
2444 expression = ast_for_expr(c, CHILD(n, 1));
2445 if (!expression)
2446 return NULL;
2447 seq1 = ast_for_suite(c, CHILD(n, 3));
2448 if (!seq1)
2449 return NULL;
2450 seq2 = ast_for_suite(c, CHILD(n, 6));
2451 if (!seq2)
2452 return NULL;
2453
2454 return If(expression, seq1, seq2, LINENO(n));
2455 }
2456 else if (s[2] == 'i') {
2457 int i, n_elif, has_else = 0;
2458 asdl_seq *orelse = NULL;
2459 n_elif = NCH(n) - 4;
2460 /* must reference the child n_elif+1 since 'else' token is third,
2461 not fourth, child from the end. */
2462 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2463 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2464 has_else = 1;
2465 n_elif -= 3;
2466 }
2467 n_elif /= 4;
2468
2469 if (has_else) {
2470 expr_ty expression;
2471 asdl_seq *seq1, *seq2;
2472
2473 orelse = asdl_seq_new(1);
2474 if (!orelse)
2475 return NULL;
2476 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2477 if (!expression) {
2478 asdl_seq_free(orelse);
2479 return NULL;
2480 }
2481 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2482 if (!seq1) {
2483 asdl_seq_free(orelse);
2484 return NULL;
2485 }
2486 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2487 if (!seq2) {
2488 asdl_seq_free(orelse);
2489 return NULL;
2490 }
2491
2492 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
2493 LINENO(CHILD(n, NCH(n) - 6))));
2494 /* the just-created orelse handled the last elif */
2495 n_elif--;
2496 }
2497 else
2498 orelse = NULL;
2499
2500 for (i = 0; i < n_elif; i++) {
2501 int off = 5 + (n_elif - i - 1) * 4;
2502 expr_ty expression;
2503 asdl_seq *suite_seq;
2504 asdl_seq *new = asdl_seq_new(1);
2505 if (!new)
2506 return NULL;
2507 expression = ast_for_expr(c, CHILD(n, off));
2508 if (!expression) {
2509 asdl_seq_free(new);
2510 return NULL;
2511 }
2512 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2513 if (!suite_seq) {
2514 asdl_seq_free(new);
2515 return NULL;
2516 }
2517
2518 asdl_seq_SET(new, 0,
2519 If(expression, suite_seq, orelse,
2520 LINENO(CHILD(n, off))));
2521 orelse = new;
2522 }
2523 return If(ast_for_expr(c, CHILD(n, 1)),
2524 ast_for_suite(c, CHILD(n, 3)),
2525 orelse, LINENO(n));
2526 }
2527 else {
2528 PyErr_Format(PyExc_Exception,
2529 "unexpected token in 'if' statement: %s", s);
2530 return NULL;
2531 }
2532}
2533
2534static stmt_ty
2535ast_for_while_stmt(struct compiling *c, const node *n)
2536{
2537 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2538 REQ(n, while_stmt);
2539
2540 if (NCH(n) == 4) {
2541 expr_ty expression;
2542 asdl_seq *suite_seq;
2543
2544 expression = ast_for_expr(c, CHILD(n, 1));
2545 if (!expression)
2546 return NULL;
2547 suite_seq = ast_for_suite(c, CHILD(n, 3));
2548 if (!suite_seq)
2549 return NULL;
2550 return While(expression, suite_seq, NULL, LINENO(n));
2551 }
2552 else if (NCH(n) == 7) {
2553 expr_ty expression;
2554 asdl_seq *seq1, *seq2;
2555
2556 expression = ast_for_expr(c, CHILD(n, 1));
2557 if (!expression)
2558 return NULL;
2559 seq1 = ast_for_suite(c, CHILD(n, 3));
2560 if (!seq1)
2561 return NULL;
2562 seq2 = ast_for_suite(c, CHILD(n, 6));
2563 if (!seq2)
2564 return NULL;
2565
2566 return While(expression, seq1, seq2, LINENO(n));
2567 }
2568 else {
2569 PyErr_Format(PyExc_Exception,
2570 "wrong number of tokens for 'while' statement: %d",
2571 NCH(n));
2572 return NULL;
2573 }
2574}
2575
2576static stmt_ty
2577ast_for_for_stmt(struct compiling *c, const node *n)
2578{
2579 asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL;
2580 expr_ty expression;
2581 expr_ty target;
2582 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2583 REQ(n, for_stmt);
2584
2585 if (NCH(n) == 9) {
2586 seq = ast_for_suite(c, CHILD(n, 8));
2587 if (!seq)
2588 return NULL;
2589 }
2590
2591 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
2592 if (!_target)
2593 return NULL;
2594 if (asdl_seq_LEN(_target) == 1) {
2595 target = asdl_seq_GET(_target, 0);
2596 asdl_seq_free(_target);
2597 }
2598 else
2599 target = Tuple(_target, Store, LINENO(n));
2600
2601 expression = ast_for_testlist(c, CHILD(n, 3), 0);
2602 if (!expression)
2603 return NULL;
2604 suite_seq = ast_for_suite(c, CHILD(n, 5));
2605 if (!suite_seq)
2606 return NULL;
2607
2608 return For(target, expression, suite_seq, seq, LINENO(n));
2609}
2610
2611static excepthandler_ty
2612ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2613{
2614 /* except_clause: 'except' [test [',' test]] */
2615 REQ(exc, except_clause);
2616 REQ(body, suite);
2617
2618 if (NCH(exc) == 1) {
2619 asdl_seq *suite_seq = ast_for_suite(c, body);
2620 if (!suite_seq)
2621 return NULL;
2622
2623 return excepthandler(NULL, NULL, suite_seq);
2624 }
2625 else if (NCH(exc) == 2) {
2626 expr_ty expression;
2627 asdl_seq *suite_seq;
2628
2629 expression = ast_for_expr(c, CHILD(exc, 1));
2630 if (!expression)
2631 return NULL;
2632 suite_seq = ast_for_suite(c, body);
2633 if (!suite_seq)
2634 return NULL;
2635
2636 return excepthandler(expression, NULL, suite_seq);
2637 }
2638 else if (NCH(exc) == 4) {
2639 asdl_seq *suite_seq;
2640 expr_ty expression;
2641 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2642 if (!e)
2643 return NULL;
2644 if (!set_context(e, Store, CHILD(exc, 3)))
2645 return NULL;
2646 expression = ast_for_expr(c, CHILD(exc, 1));
2647 if (!expression)
2648 return NULL;
2649 suite_seq = ast_for_suite(c, body);
2650 if (!suite_seq)
2651 return NULL;
2652
2653 return excepthandler(expression, e, suite_seq);
2654 }
2655 else {
2656 PyErr_Format(PyExc_Exception,
2657 "wrong number of children for 'except' clause: %d",
2658 NCH(exc));
2659 return NULL;
2660 }
2661}
2662
2663static stmt_ty
2664ast_for_try_stmt(struct compiling *c, const node *n)
2665{
2666 REQ(n, try_stmt);
2667
2668 if (TYPE(CHILD(n, 3)) == NAME) {/* must be 'finally' */
2669 /* try_stmt: 'try' ':' suite 'finally' ':' suite) */
2670 asdl_seq *s1, *s2;
2671 s1 = ast_for_suite(c, CHILD(n, 2));
2672 if (!s1)
2673 return NULL;
2674 s2 = ast_for_suite(c, CHILD(n, 5));
2675 if (!s2)
2676 return NULL;
2677
2678 return TryFinally(s1, s2, LINENO(n));
2679 }
2680 else if (TYPE(CHILD(n, 3)) == except_clause) {
2681 /* try_stmt: ('try' ':' suite (except_clause ':' suite)+
2682 ['else' ':' suite]
2683 */
2684 asdl_seq *suite_seq1, *suite_seq2;
2685 asdl_seq *handlers;
2686 int i, has_else = 0, n_except = NCH(n) - 3;
2687 if (TYPE(CHILD(n, NCH(n) - 3)) == NAME) {
2688 has_else = 1;
2689 n_except -= 3;
2690 }
2691 n_except /= 3;
2692 handlers = asdl_seq_new(n_except);
2693 if (!handlers)
2694 return NULL;
2695 for (i = 0; i < n_except; i++) {
2696 excepthandler_ty e = ast_for_except_clause(c,
2697 CHILD(n, 3 + i * 3),
2698 CHILD(n, 5 + i * 3));
2699 if (!e)
2700 return NULL;
2701 asdl_seq_SET(handlers, i, e);
2702 }
2703
2704 suite_seq1 = ast_for_suite(c, CHILD(n, 2));
2705 if (!suite_seq1)
2706 return NULL;
2707 if (has_else) {
2708 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2709 if (!suite_seq2)
2710 return NULL;
2711 }
2712 else
2713 suite_seq2 = NULL;
2714
2715 return TryExcept(suite_seq1, handlers, suite_seq2, LINENO(n));
2716 }
2717 else {
2718 PyErr_SetString(PyExc_Exception, "malformed 'try' statement");
2719 return NULL;
2720 }
2721}
2722
2723static stmt_ty
2724ast_for_classdef(struct compiling *c, const node *n)
2725{
2726 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2727 expr_ty _bases;
2728 asdl_seq *bases, *s;
2729
2730 REQ(n, classdef);
2731
2732 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2733 ast_error(n, "assignment to None");
2734 return NULL;
2735 }
2736
2737 if (NCH(n) == 4) {
2738 s = ast_for_suite(c, CHILD(n, 3));
2739 if (!s)
2740 return NULL;
2741 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2742 }
2743 /* check for empty base list */
2744 if (TYPE(CHILD(n,3)) == RPAR) {
2745 s = ast_for_suite(c, CHILD(n,5));
2746 if (!s)
2747 return NULL;
2748 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2749 }
2750
2751 /* else handle the base class list */
2752 _bases = ast_for_testlist(c, CHILD(n, 3), 0);
2753 if (!_bases)
2754 return NULL;
2755 /* XXX: I don't think we can set to diff types here, how to free???
2756
2757 Here's the allocation chain:
2758 Tuple (Python-ast.c:907)
2759 ast_for_testlist (ast.c:1782)
2760 ast_for_classdef (ast.c:2677)
2761 */
2762 if (_bases->kind == Tuple_kind)
2763 bases = _bases->v.Tuple.elts;
2764 else {
2765 bases = asdl_seq_new(1);
2766 if (!bases) {
2767 free_expr(_bases);
2768 /* XXX: free _bases */
2769 return NULL;
2770 }
2771 asdl_seq_SET(bases, 0, _bases);
2772 }
2773
2774 s = ast_for_suite(c, CHILD(n, 6));
2775 if (!s) {
2776 /* XXX: I think this free is correct, but needs to change see above */
2777 if (_bases->kind == Tuple_kind)
2778 free_expr(_bases);
2779 else {
2780 free_expr(_bases);
2781 asdl_seq_free(bases);
2782 }
2783 return NULL;
2784 }
2785 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n));
2786}
2787
2788static stmt_ty
2789ast_for_stmt(struct compiling *c, const node *n)
2790{
2791 if (TYPE(n) == stmt) {
2792 assert(NCH(n) == 1);
2793 n = CHILD(n, 0);
2794 }
2795 if (TYPE(n) == simple_stmt) {
2796 assert(num_stmts(n) == 1);
2797 n = CHILD(n, 0);
2798 }
2799 if (TYPE(n) == small_stmt) {
2800 REQ(n, small_stmt);
2801 n = CHILD(n, 0);
2802 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2803 | flow_stmt | import_stmt | global_stmt | exec_stmt
2804 | assert_stmt
2805 */
2806 switch (TYPE(n)) {
2807 case expr_stmt:
2808 return ast_for_expr_stmt(c, n);
2809 case print_stmt:
2810 return ast_for_print_stmt(c, n);
2811 case del_stmt:
2812 return ast_for_del_stmt(c, n);
2813 case pass_stmt:
2814 return Pass(LINENO(n));
2815 case flow_stmt:
2816 return ast_for_flow_stmt(c, n);
2817 case import_stmt:
2818 return ast_for_import_stmt(c, n);
2819 case global_stmt:
2820 return ast_for_global_stmt(c, n);
2821 case exec_stmt:
2822 return ast_for_exec_stmt(c, n);
2823 case assert_stmt:
2824 return ast_for_assert_stmt(c, n);
2825 default:
2826 PyErr_Format(PyExc_Exception,
2827 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2828 TYPE(n), NCH(n));
2829 return NULL;
2830 }
2831 }
2832 else {
2833 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2834 | funcdef | classdef
2835 */
2836 node *ch = CHILD(n, 0);
2837 REQ(n, compound_stmt);
2838 switch (TYPE(ch)) {
2839 case if_stmt:
2840 return ast_for_if_stmt(c, ch);
2841 case while_stmt:
2842 return ast_for_while_stmt(c, ch);
2843 case for_stmt:
2844 return ast_for_for_stmt(c, ch);
2845 case try_stmt:
2846 return ast_for_try_stmt(c, ch);
2847 case funcdef:
2848 return ast_for_funcdef(c, ch);
2849 case classdef:
2850 return ast_for_classdef(c, ch);
2851 default:
2852 PyErr_Format(PyExc_Exception,
2853 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2854 TYPE(n), NCH(n));
2855 return NULL;
2856 }
2857 }
2858}
2859
2860static PyObject *
2861parsenumber(const char *s)
2862{
2863 const char *end;
2864 long x;
2865 double dx;
2866#ifndef WITHOUT_COMPLEX
2867 Py_complex c;
2868 int imflag;
2869#endif
2870
2871 errno = 0;
2872 end = s + strlen(s) - 1;
2873#ifndef WITHOUT_COMPLEX
2874 imflag = *end == 'j' || *end == 'J';
2875#endif
2876 if (*end == 'l' || *end == 'L')
2877 return PyLong_FromString((char *)s, (char **)0, 0);
2878 if (s[0] == '0') {
2879 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2880 if (x < 0 && errno == 0) {
2881 return PyLong_FromString((char *)s,
2882 (char **)0,
2883 0);
2884 }
2885 }
2886 else
2887 x = PyOS_strtol((char *)s, (char **)&end, 0);
2888 if (*end == '\0') {
2889 if (errno != 0)
2890 return PyLong_FromString((char *)s, (char **)0, 0);
2891 return PyInt_FromLong(x);
2892 }
2893 /* XXX Huge floats may silently fail */
2894#ifndef WITHOUT_COMPLEX
2895 if (imflag) {
2896 c.real = 0.;
2897 PyFPE_START_PROTECT("atof", return 0)
2898 c.imag = atof(s);
2899 PyFPE_END_PROTECT(c)
2900 return PyComplex_FromCComplex(c);
2901 }
2902 else
2903#endif
2904 {
2905 PyFPE_START_PROTECT("atof", return 0)
2906 dx = atof(s);
2907 PyFPE_END_PROTECT(dx)
2908 return PyFloat_FromDouble(dx);
2909 }
2910}
2911
2912static PyObject *
2913decode_utf8(const char **sPtr, const char *end, char* encoding)
2914{
2915#ifndef Py_USING_UNICODE
2916 Py_FatalError("decode_utf8 should not be called in this build.");
2917 return NULL;
2918#else
2919 PyObject *u, *v;
2920 char *s, *t;
2921 t = s = (char *)*sPtr;
2922 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2923 while (s < end && (*s & 0x80)) s++;
2924 *sPtr = s;
2925 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2926 if (u == NULL)
2927 return NULL;
2928 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2929 Py_DECREF(u);
2930 return v;
2931#endif
2932}
2933
2934static PyObject *
2935decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2936{
2937 PyObject *v, *u;
2938 char *buf;
2939 char *p;
2940 const char *end;
2941 if (encoding == NULL) {
2942 buf = (char *)s;
2943 u = NULL;
2944 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2945 buf = (char *)s;
2946 u = NULL;
2947 } else {
2948 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2949 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2950 if (u == NULL)
2951 return NULL;
2952 p = buf = PyString_AsString(u);
2953 end = s + len;
2954 while (s < end) {
2955 if (*s == '\\') {
2956 *p++ = *s++;
2957 if (*s & 0x80) {
2958 strcpy(p, "u005c");
2959 p += 5;
2960 }
2961 }
2962 if (*s & 0x80) { /* XXX inefficient */
2963 PyObject *w;
2964 char *r;
2965 int rn, i;
2966 w = decode_utf8(&s, end, "utf-16-be");
2967 if (w == NULL) {
2968 Py_DECREF(u);
2969 return NULL;
2970 }
2971 r = PyString_AsString(w);
2972 rn = PyString_Size(w);
2973 assert(rn % 2 == 0);
2974 for (i = 0; i < rn; i += 2) {
2975 sprintf(p, "\\u%02x%02x",
2976 r[i + 0] & 0xFF,
2977 r[i + 1] & 0xFF);
2978 p += 6;
2979 }
2980 Py_DECREF(w);
2981 } else {
2982 *p++ = *s++;
2983 }
2984 }
2985 len = p - buf;
2986 s = buf;
2987 }
2988 if (rawmode)
2989 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
2990 else
2991 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
2992 Py_XDECREF(u);
2993 return v;
2994}
2995
2996/* s is a Python string literal, including the bracketing quote characters,
2997 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
2998 * parsestr parses it, and returns the decoded Python string object.
2999 */
3000static PyObject *
3001parsestr(const char *s, const char *encoding)
3002{
3003 PyObject *v;
3004 size_t len;
3005 int quote = *s;
3006 int rawmode = 0;
3007 int need_encoding;
3008 int unicode = 0;
3009
3010 if (isalpha(quote) || quote == '_') {
3011 if (quote == 'u' || quote == 'U') {
3012 quote = *++s;
3013 unicode = 1;
3014 }
3015 if (quote == 'r' || quote == 'R') {
3016 quote = *++s;
3017 rawmode = 1;
3018 }
3019 }
3020 if (quote != '\'' && quote != '\"') {
3021 PyErr_BadInternalCall();
3022 return NULL;
3023 }
3024 s++;
3025 len = strlen(s);
3026 if (len > INT_MAX) {
3027 PyErr_SetString(PyExc_OverflowError,
3028 "string to parse is too long");
3029 return NULL;
3030 }
3031 if (s[--len] != quote) {
3032 PyErr_BadInternalCall();
3033 return NULL;
3034 }
3035 if (len >= 4 && s[0] == quote && s[1] == quote) {
3036 s += 2;
3037 len -= 2;
3038 if (s[--len] != quote || s[--len] != quote) {
3039 PyErr_BadInternalCall();
3040 return NULL;
3041 }
3042 }
3043#ifdef Py_USING_UNICODE
3044 if (unicode || Py_UnicodeFlag) {
3045 return decode_unicode(s, len, rawmode, encoding);
3046 }
3047#endif
3048 need_encoding = (encoding != NULL &&
3049 strcmp(encoding, "utf-8") != 0 &&
3050 strcmp(encoding, "iso-8859-1") != 0);
3051 if (rawmode || strchr(s, '\\') == NULL) {
3052 if (need_encoding) {
3053#ifndef Py_USING_UNICODE
3054 /* This should not happen - we never see any other
3055 encoding. */
3056 Py_FatalError("cannot deal with encodings in this build.");
3057#else
3058 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
3059 if (u == NULL)
3060 return NULL;
3061 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3062 Py_DECREF(u);
3063 return v;
3064#endif
3065 } else {
3066 return PyString_FromStringAndSize(s, len);
3067 }
3068 }
3069
3070 v = PyString_DecodeEscape(s, len, NULL, unicode,
3071 need_encoding ? encoding : NULL);
3072 return v;
3073}
3074
3075/* Build a Python string object out of a STRING atom. This takes care of
3076 * compile-time literal catenation, calling parsestr() on each piece, and
3077 * pasting the intermediate results together.
3078 */
3079static PyObject *
3080parsestrplus(struct compiling *c, const node *n)
3081{
3082 PyObject *v;
3083 int i;
3084 REQ(CHILD(n, 0), STRING);
3085 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3086 /* String literal concatenation */
3087 for (i = 1; i < NCH(n); i++) {
3088 PyObject *s;
3089 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3090 if (s == NULL)
3091 goto onError;
3092 if (PyString_Check(v) && PyString_Check(s)) {
3093 PyString_ConcatAndDel(&v, s);
3094 if (v == NULL)
3095 goto onError;
3096 }
3097#ifdef Py_USING_UNICODE
3098 else {
3099 PyObject *temp;
3100 temp = PyUnicode_Concat(v, s);
3101 Py_DECREF(s);
3102 if (temp == NULL)
3103 goto onError;
3104 Py_DECREF(v);
3105 v = temp;
3106 }
3107#endif
3108 }
3109 }
3110 return v;
3111
3112 onError:
3113 Py_XDECREF(v);
3114 return NULL;
3115}