blob: 04b2b3e5f66b035998e7846e4756f7eb17c7fe0d [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));
Neal Norwitza3fd07d2005-11-16 05:49:04 +000037 asdl_seq_free(seq); / * ok * /
Neal Norwitzaf8f9742005-11-15 05:09:44 +000038
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.
Neal Norwitza3fd07d2005-11-16 05:49:04 +000042
43 If asdl_seq_free is appropriate, you should mark it with an ok comment.
44
45 There are still many memory problems in this file even though
46 it runs clean in valgrind, save one problem that may have existed
47 before the AST.
48
49 Any code which does something like this:
50
51 return ASTconstruct(local, LINENO(n));
52
53 will leak memory. The problem is if ASTconstruct (e.g., TryFinally)
54 cannot allocate memory, local will be leaked.
55
56 There was discussion on python-dev to replace the entire allocation
57 scheme in this file with arenas. Basically rather than allocate
58 memory in little blocks with malloc(), we allocate one big honking
59 hunk and deref everything into this block. We would still need
60 another block or technique to handle the PyObject*s.
61
62 http://mail.python.org/pipermail/python-dev/2005-November/058138.html
Neal Norwitzaf8f9742005-11-15 05:09:44 +000063*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000064
65/* Data structure used internally */
66struct compiling {
67 char *c_encoding; /* source encoding */
68};
69
70static asdl_seq *seq_for_testlist(struct compiling *, const node *);
71static expr_ty ast_for_expr(struct compiling *, const node *);
72static stmt_ty ast_for_stmt(struct compiling *, const node *);
73static asdl_seq *ast_for_suite(struct compiling *, const node *);
74static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +000075static expr_ty ast_for_testlist(struct compiling *, const node *);
76static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077
78/* Note different signature for ast_for_call */
79static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
80
81static PyObject *parsenumber(const char *);
82static PyObject *parsestr(const char *s, const char *encoding);
83static PyObject *parsestrplus(struct compiling *, const node *n);
84
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085#ifndef LINENO
86#define LINENO(n) ((n)->n_lineno)
87#endif
88
89#define NEW_IDENTIFIER(n) PyString_InternFromString(STR(n))
90
91static void
92asdl_stmt_seq_free(asdl_seq* seq)
93{
94 int n, i;
95
96 if (!seq)
97 return;
Neal Norwitze76adcd2005-11-15 05:04:31 +000098
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 n = asdl_seq_LEN(seq);
100 for (i = 0; i < n; i++)
101 free_stmt(asdl_seq_GET(seq, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +0000102 asdl_seq_free(seq); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103}
104
105static void
106asdl_expr_seq_free(asdl_seq* seq)
107{
108 int n, i;
109
110 if (!seq)
111 return;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000112
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113 n = asdl_seq_LEN(seq);
114 for (i = 0; i < n; i++)
115 free_expr(asdl_seq_GET(seq, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +0000116 asdl_seq_free(seq); /* ok */
117}
118
119static void
120asdl_alias_seq_free(asdl_seq* seq)
121{
122 int n, i;
123
124 if (!seq)
125 return;
126
127 n = asdl_seq_LEN(seq);
128 for (i = 0; i < n; i++)
129 free_alias(asdl_seq_GET(seq, i));
130 asdl_seq_free(seq); /* ok */
131}
132
133static void
134asdl_comprehension_seq_free(asdl_seq* seq)
135{
136 int n, i;
137
138 if (!seq)
139 return;
140
141 n = asdl_seq_LEN(seq);
142 for (i = 0; i < n; i++)
143 free_comprehension(asdl_seq_GET(seq, i));
144 asdl_seq_free(seq); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145}
146
147/* This routine provides an invalid object for the syntax error.
148 The outermost routine must unpack this error and create the
149 proper object. We do this so that we don't have to pass
150 the filename to everything function.
151
152 XXX Maybe we should just pass the filename...
153*/
154
155static int
156ast_error(const node *n, const char *errstr)
157{
158 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
159 if (!u)
160 return 0;
161 PyErr_SetObject(PyExc_SyntaxError, u);
162 Py_DECREF(u);
163 return 0;
164}
165
166static void
167ast_error_finish(const char *filename)
168{
169 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
170 int lineno;
171
172 assert(PyErr_Occurred());
173 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
174 return;
175
176 PyErr_Fetch(&type, &value, &tback);
177 errstr = PyTuple_GetItem(value, 0);
178 if (!errstr)
179 return;
180 Py_INCREF(errstr);
181 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000182 if (lineno == -1) {
183 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000185 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186 Py_DECREF(value);
187
188 loc = PyErr_ProgramText(filename, lineno);
189 if (!loc) {
190 Py_INCREF(Py_None);
191 loc = Py_None;
192 }
193 tmp = Py_BuildValue("(ziOO)", filename, lineno, Py_None, loc);
194 Py_DECREF(loc);
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000195 if (!tmp) {
196 Py_DECREF(errstr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 return;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000198 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 value = Py_BuildValue("(OO)", errstr, tmp);
200 Py_DECREF(errstr);
201 Py_DECREF(tmp);
202 if (!value)
203 return;
204 PyErr_Restore(type, value, tback);
205}
206
207/* num_stmts() returns number of contained statements.
208
209 Use this routine to determine how big a sequence is needed for
210 the statements in a parse tree. Its raison d'etre is this bit of
211 grammar:
212
213 stmt: simple_stmt | compound_stmt
214 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
215
216 A simple_stmt can contain multiple small_stmt elements joined
217 by semicolons. If the arg is a simple_stmt, the number of
218 small_stmt elements is returned.
219*/
220
221static int
222num_stmts(const node *n)
223{
224 int i, l;
225 node *ch;
226
227 switch (TYPE(n)) {
228 case single_input:
229 if (TYPE(CHILD(n, 0)) == NEWLINE)
230 return 0;
231 else
232 return num_stmts(CHILD(n, 0));
233 case file_input:
234 l = 0;
235 for (i = 0; i < NCH(n); i++) {
236 ch = CHILD(n, i);
237 if (TYPE(ch) == stmt)
238 l += num_stmts(ch);
239 }
240 return l;
241 case stmt:
242 return num_stmts(CHILD(n, 0));
243 case compound_stmt:
244 return 1;
245 case simple_stmt:
246 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
247 case suite:
248 if (NCH(n) == 1)
249 return num_stmts(CHILD(n, 0));
250 else {
251 l = 0;
252 for (i = 2; i < (NCH(n) - 1); i++)
253 l += num_stmts(CHILD(n, i));
254 return l;
255 }
256 default: {
257 char buf[128];
258
259 sprintf(buf, "Non-statement found: %d %d\n",
260 TYPE(n), NCH(n));
261 Py_FatalError(buf);
262 }
263 }
264 assert(0);
265 return 0;
266}
267
268/* Transform the CST rooted at node * to the appropriate AST
269*/
270
271mod_ty
272PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename)
273{
274 int i, j, num;
275 asdl_seq *stmts = NULL;
276 stmt_ty s;
277 node *ch;
278 struct compiling c;
279
280 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
281 c.c_encoding = "utf-8";
282 } else if (TYPE(n) == encoding_decl) {
283 c.c_encoding = STR(n);
284 n = CHILD(n, 0);
285 } else {
286 c.c_encoding = NULL;
287 }
288
289 switch (TYPE(n)) {
290 case file_input:
291 stmts = asdl_seq_new(num_stmts(n));
292 if (!stmts)
293 return NULL;
294 for (i = 0; i < NCH(n) - 1; i++) {
295 ch = CHILD(n, i);
296 if (TYPE(ch) == NEWLINE)
297 continue;
298 REQ(ch, stmt);
299 num = num_stmts(ch);
300 if (num == 1) {
301 s = ast_for_stmt(&c, ch);
302 if (!s)
303 goto error;
304 asdl_seq_APPEND(stmts, s);
305 }
306 else {
307 ch = CHILD(ch, 0);
308 REQ(ch, simple_stmt);
309 for (j = 0; j < num; j++) {
310 s = ast_for_stmt(&c, CHILD(ch, j * 2));
311 if (!s)
312 goto error;
313 asdl_seq_APPEND(stmts, s);
314 }
315 }
316 }
317 return Module(stmts);
318 case eval_input: {
319 expr_ty testlist_ast;
320
321 /* XXX Why not gen_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000322 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323 if (!testlist_ast)
324 goto error;
325 return Expression(testlist_ast);
326 }
327 case single_input:
328 if (TYPE(CHILD(n, 0)) == NEWLINE) {
329 stmts = asdl_seq_new(1);
330 if (!stmts)
331 goto error;
332 asdl_seq_SET(stmts, 0, Pass(n->n_lineno));
333 return Interactive(stmts);
334 }
335 else {
336 n = CHILD(n, 0);
337 num = num_stmts(n);
338 stmts = asdl_seq_new(num);
339 if (!stmts)
340 goto error;
341 if (num == 1) {
342 stmt_ty s = ast_for_stmt(&c, n);
343 if (!s)
344 goto error;
345 asdl_seq_SET(stmts, 0, s);
346 }
347 else {
348 /* Only a simple_stmt can contain multiple statements. */
349 REQ(n, simple_stmt);
350 for (i = 0; i < NCH(n); i += 2) {
351 stmt_ty s;
352 if (TYPE(CHILD(n, i)) == NEWLINE)
353 break;
354 s = ast_for_stmt(&c, CHILD(n, i));
355 if (!s)
356 goto error;
357 asdl_seq_SET(stmts, i / 2, s);
358 }
359 }
360
361 return Interactive(stmts);
362 }
363 default:
364 goto error;
365 }
366 error:
367 if (stmts)
368 asdl_stmt_seq_free(stmts);
369 ast_error_finish(filename);
370 return NULL;
371}
372
373/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
374*/
375
376static operator_ty
377get_operator(const node *n)
378{
379 switch (TYPE(n)) {
380 case VBAR:
381 return BitOr;
382 case CIRCUMFLEX:
383 return BitXor;
384 case AMPER:
385 return BitAnd;
386 case LEFTSHIFT:
387 return LShift;
388 case RIGHTSHIFT:
389 return RShift;
390 case PLUS:
391 return Add;
392 case MINUS:
393 return Sub;
394 case STAR:
395 return Mult;
396 case SLASH:
397 return Div;
398 case DOUBLESLASH:
399 return FloorDiv;
400 case PERCENT:
401 return Mod;
402 default:
403 return 0;
404 }
405}
406
407/* Set the context ctx for expr_ty e returning 0 on success, -1 on error.
408
409 Only sets context for expr kinds that "can appear in assignment context"
410 (according to ../Parser/Python.asdl). For other expr kinds, it sets
411 an appropriate syntax error and returns false.
412
413 If e is a sequential type, items in sequence will also have their context
414 set.
415
416*/
417
418static int
419set_context(expr_ty e, expr_context_ty ctx, const node *n)
420{
421 asdl_seq *s = NULL;
422
423 switch (e->kind) {
424 case Attribute_kind:
425 if (ctx == Store &&
426 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
427 return ast_error(n, "assignment to None");
428 }
429 e->v.Attribute.ctx = ctx;
430 break;
431 case Subscript_kind:
432 e->v.Subscript.ctx = ctx;
433 break;
434 case Name_kind:
435 if (ctx == Store &&
436 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
437 return ast_error(n, "assignment to None");
438 }
439 e->v.Name.ctx = ctx;
440 break;
441 case List_kind:
442 e->v.List.ctx = ctx;
443 s = e->v.List.elts;
444 break;
445 case Tuple_kind:
446 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
447 return ast_error(n, "can't assign to ()");
448 e->v.Tuple.ctx = ctx;
449 s = e->v.Tuple.elts;
450 break;
451 case Call_kind:
452 if (ctx == Store)
453 return ast_error(n, "can't assign to function call");
454 else if (ctx == Del)
455 return ast_error(n, "can't delete function call");
456 else
457 return ast_error(n, "unexpected operation on function call");
458 break;
459 case BinOp_kind:
460 return ast_error(n, "can't assign to operator");
461 case GeneratorExp_kind:
462 return ast_error(n, "assignment to generator expression "
463 "not possible");
464 case Num_kind:
465 case Str_kind:
466 return ast_error(n, "can't assign to literal");
467 default: {
468 char buf[300];
469 PyOS_snprintf(buf, sizeof(buf),
470 "unexpected expression in assignment %d (line %d)",
471 e->kind, e->lineno);
472 return ast_error(n, buf);
473 }
474 }
475 /* If the LHS is a list or tuple, we need to set the assignment
476 context for all the tuple elements.
477 */
478 if (s) {
479 int i;
480
481 for (i = 0; i < asdl_seq_LEN(s); i++) {
482 if (!set_context(asdl_seq_GET(s, i), ctx, n))
483 return 0;
484 }
485 }
486 return 1;
487}
488
489static operator_ty
490ast_for_augassign(const node *n)
491{
492 REQ(n, augassign);
493 n = CHILD(n, 0);
494 switch (STR(n)[0]) {
495 case '+':
496 return Add;
497 case '-':
498 return Sub;
499 case '/':
500 if (STR(n)[1] == '/')
501 return FloorDiv;
502 else
503 return Div;
504 case '%':
505 return Mod;
506 case '<':
507 return LShift;
508 case '>':
509 return RShift;
510 case '&':
511 return BitAnd;
512 case '^':
513 return BitXor;
514 case '|':
515 return BitOr;
516 case '*':
517 if (STR(n)[1] == '*')
518 return Pow;
519 else
520 return Mult;
521 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000522 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 return 0;
524 }
525}
526
527static cmpop_ty
528ast_for_comp_op(const node *n)
529{
530 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
531 |'is' 'not'
532 */
533 REQ(n, comp_op);
534 if (NCH(n) == 1) {
535 n = CHILD(n, 0);
536 switch (TYPE(n)) {
537 case LESS:
538 return Lt;
539 case GREATER:
540 return Gt;
541 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542 return Eq;
543 case LESSEQUAL:
544 return LtE;
545 case GREATEREQUAL:
546 return GtE;
547 case NOTEQUAL:
548 return NotEq;
549 case NAME:
550 if (strcmp(STR(n), "in") == 0)
551 return In;
552 if (strcmp(STR(n), "is") == 0)
553 return Is;
554 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000555 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 STR(n));
557 return 0;
558 }
559 }
560 else if (NCH(n) == 2) {
561 /* handle "not in" and "is not" */
562 switch (TYPE(CHILD(n, 0))) {
563 case NAME:
564 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
565 return NotIn;
566 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
567 return IsNot;
568 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000569 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
571 return 0;
572 }
573 }
Neal Norwitz79792652005-11-14 04:25:03 +0000574 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 NCH(n));
576 return 0;
577}
578
579static asdl_seq *
580seq_for_testlist(struct compiling *c, const node *n)
581{
582 /* testlist: test (',' test)* [','] */
Armin Rigo31441302005-10-21 12:57:31 +0000583 asdl_seq *seq;
584 expr_ty expression;
585 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586 assert(TYPE(n) == testlist
587 || TYPE(n) == listmaker
588 || TYPE(n) == testlist_gexp
589 || TYPE(n) == testlist_safe
590 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
592 seq = asdl_seq_new((NCH(n) + 1) / 2);
593 if (!seq)
594 return NULL;
595
596 for (i = 0; i < NCH(n); i += 2) {
597 REQ(CHILD(n, i), test);
598
599 expression = ast_for_expr(c, CHILD(n, i));
600 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000601 asdl_expr_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 return NULL;
603 }
604
605 assert(i / 2 < seq->size);
606 asdl_seq_SET(seq, i / 2, expression);
607 }
608 return seq;
609}
610
611static expr_ty
612compiler_complex_args(const node *n)
613{
614 int i, len = (NCH(n) + 1) / 2;
615 expr_ty result;
616 asdl_seq *args = asdl_seq_new(len);
617 if (!args)
618 return NULL;
619
620 REQ(n, fplist);
621
622 for (i = 0; i < len; i++) {
623 const node *child = CHILD(CHILD(n, 2*i), 0);
624 expr_ty arg;
625 if (TYPE(child) == NAME) {
626 if (!strcmp(STR(child), "None")) {
627 ast_error(child, "assignment to None");
628 return NULL;
629 }
630 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child));
631 }
632 else
633 arg = compiler_complex_args(CHILD(CHILD(n, 2*i), 1));
634 set_context(arg, Store, n);
635 asdl_seq_SET(args, i, arg);
636 }
637
638 result = Tuple(args, Store, LINENO(n));
639 set_context(result, Store, n);
640 return result;
641}
642
643/* Create AST for argument list.
644
645 XXX TO DO:
646 - check for invalid argument lists like normal after default
647*/
648
649static arguments_ty
650ast_for_arguments(struct compiling *c, const node *n)
651{
652 /* parameters: '(' [varargslist] ')'
653 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
654 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
655 */
656 int i, n_args = 0, n_defaults = 0, found_default = 0;
657 asdl_seq *args, *defaults;
658 identifier vararg = NULL, kwarg = NULL;
659 node *ch;
660
661 if (TYPE(n) == parameters) {
662 if (NCH(n) == 2) /* () as argument list */
663 return arguments(NULL, NULL, NULL, NULL);
664 n = CHILD(n, 1);
665 }
666 REQ(n, varargslist);
667
668 /* first count the number of normal args & defaults */
669 for (i = 0; i < NCH(n); i++) {
670 ch = CHILD(n, i);
671 if (TYPE(ch) == fpdef) {
672 n_args++;
673 }
674 if (TYPE(ch) == EQUAL)
675 n_defaults++;
676 }
677 args = (n_args ? asdl_seq_new(n_args) : NULL);
678 if (!args && n_args)
679 return NULL; /* Don't need to go to NULL; nothing allocated */
680 defaults = (n_defaults ? asdl_seq_new(n_defaults) : NULL);
681 if (!defaults && n_defaults)
682 goto error;
683
684 /* fpdef: NAME | '(' fplist ')'
685 fplist: fpdef (',' fpdef)* [',']
686 */
687 i = 0;
688 while (i < NCH(n)) {
689 ch = CHILD(n, i);
690 switch (TYPE(ch)) {
691 case fpdef:
692 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
693 anything other than EQUAL or a comma? */
694 /* XXX Should NCH(n) check be made a separate check? */
695 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
696 asdl_seq_APPEND(defaults,
697 ast_for_expr(c, CHILD(n, i + 2)));
698 i += 2;
699 found_default = 1;
700 }
701 else if (found_default) {
702 ast_error(n,
703 "non-default argument follows default argument");
704 goto error;
705 }
706
707 if (NCH(ch) == 3) {
708 asdl_seq_APPEND(args,
709 compiler_complex_args(CHILD(ch, 1)));
710 }
711 else if (TYPE(CHILD(ch, 0)) == NAME) {
Armin Rigo31441302005-10-21 12:57:31 +0000712 expr_ty name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
714 ast_error(CHILD(ch, 0), "assignment to None");
715 goto error;
716 }
Armin Rigo31441302005-10-21 12:57:31 +0000717 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
718 Param, LINENO(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 if (!name)
720 goto error;
721 asdl_seq_APPEND(args, name);
722
723 }
724 i += 2; /* the name and the comma */
725 break;
726 case STAR:
727 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
728 ast_error(CHILD(n, i+1), "assignment to None");
729 goto error;
730 }
731 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
732 i += 3;
733 break;
734 case DOUBLESTAR:
735 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
736 ast_error(CHILD(n, i+1), "assignment to None");
737 goto error;
738 }
739 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
740 i += 3;
741 break;
742 default:
Neal Norwitz79792652005-11-14 04:25:03 +0000743 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 "unexpected node in varargslist: %d @ %d",
745 TYPE(ch), i);
746 goto error;
747 }
748 }
749
750 return arguments(args, vararg, kwarg, defaults);
751
752 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +0000753 Py_XDECREF(vararg);
754 Py_XDECREF(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 if (args)
Neal Norwitze76adcd2005-11-15 05:04:31 +0000756 asdl_expr_seq_free(args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 if (defaults)
Neal Norwitze76adcd2005-11-15 05:04:31 +0000758 asdl_expr_seq_free(defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 return NULL;
760}
761
762static expr_ty
763ast_for_dotted_name(struct compiling *c, const node *n)
764{
765 expr_ty e = NULL;
766 expr_ty attrib = NULL;
767 identifier id = NULL;
768 int i;
769
770 REQ(n, dotted_name);
771
772 id = NEW_IDENTIFIER(CHILD(n, 0));
773 if (!id)
774 goto error;
775 e = Name(id, Load, LINENO(n));
776 if (!e)
777 goto error;
778 id = NULL;
779
780 for (i = 2; i < NCH(n); i+=2) {
781 id = NEW_IDENTIFIER(CHILD(n, i));
782 if (!id)
783 goto error;
784 attrib = Attribute(e, id, Load, LINENO(CHILD(n, i)));
785 if (!attrib)
786 goto error;
787 e = attrib;
788 attrib = NULL;
789 }
790
791 return e;
792
793 error:
794 Py_XDECREF(id);
795 free_expr(e);
796 return NULL;
797}
798
799static expr_ty
800ast_for_decorator(struct compiling *c, const node *n)
801{
802 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
803 expr_ty d = NULL;
804 expr_ty name_expr = NULL;
805
806 REQ(n, decorator);
807
808 if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6)
809 || TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) {
810 ast_error(n, "Invalid decorator node");
811 goto error;
812 }
813
814 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
815 if (!name_expr)
816 goto error;
817
818 if (NCH(n) == 3) { /* No arguments */
819 d = name_expr;
820 name_expr = NULL;
821 }
822 else if (NCH(n) == 5) { /* Call with no arguments */
823 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n));
824 if (!d)
825 goto error;
826 name_expr = NULL;
827 }
828 else {
829 d = ast_for_call(c, CHILD(n, 3), name_expr);
830 if (!d)
831 goto error;
832 name_expr = NULL;
833 }
834
835 return d;
836
837 error:
838 free_expr(name_expr);
839 free_expr(d);
840 return NULL;
841}
842
843static asdl_seq*
844ast_for_decorators(struct compiling *c, const node *n)
845{
846 asdl_seq* decorator_seq = NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +0000847 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 int i;
849
850 REQ(n, decorators);
851
852 decorator_seq = asdl_seq_new(NCH(n));
853 if (!decorator_seq)
854 return NULL;
855
856 for (i = 0; i < NCH(n); i++) {
857 d = ast_for_decorator(c, CHILD(n, i));
858 if (!d)
859 goto error;
860 asdl_seq_APPEND(decorator_seq, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 }
862 return decorator_seq;
863 error:
864 asdl_expr_seq_free(decorator_seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 return NULL;
866}
867
868static stmt_ty
869ast_for_funcdef(struct compiling *c, const node *n)
870{
871 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
872 identifier name = NULL;
873 arguments_ty args = NULL;
874 asdl_seq *body = NULL;
875 asdl_seq *decorator_seq = NULL;
876 int name_i;
877
878 REQ(n, funcdef);
879
880 if (NCH(n) == 6) { /* decorators are present */
881 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
882 if (!decorator_seq)
883 goto error;
884 name_i = 2;
885 }
886 else {
887 name_i = 1;
888 }
889
890 name = NEW_IDENTIFIER(CHILD(n, name_i));
891 if (!name)
892 goto error;
893 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
Neal Norwitze76adcd2005-11-15 05:04:31 +0000894 ast_error(CHILD(n, name_i), "assignment to None");
895 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896 }
897 args = ast_for_arguments(c, CHILD(n, name_i + 1));
898 if (!args)
899 goto error;
900 body = ast_for_suite(c, CHILD(n, name_i + 3));
901 if (!body)
902 goto error;
903
904 return FunctionDef(name, args, body, decorator_seq, LINENO(n));
905
906error:
907 asdl_stmt_seq_free(body);
908 asdl_expr_seq_free(decorator_seq);
909 free_arguments(args);
910 Py_XDECREF(name);
911 return NULL;
912}
913
914static expr_ty
915ast_for_lambdef(struct compiling *c, const node *n)
916{
917 /* lambdef: 'lambda' [varargslist] ':' test */
918 arguments_ty args;
919 expr_ty expression;
920
921 if (NCH(n) == 3) {
922 args = arguments(NULL, NULL, NULL, NULL);
923 if (!args)
924 return NULL;
925 expression = ast_for_expr(c, CHILD(n, 2));
926 if (!expression) {
927 free_arguments(args);
928 return NULL;
929 }
930 }
931 else {
932 args = ast_for_arguments(c, CHILD(n, 1));
933 if (!args)
934 return NULL;
935 expression = ast_for_expr(c, CHILD(n, 3));
936 if (!expression) {
937 free_arguments(args);
938 return NULL;
939 }
940 }
941
942 return Lambda(args, expression, LINENO(n));
943}
944
945/* Count the number of 'for' loop in a list comprehension.
946
947 Helper for ast_for_listcomp().
948*/
949
950static int
951count_list_fors(const node *n)
952{
953 int n_fors = 0;
954 node *ch = CHILD(n, 1);
955
956 count_list_for:
957 n_fors++;
958 REQ(ch, list_for);
959 if (NCH(ch) == 5)
960 ch = CHILD(ch, 4);
961 else
962 return n_fors;
963 count_list_iter:
964 REQ(ch, list_iter);
965 ch = CHILD(ch, 0);
966 if (TYPE(ch) == list_for)
967 goto count_list_for;
968 else if (TYPE(ch) == list_if) {
969 if (NCH(ch) == 3) {
970 ch = CHILD(ch, 2);
971 goto count_list_iter;
972 }
973 else
974 return n_fors;
975 }
976 else {
977 /* Should never be reached */
Neal Norwitz7b3d5e12005-11-13 21:17:28 +0000978 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 return -1;
980 }
981}
982
983/* Count the number of 'if' statements in a list comprehension.
984
985 Helper for ast_for_listcomp().
986*/
987
988static int
989count_list_ifs(const node *n)
990{
991 int n_ifs = 0;
992
993 count_list_iter:
994 REQ(n, list_iter);
995 if (TYPE(CHILD(n, 0)) == list_for)
996 return n_ifs;
997 n = CHILD(n, 0);
998 REQ(n, list_if);
999 n_ifs++;
1000 if (NCH(n) == 2)
1001 return n_ifs;
1002 n = CHILD(n, 2);
1003 goto count_list_iter;
1004}
1005
1006static expr_ty
1007ast_for_listcomp(struct compiling *c, const node *n)
1008{
1009 /* listmaker: test ( list_for | (',' test)* [','] )
1010 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1011 list_iter: list_for | list_if
1012 list_if: 'if' test [list_iter]
1013 testlist_safe: test [(',' test)+ [',']]
1014 */
1015 expr_ty elt;
1016 asdl_seq *listcomps;
1017 int i, n_fors;
1018 node *ch;
1019
1020 REQ(n, listmaker);
1021 assert(NCH(n) > 1);
1022
1023 elt = ast_for_expr(c, CHILD(n, 0));
1024 if (!elt)
1025 return NULL;
1026
1027 n_fors = count_list_fors(n);
1028 if (n_fors == -1)
1029 return NULL;
1030
1031 listcomps = asdl_seq_new(n_fors);
1032 if (!listcomps) {
1033 free_expr(elt);
1034 return NULL;
1035 }
1036
1037 ch = CHILD(n, 1);
1038 for (i = 0; i < n_fors; i++) {
1039 comprehension_ty lc;
1040 asdl_seq *t;
1041 expr_ty expression;
1042
1043 REQ(ch, list_for);
1044
1045 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
1046 if (!t) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001047 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 free_expr(elt);
1049 return NULL;
1050 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001051 expression = ast_for_testlist(c, CHILD(ch, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001053 asdl_expr_seq_free(t);
1054 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 free_expr(elt);
1056 return NULL;
1057 }
1058
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001059 if (asdl_seq_LEN(t) == 1) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001061 /* only free the sequence since we grabbed element 0 above */
1062 if (lc)
1063 asdl_seq_free(t); /* ok */
1064 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 else
1066 lc = comprehension(Tuple(t, Store, LINENO(ch)), expression, NULL);
1067
1068 if (!lc) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001069 asdl_expr_seq_free(t);
1070 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 free_expr(expression);
1072 free_expr(elt);
1073 return NULL;
1074 }
1075
1076 if (NCH(ch) == 5) {
1077 int j, n_ifs;
1078 asdl_seq *ifs;
1079
1080 ch = CHILD(ch, 4);
1081 n_ifs = count_list_ifs(ch);
1082 if (n_ifs == -1) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001083 free_comprehension(lc);
1084 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 free_expr(elt);
1086 return NULL;
1087 }
1088
1089 ifs = asdl_seq_new(n_ifs);
1090 if (!ifs) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001091 free_comprehension(lc);
1092 asdl_comprehension_seq_free(listcomps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 free_expr(elt);
1094 return NULL;
1095 }
1096
1097 for (j = 0; j < n_ifs; j++) {
1098 REQ(ch, list_iter);
1099
1100 ch = CHILD(ch, 0);
1101 REQ(ch, list_if);
1102
1103 asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1)));
1104 if (NCH(ch) == 3)
1105 ch = CHILD(ch, 2);
1106 }
1107 /* on exit, must guarantee that ch is a list_for */
1108 if (TYPE(ch) == list_iter)
1109 ch = CHILD(ch, 0);
1110 lc->ifs = ifs;
1111 }
1112 asdl_seq_APPEND(listcomps, lc);
1113 }
1114
1115 return ListComp(elt, listcomps, LINENO(n));
1116}
1117
1118/*
1119 Count the number of 'for' loops in a generator expression.
1120
1121 Helper for ast_for_genexp().
1122*/
1123
1124static int
1125count_gen_fors(const node *n)
1126{
1127 int n_fors = 0;
1128 node *ch = CHILD(n, 1);
1129
1130 count_gen_for:
1131 n_fors++;
1132 REQ(ch, gen_for);
1133 if (NCH(ch) == 5)
1134 ch = CHILD(ch, 4);
1135 else
1136 return n_fors;
1137 count_gen_iter:
1138 REQ(ch, gen_iter);
1139 ch = CHILD(ch, 0);
1140 if (TYPE(ch) == gen_for)
1141 goto count_gen_for;
1142 else if (TYPE(ch) == gen_if) {
1143 if (NCH(ch) == 3) {
1144 ch = CHILD(ch, 2);
1145 goto count_gen_iter;
1146 }
1147 else
1148 return n_fors;
1149 }
1150 else {
1151 /* Should never be reached */
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00001152 PyErr_SetString(PyExc_SystemError,
1153 "logic error in count_gen_fors");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return -1;
1155 }
1156}
1157
1158/* Count the number of 'if' statements in a generator expression.
1159
1160 Helper for ast_for_genexp().
1161*/
1162
1163static int
1164count_gen_ifs(const node *n)
1165{
1166 int n_ifs = 0;
1167
1168 while (1) {
1169 REQ(n, gen_iter);
1170 if (TYPE(CHILD(n, 0)) == gen_for)
1171 return n_ifs;
1172 n = CHILD(n, 0);
1173 REQ(n, gen_if);
1174 n_ifs++;
1175 if (NCH(n) == 2)
1176 return n_ifs;
1177 n = CHILD(n, 2);
1178 }
1179}
1180
1181static expr_ty
1182ast_for_genexp(struct compiling *c, const node *n)
1183{
1184 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1185 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1186 expr_ty elt;
1187 asdl_seq *genexps;
1188 int i, n_fors;
1189 node *ch;
1190
1191 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1192 assert(NCH(n) > 1);
1193
1194 elt = ast_for_expr(c, CHILD(n, 0));
1195 if (!elt)
1196 return NULL;
1197
1198 n_fors = count_gen_fors(n);
1199 if (n_fors == -1)
1200 return NULL;
1201
1202 genexps = asdl_seq_new(n_fors);
1203 if (!genexps) {
1204 free_expr(elt);
1205 return NULL;
1206 }
1207
1208 ch = CHILD(n, 1);
1209 for (i = 0; i < n_fors; i++) {
1210 comprehension_ty ge;
1211 asdl_seq *t;
1212 expr_ty expression;
1213
1214 REQ(ch, gen_for);
1215
1216 t = ast_for_exprlist(c, CHILD(ch, 1), Store);
1217 if (!t) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001218 asdl_comprehension_seq_free(genexps);
1219 asdl_expr_seq_free(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 free_expr(elt);
1221 return NULL;
1222 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001223 expression = ast_for_expr(c, CHILD(ch, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001225 asdl_comprehension_seq_free(genexps);
1226 asdl_expr_seq_free(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 free_expr(elt);
1228 return NULL;
1229 }
1230
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001231 if (asdl_seq_LEN(t) == 1) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 ge = comprehension(asdl_seq_GET(t, 0), expression,
1233 NULL);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001234 /* only free the sequence since we grabbed element 0 above */
1235 if (ge)
1236 asdl_seq_free(t); /* ok */
1237 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 else
1239 ge = comprehension(Tuple(t, Store, LINENO(ch)),
1240 expression, NULL);
1241
1242 if (!ge) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001243 asdl_comprehension_seq_free(genexps);
1244 asdl_expr_seq_free(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 free_expr(elt);
1246 return NULL;
1247 }
1248
1249 if (NCH(ch) == 5) {
1250 int j, n_ifs;
1251 asdl_seq *ifs;
1252
1253 ch = CHILD(ch, 4);
1254 n_ifs = count_gen_ifs(ch);
1255 if (n_ifs == -1) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001256 asdl_comprehension_seq_free(genexps);
1257 free_comprehension(ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 free_expr(elt);
1259 return NULL;
1260 }
1261
1262 ifs = asdl_seq_new(n_ifs);
1263 if (!ifs) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001264 asdl_comprehension_seq_free(genexps);
1265 free_comprehension(ge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 free_expr(elt);
1267 return NULL;
1268 }
1269
1270 for (j = 0; j < n_ifs; j++) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001271 expr_ty expression;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 REQ(ch, gen_iter);
1273 ch = CHILD(ch, 0);
1274 REQ(ch, gen_if);
1275
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001276 expression = ast_for_expr(c, CHILD(ch, 1));
1277 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001278 asdl_expr_seq_free(ifs);
1279 asdl_comprehension_seq_free(genexps);
1280 free_comprehension(ge);
1281 free_expr(elt);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001282 return NULL;
1283 }
1284 asdl_seq_APPEND(ifs, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 if (NCH(ch) == 3)
1286 ch = CHILD(ch, 2);
1287 }
1288 /* on exit, must guarantee that ch is a gen_for */
1289 if (TYPE(ch) == gen_iter)
1290 ch = CHILD(ch, 0);
1291 ge->ifs = ifs;
1292 }
1293 asdl_seq_APPEND(genexps, ge);
1294 }
1295
1296 return GeneratorExp(elt, genexps, LINENO(n));
1297}
1298
1299static expr_ty
1300ast_for_atom(struct compiling *c, const node *n)
1301{
1302 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1303 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1304 */
1305 node *ch = CHILD(n, 0);
1306
1307 switch (TYPE(ch)) {
1308 case NAME:
1309 /* All names start in Load context, but may later be
1310 changed. */
1311 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n));
1312 case STRING: {
1313 PyObject *str = parsestrplus(c, n);
1314
1315 if (!str)
1316 return NULL;
1317
1318 return Str(str, LINENO(n));
1319 }
1320 case NUMBER: {
1321 PyObject *pynum = parsenumber(STR(ch));
1322
1323 if (!pynum)
1324 return NULL;
1325
1326 return Num(pynum, LINENO(n));
1327 }
1328 case LPAR: /* some parenthesized expressions */
1329 ch = CHILD(n, 1);
1330
1331 if (TYPE(ch) == RPAR)
1332 return Tuple(NULL, Load, LINENO(n));
1333
1334 if (TYPE(ch) == yield_expr)
1335 return ast_for_expr(c, ch);
1336
1337 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1338 return ast_for_genexp(c, ch);
1339
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001340 return ast_for_testlist_gexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 case LSQB: /* list (or list comprehension) */
1342 ch = CHILD(n, 1);
1343
1344 if (TYPE(ch) == RSQB)
1345 return List(NULL, Load, LINENO(n));
1346
1347 REQ(ch, listmaker);
1348 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1349 asdl_seq *elts = seq_for_testlist(c, ch);
1350
1351 if (!elts)
1352 return NULL;
1353
1354 return List(elts, Load, LINENO(n));
1355 }
1356 else
1357 return ast_for_listcomp(c, ch);
1358 case LBRACE: {
1359 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1360 int i, size;
1361 asdl_seq *keys, *values;
1362
1363 ch = CHILD(n, 1);
1364 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1365 keys = asdl_seq_new(size);
1366 if (!keys)
1367 return NULL;
1368
1369 values = asdl_seq_new(size);
1370 if (!values) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001371 asdl_seq_free(keys); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 return NULL;
1373 }
1374
1375 for (i = 0; i < NCH(ch); i += 4) {
1376 expr_ty expression;
1377
1378 expression = ast_for_expr(c, CHILD(ch, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001379 if (!expression) {
1380 asdl_expr_seq_free(keys);
1381 asdl_expr_seq_free(values);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001383 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384
1385 asdl_seq_SET(keys, i / 4, expression);
1386
1387 expression = ast_for_expr(c, CHILD(ch, i + 2));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001388 if (!expression) {
1389 asdl_expr_seq_free(keys);
1390 asdl_expr_seq_free(values);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001392 }
1393
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 asdl_seq_SET(values, i / 4, expression);
1395 }
1396 return Dict(keys, values, LINENO(n));
1397 }
1398 case BACKQUOTE: { /* repr */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001399 expr_ty expression = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400
1401 if (!expression)
1402 return NULL;
1403
1404 return Repr(expression, LINENO(n));
1405 }
1406 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001407 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 return NULL;
1409 }
1410}
1411
1412static slice_ty
1413ast_for_slice(struct compiling *c, const node *n)
1414{
1415 node *ch;
1416 expr_ty lower = NULL, upper = NULL, step = NULL;
1417
1418 REQ(n, subscript);
1419
1420 /*
1421 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1422 sliceop: ':' [test]
1423 */
1424 ch = CHILD(n, 0);
1425 if (TYPE(ch) == DOT)
1426 return Ellipsis();
1427
1428 if (NCH(n) == 1 && TYPE(ch) == test) {
1429 /* 'step' variable hold no significance in terms of being used over
1430 other vars */
1431 step = ast_for_expr(c, ch);
1432 if (!step)
1433 return NULL;
1434
1435 return Index(step);
1436 }
1437
1438 if (TYPE(ch) == test) {
1439 lower = ast_for_expr(c, ch);
1440 if (!lower)
1441 return NULL;
1442 }
1443
1444 /* If there's an upper bound it's in the second or third position. */
1445 if (TYPE(ch) == COLON) {
1446 if (NCH(n) > 1) {
1447 node *n2 = CHILD(n, 1);
1448
1449 if (TYPE(n2) == test) {
1450 upper = ast_for_expr(c, n2);
1451 if (!upper)
1452 return NULL;
1453 }
1454 }
1455 } else if (NCH(n) > 2) {
1456 node *n2 = CHILD(n, 2);
1457
1458 if (TYPE(n2) == test) {
1459 upper = ast_for_expr(c, n2);
1460 if (!upper)
1461 return NULL;
1462 }
1463 }
1464
1465 ch = CHILD(n, NCH(n) - 1);
1466 if (TYPE(ch) == sliceop) {
1467 if (NCH(ch) == 1)
1468 /* XXX: If only 1 child, then should just be a colon. Should we
1469 just skip assigning and just get to the return? */
1470 ch = CHILD(ch, 0);
1471 else
1472 ch = CHILD(ch, 1);
1473 if (TYPE(ch) == test) {
1474 step = ast_for_expr(c, ch);
1475 if (!step)
1476 return NULL;
1477 }
1478 }
1479
1480 return Slice(lower, upper, step);
1481}
1482
1483static expr_ty
1484ast_for_binop(struct compiling *c, const node *n)
1485{
1486 /* Must account for a sequence of expressions.
1487 How should A op B op C by represented?
1488 BinOp(BinOp(A, op, B), op, C).
1489 */
1490
1491 int i, nops;
1492 expr_ty expr1, expr2, result;
1493 operator_ty operator;
1494
1495 expr1 = ast_for_expr(c, CHILD(n, 0));
1496 if (!expr1)
1497 return NULL;
1498
1499 expr2 = ast_for_expr(c, CHILD(n, 2));
1500 if (!expr2)
1501 return NULL;
1502
1503 operator = get_operator(CHILD(n, 1));
1504 if (!operator)
1505 return NULL;
1506
1507 result = BinOp(expr1, operator, expr2, LINENO(n));
1508 if (!result)
1509 return NULL;
1510
1511 nops = (NCH(n) - 1) / 2;
1512 for (i = 1; i < nops; i++) {
1513 expr_ty tmp_result, tmp;
1514 const node* next_oper = CHILD(n, i * 2 + 1);
1515
1516 operator = get_operator(next_oper);
1517 if (!operator)
1518 return NULL;
1519
1520 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1521 if (!tmp)
1522 return NULL;
1523
1524 tmp_result = BinOp(result, operator, tmp,
1525 LINENO(next_oper));
1526 if (!tmp)
1527 return NULL;
1528 result = tmp_result;
1529 }
1530 return result;
1531}
1532
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001533static expr_ty
1534ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1535{
1536 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1537 expr_ty e;
1538 REQ(n, trailer);
1539 if (TYPE(CHILD(n, 0)) == LPAR) {
1540 if (NCH(n) == 2)
1541 e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n));
1542 else
1543 e = ast_for_call(c, CHILD(n, 1), left_expr);
1544 }
1545 else if (TYPE(CHILD(n, 0)) == LSQB) {
1546 REQ(CHILD(n, 2), RSQB);
1547 n = CHILD(n, 1);
1548 if (NCH(n) <= 2) {
1549 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1550 if (!slc)
1551 return NULL;
1552 e = Subscript(left_expr, slc, Load, LINENO(n));
1553 if (!e) {
1554 free_slice(slc);
1555 return NULL;
1556 }
1557 }
1558 else {
1559 int j;
1560 slice_ty slc;
1561 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2);
1562 if (!slices)
1563 return NULL;
1564 for (j = 0; j < NCH(n); j += 2) {
1565 slc = ast_for_slice(c, CHILD(n, j));
1566 if (!slc) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001567 for (j = j / 2; j >= 0; j--)
1568 free_slice(asdl_seq_GET(slices, j));
1569 asdl_seq_free(slices); /* ok */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001570 return NULL;
1571 }
1572 asdl_seq_SET(slices, j / 2, slc);
1573 }
1574 e = Subscript(left_expr, ExtSlice(slices), Load, LINENO(n));
1575 if (!e) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001576 for (j = 0; j < asdl_seq_LEN(slices); j++)
1577 free_slice(asdl_seq_GET(slices, j));
1578 asdl_seq_free(slices); /* ok */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001579 return NULL;
1580 }
1581 }
1582 }
1583 else {
1584 assert(TYPE(CHILD(n, 0)) == DOT);
1585 e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n));
1586 }
1587 return e;
1588}
1589
1590static expr_ty
1591ast_for_power(struct compiling *c, const node *n)
1592{
1593 /* power: atom trailer* ('**' factor)*
1594 */
1595 int i;
1596 expr_ty e, tmp;
1597 REQ(n, power);
1598 e = ast_for_atom(c, CHILD(n, 0));
1599 if (!e)
1600 return NULL;
1601 if (NCH(n) == 1)
1602 return e;
1603 for (i = 1; i < NCH(n); i++) {
1604 node *ch = CHILD(n, i);
1605 if (TYPE(ch) != trailer)
1606 break;
1607 tmp = ast_for_trailer(c, ch, e);
1608 if (!tmp) {
1609 free_expr(e);
1610 return NULL;
1611 }
1612 e = tmp;
1613 }
1614 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1615 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1616 if (!f) {
1617 free_expr(e);
1618 return NULL;
1619 }
1620 tmp = BinOp(e, Pow, f, LINENO(n));
1621 if (!tmp) {
Neal Norwitz6b347892005-11-15 07:17:53 +00001622 free_expr(f);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001623 free_expr(e);
1624 return NULL;
1625 }
1626 e = tmp;
1627 }
1628 return e;
1629}
1630
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631/* Do not name a variable 'expr'! Will cause a compile error.
1632*/
1633
1634static expr_ty
1635ast_for_expr(struct compiling *c, const node *n)
1636{
1637 /* handle the full range of simple expressions
1638 test: and_test ('or' and_test)* | lambdef
1639 and_test: not_test ('and' not_test)*
1640 not_test: 'not' not_test | comparison
1641 comparison: expr (comp_op expr)*
1642 expr: xor_expr ('|' xor_expr)*
1643 xor_expr: and_expr ('^' and_expr)*
1644 and_expr: shift_expr ('&' shift_expr)*
1645 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1646 arith_expr: term (('+'|'-') term)*
1647 term: factor (('*'|'/'|'%'|'//') factor)*
1648 factor: ('+'|'-'|'~') factor | power
1649 power: atom trailer* ('**' factor)*
1650 */
1651
1652 asdl_seq *seq;
1653 int i;
1654
1655 loop:
1656 switch (TYPE(n)) {
1657 case test:
1658 if (TYPE(CHILD(n, 0)) == lambdef)
1659 return ast_for_lambdef(c, CHILD(n, 0));
1660 /* Fall through to and_test */
1661 case and_test:
1662 if (NCH(n) == 1) {
1663 n = CHILD(n, 0);
1664 goto loop;
1665 }
1666 seq = asdl_seq_new((NCH(n) + 1) / 2);
1667 if (!seq)
1668 return NULL;
1669 for (i = 0; i < NCH(n); i += 2) {
1670 expr_ty e = ast_for_expr(c, CHILD(n, i));
1671 if (!e)
1672 return NULL;
1673 asdl_seq_SET(seq, i / 2, e);
1674 }
1675 if (!strcmp(STR(CHILD(n, 1)), "and"))
1676 return BoolOp(And, seq, LINENO(n));
1677 else {
1678 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1679 return BoolOp(Or, seq, LINENO(n));
1680 }
1681 break;
1682 case not_test:
1683 if (NCH(n) == 1) {
1684 n = CHILD(n, 0);
1685 goto loop;
1686 }
1687 else {
1688 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1689 if (!expression)
1690 return NULL;
1691
1692 return UnaryOp(Not, expression, LINENO(n));
1693 }
1694 case comparison:
1695 if (NCH(n) == 1) {
1696 n = CHILD(n, 0);
1697 goto loop;
1698 }
1699 else {
1700 expr_ty expression;
1701 asdl_seq *ops, *cmps;
1702 ops = asdl_seq_new(NCH(n) / 2);
1703 if (!ops)
1704 return NULL;
1705 cmps = asdl_seq_new(NCH(n) / 2);
1706 if (!cmps) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001707 asdl_seq_free(ops); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 return NULL;
1709 }
1710 for (i = 1; i < NCH(n); i += 2) {
1711 /* XXX cmpop_ty is just an enum */
1712 cmpop_ty operator;
1713
1714 operator = ast_for_comp_op(CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001715 if (!operator) {
1716 asdl_expr_seq_free(ops);
1717 asdl_expr_seq_free(cmps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001719 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720
1721 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001722 if (!expression) {
1723 asdl_expr_seq_free(ops);
1724 asdl_expr_seq_free(cmps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001726 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727
1728 asdl_seq_SET(ops, i / 2, (void *)operator);
1729 asdl_seq_SET(cmps, i / 2, expression);
1730 }
1731 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00001732 if (!expression) {
1733 asdl_expr_seq_free(ops);
1734 asdl_expr_seq_free(cmps);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001736 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737
1738 return Compare(expression, ops, cmps, LINENO(n));
1739 }
1740 break;
1741
1742 /* The next five cases all handle BinOps. The main body of code
1743 is the same in each case, but the switch turned inside out to
1744 reuse the code for each type of operator.
1745 */
1746 case expr:
1747 case xor_expr:
1748 case and_expr:
1749 case shift_expr:
1750 case arith_expr:
1751 case term:
1752 if (NCH(n) == 1) {
1753 n = CHILD(n, 0);
1754 goto loop;
1755 }
1756 return ast_for_binop(c, n);
1757 case yield_expr: {
1758 expr_ty exp = NULL;
1759 if (NCH(n) == 2) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001760 exp = ast_for_testlist(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 if (!exp)
1762 return NULL;
1763 }
1764 return Yield(exp, LINENO(n));
1765 }
1766 case factor: {
1767 expr_ty expression;
1768
1769 if (NCH(n) == 1) {
1770 n = CHILD(n, 0);
1771 goto loop;
1772 }
1773
1774 expression = ast_for_expr(c, CHILD(n, 1));
1775 if (!expression)
1776 return NULL;
1777
1778 switch (TYPE(CHILD(n, 0))) {
1779 case PLUS:
1780 return UnaryOp(UAdd, expression, LINENO(n));
1781 case MINUS:
1782 return UnaryOp(USub, expression, LINENO(n));
1783 case TILDE:
1784 return UnaryOp(Invert, expression, LINENO(n));
1785 }
Neal Norwitze76adcd2005-11-15 05:04:31 +00001786 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1787 TYPE(CHILD(n, 0)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788 break;
1789 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00001790 case power:
1791 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001793 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 return NULL;
1795 }
1796 /* should never get here */
1797 return NULL;
1798}
1799
1800static expr_ty
1801ast_for_call(struct compiling *c, const node *n, expr_ty func)
1802{
1803 /*
1804 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1805 | '**' test)
1806 argument: [test '='] test [gen_for] # Really [keyword '='] test
1807 */
1808
1809 int i, nargs, nkeywords, ngens;
1810 asdl_seq *args = NULL;
1811 asdl_seq *keywords = NULL;
1812 expr_ty vararg = NULL, kwarg = NULL;
1813
1814 REQ(n, arglist);
1815
1816 nargs = 0;
1817 nkeywords = 0;
1818 ngens = 0;
1819 for (i = 0; i < NCH(n); i++) {
1820 node *ch = CHILD(n, i);
1821 if (TYPE(ch) == argument) {
1822 if (NCH(ch) == 1)
1823 nargs++;
1824 else if (TYPE(CHILD(ch, 1)) == gen_for)
1825 ngens++;
1826 else
1827 nkeywords++;
1828 }
1829 }
1830 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1831 ast_error(n, "Generator expression must be parenthesised "
1832 "if not sole argument");
1833 return NULL;
1834 }
1835
1836 if (nargs + nkeywords + ngens > 255) {
1837 ast_error(n, "more than 255 arguments");
1838 return NULL;
1839 }
1840
1841 args = asdl_seq_new(nargs + ngens);
1842 if (!args)
1843 goto error;
1844 keywords = asdl_seq_new(nkeywords);
1845 if (!keywords)
1846 goto error;
1847 nargs = 0;
1848 nkeywords = 0;
1849 for (i = 0; i < NCH(n); i++) {
1850 node *ch = CHILD(n, i);
1851 if (TYPE(ch) == argument) {
1852 expr_ty e;
1853 if (NCH(ch) == 1) {
1854 e = ast_for_expr(c, CHILD(ch, 0));
1855 if (!e)
1856 goto error;
1857 asdl_seq_SET(args, nargs++, e);
1858 }
1859 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1860 e = ast_for_genexp(c, ch);
1861 if (!e)
1862 goto error;
1863 asdl_seq_SET(args, nargs++, e);
1864 }
1865 else {
1866 keyword_ty kw;
1867 identifier key;
1868
1869 /* CHILD(ch, 0) is test, but must be an identifier? */
1870 e = ast_for_expr(c, CHILD(ch, 0));
1871 if (!e)
1872 goto error;
1873 /* f(lambda x: x[0] = 3) ends up getting parsed with
1874 * LHS test = lambda x: x[0], and RHS test = 3.
1875 * SF bug 132313 points out that complaining about a keyword
1876 * then is very confusing.
1877 */
1878 if (e->kind == Lambda_kind) {
1879 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
1880 goto error;
1881 } else if (e->kind != Name_kind) {
1882 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1883 goto error;
1884 }
1885 key = e->v.Name.id;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001886 free(e); /* XXX: is free correct here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 e = ast_for_expr(c, CHILD(ch, 2));
1888 if (!e)
1889 goto error;
1890 kw = keyword(key, e);
1891 if (!kw)
1892 goto error;
1893 asdl_seq_SET(keywords, nkeywords++, kw);
1894 }
1895 }
1896 else if (TYPE(ch) == STAR) {
1897 vararg = ast_for_expr(c, CHILD(n, i+1));
1898 i++;
1899 }
1900 else if (TYPE(ch) == DOUBLESTAR) {
1901 kwarg = ast_for_expr(c, CHILD(n, i+1));
1902 i++;
1903 }
1904 }
1905
1906 return Call(func, args, keywords, vararg, kwarg, LINENO(n));
1907
1908 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +00001909 free_expr(vararg);
1910 free_expr(kwarg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 if (args)
Neal Norwitze76adcd2005-11-15 05:04:31 +00001912 asdl_expr_seq_free(args);
1913 if (keywords) {
1914 for (i = 0; i < asdl_seq_LEN(keywords); i++)
1915 free_keyword(asdl_seq_GET(keywords, i));
1916 asdl_seq_free(keywords); /* ok */
1917 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 return NULL;
1919}
1920
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001922ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923{
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001924 /* testlist_gexp: test (',' test)* [','] */
1925 /* testlist: test (',' test)* [','] */
1926 /* testlist_safe: test (',' test)+ [','] */
1927 /* testlist1: test (',' test)* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 assert(NCH(n) > 0);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001929 if (TYPE(n) == testlist_gexp) {
1930 if (NCH(n) > 1)
1931 assert(TYPE(CHILD(n, 1)) != gen_for);
1932 }
1933 else {
1934 assert(TYPE(n) == testlist ||
1935 TYPE(n) == testlist_safe ||
1936 TYPE(n) == testlist1);
1937 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938 if (NCH(n) == 1)
1939 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 else {
1941 asdl_seq *tmp = seq_for_testlist(c, n);
1942 if (!tmp)
1943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 return Tuple(tmp, Load, LINENO(n));
1945 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001946}
1947
1948static expr_ty
1949ast_for_testlist_gexp(struct compiling *c, const node* n)
1950{
1951 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1952 /* argument: test [ gen_for ] */
1953 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
1954 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) {
1955 return ast_for_genexp(c, n);
1956 }
1957 else
1958 return ast_for_testlist(c, n);
1959}
1960
1961/* like ast_for_testlist() but returns a sequence */
1962static asdl_seq*
1963ast_for_class_bases(struct compiling *c, const node* n)
1964{
1965 /* testlist: test (',' test)* [','] */
1966 assert(NCH(n) > 0);
1967 REQ(n, testlist);
1968 if (NCH(n) == 1) {
1969 expr_ty base;
1970 asdl_seq *bases = asdl_seq_new(1);
1971 if (!bases)
1972 return NULL;
1973 base = ast_for_expr(c, CHILD(n, 0));
1974 if (!base) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00001975 asdl_seq_free(bases); /* ok */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001976 return NULL;
1977 }
1978 asdl_seq_SET(bases, 0, base);
1979 return bases;
1980 }
1981 else {
1982 return seq_for_testlist(c, n);
1983 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984}
1985
1986static stmt_ty
1987ast_for_expr_stmt(struct compiling *c, const node *n)
1988{
1989 REQ(n, expr_stmt);
1990 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1991 | ('=' (yield_expr|testlist))*)
1992 testlist: test (',' test)* [',']
1993 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1994 | '<<=' | '>>=' | '**=' | '//='
1995 test: ... here starts the operator precendence dance
1996 */
1997
1998 if (NCH(n) == 1) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001999 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 if (!e)
2001 return NULL;
2002
2003 return Expr(e, LINENO(n));
2004 }
2005 else if (TYPE(CHILD(n, 1)) == augassign) {
2006 expr_ty expr1, expr2;
2007 operator_ty operator;
2008 node *ch = CHILD(n, 0);
2009
2010 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002011 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 else
2013 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch));
2014
2015 if (!expr1)
2016 return NULL;
2017 if (expr1->kind == GeneratorExp_kind) {
Neal Norwitze8c05362005-11-14 00:18:03 +00002018 free_expr(expr1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 ast_error(ch, "augmented assignment to generator "
2020 "expression not possible");
2021 return NULL;
2022 }
2023 if (expr1->kind == Name_kind) {
2024 char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2025 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
Neal Norwitze8c05362005-11-14 00:18:03 +00002026 free_expr(expr1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027 ast_error(ch, "assignment to None");
2028 return NULL;
2029 }
2030 }
2031
2032 ch = CHILD(n, 2);
2033 if (TYPE(ch) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002034 expr2 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 else
2036 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch));
Neal Norwitze8c05362005-11-14 00:18:03 +00002037 if (!expr2) {
2038 free_expr(expr1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 return NULL;
Neal Norwitze8c05362005-11-14 00:18:03 +00002040 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041
2042 operator = ast_for_augassign(CHILD(n, 1));
Neal Norwitze8c05362005-11-14 00:18:03 +00002043 if (!operator) {
2044 free_expr(expr1);
2045 free_expr(expr2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 return NULL;
Neal Norwitze8c05362005-11-14 00:18:03 +00002047 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048
2049 return AugAssign(expr1, operator, expr2, LINENO(n));
2050 }
2051 else {
2052 int i;
2053 asdl_seq *targets;
2054 node *value;
2055 expr_ty expression;
2056
2057 /* a normal assignment */
2058 REQ(CHILD(n, 1), EQUAL);
2059 targets = asdl_seq_new(NCH(n) / 2);
2060 if (!targets)
2061 return NULL;
2062 for (i = 0; i < NCH(n) - 2; i += 2) {
Armin Rigo31441302005-10-21 12:57:31 +00002063 expr_ty e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 node *ch = CHILD(n, i);
2065 if (TYPE(ch) == yield_expr) {
2066 ast_error(ch, "assignment to yield expression not possible");
2067 goto error;
2068 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002069 e = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070
2071 /* set context to assign */
2072 if (!e)
2073 goto error;
2074
2075 if (!set_context(e, Store, CHILD(n, i))) {
2076 free_expr(e);
2077 goto error;
2078 }
2079
2080 asdl_seq_SET(targets, i / 2, e);
2081 }
2082 value = CHILD(n, NCH(n) - 1);
2083 if (TYPE(value) == testlist)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002084 expression = ast_for_testlist(c, value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 else
2086 expression = ast_for_expr(c, value);
2087 if (!expression)
Neal Norwitze8c05362005-11-14 00:18:03 +00002088 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 return Assign(targets, expression, LINENO(n));
2090 error:
Neal Norwitze76adcd2005-11-15 05:04:31 +00002091 asdl_expr_seq_free(targets);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 }
2093 return NULL;
2094}
2095
2096static stmt_ty
2097ast_for_print_stmt(struct compiling *c, const node *n)
2098{
2099 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2100 | '>>' test [ (',' test)+ [','] ] )
2101 */
2102 expr_ty dest = NULL, expression;
2103 asdl_seq *seq;
2104 bool nl;
2105 int i, start = 1;
2106
2107 REQ(n, print_stmt);
2108 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2109 dest = ast_for_expr(c, CHILD(n, 2));
2110 if (!dest)
2111 return NULL;
2112 start = 4;
2113 }
2114 seq = asdl_seq_new((NCH(n) + 1 - start) / 2);
2115 if (!seq)
2116 return NULL;
2117 for (i = start; i < NCH(n); i += 2) {
2118 expression = ast_for_expr(c, CHILD(n, i));
2119 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002120 free_expr(dest);
2121 asdl_expr_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 return NULL;
2123 }
2124
2125 asdl_seq_APPEND(seq, expression);
2126 }
2127 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
2128 return Print(dest, seq, nl, LINENO(n));
2129}
2130
2131static asdl_seq *
2132ast_for_exprlist(struct compiling *c, const node *n, int context)
2133{
2134 asdl_seq *seq;
2135 int i;
2136 expr_ty e;
2137
2138 REQ(n, exprlist);
2139
2140 seq = asdl_seq_new((NCH(n) + 1) / 2);
2141 if (!seq)
2142 return NULL;
2143 for (i = 0; i < NCH(n); i += 2) {
2144 e = ast_for_expr(c, CHILD(n, i));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002145 if (!e)
2146 goto error;
Neal Norwitz6b347892005-11-15 07:17:53 +00002147 asdl_seq_SET(seq, i / 2, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 if (context) {
2149 if (!set_context(e, context, CHILD(n, i)))
Neal Norwitze76adcd2005-11-15 05:04:31 +00002150 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 }
2153 return seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002154
2155error:
2156 asdl_expr_seq_free(seq);
2157 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158}
2159
2160static stmt_ty
2161ast_for_del_stmt(struct compiling *c, const node *n)
2162{
2163 asdl_seq *expr_list;
2164
2165 /* del_stmt: 'del' exprlist */
2166 REQ(n, del_stmt);
2167
2168 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2169 if (!expr_list)
2170 return NULL;
2171 return Delete(expr_list, LINENO(n));
2172}
2173
2174static stmt_ty
2175ast_for_flow_stmt(struct compiling *c, const node *n)
2176{
2177 /*
2178 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2179 | yield_stmt
2180 break_stmt: 'break'
2181 continue_stmt: 'continue'
2182 return_stmt: 'return' [testlist]
2183 yield_stmt: yield_expr
2184 yield_expr: 'yield' testlist
2185 raise_stmt: 'raise' [test [',' test [',' test]]]
2186 */
2187 node *ch;
2188
2189 REQ(n, flow_stmt);
2190 ch = CHILD(n, 0);
2191 switch (TYPE(ch)) {
2192 case break_stmt:
2193 return Break(LINENO(n));
2194 case continue_stmt:
2195 return Continue(LINENO(n));
2196 case yield_stmt: { /* will reduce to yield_expr */
2197 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2198 if (!exp)
2199 return NULL;
2200 return Expr(exp, LINENO(n));
2201 }
2202 case return_stmt:
2203 if (NCH(ch) == 1)
2204 return Return(NULL, LINENO(n));
2205 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002206 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 if (!expression)
2208 return NULL;
2209 return Return(expression, LINENO(n));
2210 }
2211 case raise_stmt:
2212 if (NCH(ch) == 1)
2213 return Raise(NULL, NULL, NULL, LINENO(n));
2214 else if (NCH(ch) == 2) {
2215 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2216 if (!expression)
2217 return NULL;
2218 return Raise(expression, NULL, NULL, LINENO(n));
2219 }
2220 else if (NCH(ch) == 4) {
2221 expr_ty expr1, expr2;
2222
2223 expr1 = ast_for_expr(c, CHILD(ch, 1));
2224 if (!expr1)
2225 return NULL;
2226 expr2 = ast_for_expr(c, CHILD(ch, 3));
2227 if (!expr2)
2228 return NULL;
2229
2230 return Raise(expr1, expr2, NULL, LINENO(n));
2231 }
2232 else if (NCH(ch) == 6) {
2233 expr_ty expr1, expr2, expr3;
2234
2235 expr1 = ast_for_expr(c, CHILD(ch, 1));
2236 if (!expr1)
2237 return NULL;
2238 expr2 = ast_for_expr(c, CHILD(ch, 3));
2239 if (!expr2)
2240 return NULL;
2241 expr3 = ast_for_expr(c, CHILD(ch, 5));
2242 if (!expr3)
2243 return NULL;
2244
2245 return Raise(expr1, expr2, expr3, LINENO(n));
2246 }
2247 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002248 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 "unexpected flow_stmt: %d", TYPE(ch));
2250 return NULL;
2251 }
2252}
2253
2254static alias_ty
2255alias_for_import_name(const node *n)
2256{
2257 /*
2258 import_as_name: NAME [NAME NAME]
2259 dotted_as_name: dotted_name [NAME NAME]
2260 dotted_name: NAME ('.' NAME)*
2261 */
2262 loop:
2263 switch (TYPE(n)) {
2264 case import_as_name:
2265 if (NCH(n) == 3)
2266 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
2267 NEW_IDENTIFIER(CHILD(n, 2)));
2268 else
2269 return alias(NEW_IDENTIFIER(CHILD(n, 0)),
2270 NULL);
2271 break;
2272 case dotted_as_name:
2273 if (NCH(n) == 1) {
2274 n = CHILD(n, 0);
2275 goto loop;
2276 }
2277 else {
2278 alias_ty a = alias_for_import_name(CHILD(n, 0));
2279 assert(!a->asname);
2280 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2281 return a;
2282 }
2283 break;
2284 case dotted_name:
2285 if (NCH(n) == 1)
2286 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL);
2287 else {
2288 /* Create a string of the form "a.b.c" */
2289 int i, len;
2290 PyObject *str;
2291 char *s;
2292
2293 len = 0;
2294 for (i = 0; i < NCH(n); i += 2)
2295 /* length of string plus one for the dot */
2296 len += strlen(STR(CHILD(n, i))) + 1;
2297 len--; /* the last name doesn't have a dot */
2298 str = PyString_FromStringAndSize(NULL, len);
2299 if (!str)
2300 return NULL;
2301 s = PyString_AS_STRING(str);
2302 if (!s)
2303 return NULL;
2304 for (i = 0; i < NCH(n); i += 2) {
2305 char *sch = STR(CHILD(n, i));
2306 strcpy(s, STR(CHILD(n, i)));
2307 s += strlen(sch);
2308 *s++ = '.';
2309 }
2310 --s;
2311 *s = '\0';
2312 PyString_InternInPlace(&str);
2313 return alias(str, NULL);
2314 }
2315 break;
2316 case STAR:
2317 return alias(PyString_InternFromString("*"), NULL);
2318 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002319 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 "unexpected import name: %d", TYPE(n));
2321 return NULL;
2322 }
2323 return NULL;
2324}
2325
2326static stmt_ty
2327ast_for_import_stmt(struct compiling *c, const node *n)
2328{
2329 /*
2330 import_stmt: import_name | import_from
2331 import_name: 'import' dotted_as_names
2332 import_from: 'from' dotted_name 'import' ('*' |
2333 '(' import_as_names ')' |
2334 import_as_names)
2335 */
2336 int i;
2337 asdl_seq *aliases;
2338
2339 REQ(n, import_stmt);
2340 n = CHILD(n, 0);
2341 if (STR(CHILD(n, 0))[0] == 'i') { /* import */
2342 n = CHILD(n, 1);
Neil Schemenauer147b7592005-10-23 03:38:19 +00002343 REQ(n, dotted_as_names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 aliases = asdl_seq_new((NCH(n) + 1) / 2);
2345 if (!aliases)
2346 return NULL;
2347 for (i = 0; i < NCH(n); i += 2) {
2348 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2349 if (!import_alias) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002350 asdl_alias_seq_free(aliases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 return NULL;
2352 }
2353 asdl_seq_SET(aliases, i / 2, import_alias);
2354 }
2355 return Import(aliases, LINENO(n));
2356 }
2357 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
2358 stmt_ty import;
2359 int n_children;
2360 const char *from_modules;
2361 int lineno = LINENO(n);
2362 alias_ty mod = alias_for_import_name(CHILD(n, 1));
2363 if (!mod)
2364 return NULL;
2365
2366 /* XXX this needs to be cleaned up */
2367
2368 from_modules = STR(CHILD(n, 3));
2369 if (!from_modules) {
2370 n = CHILD(n, 3); /* from ... import x, y, z */
2371 if (NCH(n) % 2 == 0) {
2372 /* it ends with a comma, not valid but the parser allows it */
Neal Norwitze76adcd2005-11-15 05:04:31 +00002373 free_alias(mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 ast_error(n, "trailing comma not allowed without"
2375 " surrounding parentheses");
2376 return NULL;
2377 }
2378 }
2379 else if (from_modules[0] == '*') {
2380 n = CHILD(n, 3); /* from ... import * */
2381 }
2382 else if (from_modules[0] == '(')
2383 n = CHILD(n, 4); /* from ... import (x, y, z) */
Neal Norwitze76adcd2005-11-15 05:04:31 +00002384 else {
2385 /* XXX: don't we need to call ast_error(n, "..."); */
2386 free_alias(mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002388 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389
2390 n_children = NCH(n);
2391 if (from_modules && from_modules[0] == '*')
2392 n_children = 1;
2393
2394 aliases = asdl_seq_new((n_children + 1) / 2);
2395 if (!aliases) {
2396 free_alias(mod);
2397 return NULL;
2398 }
2399
2400 /* handle "from ... import *" special b/c there's no children */
2401 if (from_modules && from_modules[0] == '*') {
2402 alias_ty import_alias = alias_for_import_name(n);
2403 if (!import_alias) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002404 asdl_alias_seq_free(aliases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 free_alias(mod);
2406 return NULL;
2407 }
2408 asdl_seq_APPEND(aliases, import_alias);
2409 }
2410
2411 for (i = 0; i < NCH(n); i += 2) {
2412 alias_ty import_alias = alias_for_import_name(CHILD(n, i));
2413 if (!import_alias) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002414 asdl_alias_seq_free(aliases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 free_alias(mod);
2416 return NULL;
2417 }
2418 asdl_seq_APPEND(aliases, import_alias);
2419 }
2420 Py_INCREF(mod->name);
2421 import = ImportFrom(mod->name, aliases, lineno);
2422 free_alias(mod);
2423 return import;
2424 }
Neal Norwitz79792652005-11-14 04:25:03 +00002425 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 "unknown import statement: starts with command '%s'",
2427 STR(CHILD(n, 0)));
2428 return NULL;
2429}
2430
2431static stmt_ty
2432ast_for_global_stmt(struct compiling *c, const node *n)
2433{
2434 /* global_stmt: 'global' NAME (',' NAME)* */
2435 identifier name;
2436 asdl_seq *s;
2437 int i;
2438
2439 REQ(n, global_stmt);
2440 s = asdl_seq_new(NCH(n) / 2);
2441 if (!s)
2442 return NULL;
2443 for (i = 1; i < NCH(n); i += 2) {
2444 name = NEW_IDENTIFIER(CHILD(n, i));
2445 if (!name) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002446 for (i = i / 2; i > 0; i--)
2447 Py_XDECREF((identifier) asdl_seq_GET(s, i));
2448 asdl_seq_free(s); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 return NULL;
2450 }
2451 asdl_seq_SET(s, i / 2, name);
2452 }
2453 return Global(s, LINENO(n));
2454}
2455
2456static stmt_ty
2457ast_for_exec_stmt(struct compiling *c, const node *n)
2458{
2459 expr_ty expr1, globals = NULL, locals = NULL;
2460 int n_children = NCH(n);
2461 if (n_children != 2 && n_children != 4 && n_children != 6) {
Neal Norwitz79792652005-11-14 04:25:03 +00002462 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 "poorly formed 'exec' statement: %d parts to statement",
2464 n_children);
2465 return NULL;
2466 }
2467
2468 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2469 REQ(n, exec_stmt);
2470 expr1 = ast_for_expr(c, CHILD(n, 1));
2471 if (!expr1)
2472 return NULL;
2473 if (n_children >= 4) {
2474 globals = ast_for_expr(c, CHILD(n, 3));
2475 if (!globals)
2476 return NULL;
2477 }
2478 if (n_children == 6) {
2479 locals = ast_for_expr(c, CHILD(n, 5));
2480 if (!locals)
2481 return NULL;
2482 }
2483
2484 return Exec(expr1, globals, locals, LINENO(n));
2485}
2486
2487static stmt_ty
2488ast_for_assert_stmt(struct compiling *c, const node *n)
2489{
2490 /* assert_stmt: 'assert' test [',' test] */
2491 REQ(n, assert_stmt);
2492 if (NCH(n) == 2) {
2493 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2494 if (!expression)
2495 return NULL;
2496 return Assert(expression, NULL, LINENO(n));
2497 }
2498 else if (NCH(n) == 4) {
2499 expr_ty expr1, expr2;
2500
2501 expr1 = ast_for_expr(c, CHILD(n, 1));
2502 if (!expr1)
2503 return NULL;
2504 expr2 = ast_for_expr(c, CHILD(n, 3));
2505 if (!expr2)
2506 return NULL;
2507
2508 return Assert(expr1, expr2, LINENO(n));
2509 }
Neal Norwitz79792652005-11-14 04:25:03 +00002510 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 "improper number of parts to 'assert' statement: %d",
2512 NCH(n));
2513 return NULL;
2514}
2515
2516static asdl_seq *
2517ast_for_suite(struct compiling *c, const node *n)
2518{
2519 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2520 asdl_seq *seq = NULL;
2521 stmt_ty s;
2522 int i, total, num, end, pos = 0;
2523 node *ch;
2524
2525 REQ(n, suite);
2526
2527 total = num_stmts(n);
2528 seq = asdl_seq_new(total);
2529 if (!seq)
2530 return NULL;
2531 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2532 n = CHILD(n, 0);
2533 /* simple_stmt always ends with a NEWLINE,
2534 and may have a trailing SEMI
2535 */
2536 end = NCH(n) - 1;
2537 if (TYPE(CHILD(n, end - 1)) == SEMI)
2538 end--;
2539 /* loop by 2 to skip semi-colons */
2540 for (i = 0; i < end; i += 2) {
2541 ch = CHILD(n, i);
2542 s = ast_for_stmt(c, ch);
2543 if (!s)
2544 goto error;
2545 asdl_seq_SET(seq, pos++, s);
2546 }
2547 }
2548 else {
2549 for (i = 2; i < (NCH(n) - 1); i++) {
2550 ch = CHILD(n, i);
2551 REQ(ch, stmt);
2552 num = num_stmts(ch);
2553 if (num == 1) {
2554 /* small_stmt or compound_stmt with only one child */
2555 s = ast_for_stmt(c, ch);
2556 if (!s)
2557 goto error;
2558 asdl_seq_SET(seq, pos++, s);
2559 }
2560 else {
2561 int j;
2562 ch = CHILD(ch, 0);
2563 REQ(ch, simple_stmt);
2564 for (j = 0; j < NCH(ch); j += 2) {
Neal Norwitzf8d403d2005-12-11 20:12:40 +00002565 /* statement terminates with a semi-colon ';' */
2566 if (NCH(CHILD(ch, j)) == 0) {
2567 assert((j + 1) == NCH(ch));
2568 break;
2569 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 s = ast_for_stmt(c, CHILD(ch, j));
2571 if (!s)
2572 goto error;
2573 asdl_seq_SET(seq, pos++, s);
2574 }
2575 }
2576 }
2577 }
2578 assert(pos == seq->size);
2579 return seq;
2580 error:
2581 if (seq)
Neal Norwitze76adcd2005-11-15 05:04:31 +00002582 asdl_stmt_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 return NULL;
2584}
2585
2586static stmt_ty
2587ast_for_if_stmt(struct compiling *c, const node *n)
2588{
2589 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2590 ['else' ':' suite]
2591 */
2592 char *s;
2593
2594 REQ(n, if_stmt);
2595
2596 if (NCH(n) == 4) {
2597 expr_ty expression;
2598 asdl_seq *suite_seq;
2599
2600 expression = ast_for_expr(c, CHILD(n, 1));
2601 if (!expression)
2602 return NULL;
2603 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002604 if (!suite_seq) {
2605 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002607 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608
2609 return If(expression, suite_seq, NULL, LINENO(n));
2610 }
2611 s = STR(CHILD(n, 4));
2612 /* s[2], the third character in the string, will be
2613 's' for el_s_e, or
2614 'i' for el_i_f
2615 */
2616 if (s[2] == 's') {
2617 expr_ty expression;
2618 asdl_seq *seq1, *seq2;
2619
2620 expression = ast_for_expr(c, CHILD(n, 1));
2621 if (!expression)
2622 return NULL;
2623 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002624 if (!seq1) {
2625 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002629 if (!seq2) {
2630 asdl_stmt_seq_free(seq1);
2631 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002633 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634
2635 return If(expression, seq1, seq2, LINENO(n));
2636 }
2637 else if (s[2] == 'i') {
2638 int i, n_elif, has_else = 0;
2639 asdl_seq *orelse = NULL;
2640 n_elif = NCH(n) - 4;
2641 /* must reference the child n_elif+1 since 'else' token is third,
2642 not fourth, child from the end. */
2643 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2644 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2645 has_else = 1;
2646 n_elif -= 3;
2647 }
2648 n_elif /= 4;
2649
2650 if (has_else) {
2651 expr_ty expression;
2652 asdl_seq *seq1, *seq2;
2653
2654 orelse = asdl_seq_new(1);
2655 if (!orelse)
2656 return NULL;
2657 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2658 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002659 asdl_seq_free(orelse); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 return NULL;
2661 }
2662 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2663 if (!seq1) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002664 free_expr(expression);
2665 asdl_seq_free(orelse); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 return NULL;
2667 }
2668 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2669 if (!seq2) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002670 free_expr(expression);
2671 asdl_stmt_seq_free(seq1);
2672 asdl_seq_free(orelse); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 return NULL;
2674 }
2675
2676 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
2677 LINENO(CHILD(n, NCH(n) - 6))));
2678 /* the just-created orelse handled the last elif */
2679 n_elif--;
2680 }
2681 else
2682 orelse = NULL;
2683
2684 for (i = 0; i < n_elif; i++) {
2685 int off = 5 + (n_elif - i - 1) * 4;
2686 expr_ty expression;
2687 asdl_seq *suite_seq;
2688 asdl_seq *new = asdl_seq_new(1);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002689 if (!new) {
2690 asdl_stmt_seq_free(orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002692 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 expression = ast_for_expr(c, CHILD(n, off));
2694 if (!expression) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002695 asdl_stmt_seq_free(orelse);
2696 asdl_seq_free(new); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 return NULL;
2698 }
2699 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2700 if (!suite_seq) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002701 asdl_stmt_seq_free(orelse);
2702 free_expr(expression);
2703 asdl_seq_free(new); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 return NULL;
2705 }
2706
2707 asdl_seq_SET(new, 0,
2708 If(expression, suite_seq, orelse,
2709 LINENO(CHILD(n, off))));
2710 orelse = new;
2711 }
2712 return If(ast_for_expr(c, CHILD(n, 1)),
2713 ast_for_suite(c, CHILD(n, 3)),
2714 orelse, LINENO(n));
2715 }
2716 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002717 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 "unexpected token in 'if' statement: %s", s);
2719 return NULL;
2720 }
2721}
2722
2723static stmt_ty
2724ast_for_while_stmt(struct compiling *c, const node *n)
2725{
2726 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2727 REQ(n, while_stmt);
2728
2729 if (NCH(n) == 4) {
2730 expr_ty expression;
2731 asdl_seq *suite_seq;
2732
2733 expression = ast_for_expr(c, CHILD(n, 1));
2734 if (!expression)
2735 return NULL;
2736 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002737 if (!suite_seq) {
2738 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002740 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 return While(expression, suite_seq, NULL, LINENO(n));
2742 }
2743 else if (NCH(n) == 7) {
2744 expr_ty expression;
2745 asdl_seq *seq1, *seq2;
2746
2747 expression = ast_for_expr(c, CHILD(n, 1));
2748 if (!expression)
2749 return NULL;
2750 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002751 if (!seq1) {
2752 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002754 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002756 if (!seq2) {
2757 asdl_stmt_seq_free(seq1);
2758 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002760 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761
2762 return While(expression, seq1, seq2, LINENO(n));
2763 }
2764 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002765 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 "wrong number of tokens for 'while' statement: %d",
2767 NCH(n));
2768 return NULL;
2769 }
2770}
2771
2772static stmt_ty
2773ast_for_for_stmt(struct compiling *c, const node *n)
2774{
2775 asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL;
2776 expr_ty expression;
2777 expr_ty target;
2778 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2779 REQ(n, for_stmt);
2780
2781 if (NCH(n) == 9) {
2782 seq = ast_for_suite(c, CHILD(n, 8));
2783 if (!seq)
2784 return NULL;
2785 }
2786
2787 _target = ast_for_exprlist(c, CHILD(n, 1), Store);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002788 if (!_target) {
2789 asdl_stmt_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002791 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 if (asdl_seq_LEN(_target) == 1) {
2793 target = asdl_seq_GET(_target, 0);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002794 asdl_seq_free(_target); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 }
2796 else
2797 target = Tuple(_target, Store, LINENO(n));
2798
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002799 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002800 if (!expression) {
2801 free_expr(target);
2802 asdl_stmt_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002804 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002806 if (!suite_seq) {
2807 free_expr(target);
2808 free_expr(expression);
2809 asdl_stmt_seq_free(seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002811 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812
2813 return For(target, expression, suite_seq, seq, LINENO(n));
2814}
2815
2816static excepthandler_ty
2817ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2818{
2819 /* except_clause: 'except' [test [',' test]] */
2820 REQ(exc, except_clause);
2821 REQ(body, suite);
2822
2823 if (NCH(exc) == 1) {
2824 asdl_seq *suite_seq = ast_for_suite(c, body);
2825 if (!suite_seq)
2826 return NULL;
2827
2828 return excepthandler(NULL, NULL, suite_seq);
2829 }
2830 else if (NCH(exc) == 2) {
2831 expr_ty expression;
2832 asdl_seq *suite_seq;
2833
2834 expression = ast_for_expr(c, CHILD(exc, 1));
2835 if (!expression)
2836 return NULL;
2837 suite_seq = ast_for_suite(c, body);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002838 if (!suite_seq) {
2839 free_expr(expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002841 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842
2843 return excepthandler(expression, NULL, suite_seq);
2844 }
2845 else if (NCH(exc) == 4) {
2846 asdl_seq *suite_seq;
2847 expr_ty expression;
2848 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2849 if (!e)
2850 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002851 if (!set_context(e, Store, CHILD(exc, 3))) {
2852 free_expr(e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002854 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002856 if (!expression) {
2857 free_expr(e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 suite_seq = ast_for_suite(c, body);
Neal Norwitze76adcd2005-11-15 05:04:31 +00002861 if (!suite_seq) {
2862 free_expr(expression);
2863 free_expr(e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002865 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866
2867 return excepthandler(expression, e, suite_seq);
2868 }
2869 else {
Neal Norwitz79792652005-11-14 04:25:03 +00002870 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 "wrong number of children for 'except' clause: %d",
2872 NCH(exc));
2873 return NULL;
2874 }
2875}
2876
2877static stmt_ty
2878ast_for_try_stmt(struct compiling *c, const node *n)
2879{
2880 REQ(n, try_stmt);
2881
2882 if (TYPE(CHILD(n, 3)) == NAME) {/* must be 'finally' */
2883 /* try_stmt: 'try' ':' suite 'finally' ':' suite) */
2884 asdl_seq *s1, *s2;
2885 s1 = ast_for_suite(c, CHILD(n, 2));
2886 if (!s1)
2887 return NULL;
2888 s2 = ast_for_suite(c, CHILD(n, 5));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002889 if (!s2) {
2890 asdl_stmt_seq_free(s1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893
2894 return TryFinally(s1, s2, LINENO(n));
2895 }
2896 else if (TYPE(CHILD(n, 3)) == except_clause) {
2897 /* try_stmt: ('try' ':' suite (except_clause ':' suite)+
2898 ['else' ':' suite]
2899 */
2900 asdl_seq *suite_seq1, *suite_seq2;
2901 asdl_seq *handlers;
2902 int i, has_else = 0, n_except = NCH(n) - 3;
2903 if (TYPE(CHILD(n, NCH(n) - 3)) == NAME) {
2904 has_else = 1;
2905 n_except -= 3;
2906 }
2907 n_except /= 3;
2908 handlers = asdl_seq_new(n_except);
2909 if (!handlers)
2910 return NULL;
2911 for (i = 0; i < n_except; i++) {
2912 excepthandler_ty e = ast_for_except_clause(c,
2913 CHILD(n, 3 + i * 3),
2914 CHILD(n, 5 + i * 3));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002915 if (!e) {
2916 for ( ; i >= 0; i--)
2917 free_excepthandler(asdl_seq_GET(handlers, i));
2918 asdl_seq_free(handlers); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002920 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921 asdl_seq_SET(handlers, i, e);
2922 }
2923
2924 suite_seq1 = ast_for_suite(c, CHILD(n, 2));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002925 if (!suite_seq1) {
2926 for (i = 0; i < asdl_seq_LEN(handlers); i++)
2927 free_excepthandler(asdl_seq_GET(handlers, i));
2928 asdl_seq_free(handlers); /* ok */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002930 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 if (has_else) {
2932 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002933 if (!suite_seq2) {
2934 for (i = 0; i < asdl_seq_LEN(handlers); i++)
2935 free_excepthandler(asdl_seq_GET(handlers, i));
2936 asdl_seq_free(handlers); /* ok */
2937 asdl_stmt_seq_free(suite_seq1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 return NULL;
Neal Norwitze76adcd2005-11-15 05:04:31 +00002939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 }
2941 else
2942 suite_seq2 = NULL;
2943
2944 return TryExcept(suite_seq1, handlers, suite_seq2, LINENO(n));
2945 }
2946 else {
Neal Norwitz7b3d5e12005-11-13 21:17:28 +00002947 ast_error(n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 return NULL;
2949 }
2950}
2951
2952static stmt_ty
2953ast_for_classdef(struct compiling *c, const node *n)
2954{
2955 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956 asdl_seq *bases, *s;
2957
2958 REQ(n, classdef);
2959
2960 if (!strcmp(STR(CHILD(n, 1)), "None")) {
2961 ast_error(n, "assignment to None");
2962 return NULL;
2963 }
2964
2965 if (NCH(n) == 4) {
2966 s = ast_for_suite(c, CHILD(n, 3));
2967 if (!s)
2968 return NULL;
2969 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2970 }
2971 /* check for empty base list */
2972 if (TYPE(CHILD(n,3)) == RPAR) {
2973 s = ast_for_suite(c, CHILD(n,5));
2974 if (!s)
2975 return NULL;
2976 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n));
2977 }
2978
2979 /* else handle the base class list */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002980 bases = ast_for_class_bases(c, CHILD(n, 3));
2981 if (!bases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983
2984 s = ast_for_suite(c, CHILD(n, 6));
2985 if (!s) {
Neal Norwitze76adcd2005-11-15 05:04:31 +00002986 asdl_expr_seq_free(bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 return NULL;
2988 }
2989 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n));
2990}
2991
2992static stmt_ty
2993ast_for_stmt(struct compiling *c, const node *n)
2994{
2995 if (TYPE(n) == stmt) {
2996 assert(NCH(n) == 1);
2997 n = CHILD(n, 0);
2998 }
2999 if (TYPE(n) == simple_stmt) {
3000 assert(num_stmts(n) == 1);
3001 n = CHILD(n, 0);
3002 }
3003 if (TYPE(n) == small_stmt) {
3004 REQ(n, small_stmt);
3005 n = CHILD(n, 0);
3006 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3007 | flow_stmt | import_stmt | global_stmt | exec_stmt
3008 | assert_stmt
3009 */
3010 switch (TYPE(n)) {
3011 case expr_stmt:
3012 return ast_for_expr_stmt(c, n);
3013 case print_stmt:
3014 return ast_for_print_stmt(c, n);
3015 case del_stmt:
3016 return ast_for_del_stmt(c, n);
3017 case pass_stmt:
3018 return Pass(LINENO(n));
3019 case flow_stmt:
3020 return ast_for_flow_stmt(c, n);
3021 case import_stmt:
3022 return ast_for_import_stmt(c, n);
3023 case global_stmt:
3024 return ast_for_global_stmt(c, n);
3025 case exec_stmt:
3026 return ast_for_exec_stmt(c, n);
3027 case assert_stmt:
3028 return ast_for_assert_stmt(c, n);
3029 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003030 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3032 TYPE(n), NCH(n));
3033 return NULL;
3034 }
3035 }
3036 else {
3037 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3038 | funcdef | classdef
3039 */
3040 node *ch = CHILD(n, 0);
3041 REQ(n, compound_stmt);
3042 switch (TYPE(ch)) {
3043 case if_stmt:
3044 return ast_for_if_stmt(c, ch);
3045 case while_stmt:
3046 return ast_for_while_stmt(c, ch);
3047 case for_stmt:
3048 return ast_for_for_stmt(c, ch);
3049 case try_stmt:
3050 return ast_for_try_stmt(c, ch);
3051 case funcdef:
3052 return ast_for_funcdef(c, ch);
3053 case classdef:
3054 return ast_for_classdef(c, ch);
3055 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003056 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3058 TYPE(n), NCH(n));
3059 return NULL;
3060 }
3061 }
3062}
3063
3064static PyObject *
3065parsenumber(const char *s)
3066{
3067 const char *end;
3068 long x;
3069 double dx;
3070#ifndef WITHOUT_COMPLEX
3071 Py_complex c;
3072 int imflag;
3073#endif
3074
3075 errno = 0;
3076 end = s + strlen(s) - 1;
3077#ifndef WITHOUT_COMPLEX
3078 imflag = *end == 'j' || *end == 'J';
3079#endif
3080 if (*end == 'l' || *end == 'L')
3081 return PyLong_FromString((char *)s, (char **)0, 0);
3082 if (s[0] == '0') {
3083 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3084 if (x < 0 && errno == 0) {
3085 return PyLong_FromString((char *)s,
3086 (char **)0,
3087 0);
3088 }
3089 }
3090 else
3091 x = PyOS_strtol((char *)s, (char **)&end, 0);
3092 if (*end == '\0') {
3093 if (errno != 0)
3094 return PyLong_FromString((char *)s, (char **)0, 0);
3095 return PyInt_FromLong(x);
3096 }
3097 /* XXX Huge floats may silently fail */
3098#ifndef WITHOUT_COMPLEX
3099 if (imflag) {
3100 c.real = 0.;
3101 PyFPE_START_PROTECT("atof", return 0)
3102 c.imag = atof(s);
3103 PyFPE_END_PROTECT(c)
3104 return PyComplex_FromCComplex(c);
3105 }
3106 else
3107#endif
3108 {
3109 PyFPE_START_PROTECT("atof", return 0)
3110 dx = atof(s);
3111 PyFPE_END_PROTECT(dx)
3112 return PyFloat_FromDouble(dx);
3113 }
3114}
3115
3116static PyObject *
3117decode_utf8(const char **sPtr, const char *end, char* encoding)
3118{
3119#ifndef Py_USING_UNICODE
3120 Py_FatalError("decode_utf8 should not be called in this build.");
3121 return NULL;
3122#else
3123 PyObject *u, *v;
3124 char *s, *t;
3125 t = s = (char *)*sPtr;
3126 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3127 while (s < end && (*s & 0x80)) s++;
3128 *sPtr = s;
3129 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3130 if (u == NULL)
3131 return NULL;
3132 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3133 Py_DECREF(u);
3134 return v;
3135#endif
3136}
3137
3138static PyObject *
3139decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3140{
3141 PyObject *v, *u;
3142 char *buf;
3143 char *p;
3144 const char *end;
3145 if (encoding == NULL) {
3146 buf = (char *)s;
3147 u = NULL;
3148 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3149 buf = (char *)s;
3150 u = NULL;
3151 } else {
3152 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3153 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3154 if (u == NULL)
3155 return NULL;
3156 p = buf = PyString_AsString(u);
3157 end = s + len;
3158 while (s < end) {
3159 if (*s == '\\') {
3160 *p++ = *s++;
3161 if (*s & 0x80) {
3162 strcpy(p, "u005c");
3163 p += 5;
3164 }
3165 }
3166 if (*s & 0x80) { /* XXX inefficient */
3167 PyObject *w;
3168 char *r;
3169 int rn, i;
3170 w = decode_utf8(&s, end, "utf-16-be");
3171 if (w == NULL) {
3172 Py_DECREF(u);
3173 return NULL;
3174 }
3175 r = PyString_AsString(w);
3176 rn = PyString_Size(w);
3177 assert(rn % 2 == 0);
3178 for (i = 0; i < rn; i += 2) {
3179 sprintf(p, "\\u%02x%02x",
3180 r[i + 0] & 0xFF,
3181 r[i + 1] & 0xFF);
3182 p += 6;
3183 }
3184 Py_DECREF(w);
3185 } else {
3186 *p++ = *s++;
3187 }
3188 }
3189 len = p - buf;
3190 s = buf;
3191 }
3192 if (rawmode)
3193 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3194 else
3195 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3196 Py_XDECREF(u);
3197 return v;
3198}
3199
3200/* s is a Python string literal, including the bracketing quote characters,
3201 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3202 * parsestr parses it, and returns the decoded Python string object.
3203 */
3204static PyObject *
3205parsestr(const char *s, const char *encoding)
3206{
3207 PyObject *v;
3208 size_t len;
3209 int quote = *s;
3210 int rawmode = 0;
3211 int need_encoding;
3212 int unicode = 0;
3213
3214 if (isalpha(quote) || quote == '_') {
3215 if (quote == 'u' || quote == 'U') {
3216 quote = *++s;
3217 unicode = 1;
3218 }
3219 if (quote == 'r' || quote == 'R') {
3220 quote = *++s;
3221 rawmode = 1;
3222 }
3223 }
3224 if (quote != '\'' && quote != '\"') {
3225 PyErr_BadInternalCall();
3226 return NULL;
3227 }
3228 s++;
3229 len = strlen(s);
3230 if (len > INT_MAX) {
3231 PyErr_SetString(PyExc_OverflowError,
3232 "string to parse is too long");
3233 return NULL;
3234 }
3235 if (s[--len] != quote) {
3236 PyErr_BadInternalCall();
3237 return NULL;
3238 }
3239 if (len >= 4 && s[0] == quote && s[1] == quote) {
3240 s += 2;
3241 len -= 2;
3242 if (s[--len] != quote || s[--len] != quote) {
3243 PyErr_BadInternalCall();
3244 return NULL;
3245 }
3246 }
3247#ifdef Py_USING_UNICODE
3248 if (unicode || Py_UnicodeFlag) {
3249 return decode_unicode(s, len, rawmode, encoding);
3250 }
3251#endif
3252 need_encoding = (encoding != NULL &&
3253 strcmp(encoding, "utf-8") != 0 &&
3254 strcmp(encoding, "iso-8859-1") != 0);
3255 if (rawmode || strchr(s, '\\') == NULL) {
3256 if (need_encoding) {
3257#ifndef Py_USING_UNICODE
3258 /* This should not happen - we never see any other
3259 encoding. */
3260 Py_FatalError("cannot deal with encodings in this build.");
3261#else
3262 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
3263 if (u == NULL)
3264 return NULL;
3265 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3266 Py_DECREF(u);
3267 return v;
3268#endif
3269 } else {
3270 return PyString_FromStringAndSize(s, len);
3271 }
3272 }
3273
3274 v = PyString_DecodeEscape(s, len, NULL, unicode,
3275 need_encoding ? encoding : NULL);
3276 return v;
3277}
3278
3279/* Build a Python string object out of a STRING atom. This takes care of
3280 * compile-time literal catenation, calling parsestr() on each piece, and
3281 * pasting the intermediate results together.
3282 */
3283static PyObject *
3284parsestrplus(struct compiling *c, const node *n)
3285{
3286 PyObject *v;
3287 int i;
3288 REQ(CHILD(n, 0), STRING);
3289 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
3290 /* String literal concatenation */
3291 for (i = 1; i < NCH(n); i++) {
3292 PyObject *s;
3293 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
3294 if (s == NULL)
3295 goto onError;
3296 if (PyString_Check(v) && PyString_Check(s)) {
3297 PyString_ConcatAndDel(&v, s);
3298 if (v == NULL)
3299 goto onError;
3300 }
3301#ifdef Py_USING_UNICODE
3302 else {
3303 PyObject *temp;
3304 temp = PyUnicode_Concat(v, s);
3305 Py_DECREF(s);
3306 if (temp == NULL)
3307 goto onError;
3308 Py_DECREF(v);
3309 v = temp;
3310 }
3311#endif
3312 }
3313 }
3314 return v;
3315
3316 onError:
3317 Py_XDECREF(v);
3318 return NULL;
3319}