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