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().
7 #include "Python-ast.h"
18 /* Data structure used internally */
20 char *c_encoding
; /* source encoding */
21 int c_future_unicode
; /* __future__ unicode literals flag */
22 PyArena
*c_arena
; /* arena for allocating memeory */
23 const char *c_filename
; /* filename */
26 static asdl_seq
*seq_for_testlist(struct compiling
*, const node
*);
27 static expr_ty
ast_for_expr(struct compiling
*, const node
*);
28 static stmt_ty
ast_for_stmt(struct compiling
*, const node
*);
29 static asdl_seq
*ast_for_suite(struct compiling
*, const node
*);
30 static asdl_seq
*ast_for_exprlist(struct compiling
*, const node
*,
32 static expr_ty
ast_for_testlist(struct compiling
*, const node
*);
33 static stmt_ty
ast_for_classdef(struct compiling
*, const node
*, asdl_seq
*);
34 static expr_ty
ast_for_testlist_gexp(struct compiling
*, const node
*);
36 /* Note different signature for ast_for_call */
37 static expr_ty
ast_for_call(struct compiling
*, const node
*, expr_ty
);
39 static PyObject
*parsenumber(const char *);
40 static PyObject
*parsestr(struct compiling
*, const char *);
41 static PyObject
*parsestrplus(struct compiling
*, const node
*n
);
44 #define LINENO(n) ((n)->n_lineno)
48 new_identifier(const char* n
, PyArena
*arena
) {
49 PyObject
* id
= PyString_InternFromString(n
);
50 PyArena_AddPyObject(arena
, id
);
54 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
56 /* This routine provides an invalid object for the syntax error.
57 The outermost routine must unpack this error and create the
58 proper object. We do this so that we don't have to pass
59 the filename to everything function.
61 XXX Maybe we should just pass the filename...
65 ast_error(const node
*n
, const char *errstr
)
67 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
70 PyErr_SetObject(PyExc_SyntaxError
, u
);
76 ast_error_finish(const char *filename
)
78 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
81 assert(PyErr_Occurred());
82 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
85 PyErr_Fetch(&type
, &value
, &tback
);
86 errstr
= PyTuple_GetItem(value
, 0);
90 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
97 loc
= PyErr_ProgramText(filename
, lineno
);
102 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
108 value
= PyTuple_Pack(2, errstr
, tmp
);
113 PyErr_Restore(type
, value
, tback
);
116 /* num_stmts() returns number of contained statements.
118 Use this routine to determine how big a sequence is needed for
119 the statements in a parse tree. Its raison d'etre is this bit of
122 stmt: simple_stmt | compound_stmt
123 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
125 A simple_stmt can contain multiple small_stmt elements joined
126 by semicolons. If the arg is a simple_stmt, the number of
127 small_stmt elements is returned.
131 num_stmts(const node
*n
)
138 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
141 return num_stmts(CHILD(n
, 0));
144 for (i
= 0; i
< NCH(n
); i
++) {
146 if (TYPE(ch
) == stmt
)
151 return num_stmts(CHILD(n
, 0));
155 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
158 return num_stmts(CHILD(n
, 0));
161 for (i
= 2; i
< (NCH(n
) - 1); i
++)
162 l
+= num_stmts(CHILD(n
, i
));
168 sprintf(buf
, "Non-statement found: %d %d\n",
177 /* Transform the CST rooted at node * to the appropriate AST
181 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
185 asdl_seq
*stmts
= NULL
;
190 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
191 c
.c_encoding
= "utf-8";
192 if (TYPE(n
) == encoding_decl
) {
193 ast_error(n
, "encoding declaration in Unicode string");
196 } else if (TYPE(n
) == encoding_decl
) {
197 c
.c_encoding
= STR(n
);
202 c
.c_future_unicode
= flags
&& flags
->cf_flags
& CO_FUTURE_UNICODE_LITERALS
;
204 c
.c_filename
= filename
;
209 stmts
= asdl_seq_new(num_stmts(n
), arena
);
212 for (i
= 0; i
< NCH(n
) - 1; i
++) {
214 if (TYPE(ch
) == NEWLINE
)
219 s
= ast_for_stmt(&c
, ch
);
222 asdl_seq_SET(stmts
, k
++, s
);
226 REQ(ch
, simple_stmt
);
227 for (j
= 0; j
< num
; j
++) {
228 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
231 asdl_seq_SET(stmts
, k
++, s
);
235 return Module(stmts
, arena
);
237 expr_ty testlist_ast
;
239 /* XXX Why not gen_for here? */
240 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
243 return Expression(testlist_ast
, arena
);
246 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
247 stmts
= asdl_seq_new(1, arena
);
250 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
,
252 if (!asdl_seq_GET(stmts
, 0))
254 return Interactive(stmts
, arena
);
259 stmts
= asdl_seq_new(num
, arena
);
263 s
= ast_for_stmt(&c
, n
);
266 asdl_seq_SET(stmts
, 0, s
);
269 /* Only a simple_stmt can contain multiple statements. */
271 for (i
= 0; i
< NCH(n
); i
+= 2) {
272 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
274 s
= ast_for_stmt(&c
, CHILD(n
, i
));
277 asdl_seq_SET(stmts
, i
/ 2, s
);
281 return Interactive(stmts
, arena
);
284 PyErr_Format(PyExc_SystemError
,
285 "invalid node %d for PyAST_FromNode", TYPE(n
));
289 ast_error_finish(filename
);
293 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
297 get_operator(const node
*n
)
323 return (operator_ty
)0;
327 /* Set the context ctx for expr_ty e, recursively traversing e.
329 Only sets context for expr kinds that "can appear in assignment context"
330 (according to ../Parser/Python.asdl). For other expr kinds, it sets
331 an appropriate syntax error and returns false.
335 set_context(expr_ty e
, expr_context_ty ctx
, const node
*n
)
338 /* If a particular expression type can't be used for assign / delete,
339 set expr_name to its name and an error message will be generated.
341 const char* expr_name
= NULL
;
343 /* The ast defines augmented store and load contexts, but the
344 implementation here doesn't actually use them. The code may be
345 a little more complex than necessary as a result. It also means
346 that expressions in an augmented assignment have a Store context.
347 Consider restructuring so that augmented assignment uses
350 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
355 !strcmp(PyString_AS_STRING(e
->v
.Attribute
.attr
), "None")) {
356 return ast_error(n
, "assignment to None");
358 e
->v
.Attribute
.ctx
= ctx
;
361 e
->v
.Subscript
.ctx
= ctx
;
365 !strcmp(PyString_AS_STRING(e
->v
.Name
.id
), "None")) {
366 return ast_error(n
, "assignment to None");
375 if (asdl_seq_LEN(e
->v
.Tuple
.elts
) == 0)
376 return ast_error(n
, "can't assign to ()");
377 e
->v
.Tuple
.ctx
= ctx
;
381 expr_name
= "lambda";
384 expr_name
= "function call";
389 expr_name
= "operator";
391 case GeneratorExp_kind
:
392 expr_name
= "generator expression";
395 expr_name
= "yield expression";
398 expr_name
= "list comprehension";
403 expr_name
= "literal";
406 expr_name
= "comparison";
412 expr_name
= "conditional expression";
415 PyErr_Format(PyExc_SystemError
,
416 "unexpected expression in assignment %d (line %d)",
420 /* Check for error string set by switch */
423 PyOS_snprintf(buf
, sizeof(buf
),
425 ctx
== Store
? "assign to" : "delete",
427 return ast_error(n
, buf
);
430 /* If the LHS is a list or tuple, we need to set the assignment
431 context for all the contained elements.
436 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
437 if (!set_context((expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
445 ast_for_augassign(const node
*n
)
455 if (STR(n
)[1] == '/')
472 if (STR(n
)[1] == '*')
477 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
478 return (operator_ty
)0;
483 ast_for_comp_op(const node
*n
)
485 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
496 case EQEQUAL
: /* == */
505 if (strcmp(STR(n
), "in") == 0)
507 if (strcmp(STR(n
), "is") == 0)
510 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
515 else if (NCH(n
) == 2) {
516 /* handle "not in" and "is not" */
517 switch (TYPE(CHILD(n
, 0))) {
519 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
521 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
524 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
525 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
529 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
535 seq_for_testlist(struct compiling
*c
, const node
*n
)
537 /* testlist: test (',' test)* [','] */
541 assert(TYPE(n
) == testlist
||
542 TYPE(n
) == listmaker
||
543 TYPE(n
) == testlist_gexp
||
544 TYPE(n
) == testlist_safe
||
545 TYPE(n
) == testlist1
);
547 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
551 for (i
= 0; i
< NCH(n
); i
+= 2) {
552 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
554 expression
= ast_for_expr(c
, CHILD(n
, i
));
558 assert(i
/ 2 < seq
->size
);
559 asdl_seq_SET(seq
, i
/ 2, expression
);
565 compiler_complex_args(struct compiling
*c
, const node
*n
)
567 int i
, len
= (NCH(n
) + 1) / 2;
569 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
573 /* fpdef: NAME | '(' fplist ')'
574 fplist: fpdef (',' fpdef)* [',']
577 for (i
= 0; i
< len
; i
++) {
578 const node
*fpdef_node
= CHILD(n
, 2*i
);
582 /* fpdef_node is either a NAME or an fplist */
583 child
= CHILD(fpdef_node
, 0);
584 if (TYPE(child
) == NAME
) {
585 if (!strcmp(STR(child
), "None")) {
586 ast_error(child
, "assignment to None");
589 arg
= Name(NEW_IDENTIFIER(child
), Store
, LINENO(child
),
590 child
->n_col_offset
, c
->c_arena
);
593 assert(TYPE(fpdef_node
) == fpdef
);
594 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
595 child
= CHILD(fpdef_node
, 1);
596 assert(TYPE(child
) == fplist
);
597 /* NCH == 1 means we have (x), we need to elide the extra parens */
598 if (NCH(child
) == 1) {
599 fpdef_node
= CHILD(child
, 0);
600 assert(TYPE(fpdef_node
) == fpdef
);
603 arg
= compiler_complex_args(c
, child
);
605 asdl_seq_SET(args
, i
, arg
);
608 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
609 if (!set_context(result
, Store
, n
))
615 /* Create AST for argument list. */
618 ast_for_arguments(struct compiling
*c
, const node
*n
)
620 /* parameters: '(' [varargslist] ')'
621 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
622 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
624 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
625 asdl_seq
*args
, *defaults
;
626 identifier vararg
= NULL
, kwarg
= NULL
;
629 if (TYPE(n
) == parameters
) {
630 if (NCH(n
) == 2) /* () as argument list */
631 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
636 /* first count the number of normal args & defaults */
637 for (i
= 0; i
< NCH(n
); i
++) {
639 if (TYPE(ch
) == fpdef
)
641 if (TYPE(ch
) == EQUAL
)
644 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
646 return NULL
; /* Don't need to goto error; no objects allocated */
647 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
648 if (!defaults
&& n_defaults
)
649 return NULL
; /* Don't need to goto error; no objects allocated */
651 /* fpdef: NAME | '(' fplist ')'
652 fplist: fpdef (',' fpdef)* [',']
655 j
= 0; /* index for defaults */
656 k
= 0; /* index for args */
662 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
663 anything other than EQUAL or a comma? */
664 /* XXX Should NCH(n) check be made a separate check? */
665 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
666 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
669 assert(defaults
!= NULL
);
670 asdl_seq_SET(defaults
, j
++, expression
);
674 else if (found_default
) {
676 "non-default argument follows default argument");
681 /* def foo((x)): is not complex, special case. */
683 /* We have complex arguments, setup for unpacking. */
684 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
685 if (!asdl_seq_GET(args
, k
-1))
688 /* def foo((x)): setup for checking NAME below. */
689 /* Loop because there can be many parens and tuple
690 unpacking mixed in. */
692 assert(TYPE(ch
) == fpdef
);
696 if (TYPE(CHILD(ch
, 0)) == NAME
) {
698 if (!strcmp(STR(CHILD(ch
, 0)), "None")) {
699 ast_error(CHILD(ch
, 0), "assignment to None");
702 name
= Name(NEW_IDENTIFIER(CHILD(ch
, 0)),
703 Param
, LINENO(ch
), ch
->n_col_offset
,
707 asdl_seq_SET(args
, k
++, name
);
710 i
+= 2; /* the name and the comma */
713 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
714 ast_error(CHILD(n
, i
+1), "assignment to None");
717 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
721 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
722 ast_error(CHILD(n
, i
+1), "assignment to None");
725 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
729 PyErr_Format(PyExc_SystemError
,
730 "unexpected node in varargslist: %d @ %d",
736 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
745 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
749 int lineno
, col_offset
;
755 col_offset
= n
->n_col_offset
;
757 id
= NEW_IDENTIFIER(CHILD(n
, 0));
760 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
764 for (i
= 2; i
< NCH(n
); i
+=2) {
765 id
= NEW_IDENTIFIER(CHILD(n
, i
));
768 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
777 ast_for_decorator(struct compiling
*c
, const node
*n
)
779 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
784 REQ(CHILD(n
, 0), AT
);
785 REQ(RCHILD(n
, -1), NEWLINE
);
787 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
791 if (NCH(n
) == 3) { /* No arguments */
795 else if (NCH(n
) == 5) { /* Call with no arguments */
796 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
797 n
->n_col_offset
, c
->c_arena
);
803 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
813 ast_for_decorators(struct compiling
*c
, const node
*n
)
815 asdl_seq
* decorator_seq
;
820 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
824 for (i
= 0; i
< NCH(n
); i
++) {
825 d
= ast_for_decorator(c
, CHILD(n
, i
));
828 asdl_seq_SET(decorator_seq
, i
, d
);
830 return decorator_seq
;
834 ast_for_funcdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
836 /* funcdef: 'def' NAME parameters ':' suite */
844 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
847 else if (!strcmp(STR(CHILD(n
, name_i
)), "None")) {
848 ast_error(CHILD(n
, name_i
), "assignment to None");
851 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
854 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
858 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
859 n
->n_col_offset
, c
->c_arena
);
863 ast_for_decorated(struct compiling
*c
, const node
*n
)
865 /* decorated: decorators (classdef | funcdef) */
866 stmt_ty thing
= NULL
;
867 asdl_seq
*decorator_seq
= NULL
;
871 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
875 assert(TYPE(CHILD(n
, 1)) == funcdef
||
876 TYPE(CHILD(n
, 1)) == classdef
);
878 if (TYPE(CHILD(n
, 1)) == funcdef
) {
879 thing
= ast_for_funcdef(c
, CHILD(n
, 1), decorator_seq
);
880 } else if (TYPE(CHILD(n
, 1)) == classdef
) {
881 thing
= ast_for_classdef(c
, CHILD(n
, 1), decorator_seq
);
883 /* we count the decorators in when talking about the class' or
884 function's line number */
886 thing
->lineno
= LINENO(n
);
887 thing
->col_offset
= n
->n_col_offset
;
893 ast_for_lambdef(struct compiling
*c
, const node
*n
)
895 /* lambdef: 'lambda' [varargslist] ':' test */
900 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
903 expression
= ast_for_expr(c
, CHILD(n
, 2));
908 args
= ast_for_arguments(c
, CHILD(n
, 1));
911 expression
= ast_for_expr(c
, CHILD(n
, 3));
916 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
920 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
922 /* test: or_test 'if' or_test 'else' test */
923 expr_ty expression
, body
, orelse
;
926 body
= ast_for_expr(c
, CHILD(n
, 0));
929 expression
= ast_for_expr(c
, CHILD(n
, 2));
932 orelse
= ast_for_expr(c
, CHILD(n
, 4));
935 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
939 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
940 so there is only a single version. Possibly for loops can also re-use
944 /* Count the number of 'for' loop in a list comprehension.
946 Helper for ast_for_listcomp().
950 count_list_fors(const node
*n
)
953 node
*ch
= CHILD(n
, 1);
965 if (TYPE(ch
) == list_for
)
967 else if (TYPE(ch
) == list_if
) {
970 goto count_list_iter
;
976 /* Should never be reached */
977 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
981 /* Count the number of 'if' statements in a list comprehension.
983 Helper for ast_for_listcomp().
987 count_list_ifs(const node
*n
)
993 if (TYPE(CHILD(n
, 0)) == list_for
)
1001 goto count_list_iter
;
1005 ast_for_listcomp(struct compiling
*c
, const node
*n
)
1007 /* listmaker: test ( list_for | (',' test)* [','] )
1008 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1009 list_iter: list_for | list_if
1010 list_if: 'if' test [list_iter]
1011 testlist_safe: test [(',' test)+ [',']]
1014 asdl_seq
*listcomps
;
1021 elt
= ast_for_expr(c
, CHILD(n
, 0));
1025 n_fors
= count_list_fors(n
);
1029 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
1034 for (i
= 0; i
< n_fors
; i
++) {
1035 comprehension_ty lc
;
1042 for_ch
= CHILD(ch
, 1);
1043 t
= ast_for_exprlist(c
, for_ch
, Store
);
1046 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
1050 /* Check the # of children rather than the length of t, since
1051 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1053 if (NCH(for_ch
) == 1)
1054 lc
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
, NULL
,
1057 lc
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1059 expression
, NULL
, c
->c_arena
);
1066 expr_ty list_for_expr
;
1069 n_ifs
= count_list_ifs(ch
);
1073 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1077 for (j
= 0; j
< n_ifs
; j
++) {
1082 list_for_expr
= ast_for_expr(c
, CHILD(ch
, 1));
1086 asdl_seq_SET(ifs
, j
, list_for_expr
);
1090 /* on exit, must guarantee that ch is a list_for */
1091 if (TYPE(ch
) == list_iter
)
1095 asdl_seq_SET(listcomps
, i
, lc
);
1098 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1101 /* Count the number of 'for' loops in a generator expression.
1103 Helper for ast_for_genexp().
1107 count_gen_fors(const node
*n
)
1110 node
*ch
= CHILD(n
, 1);
1122 if (TYPE(ch
) == gen_for
)
1124 else if (TYPE(ch
) == gen_if
) {
1127 goto count_gen_iter
;
1133 /* Should never be reached */
1134 PyErr_SetString(PyExc_SystemError
,
1135 "logic error in count_gen_fors");
1139 /* Count the number of 'if' statements in a generator expression.
1141 Helper for ast_for_genexp().
1145 count_gen_ifs(const node
*n
)
1151 if (TYPE(CHILD(n
, 0)) == gen_for
)
1162 /* TODO(jhylton): Combine with list comprehension code? */
1164 ast_for_genexp(struct compiling
*c
, const node
*n
)
1166 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1167 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1173 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1176 elt
= ast_for_expr(c
, CHILD(n
, 0));
1180 n_fors
= count_gen_fors(n
);
1184 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1189 for (i
= 0; i
< n_fors
; i
++) {
1190 comprehension_ty ge
;
1197 for_ch
= CHILD(ch
, 1);
1198 t
= ast_for_exprlist(c
, for_ch
, Store
);
1201 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1205 /* Check the # of children rather than the length of t, since
1206 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1207 if (NCH(for_ch
) == 1)
1208 ge
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
,
1211 ge
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1213 expression
, NULL
, c
->c_arena
);
1223 n_ifs
= count_gen_ifs(ch
);
1227 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1231 for (j
= 0; j
< n_ifs
; j
++) {
1236 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1239 asdl_seq_SET(ifs
, j
, expression
);
1243 /* on exit, must guarantee that ch is a gen_for */
1244 if (TYPE(ch
) == gen_iter
)
1248 asdl_seq_SET(genexps
, i
, ge
);
1251 return GeneratorExp(elt
, genexps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1255 ast_for_atom(struct compiling
*c
, const node
*n
)
1257 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1258 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1260 node
*ch
= CHILD(n
, 0);
1264 /* All names start in Load context, but may later be
1266 return Name(NEW_IDENTIFIER(ch
), Load
, LINENO(n
), n
->n_col_offset
,
1269 PyObject
*str
= parsestrplus(c
, n
);
1271 #ifdef Py_USING_UNICODE
1272 if (PyErr_ExceptionMatches(PyExc_UnicodeError
)){
1273 PyObject
*type
, *value
, *tback
, *errstr
;
1274 PyErr_Fetch(&type
, &value
, &tback
);
1275 errstr
= ((PyUnicodeErrorObject
*)value
)->reason
;
1279 s
= PyString_AsString(errstr
);
1280 PyOS_snprintf(buf
, sizeof(buf
), "(unicode error) %s", s
);
1283 ast_error(n
, "(unicode error) unknown error");
1292 PyArena_AddPyObject(c
->c_arena
, str
);
1293 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1296 PyObject
*pynum
= parsenumber(STR(ch
));
1300 PyArena_AddPyObject(c
->c_arena
, pynum
);
1301 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1303 case LPAR
: /* some parenthesized expressions */
1306 if (TYPE(ch
) == RPAR
)
1307 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1309 if (TYPE(ch
) == yield_expr
)
1310 return ast_for_expr(c
, ch
);
1312 if ((NCH(ch
) > 1) && (TYPE(CHILD(ch
, 1)) == gen_for
))
1313 return ast_for_genexp(c
, ch
);
1315 return ast_for_testlist_gexp(c
, ch
);
1316 case LSQB
: /* list (or list comprehension) */
1319 if (TYPE(ch
) == RSQB
)
1320 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1323 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1324 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1328 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1331 return ast_for_listcomp(c
, ch
);
1333 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1335 asdl_seq
*keys
, *values
;
1338 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1339 keys
= asdl_seq_new(size
, c
->c_arena
);
1343 values
= asdl_seq_new(size
, c
->c_arena
);
1347 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1350 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1354 asdl_seq_SET(keys
, i
/ 4, expression
);
1356 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1360 asdl_seq_SET(values
, i
/ 4, expression
);
1362 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1364 case BACKQUOTE
: { /* repr */
1366 if (Py_Py3kWarningFlag
) {
1367 if (PyErr_WarnExplicit(PyExc_DeprecationWarning
,
1368 "backquote not supported in 3.x; use repr()",
1369 c
->c_filename
, LINENO(n
),
1374 expression
= ast_for_testlist(c
, CHILD(n
, 1));
1378 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1381 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1387 ast_for_slice(struct compiling
*c
, const node
*n
)
1390 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1395 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1399 if (TYPE(ch
) == DOT
)
1400 return Ellipsis(c
->c_arena
);
1402 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1403 /* 'step' variable hold no significance in terms of being used over
1405 step
= ast_for_expr(c
, ch
);
1409 return Index(step
, c
->c_arena
);
1412 if (TYPE(ch
) == test
) {
1413 lower
= ast_for_expr(c
, ch
);
1418 /* If there's an upper bound it's in the second or third position. */
1419 if (TYPE(ch
) == COLON
) {
1421 node
*n2
= CHILD(n
, 1);
1423 if (TYPE(n2
) == test
) {
1424 upper
= ast_for_expr(c
, n2
);
1429 } else if (NCH(n
) > 2) {
1430 node
*n2
= CHILD(n
, 2);
1432 if (TYPE(n2
) == test
) {
1433 upper
= ast_for_expr(c
, n2
);
1439 ch
= CHILD(n
, NCH(n
) - 1);
1440 if (TYPE(ch
) == sliceop
) {
1442 /* No expression, so step is None */
1444 step
= Name(new_identifier("None", c
->c_arena
), Load
,
1445 LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1450 if (TYPE(ch
) == test
) {
1451 step
= ast_for_expr(c
, ch
);
1458 return Slice(lower
, upper
, step
, c
->c_arena
);
1462 ast_for_binop(struct compiling
*c
, const node
*n
)
1464 /* Must account for a sequence of expressions.
1465 How should A op B op C by represented?
1466 BinOp(BinOp(A, op, B), op, C).
1470 expr_ty expr1
, expr2
, result
;
1471 operator_ty newoperator
;
1473 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1477 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1481 newoperator
= get_operator(CHILD(n
, 1));
1485 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1490 nops
= (NCH(n
) - 1) / 2;
1491 for (i
= 1; i
< nops
; i
++) {
1492 expr_ty tmp_result
, tmp
;
1493 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1495 newoperator
= get_operator(next_oper
);
1499 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1503 tmp_result
= BinOp(result
, newoperator
, tmp
,
1504 LINENO(next_oper
), next_oper
->n_col_offset
,
1508 result
= tmp_result
;
1514 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1516 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1517 subscriptlist: subscript (',' subscript)* [',']
1518 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1521 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1523 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1524 n
->n_col_offset
, c
->c_arena
);
1526 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1528 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1529 return Attribute(left_expr
, NEW_IDENTIFIER(CHILD(n
, 1)), Load
,
1530 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1533 REQ(CHILD(n
, 0), LSQB
);
1534 REQ(CHILD(n
, 2), RSQB
);
1537 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1540 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1544 /* The grammar is ambiguous here. The ambiguity is resolved
1545 by treating the sequence as a tuple literal if there are
1552 asdl_seq
*slices
, *elts
;
1553 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1556 for (j
= 0; j
< NCH(n
); j
+= 2) {
1557 slc
= ast_for_slice(c
, CHILD(n
, j
));
1560 if (slc
->kind
!= Index_kind
)
1562 asdl_seq_SET(slices
, j
/ 2, slc
);
1565 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1566 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1568 /* extract Index values and put them in a Tuple */
1569 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1572 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1573 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1574 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1575 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1577 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1580 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1581 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1587 ast_for_factor(struct compiling
*c
, const node
*n
)
1589 node
*pfactor
, *ppower
, *patom
, *pnum
;
1592 /* If the unary - operator is applied to a constant, don't generate
1593 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1594 constant. The peephole optimizer already does something like
1595 this but it doesn't handle the case where the constant is
1596 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1599 if (TYPE(CHILD(n
, 0)) == MINUS
&&
1601 TYPE((pfactor
= CHILD(n
, 1))) == factor
&&
1602 NCH(pfactor
) == 1 &&
1603 TYPE((ppower
= CHILD(pfactor
, 0))) == power
&&
1605 TYPE((patom
= CHILD(ppower
, 0))) == atom
&&
1606 TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1607 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1611 strcpy(s
+ 1, STR(pnum
));
1612 PyObject_FREE(STR(pnum
));
1614 return ast_for_atom(c
, patom
);
1617 expression
= ast_for_expr(c
, CHILD(n
, 1));
1621 switch (TYPE(CHILD(n
, 0))) {
1623 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1626 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1629 return UnaryOp(Invert
, expression
, LINENO(n
),
1630 n
->n_col_offset
, c
->c_arena
);
1632 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1638 ast_for_power(struct compiling
*c
, const node
*n
)
1640 /* power: atom trailer* ('**' factor)*
1645 e
= ast_for_atom(c
, CHILD(n
, 0));
1650 for (i
= 1; i
< NCH(n
); i
++) {
1651 node
*ch
= CHILD(n
, i
);
1652 if (TYPE(ch
) != trailer
)
1654 tmp
= ast_for_trailer(c
, ch
, e
);
1657 tmp
->lineno
= e
->lineno
;
1658 tmp
->col_offset
= e
->col_offset
;
1661 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1662 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1665 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1673 /* Do not name a variable 'expr'! Will cause a compile error.
1677 ast_for_expr(struct compiling
*c
, const node
*n
)
1679 /* handle the full range of simple expressions
1680 test: or_test ['if' or_test 'else' test] | lambdef
1681 or_test: and_test ('or' and_test)*
1682 and_test: not_test ('and' not_test)*
1683 not_test: 'not' not_test | comparison
1684 comparison: expr (comp_op expr)*
1685 expr: xor_expr ('|' xor_expr)*
1686 xor_expr: and_expr ('^' and_expr)*
1687 and_expr: shift_expr ('&' shift_expr)*
1688 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1689 arith_expr: term (('+'|'-') term)*
1690 term: factor (('*'|'/'|'%'|'//') factor)*
1691 factor: ('+'|'-'|'~') factor | power
1692 power: atom trailer* ('**' factor)*
1694 As well as modified versions that exist for backward compatibility,
1695 to explicitly allow:
1696 [ x for x in lambda: 0, lambda: 1 ]
1697 (which would be ambiguous without these extra rules)
1699 old_test: or_test | old_lambdef
1700 old_lambdef: 'lambda' [vararglist] ':' old_test
1711 if (TYPE(CHILD(n
, 0)) == lambdef
||
1712 TYPE(CHILD(n
, 0)) == old_lambdef
)
1713 return ast_for_lambdef(c
, CHILD(n
, 0));
1714 else if (NCH(n
) > 1)
1715 return ast_for_ifexpr(c
, n
);
1723 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1726 for (i
= 0; i
< NCH(n
); i
+= 2) {
1727 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1730 asdl_seq_SET(seq
, i
/ 2, e
);
1732 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1733 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1735 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1736 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1743 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1747 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1759 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1762 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1766 for (i
= 1; i
< NCH(n
); i
+= 2) {
1767 cmpop_ty newoperator
;
1769 newoperator
= ast_for_comp_op(CHILD(n
, i
));
1774 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1779 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1780 asdl_seq_SET(cmps
, i
/ 2, expression
);
1782 expression
= ast_for_expr(c
, CHILD(n
, 0));
1787 return Compare(expression
, ops
, cmps
, LINENO(n
),
1788 n
->n_col_offset
, c
->c_arena
);
1792 /* The next five cases all handle BinOps. The main body of code
1793 is the same in each case, but the switch turned inside out to
1794 reuse the code for each type of operator.
1806 return ast_for_binop(c
, n
);
1810 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1814 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1821 return ast_for_factor(c
, n
);
1823 return ast_for_power(c
, n
);
1825 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1828 /* should never get here unless if error is set */
1833 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1836 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1838 argument: [test '='] test [gen_for] # Really [keyword '='] test
1841 int i
, nargs
, nkeywords
, ngens
;
1844 expr_ty vararg
= NULL
, kwarg
= NULL
;
1851 for (i
= 0; i
< NCH(n
); i
++) {
1852 node
*ch
= CHILD(n
, i
);
1853 if (TYPE(ch
) == argument
) {
1856 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1862 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1863 ast_error(n
, "Generator expression must be parenthesized "
1864 "if not sole argument");
1868 if (nargs
+ nkeywords
+ ngens
> 255) {
1869 ast_error(n
, "more than 255 arguments");
1873 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1876 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1881 for (i
= 0; i
< NCH(n
); i
++) {
1882 node
*ch
= CHILD(n
, i
);
1883 if (TYPE(ch
) == argument
) {
1887 ast_error(CHILD(ch
, 0),
1888 "non-keyword arg after keyword arg");
1891 e
= ast_for_expr(c
, CHILD(ch
, 0));
1894 asdl_seq_SET(args
, nargs
++, e
);
1896 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1897 e
= ast_for_genexp(c
, ch
);
1900 asdl_seq_SET(args
, nargs
++, e
);
1906 /* CHILD(ch, 0) is test, but must be an identifier? */
1907 e
= ast_for_expr(c
, CHILD(ch
, 0));
1910 /* f(lambda x: x[0] = 3) ends up getting parsed with
1911 * LHS test = lambda x: x[0], and RHS test = 3.
1912 * SF bug 132313 points out that complaining about a keyword
1913 * then is very confusing.
1915 if (e
->kind
== Lambda_kind
) {
1916 ast_error(CHILD(ch
, 0),
1917 "lambda cannot contain assignment");
1919 } else if (e
->kind
!= Name_kind
) {
1920 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1924 if (!strcmp(PyString_AS_STRING(key
), "None")) {
1925 ast_error(CHILD(ch
, 0), "assignment to None");
1928 e
= ast_for_expr(c
, CHILD(ch
, 2));
1931 kw
= keyword(key
, e
, c
->c_arena
);
1934 asdl_seq_SET(keywords
, nkeywords
++, kw
);
1937 else if (TYPE(ch
) == STAR
) {
1938 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
1943 else if (TYPE(ch
) == DOUBLESTAR
) {
1944 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
1951 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
,
1952 func
->col_offset
, c
->c_arena
);
1956 ast_for_testlist(struct compiling
*c
, const node
* n
)
1958 /* testlist_gexp: test (',' test)* [','] */
1959 /* testlist: test (',' test)* [','] */
1960 /* testlist_safe: test (',' test)+ [','] */
1961 /* testlist1: test (',' test)* */
1963 if (TYPE(n
) == testlist_gexp
) {
1965 assert(TYPE(CHILD(n
, 1)) != gen_for
);
1968 assert(TYPE(n
) == testlist
||
1969 TYPE(n
) == testlist_safe
||
1970 TYPE(n
) == testlist1
);
1973 return ast_for_expr(c
, CHILD(n
, 0));
1975 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
1978 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1983 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
1985 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1986 /* argument: test [ gen_for ] */
1987 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
1988 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
1989 return ast_for_genexp(c
, n
);
1990 return ast_for_testlist(c
, n
);
1993 /* like ast_for_testlist() but returns a sequence */
1995 ast_for_class_bases(struct compiling
*c
, const node
* n
)
1997 /* testlist: test (',' test)* [','] */
2002 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
2005 base
= ast_for_expr(c
, CHILD(n
, 0));
2008 asdl_seq_SET(bases
, 0, base
);
2012 return seq_for_testlist(c
, n
);
2016 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
2019 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2020 | ('=' (yield_expr|testlist))*)
2021 testlist: test (',' test)* [',']
2022 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2023 | '<<=' | '>>=' | '**=' | '//='
2024 test: ... here starts the operator precendence dance
2028 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
2032 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2034 else if (TYPE(CHILD(n
, 1)) == augassign
) {
2035 expr_ty expr1
, expr2
;
2036 operator_ty newoperator
;
2037 node
*ch
= CHILD(n
, 0);
2039 expr1
= ast_for_testlist(c
, ch
);
2042 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2043 switch (expr1
->kind
) {
2044 case GeneratorExp_kind
:
2045 ast_error(ch
, "augmented assignment to generator "
2046 "expression not possible");
2049 ast_error(ch
, "augmented assignment to yield "
2050 "expression not possible");
2053 const char *var_name
= PyString_AS_STRING(expr1
->v
.Name
.id
);
2054 if (var_name
[0] == 'N' && !strcmp(var_name
, "None")) {
2055 ast_error(ch
, "assignment to None");
2060 case Attribute_kind
:
2061 case Subscript_kind
:
2064 ast_error(ch
, "illegal expression for augmented "
2068 if(!set_context(expr1
, Store
, ch
))
2072 if (TYPE(ch
) == testlist
)
2073 expr2
= ast_for_testlist(c
, ch
);
2075 expr2
= ast_for_expr(c
, ch
);
2079 newoperator
= ast_for_augassign(CHILD(n
, 1));
2083 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
2092 /* a normal assignment */
2093 REQ(CHILD(n
, 1), EQUAL
);
2094 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2097 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2099 node
*ch
= CHILD(n
, i
);
2100 if (TYPE(ch
) == yield_expr
) {
2101 ast_error(ch
, "assignment to yield expression not possible");
2104 e
= ast_for_testlist(c
, ch
);
2106 /* set context to assign */
2110 if (!set_context(e
, Store
, CHILD(n
, i
)))
2113 asdl_seq_SET(targets
, i
/ 2, e
);
2115 value
= CHILD(n
, NCH(n
) - 1);
2116 if (TYPE(value
) == testlist
)
2117 expression
= ast_for_testlist(c
, value
);
2119 expression
= ast_for_expr(c
, value
);
2122 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
,
2128 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
2130 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2131 | '>>' test [ (',' test)+ [','] ] )
2133 expr_ty dest
= NULL
, expression
;
2136 int i
, j
, start
= 1;
2139 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2140 dest
= ast_for_expr(c
, CHILD(n
, 2));
2145 seq
= asdl_seq_new((NCH(n
) + 1 - start
) / 2, c
->c_arena
);
2148 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2149 expression
= ast_for_expr(c
, CHILD(n
, i
));
2152 asdl_seq_SET(seq
, j
, expression
);
2154 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2155 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2159 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2167 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2170 for (i
= 0; i
< NCH(n
); i
+= 2) {
2171 e
= ast_for_expr(c
, CHILD(n
, i
));
2174 asdl_seq_SET(seq
, i
/ 2, e
);
2175 if (context
&& !set_context(e
, context
, CHILD(n
, i
)))
2182 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2184 asdl_seq
*expr_list
;
2186 /* del_stmt: 'del' exprlist */
2189 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2192 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2196 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2199 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2202 continue_stmt: 'continue'
2203 return_stmt: 'return' [testlist]
2204 yield_stmt: yield_expr
2205 yield_expr: 'yield' testlist
2206 raise_stmt: 'raise' [test [',' test [',' test]]]
2214 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2216 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2217 case yield_stmt
: { /* will reduce to yield_expr */
2218 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2221 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2225 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2227 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2230 return Return(expression
, LINENO(n
), n
->n_col_offset
,
2235 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
,
2237 else if (NCH(ch
) == 2) {
2238 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2241 return Raise(expression
, NULL
, NULL
, LINENO(n
),
2242 n
->n_col_offset
, c
->c_arena
);
2244 else if (NCH(ch
) == 4) {
2245 expr_ty expr1
, expr2
;
2247 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2250 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2254 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
,
2257 else if (NCH(ch
) == 6) {
2258 expr_ty expr1
, expr2
, expr3
;
2260 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2263 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2266 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2270 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
,
2274 PyErr_Format(PyExc_SystemError
,
2275 "unexpected flow_stmt: %d", TYPE(ch
));
2279 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2284 alias_for_import_name(struct compiling
*c
, const node
*n
)
2287 import_as_name: NAME ['as' NAME]
2288 dotted_as_name: dotted_name ['as' NAME]
2289 dotted_name: NAME ('.' NAME)*
2295 case import_as_name
:
2298 str
= NEW_IDENTIFIER(CHILD(n
, 2));
2300 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), str
, c
->c_arena
);
2301 case dotted_as_name
:
2307 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0));
2311 a
->asname
= NEW_IDENTIFIER(CHILD(n
, 2));
2317 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), NULL
, c
->c_arena
);
2319 /* Create a string of the form "a.b.c" */
2325 for (i
= 0; i
< NCH(n
); i
+= 2)
2326 /* length of string plus one for the dot */
2327 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2328 len
--; /* the last name doesn't have a dot */
2329 str
= PyString_FromStringAndSize(NULL
, len
);
2332 s
= PyString_AS_STRING(str
);
2335 for (i
= 0; i
< NCH(n
); i
+= 2) {
2336 char *sch
= STR(CHILD(n
, i
));
2337 strcpy(s
, STR(CHILD(n
, i
)));
2343 PyString_InternInPlace(&str
);
2344 PyArena_AddPyObject(c
->c_arena
, str
);
2345 return alias(str
, NULL
, c
->c_arena
);
2349 str
= PyString_InternFromString("*");
2350 PyArena_AddPyObject(c
->c_arena
, str
);
2351 return alias(str
, NULL
, c
->c_arena
);
2353 PyErr_Format(PyExc_SystemError
,
2354 "unexpected import name: %d", TYPE(n
));
2358 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2363 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2366 import_stmt: import_name | import_from
2367 import_name: 'import' dotted_as_names
2368 import_from: 'from' ('.'* dotted_name | '.') 'import'
2369 ('*' | '(' import_as_names ')' | import_as_names)
2376 REQ(n
, import_stmt
);
2378 col_offset
= n
->n_col_offset
;
2380 if (TYPE(n
) == import_name
) {
2382 REQ(n
, dotted_as_names
);
2383 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2386 for (i
= 0; i
< NCH(n
); i
+= 2) {
2387 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2390 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2392 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2394 else if (TYPE(n
) == import_from
) {
2397 alias_ty mod
= NULL
;
2400 /* Count the number of dots (for relative imports) and check for the
2401 optional module name */
2402 for (idx
= 1; idx
< NCH(n
); idx
++) {
2403 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2404 mod
= alias_for_import_name(c
, CHILD(n
, idx
));
2407 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2412 idx
++; /* skip over the 'import' keyword */
2413 switch (TYPE(CHILD(n
, idx
))) {
2415 /* from ... import * */
2420 /* from ... import (x, y, z) */
2421 n
= CHILD(n
, idx
+ 1);
2422 n_children
= NCH(n
);
2424 case import_as_names
:
2425 /* from ... import x, y, z */
2427 n_children
= NCH(n
);
2428 if (n_children
% 2 == 0) {
2429 ast_error(n
, "trailing comma not allowed without"
2430 " surrounding parentheses");
2435 ast_error(n
, "Unexpected node-type in from-import");
2439 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2443 /* handle "from ... import *" special b/c there's no children */
2444 if (TYPE(n
) == STAR
) {
2445 alias_ty import_alias
= alias_for_import_name(c
, n
);
2448 asdl_seq_SET(aliases
, 0, import_alias
);
2451 for (i
= 0; i
< NCH(n
); i
+= 2) {
2452 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2455 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2459 modname
= mod
->name
;
2461 modname
= new_identifier("", c
->c_arena
);
2462 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2465 PyErr_Format(PyExc_SystemError
,
2466 "unknown import statement: starts with command '%s'",
2472 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2474 /* global_stmt: 'global' NAME (',' NAME)* */
2479 REQ(n
, global_stmt
);
2480 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2483 for (i
= 1; i
< NCH(n
); i
+= 2) {
2484 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2487 asdl_seq_SET(s
, i
/ 2, name
);
2489 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2493 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2495 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2496 int n_children
= NCH(n
);
2497 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2498 PyErr_Format(PyExc_SystemError
,
2499 "poorly formed 'exec' statement: %d parts to statement",
2504 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2506 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2509 if (n_children
>= 4) {
2510 globals
= ast_for_expr(c
, CHILD(n
, 3));
2514 if (n_children
== 6) {
2515 locals
= ast_for_expr(c
, CHILD(n
, 5));
2520 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
,
2525 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2527 /* assert_stmt: 'assert' test [',' test] */
2528 REQ(n
, assert_stmt
);
2530 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2533 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
,
2536 else if (NCH(n
) == 4) {
2537 expr_ty expr1
, expr2
;
2539 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2542 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2546 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2548 PyErr_Format(PyExc_SystemError
,
2549 "improper number of parts to 'assert' statement: %d",
2555 ast_for_suite(struct compiling
*c
, const node
*n
)
2557 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2560 int i
, total
, num
, end
, pos
= 0;
2565 total
= num_stmts(n
);
2566 seq
= asdl_seq_new(total
, c
->c_arena
);
2569 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2571 /* simple_stmt always ends with a NEWLINE,
2572 and may have a trailing SEMI
2575 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2577 /* loop by 2 to skip semi-colons */
2578 for (i
= 0; i
< end
; i
+= 2) {
2580 s
= ast_for_stmt(c
, ch
);
2583 asdl_seq_SET(seq
, pos
++, s
);
2587 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2590 num
= num_stmts(ch
);
2592 /* small_stmt or compound_stmt with only one child */
2593 s
= ast_for_stmt(c
, ch
);
2596 asdl_seq_SET(seq
, pos
++, s
);
2601 REQ(ch
, simple_stmt
);
2602 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2603 /* statement terminates with a semi-colon ';' */
2604 if (NCH(CHILD(ch
, j
)) == 0) {
2605 assert((j
+ 1) == NCH(ch
));
2608 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2611 asdl_seq_SET(seq
, pos
++, s
);
2616 assert(pos
== seq
->size
);
2621 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2623 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2632 asdl_seq
*suite_seq
;
2634 expression
= ast_for_expr(c
, CHILD(n
, 1));
2637 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2641 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2645 s
= STR(CHILD(n
, 4));
2646 /* s[2], the third character in the string, will be
2652 asdl_seq
*seq1
, *seq2
;
2654 expression
= ast_for_expr(c
, CHILD(n
, 1));
2657 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2660 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2664 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2667 else if (s
[2] == 'i') {
2668 int i
, n_elif
, has_else
= 0;
2670 asdl_seq
*suite_seq
;
2671 asdl_seq
*orelse
= NULL
;
2672 n_elif
= NCH(n
) - 4;
2673 /* must reference the child n_elif+1 since 'else' token is third,
2674 not fourth, child from the end. */
2675 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2676 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2683 asdl_seq
*suite_seq2
;
2685 orelse
= asdl_seq_new(1, c
->c_arena
);
2688 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2691 suite_seq
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2694 suite_seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2698 asdl_seq_SET(orelse
, 0,
2699 If(expression
, suite_seq
, suite_seq2
,
2700 LINENO(CHILD(n
, NCH(n
) - 6)),
2701 CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2703 /* the just-created orelse handled the last elif */
2707 for (i
= 0; i
< n_elif
; i
++) {
2708 int off
= 5 + (n_elif
- i
- 1) * 4;
2709 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2712 expression
= ast_for_expr(c
, CHILD(n
, off
));
2715 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2719 asdl_seq_SET(newobj
, 0,
2720 If(expression
, suite_seq
, orelse
,
2721 LINENO(CHILD(n
, off
)),
2722 CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2725 expression
= ast_for_expr(c
, CHILD(n
, 1));
2728 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2731 return If(expression
, suite_seq
, orelse
,
2732 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2735 PyErr_Format(PyExc_SystemError
,
2736 "unexpected token in 'if' statement: %s", s
);
2741 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2743 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2748 asdl_seq
*suite_seq
;
2750 expression
= ast_for_expr(c
, CHILD(n
, 1));
2753 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2756 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2759 else if (NCH(n
) == 7) {
2761 asdl_seq
*seq1
, *seq2
;
2763 expression
= ast_for_expr(c
, CHILD(n
, 1));
2766 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2769 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2773 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2777 PyErr_Format(PyExc_SystemError
,
2778 "wrong number of tokens for 'while' statement: %d",
2784 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2786 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2789 const node
*node_target
;
2790 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2794 seq
= ast_for_suite(c
, CHILD(n
, 8));
2799 node_target
= CHILD(n
, 1);
2800 _target
= ast_for_exprlist(c
, node_target
, Store
);
2803 /* Check the # of children rather than the length of _target, since
2804 for x, in ... has 1 element in _target, but still requires a Tuple. */
2805 if (NCH(node_target
) == 1)
2806 target
= (expr_ty
)asdl_seq_GET(_target
, 0);
2808 target
= Tuple(_target
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2810 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2813 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2817 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2821 static excepthandler_ty
2822 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2824 /* except_clause: 'except' [test [(',' | 'as') test]] */
2825 REQ(exc
, except_clause
);
2828 if (NCH(exc
) == 1) {
2829 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2833 return ExceptHandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
2834 exc
->n_col_offset
, c
->c_arena
);
2836 else if (NCH(exc
) == 2) {
2838 asdl_seq
*suite_seq
;
2840 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2843 suite_seq
= ast_for_suite(c
, body
);
2847 return ExceptHandler(expression
, NULL
, suite_seq
, LINENO(exc
),
2848 exc
->n_col_offset
, c
->c_arena
);
2850 else if (NCH(exc
) == 4) {
2851 asdl_seq
*suite_seq
;
2853 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2856 if (!set_context(e
, Store
, CHILD(exc
, 3)))
2858 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2861 suite_seq
= ast_for_suite(c
, body
);
2865 return ExceptHandler(expression
, e
, suite_seq
, LINENO(exc
),
2866 exc
->n_col_offset
, c
->c_arena
);
2869 PyErr_Format(PyExc_SystemError
,
2870 "wrong number of children for 'except' clause: %d",
2876 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2878 const int nch
= NCH(n
);
2879 int n_except
= (nch
- 3)/3;
2880 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2884 body
= ast_for_suite(c
, CHILD(n
, 2));
2888 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2889 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2890 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2891 /* we can assume it's an "else",
2892 because nch >= 9 for try-else-finally and
2893 it would otherwise have a type of except_clause */
2894 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2900 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2901 if (finally
== NULL
)
2906 /* we can assume it's an "else",
2907 otherwise it would have a type of except_clause */
2908 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2914 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2915 ast_error(n
, "malformed 'try' statement");
2922 /* process except statements to create a try ... except */
2923 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2924 if (handlers
== NULL
)
2927 for (i
= 0; i
< n_except
; i
++) {
2928 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
2929 CHILD(n
, 5 + i
* 3));
2932 asdl_seq_SET(handlers
, i
, e
);
2935 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
2936 n
->n_col_offset
, c
->c_arena
);
2940 /* if a 'finally' is present too, we nest the TryExcept within a
2941 TryFinally to emulate try ... except ... finally */
2942 body
= asdl_seq_new(1, c
->c_arena
);
2945 asdl_seq_SET(body
, 0, except_st
);
2948 /* must be a try ... finally (except clauses are in body, if any exist) */
2949 assert(finally
!= NULL
);
2950 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2954 ast_for_with_var(struct compiling
*c
, const node
*n
)
2957 return ast_for_expr(c
, CHILD(n
, 1));
2960 /* with_stmt: 'with' test [ with_var ] ':' suite */
2962 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
2964 expr_ty context_expr
, optional_vars
= NULL
;
2965 int suite_index
= 3; /* skip 'with', test, and ':' */
2966 asdl_seq
*suite_seq
;
2968 assert(TYPE(n
) == with_stmt
);
2969 context_expr
= ast_for_expr(c
, CHILD(n
, 1));
2972 if (TYPE(CHILD(n
, 2)) == with_var
) {
2973 optional_vars
= ast_for_with_var(c
, CHILD(n
, 2));
2975 if (!optional_vars
) {
2978 if (!set_context(optional_vars
, Store
, n
)) {
2984 suite_seq
= ast_for_suite(c
, CHILD(n
, suite_index
));
2988 return With(context_expr
, optional_vars
, suite_seq
, LINENO(n
),
2989 n
->n_col_offset
, c
->c_arena
);
2993 ast_for_classdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
2995 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2996 asdl_seq
*bases
, *s
;
3000 if (!strcmp(STR(CHILD(n
, 1)), "None")) {
3001 ast_error(n
, "assignment to None");
3006 s
= ast_for_suite(c
, CHILD(n
, 3));
3009 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, decorator_seq
,
3010 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3012 /* check for empty base list */
3013 if (TYPE(CHILD(n
,3)) == RPAR
) {
3014 s
= ast_for_suite(c
, CHILD(n
,5));
3017 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, decorator_seq
,
3018 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3021 /* else handle the base class list */
3022 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
3026 s
= ast_for_suite(c
, CHILD(n
, 6));
3029 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), bases
, s
, decorator_seq
,
3030 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3034 ast_for_stmt(struct compiling
*c
, const node
*n
)
3036 if (TYPE(n
) == stmt
) {
3037 assert(NCH(n
) == 1);
3040 if (TYPE(n
) == simple_stmt
) {
3041 assert(num_stmts(n
) == 1);
3044 if (TYPE(n
) == small_stmt
) {
3047 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3048 | flow_stmt | import_stmt | global_stmt | exec_stmt
3053 return ast_for_expr_stmt(c
, n
);
3055 return ast_for_print_stmt(c
, n
);
3057 return ast_for_del_stmt(c
, n
);
3059 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3061 return ast_for_flow_stmt(c
, n
);
3063 return ast_for_import_stmt(c
, n
);
3065 return ast_for_global_stmt(c
, n
);
3067 return ast_for_exec_stmt(c
, n
);
3069 return ast_for_assert_stmt(c
, n
);
3071 PyErr_Format(PyExc_SystemError
,
3072 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3078 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3079 | funcdef | classdef | decorated
3081 node
*ch
= CHILD(n
, 0);
3082 REQ(n
, compound_stmt
);
3085 return ast_for_if_stmt(c
, ch
);
3087 return ast_for_while_stmt(c
, ch
);
3089 return ast_for_for_stmt(c
, ch
);
3091 return ast_for_try_stmt(c
, ch
);
3093 return ast_for_with_stmt(c
, ch
);
3095 return ast_for_funcdef(c
, ch
, NULL
);
3097 return ast_for_classdef(c
, ch
, NULL
);
3099 return ast_for_decorated(c
, ch
);
3101 PyErr_Format(PyExc_SystemError
,
3102 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3110 parsenumber(const char *s
)
3115 #ifndef WITHOUT_COMPLEX
3121 end
= s
+ strlen(s
) - 1;
3122 #ifndef WITHOUT_COMPLEX
3123 imflag
= *end
== 'j' || *end
== 'J';
3125 if (*end
== 'l' || *end
== 'L')
3126 return PyLong_FromString((char *)s
, (char **)0, 0);
3128 x
= (long) PyOS_strtoul((char *)s
, (char **)&end
, 0);
3129 if (x
< 0 && errno
== 0) {
3130 return PyLong_FromString((char *)s
,
3136 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3139 return PyLong_FromString((char *)s
, (char **)0, 0);
3140 return PyInt_FromLong(x
);
3142 /* XXX Huge floats may silently fail */
3143 #ifndef WITHOUT_COMPLEX
3146 PyFPE_START_PROTECT("atof", return 0)
3147 c
.imag
= PyOS_ascii_atof(s
);
3148 PyFPE_END_PROTECT(c
)
3149 return PyComplex_FromCComplex(c
);
3154 PyFPE_START_PROTECT("atof", return 0)
3155 dx
= PyOS_ascii_atof(s
);
3156 PyFPE_END_PROTECT(dx
)
3157 return PyFloat_FromDouble(dx
);
3162 decode_utf8(const char **sPtr
, const char *end
, char* encoding
)
3164 #ifndef Py_USING_UNICODE
3165 Py_FatalError("decode_utf8 should not be called in this build.");
3170 t
= s
= (char *)*sPtr
;
3171 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3172 while (s
< end
&& (*s
& 0x80)) s
++;
3174 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3177 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3183 #ifdef Py_USING_UNICODE
3185 decode_unicode(const char *s
, size_t len
, int rawmode
, const char *encoding
)
3191 if (encoding
== NULL
) {
3194 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
3198 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3199 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
3202 p
= buf
= PyString_AsString(u
);
3212 if (*s
& 0x80) { /* XXX inefficient */
3216 w
= decode_utf8(&s
, end
, "utf-16-be");
3221 r
= PyString_AsString(w
);
3222 rn
= PyString_Size(w
);
3223 assert(rn
% 2 == 0);
3224 for (i
= 0; i
< rn
; i
+= 2) {
3225 sprintf(p
, "\\u%02x%02x",
3239 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3241 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3247 /* s is a Python string literal, including the bracketing quote characters,
3248 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3249 * parsestr parses it, and returns the decoded Python string object.
3252 parsestr(struct compiling
*c
, const char *s
)
3255 int quote
= Py_CHARMASK(*s
);
3258 int unicode
= c
->c_future_unicode
;
3260 if (isalpha(quote
) || quote
== '_') {
3261 if (quote
== 'u' || quote
== 'U') {
3265 if (quote
== 'b' || quote
== 'B') {
3269 if (quote
== 'r' || quote
== 'R') {
3274 if (quote
!= '\'' && quote
!= '\"') {
3275 PyErr_BadInternalCall();
3280 if (len
> INT_MAX
) {
3281 PyErr_SetString(PyExc_OverflowError
,
3282 "string to parse is too long");
3285 if (s
[--len
] != quote
) {
3286 PyErr_BadInternalCall();
3289 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3292 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3293 PyErr_BadInternalCall();
3297 #ifdef Py_USING_UNICODE
3298 if (unicode
|| Py_UnicodeFlag
) {
3299 return decode_unicode(s
, len
, rawmode
, c
->c_encoding
);
3302 need_encoding
= (c
->c_encoding
!= NULL
&&
3303 strcmp(c
->c_encoding
, "utf-8") != 0 &&
3304 strcmp(c
->c_encoding
, "iso-8859-1") != 0);
3305 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3306 if (need_encoding
) {
3307 #ifndef Py_USING_UNICODE
3308 /* This should not happen - we never see any other
3311 "cannot deal with encodings in this build.");
3313 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3316 v
= PyUnicode_AsEncodedString(u
, c
->c_encoding
, NULL
);
3321 return PyString_FromStringAndSize(s
, len
);
3325 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3326 need_encoding
? c
->c_encoding
: NULL
);
3329 /* Build a Python string object out of a STRING atom. This takes care of
3330 * compile-time literal catenation, calling parsestr() on each piece, and
3331 * pasting the intermediate results together.
3334 parsestrplus(struct compiling
*c
, const node
*n
)
3338 REQ(CHILD(n
, 0), STRING
);
3339 if ((v
= parsestr(c
, STR(CHILD(n
, 0)))) != NULL
) {
3340 /* String literal concatenation */
3341 for (i
= 1; i
< NCH(n
); i
++) {
3343 s
= parsestr(c
, STR(CHILD(n
, i
)));
3346 if (PyString_Check(v
) && PyString_Check(s
)) {
3347 PyString_ConcatAndDel(&v
, s
);
3351 #ifdef Py_USING_UNICODE
3353 PyObject
*temp
= PyUnicode_Concat(v
, s
);