blob: e5df90627ac9b0b29ece7a868030c2f61015a307 [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
Neal Norwitzaf8f9742005-11-15 05:09:44 +000023/*
24 Note:
25
26 You should rarely need to use the asdl_seq_free() in this file.
27 If you use asdl_seq_free(), you will leak any objects held in the seq.
28 If there is an appropriate asdl_*_seq_free() function, use it.
29 If there isn't an asdl_*_seq_free() function for you, you will
30 need to loop over the data in the sequence and free it.
31
32 asdl_seq* seq;
33 int i;
34
35 for (i = 0; i < asdl_seq_LEN(seq); i++)
36 free_***(asdl_seq_GET(seq, i));
37 asdl_seq_free(seq);
38
39 Almost all of the ast functions return a seq of expr, so you should
40 use asdl_expr_seq_free(). The exception is ast_for_suite() which
41 returns a seq of stmt's, so use asdl_stmt_seq_free() to free it.
42*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043
44/* Data structure used internally */
45struct compiling {
46 char *c_encoding; /* source encoding */
47};
48
49static asdl_seq *seq_for_testlist(struct compiling *, const node *);
50static expr_ty ast_for_expr(struct compiling *, const node *);
51static stmt_ty ast_for_stmt(struct compiling *, const node *);
52static asdl_seq *ast_for_suite(struct compiling *, const node *);
53static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000054static expr_ty ast_for_testlist(struct compiling *, const node *);
55static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056
57/* Note different signature for ast_for_call */
58static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
59
60static PyObject *parsenumber(const char *);
61static PyObject *parsestr(const char *s, const char *encoding);
62static PyObject *parsestrplus(struct compiling *, const node *n);
63
64extern grammar _PyParser_Grammar; /* From graminit.c */
65
66#ifndef LINENO
67#define LINENO(n) ((n)->n_lineno)
68#endif
69
70#define NEW_IDENTIFIER(n) PyString_InternFromString(STR(n))
71
72static void
73asdl_stmt_seq_free(asdl_seq* seq)
74{
75 int n, i;
76
77 if (!seq)
78 return;
Neal Norwitze76adcd2005-11-15 05:04:31 +000079
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080 n = asdl_seq_LEN(seq);
81 for (i = 0; i < n; i++)
82 free_stmt(asdl_seq_GET(seq, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +000083 asdl_seq_free(seq); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084}
85
86static void
87asdl_expr_seq_free(asdl_seq* seq)
88{
89 int n, i;
90
91 if (!seq)
92 return;
Neal Norwitze76adcd2005-11-15 05:04:31 +000093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000094 n = asdl_seq_LEN(seq);
95 for (i = 0; i < n; i++)
96 free_expr(asdl_seq_GET(seq, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +000097 asdl_seq_free(seq); /* ok */
98}
99
100static void
101asdl_alias_seq_free(asdl_seq* seq)
102{
103 int n, i;
104
105 if (!seq)
106 return;
107
108 n = asdl_seq_LEN(seq);
109 for (i = 0; i < n; i++)
110 free_alias(asdl_seq_GET(seq, i));
111 asdl_seq_free(seq); /* ok */
112}
113
114static void
115asdl_comprehension_seq_free(asdl_seq* seq)
116{
117 int n, i;
118
119 if (!seq)
120 return;
121
122 n = asdl_seq_LEN(seq);
123 for (i = 0; i < n; i++)
124 free_comprehension(asdl_seq_GET(seq, i));
125 asdl_seq_free(seq); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000126}
127
128/* This routine provides an invalid object for the syntax error.
129 The outermost routine must unpack this error and create the
130 proper object. We do this so that we don't have to pass
131 the filename to everything function.
132
133 XXX Maybe we should just pass the filename...
134*/
135
136static int
137ast_error(const node *n, const char *errstr)
138{
139 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
140 if (!u)
141 return 0;
142 PyErr_SetObject(PyExc_SyntaxError, u);
143 Py_DECREF(u);
144 return 0;
145}
146
147static void
148ast_error_finish(const char *filename)
149{
150 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
151 int lineno;
152
153 assert(PyErr_Occurred());
154 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
155 return;
156
157 PyErr_Fetch(&type, &value, &tback);
158 errstr = PyTuple_GetItem(value, 0);
159 if (!errstr)
160 return;
161 Py_INCREF(errstr);
162 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
163 if (lineno == -1)
164 return;
165 Py_DECREF(value);
166
167 loc = PyErr_ProgramText(filename, lineno);
168 if (!loc) {
169 Py_INCREF(Py_None);
170 loc = Py_None;
171 }
172 tmp = Py_BuildValue("(ziOO)", filename, lineno, Py_None, loc);
173 Py_DECREF(loc);
174 if (!tmp)
175 return;
176 value = Py_BuildValue("(OO)", errstr, tmp);
177 Py_DECREF(errstr);
178 Py_DECREF(tmp);
179 if (!value)
180 return;
181 PyErr_Restore(type, value, tback);
182}
183
184/* num_stmts() returns number of contained statements.
185
186 Use this routine to determine how big a sequence is needed for
187 the statements in a parse tree. Its raison d'etre is this bit of
188 grammar:
189
190 stmt: simple_stmt | compound_stmt
191 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
192
193 A simple_stmt can contain multiple small_stmt elements joined
194 by semicolons. If the arg is a simple_stmt, the number of
195 small_stmt elements is returned.
196*/
197
198static int
199num_stmts(const node *n)
200{
201 int i, l;
202 node *ch;
203
204 switch (TYPE(n)) {
205 case single_input:
206 if (TYPE(CHILD(n, 0)) == NEWLINE)
207 return 0;
208 else
209 return num_stmts(CHILD(n, 0));
210 case file_input:
211 l = 0;
212 for (i = 0; i < NCH(n); i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == stmt)
215 l += num_stmts(ch);
216 }
217 return l;
218 case stmt:
219 return num_stmts(CHILD(n, 0));
220 case compound_stmt:
221 return 1;
222 case simple_stmt:
223 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
224 case suite:
225 if (NCH(n) == 1)
226 return num_stmts(CHILD(n, 0));
227 else {
228 l = 0;
229 for (i = 2; i < (NCH(n) - 1); i++)
230 l += num_stmts(CHILD(n, i));
231 return l;
232 }
233 default: {
234 char buf[128];
235
236 sprintf(buf, "Non-statement found: %d %d\n",
237 TYPE(n), NCH(n));
238 Py_FatalError(buf);
239 }
240 }
241 assert(0);
242 return 0;
243}
244
245/* Transform the CST rooted at node * to the appropriate AST
246*/
247
248mod_ty
249PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename)
250{
251 int i, j, num;
252 asdl_seq *stmts = NULL;
253 stmt_ty s;
254 node *ch;
255 struct compiling c;
256
257 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
258 c.c_encoding = "utf-8";
259 } else if (TYPE(n) == encoding_decl) {
260 c.c_encoding = STR(n);
261 n = CHILD(n, 0);
262 } else {
263 c.c_encoding = NULL;
264 }
265
266 switch (TYPE(n)) {
267 case file_input:
268 stmts = asdl_seq_new(num_stmts(n));
269 if (!stmts)
270 return NULL;
271 for (i = 0; i < NCH(n) - 1; i++) {
272 ch = CHILD(n, i);
273 if (TYPE(ch) == NEWLINE)
274 continue;
275 REQ(ch, stmt);
276 num = num_stmts(ch);
277 if (num == 1) {
278 s = ast_for_stmt(&c, ch);
279 if (!s)
280 goto error;
281 asdl_seq_APPEND(stmts, s);
282 }
283 else {
284 ch = CHILD(ch, 0);
285 REQ(ch, simple_stmt);
286 for (j = 0; j < num; j++) {
287 s = ast_for_stmt(&c, CHILD(ch, j * 2));
288 if (!s)
289 goto error;
290 asdl_seq_APPEND(stmts, s);
291 }
292 }
293 }
294 return Module(stmts);
295 case eval_input: {
296 expr_ty testlist_ast;
297
298 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000299 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 if (!testlist_ast)
301 goto error;
302 return Expression(testlist_ast);
303 }
304 case single_input:
305 if (TYPE(CHILD(n, 0)) == NEWLINE) {
306 stmts = asdl_seq_new(1);
307 if (!stmts)
308 goto error;
309 asdl_seq_SET(stmts, 0, Pass(n->n_lineno));
310 return Interactive(stmts);
311 }
312 else {
313 n = CHILD(n, 0);
314 num = num_stmts(n);
315 stmts = asdl_seq_new(num);
316 if (!stmts)
317 goto error;
318 if (num == 1) {
319 stmt_ty s = ast_for_stmt(&c, n);
320 if (!s)
321 goto error;
322 asdl_seq_SET(stmts, 0, s);
323 }
324 else {
325 /* Only a simple_stmt can contain multiple statements. */
326 REQ(n, simple_stmt);
327 for (i = 0; i < NCH(n); i += 2) {
328 stmt_ty s;
329 if (TYPE(CHILD(n, i)) == NEWLINE)
330 break;
331 s = ast_for_stmt(&c, CHILD(n, i));
332 if (!s)
333 goto error;
334 asdl_seq_SET(stmts, i / 2, s);
335 }
336 }
337
338 return Interactive(stmts);
339 }
340 default:
341 goto error;
342 }
343 error:
344 if (stmts)
345 asdl_stmt_seq_free(stmts);
346 ast_error_finish(filename);
347 return NULL;
348}
349
350/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
351*/
352
353static operator_ty
354get_operator(const node *n)
355{
356 switch (TYPE(n)) {
357 case VBAR:
358 return BitOr;
359 case CIRCUMFLEX:
360 return BitXor;
361 case AMPER:
362 return BitAnd;
363 case LEFTSHIFT:
364 return LShift;
365 case RIGHTSHIFT:
366 return RShift;
367 case PLUS:
368 return Add;
369 case MINUS:
370 return Sub;
371 case STAR:
372 return Mult;
373 case SLASH:
374 return Div;
375 case DOUBLESLASH:
376 return FloorDiv;
377 case PERCENT:
378 return Mod;
379 default:
380 return 0;
381 }
382}
383
384/* Set the context ctx for expr_ty e returning 0 on success, -1 on error.
385
386 Only sets context for expr kinds that "can appear in assignment context"
387 (according to ../Parser/Python.asdl). For other expr kinds, it sets
388 an appropriate syntax error and returns false.
389
390 If e is a sequential type, items in sequence will also have their context
391 set.
392
393*/
394
395static int
396set_context(expr_ty e, expr_context_ty ctx, const node *n)
397{
398 asdl_seq *s = NULL;
399
400 switch (e->kind) {
401 case Attribute_kind:
402 if (ctx == Store &&
403 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
404 return ast_error(n, "assignment to None");
405 }
406 e->v.Attribute.ctx = ctx;
407 break;
408 case Subscript_kind:
409 e->v.Subscript.ctx = ctx;
410 break;
411 case Name_kind:
412 if (ctx == Store &&
413 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
414 return ast_error(n, "assignment to None");
415 }
416 e->v.Name.ctx = ctx;
417 break;
418 case List_kind:
419 e->v.List.ctx = ctx;
420 s = e->v.List.elts;
421 break;
422 case Tuple_kind:
423 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
424 return ast_error(n, "can't assign to ()");
425 e->v.Tuple.ctx = ctx;
426 s = e->v.Tuple.elts;
427 break;
428 case Call_kind:
429 if (ctx == Store)
430 return ast_error(n, "can't assign to function call");
431 else if (ctx == Del)
432 return ast_error(n, "can't delete function call");
433 else
434 return ast_error(n, "unexpected operation on function call");
435 break;
436 case BinOp_kind:
437 return ast_error(n, "can't assign to operator");
438 case GeneratorExp_kind:
439 return ast_error(n, "assignment to generator expression "
440 "not possible");
441 case Num_kind:
442 case Str_kind:
443 return ast_error(n, "can't assign to literal");
444 default: {
445 char buf[300];
446 PyOS_snprintf(buf, sizeof(buf),
447 "unexpected expression in assignment %d (line %d)",
448 e->kind, e->lineno);
449 return ast_error(n, buf);
450 }
451 }
452 /* If the LHS is a list or tuple, we need to set the assignment
453 context for all the tuple elements.
454 */
455 if (s) {
456 int i;
457
458 for (i = 0; i < asdl_seq_LEN(s); i++) {
459 if (!set_context(asdl_seq_GET(s, i), ctx, n))
460 return 0;
461 }
462 }
463 return 1;
464}
465
466static operator_ty
467ast_for_augassign(const node *n)
468{
469 REQ(n, augassign);
470 n = CHILD(n, 0);
471 switch (STR(n)[0]) {
472 case '+':
473 return Add;
474 case '-':
475 return Sub;
476 case '/':
477 if (STR(n)[1] == '/')
478 return FloorDiv;
479 else
480 return Div;
481 case '%':
482 return Mod;
483 case '<':
484 return LShift;
485 case '>':
486 return RShift;
487 case '&':
488 return BitAnd;
489 case '^':
490 return BitXor;
491 case '|':
492 return BitOr;
493 case '*':
494 if (STR(n)[1] == '*')
495 return Pow;
496 else
497 return Mult;
498 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000499 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 return 0;
501 }
502}
503
504static cmpop_ty
505ast_for_comp_op(const node *n)
506{
507 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
508 |'is' 'not'
509 */
510 REQ(n, comp_op);
511 if (NCH(n) == 1) {
512 n = CHILD(n, 0);
513 switch (TYPE(n)) {
514 case LESS:
515 return Lt;
516 case GREATER:
517 return Gt;
518 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 return Eq;
520 case LESSEQUAL:
521 return LtE;
522 case GREATEREQUAL:
523 return GtE;
524 case NOTEQUAL:
525 return NotEq;
526 case NAME:
527 if (strcmp(STR(n), "in") == 0)
528 return In;
529 if (strcmp(STR(n), "is") == 0)
530 return Is;
531 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000532 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533 STR(n));
534 return 0;
535 }
536 }
537 else if (NCH(n) == 2) {
538 /* handle "not in" and "is not" */
539 switch (TYPE(CHILD(n, 0))) {
540 case NAME:
541 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
542 return NotIn;
543 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
544 return IsNot;
545 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000546 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
548 return 0;
549 }
550 }
Neal Norwitz79792652005-11-14 04:25:03 +0000551 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552 NCH(n));
553 return 0;
554}
555
556static asdl_seq *
557seq_for_testlist(struct compiling *c, const node *n)
558{
559 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000560 asdl_seq *seq;
561 expr_ty expression;
562 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 assert(TYPE(n) == testlist
564 || TYPE(n) == listmaker
565 || TYPE(n) == testlist_gexp
566 || TYPE(n) == testlist_safe
567 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568
569 seq = asdl_seq_new((NCH(n) + 1) / 2);
570 if (!seq)
571 return NULL;
572
573 for (i = 0; i < NCH(n); i += 2) {
574 REQ(CHILD(n, i), test);
575
576 expression = ast_for_expr(c, CHILD(n, i));
577 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000578 asdl_expr_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579 return NULL;
580 }
581
582 assert(i / 2 < seq->size);
583 asdl_seq_SET(seq, i / 2, expression);
584 }
585 return seq;
586}
587
588static expr_ty
589compiler_complex_args(const node *n)
590{
591 int i, len = (NCH(n) + 1) / 2;
592 expr_ty result;
593 asdl_seq *args = asdl_seq_new(len);
594 if (!args)
595 return NULL;
596
597 REQ(n, fplist);
598
599 for (i = 0; i < len; i++) {
600 const node *child = CHILD(CHILD(n, 2*i), 0);
601 expr_ty arg;
602 if (TYPE(child) == NAME) {
603 if (!strcmp(STR(child), "None")) {
604 ast_error(child, "assignment to None");
605 return NULL;
606 }
607 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child));
608 }
609 else
610 arg = compiler_complex_args(CHILD(CHILD(n, 2*i), 1));
611 set_context(arg, Store, n);
612 asdl_seq_SET(args, i, arg);
613 }
614
615 result = Tuple(args, Store, LINENO(n));
616 set_context(result, Store, n);
617 return result;
618}
619
620/* Create AST for argument list.
621
622 XXX TO DO:
623 - check for invalid argument lists like normal after default
624*/
625
626static arguments_ty
627ast_for_arguments(struct compiling *c, const node *n)
628{
629 /* parameters: '(' [varargslist] ')'
630 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
631 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
632 */
633 int i, n_args = 0, n_defaults = 0, found_default = 0;
634 asdl_seq *args, *defaults;
635 identifier vararg = NULL, kwarg = NULL;
636 node *ch;
637
638 if (TYPE(n) == parameters) {
639 if (NCH(n) == 2) /* () as argument list */
640 return arguments(NULL, NULL, NULL, NULL);
641 n = CHILD(n, 1);
642 }
643 REQ(n, varargslist);
644
645 /* first count the number of normal args & defaults */
646 for (i = 0; i < NCH(n); i++) {
647 ch = CHILD(n, i);
648 if (TYPE(ch) == fpdef) {
649 n_args++;
650 }
651 if (TYPE(ch) == EQUAL)
652 n_defaults++;
653 }
654 args = (n_args ? asdl_seq_new(n_args) : NULL);
655 if (!args && n_args)
656 return NULL; /* Don't need to go to NULL; nothing allocated */
657 defaults = (n_defaults ? asdl_seq_new(n_defaults) : NULL);
658 if (!defaults && n_defaults)
659 goto error;
660
661 /* fpdef: NAME | '(' fplist ')'
662 fplist: fpdef (',' fpdef)* [',']
663 */
664 i = 0;
665 while (i < NCH(n)) {
666 ch = CHILD(n, i);
667 switch (TYPE(ch)) {
668 case fpdef:
669 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
670 anything other than EQUAL or a comma? */
671 /* XXX Should NCH(n) check be made a separate check? */
672 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
673 asdl_seq_APPEND(defaults,
674 ast_for_expr(c, CHILD(n, i + 2)));
675 i += 2;
676 found_default = 1;
677 }
678 else if (found_default) {
679 ast_error(n,
680 "non-default argument follows default argument");
681 goto error;
682 }
683
684 if (NCH(ch) == 3) {
685 asdl_seq_APPEND(args,
686 compiler_complex_args(CHILD(ch, 1)));
687 }
688 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000689 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
691 ast_error(CHILD(ch, 0), "assignment to None");
692 goto error;
693 }
Armin Rigo31441302005-10-21 12:57:31 +0000694 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
695 Param, LINENO(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 if (!name)
697 goto error;
698 asdl_seq_APPEND(args, name);
699
700 }
701 i += 2; /* the name and the comma */
702 break;
703 case STAR:
704 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
705 ast_error(CHILD(n, i+1), "assignment to None");
706 goto error;
707 }
708 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
709 i += 3;
710 break;
711 case DOUBLESTAR:
712 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
713 ast_error(CHILD(n, i+1), "assignment to None");
714 goto error;
715 }
716 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
717 i += 3;
718 break;
719 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000720 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 "unexpected node in varargslist: %d @ %d",
722 TYPE(ch), i);
723 goto error;
724 }
725 }
726
727 return arguments(args, vararg, kwarg, defaults);
728
729 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000730 Py_XDECREF(vararg);
731 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 if (args)
Neal Norwitze76adcd2005-11-15 05:04:31 +0000733 asdl_expr_seq_free(args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 if (defaults)
Neal Norwitze76adcd2005-11-15 05:04:31 +0000735 asdl_expr_seq_free(defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736 return NULL;
737}
738
739static expr_ty
740ast_for_dotted_name(struct compiling *c, const node *n)
741{
742 expr_ty e = NULL;
743 expr_ty attrib = NULL;
744 identifier id = NULL;
745 int i;
746
747 REQ(n, dotted_name);
748
749 id = NEW_IDENTIFIER(CHILD(n, 0));
750 if (!id)
751 goto error;
752 e = Name(id, Load, LINENO(n));
753 if (!e)
754 goto error;
755 id = NULL;
756
757 for (i = 2; i < NCH(n); i+=2) {
758 id = NEW_IDENTIFIER(CHILD(n, i));
759 if (!id)
760 goto error;
761 attrib = Attribute(e, id, Load, LINENO(CHILD(n, i)));
762 if (!attrib)
763 goto error;
764 e = attrib;
765 attrib = NULL;
766 }
767
768 return e;
769
770 error:
771 Py_XDECREF(id);
772 free_expr(e);
773 return NULL;
774}
775
776static expr_ty
777ast_for_decorator(struct compiling *c, const node *n)
778{
779 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
780 expr_ty d = NULL;
781 expr_ty name_expr = NULL;
782
783 REQ(n, decorator);
784
785 if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6)
786 || TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) {
787 ast_error(n, "Invalid decorator node");
788 goto error;
789 }
790
791 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
792 if (!name_expr)
793 goto error;
794
795 if (NCH(n) == 3) { /* No arguments */
796 d = name_expr;
797 name_expr = NULL;
798 }
799 else if (NCH(n) == 5) { /* Call with no arguments */
800 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n));
801 if (!d)
802 goto error;
803 name_expr = NULL;
804 }
805 else {
806 d = ast_for_call(c, CHILD(n, 3), name_expr);
807 if (!d)
808 goto error;
809 name_expr = NULL;
810 }
811
812 return d;
813
814 error:
815 free_expr(name_expr);
816 free_expr(d);
817 return NULL;
818}
819
820static asdl_seq*
821ast_for_decorators(struct compiling *c, const node *n)
822{
823 asdl_seq* decorator_seq = NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000824 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 int i;
826
827 REQ(n, decorators);
828
829 decorator_seq = asdl_seq_new(NCH(n));
830 if (!decorator_seq)
831 return NULL;
832
833 for (i = 0; i < NCH(n); i++) {
834 d = ast_for_decorator(c, CHILD(n, i));
835 if (!d)
836 goto error;
837 asdl_seq_APPEND(decorator_seq, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 }
839 return decorator_seq;
840 error:
841 asdl_expr_seq_free(decorator_seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 return NULL;
843}
844
845static stmt_ty
846ast_for_funcdef(struct compiling *c, const node *n)
847{
848 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
849 identifier name = NULL;
850 arguments_ty args = NULL;
851 asdl_seq *body = NULL;
852 asdl_seq *decorator_seq = NULL;
853 int name_i;
854
855 REQ(n, funcdef);
856
857 if (NCH(n) == 6) { /* decorators are present */
858 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
859 if (!decorator_seq)
860 goto error;
861 name_i = 2;
862 }
863 else {
864 name_i = 1;
865 }
866
867 name = NEW_IDENTIFIER(CHILD(n, name_i));
868 if (!name)
869 goto error;
870 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000871 ast_error(CHILD(n, name_i), "assignment to None");
872 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 }
874 args = ast_for_arguments(c, CHILD(n, name_i + 1));
875 if (!args)
876 goto error;
877 body = ast_for_suite(c, CHILD(n, name_i + 3));
878 if (!body)
879 goto error;
880
881 return FunctionDef(name, args, body, decorator_seq, LINENO(n));
882
883error:
884 asdl_stmt_seq_free(body);
885 asdl_expr_seq_free(decorator_seq);
886 free_arguments(args);
887 Py_XDECREF(name);
888 return NULL;
889}
890
891static expr_ty
892ast_for_lambdef(struct compiling *c, const node *n)
893{
894 /* lambdef: 'lambda' [varargslist] ':' test */
895 arguments_ty args;
896 expr_ty expression;
897
898 if (NCH(n) == 3) {
899 args = arguments(NULL, NULL, NULL, NULL);
900 if (!args)
901 return NULL;
902 expression = ast_for_expr(c, CHILD(n, 2));
903 if (!expression) {
904 free_arguments(args);
905 return NULL;
906 }
907 }
908 else {
909 args = ast_for_arguments(c, CHILD(n, 1));
910 if (!args)
911 return NULL;
912 expression = ast_for_expr(c, CHILD(n, 3));
913 if (!expression) {
914 free_arguments(args);
915 return NULL;
916 }
917 }
918
919 return Lambda(args, expression, LINENO(n));
920}
921
922/* Count the number of 'for' loop in a list comprehension.
923
924 Helper for ast_for_listcomp().
925*/
926
927static int
928count_list_fors(const node *n)
929{
930 int n_fors = 0;
931 node *ch = CHILD(n, 1);
932
933 count_list_for:
934 n_fors++;
935 REQ(ch, list_for);
936 if (NCH(ch) == 5)
937 ch = CHILD(ch, 4);
938 else
939 return n_fors;
940 count_list_iter:
941 REQ(ch, list_iter);
942 ch = CHILD(ch, 0);
943 if (TYPE(ch) == list_for)
944 goto count_list_for;
945 else if (TYPE(ch) == list_if) {
946 if (NCH(ch) == 3) {
947 ch = CHILD(ch, 2);
948 goto count_list_iter;
949 }
950 else
951 return n_fors;
952 }
953 else {
954 /* Should never be reached */
Neal Norwitz7b3d5e12005-11-13 21:17:28 +0000955 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 return -1;
957 }
958}
959
960/* Count the number of 'if' statements in a list comprehension.
961
962 Helper for ast_for_listcomp().
963*/
964
965static int
966count_list_ifs(const node *n)
967{
968 int n_ifs = 0;
969
970 count_list_iter:
971 REQ(n, list_iter);
972 if (TYPE(CHILD(n, 0)) == list_for)
973 return n_ifs;
974 n = CHILD(n, 0);
975 REQ(n, list_if);
976 n_ifs++;
977 if (NCH(n) == 2)
978 return n_ifs;
979 n = CHILD(n, 2);
980 goto count_list_iter;
981}
982
983static expr_ty
984ast_for_listcomp(struct compiling *c, const node *n)
985{
986 /* listmaker: test ( list_for | (',' test)* [','] )
987 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
988 list_iter: list_for | list_if
989 list_if: 'if' test [list_iter]
990 testlist_safe: test [(',' test)+ [',']]
991 */
992 expr_ty elt;
993 asdl_seq *listcomps;
994 int i, n_fors;
995 node *ch;
996
997 REQ(n, listmaker);
998 assert(NCH(n) > 1);
999
1000 elt = ast_for_expr(c, CHILD(n, 0));
1001 if (!elt)
1002 return NULL;
1003
1004 n_fors = count_list_fors(n);
1005 if (n_fors == -1)
1006 return NULL;
1007
1008 listcomps = asdl_seq_new(n_fors);
1009 if (!listcomps) {
1010 free_expr(elt);
1011 return NULL;
1012 }
1013
1014 ch = CHILD(n, 1);
1015 for (i = 0; i < n_fors; i++) {
1016 comprehension_ty lc;
1017 asdl_seq *t;
1018 expr_ty expression;
1019
1020 REQ(ch, list_for);
1021
1022 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
1023 if (!t) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001024 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 free_expr(elt);
1026 return NULL;
1027 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001028 expression = ast_for_testlist(c, CHILD(ch, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001030 asdl_expr_seq_free(t);
1031 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 free_expr(elt);
1033 return NULL;
1034 }
1035
1036 if (asdl_seq_LEN(t) == 1)
1037 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL);
1038 else
1039 lc = comprehension(Tuple(t, Store, LINENO(ch)), expression, NULL);
1040
1041 if (!lc) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001042 asdl_expr_seq_free(t);
1043 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 free_expr(expression);
1045 free_expr(elt);
1046 return NULL;
1047 }
1048
1049 if (NCH(ch) == 5) {
1050 int j, n_ifs;
1051 asdl_seq *ifs;
1052
1053 ch = CHILD(ch, 4);
1054 n_ifs = count_list_ifs(ch);
1055 if (n_ifs == -1) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001056 free_comprehension(lc);
1057 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 free_expr(elt);
1059 return NULL;
1060 }
1061
1062 ifs = asdl_seq_new(n_ifs);
1063 if (!ifs) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001064 free_comprehension(lc);
1065 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 free_expr(elt);
1067 return NULL;
1068 }
1069
1070 for (j = 0; j < n_ifs; j++) {
1071 REQ(ch, list_iter);
1072
1073 ch = CHILD(ch, 0);
1074 REQ(ch, list_if);
1075
1076 asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1)));
1077 if (NCH(ch) == 3)
1078 ch = CHILD(ch, 2);
1079 }
1080 /* on exit, must guarantee that ch is a list_for */
1081 if (TYPE(ch) == list_iter)
1082 ch = CHILD(ch, 0);
1083 lc->ifs = ifs;
1084 }
1085 asdl_seq_APPEND(listcomps, lc);
1086 }
1087
1088 return ListComp(elt, listcomps, LINENO(n));
1089}
1090
1091/*
1092 Count the number of 'for' loops in a generator expression.
1093
1094 Helper for ast_for_genexp().
1095*/
1096
1097static int
1098count_gen_fors(const node *n)
1099{
1100 int n_fors = 0;
1101 node *ch = CHILD(n, 1);
1102
1103 count_gen_for:
1104 n_fors++;
1105 REQ(ch, gen_for);
1106 if (NCH(ch) == 5)
1107 ch = CHILD(ch, 4);
1108 else
1109 return n_fors;
1110 count_gen_iter:
1111 REQ(ch, gen_iter);
1112 ch = CHILD(ch, 0);
1113 if (TYPE(ch) == gen_for)
1114 goto count_gen_for;
1115 else if (TYPE(ch) == gen_if) {
1116 if (NCH(ch) == 3) {
1117 ch = CHILD(ch, 2);
1118 goto count_gen_iter;
1119 }
1120 else
1121 return n_fors;
1122 }
1123 else {
1124 /* Should never be reached */
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00001125 PyErr_SetString(PyExc_SystemError,
1126 "logic error in count_gen_fors");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 return -1;
1128 }
1129}
1130
1131/* Count the number of 'if' statements in a generator expression.
1132
1133 Helper for ast_for_genexp().
1134*/
1135
1136static int
1137count_gen_ifs(const node *n)
1138{
1139 int n_ifs = 0;
1140
1141 while (1) {
1142 REQ(n, gen_iter);
1143 if (TYPE(CHILD(n, 0)) == gen_for)
1144 return n_ifs;
1145 n = CHILD(n, 0);
1146 REQ(n, gen_if);
1147 n_ifs++;
1148 if (NCH(n) == 2)
1149 return n_ifs;
1150 n = CHILD(n, 2);
1151 }
1152}
1153
1154static expr_ty
1155ast_for_genexp(struct compiling *c, const node *n)
1156{
1157 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1158 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1159 expr_ty elt;
1160 asdl_seq *genexps;
1161 int i, n_fors;
1162 node *ch;
1163
1164 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1165 assert(NCH(n) > 1);
1166
1167 elt = ast_for_expr(c, CHILD(n, 0));
1168 if (!elt)
1169 return NULL;
1170
1171 n_fors = count_gen_fors(n);
1172 if (n_fors == -1)
1173 return NULL;
1174
1175 genexps = asdl_seq_new(n_fors);
1176 if (!genexps) {
1177 free_expr(elt);
1178 return NULL;
1179 }
1180
1181 ch = CHILD(n, 1);
1182 for (i = 0; i < n_fors; i++) {
1183 comprehension_ty ge;
1184 asdl_seq *t;
1185 expr_ty expression;
1186
1187 REQ(ch, gen_for);
1188
1189 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
1190 if (!t) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001191 asdl_comprehension_seq_free(genexps);
1192 asdl_expr_seq_free(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 free_expr(elt);
1194 return NULL;
1195 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001196 expression = ast_for_expr(c, CHILD(ch, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001198 asdl_comprehension_seq_free(genexps);
1199 asdl_expr_seq_free(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 free_expr(elt);
1201 return NULL;
1202 }
1203
1204 if (asdl_seq_LEN(t) == 1)
1205 ge = comprehension(asdl_seq_GET(t, 0), expression,
1206 NULL);
1207 else
1208 ge = comprehension(Tuple(t, Store, LINENO(ch)),
1209 expression, NULL);
1210
1211 if (!ge) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001212 asdl_comprehension_seq_free(genexps);
1213 asdl_expr_seq_free(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 free_expr(elt);
1215 return NULL;
1216 }
1217
1218 if (NCH(ch) == 5) {
1219 int j, n_ifs;
1220 asdl_seq *ifs;
1221
1222 ch = CHILD(ch, 4);
1223 n_ifs = count_gen_ifs(ch);
1224 if (n_ifs == -1) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001225 asdl_comprehension_seq_free(genexps);
1226 free_comprehension(ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 free_expr(elt);
1228 return NULL;
1229 }
1230
1231 ifs = asdl_seq_new(n_ifs);
1232 if (!ifs) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001233 asdl_comprehension_seq_free(genexps);
1234 free_comprehension(ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 free_expr(elt);
1236 return NULL;
1237 }
1238
1239 for (j = 0; j < n_ifs; j++) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001240 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 REQ(ch, gen_iter);
1242 ch = CHILD(ch, 0);
1243 REQ(ch, gen_if);
1244
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001245 expression = ast_for_expr(c, CHILD(ch, 1));
1246 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001247 asdl_expr_seq_free(ifs);
1248 asdl_comprehension_seq_free(genexps);
1249 free_comprehension(ge);
1250 free_expr(elt);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001251 return NULL;
1252 }
1253 asdl_seq_APPEND(ifs, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 if (NCH(ch) == 3)
1255 ch = CHILD(ch, 2);
1256 }
1257 /* on exit, must guarantee that ch is a gen_for */
1258 if (TYPE(ch) == gen_iter)
1259 ch = CHILD(ch, 0);
1260 ge->ifs = ifs;
1261 }
1262 asdl_seq_APPEND(genexps, ge);
1263 }
1264
1265 return GeneratorExp(elt, genexps, LINENO(n));
1266}
1267
1268static expr_ty
1269ast_for_atom(struct compiling *c, const node *n)
1270{
1271 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1272 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1273 */
1274 node *ch = CHILD(n, 0);
1275
1276 switch (TYPE(ch)) {
1277 case NAME:
1278 /* All names start in Load context, but may later be
1279 changed. */
1280 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n));
1281 case STRING: {
1282 PyObject *str = parsestrplus(c, n);
1283
1284 if (!str)
1285 return NULL;
1286
1287 return Str(str, LINENO(n));
1288 }
1289 case NUMBER: {
1290 PyObject *pynum = parsenumber(STR(ch));
1291
1292 if (!pynum)
1293 return NULL;
1294
1295 return Num(pynum, LINENO(n));
1296 }
1297 case LPAR: /* some parenthesized expressions */
1298 ch = CHILD(n, 1);
1299
1300 if (TYPE(ch) == RPAR)
1301 return Tuple(NULL, Load, LINENO(n));
1302
1303 if (TYPE(ch) == yield_expr)
1304 return ast_for_expr(c, ch);
1305
1306 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1307 return ast_for_genexp(c, ch);
1308
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001309 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 case LSQB: /* list (or list comprehension) */
1311 ch = CHILD(n, 1);
1312
1313 if (TYPE(ch) == RSQB)
1314 return List(NULL, Load, LINENO(n));
1315
1316 REQ(ch, listmaker);
1317 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1318 asdl_seq *elts = seq_for_testlist(c, ch);
1319
1320 if (!elts)
1321 return NULL;
1322
1323 return List(elts, Load, LINENO(n));
1324 }
1325 else
1326 return ast_for_listcomp(c, ch);
1327 case LBRACE: {
1328 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1329 int i, size;
1330 asdl_seq *keys, *values;
1331
1332 ch = CHILD(n, 1);
1333 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1334 keys = asdl_seq_new(size);
1335 if (!keys)
1336 return NULL;
1337
1338 values = asdl_seq_new(size);
1339 if (!values) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001340 asdl_seq_free(keys); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 return NULL;
1342 }
1343
1344 for (i = 0; i < NCH(ch); i += 4) {
1345 expr_ty expression;
1346
1347 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001348 if (!expression) {
1349 asdl_expr_seq_free(keys);
1350 asdl_expr_seq_free(values);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001352 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353
1354 asdl_seq_SET(keys, i / 4, expression);
1355
1356 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001357 if (!expression) {
1358 asdl_expr_seq_free(keys);
1359 asdl_expr_seq_free(values);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001361 }
1362
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 asdl_seq_SET(values, i / 4, expression);
1364 }
1365 return Dict(keys, values, LINENO(n));
1366 }
1367 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001368 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369
1370 if (!expression)
1371 return NULL;
1372
1373 return Repr(expression, LINENO(n));
1374 }
1375 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001376 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 return NULL;
1378 }
1379}
1380
1381static slice_ty
1382ast_for_slice(struct compiling *c, const node *n)
1383{
1384 node *ch;
1385 expr_ty lower = NULL, upper = NULL, step = NULL;
1386
1387 REQ(n, subscript);
1388
1389 /*
1390 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1391 sliceop: ':' [test]
1392 */
1393 ch = CHILD(n, 0);
1394 if (TYPE(ch) == DOT)
1395 return Ellipsis();
1396
1397 if (NCH(n) == 1 && TYPE(ch) == test) {
1398 /* 'step' variable hold no significance in terms of being used over
1399 other vars */
1400 step = ast_for_expr(c, ch);
1401 if (!step)
1402 return NULL;
1403
1404 return Index(step);
1405 }
1406
1407 if (TYPE(ch) == test) {
1408 lower = ast_for_expr(c, ch);
1409 if (!lower)
1410 return NULL;
1411 }
1412
1413 /* If there's an upper bound it's in the second or third position. */
1414 if (TYPE(ch) == COLON) {
1415 if (NCH(n) > 1) {
1416 node *n2 = CHILD(n, 1);
1417
1418 if (TYPE(n2) == test) {
1419 upper = ast_for_expr(c, n2);
1420 if (!upper)
1421 return NULL;
1422 }
1423 }
1424 } else if (NCH(n) > 2) {
1425 node *n2 = CHILD(n, 2);
1426
1427 if (TYPE(n2) == test) {
1428 upper = ast_for_expr(c, n2);
1429 if (!upper)
1430 return NULL;
1431 }
1432 }
1433
1434 ch = CHILD(n, NCH(n) - 1);
1435 if (TYPE(ch) == sliceop) {
1436 if (NCH(ch) == 1)
1437 /* XXX: If only 1 child, then should just be a colon. Should we
1438 just skip assigning and just get to the return? */
1439 ch = CHILD(ch, 0);
1440 else
1441 ch = CHILD(ch, 1);
1442 if (TYPE(ch) == test) {
1443 step = ast_for_expr(c, ch);
1444 if (!step)
1445 return NULL;
1446 }
1447 }
1448
1449 return Slice(lower, upper, step);
1450}
1451
1452static expr_ty
1453ast_for_binop(struct compiling *c, const node *n)
1454{
1455 /* Must account for a sequence of expressions.
1456 How should A op B op C by represented?
1457 BinOp(BinOp(A, op, B), op, C).
1458 */
1459
1460 int i, nops;
1461 expr_ty expr1, expr2, result;
1462 operator_ty operator;
1463
1464 expr1 = ast_for_expr(c, CHILD(n, 0));
1465 if (!expr1)
1466 return NULL;
1467
1468 expr2 = ast_for_expr(c, CHILD(n, 2));
1469 if (!expr2)
1470 return NULL;
1471
1472 operator = get_operator(CHILD(n, 1));
1473 if (!operator)
1474 return NULL;
1475
1476 result = BinOp(expr1, operator, expr2, LINENO(n));
1477 if (!result)
1478 return NULL;
1479
1480 nops = (NCH(n) - 1) / 2;
1481 for (i = 1; i < nops; i++) {
1482 expr_ty tmp_result, tmp;
1483 const node* next_oper = CHILD(n, i * 2 + 1);
1484
1485 operator = get_operator(next_oper);
1486 if (!operator)
1487 return NULL;
1488
1489 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1490 if (!tmp)
1491 return NULL;
1492
1493 tmp_result = BinOp(result, operator, tmp,
1494 LINENO(next_oper));
1495 if (!tmp)
1496 return NULL;
1497 result = tmp_result;
1498 }
1499 return result;
1500}
1501
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001502static expr_ty
1503ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1504{
1505 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1506 expr_ty e;
1507 REQ(n, trailer);
1508 if (TYPE(CHILD(n, 0)) == LPAR) {
1509 if (NCH(n) == 2)
1510 e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n));
1511 else
1512 e = ast_for_call(c, CHILD(n, 1), left_expr);
1513 }
1514 else if (TYPE(CHILD(n, 0)) == LSQB) {
1515 REQ(CHILD(n, 2), RSQB);
1516 n = CHILD(n, 1);
1517 if (NCH(n) <= 2) {
1518 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1519 if (!slc)
1520 return NULL;
1521 e = Subscript(left_expr, slc, Load, LINENO(n));
1522 if (!e) {
1523 free_slice(slc);
1524 return NULL;
1525 }
1526 }
1527 else {
1528 int j;
1529 slice_ty slc;
1530 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2);
1531 if (!slices)
1532 return NULL;
1533 for (j = 0; j < NCH(n); j += 2) {
1534 slc = ast_for_slice(c, CHILD(n, j));
1535 if (!slc) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001536 for (j = j / 2; j >= 0; j--)
1537 free_slice(asdl_seq_GET(slices, j));
1538 asdl_seq_free(slices); /* ok */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001539 return NULL;
1540 }
1541 asdl_seq_SET(slices, j / 2, slc);
1542 }
1543 e = Subscript(left_expr, ExtSlice(slices), Load, LINENO(n));
1544 if (!e) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001545 for (j = 0; j < asdl_seq_LEN(slices); j++)
1546 free_slice(asdl_seq_GET(slices, j));
1547 asdl_seq_free(slices); /* ok */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001548 return NULL;
1549 }
1550 }
1551 }
1552 else {
1553 assert(TYPE(CHILD(n, 0)) == DOT);
1554 e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n));
1555 }
1556 return e;
1557}
1558
1559static expr_ty
1560ast_for_power(struct compiling *c, const node *n)
1561{
1562 /* power: atom trailer* ('**' factor)*
1563 */
1564 int i;
1565 expr_ty e, tmp;
1566 REQ(n, power);
1567 e = ast_for_atom(c, CHILD(n, 0));
1568 if (!e)
1569 return NULL;
1570 if (NCH(n) == 1)
1571 return e;
1572 for (i = 1; i < NCH(n); i++) {
1573 node *ch = CHILD(n, i);
1574 if (TYPE(ch) != trailer)
1575 break;
1576 tmp = ast_for_trailer(c, ch, e);
1577 if (!tmp) {
1578 free_expr(e);
1579 return NULL;
1580 }
1581 e = tmp;
1582 }
1583 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1584 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1585 if (!f) {
1586 free_expr(e);
1587 return NULL;
1588 }
1589 tmp = BinOp(e, Pow, f, LINENO(n));
1590 if (!tmp) {
Neal Norwitz6b347892005-11-15 07:17:53 +00001591 free_expr(f);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001592 free_expr(e);
1593 return NULL;
1594 }
1595 e = tmp;
1596 }
1597 return e;
1598}
1599
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600/* Do not name a variable 'expr'! Will cause a compile error.
1601*/
1602
1603static expr_ty
1604ast_for_expr(struct compiling *c, const node *n)
1605{
1606 /* handle the full range of simple expressions
1607 test: and_test ('or' and_test)* | lambdef
1608 and_test: not_test ('and' not_test)*
1609 not_test: 'not' not_test | comparison
1610 comparison: expr (comp_op expr)*
1611 expr: xor_expr ('|' xor_expr)*
1612 xor_expr: and_expr ('^' and_expr)*
1613 and_expr: shift_expr ('&' shift_expr)*
1614 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1615 arith_expr: term (('+'|'-') term)*
1616 term: factor (('*'|'/'|'%'|'//') factor)*
1617 factor: ('+'|'-'|'~') factor | power
1618 power: atom trailer* ('**' factor)*
1619 */
1620
1621 asdl_seq *seq;
1622 int i;
1623
1624 loop:
1625 switch (TYPE(n)) {
1626 case test:
1627 if (TYPE(CHILD(n, 0)) == lambdef)
1628 return ast_for_lambdef(c, CHILD(n, 0));
1629 /* Fall through to and_test */
1630 case and_test:
1631 if (NCH(n) == 1) {
1632 n = CHILD(n, 0);
1633 goto loop;
1634 }
1635 seq = asdl_seq_new((NCH(n) + 1) / 2);
1636 if (!seq)
1637 return NULL;
1638 for (i = 0; i < NCH(n); i += 2) {
1639 expr_ty e = ast_for_expr(c, CHILD(n, i));
1640 if (!e)
1641 return NULL;
1642 asdl_seq_SET(seq, i / 2, e);
1643 }
1644 if (!strcmp(STR(CHILD(n, 1)), "and"))
1645 return BoolOp(And, seq, LINENO(n));
1646 else {
1647 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1648 return BoolOp(Or, seq, LINENO(n));
1649 }
1650 break;
1651 case not_test:
1652 if (NCH(n) == 1) {
1653 n = CHILD(n, 0);
1654 goto loop;
1655 }
1656 else {
1657 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1658 if (!expression)
1659 return NULL;
1660
1661 return UnaryOp(Not, expression, LINENO(n));
1662 }
1663 case comparison:
1664 if (NCH(n) == 1) {
1665 n = CHILD(n, 0);
1666 goto loop;
1667 }
1668 else {
1669 expr_ty expression;
1670 asdl_seq *ops, *cmps;
1671 ops = asdl_seq_new(NCH(n) / 2);
1672 if (!ops)
1673 return NULL;
1674 cmps = asdl_seq_new(NCH(n) / 2);
1675 if (!cmps) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001676 asdl_seq_free(ops); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 return NULL;
1678 }
1679 for (i = 1; i < NCH(n); i += 2) {
1680 /* XXX cmpop_ty is just an enum */
1681 cmpop_ty operator;
1682
1683 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001684 if (!operator) {
1685 asdl_expr_seq_free(ops);
1686 asdl_expr_seq_free(cmps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001688 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689
1690 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001691 if (!expression) {
1692 asdl_expr_seq_free(ops);
1693 asdl_expr_seq_free(cmps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001695 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696
1697 asdl_seq_SET(ops, i / 2, (void *)operator);
1698 asdl_seq_SET(cmps, i / 2, expression);
1699 }
1700 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001701 if (!expression) {
1702 asdl_expr_seq_free(ops);
1703 asdl_expr_seq_free(cmps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001705 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706
1707 return Compare(expression, ops, cmps, LINENO(n));
1708 }
1709 break;
1710
1711 /* The next five cases all handle BinOps. The main body of code
1712 is the same in each case, but the switch turned inside out to
1713 reuse the code for each type of operator.
1714 */
1715 case expr:
1716 case xor_expr:
1717 case and_expr:
1718 case shift_expr:
1719 case arith_expr:
1720 case term:
1721 if (NCH(n) == 1) {
1722 n = CHILD(n, 0);
1723 goto loop;
1724 }
1725 return ast_for_binop(c, n);
1726 case yield_expr: {
1727 expr_ty exp = NULL;
1728 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001729 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 if (!exp)
1731 return NULL;
1732 }
1733 return Yield(exp, LINENO(n));
1734 }
1735 case factor: {
1736 expr_ty expression;
1737
1738 if (NCH(n) == 1) {
1739 n = CHILD(n, 0);
1740 goto loop;
1741 }
1742
1743 expression = ast_for_expr(c, CHILD(n, 1));
1744 if (!expression)
1745 return NULL;
1746
1747 switch (TYPE(CHILD(n, 0))) {
1748 case PLUS:
1749 return UnaryOp(UAdd, expression, LINENO(n));
1750 case MINUS:
1751 return UnaryOp(USub, expression, LINENO(n));
1752 case TILDE:
1753 return UnaryOp(Invert, expression, LINENO(n));
1754 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001755 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1756 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 break;
1758 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001759 case power:
1760 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001762 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 return NULL;
1764 }
1765 /* should never get here */
1766 return NULL;
1767}
1768
1769static expr_ty
1770ast_for_call(struct compiling *c, const node *n, expr_ty func)
1771{
1772 /*
1773 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1774 | '**' test)
1775 argument: [test '='] test [gen_for] # Really [keyword '='] test
1776 */
1777
1778 int i, nargs, nkeywords, ngens;
1779 asdl_seq *args = NULL;
1780 asdl_seq *keywords = NULL;
1781 expr_ty vararg = NULL, kwarg = NULL;
1782
1783 REQ(n, arglist);
1784
1785 nargs = 0;
1786 nkeywords = 0;
1787 ngens = 0;
1788 for (i = 0; i < NCH(n); i++) {
1789 node *ch = CHILD(n, i);
1790 if (TYPE(ch) == argument) {
1791 if (NCH(ch) == 1)
1792 nargs++;
1793 else if (TYPE(CHILD(ch, 1)) == gen_for)
1794 ngens++;
1795 else
1796 nkeywords++;
1797 }
1798 }
1799 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1800 ast_error(n, "Generator expression must be parenthesised "
1801 "if not sole argument");
1802 return NULL;
1803 }
1804
1805 if (nargs + nkeywords + ngens > 255) {
1806 ast_error(n, "more than 255 arguments");
1807 return NULL;
1808 }
1809
1810 args = asdl_seq_new(nargs + ngens);
1811 if (!args)
1812 goto error;
1813 keywords = asdl_seq_new(nkeywords);
1814 if (!keywords)
1815 goto error;
1816 nargs = 0;
1817 nkeywords = 0;
1818 for (i = 0; i < NCH(n); i++) {
1819 node *ch = CHILD(n, i);
1820 if (TYPE(ch) == argument) {
1821 expr_ty e;
1822 if (NCH(ch) == 1) {
1823 e = ast_for_expr(c, CHILD(ch, 0));
1824 if (!e)
1825 goto error;
1826 asdl_seq_SET(args, nargs++, e);
1827 }
1828 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1829 e = ast_for_genexp(c, ch);
1830 if (!e)
1831 goto error;
1832 asdl_seq_SET(args, nargs++, e);
1833 }
1834 else {
1835 keyword_ty kw;
1836 identifier key;
1837
1838 /* CHILD(ch, 0) is test, but must be an identifier? */
1839 e = ast_for_expr(c, CHILD(ch, 0));
1840 if (!e)
1841 goto error;
1842 /* f(lambda x: x[0] = 3) ends up getting parsed with
1843 * LHS test = lambda x: x[0], and RHS test = 3.
1844 * SF bug 132313 points out that complaining about a keyword
1845 * then is very confusing.
1846 */
1847 if (e->kind == Lambda_kind) {
1848 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
1849 goto error;
1850 } else if (e->kind != Name_kind) {
1851 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1852 goto error;
1853 }
1854 key = e->v.Name.id;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001855 free(e); /* XXX: is free correct here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 e = ast_for_expr(c, CHILD(ch, 2));
1857 if (!e)
1858 goto error;
1859 kw = keyword(key, e);
1860 if (!kw)
1861 goto error;
1862 asdl_seq_SET(keywords, nkeywords++, kw);
1863 }
1864 }
1865 else if (TYPE(ch) == STAR) {
1866 vararg = ast_for_expr(c, CHILD(n, i+1));
1867 i++;
1868 }
1869 else if (TYPE(ch) == DOUBLESTAR) {
1870 kwarg = ast_for_expr(c, CHILD(n, i+1));
1871 i++;
1872 }
1873 }
1874
1875 return Call(func, args, keywords, vararg, kwarg, LINENO(n));
1876
1877 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +00001878 free_expr(vararg);
1879 free_expr(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 if (args)
Neal Norwitze76adcd2005-11-15 05:04:31 +00001881 asdl_expr_seq_free(args);
1882 if (keywords) {
1883 for (i = 0; i < asdl_seq_LEN(keywords); i++)
1884 free_keyword(asdl_seq_GET(keywords, i));
1885 asdl_seq_free(keywords); /* ok */
1886 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 return NULL;
1888}
1889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001891ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001893 /* testlist_gexp: test (',' test)* [','] */
1894 /* testlist: test (',' test)* [','] */
1895 /* testlist_safe: test (',' test)+ [','] */
1896 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001898 if (TYPE(n) == testlist_gexp) {
1899 if (NCH(n) > 1)
1900 assert(TYPE(CHILD(n, 1)) != gen_for);
1901 }
1902 else {
1903 assert(TYPE(n) == testlist ||
1904 TYPE(n) == testlist_safe ||
1905 TYPE(n) == testlist1);
1906 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 if (NCH(n) == 1)
1908 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 else {
1910 asdl_seq *tmp = seq_for_testlist(c, n);
1911 if (!tmp)
1912 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 return Tuple(tmp, Load, LINENO(n));
1914 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001915}
1916
1917static expr_ty
1918ast_for_testlist_gexp(struct compiling *c, const node* n)
1919{
1920 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1921 /* argument: test [ gen_for ] */
1922 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
1923 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) {
1924 return ast_for_genexp(c, n);
1925 }
1926 else
1927 return ast_for_testlist(c, n);
1928}
1929
1930/* like ast_for_testlist() but returns a sequence */
1931static asdl_seq*
1932ast_for_class_bases(struct compiling *c, const node* n)
1933{
1934 /* testlist: test (',' test)* [','] */
1935 assert(NCH(n) > 0);
1936 REQ(n, testlist);
1937 if (NCH(n) == 1) {
1938 expr_ty base;
1939 asdl_seq *bases = asdl_seq_new(1);
1940 if (!bases)
1941 return NULL;
1942 base = ast_for_expr(c, CHILD(n, 0));
1943 if (!base) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001944 asdl_seq_free(bases); /* ok */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001945 return NULL;
1946 }
1947 asdl_seq_SET(bases, 0, base);
1948 return bases;
1949 }
1950 else {
1951 return seq_for_testlist(c, n);
1952 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953}
1954
1955static stmt_ty
1956ast_for_expr_stmt(struct compiling *c, const node *n)
1957{
1958 REQ(n, expr_stmt);
1959 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1960 | ('=' (yield_expr|testlist))*)
1961 testlist: test (',' test)* [',']
1962 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1963 | '<<=' | '>>=' | '**=' | '//='
1964 test: ... here starts the operator precendence dance
1965 */
1966
1967 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001968 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 if (!e)
1970 return NULL;
1971
1972 return Expr(e, LINENO(n));
1973 }
1974 else if (TYPE(CHILD(n, 1)) == augassign) {
1975 expr_ty expr1, expr2;
1976 operator_ty operator;
1977 node *ch = CHILD(n, 0);
1978
1979 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001980 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 else
1982 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch));
1983
1984 if (!expr1)
1985 return NULL;
1986 if (expr1->kind == GeneratorExp_kind) {
Neal Norwitze8c05362005-11-14 00:18:03 +00001987 free_expr(expr1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 ast_error(ch, "augmented assignment to generator "
1989 "expression not possible");
1990 return NULL;
1991 }
1992 if (expr1->kind == Name_kind) {
1993 char *var_name = PyString_AS_STRING(expr1->v.Name.id);
1994 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
Neal Norwitze8c05362005-11-14 00:18:03 +00001995 free_expr(expr1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 ast_error(ch, "assignment to None");
1997 return NULL;
1998 }
1999 }
2000
2001 ch = CHILD(n, 2);
2002 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002003 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 else
2005 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch));
Neal Norwitze8c05362005-11-14 00:18:03 +00002006 if (!expr2) {
2007 free_expr(expr1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 return NULL;
Neal Norwitze8c05362005-11-14 00:18:03 +00002009 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010
2011 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitze8c05362005-11-14 00:18:03 +00002012 if (!operator) {
2013 free_expr(expr1);
2014 free_expr(expr2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 return NULL;
Neal Norwitze8c05362005-11-14 00:18:03 +00002016 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017
2018 return AugAssign(expr1, operator, expr2, LINENO(n));
2019 }
2020 else {
2021 int i;
2022 asdl_seq *targets;
2023 node *value;
2024 expr_ty expression;
2025
2026 /* a normal assignment */
2027 REQ(CHILD(n, 1), EQUAL);
2028 targets = asdl_seq_new(NCH(n) / 2);
2029 if (!targets)
2030 return NULL;
2031 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002032 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 node *ch = CHILD(n, i);
2034 if (TYPE(ch) == yield_expr) {
2035 ast_error(ch, "assignment to yield expression not possible");
2036 goto error;
2037 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002038 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039
2040 /* set context to assign */
2041 if (!e)
2042 goto error;
2043
2044 if (!set_context(e, Store, CHILD(n, i))) {
2045 free_expr(e);
2046 goto error;
2047 }
2048
2049 asdl_seq_SET(targets, i / 2, e);
2050 }
2051 value = CHILD(n, NCH(n) - 1);
2052 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002053 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 else
2055 expression = ast_for_expr(c, value);
2056 if (!expression)
Neal Norwitze8c05362005-11-14 00:18:03 +00002057 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 return Assign(targets, expression, LINENO(n));
2059 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +00002060 asdl_expr_seq_free(targets);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 }
2062 return NULL;
2063}
2064
2065static stmt_ty
2066ast_for_print_stmt(struct compiling *c, const node *n)
2067{
2068 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2069 | '>>' test [ (',' test)+ [','] ] )
2070 */
2071 expr_ty dest = NULL, expression;
2072 asdl_seq *seq;
2073 bool nl;
2074 int i, start = 1;
2075
2076 REQ(n, print_stmt);
2077 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2078 dest = ast_for_expr(c, CHILD(n, 2));
2079 if (!dest)
2080 return NULL;
2081 start = 4;
2082 }
2083 seq = asdl_seq_new((NCH(n) + 1 - start) / 2);
2084 if (!seq)
2085 return NULL;
2086 for (i = start; i < NCH(n); i += 2) {
2087 expression = ast_for_expr(c, CHILD(n, i));
2088 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002089 free_expr(dest);
2090 asdl_expr_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 return NULL;
2092 }
2093
2094 asdl_seq_APPEND(seq, expression);
2095 }
2096 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
2097 return Print(dest, seq, nl, LINENO(n));
2098}
2099
2100static asdl_seq *
2101ast_for_exprlist(struct compiling *c, const node *n, int context)
2102{
2103 asdl_seq *seq;
2104 int i;
2105 expr_ty e;
2106
2107 REQ(n, exprlist);
2108
2109 seq = asdl_seq_new((NCH(n) + 1) / 2);
2110 if (!seq)
2111 return NULL;
2112 for (i = 0; i < NCH(n); i += 2) {
2113 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002114 if (!e)
2115 goto error;
Neal Norwitz6b347892005-11-15 07:17:53 +00002116 asdl_seq_SET(seq, i / 2, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 if (context) {
2118 if (!set_context(e, context, CHILD(n, i)))
Neal Norwitze76adcd2005-11-15 05:04:31 +00002119 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 }
2122 return seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002123
2124error:
2125 asdl_expr_seq_free(seq);
2126 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127}
2128
2129static stmt_ty
2130ast_for_del_stmt(struct compiling *c, const node *n)
2131{
2132 asdl_seq *expr_list;
2133
2134 /* del_stmt: 'del' exprlist */
2135 REQ(n, del_stmt);
2136
2137 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2138 if (!expr_list)
2139 return NULL;
2140 return Delete(expr_list, LINENO(n));
2141}
2142
2143static stmt_ty
2144ast_for_flow_stmt(struct compiling *c, const node *n)
2145{
2146 /*
2147 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2148 | yield_stmt
2149 break_stmt: 'break'
2150 continue_stmt: 'continue'
2151 return_stmt: 'return' [testlist]
2152 yield_stmt: yield_expr
2153 yield_expr: 'yield' testlist
2154 raise_stmt: 'raise' [test [',' test [',' test]]]
2155 */
2156 node *ch;
2157
2158 REQ(n, flow_stmt);
2159 ch = CHILD(n, 0);
2160 switch (TYPE(ch)) {
2161 case break_stmt:
2162 return Break(LINENO(n));
2163 case continue_stmt:
2164 return Continue(LINENO(n));
2165 case yield_stmt: { /* will reduce to yield_expr */
2166 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2167 if (!exp)
2168 return NULL;
2169 return Expr(exp, LINENO(n));
2170 }
2171 case return_stmt:
2172 if (NCH(ch) == 1)
2173 return Return(NULL, LINENO(n));
2174 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002175 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 if (!expression)
2177 return NULL;
2178 return Return(expression, LINENO(n));
2179 }
2180 case raise_stmt:
2181 if (NCH(ch) == 1)
2182 return Raise(NULL, NULL, NULL, LINENO(n));
2183 else if (NCH(ch) == 2) {
2184 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2185 if (!expression)
2186 return NULL;
2187 return Raise(expression, NULL, NULL, LINENO(n));
2188 }
2189 else if (NCH(ch) == 4) {
2190 expr_ty expr1, expr2;
2191
2192 expr1 = ast_for_expr(c, CHILD(ch, 1));
2193 if (!expr1)
2194 return NULL;
2195 expr2 = ast_for_expr(c, CHILD(ch, 3));
2196 if (!expr2)
2197 return NULL;
2198
2199 return Raise(expr1, expr2, NULL, LINENO(n));
2200 }
2201 else if (NCH(ch) == 6) {
2202 expr_ty expr1, expr2, expr3;
2203
2204 expr1 = ast_for_expr(c, CHILD(ch, 1));
2205 if (!expr1)
2206 return NULL;
2207 expr2 = ast_for_expr(c, CHILD(ch, 3));
2208 if (!expr2)
2209 return NULL;
2210 expr3 = ast_for_expr(c, CHILD(ch, 5));
2211 if (!expr3)
2212 return NULL;
2213
2214 return Raise(expr1, expr2, expr3, LINENO(n));
2215 }
2216 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002217 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 "unexpected flow_stmt: %d", TYPE(ch));
2219 return NULL;
2220 }
2221}
2222
2223static alias_ty
2224alias_for_import_name(const node *n)
2225{
2226 /*
2227 import_as_name: NAME [NAME NAME]
2228 dotted_as_name: dotted_name [NAME NAME]
2229 dotted_name: NAME ('.' NAME)*
2230 */
2231 loop:
2232 switch (TYPE(n)) {
2233 case import_as_name:
2234 if (NCH(n) == 3)
2235 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
2236 NEW_IDENTIFIER(CHILD(n, 2)));
2237 else
2238 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
2239 NULL);
2240 break;
2241 case dotted_as_name:
2242 if (NCH(n) == 1) {
2243 n = CHILD(n, 0);
2244 goto loop;
2245 }
2246 else {
2247 alias_ty a = alias_for_import_name(CHILD(n, 0));
2248 assert(!a->asname);
2249 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2250 return a;
2251 }
2252 break;
2253 case dotted_name:
2254 if (NCH(n) == 1)
2255 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL);
2256 else {
2257 /* Create a string of the form "a.b.c" */
2258 int i, len;
2259 PyObject *str;
2260 char *s;
2261
2262 len = 0;
2263 for (i = 0; i < NCH(n); i += 2)
2264 /* length of string plus one for the dot */
2265 len += strlen(STR(CHILD(n, i))) + 1;
2266 len--; /* the last name doesn't have a dot */
2267 str = PyString_FromStringAndSize(NULL, len);
2268 if (!str)
2269 return NULL;
2270 s = PyString_AS_STRING(str);
2271 if (!s)
2272 return NULL;
2273 for (i = 0; i < NCH(n); i += 2) {
2274 char *sch = STR(CHILD(n, i));
2275 strcpy(s, STR(CHILD(n, i)));
2276 s += strlen(sch);
2277 *s++ = '.';
2278 }
2279 --s;
2280 *s = '\0';
2281 PyString_InternInPlace(&str);
2282 return alias(str, NULL);
2283 }
2284 break;
2285 case STAR:
2286 return alias(PyString_InternFromString("*"), NULL);
2287 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002288 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 "unexpected import name: %d", TYPE(n));
2290 return NULL;
2291 }
2292 return NULL;
2293}
2294
2295static stmt_ty
2296ast_for_import_stmt(struct compiling *c, const node *n)
2297{
2298 /*
2299 import_stmt: import_name | import_from
2300 import_name: 'import' dotted_as_names
2301 import_from: 'from' dotted_name 'import' ('*' |
2302 '(' import_as_names ')' |
2303 import_as_names)
2304 */
2305 int i;
2306 asdl_seq *aliases;
2307
2308 REQ(n, import_stmt);
2309 n = CHILD(n, 0);
2310 if (STR(CHILD(n, 0))[0] == 'i') { /* import */
2311 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002312 REQ(n, dotted_as_names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 aliases = asdl_seq_new((NCH(n) + 1) / 2);
2314 if (!aliases)
2315 return NULL;
2316 for (i = 0; i < NCH(n); i += 2) {
2317 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2318 if (!import_alias) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002319 asdl_alias_seq_free(aliases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 return NULL;
2321 }
2322 asdl_seq_SET(aliases, i / 2, import_alias);
2323 }
2324 return Import(aliases, LINENO(n));
2325 }
2326 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
2327 stmt_ty import;
2328 int n_children;
2329 const char *from_modules;
2330 int lineno = LINENO(n);
2331 alias_ty mod = alias_for_import_name(CHILD(n, 1));
2332 if (!mod)
2333 return NULL;
2334
2335 /* XXX this needs to be cleaned up */
2336
2337 from_modules = STR(CHILD(n, 3));
2338 if (!from_modules) {
2339 n = CHILD(n, 3); /* from ... import x, y, z */
2340 if (NCH(n) % 2 == 0) {
2341 /* it ends with a comma, not valid but the parser allows it */
Neal Norwitze76adcd2005-11-15 05:04:31 +00002342 free_alias(mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 ast_error(n, "trailing comma not allowed without"
2344 " surrounding parentheses");
2345 return NULL;
2346 }
2347 }
2348 else if (from_modules[0] == '*') {
2349 n = CHILD(n, 3); /* from ... import * */
2350 }
2351 else if (from_modules[0] == '(')
2352 n = CHILD(n, 4); /* from ... import (x, y, z) */
Neal Norwitze76adcd2005-11-15 05:04:31 +00002353 else {
2354 /* XXX: don't we need to call ast_error(n, "..."); */
2355 free_alias(mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002358
2359 n_children = NCH(n);
2360 if (from_modules && from_modules[0] == '*')
2361 n_children = 1;
2362
2363 aliases = asdl_seq_new((n_children + 1) / 2);
2364 if (!aliases) {
2365 free_alias(mod);
2366 return NULL;
2367 }
2368
2369 /* handle "from ... import *" special b/c there's no children */
2370 if (from_modules && from_modules[0] == '*') {
2371 alias_ty import_alias = alias_for_import_name(n);
2372 if (!import_alias) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002373 asdl_alias_seq_free(aliases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 free_alias(mod);
2375 return NULL;
2376 }
2377 asdl_seq_APPEND(aliases, import_alias);
2378 }
2379
2380 for (i = 0; i < NCH(n); i += 2) {
2381 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2382 if (!import_alias) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002383 asdl_alias_seq_free(aliases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384 free_alias(mod);
2385 return NULL;
2386 }
2387 asdl_seq_APPEND(aliases, import_alias);
2388 }
2389 Py_INCREF(mod->name);
2390 import = ImportFrom(mod->name, aliases, lineno);
2391 free_alias(mod);
2392 return import;
2393 }
Neal Norwitz79792652005-11-14 04:25:03 +00002394 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 "unknown import statement: starts with command '%s'",
2396 STR(CHILD(n, 0)));
2397 return NULL;
2398}
2399
2400static stmt_ty
2401ast_for_global_stmt(struct compiling *c, const node *n)
2402{
2403 /* global_stmt: 'global' NAME (',' NAME)* */
2404 identifier name;
2405 asdl_seq *s;
2406 int i;
2407
2408 REQ(n, global_stmt);
2409 s = asdl_seq_new(NCH(n) / 2);
2410 if (!s)
2411 return NULL;
2412 for (i = 1; i < NCH(n); i += 2) {
2413 name = NEW_IDENTIFIER(CHILD(n, i));
2414 if (!name) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002415 for (i = i / 2; i > 0; i--)
2416 Py_XDECREF((identifier) asdl_seq_GET(s, i));
2417 asdl_seq_free(s); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418 return NULL;
2419 }
2420 asdl_seq_SET(s, i / 2, name);
2421 }
2422 return Global(s, LINENO(n));
2423}
2424
2425static stmt_ty
2426ast_for_exec_stmt(struct compiling *c, const node *n)
2427{
2428 expr_ty expr1, globals = NULL, locals = NULL;
2429 int n_children = NCH(n);
2430 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002431 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 "poorly formed 'exec' statement: %d parts to statement",
2433 n_children);
2434 return NULL;
2435 }
2436
2437 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2438 REQ(n, exec_stmt);
2439 expr1 = ast_for_expr(c, CHILD(n, 1));
2440 if (!expr1)
2441 return NULL;
2442 if (n_children >= 4) {
2443 globals = ast_for_expr(c, CHILD(n, 3));
2444 if (!globals)
2445 return NULL;
2446 }
2447 if (n_children == 6) {
2448 locals = ast_for_expr(c, CHILD(n, 5));
2449 if (!locals)
2450 return NULL;
2451 }
2452
2453 return Exec(expr1, globals, locals, LINENO(n));
2454}
2455
2456static stmt_ty
2457ast_for_assert_stmt(struct compiling *c, const node *n)
2458{
2459 /* assert_stmt: 'assert' test [',' test] */
2460 REQ(n, assert_stmt);
2461 if (NCH(n) == 2) {
2462 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2463 if (!expression)
2464 return NULL;
2465 return Assert(expression, NULL, LINENO(n));
2466 }
2467 else if (NCH(n) == 4) {
2468 expr_ty expr1, expr2;
2469
2470 expr1 = ast_for_expr(c, CHILD(n, 1));
2471 if (!expr1)
2472 return NULL;
2473 expr2 = ast_for_expr(c, CHILD(n, 3));
2474 if (!expr2)
2475 return NULL;
2476
2477 return Assert(expr1, expr2, LINENO(n));
2478 }
Neal Norwitz79792652005-11-14 04:25:03 +00002479 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 "improper number of parts to 'assert' statement: %d",
2481 NCH(n));
2482 return NULL;
2483}
2484
2485static asdl_seq *
2486ast_for_suite(struct compiling *c, const node *n)
2487{
2488 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2489 asdl_seq *seq = NULL;
2490 stmt_ty s;
2491 int i, total, num, end, pos = 0;
2492 node *ch;
2493
2494 REQ(n, suite);
2495
2496 total = num_stmts(n);
2497 seq = asdl_seq_new(total);
2498 if (!seq)
2499 return NULL;
2500 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2501 n = CHILD(n, 0);
2502 /* simple_stmt always ends with a NEWLINE,
2503 and may have a trailing SEMI
2504 */
2505 end = NCH(n) - 1;
2506 if (TYPE(CHILD(n, end - 1)) == SEMI)
2507 end--;
2508 /* loop by 2 to skip semi-colons */
2509 for (i = 0; i < end; i += 2) {
2510 ch = CHILD(n, i);
2511 s = ast_for_stmt(c, ch);
2512 if (!s)
2513 goto error;
2514 asdl_seq_SET(seq, pos++, s);
2515 }
2516 }
2517 else {
2518 for (i = 2; i < (NCH(n) - 1); i++) {
2519 ch = CHILD(n, i);
2520 REQ(ch, stmt);
2521 num = num_stmts(ch);
2522 if (num == 1) {
2523 /* small_stmt or compound_stmt with only one child */
2524 s = ast_for_stmt(c, ch);
2525 if (!s)
2526 goto error;
2527 asdl_seq_SET(seq, pos++, s);
2528 }
2529 else {
2530 int j;
2531 ch = CHILD(ch, 0);
2532 REQ(ch, simple_stmt);
2533 for (j = 0; j < NCH(ch); j += 2) {
2534 s = ast_for_stmt(c, CHILD(ch, j));
2535 if (!s)
2536 goto error;
2537 asdl_seq_SET(seq, pos++, s);
2538 }
2539 }
2540 }
2541 }
2542 assert(pos == seq->size);
2543 return seq;
2544 error:
2545 if (seq)
Neal Norwitze76adcd2005-11-15 05:04:31 +00002546 asdl_stmt_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 return NULL;
2548}
2549
2550static stmt_ty
2551ast_for_if_stmt(struct compiling *c, const node *n)
2552{
2553 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2554 ['else' ':' suite]
2555 */
2556 char *s;
2557
2558 REQ(n, if_stmt);
2559
2560 if (NCH(n) == 4) {
2561 expr_ty expression;
2562 asdl_seq *suite_seq;
2563
2564 expression = ast_for_expr(c, CHILD(n, 1));
2565 if (!expression)
2566 return NULL;
2567 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002568 if (!suite_seq) {
2569 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002571 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572
2573 return If(expression, suite_seq, NULL, LINENO(n));
2574 }
2575 s = STR(CHILD(n, 4));
2576 /* s[2], the third character in the string, will be
2577 's' for el_s_e, or
2578 'i' for el_i_f
2579 */
2580 if (s[2] == 's') {
2581 expr_ty expression;
2582 asdl_seq *seq1, *seq2;
2583
2584 expression = ast_for_expr(c, CHILD(n, 1));
2585 if (!expression)
2586 return NULL;
2587 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002588 if (!seq1) {
2589 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002591 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002593 if (!seq2) {
2594 asdl_stmt_seq_free(seq1);
2595 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002597 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598
2599 return If(expression, seq1, seq2, LINENO(n));
2600 }
2601 else if (s[2] == 'i') {
2602 int i, n_elif, has_else = 0;
2603 asdl_seq *orelse = NULL;
2604 n_elif = NCH(n) - 4;
2605 /* must reference the child n_elif+1 since 'else' token is third,
2606 not fourth, child from the end. */
2607 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2608 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2609 has_else = 1;
2610 n_elif -= 3;
2611 }
2612 n_elif /= 4;
2613
2614 if (has_else) {
2615 expr_ty expression;
2616 asdl_seq *seq1, *seq2;
2617
2618 orelse = asdl_seq_new(1);
2619 if (!orelse)
2620 return NULL;
2621 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2622 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002623 asdl_seq_free(orelse); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 return NULL;
2625 }
2626 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2627 if (!seq1) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002628 free_expr(expression);
2629 asdl_seq_free(orelse); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630 return NULL;
2631 }
2632 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2633 if (!seq2) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002634 free_expr(expression);
2635 asdl_stmt_seq_free(seq1);
2636 asdl_seq_free(orelse); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 return NULL;
2638 }
2639
2640 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
2641 LINENO(CHILD(n, NCH(n) - 6))));
2642 /* the just-created orelse handled the last elif */
2643 n_elif--;
2644 }
2645 else
2646 orelse = NULL;
2647
2648 for (i = 0; i < n_elif; i++) {
2649 int off = 5 + (n_elif - i - 1) * 4;
2650 expr_ty expression;
2651 asdl_seq *suite_seq;
2652 asdl_seq *new = asdl_seq_new(1);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002653 if (!new) {
2654 asdl_stmt_seq_free(orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002656 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 expression = ast_for_expr(c, CHILD(n, off));
2658 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002659 asdl_stmt_seq_free(orelse);
2660 asdl_seq_free(new); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 return NULL;
2662 }
2663 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2664 if (!suite_seq) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002665 asdl_stmt_seq_free(orelse);
2666 free_expr(expression);
2667 asdl_seq_free(new); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 return NULL;
2669 }
2670
2671 asdl_seq_SET(new, 0,
2672 If(expression, suite_seq, orelse,
2673 LINENO(CHILD(n, off))));
2674 orelse = new;
2675 }
2676 return If(ast_for_expr(c, CHILD(n, 1)),
2677 ast_for_suite(c, CHILD(n, 3)),
2678 orelse, LINENO(n));
2679 }
2680 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002681 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 "unexpected token in 'if' statement: %s", s);
2683 return NULL;
2684 }
2685}
2686
2687static stmt_ty
2688ast_for_while_stmt(struct compiling *c, const node *n)
2689{
2690 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2691 REQ(n, while_stmt);
2692
2693 if (NCH(n) == 4) {
2694 expr_ty expression;
2695 asdl_seq *suite_seq;
2696
2697 expression = ast_for_expr(c, CHILD(n, 1));
2698 if (!expression)
2699 return NULL;
2700 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002701 if (!suite_seq) {
2702 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002704 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 return While(expression, suite_seq, NULL, LINENO(n));
2706 }
2707 else if (NCH(n) == 7) {
2708 expr_ty expression;
2709 asdl_seq *seq1, *seq2;
2710
2711 expression = ast_for_expr(c, CHILD(n, 1));
2712 if (!expression)
2713 return NULL;
2714 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002715 if (!seq1) {
2716 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002718 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002720 if (!seq2) {
2721 asdl_stmt_seq_free(seq1);
2722 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002724 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725
2726 return While(expression, seq1, seq2, LINENO(n));
2727 }
2728 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002729 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 "wrong number of tokens for 'while' statement: %d",
2731 NCH(n));
2732 return NULL;
2733 }
2734}
2735
2736static stmt_ty
2737ast_for_for_stmt(struct compiling *c, const node *n)
2738{
2739 asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL;
2740 expr_ty expression;
2741 expr_ty target;
2742 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2743 REQ(n, for_stmt);
2744
2745 if (NCH(n) == 9) {
2746 seq = ast_for_suite(c, CHILD(n, 8));
2747 if (!seq)
2748 return NULL;
2749 }
2750
2751 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002752 if (!_target) {
2753 asdl_stmt_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002755 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 if (asdl_seq_LEN(_target) == 1) {
2757 target = asdl_seq_GET(_target, 0);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002758 asdl_seq_free(_target); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 }
2760 else
2761 target = Tuple(_target, Store, LINENO(n));
2762
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002763 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002764 if (!expression) {
2765 free_expr(target);
2766 asdl_stmt_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002768 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002770 if (!suite_seq) {
2771 free_expr(target);
2772 free_expr(expression);
2773 asdl_stmt_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002775 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776
2777 return For(target, expression, suite_seq, seq, LINENO(n));
2778}
2779
2780static excepthandler_ty
2781ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2782{
2783 /* except_clause: 'except' [test [',' test]] */
2784 REQ(exc, except_clause);
2785 REQ(body, suite);
2786
2787 if (NCH(exc) == 1) {
2788 asdl_seq *suite_seq = ast_for_suite(c, body);
2789 if (!suite_seq)
2790 return NULL;
2791
2792 return excepthandler(NULL, NULL, suite_seq);
2793 }
2794 else if (NCH(exc) == 2) {
2795 expr_ty expression;
2796 asdl_seq *suite_seq;
2797
2798 expression = ast_for_expr(c, CHILD(exc, 1));
2799 if (!expression)
2800 return NULL;
2801 suite_seq = ast_for_suite(c, body);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002802 if (!suite_seq) {
2803 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806
2807 return excepthandler(expression, NULL, suite_seq);
2808 }
2809 else if (NCH(exc) == 4) {
2810 asdl_seq *suite_seq;
2811 expr_ty expression;
2812 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2813 if (!e)
2814 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002815 if (!set_context(e, Store, CHILD(exc, 3))) {
2816 free_expr(e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002820 if (!expression) {
2821 free_expr(e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 suite_seq = ast_for_suite(c, body);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002825 if (!suite_seq) {
2826 free_expr(expression);
2827 free_expr(e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002829 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830
2831 return excepthandler(expression, e, suite_seq);
2832 }
2833 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002834 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 "wrong number of children for 'except' clause: %d",
2836 NCH(exc));
2837 return NULL;
2838 }
2839}
2840
2841static stmt_ty
2842ast_for_try_stmt(struct compiling *c, const node *n)
2843{
2844 REQ(n, try_stmt);
2845
2846 if (TYPE(CHILD(n, 3)) == NAME) {/* must be 'finally' */
2847 /* try_stmt: 'try' ':' suite 'finally' ':' suite) */
2848 asdl_seq *s1, *s2;
2849 s1 = ast_for_suite(c, CHILD(n, 2));
2850 if (!s1)
2851 return NULL;
2852 s2 = ast_for_suite(c, CHILD(n, 5));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002853 if (!s2) {
2854 asdl_stmt_seq_free(s1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002856 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857
2858 return TryFinally(s1, s2, LINENO(n));
2859 }
2860 else if (TYPE(CHILD(n, 3)) == except_clause) {
2861 /* try_stmt: ('try' ':' suite (except_clause ':' suite)+
2862 ['else' ':' suite]
2863 */
2864 asdl_seq *suite_seq1, *suite_seq2;
2865 asdl_seq *handlers;
2866 int i, has_else = 0, n_except = NCH(n) - 3;
2867 if (TYPE(CHILD(n, NCH(n) - 3)) == NAME) {
2868 has_else = 1;
2869 n_except -= 3;
2870 }
2871 n_except /= 3;
2872 handlers = asdl_seq_new(n_except);
2873 if (!handlers)
2874 return NULL;
2875 for (i = 0; i < n_except; i++) {
2876 excepthandler_ty e = ast_for_except_clause(c,
2877 CHILD(n, 3 + i * 3),
2878 CHILD(n, 5 + i * 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002879 if (!e) {
2880 for ( ; i >= 0; i--)
2881 free_excepthandler(asdl_seq_GET(handlers, i));
2882 asdl_seq_free(handlers); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002884 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 asdl_seq_SET(handlers, i, e);
2886 }
2887
2888 suite_seq1 = ast_for_suite(c, CHILD(n, 2));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002889 if (!suite_seq1) {
2890 for (i = 0; i < asdl_seq_LEN(handlers); i++)
2891 free_excepthandler(asdl_seq_GET(handlers, i));
2892 asdl_seq_free(handlers); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002894 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 if (has_else) {
2896 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002897 if (!suite_seq2) {
2898 for (i = 0; i < asdl_seq_LEN(handlers); i++)
2899 free_excepthandler(asdl_seq_GET(handlers, i));
2900 asdl_seq_free(handlers); /* ok */
2901 asdl_stmt_seq_free(suite_seq1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002903 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 }
2905 else
2906 suite_seq2 = NULL;
2907
2908 return TryExcept(suite_seq1, handlers, suite_seq2, LINENO(n));
2909 }
2910 else {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002911 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 return NULL;
2913 }
2914}
2915
2916static stmt_ty
2917ast_for_classdef(struct compiling *c, const node *n)
2918{
2919 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 asdl_seq *bases, *s;
2921
2922 REQ(n, classdef);
2923
2924 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2925 ast_error(n, "assignment to None");
2926 return NULL;
2927 }
2928
2929 if (NCH(n) == 4) {
2930 s = ast_for_suite(c, CHILD(n, 3));
2931 if (!s)
2932 return NULL;
2933 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2934 }
2935 /* check for empty base list */
2936 if (TYPE(CHILD(n,3)) == RPAR) {
2937 s = ast_for_suite(c, CHILD(n,5));
2938 if (!s)
2939 return NULL;
2940 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2941 }
2942
2943 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002944 bases = ast_for_class_bases(c, CHILD(n, 3));
2945 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947
2948 s = ast_for_suite(c, CHILD(n, 6));
2949 if (!s) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002950 asdl_expr_seq_free(bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 return NULL;
2952 }
2953 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n));
2954}
2955
2956static stmt_ty
2957ast_for_stmt(struct compiling *c, const node *n)
2958{
2959 if (TYPE(n) == stmt) {
2960 assert(NCH(n) == 1);
2961 n = CHILD(n, 0);
2962 }
2963 if (TYPE(n) == simple_stmt) {
2964 assert(num_stmts(n) == 1);
2965 n = CHILD(n, 0);
2966 }
2967 if (TYPE(n) == small_stmt) {
2968 REQ(n, small_stmt);
2969 n = CHILD(n, 0);
2970 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2971 | flow_stmt | import_stmt | global_stmt | exec_stmt
2972 | assert_stmt
2973 */
2974 switch (TYPE(n)) {
2975 case expr_stmt:
2976 return ast_for_expr_stmt(c, n);
2977 case print_stmt:
2978 return ast_for_print_stmt(c, n);
2979 case del_stmt:
2980 return ast_for_del_stmt(c, n);
2981 case pass_stmt:
2982 return Pass(LINENO(n));
2983 case flow_stmt:
2984 return ast_for_flow_stmt(c, n);
2985 case import_stmt:
2986 return ast_for_import_stmt(c, n);
2987 case global_stmt:
2988 return ast_for_global_stmt(c, n);
2989 case exec_stmt:
2990 return ast_for_exec_stmt(c, n);
2991 case assert_stmt:
2992 return ast_for_assert_stmt(c, n);
2993 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002994 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2996 TYPE(n), NCH(n));
2997 return NULL;
2998 }
2999 }
3000 else {
3001 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3002 | funcdef | classdef
3003 */
3004 node *ch = CHILD(n, 0);
3005 REQ(n, compound_stmt);
3006 switch (TYPE(ch)) {
3007 case if_stmt:
3008 return ast_for_if_stmt(c, ch);
3009 case while_stmt:
3010 return ast_for_while_stmt(c, ch);
3011 case for_stmt:
3012 return ast_for_for_stmt(c, ch);
3013 case try_stmt:
3014 return ast_for_try_stmt(c, ch);
3015 case funcdef:
3016 return ast_for_funcdef(c, ch);
3017 case classdef:
3018 return ast_for_classdef(c, ch);
3019 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003020 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3022 TYPE(n), NCH(n));
3023 return NULL;
3024 }
3025 }
3026}
3027
3028static PyObject *
3029parsenumber(const char *s)
3030{
3031 const char *end;
3032 long x;
3033 double dx;
3034#ifndef WITHOUT_COMPLEX
3035 Py_complex c;
3036 int imflag;
3037#endif
3038
3039 errno = 0;
3040 end = s + strlen(s) - 1;
3041#ifndef WITHOUT_COMPLEX
3042 imflag = *end == 'j' || *end == 'J';
3043#endif
3044 if (*end == 'l' || *end == 'L')
3045 return PyLong_FromString((char *)s, (char **)0, 0);
3046 if (s[0] == '0') {
3047 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3048 if (x < 0 && errno == 0) {
3049 return PyLong_FromString((char *)s,
3050 (char **)0,
3051 0);
3052 }
3053 }
3054 else
3055 x = PyOS_strtol((char *)s, (char **)&end, 0);
3056 if (*end == '\0') {
3057 if (errno != 0)
3058 return PyLong_FromString((char *)s, (char **)0, 0);
3059 return PyInt_FromLong(x);
3060 }
3061 /* XXX Huge floats may silently fail */
3062#ifndef WITHOUT_COMPLEX
3063 if (imflag) {
3064 c.real = 0.;
3065 PyFPE_START_PROTECT("atof", return 0)
3066 c.imag = atof(s);
3067 PyFPE_END_PROTECT(c)
3068 return PyComplex_FromCComplex(c);
3069 }
3070 else
3071#endif
3072 {
3073 PyFPE_START_PROTECT("atof", return 0)
3074 dx = atof(s);
3075 PyFPE_END_PROTECT(dx)
3076 return PyFloat_FromDouble(dx);
3077 }
3078}
3079
3080static PyObject *
3081decode_utf8(const char **sPtr, const char *end, char* encoding)
3082{
3083#ifndef Py_USING_UNICODE
3084 Py_FatalError("decode_utf8 should not be called in this build.");
3085 return NULL;
3086#else
3087 PyObject *u, *v;
3088 char *s, *t;
3089 t = s = (char *)*sPtr;
3090 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3091 while (s < end && (*s & 0x80)) s++;
3092 *sPtr = s;
3093 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3094 if (u == NULL)
3095 return NULL;
3096 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3097 Py_DECREF(u);
3098 return v;
3099#endif
3100}
3101
3102static PyObject *
3103decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3104{
3105 PyObject *v, *u;
3106 char *buf;
3107 char *p;
3108 const char *end;
3109 if (encoding == NULL) {
3110 buf = (char *)s;
3111 u = NULL;
3112 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3113 buf = (char *)s;
3114 u = NULL;
3115 } else {
3116 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3117 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3118 if (u == NULL)
3119 return NULL;
3120 p = buf = PyString_AsString(u);
3121 end = s + len;
3122 while (s < end) {
3123 if (*s == '\\') {
3124 *p++ = *s++;
3125 if (*s & 0x80) {
3126 strcpy(p, "u005c");
3127 p += 5;
3128 }
3129 }
3130 if (*s & 0x80) { /* XXX inefficient */
3131 PyObject *w;
3132 char *r;
3133 int rn, i;
3134 w = decode_utf8(&s, end, "utf-16-be");
3135 if (w == NULL) {
3136 Py_DECREF(u);
3137 return NULL;
3138 }
3139 r = PyString_AsString(w);
3140 rn = PyString_Size(w);
3141 assert(rn % 2 == 0);
3142 for (i = 0; i < rn; i += 2) {
3143 sprintf(p, "\\u%02x%02x",
3144 r[i + 0] & 0xFF,
3145 r[i + 1] & 0xFF);
3146 p += 6;
3147 }
3148 Py_DECREF(w);
3149 } else {
3150 *p++ = *s++;
3151 }
3152 }
3153 len = p - buf;
3154 s = buf;
3155 }
3156 if (rawmode)
3157 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3158 else
3159 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3160 Py_XDECREF(u);
3161 return v;
3162}
3163
3164/* s is a Python string literal, including the bracketing quote characters,
3165 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3166 * parsestr parses it, and returns the decoded Python string object.
3167 */
3168static PyObject *
3169parsestr(const char *s, const char *encoding)
3170{
3171 PyObject *v;
3172 size_t len;
3173 int quote = *s;
3174 int rawmode = 0;
3175 int need_encoding;
3176 int unicode = 0;
3177
3178 if (isalpha(quote) || quote == '_') {
3179 if (quote == 'u' || quote == 'U') {
3180 quote = *++s;
3181 unicode = 1;
3182 }
3183 if (quote == 'r' || quote == 'R') {
3184 quote = *++s;
3185 rawmode = 1;
3186 }
3187 }
3188 if (quote != '\'' && quote != '\"') {
3189 PyErr_BadInternalCall();
3190 return NULL;
3191 }
3192 s++;
3193 len = strlen(s);
3194 if (len > INT_MAX) {
3195 PyErr_SetString(PyExc_OverflowError,
3196 "string to parse is too long");
3197 return NULL;
3198 }
3199 if (s[--len] != quote) {
3200 PyErr_BadInternalCall();
3201 return NULL;
3202 }
3203 if (len >= 4 && s[0] == quote && s[1] == quote) {
3204 s += 2;
3205 len -= 2;
3206 if (s[--len] != quote || s[--len] != quote) {
3207 PyErr_BadInternalCall();
3208 return NULL;
3209 }
3210 }
3211#ifdef Py_USING_UNICODE
3212 if (unicode || Py_UnicodeFlag) {
3213 return decode_unicode(s, len, rawmode, encoding);
3214 }
3215#endif
3216 need_encoding = (encoding != NULL &&
3217 strcmp(encoding, "utf-8") != 0 &&
3218 strcmp(encoding, "iso-8859-1") != 0);
3219 if (rawmode || strchr(s, '\\') == NULL) {
3220 if (need_encoding) {
3221#ifndef Py_USING_UNICODE
3222 /* This should not happen - we never see any other
3223 encoding. */
3224 Py_FatalError("cannot deal with encodings in this build.");
3225#else
3226 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
3227 if (u == NULL)
3228 return NULL;
3229 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3230 Py_DECREF(u);
3231 return v;
3232#endif
3233 } else {
3234 return PyString_FromStringAndSize(s, len);
3235 }
3236 }
3237
3238 v = PyString_DecodeEscape(s, len, NULL, unicode,
3239 need_encoding ? encoding : NULL);
3240 return v;
3241}
3242
3243/* Build a Python string object out of a STRING atom. This takes care of
3244 * compile-time literal catenation, calling parsestr() on each piece, and
3245 * pasting the intermediate results together.
3246 */
3247static PyObject *
3248parsestrplus(struct compiling *c, const node *n)
3249{
3250 PyObject *v;
3251 int i;
3252 REQ(CHILD(n, 0), STRING);
3253 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3254 /* String literal concatenation */
3255 for (i = 1; i < NCH(n); i++) {
3256 PyObject *s;
3257 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3258 if (s == NULL)
3259 goto onError;
3260 if (PyString_Check(v) && PyString_Check(s)) {
3261 PyString_ConcatAndDel(&v, s);
3262 if (v == NULL)
3263 goto onError;
3264 }
3265#ifdef Py_USING_UNICODE
3266 else {
3267 PyObject *temp;
3268 temp = PyUnicode_Concat(v, s);
3269 Py_DECREF(s);
3270 if (temp == NULL)
3271 goto onError;
3272 Py_DECREF(v);
3273 v = temp;
3274 }
3275#endif
3276 }
3277 }
3278 return v;
3279
3280 onError:
3281 Py_XDECREF(v);
3282 return NULL;
3283}