blob: e483650a7a8510f9ff5f22019ba8da643baf93d8 [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:
451 PyErr_Format(PyExc_Exception, "invalid augassign: %s", STR(n));
452 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:
484 PyErr_Format(PyExc_Exception, "invalid comp_op: %s",
485 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:
498 PyErr_Format(PyExc_Exception, "invalid comp_op: %s %s",
499 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
500 return 0;
501 }
502 }
503 PyErr_Format(PyExc_Exception, "invalid comp_op: has %d children",
504 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:
672 PyErr_Format(PyExc_Exception,
673 "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:
1312 PyErr_Format(PyExc_Exception, "unhandled atom %d",
1313 TYPE(ch));
1314 return NULL;
1315 }
1316}
1317
1318static slice_ty
1319ast_for_slice(struct compiling *c, const node *n)
1320{
1321 node *ch;
1322 expr_ty lower = NULL, upper = NULL, step = NULL;
1323
1324 REQ(n, subscript);
1325
1326 /*
1327 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1328 sliceop: ':' [test]
1329 */
1330 ch = CHILD(n, 0);
1331 if (TYPE(ch) == DOT)
1332 return Ellipsis();
1333
1334 if (NCH(n) == 1 && TYPE(ch) == test) {
1335 /* 'step' variable hold no significance in terms of being used over
1336 other vars */
1337 step = ast_for_expr(c, ch);
1338 if (!step)
1339 return NULL;
1340
1341 return Index(step);
1342 }
1343
1344 if (TYPE(ch) == test) {
1345 lower = ast_for_expr(c, ch);
1346 if (!lower)
1347 return NULL;
1348 }
1349
1350 /* If there's an upper bound it's in the second or third position. */
1351 if (TYPE(ch) == COLON) {
1352 if (NCH(n) > 1) {
1353 node *n2 = CHILD(n, 1);
1354
1355 if (TYPE(n2) == test) {
1356 upper = ast_for_expr(c, n2);
1357 if (!upper)
1358 return NULL;
1359 }
1360 }
1361 } else if (NCH(n) > 2) {
1362 node *n2 = CHILD(n, 2);
1363
1364 if (TYPE(n2) == test) {
1365 upper = ast_for_expr(c, n2);
1366 if (!upper)
1367 return NULL;
1368 }
1369 }
1370
1371 ch = CHILD(n, NCH(n) - 1);
1372 if (TYPE(ch) == sliceop) {
1373 if (NCH(ch) == 1)
1374 /* XXX: If only 1 child, then should just be a colon. Should we
1375 just skip assigning and just get to the return? */
1376 ch = CHILD(ch, 0);
1377 else
1378 ch = CHILD(ch, 1);
1379 if (TYPE(ch) == test) {
1380 step = ast_for_expr(c, ch);
1381 if (!step)
1382 return NULL;
1383 }
1384 }
1385
1386 return Slice(lower, upper, step);
1387}
1388
1389static expr_ty
1390ast_for_binop(struct compiling *c, const node *n)
1391{
1392 /* Must account for a sequence of expressions.
1393 How should A op B op C by represented?
1394 BinOp(BinOp(A, op, B), op, C).
1395 */
1396
1397 int i, nops;
1398 expr_ty expr1, expr2, result;
1399 operator_ty operator;
1400
1401 expr1 = ast_for_expr(c, CHILD(n, 0));
1402 if (!expr1)
1403 return NULL;
1404
1405 expr2 = ast_for_expr(c, CHILD(n, 2));
1406 if (!expr2)
1407 return NULL;
1408
1409 operator = get_operator(CHILD(n, 1));
1410 if (!operator)
1411 return NULL;
1412
1413 result = BinOp(expr1, operator, expr2, LINENO(n));
1414 if (!result)
1415 return NULL;
1416
1417 nops = (NCH(n) - 1) / 2;
1418 for (i = 1; i < nops; i++) {
1419 expr_ty tmp_result, tmp;
1420 const node* next_oper = CHILD(n, i * 2 + 1);
1421
1422 operator = get_operator(next_oper);
1423 if (!operator)
1424 return NULL;
1425
1426 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1427 if (!tmp)
1428 return NULL;
1429
1430 tmp_result = BinOp(result, operator, tmp,
1431 LINENO(next_oper));
1432 if (!tmp)
1433 return NULL;
1434 result = tmp_result;
1435 }
1436 return result;
1437}
1438
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001439static expr_ty
1440ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1441{
1442 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1443 expr_ty e;
1444 REQ(n, trailer);
1445 if (TYPE(CHILD(n, 0)) == LPAR) {
1446 if (NCH(n) == 2)
1447 e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n));
1448 else
1449 e = ast_for_call(c, CHILD(n, 1), left_expr);
1450 }
1451 else if (TYPE(CHILD(n, 0)) == LSQB) {
1452 REQ(CHILD(n, 2), RSQB);
1453 n = CHILD(n, 1);
1454 if (NCH(n) <= 2) {
1455 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1456 if (!slc)
1457 return NULL;
1458 e = Subscript(left_expr, slc, Load, LINENO(n));
1459 if (!e) {
1460 free_slice(slc);
1461 return NULL;
1462 }
1463 }
1464 else {
1465 int j;
1466 slice_ty slc;
1467 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2);
1468 if (!slices)
1469 return NULL;
1470 for (j = 0; j < NCH(n); j += 2) {
1471 slc = ast_for_slice(c, CHILD(n, j));
1472 if (!slc) {
1473 asdl_seq_free(slices);
1474 return NULL;
1475 }
1476 asdl_seq_SET(slices, j / 2, slc);
1477 }
1478 e = Subscript(left_expr, ExtSlice(slices), Load, LINENO(n));
1479 if (!e) {
1480 asdl_seq_free(slices);
1481 return NULL;
1482 }
1483 }
1484 }
1485 else {
1486 assert(TYPE(CHILD(n, 0)) == DOT);
1487 e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n));
1488 }
1489 return e;
1490}
1491
1492static expr_ty
1493ast_for_power(struct compiling *c, const node *n)
1494{
1495 /* power: atom trailer* ('**' factor)*
1496 */
1497 int i;
1498 expr_ty e, tmp;
1499 REQ(n, power);
1500 e = ast_for_atom(c, CHILD(n, 0));
1501 if (!e)
1502 return NULL;
1503 if (NCH(n) == 1)
1504 return e;
1505 for (i = 1; i < NCH(n); i++) {
1506 node *ch = CHILD(n, i);
1507 if (TYPE(ch) != trailer)
1508 break;
1509 tmp = ast_for_trailer(c, ch, e);
1510 if (!tmp) {
1511 free_expr(e);
1512 return NULL;
1513 }
1514 e = tmp;
1515 }
1516 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1517 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1518 if (!f) {
1519 free_expr(e);
1520 return NULL;
1521 }
1522 tmp = BinOp(e, Pow, f, LINENO(n));
1523 if (!tmp) {
1524 free_expr(e);
1525 return NULL;
1526 }
1527 e = tmp;
1528 }
1529 return e;
1530}
1531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532/* Do not name a variable 'expr'! Will cause a compile error.
1533*/
1534
1535static expr_ty
1536ast_for_expr(struct compiling *c, const node *n)
1537{
1538 /* handle the full range of simple expressions
1539 test: and_test ('or' and_test)* | lambdef
1540 and_test: not_test ('and' not_test)*
1541 not_test: 'not' not_test | comparison
1542 comparison: expr (comp_op expr)*
1543 expr: xor_expr ('|' xor_expr)*
1544 xor_expr: and_expr ('^' and_expr)*
1545 and_expr: shift_expr ('&' shift_expr)*
1546 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1547 arith_expr: term (('+'|'-') term)*
1548 term: factor (('*'|'/'|'%'|'//') factor)*
1549 factor: ('+'|'-'|'~') factor | power
1550 power: atom trailer* ('**' factor)*
1551 */
1552
1553 asdl_seq *seq;
1554 int i;
1555
1556 loop:
1557 switch (TYPE(n)) {
1558 case test:
1559 if (TYPE(CHILD(n, 0)) == lambdef)
1560 return ast_for_lambdef(c, CHILD(n, 0));
1561 /* Fall through to and_test */
1562 case and_test:
1563 if (NCH(n) == 1) {
1564 n = CHILD(n, 0);
1565 goto loop;
1566 }
1567 seq = asdl_seq_new((NCH(n) + 1) / 2);
1568 if (!seq)
1569 return NULL;
1570 for (i = 0; i < NCH(n); i += 2) {
1571 expr_ty e = ast_for_expr(c, CHILD(n, i));
1572 if (!e)
1573 return NULL;
1574 asdl_seq_SET(seq, i / 2, e);
1575 }
1576 if (!strcmp(STR(CHILD(n, 1)), "and"))
1577 return BoolOp(And, seq, LINENO(n));
1578 else {
1579 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1580 return BoolOp(Or, seq, LINENO(n));
1581 }
1582 break;
1583 case not_test:
1584 if (NCH(n) == 1) {
1585 n = CHILD(n, 0);
1586 goto loop;
1587 }
1588 else {
1589 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1590 if (!expression)
1591 return NULL;
1592
1593 return UnaryOp(Not, expression, LINENO(n));
1594 }
1595 case comparison:
1596 if (NCH(n) == 1) {
1597 n = CHILD(n, 0);
1598 goto loop;
1599 }
1600 else {
1601 expr_ty expression;
1602 asdl_seq *ops, *cmps;
1603 ops = asdl_seq_new(NCH(n) / 2);
1604 if (!ops)
1605 return NULL;
1606 cmps = asdl_seq_new(NCH(n) / 2);
1607 if (!cmps) {
1608 asdl_seq_free(ops);
1609 return NULL;
1610 }
1611 for (i = 1; i < NCH(n); i += 2) {
1612 /* XXX cmpop_ty is just an enum */
1613 cmpop_ty operator;
1614
1615 operator = ast_for_comp_op(CHILD(n, i));
1616 if (!operator)
1617 return NULL;
1618
1619 expression = ast_for_expr(c, CHILD(n, i + 1));
1620 if (!expression)
1621 return NULL;
1622
1623 asdl_seq_SET(ops, i / 2, (void *)operator);
1624 asdl_seq_SET(cmps, i / 2, expression);
1625 }
1626 expression = ast_for_expr(c, CHILD(n, 0));
1627 if (!expression)
1628 return NULL;
1629
1630 return Compare(expression, ops, cmps, LINENO(n));
1631 }
1632 break;
1633
1634 /* The next five cases all handle BinOps. The main body of code
1635 is the same in each case, but the switch turned inside out to
1636 reuse the code for each type of operator.
1637 */
1638 case expr:
1639 case xor_expr:
1640 case and_expr:
1641 case shift_expr:
1642 case arith_expr:
1643 case term:
1644 if (NCH(n) == 1) {
1645 n = CHILD(n, 0);
1646 goto loop;
1647 }
1648 return ast_for_binop(c, n);
1649 case yield_expr: {
1650 expr_ty exp = NULL;
1651 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001652 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 if (!exp)
1654 return NULL;
1655 }
1656 return Yield(exp, LINENO(n));
1657 }
1658 case factor: {
1659 expr_ty expression;
1660
1661 if (NCH(n) == 1) {
1662 n = CHILD(n, 0);
1663 goto loop;
1664 }
1665
1666 expression = ast_for_expr(c, CHILD(n, 1));
1667 if (!expression)
1668 return NULL;
1669
1670 switch (TYPE(CHILD(n, 0))) {
1671 case PLUS:
1672 return UnaryOp(UAdd, expression, LINENO(n));
1673 case MINUS:
1674 return UnaryOp(USub, expression, LINENO(n));
1675 case TILDE:
1676 return UnaryOp(Invert, expression, LINENO(n));
1677 }
1678 break;
1679 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001680 case power:
1681 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 default:
1683 abort();
1684 PyErr_Format(PyExc_Exception, "unhandled expr: %d", TYPE(n));
1685 return NULL;
1686 }
1687 /* should never get here */
1688 return NULL;
1689}
1690
1691static expr_ty
1692ast_for_call(struct compiling *c, const node *n, expr_ty func)
1693{
1694 /*
1695 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1696 | '**' test)
1697 argument: [test '='] test [gen_for] # Really [keyword '='] test
1698 */
1699
1700 int i, nargs, nkeywords, ngens;
1701 asdl_seq *args = NULL;
1702 asdl_seq *keywords = NULL;
1703 expr_ty vararg = NULL, kwarg = NULL;
1704
1705 REQ(n, arglist);
1706
1707 nargs = 0;
1708 nkeywords = 0;
1709 ngens = 0;
1710 for (i = 0; i < NCH(n); i++) {
1711 node *ch = CHILD(n, i);
1712 if (TYPE(ch) == argument) {
1713 if (NCH(ch) == 1)
1714 nargs++;
1715 else if (TYPE(CHILD(ch, 1)) == gen_for)
1716 ngens++;
1717 else
1718 nkeywords++;
1719 }
1720 }
1721 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1722 ast_error(n, "Generator expression must be parenthesised "
1723 "if not sole argument");
1724 return NULL;
1725 }
1726
1727 if (nargs + nkeywords + ngens > 255) {
1728 ast_error(n, "more than 255 arguments");
1729 return NULL;
1730 }
1731
1732 args = asdl_seq_new(nargs + ngens);
1733 if (!args)
1734 goto error;
1735 keywords = asdl_seq_new(nkeywords);
1736 if (!keywords)
1737 goto error;
1738 nargs = 0;
1739 nkeywords = 0;
1740 for (i = 0; i < NCH(n); i++) {
1741 node *ch = CHILD(n, i);
1742 if (TYPE(ch) == argument) {
1743 expr_ty e;
1744 if (NCH(ch) == 1) {
1745 e = ast_for_expr(c, CHILD(ch, 0));
1746 if (!e)
1747 goto error;
1748 asdl_seq_SET(args, nargs++, e);
1749 }
1750 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1751 e = ast_for_genexp(c, ch);
1752 if (!e)
1753 goto error;
1754 asdl_seq_SET(args, nargs++, e);
1755 }
1756 else {
1757 keyword_ty kw;
1758 identifier key;
1759
1760 /* CHILD(ch, 0) is test, but must be an identifier? */
1761 e = ast_for_expr(c, CHILD(ch, 0));
1762 if (!e)
1763 goto error;
1764 /* f(lambda x: x[0] = 3) ends up getting parsed with
1765 * LHS test = lambda x: x[0], and RHS test = 3.
1766 * SF bug 132313 points out that complaining about a keyword
1767 * then is very confusing.
1768 */
1769 if (e->kind == Lambda_kind) {
1770 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
1771 goto error;
1772 } else if (e->kind != Name_kind) {
1773 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1774 goto error;
1775 }
1776 key = e->v.Name.id;
1777 free(e);
1778 e = ast_for_expr(c, CHILD(ch, 2));
1779 if (!e)
1780 goto error;
1781 kw = keyword(key, e);
1782 if (!kw)
1783 goto error;
1784 asdl_seq_SET(keywords, nkeywords++, kw);
1785 }
1786 }
1787 else if (TYPE(ch) == STAR) {
1788 vararg = ast_for_expr(c, CHILD(n, i+1));
1789 i++;
1790 }
1791 else if (TYPE(ch) == DOUBLESTAR) {
1792 kwarg = ast_for_expr(c, CHILD(n, i+1));
1793 i++;
1794 }
1795 }
1796
1797 return Call(func, args, keywords, vararg, kwarg, LINENO(n));
1798
1799 error:
1800 if (args)
1801 asdl_seq_free(args);
1802 if (keywords)
1803 asdl_seq_free(keywords);
1804 return NULL;
1805}
1806
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001808ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001810 /* testlist_gexp: test (',' test)* [','] */
1811 /* testlist: test (',' test)* [','] */
1812 /* testlist_safe: test (',' test)+ [','] */
1813 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001815 if (TYPE(n) == testlist_gexp) {
1816 if (NCH(n) > 1)
1817 assert(TYPE(CHILD(n, 1)) != gen_for);
1818 }
1819 else {
1820 assert(TYPE(n) == testlist ||
1821 TYPE(n) == testlist_safe ||
1822 TYPE(n) == testlist1);
1823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824 if (NCH(n) == 1)
1825 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 else {
1827 asdl_seq *tmp = seq_for_testlist(c, n);
1828 if (!tmp)
1829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 return Tuple(tmp, Load, LINENO(n));
1831 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001832}
1833
1834static expr_ty
1835ast_for_testlist_gexp(struct compiling *c, const node* n)
1836{
1837 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1838 /* argument: test [ gen_for ] */
1839 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
1840 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) {
1841 return ast_for_genexp(c, n);
1842 }
1843 else
1844 return ast_for_testlist(c, n);
1845}
1846
1847/* like ast_for_testlist() but returns a sequence */
1848static asdl_seq*
1849ast_for_class_bases(struct compiling *c, const node* n)
1850{
1851 /* testlist: test (',' test)* [','] */
1852 assert(NCH(n) > 0);
1853 REQ(n, testlist);
1854 if (NCH(n) == 1) {
1855 expr_ty base;
1856 asdl_seq *bases = asdl_seq_new(1);
1857 if (!bases)
1858 return NULL;
1859 base = ast_for_expr(c, CHILD(n, 0));
1860 if (!base) {
1861 asdl_seq_free(bases);
1862 return NULL;
1863 }
1864 asdl_seq_SET(bases, 0, base);
1865 return bases;
1866 }
1867 else {
1868 return seq_for_testlist(c, n);
1869 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870}
1871
1872static stmt_ty
1873ast_for_expr_stmt(struct compiling *c, const node *n)
1874{
1875 REQ(n, expr_stmt);
1876 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1877 | ('=' (yield_expr|testlist))*)
1878 testlist: test (',' test)* [',']
1879 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1880 | '<<=' | '>>=' | '**=' | '//='
1881 test: ... here starts the operator precendence dance
1882 */
1883
1884 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001885 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 if (!e)
1887 return NULL;
1888
1889 return Expr(e, LINENO(n));
1890 }
1891 else if (TYPE(CHILD(n, 1)) == augassign) {
1892 expr_ty expr1, expr2;
1893 operator_ty operator;
1894 node *ch = CHILD(n, 0);
1895
1896 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001897 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 else
1899 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch));
1900
1901 if (!expr1)
1902 return NULL;
1903 if (expr1->kind == GeneratorExp_kind) {
1904 ast_error(ch, "augmented assignment to generator "
1905 "expression not possible");
1906 return NULL;
1907 }
1908 if (expr1->kind == Name_kind) {
1909 char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1910 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
1911 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));
1921 if (!expr2)
1922 return NULL;
1923
1924 operator = ast_for_augassign(CHILD(n, 1));
1925 if (!operator)
1926 return NULL;
1927
1928 return AugAssign(expr1, operator, expr2, LINENO(n));
1929 }
1930 else {
1931 int i;
1932 asdl_seq *targets;
1933 node *value;
1934 expr_ty expression;
1935
1936 /* a normal assignment */
1937 REQ(CHILD(n, 1), EQUAL);
1938 targets = asdl_seq_new(NCH(n) / 2);
1939 if (!targets)
1940 return NULL;
1941 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00001942 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 node *ch = CHILD(n, i);
1944 if (TYPE(ch) == yield_expr) {
1945 ast_error(ch, "assignment to yield expression not possible");
1946 goto error;
1947 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001948 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949
1950 /* set context to assign */
1951 if (!e)
1952 goto error;
1953
1954 if (!set_context(e, Store, CHILD(n, i))) {
1955 free_expr(e);
1956 goto error;
1957 }
1958
1959 asdl_seq_SET(targets, i / 2, e);
1960 }
1961 value = CHILD(n, NCH(n) - 1);
1962 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001963 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 else
1965 expression = ast_for_expr(c, value);
1966 if (!expression)
1967 return NULL;
1968 return Assign(targets, expression, LINENO(n));
1969 error:
1970 for (i = i / 2; i >= 0; i--)
1971 free_expr((expr_ty)asdl_seq_GET(targets, i));
1972 asdl_seq_free(targets);
1973 return NULL;
1974 }
1975 return NULL;
1976}
1977
1978static stmt_ty
1979ast_for_print_stmt(struct compiling *c, const node *n)
1980{
1981 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1982 | '>>' test [ (',' test)+ [','] ] )
1983 */
1984 expr_ty dest = NULL, expression;
1985 asdl_seq *seq;
1986 bool nl;
1987 int i, start = 1;
1988
1989 REQ(n, print_stmt);
1990 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
1991 dest = ast_for_expr(c, CHILD(n, 2));
1992 if (!dest)
1993 return NULL;
1994 start = 4;
1995 }
1996 seq = asdl_seq_new((NCH(n) + 1 - start) / 2);
1997 if (!seq)
1998 return NULL;
1999 for (i = start; i < NCH(n); i += 2) {
2000 expression = ast_for_expr(c, CHILD(n, i));
2001 if (!expression) {
2002 asdl_seq_free(seq);
2003 return NULL;
2004 }
2005
2006 asdl_seq_APPEND(seq, expression);
2007 }
2008 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
2009 return Print(dest, seq, nl, LINENO(n));
2010}
2011
2012static asdl_seq *
2013ast_for_exprlist(struct compiling *c, const node *n, int context)
2014{
2015 asdl_seq *seq;
2016 int i;
2017 expr_ty e;
2018
2019 REQ(n, exprlist);
2020
2021 seq = asdl_seq_new((NCH(n) + 1) / 2);
2022 if (!seq)
2023 return NULL;
2024 for (i = 0; i < NCH(n); i += 2) {
2025 e = ast_for_expr(c, CHILD(n, i));
2026 if (!e) {
2027 asdl_seq_free(seq);
2028 return NULL;
2029 }
2030 if (context) {
2031 if (!set_context(e, context, CHILD(n, i)))
2032 return NULL;
2033 }
2034 asdl_seq_SET(seq, i / 2, e);
2035 }
2036 return seq;
2037}
2038
2039static stmt_ty
2040ast_for_del_stmt(struct compiling *c, const node *n)
2041{
2042 asdl_seq *expr_list;
2043
2044 /* del_stmt: 'del' exprlist */
2045 REQ(n, del_stmt);
2046
2047 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2048 if (!expr_list)
2049 return NULL;
2050 return Delete(expr_list, LINENO(n));
2051}
2052
2053static stmt_ty
2054ast_for_flow_stmt(struct compiling *c, const node *n)
2055{
2056 /*
2057 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2058 | yield_stmt
2059 break_stmt: 'break'
2060 continue_stmt: 'continue'
2061 return_stmt: 'return' [testlist]
2062 yield_stmt: yield_expr
2063 yield_expr: 'yield' testlist
2064 raise_stmt: 'raise' [test [',' test [',' test]]]
2065 */
2066 node *ch;
2067
2068 REQ(n, flow_stmt);
2069 ch = CHILD(n, 0);
2070 switch (TYPE(ch)) {
2071 case break_stmt:
2072 return Break(LINENO(n));
2073 case continue_stmt:
2074 return Continue(LINENO(n));
2075 case yield_stmt: { /* will reduce to yield_expr */
2076 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2077 if (!exp)
2078 return NULL;
2079 return Expr(exp, LINENO(n));
2080 }
2081 case return_stmt:
2082 if (NCH(ch) == 1)
2083 return Return(NULL, LINENO(n));
2084 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002085 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 if (!expression)
2087 return NULL;
2088 return Return(expression, LINENO(n));
2089 }
2090 case raise_stmt:
2091 if (NCH(ch) == 1)
2092 return Raise(NULL, NULL, NULL, LINENO(n));
2093 else if (NCH(ch) == 2) {
2094 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2095 if (!expression)
2096 return NULL;
2097 return Raise(expression, NULL, NULL, LINENO(n));
2098 }
2099 else if (NCH(ch) == 4) {
2100 expr_ty expr1, expr2;
2101
2102 expr1 = ast_for_expr(c, CHILD(ch, 1));
2103 if (!expr1)
2104 return NULL;
2105 expr2 = ast_for_expr(c, CHILD(ch, 3));
2106 if (!expr2)
2107 return NULL;
2108
2109 return Raise(expr1, expr2, NULL, LINENO(n));
2110 }
2111 else if (NCH(ch) == 6) {
2112 expr_ty expr1, expr2, expr3;
2113
2114 expr1 = ast_for_expr(c, CHILD(ch, 1));
2115 if (!expr1)
2116 return NULL;
2117 expr2 = ast_for_expr(c, CHILD(ch, 3));
2118 if (!expr2)
2119 return NULL;
2120 expr3 = ast_for_expr(c, CHILD(ch, 5));
2121 if (!expr3)
2122 return NULL;
2123
2124 return Raise(expr1, expr2, expr3, LINENO(n));
2125 }
2126 default:
2127 PyErr_Format(PyExc_Exception,
2128 "unexpected flow_stmt: %d", TYPE(ch));
2129 return NULL;
2130 }
2131}
2132
2133static alias_ty
2134alias_for_import_name(const node *n)
2135{
2136 /*
2137 import_as_name: NAME [NAME NAME]
2138 dotted_as_name: dotted_name [NAME NAME]
2139 dotted_name: NAME ('.' NAME)*
2140 */
2141 loop:
2142 switch (TYPE(n)) {
2143 case import_as_name:
2144 if (NCH(n) == 3)
2145 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
2146 NEW_IDENTIFIER(CHILD(n, 2)));
2147 else
2148 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
2149 NULL);
2150 break;
2151 case dotted_as_name:
2152 if (NCH(n) == 1) {
2153 n = CHILD(n, 0);
2154 goto loop;
2155 }
2156 else {
2157 alias_ty a = alias_for_import_name(CHILD(n, 0));
2158 assert(!a->asname);
2159 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2160 return a;
2161 }
2162 break;
2163 case dotted_name:
2164 if (NCH(n) == 1)
2165 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL);
2166 else {
2167 /* Create a string of the form "a.b.c" */
2168 int i, len;
2169 PyObject *str;
2170 char *s;
2171
2172 len = 0;
2173 for (i = 0; i < NCH(n); i += 2)
2174 /* length of string plus one for the dot */
2175 len += strlen(STR(CHILD(n, i))) + 1;
2176 len--; /* the last name doesn't have a dot */
2177 str = PyString_FromStringAndSize(NULL, len);
2178 if (!str)
2179 return NULL;
2180 s = PyString_AS_STRING(str);
2181 if (!s)
2182 return NULL;
2183 for (i = 0; i < NCH(n); i += 2) {
2184 char *sch = STR(CHILD(n, i));
2185 strcpy(s, STR(CHILD(n, i)));
2186 s += strlen(sch);
2187 *s++ = '.';
2188 }
2189 --s;
2190 *s = '\0';
2191 PyString_InternInPlace(&str);
2192 return alias(str, NULL);
2193 }
2194 break;
2195 case STAR:
2196 return alias(PyString_InternFromString("*"), NULL);
2197 default:
2198 PyErr_Format(PyExc_Exception,
2199 "unexpected import name: %d", TYPE(n));
2200 return NULL;
2201 }
2202 return NULL;
2203}
2204
2205static stmt_ty
2206ast_for_import_stmt(struct compiling *c, const node *n)
2207{
2208 /*
2209 import_stmt: import_name | import_from
2210 import_name: 'import' dotted_as_names
2211 import_from: 'from' dotted_name 'import' ('*' |
2212 '(' import_as_names ')' |
2213 import_as_names)
2214 */
2215 int i;
2216 asdl_seq *aliases;
2217
2218 REQ(n, import_stmt);
2219 n = CHILD(n, 0);
2220 if (STR(CHILD(n, 0))[0] == 'i') { /* import */
2221 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002222 REQ(n, dotted_as_names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 aliases = asdl_seq_new((NCH(n) + 1) / 2);
2224 if (!aliases)
2225 return NULL;
2226 for (i = 0; i < NCH(n); i += 2) {
2227 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2228 if (!import_alias) {
2229 asdl_seq_free(aliases);
2230 return NULL;
2231 }
2232 asdl_seq_SET(aliases, i / 2, import_alias);
2233 }
2234 return Import(aliases, LINENO(n));
2235 }
2236 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
2237 stmt_ty import;
2238 int n_children;
2239 const char *from_modules;
2240 int lineno = LINENO(n);
2241 alias_ty mod = alias_for_import_name(CHILD(n, 1));
2242 if (!mod)
2243 return NULL;
2244
2245 /* XXX this needs to be cleaned up */
2246
2247 from_modules = STR(CHILD(n, 3));
2248 if (!from_modules) {
2249 n = CHILD(n, 3); /* from ... import x, y, z */
2250 if (NCH(n) % 2 == 0) {
2251 /* it ends with a comma, not valid but the parser allows it */
2252 ast_error(n, "trailing comma not allowed without"
2253 " surrounding parentheses");
2254 return NULL;
2255 }
2256 }
2257 else if (from_modules[0] == '*') {
2258 n = CHILD(n, 3); /* from ... import * */
2259 }
2260 else if (from_modules[0] == '(')
2261 n = CHILD(n, 4); /* from ... import (x, y, z) */
2262 else
2263 return NULL;
2264
2265 n_children = NCH(n);
2266 if (from_modules && from_modules[0] == '*')
2267 n_children = 1;
2268
2269 aliases = asdl_seq_new((n_children + 1) / 2);
2270 if (!aliases) {
2271 free_alias(mod);
2272 return NULL;
2273 }
2274
2275 /* handle "from ... import *" special b/c there's no children */
2276 if (from_modules && from_modules[0] == '*') {
2277 alias_ty import_alias = alias_for_import_name(n);
2278 if (!import_alias) {
2279 asdl_seq_free(aliases);
2280 free_alias(mod);
2281 return NULL;
2282 }
2283 asdl_seq_APPEND(aliases, import_alias);
2284 }
2285
2286 for (i = 0; i < NCH(n); i += 2) {
2287 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2288 if (!import_alias) {
2289 asdl_seq_free(aliases);
2290 free_alias(mod);
2291 return NULL;
2292 }
2293 asdl_seq_APPEND(aliases, import_alias);
2294 }
2295 Py_INCREF(mod->name);
2296 import = ImportFrom(mod->name, aliases, lineno);
2297 free_alias(mod);
2298 return import;
2299 }
2300 PyErr_Format(PyExc_Exception,
2301 "unknown import statement: starts with command '%s'",
2302 STR(CHILD(n, 0)));
2303 return NULL;
2304}
2305
2306static stmt_ty
2307ast_for_global_stmt(struct compiling *c, const node *n)
2308{
2309 /* global_stmt: 'global' NAME (',' NAME)* */
2310 identifier name;
2311 asdl_seq *s;
2312 int i;
2313
2314 REQ(n, global_stmt);
2315 s = asdl_seq_new(NCH(n) / 2);
2316 if (!s)
2317 return NULL;
2318 for (i = 1; i < NCH(n); i += 2) {
2319 name = NEW_IDENTIFIER(CHILD(n, i));
2320 if (!name) {
2321 asdl_seq_free(s);
2322 return NULL;
2323 }
2324 asdl_seq_SET(s, i / 2, name);
2325 }
2326 return Global(s, LINENO(n));
2327}
2328
2329static stmt_ty
2330ast_for_exec_stmt(struct compiling *c, const node *n)
2331{
2332 expr_ty expr1, globals = NULL, locals = NULL;
2333 int n_children = NCH(n);
2334 if (n_children != 2 && n_children != 4 && n_children != 6) {
2335 PyErr_Format(PyExc_Exception,
2336 "poorly formed 'exec' statement: %d parts to statement",
2337 n_children);
2338 return NULL;
2339 }
2340
2341 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2342 REQ(n, exec_stmt);
2343 expr1 = ast_for_expr(c, CHILD(n, 1));
2344 if (!expr1)
2345 return NULL;
2346 if (n_children >= 4) {
2347 globals = ast_for_expr(c, CHILD(n, 3));
2348 if (!globals)
2349 return NULL;
2350 }
2351 if (n_children == 6) {
2352 locals = ast_for_expr(c, CHILD(n, 5));
2353 if (!locals)
2354 return NULL;
2355 }
2356
2357 return Exec(expr1, globals, locals, LINENO(n));
2358}
2359
2360static stmt_ty
2361ast_for_assert_stmt(struct compiling *c, const node *n)
2362{
2363 /* assert_stmt: 'assert' test [',' test] */
2364 REQ(n, assert_stmt);
2365 if (NCH(n) == 2) {
2366 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2367 if (!expression)
2368 return NULL;
2369 return Assert(expression, NULL, LINENO(n));
2370 }
2371 else if (NCH(n) == 4) {
2372 expr_ty expr1, expr2;
2373
2374 expr1 = ast_for_expr(c, CHILD(n, 1));
2375 if (!expr1)
2376 return NULL;
2377 expr2 = ast_for_expr(c, CHILD(n, 3));
2378 if (!expr2)
2379 return NULL;
2380
2381 return Assert(expr1, expr2, LINENO(n));
2382 }
2383 PyErr_Format(PyExc_Exception,
2384 "improper number of parts to 'assert' statement: %d",
2385 NCH(n));
2386 return NULL;
2387}
2388
2389static asdl_seq *
2390ast_for_suite(struct compiling *c, const node *n)
2391{
2392 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2393 asdl_seq *seq = NULL;
2394 stmt_ty s;
2395 int i, total, num, end, pos = 0;
2396 node *ch;
2397
2398 REQ(n, suite);
2399
2400 total = num_stmts(n);
2401 seq = asdl_seq_new(total);
2402 if (!seq)
2403 return NULL;
2404 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2405 n = CHILD(n, 0);
2406 /* simple_stmt always ends with a NEWLINE,
2407 and may have a trailing SEMI
2408 */
2409 end = NCH(n) - 1;
2410 if (TYPE(CHILD(n, end - 1)) == SEMI)
2411 end--;
2412 /* loop by 2 to skip semi-colons */
2413 for (i = 0; i < end; i += 2) {
2414 ch = CHILD(n, i);
2415 s = ast_for_stmt(c, ch);
2416 if (!s)
2417 goto error;
2418 asdl_seq_SET(seq, pos++, s);
2419 }
2420 }
2421 else {
2422 for (i = 2; i < (NCH(n) - 1); i++) {
2423 ch = CHILD(n, i);
2424 REQ(ch, stmt);
2425 num = num_stmts(ch);
2426 if (num == 1) {
2427 /* small_stmt or compound_stmt with only one child */
2428 s = ast_for_stmt(c, ch);
2429 if (!s)
2430 goto error;
2431 asdl_seq_SET(seq, pos++, s);
2432 }
2433 else {
2434 int j;
2435 ch = CHILD(ch, 0);
2436 REQ(ch, simple_stmt);
2437 for (j = 0; j < NCH(ch); j += 2) {
2438 s = ast_for_stmt(c, CHILD(ch, j));
2439 if (!s)
2440 goto error;
2441 asdl_seq_SET(seq, pos++, s);
2442 }
2443 }
2444 }
2445 }
2446 assert(pos == seq->size);
2447 return seq;
2448 error:
2449 if (seq)
2450 asdl_seq_free(seq);
2451 return NULL;
2452}
2453
2454static stmt_ty
2455ast_for_if_stmt(struct compiling *c, const node *n)
2456{
2457 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2458 ['else' ':' suite]
2459 */
2460 char *s;
2461
2462 REQ(n, if_stmt);
2463
2464 if (NCH(n) == 4) {
2465 expr_ty expression;
2466 asdl_seq *suite_seq;
2467
2468 expression = ast_for_expr(c, CHILD(n, 1));
2469 if (!expression)
2470 return NULL;
2471 suite_seq = ast_for_suite(c, CHILD(n, 3));
2472 if (!suite_seq)
2473 return NULL;
2474
2475 return If(expression, suite_seq, NULL, LINENO(n));
2476 }
2477 s = STR(CHILD(n, 4));
2478 /* s[2], the third character in the string, will be
2479 's' for el_s_e, or
2480 'i' for el_i_f
2481 */
2482 if (s[2] == 's') {
2483 expr_ty expression;
2484 asdl_seq *seq1, *seq2;
2485
2486 expression = ast_for_expr(c, CHILD(n, 1));
2487 if (!expression)
2488 return NULL;
2489 seq1 = ast_for_suite(c, CHILD(n, 3));
2490 if (!seq1)
2491 return NULL;
2492 seq2 = ast_for_suite(c, CHILD(n, 6));
2493 if (!seq2)
2494 return NULL;
2495
2496 return If(expression, seq1, seq2, LINENO(n));
2497 }
2498 else if (s[2] == 'i') {
2499 int i, n_elif, has_else = 0;
2500 asdl_seq *orelse = NULL;
2501 n_elif = NCH(n) - 4;
2502 /* must reference the child n_elif+1 since 'else' token is third,
2503 not fourth, child from the end. */
2504 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2505 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2506 has_else = 1;
2507 n_elif -= 3;
2508 }
2509 n_elif /= 4;
2510
2511 if (has_else) {
2512 expr_ty expression;
2513 asdl_seq *seq1, *seq2;
2514
2515 orelse = asdl_seq_new(1);
2516 if (!orelse)
2517 return NULL;
2518 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2519 if (!expression) {
2520 asdl_seq_free(orelse);
2521 return NULL;
2522 }
2523 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2524 if (!seq1) {
2525 asdl_seq_free(orelse);
2526 return NULL;
2527 }
2528 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2529 if (!seq2) {
2530 asdl_seq_free(orelse);
2531 return NULL;
2532 }
2533
2534 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
2535 LINENO(CHILD(n, NCH(n) - 6))));
2536 /* the just-created orelse handled the last elif */
2537 n_elif--;
2538 }
2539 else
2540 orelse = NULL;
2541
2542 for (i = 0; i < n_elif; i++) {
2543 int off = 5 + (n_elif - i - 1) * 4;
2544 expr_ty expression;
2545 asdl_seq *suite_seq;
2546 asdl_seq *new = asdl_seq_new(1);
2547 if (!new)
2548 return NULL;
2549 expression = ast_for_expr(c, CHILD(n, off));
2550 if (!expression) {
2551 asdl_seq_free(new);
2552 return NULL;
2553 }
2554 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2555 if (!suite_seq) {
2556 asdl_seq_free(new);
2557 return NULL;
2558 }
2559
2560 asdl_seq_SET(new, 0,
2561 If(expression, suite_seq, orelse,
2562 LINENO(CHILD(n, off))));
2563 orelse = new;
2564 }
2565 return If(ast_for_expr(c, CHILD(n, 1)),
2566 ast_for_suite(c, CHILD(n, 3)),
2567 orelse, LINENO(n));
2568 }
2569 else {
2570 PyErr_Format(PyExc_Exception,
2571 "unexpected token in 'if' statement: %s", s);
2572 return NULL;
2573 }
2574}
2575
2576static stmt_ty
2577ast_for_while_stmt(struct compiling *c, const node *n)
2578{
2579 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2580 REQ(n, while_stmt);
2581
2582 if (NCH(n) == 4) {
2583 expr_ty expression;
2584 asdl_seq *suite_seq;
2585
2586 expression = ast_for_expr(c, CHILD(n, 1));
2587 if (!expression)
2588 return NULL;
2589 suite_seq = ast_for_suite(c, CHILD(n, 3));
2590 if (!suite_seq)
2591 return NULL;
2592 return While(expression, suite_seq, NULL, LINENO(n));
2593 }
2594 else if (NCH(n) == 7) {
2595 expr_ty expression;
2596 asdl_seq *seq1, *seq2;
2597
2598 expression = ast_for_expr(c, CHILD(n, 1));
2599 if (!expression)
2600 return NULL;
2601 seq1 = ast_for_suite(c, CHILD(n, 3));
2602 if (!seq1)
2603 return NULL;
2604 seq2 = ast_for_suite(c, CHILD(n, 6));
2605 if (!seq2)
2606 return NULL;
2607
2608 return While(expression, seq1, seq2, LINENO(n));
2609 }
2610 else {
2611 PyErr_Format(PyExc_Exception,
2612 "wrong number of tokens for 'while' statement: %d",
2613 NCH(n));
2614 return NULL;
2615 }
2616}
2617
2618static stmt_ty
2619ast_for_for_stmt(struct compiling *c, const node *n)
2620{
2621 asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL;
2622 expr_ty expression;
2623 expr_ty target;
2624 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2625 REQ(n, for_stmt);
2626
2627 if (NCH(n) == 9) {
2628 seq = ast_for_suite(c, CHILD(n, 8));
2629 if (!seq)
2630 return NULL;
2631 }
2632
2633 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
2634 if (!_target)
2635 return NULL;
2636 if (asdl_seq_LEN(_target) == 1) {
2637 target = asdl_seq_GET(_target, 0);
2638 asdl_seq_free(_target);
2639 }
2640 else
2641 target = Tuple(_target, Store, LINENO(n));
2642
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002643 expression = ast_for_testlist(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 if (!expression)
2645 return NULL;
2646 suite_seq = ast_for_suite(c, CHILD(n, 5));
2647 if (!suite_seq)
2648 return NULL;
2649
2650 return For(target, expression, suite_seq, seq, LINENO(n));
2651}
2652
2653static excepthandler_ty
2654ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2655{
2656 /* except_clause: 'except' [test [',' test]] */
2657 REQ(exc, except_clause);
2658 REQ(body, suite);
2659
2660 if (NCH(exc) == 1) {
2661 asdl_seq *suite_seq = ast_for_suite(c, body);
2662 if (!suite_seq)
2663 return NULL;
2664
2665 return excepthandler(NULL, NULL, suite_seq);
2666 }
2667 else if (NCH(exc) == 2) {
2668 expr_ty expression;
2669 asdl_seq *suite_seq;
2670
2671 expression = ast_for_expr(c, CHILD(exc, 1));
2672 if (!expression)
2673 return NULL;
2674 suite_seq = ast_for_suite(c, body);
2675 if (!suite_seq)
2676 return NULL;
2677
2678 return excepthandler(expression, NULL, suite_seq);
2679 }
2680 else if (NCH(exc) == 4) {
2681 asdl_seq *suite_seq;
2682 expr_ty expression;
2683 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2684 if (!e)
2685 return NULL;
2686 if (!set_context(e, Store, CHILD(exc, 3)))
2687 return NULL;
2688 expression = ast_for_expr(c, CHILD(exc, 1));
2689 if (!expression)
2690 return NULL;
2691 suite_seq = ast_for_suite(c, body);
2692 if (!suite_seq)
2693 return NULL;
2694
2695 return excepthandler(expression, e, suite_seq);
2696 }
2697 else {
2698 PyErr_Format(PyExc_Exception,
2699 "wrong number of children for 'except' clause: %d",
2700 NCH(exc));
2701 return NULL;
2702 }
2703}
2704
2705static stmt_ty
2706ast_for_try_stmt(struct compiling *c, const node *n)
2707{
2708 REQ(n, try_stmt);
2709
2710 if (TYPE(CHILD(n, 3)) == NAME) {/* must be 'finally' */
2711 /* try_stmt: 'try' ':' suite 'finally' ':' suite) */
2712 asdl_seq *s1, *s2;
2713 s1 = ast_for_suite(c, CHILD(n, 2));
2714 if (!s1)
2715 return NULL;
2716 s2 = ast_for_suite(c, CHILD(n, 5));
2717 if (!s2)
2718 return NULL;
2719
2720 return TryFinally(s1, s2, LINENO(n));
2721 }
2722 else if (TYPE(CHILD(n, 3)) == except_clause) {
2723 /* try_stmt: ('try' ':' suite (except_clause ':' suite)+
2724 ['else' ':' suite]
2725 */
2726 asdl_seq *suite_seq1, *suite_seq2;
2727 asdl_seq *handlers;
2728 int i, has_else = 0, n_except = NCH(n) - 3;
2729 if (TYPE(CHILD(n, NCH(n) - 3)) == NAME) {
2730 has_else = 1;
2731 n_except -= 3;
2732 }
2733 n_except /= 3;
2734 handlers = asdl_seq_new(n_except);
2735 if (!handlers)
2736 return NULL;
2737 for (i = 0; i < n_except; i++) {
2738 excepthandler_ty e = ast_for_except_clause(c,
2739 CHILD(n, 3 + i * 3),
2740 CHILD(n, 5 + i * 3));
2741 if (!e)
2742 return NULL;
2743 asdl_seq_SET(handlers, i, e);
2744 }
2745
2746 suite_seq1 = ast_for_suite(c, CHILD(n, 2));
2747 if (!suite_seq1)
2748 return NULL;
2749 if (has_else) {
2750 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2751 if (!suite_seq2)
2752 return NULL;
2753 }
2754 else
2755 suite_seq2 = NULL;
2756
2757 return TryExcept(suite_seq1, handlers, suite_seq2, LINENO(n));
2758 }
2759 else {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002760 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761 return NULL;
2762 }
2763}
2764
2765static stmt_ty
2766ast_for_classdef(struct compiling *c, const node *n)
2767{
2768 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 asdl_seq *bases, *s;
2770
2771 REQ(n, classdef);
2772
2773 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2774 ast_error(n, "assignment to None");
2775 return NULL;
2776 }
2777
2778 if (NCH(n) == 4) {
2779 s = ast_for_suite(c, CHILD(n, 3));
2780 if (!s)
2781 return NULL;
2782 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2783 }
2784 /* check for empty base list */
2785 if (TYPE(CHILD(n,3)) == RPAR) {
2786 s = ast_for_suite(c, CHILD(n,5));
2787 if (!s)
2788 return NULL;
2789 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2790 }
2791
2792 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002793 bases = ast_for_class_bases(c, CHILD(n, 3));
2794 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796
2797 s = ast_for_suite(c, CHILD(n, 6));
2798 if (!s) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002799 asdl_seq_free(bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 return NULL;
2801 }
2802 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n));
2803}
2804
2805static stmt_ty
2806ast_for_stmt(struct compiling *c, const node *n)
2807{
2808 if (TYPE(n) == stmt) {
2809 assert(NCH(n) == 1);
2810 n = CHILD(n, 0);
2811 }
2812 if (TYPE(n) == simple_stmt) {
2813 assert(num_stmts(n) == 1);
2814 n = CHILD(n, 0);
2815 }
2816 if (TYPE(n) == small_stmt) {
2817 REQ(n, small_stmt);
2818 n = CHILD(n, 0);
2819 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2820 | flow_stmt | import_stmt | global_stmt | exec_stmt
2821 | assert_stmt
2822 */
2823 switch (TYPE(n)) {
2824 case expr_stmt:
2825 return ast_for_expr_stmt(c, n);
2826 case print_stmt:
2827 return ast_for_print_stmt(c, n);
2828 case del_stmt:
2829 return ast_for_del_stmt(c, n);
2830 case pass_stmt:
2831 return Pass(LINENO(n));
2832 case flow_stmt:
2833 return ast_for_flow_stmt(c, n);
2834 case import_stmt:
2835 return ast_for_import_stmt(c, n);
2836 case global_stmt:
2837 return ast_for_global_stmt(c, n);
2838 case exec_stmt:
2839 return ast_for_exec_stmt(c, n);
2840 case assert_stmt:
2841 return ast_for_assert_stmt(c, n);
2842 default:
2843 PyErr_Format(PyExc_Exception,
2844 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2845 TYPE(n), NCH(n));
2846 return NULL;
2847 }
2848 }
2849 else {
2850 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2851 | funcdef | classdef
2852 */
2853 node *ch = CHILD(n, 0);
2854 REQ(n, compound_stmt);
2855 switch (TYPE(ch)) {
2856 case if_stmt:
2857 return ast_for_if_stmt(c, ch);
2858 case while_stmt:
2859 return ast_for_while_stmt(c, ch);
2860 case for_stmt:
2861 return ast_for_for_stmt(c, ch);
2862 case try_stmt:
2863 return ast_for_try_stmt(c, ch);
2864 case funcdef:
2865 return ast_for_funcdef(c, ch);
2866 case classdef:
2867 return ast_for_classdef(c, ch);
2868 default:
2869 PyErr_Format(PyExc_Exception,
2870 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2871 TYPE(n), NCH(n));
2872 return NULL;
2873 }
2874 }
2875}
2876
2877static PyObject *
2878parsenumber(const char *s)
2879{
2880 const char *end;
2881 long x;
2882 double dx;
2883#ifndef WITHOUT_COMPLEX
2884 Py_complex c;
2885 int imflag;
2886#endif
2887
2888 errno = 0;
2889 end = s + strlen(s) - 1;
2890#ifndef WITHOUT_COMPLEX
2891 imflag = *end == 'j' || *end == 'J';
2892#endif
2893 if (*end == 'l' || *end == 'L')
2894 return PyLong_FromString((char *)s, (char **)0, 0);
2895 if (s[0] == '0') {
2896 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
2897 if (x < 0 && errno == 0) {
2898 return PyLong_FromString((char *)s,
2899 (char **)0,
2900 0);
2901 }
2902 }
2903 else
2904 x = PyOS_strtol((char *)s, (char **)&end, 0);
2905 if (*end == '\0') {
2906 if (errno != 0)
2907 return PyLong_FromString((char *)s, (char **)0, 0);
2908 return PyInt_FromLong(x);
2909 }
2910 /* XXX Huge floats may silently fail */
2911#ifndef WITHOUT_COMPLEX
2912 if (imflag) {
2913 c.real = 0.;
2914 PyFPE_START_PROTECT("atof", return 0)
2915 c.imag = atof(s);
2916 PyFPE_END_PROTECT(c)
2917 return PyComplex_FromCComplex(c);
2918 }
2919 else
2920#endif
2921 {
2922 PyFPE_START_PROTECT("atof", return 0)
2923 dx = atof(s);
2924 PyFPE_END_PROTECT(dx)
2925 return PyFloat_FromDouble(dx);
2926 }
2927}
2928
2929static PyObject *
2930decode_utf8(const char **sPtr, const char *end, char* encoding)
2931{
2932#ifndef Py_USING_UNICODE
2933 Py_FatalError("decode_utf8 should not be called in this build.");
2934 return NULL;
2935#else
2936 PyObject *u, *v;
2937 char *s, *t;
2938 t = s = (char *)*sPtr;
2939 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2940 while (s < end && (*s & 0x80)) s++;
2941 *sPtr = s;
2942 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
2943 if (u == NULL)
2944 return NULL;
2945 v = PyUnicode_AsEncodedString(u, encoding, NULL);
2946 Py_DECREF(u);
2947 return v;
2948#endif
2949}
2950
2951static PyObject *
2952decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
2953{
2954 PyObject *v, *u;
2955 char *buf;
2956 char *p;
2957 const char *end;
2958 if (encoding == NULL) {
2959 buf = (char *)s;
2960 u = NULL;
2961 } else if (strcmp(encoding, "iso-8859-1") == 0) {
2962 buf = (char *)s;
2963 u = NULL;
2964 } else {
2965 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2966 u = PyString_FromStringAndSize((char *)NULL, len * 4);
2967 if (u == NULL)
2968 return NULL;
2969 p = buf = PyString_AsString(u);
2970 end = s + len;
2971 while (s < end) {
2972 if (*s == '\\') {
2973 *p++ = *s++;
2974 if (*s & 0x80) {
2975 strcpy(p, "u005c");
2976 p += 5;
2977 }
2978 }
2979 if (*s & 0x80) { /* XXX inefficient */
2980 PyObject *w;
2981 char *r;
2982 int rn, i;
2983 w = decode_utf8(&s, end, "utf-16-be");
2984 if (w == NULL) {
2985 Py_DECREF(u);
2986 return NULL;
2987 }
2988 r = PyString_AsString(w);
2989 rn = PyString_Size(w);
2990 assert(rn % 2 == 0);
2991 for (i = 0; i < rn; i += 2) {
2992 sprintf(p, "\\u%02x%02x",
2993 r[i + 0] & 0xFF,
2994 r[i + 1] & 0xFF);
2995 p += 6;
2996 }
2997 Py_DECREF(w);
2998 } else {
2999 *p++ = *s++;
3000 }
3001 }
3002 len = p - buf;
3003 s = buf;
3004 }
3005 if (rawmode)
3006 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3007 else
3008 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3009 Py_XDECREF(u);
3010 return v;
3011}
3012
3013/* s is a Python string literal, including the bracketing quote characters,
3014 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3015 * parsestr parses it, and returns the decoded Python string object.
3016 */
3017static PyObject *
3018parsestr(const char *s, const char *encoding)
3019{
3020 PyObject *v;
3021 size_t len;
3022 int quote = *s;
3023 int rawmode = 0;
3024 int need_encoding;
3025 int unicode = 0;
3026
3027 if (isalpha(quote) || quote == '_') {
3028 if (quote == 'u' || quote == 'U') {
3029 quote = *++s;
3030 unicode = 1;
3031 }
3032 if (quote == 'r' || quote == 'R') {
3033 quote = *++s;
3034 rawmode = 1;
3035 }
3036 }
3037 if (quote != '\'' && quote != '\"') {
3038 PyErr_BadInternalCall();
3039 return NULL;
3040 }
3041 s++;
3042 len = strlen(s);
3043 if (len > INT_MAX) {
3044 PyErr_SetString(PyExc_OverflowError,
3045 "string to parse is too long");
3046 return NULL;
3047 }
3048 if (s[--len] != quote) {
3049 PyErr_BadInternalCall();
3050 return NULL;
3051 }
3052 if (len >= 4 && s[0] == quote && s[1] == quote) {
3053 s += 2;
3054 len -= 2;
3055 if (s[--len] != quote || s[--len] != quote) {
3056 PyErr_BadInternalCall();
3057 return NULL;
3058 }
3059 }
3060#ifdef Py_USING_UNICODE
3061 if (unicode || Py_UnicodeFlag) {
3062 return decode_unicode(s, len, rawmode, encoding);
3063 }
3064#endif
3065 need_encoding = (encoding != NULL &&
3066 strcmp(encoding, "utf-8") != 0 &&
3067 strcmp(encoding, "iso-8859-1") != 0);
3068 if (rawmode || strchr(s, '\\') == NULL) {
3069 if (need_encoding) {
3070#ifndef Py_USING_UNICODE
3071 /* This should not happen - we never see any other
3072 encoding. */
3073 Py_FatalError("cannot deal with encodings in this build.");
3074#else
3075 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
3076 if (u == NULL)
3077 return NULL;
3078 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3079 Py_DECREF(u);
3080 return v;
3081#endif
3082 } else {
3083 return PyString_FromStringAndSize(s, len);
3084 }
3085 }
3086
3087 v = PyString_DecodeEscape(s, len, NULL, unicode,
3088 need_encoding ? encoding : NULL);
3089 return v;
3090}
3091
3092/* Build a Python string object out of a STRING atom. This takes care of
3093 * compile-time literal catenation, calling parsestr() on each piece, and
3094 * pasting the intermediate results together.
3095 */
3096static PyObject *
3097parsestrplus(struct compiling *c, const node *n)
3098{
3099 PyObject *v;
3100 int i;
3101 REQ(CHILD(n, 0), STRING);
3102 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3103 /* String literal concatenation */
3104 for (i = 1; i < NCH(n); i++) {
3105 PyObject *s;
3106 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3107 if (s == NULL)
3108 goto onError;
3109 if (PyString_Check(v) && PyString_Check(s)) {
3110 PyString_ConcatAndDel(&v, s);
3111 if (v == NULL)
3112 goto onError;
3113 }
3114#ifdef Py_USING_UNICODE
3115 else {
3116 PyObject *temp;
3117 temp = PyUnicode_Concat(v, s);
3118 Py_DECREF(s);
3119 if (temp == NULL)
3120 goto onError;
3121 Py_DECREF(v);
3122 v = temp;
3123 }
3124#endif
3125 }
3126 }
3127 return v;
3128
3129 onError:
3130 Py_XDECREF(v);
3131 return NULL;
3132}