blob: 0506b47a3644c39a84322910f00c00f14ef93a7b [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);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002181 REQ(n, dotted_as_names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 aliases = asdl_seq_new((NCH(n) + 1) / 2);
2183 if (!aliases)
2184 return NULL;
2185 for (i = 0; i < NCH(n); i += 2) {
2186 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2187 if (!import_alias) {
2188 asdl_seq_free(aliases);
2189 return NULL;
2190 }
2191 asdl_seq_SET(aliases, i / 2, import_alias);
2192 }
2193 return Import(aliases, LINENO(n));
2194 }
2195 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
2196 stmt_ty import;
2197 int n_children;
2198 const char *from_modules;
2199 int lineno = LINENO(n);
2200 alias_ty mod = alias_for_import_name(CHILD(n, 1));
2201 if (!mod)
2202 return NULL;
2203
2204 /* XXX this needs to be cleaned up */
2205
2206 from_modules = STR(CHILD(n, 3));
2207 if (!from_modules) {
2208 n = CHILD(n, 3); /* from ... import x, y, z */
2209 if (NCH(n) % 2 == 0) {
2210 /* it ends with a comma, not valid but the parser allows it */
2211 ast_error(n, "trailing comma not allowed without"
2212 " surrounding parentheses");
2213 return NULL;
2214 }
2215 }
2216 else if (from_modules[0] == '*') {
2217 n = CHILD(n, 3); /* from ... import * */
2218 }
2219 else if (from_modules[0] == '(')
2220 n = CHILD(n, 4); /* from ... import (x, y, z) */
2221 else
2222 return NULL;
2223
2224 n_children = NCH(n);
2225 if (from_modules && from_modules[0] == '*')
2226 n_children = 1;
2227
2228 aliases = asdl_seq_new((n_children + 1) / 2);
2229 if (!aliases) {
2230 free_alias(mod);
2231 return NULL;
2232 }
2233
2234 /* handle "from ... import *" special b/c there's no children */
2235 if (from_modules && from_modules[0] == '*') {
2236 alias_ty import_alias = alias_for_import_name(n);
2237 if (!import_alias) {
2238 asdl_seq_free(aliases);
2239 free_alias(mod);
2240 return NULL;
2241 }
2242 asdl_seq_APPEND(aliases, import_alias);
2243 }
2244
2245 for (i = 0; i < NCH(n); i += 2) {
2246 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2247 if (!import_alias) {
2248 asdl_seq_free(aliases);
2249 free_alias(mod);
2250 return NULL;
2251 }
2252 asdl_seq_APPEND(aliases, import_alias);
2253 }
2254 Py_INCREF(mod->name);
2255 import = ImportFrom(mod->name, aliases, lineno);
2256 free_alias(mod);
2257 return import;
2258 }
2259 PyErr_Format(PyExc_Exception,
2260 "unknown import statement: starts with command '%s'",
2261 STR(CHILD(n, 0)));
2262 return NULL;
2263}
2264
2265static stmt_ty
2266ast_for_global_stmt(struct compiling *c, const node *n)
2267{
2268 /* global_stmt: 'global' NAME (',' NAME)* */
2269 identifier name;
2270 asdl_seq *s;
2271 int i;
2272
2273 REQ(n, global_stmt);
2274 s = asdl_seq_new(NCH(n) / 2);
2275 if (!s)
2276 return NULL;
2277 for (i = 1; i < NCH(n); i += 2) {
2278 name = NEW_IDENTIFIER(CHILD(n, i));
2279 if (!name) {
2280 asdl_seq_free(s);
2281 return NULL;
2282 }
2283 asdl_seq_SET(s, i / 2, name);
2284 }
2285 return Global(s, LINENO(n));
2286}
2287
2288static stmt_ty
2289ast_for_exec_stmt(struct compiling *c, const node *n)
2290{
2291 expr_ty expr1, globals = NULL, locals = NULL;
2292 int n_children = NCH(n);
2293 if (n_children != 2 && n_children != 4 && n_children != 6) {
2294 PyErr_Format(PyExc_Exception,
2295 "poorly formed 'exec' statement: %d parts to statement",
2296 n_children);
2297 return NULL;
2298 }
2299
2300 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2301 REQ(n, exec_stmt);
2302 expr1 = ast_for_expr(c, CHILD(n, 1));
2303 if (!expr1)
2304 return NULL;
2305 if (n_children >= 4) {
2306 globals = ast_for_expr(c, CHILD(n, 3));
2307 if (!globals)
2308 return NULL;
2309 }
2310 if (n_children == 6) {
2311 locals = ast_for_expr(c, CHILD(n, 5));
2312 if (!locals)
2313 return NULL;
2314 }
2315
2316 return Exec(expr1, globals, locals, LINENO(n));
2317}
2318
2319static stmt_ty
2320ast_for_assert_stmt(struct compiling *c, const node *n)
2321{
2322 /* assert_stmt: 'assert' test [',' test] */
2323 REQ(n, assert_stmt);
2324 if (NCH(n) == 2) {
2325 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2326 if (!expression)
2327 return NULL;
2328 return Assert(expression, NULL, LINENO(n));
2329 }
2330 else if (NCH(n) == 4) {
2331 expr_ty expr1, expr2;
2332
2333 expr1 = ast_for_expr(c, CHILD(n, 1));
2334 if (!expr1)
2335 return NULL;
2336 expr2 = ast_for_expr(c, CHILD(n, 3));
2337 if (!expr2)
2338 return NULL;
2339
2340 return Assert(expr1, expr2, LINENO(n));
2341 }
2342 PyErr_Format(PyExc_Exception,
2343 "improper number of parts to 'assert' statement: %d",
2344 NCH(n));
2345 return NULL;
2346}
2347
2348static asdl_seq *
2349ast_for_suite(struct compiling *c, const node *n)
2350{
2351 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2352 asdl_seq *seq = NULL;
2353 stmt_ty s;
2354 int i, total, num, end, pos = 0;
2355 node *ch;
2356
2357 REQ(n, suite);
2358
2359 total = num_stmts(n);
2360 seq = asdl_seq_new(total);
2361 if (!seq)
2362 return NULL;
2363 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2364 n = CHILD(n, 0);
2365 /* simple_stmt always ends with a NEWLINE,
2366 and may have a trailing SEMI
2367 */
2368 end = NCH(n) - 1;
2369 if (TYPE(CHILD(n, end - 1)) == SEMI)
2370 end--;
2371 /* loop by 2 to skip semi-colons */
2372 for (i = 0; i < end; i += 2) {
2373 ch = CHILD(n, i);
2374 s = ast_for_stmt(c, ch);
2375 if (!s)
2376 goto error;
2377 asdl_seq_SET(seq, pos++, s);
2378 }
2379 }
2380 else {
2381 for (i = 2; i < (NCH(n) - 1); i++) {
2382 ch = CHILD(n, i);
2383 REQ(ch, stmt);
2384 num = num_stmts(ch);
2385 if (num == 1) {
2386 /* small_stmt or compound_stmt with only one child */
2387 s = ast_for_stmt(c, ch);
2388 if (!s)
2389 goto error;
2390 asdl_seq_SET(seq, pos++, s);
2391 }
2392 else {
2393 int j;
2394 ch = CHILD(ch, 0);
2395 REQ(ch, simple_stmt);
2396 for (j = 0; j < NCH(ch); j += 2) {
2397 s = ast_for_stmt(c, CHILD(ch, j));
2398 if (!s)
2399 goto error;
2400 asdl_seq_SET(seq, pos++, s);
2401 }
2402 }
2403 }
2404 }
2405 assert(pos == seq->size);
2406 return seq;
2407 error:
2408 if (seq)
2409 asdl_seq_free(seq);
2410 return NULL;
2411}
2412
2413static stmt_ty
2414ast_for_if_stmt(struct compiling *c, const node *n)
2415{
2416 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2417 ['else' ':' suite]
2418 */
2419 char *s;
2420
2421 REQ(n, if_stmt);
2422
2423 if (NCH(n) == 4) {
2424 expr_ty expression;
2425 asdl_seq *suite_seq;
2426
2427 expression = ast_for_expr(c, CHILD(n, 1));
2428 if (!expression)
2429 return NULL;
2430 suite_seq = ast_for_suite(c, CHILD(n, 3));
2431 if (!suite_seq)
2432 return NULL;
2433
2434 return If(expression, suite_seq, NULL, LINENO(n));
2435 }
2436 s = STR(CHILD(n, 4));
2437 /* s[2], the third character in the string, will be
2438 's' for el_s_e, or
2439 'i' for el_i_f
2440 */
2441 if (s[2] == 's') {
2442 expr_ty expression;
2443 asdl_seq *seq1, *seq2;
2444
2445 expression = ast_for_expr(c, CHILD(n, 1));
2446 if (!expression)
2447 return NULL;
2448 seq1 = ast_for_suite(c, CHILD(n, 3));
2449 if (!seq1)
2450 return NULL;
2451 seq2 = ast_for_suite(c, CHILD(n, 6));
2452 if (!seq2)
2453 return NULL;
2454
2455 return If(expression, seq1, seq2, LINENO(n));
2456 }
2457 else if (s[2] == 'i') {
2458 int i, n_elif, has_else = 0;
2459 asdl_seq *orelse = NULL;
2460 n_elif = NCH(n) - 4;
2461 /* must reference the child n_elif+1 since 'else' token is third,
2462 not fourth, child from the end. */
2463 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2464 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2465 has_else = 1;
2466 n_elif -= 3;
2467 }
2468 n_elif /= 4;
2469
2470 if (has_else) {
2471 expr_ty expression;
2472 asdl_seq *seq1, *seq2;
2473
2474 orelse = asdl_seq_new(1);
2475 if (!orelse)
2476 return NULL;
2477 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2478 if (!expression) {
2479 asdl_seq_free(orelse);
2480 return NULL;
2481 }
2482 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2483 if (!seq1) {
2484 asdl_seq_free(orelse);
2485 return NULL;
2486 }
2487 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2488 if (!seq2) {
2489 asdl_seq_free(orelse);
2490 return NULL;
2491 }
2492
2493 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
2494 LINENO(CHILD(n, NCH(n) - 6))));
2495 /* the just-created orelse handled the last elif */
2496 n_elif--;
2497 }
2498 else
2499 orelse = NULL;
2500
2501 for (i = 0; i < n_elif; i++) {
2502 int off = 5 + (n_elif - i - 1) * 4;
2503 expr_ty expression;
2504 asdl_seq *suite_seq;
2505 asdl_seq *new = asdl_seq_new(1);
2506 if (!new)
2507 return NULL;
2508 expression = ast_for_expr(c, CHILD(n, off));
2509 if (!expression) {
2510 asdl_seq_free(new);
2511 return NULL;
2512 }
2513 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2514 if (!suite_seq) {
2515 asdl_seq_free(new);
2516 return NULL;
2517 }
2518
2519 asdl_seq_SET(new, 0,
2520 If(expression, suite_seq, orelse,
2521 LINENO(CHILD(n, off))));
2522 orelse = new;
2523 }
2524 return If(ast_for_expr(c, CHILD(n, 1)),
2525 ast_for_suite(c, CHILD(n, 3)),
2526 orelse, LINENO(n));
2527 }
2528 else {
2529 PyErr_Format(PyExc_Exception,
2530 "unexpected token in 'if' statement: %s", s);
2531 return NULL;
2532 }
2533}
2534
2535static stmt_ty
2536ast_for_while_stmt(struct compiling *c, const node *n)
2537{
2538 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2539 REQ(n, while_stmt);
2540
2541 if (NCH(n) == 4) {
2542 expr_ty expression;
2543 asdl_seq *suite_seq;
2544
2545 expression = ast_for_expr(c, CHILD(n, 1));
2546 if (!expression)
2547 return NULL;
2548 suite_seq = ast_for_suite(c, CHILD(n, 3));
2549 if (!suite_seq)
2550 return NULL;
2551 return While(expression, suite_seq, NULL, LINENO(n));
2552 }
2553 else if (NCH(n) == 7) {
2554 expr_ty expression;
2555 asdl_seq *seq1, *seq2;
2556
2557 expression = ast_for_expr(c, CHILD(n, 1));
2558 if (!expression)
2559 return NULL;
2560 seq1 = ast_for_suite(c, CHILD(n, 3));
2561 if (!seq1)
2562 return NULL;
2563 seq2 = ast_for_suite(c, CHILD(n, 6));
2564 if (!seq2)
2565 return NULL;
2566
2567 return While(expression, seq1, seq2, LINENO(n));
2568 }
2569 else {
2570 PyErr_Format(PyExc_Exception,
2571 "wrong number of tokens for 'while' statement: %d",
2572 NCH(n));
2573 return NULL;
2574 }
2575}
2576
2577static stmt_ty
2578ast_for_for_stmt(struct compiling *c, const node *n)
2579{
2580 asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL;
2581 expr_ty expression;
2582 expr_ty target;
2583 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2584 REQ(n, for_stmt);
2585
2586 if (NCH(n) == 9) {
2587 seq = ast_for_suite(c, CHILD(n, 8));
2588 if (!seq)
2589 return NULL;
2590 }
2591
2592 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
2593 if (!_target)
2594 return NULL;
2595 if (asdl_seq_LEN(_target) == 1) {
2596 target = asdl_seq_GET(_target, 0);
2597 asdl_seq_free(_target);
2598 }
2599 else
2600 target = Tuple(_target, Store, LINENO(n));
2601
2602 expression = ast_for_testlist(c, CHILD(n, 3), 0);
2603 if (!expression)
2604 return NULL;
2605 suite_seq = ast_for_suite(c, CHILD(n, 5));
2606 if (!suite_seq)
2607 return NULL;
2608
2609 return For(target, expression, suite_seq, seq, LINENO(n));
2610}
2611
2612static excepthandler_ty
2613ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2614{
2615 /* except_clause: 'except' [test [',' test]] */
2616 REQ(exc, except_clause);
2617 REQ(body, suite);
2618
2619 if (NCH(exc) == 1) {
2620 asdl_seq *suite_seq = ast_for_suite(c, body);
2621 if (!suite_seq)
2622 return NULL;
2623
2624 return excepthandler(NULL, NULL, suite_seq);
2625 }
2626 else if (NCH(exc) == 2) {
2627 expr_ty expression;
2628 asdl_seq *suite_seq;
2629
2630 expression = ast_for_expr(c, CHILD(exc, 1));
2631 if (!expression)
2632 return NULL;
2633 suite_seq = ast_for_suite(c, body);
2634 if (!suite_seq)
2635 return NULL;
2636
2637 return excepthandler(expression, NULL, suite_seq);
2638 }
2639 else if (NCH(exc) == 4) {
2640 asdl_seq *suite_seq;
2641 expr_ty expression;
2642 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2643 if (!e)
2644 return NULL;
2645 if (!set_context(e, Store, CHILD(exc, 3)))
2646 return NULL;
2647 expression = ast_for_expr(c, CHILD(exc, 1));
2648 if (!expression)
2649 return NULL;
2650 suite_seq = ast_for_suite(c, body);
2651 if (!suite_seq)
2652 return NULL;
2653
2654 return excepthandler(expression, e, suite_seq);
2655 }
2656 else {
2657 PyErr_Format(PyExc_Exception,
2658 "wrong number of children for 'except' clause: %d",
2659 NCH(exc));
2660 return NULL;
2661 }
2662}
2663
2664static stmt_ty
2665ast_for_try_stmt(struct compiling *c, const node *n)
2666{
2667 REQ(n, try_stmt);
2668
2669 if (TYPE(CHILD(n, 3)) == NAME) {/* must be 'finally' */
2670 /* try_stmt: 'try' ':' suite 'finally' ':' suite) */
2671 asdl_seq *s1, *s2;
2672 s1 = ast_for_suite(c, CHILD(n, 2));
2673 if (!s1)
2674 return NULL;
2675 s2 = ast_for_suite(c, CHILD(n, 5));
2676 if (!s2)
2677 return NULL;
2678
2679 return TryFinally(s1, s2, LINENO(n));
2680 }
2681 else if (TYPE(CHILD(n, 3)) == except_clause) {
2682 /* try_stmt: ('try' ':' suite (except_clause ':' suite)+
2683 ['else' ':' suite]
2684 */
2685 asdl_seq *suite_seq1, *suite_seq2;
2686 asdl_seq *handlers;
2687 int i, has_else = 0, n_except = NCH(n) - 3;
2688 if (TYPE(CHILD(n, NCH(n) - 3)) == NAME) {
2689 has_else = 1;
2690 n_except -= 3;
2691 }
2692 n_except /= 3;
2693 handlers = asdl_seq_new(n_except);
2694 if (!handlers)
2695 return NULL;
2696 for (i = 0; i < n_except; i++) {
2697 excepthandler_ty e = ast_for_except_clause(c,
2698 CHILD(n, 3 + i * 3),
2699 CHILD(n, 5 + i * 3));
2700 if (!e)
2701 return NULL;
2702 asdl_seq_SET(handlers, i, e);
2703 }
2704
2705 suite_seq1 = ast_for_suite(c, CHILD(n, 2));
2706 if (!suite_seq1)
2707 return NULL;
2708 if (has_else) {
2709 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2710 if (!suite_seq2)
2711 return NULL;
2712 }
2713 else
2714 suite_seq2 = NULL;
2715
2716 return TryExcept(suite_seq1, handlers, suite_seq2, LINENO(n));
2717 }
2718 else {
2719 PyErr_SetString(PyExc_Exception, "malformed 'try' statement");
2720 return NULL;
2721 }
2722}
2723
2724static stmt_ty
2725ast_for_classdef(struct compiling *c, const node *n)
2726{
2727 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2728 expr_ty _bases;
2729 asdl_seq *bases, *s;
2730
2731 REQ(n, classdef);
2732
2733 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2734 ast_error(n, "assignment to None");
2735 return NULL;
2736 }
2737
2738 if (NCH(n) == 4) {
2739 s = ast_for_suite(c, CHILD(n, 3));
2740 if (!s)
2741 return NULL;
2742 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2743 }
2744 /* check for empty base list */
2745 if (TYPE(CHILD(n,3)) == RPAR) {
2746 s = ast_for_suite(c, CHILD(n,5));
2747 if (!s)
2748 return NULL;
2749 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2750 }
2751
2752 /* else handle the base class list */
2753 _bases = ast_for_testlist(c, CHILD(n, 3), 0);
2754 if (!_bases)
2755 return NULL;
2756 /* XXX: I don't think we can set to diff types here, how to free???
2757
2758 Here's the allocation chain:
2759 Tuple (Python-ast.c:907)
2760 ast_for_testlist (ast.c:1782)
2761 ast_for_classdef (ast.c:2677)
2762 */
2763 if (_bases->kind == Tuple_kind)
2764 bases = _bases->v.Tuple.elts;
2765 else {
2766 bases = asdl_seq_new(1);
2767 if (!bases) {
2768 free_expr(_bases);
2769 /* XXX: free _bases */
2770 return NULL;
2771 }
2772 asdl_seq_SET(bases, 0, _bases);
2773 }
2774
2775 s = ast_for_suite(c, CHILD(n, 6));
2776 if (!s) {
2777 /* XXX: I think this free is correct, but needs to change see above */
2778 if (_bases->kind == Tuple_kind)
2779 free_expr(_bases);
2780 else {
2781 free_expr(_bases);
2782 asdl_seq_free(bases);
2783 }
2784 return NULL;
2785 }
2786 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n));
2787}
2788
2789static stmt_ty
2790ast_for_stmt(struct compiling *c, const node *n)
2791{
2792 if (TYPE(n) == stmt) {
2793 assert(NCH(n) == 1);
2794 n = CHILD(n, 0);
2795 }
2796 if (TYPE(n) == simple_stmt) {
2797 assert(num_stmts(n) == 1);
2798 n = CHILD(n, 0);
2799 }
2800 if (TYPE(n) == small_stmt) {
2801 REQ(n, small_stmt);
2802 n = CHILD(n, 0);
2803 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2804 | flow_stmt | import_stmt | global_stmt | exec_stmt
2805 | assert_stmt
2806 */
2807 switch (TYPE(n)) {
2808 case expr_stmt:
2809 return ast_for_expr_stmt(c, n);
2810 case print_stmt:
2811 return ast_for_print_stmt(c, n);
2812 case del_stmt:
2813 return ast_for_del_stmt(c, n);
2814 case pass_stmt:
2815 return Pass(LINENO(n));
2816 case flow_stmt:
2817 return ast_for_flow_stmt(c, n);
2818 case import_stmt:
2819 return ast_for_import_stmt(c, n);
2820 case global_stmt:
2821 return ast_for_global_stmt(c, n);
2822 case exec_stmt:
2823 return ast_for_exec_stmt(c, n);
2824 case assert_stmt:
2825 return ast_for_assert_stmt(c, n);
2826 default:
2827 PyErr_Format(PyExc_Exception,
2828 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2829 TYPE(n), NCH(n));
2830 return NULL;
2831 }
2832 }
2833 else {
2834 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2835 | funcdef | classdef
2836 */
2837 node *ch = CHILD(n, 0);
2838 REQ(n, compound_stmt);
2839 switch (TYPE(ch)) {
2840 case if_stmt:
2841 return ast_for_if_stmt(c, ch);
2842 case while_stmt:
2843 return ast_for_while_stmt(c, ch);
2844 case for_stmt:
2845 return ast_for_for_stmt(c, ch);
2846 case try_stmt:
2847 return ast_for_try_stmt(c, ch);
2848 case funcdef:
2849 return ast_for_funcdef(c, ch);
2850 case classdef:
2851 return ast_for_classdef(c, ch);
2852 default:
2853 PyErr_Format(PyExc_Exception,
2854 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2855 TYPE(n), NCH(n));
2856 return NULL;
2857 }
2858 }
2859}
2860
2861static PyObject *
2862parsenumber(const char *s)
2863{
2864 const char *end;
2865 long x;
2866 double dx;
2867#ifndef WITHOUT_COMPLEX
2868 Py_complex c;
2869 int imflag;
2870#endif
2871
2872 errno = 0;
2873 end = s + strlen(s) - 1;
2874#ifndef WITHOUT_COMPLEX
2875 imflag = *end == 'j' || *end == 'J';
2876#endif
2877 if (*end == 'l' || *end == 'L')
2878 return PyLong_FromString((char *)s, (char **)0, 0);
2879 if (s[0] == '0') {
2880 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2881 if (x < 0 && errno == 0) {
2882 return PyLong_FromString((char *)s,
2883 (char **)0,
2884 0);
2885 }
2886 }
2887 else
2888 x = PyOS_strtol((char *)s, (char **)&end, 0);
2889 if (*end == '\0') {
2890 if (errno != 0)
2891 return PyLong_FromString((char *)s, (char **)0, 0);
2892 return PyInt_FromLong(x);
2893 }
2894 /* XXX Huge floats may silently fail */
2895#ifndef WITHOUT_COMPLEX
2896 if (imflag) {
2897 c.real = 0.;
2898 PyFPE_START_PROTECT("atof", return 0)
2899 c.imag = atof(s);
2900 PyFPE_END_PROTECT(c)
2901 return PyComplex_FromCComplex(c);
2902 }
2903 else
2904#endif
2905 {
2906 PyFPE_START_PROTECT("atof", return 0)
2907 dx = atof(s);
2908 PyFPE_END_PROTECT(dx)
2909 return PyFloat_FromDouble(dx);
2910 }
2911}
2912
2913static PyObject *
2914decode_utf8(const char **sPtr, const char *end, char* encoding)
2915{
2916#ifndef Py_USING_UNICODE
2917 Py_FatalError("decode_utf8 should not be called in this build.");
2918 return NULL;
2919#else
2920 PyObject *u, *v;
2921 char *s, *t;
2922 t = s = (char *)*sPtr;
2923 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2924 while (s < end && (*s & 0x80)) s++;
2925 *sPtr = s;
2926 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2927 if (u == NULL)
2928 return NULL;
2929 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2930 Py_DECREF(u);
2931 return v;
2932#endif
2933}
2934
2935static PyObject *
2936decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2937{
2938 PyObject *v, *u;
2939 char *buf;
2940 char *p;
2941 const char *end;
2942 if (encoding == NULL) {
2943 buf = (char *)s;
2944 u = NULL;
2945 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2946 buf = (char *)s;
2947 u = NULL;
2948 } else {
2949 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2950 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2951 if (u == NULL)
2952 return NULL;
2953 p = buf = PyString_AsString(u);
2954 end = s + len;
2955 while (s < end) {
2956 if (*s == '\\') {
2957 *p++ = *s++;
2958 if (*s & 0x80) {
2959 strcpy(p, "u005c");
2960 p += 5;
2961 }
2962 }
2963 if (*s & 0x80) { /* XXX inefficient */
2964 PyObject *w;
2965 char *r;
2966 int rn, i;
2967 w = decode_utf8(&s, end, "utf-16-be");
2968 if (w == NULL) {
2969 Py_DECREF(u);
2970 return NULL;
2971 }
2972 r = PyString_AsString(w);
2973 rn = PyString_Size(w);
2974 assert(rn % 2 == 0);
2975 for (i = 0; i < rn; i += 2) {
2976 sprintf(p, "\\u%02x%02x",
2977 r[i + 0] & 0xFF,
2978 r[i + 1] & 0xFF);
2979 p += 6;
2980 }
2981 Py_DECREF(w);
2982 } else {
2983 *p++ = *s++;
2984 }
2985 }
2986 len = p - buf;
2987 s = buf;
2988 }
2989 if (rawmode)
2990 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
2991 else
2992 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
2993 Py_XDECREF(u);
2994 return v;
2995}
2996
2997/* s is a Python string literal, including the bracketing quote characters,
2998 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
2999 * parsestr parses it, and returns the decoded Python string object.
3000 */
3001static PyObject *
3002parsestr(const char *s, const char *encoding)
3003{
3004 PyObject *v;
3005 size_t len;
3006 int quote = *s;
3007 int rawmode = 0;
3008 int need_encoding;
3009 int unicode = 0;
3010
3011 if (isalpha(quote) || quote == '_') {
3012 if (quote == 'u' || quote == 'U') {
3013 quote = *++s;
3014 unicode = 1;
3015 }
3016 if (quote == 'r' || quote == 'R') {
3017 quote = *++s;
3018 rawmode = 1;
3019 }
3020 }
3021 if (quote != '\'' && quote != '\"') {
3022 PyErr_BadInternalCall();
3023 return NULL;
3024 }
3025 s++;
3026 len = strlen(s);
3027 if (len > INT_MAX) {
3028 PyErr_SetString(PyExc_OverflowError,
3029 "string to parse is too long");
3030 return NULL;
3031 }
3032 if (s[--len] != quote) {
3033 PyErr_BadInternalCall();
3034 return NULL;
3035 }
3036 if (len >= 4 && s[0] == quote && s[1] == quote) {
3037 s += 2;
3038 len -= 2;
3039 if (s[--len] != quote || s[--len] != quote) {
3040 PyErr_BadInternalCall();
3041 return NULL;
3042 }
3043 }
3044#ifdef Py_USING_UNICODE
3045 if (unicode || Py_UnicodeFlag) {
3046 return decode_unicode(s, len, rawmode, encoding);
3047 }
3048#endif
3049 need_encoding = (encoding != NULL &&
3050 strcmp(encoding, "utf-8") != 0 &&
3051 strcmp(encoding, "iso-8859-1") != 0);
3052 if (rawmode || strchr(s, '\\') == NULL) {
3053 if (need_encoding) {
3054#ifndef Py_USING_UNICODE
3055 /* This should not happen - we never see any other
3056 encoding. */
3057 Py_FatalError("cannot deal with encodings in this build.");
3058#else
3059 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
3060 if (u == NULL)
3061 return NULL;
3062 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3063 Py_DECREF(u);
3064 return v;
3065#endif
3066 } else {
3067 return PyString_FromStringAndSize(s, len);
3068 }
3069 }
3070
3071 v = PyString_DecodeEscape(s, len, NULL, unicode,
3072 need_encoding ? encoding : NULL);
3073 return v;
3074}
3075
3076/* Build a Python string object out of a STRING atom. This takes care of
3077 * compile-time literal catenation, calling parsestr() on each piece, and
3078 * pasting the intermediate results together.
3079 */
3080static PyObject *
3081parsestrplus(struct compiling *c, const node *n)
3082{
3083 PyObject *v;
3084 int i;
3085 REQ(CHILD(n, 0), STRING);
3086 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3087 /* String literal concatenation */
3088 for (i = 1; i < NCH(n); i++) {
3089 PyObject *s;
3090 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3091 if (s == NULL)
3092 goto onError;
3093 if (PyString_Check(v) && PyString_Check(s)) {
3094 PyString_ConcatAndDel(&v, s);
3095 if (v == NULL)
3096 goto onError;
3097 }
3098#ifdef Py_USING_UNICODE
3099 else {
3100 PyObject *temp;
3101 temp = PyUnicode_Concat(v, s);
3102 Py_DECREF(s);
3103 if (temp == NULL)
3104 goto onError;
3105 Py_DECREF(v);
3106 v = temp;
3107 }
3108#endif
3109 }
3110 }
3111 return v;
3112
3113 onError:
3114 Py_XDECREF(v);
3115 return NULL;
3116}