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(struct compiling
*, 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
);
51 PyArena_AddPyObject(arena
, id
);
55 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
57 /* This routine provides an invalid object for the syntax error.
58 The outermost routine must unpack this error and create the
59 proper object. We do this so that we don't have to pass
60 the filename to everything function.
62 XXX Maybe we should just pass the filename...
66 ast_error(const node
*n
, const char *errstr
)
68 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
71 PyErr_SetObject(PyExc_SyntaxError
, u
);
77 ast_error_finish(const char *filename
)
79 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
82 assert(PyErr_Occurred());
83 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
86 PyErr_Fetch(&type
, &value
, &tback
);
87 errstr
= PyTuple_GetItem(value
, 0);
91 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
98 loc
= PyErr_ProgramText(filename
, lineno
);
103 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
109 value
= PyTuple_Pack(2, errstr
, tmp
);
114 PyErr_Restore(type
, value
, tback
);
118 ast_warn(struct compiling
*c
, const node
*n
, char *msg
)
120 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, c
->c_filename
, LINENO(n
),
122 /* if -Werr, change it to a SyntaxError */
123 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning
))
131 forbidden_check(struct compiling
*c
, const node
*n
, const char *x
)
133 if (!strcmp(x
, "None"))
134 return ast_error(n
, "cannot assign to None");
135 if (!strcmp(x
, "__debug__"))
136 return ast_error(n
, "cannot assign to __debug__");
137 if (Py_Py3kWarningFlag
) {
138 if (!(strcmp(x
, "True") && strcmp(x
, "False")) &&
139 !ast_warn(c
, n
, "assignment to True or False is forbidden in 3.x"))
141 if (!strcmp(x
, "nonlocal") &&
142 !ast_warn(c
, n
, "nonlocal is a keyword in 3.x"))
148 /* num_stmts() returns number of contained statements.
150 Use this routine to determine how big a sequence is needed for
151 the statements in a parse tree. Its raison d'etre is this bit of
154 stmt: simple_stmt | compound_stmt
155 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
157 A simple_stmt can contain multiple small_stmt elements joined
158 by semicolons. If the arg is a simple_stmt, the number of
159 small_stmt elements is returned.
163 num_stmts(const node
*n
)
170 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
173 return num_stmts(CHILD(n
, 0));
176 for (i
= 0; i
< NCH(n
); i
++) {
178 if (TYPE(ch
) == stmt
)
183 return num_stmts(CHILD(n
, 0));
187 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
190 return num_stmts(CHILD(n
, 0));
193 for (i
= 2; i
< (NCH(n
) - 1); i
++)
194 l
+= num_stmts(CHILD(n
, i
));
200 sprintf(buf
, "Non-statement found: %d %d",
209 /* Transform the CST rooted at node * to the appropriate AST
213 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
217 asdl_seq
*stmts
= NULL
;
222 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
223 c
.c_encoding
= "utf-8";
224 if (TYPE(n
) == encoding_decl
) {
225 ast_error(n
, "encoding declaration in Unicode string");
228 } else if (TYPE(n
) == encoding_decl
) {
229 c
.c_encoding
= STR(n
);
234 c
.c_future_unicode
= flags
&& flags
->cf_flags
& CO_FUTURE_UNICODE_LITERALS
;
236 c
.c_filename
= filename
;
241 stmts
= asdl_seq_new(num_stmts(n
), arena
);
244 for (i
= 0; i
< NCH(n
) - 1; i
++) {
246 if (TYPE(ch
) == NEWLINE
)
251 s
= ast_for_stmt(&c
, ch
);
254 asdl_seq_SET(stmts
, k
++, s
);
258 REQ(ch
, simple_stmt
);
259 for (j
= 0; j
< num
; j
++) {
260 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
263 asdl_seq_SET(stmts
, k
++, s
);
267 return Module(stmts
, arena
);
269 expr_ty testlist_ast
;
271 /* XXX Why not gen_for here? */
272 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
275 return Expression(testlist_ast
, arena
);
278 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
279 stmts
= asdl_seq_new(1, arena
);
282 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
,
284 if (!asdl_seq_GET(stmts
, 0))
286 return Interactive(stmts
, arena
);
291 stmts
= asdl_seq_new(num
, arena
);
295 s
= ast_for_stmt(&c
, n
);
298 asdl_seq_SET(stmts
, 0, s
);
301 /* Only a simple_stmt can contain multiple statements. */
303 for (i
= 0; i
< NCH(n
); i
+= 2) {
304 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
306 s
= ast_for_stmt(&c
, CHILD(n
, i
));
309 asdl_seq_SET(stmts
, i
/ 2, s
);
313 return Interactive(stmts
, arena
);
316 PyErr_Format(PyExc_SystemError
,
317 "invalid node %d for PyAST_FromNode", TYPE(n
));
321 ast_error_finish(filename
);
325 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
329 get_operator(const node
*n
)
355 return (operator_ty
)0;
359 /* Set the context ctx for expr_ty e, recursively traversing e.
361 Only sets context for expr kinds that "can appear in assignment context"
362 (according to ../Parser/Python.asdl). For other expr kinds, it sets
363 an appropriate syntax error and returns false.
367 set_context(struct compiling
*c
, expr_ty e
, expr_context_ty ctx
, const node
*n
)
370 /* If a particular expression type can't be used for assign / delete,
371 set expr_name to its name and an error message will be generated.
373 const char* expr_name
= NULL
;
375 /* The ast defines augmented store and load contexts, but the
376 implementation here doesn't actually use them. The code may be
377 a little more complex than necessary as a result. It also means
378 that expressions in an augmented assignment have a Store context.
379 Consider restructuring so that augmented assignment uses
382 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
386 if (ctx
== Store
&& !forbidden_check(c
, n
,
387 PyBytes_AS_STRING(e
->v
.Attribute
.attr
)))
389 e
->v
.Attribute
.ctx
= ctx
;
392 e
->v
.Subscript
.ctx
= ctx
;
395 if (ctx
== Store
&& !forbidden_check(c
, n
,
396 PyBytes_AS_STRING(e
->v
.Name
.id
)))
405 if (asdl_seq_LEN(e
->v
.Tuple
.elts
)) {
406 e
->v
.Tuple
.ctx
= ctx
;
414 expr_name
= "lambda";
417 expr_name
= "function call";
422 expr_name
= "operator";
424 case GeneratorExp_kind
:
425 expr_name
= "generator expression";
428 expr_name
= "yield expression";
431 expr_name
= "list comprehension";
436 expr_name
= "literal";
439 expr_name
= "comparison";
445 expr_name
= "conditional expression";
448 PyErr_Format(PyExc_SystemError
,
449 "unexpected expression in assignment %d (line %d)",
453 /* Check for error string set by switch */
456 PyOS_snprintf(buf
, sizeof(buf
),
458 ctx
== Store
? "assign to" : "delete",
460 return ast_error(n
, buf
);
463 /* If the LHS is a list or tuple, we need to set the assignment
464 context for all the contained elements.
469 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
470 if (!set_context(c
, (expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
478 ast_for_augassign(struct compiling
*c
, const node
*n
)
488 if (STR(n
)[1] == '/')
505 if (STR(n
)[1] == '*')
510 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
511 return (operator_ty
)0;
516 ast_for_comp_op(struct compiling
*c
, const node
*n
)
518 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
529 case EQEQUAL
: /* == */
538 if (strcmp(STR(n
), "in") == 0)
540 if (strcmp(STR(n
), "is") == 0)
543 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
548 else if (NCH(n
) == 2) {
549 /* handle "not in" and "is not" */
550 switch (TYPE(CHILD(n
, 0))) {
552 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
554 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
557 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
558 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
562 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
568 seq_for_testlist(struct compiling
*c
, const node
*n
)
570 /* testlist: test (',' test)* [','] */
574 assert(TYPE(n
) == testlist
||
575 TYPE(n
) == listmaker
||
576 TYPE(n
) == testlist_gexp
||
577 TYPE(n
) == testlist_safe
||
578 TYPE(n
) == testlist1
);
580 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
584 for (i
= 0; i
< NCH(n
); i
+= 2) {
585 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
587 expression
= ast_for_expr(c
, CHILD(n
, i
));
591 assert(i
/ 2 < seq
->size
);
592 asdl_seq_SET(seq
, i
/ 2, expression
);
598 compiler_complex_args(struct compiling
*c
, const node
*n
)
600 int i
, len
= (NCH(n
) + 1) / 2;
602 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
606 /* fpdef: NAME | '(' fplist ')'
607 fplist: fpdef (',' fpdef)* [',']
610 for (i
= 0; i
< len
; i
++) {
612 const node
*fpdef_node
= CHILD(n
, 2*i
);
616 /* fpdef_node is either a NAME or an fplist */
617 child
= CHILD(fpdef_node
, 0);
618 if (TYPE(child
) == NAME
) {
619 if (!forbidden_check(c
, n
, STR(child
)))
621 arg_id
= NEW_IDENTIFIER(child
);
624 arg
= Name(arg_id
, Store
, LINENO(child
), child
->n_col_offset
,
628 assert(TYPE(fpdef_node
) == fpdef
);
629 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
630 child
= CHILD(fpdef_node
, 1);
631 assert(TYPE(child
) == fplist
);
632 /* NCH == 1 means we have (x), we need to elide the extra parens */
633 if (NCH(child
) == 1) {
634 fpdef_node
= CHILD(child
, 0);
635 assert(TYPE(fpdef_node
) == fpdef
);
638 arg
= compiler_complex_args(c
, child
);
640 asdl_seq_SET(args
, i
, arg
);
643 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
644 if (!set_context(c
, result
, Store
, n
))
650 /* Create AST for argument list. */
653 ast_for_arguments(struct compiling
*c
, const node
*n
)
655 /* parameters: '(' [varargslist] ')'
656 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
657 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
659 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
660 asdl_seq
*args
, *defaults
;
661 identifier vararg
= NULL
, kwarg
= NULL
;
664 if (TYPE(n
) == parameters
) {
665 if (NCH(n
) == 2) /* () as argument list */
666 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
671 /* first count the number of normal args & defaults */
672 for (i
= 0; i
< NCH(n
); i
++) {
674 if (TYPE(ch
) == fpdef
)
676 if (TYPE(ch
) == EQUAL
)
679 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
681 return NULL
; /* Don't need to goto error; no objects allocated */
682 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
683 if (!defaults
&& n_defaults
)
684 return NULL
; /* Don't need to goto error; no objects allocated */
686 /* fpdef: NAME | '(' fplist ')'
687 fplist: fpdef (',' fpdef)* [',']
690 j
= 0; /* index for defaults */
691 k
= 0; /* index for args */
697 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
698 anything other than EQUAL or a comma? */
699 /* XXX Should NCH(n) check be made a separate check? */
700 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
701 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
704 assert(defaults
!= NULL
);
705 asdl_seq_SET(defaults
, j
++, expression
);
709 else if (found_default
) {
711 "non-default argument follows default argument");
716 /* def foo((x)): is not complex, special case. */
718 /* We have complex arguments, setup for unpacking. */
719 if (Py_Py3kWarningFlag
&& !ast_warn(c
, ch
,
720 "tuple parameter unpacking has been removed in 3.x"))
722 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
723 if (!asdl_seq_GET(args
, k
-1))
726 /* def foo((x)): setup for checking NAME below. */
727 /* Loop because there can be many parens and tuple
728 unpacking mixed in. */
730 assert(TYPE(ch
) == fpdef
);
734 if (TYPE(CHILD(ch
, 0)) == NAME
) {
737 if (!forbidden_check(c
, n
, STR(CHILD(ch
, 0))))
739 id
= NEW_IDENTIFIER(CHILD(ch
, 0));
742 name
= Name(id
, Param
, LINENO(ch
), ch
->n_col_offset
,
746 asdl_seq_SET(args
, k
++, name
);
749 i
+= 2; /* the name and the comma */
752 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
754 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
760 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
762 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
768 PyErr_Format(PyExc_SystemError
,
769 "unexpected node in varargslist: %d @ %d",
775 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
784 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
788 int lineno
, col_offset
;
794 col_offset
= n
->n_col_offset
;
796 id
= NEW_IDENTIFIER(CHILD(n
, 0));
799 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
803 for (i
= 2; i
< NCH(n
); i
+=2) {
804 id
= NEW_IDENTIFIER(CHILD(n
, i
));
807 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
816 ast_for_decorator(struct compiling
*c
, const node
*n
)
818 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
823 REQ(CHILD(n
, 0), AT
);
824 REQ(RCHILD(n
, -1), NEWLINE
);
826 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
830 if (NCH(n
) == 3) { /* No arguments */
834 else if (NCH(n
) == 5) { /* Call with no arguments */
835 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
836 n
->n_col_offset
, c
->c_arena
);
842 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
852 ast_for_decorators(struct compiling
*c
, const node
*n
)
854 asdl_seq
* decorator_seq
;
859 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
863 for (i
= 0; i
< NCH(n
); i
++) {
864 d
= ast_for_decorator(c
, CHILD(n
, i
));
867 asdl_seq_SET(decorator_seq
, i
, d
);
869 return decorator_seq
;
873 ast_for_funcdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
875 /* funcdef: 'def' NAME parameters ':' suite */
883 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
886 else if (!forbidden_check(c
, CHILD(n
, name_i
), STR(CHILD(n
, name_i
))))
888 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
891 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
895 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
896 n
->n_col_offset
, c
->c_arena
);
900 ast_for_decorated(struct compiling
*c
, const node
*n
)
902 /* decorated: decorators (classdef | funcdef) */
903 stmt_ty thing
= NULL
;
904 asdl_seq
*decorator_seq
= NULL
;
908 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
912 assert(TYPE(CHILD(n
, 1)) == funcdef
||
913 TYPE(CHILD(n
, 1)) == classdef
);
915 if (TYPE(CHILD(n
, 1)) == funcdef
) {
916 thing
= ast_for_funcdef(c
, CHILD(n
, 1), decorator_seq
);
917 } else if (TYPE(CHILD(n
, 1)) == classdef
) {
918 thing
= ast_for_classdef(c
, CHILD(n
, 1), decorator_seq
);
920 /* we count the decorators in when talking about the class' or
921 function's line number */
923 thing
->lineno
= LINENO(n
);
924 thing
->col_offset
= n
->n_col_offset
;
930 ast_for_lambdef(struct compiling
*c
, const node
*n
)
932 /* lambdef: 'lambda' [varargslist] ':' test */
937 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
940 expression
= ast_for_expr(c
, CHILD(n
, 2));
945 args
= ast_for_arguments(c
, CHILD(n
, 1));
948 expression
= ast_for_expr(c
, CHILD(n
, 3));
953 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
957 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
959 /* test: or_test 'if' or_test 'else' test */
960 expr_ty expression
, body
, orelse
;
963 body
= ast_for_expr(c
, CHILD(n
, 0));
966 expression
= ast_for_expr(c
, CHILD(n
, 2));
969 orelse
= ast_for_expr(c
, CHILD(n
, 4));
972 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
976 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
977 so there is only a single version. Possibly for loops can also re-use
981 /* Count the number of 'for' loop in a list comprehension.
983 Helper for ast_for_listcomp().
987 count_list_fors(struct compiling
*c
, const node
*n
)
990 node
*ch
= CHILD(n
, 1);
1002 if (TYPE(ch
) == list_for
)
1003 goto count_list_for
;
1004 else if (TYPE(ch
) == list_if
) {
1007 goto count_list_iter
;
1013 /* Should never be reached */
1014 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
1018 /* Count the number of 'if' statements in a list comprehension.
1020 Helper for ast_for_listcomp().
1024 count_list_ifs(struct compiling
*c
, const node
*n
)
1030 if (TYPE(CHILD(n
, 0)) == list_for
)
1038 goto count_list_iter
;
1042 ast_for_listcomp(struct compiling
*c
, const node
*n
)
1044 /* listmaker: test ( list_for | (',' test)* [','] )
1045 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1046 list_iter: list_for | list_if
1047 list_if: 'if' test [list_iter]
1048 testlist_safe: test [(',' test)+ [',']]
1051 asdl_seq
*listcomps
;
1058 elt
= ast_for_expr(c
, CHILD(n
, 0));
1062 n_fors
= count_list_fors(c
, n
);
1066 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
1071 for (i
= 0; i
< n_fors
; i
++) {
1072 comprehension_ty lc
;
1079 for_ch
= CHILD(ch
, 1);
1080 t
= ast_for_exprlist(c
, for_ch
, Store
);
1083 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
1087 /* Check the # of children rather than the length of t, since
1088 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1090 first
= (expr_ty
)asdl_seq_GET(t
, 0);
1091 if (NCH(for_ch
) == 1)
1092 lc
= comprehension(first
, expression
, NULL
, c
->c_arena
);
1094 lc
= comprehension(Tuple(t
, Store
, first
->lineno
, first
->col_offset
,
1096 expression
, NULL
, c
->c_arena
);
1103 expr_ty list_for_expr
;
1106 n_ifs
= count_list_ifs(c
, ch
);
1110 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1114 for (j
= 0; j
< n_ifs
; j
++) {
1119 list_for_expr
= ast_for_expr(c
, CHILD(ch
, 1));
1123 asdl_seq_SET(ifs
, j
, list_for_expr
);
1127 /* on exit, must guarantee that ch is a list_for */
1128 if (TYPE(ch
) == list_iter
)
1132 asdl_seq_SET(listcomps
, i
, lc
);
1135 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1138 /* Count the number of 'for' loops in a generator expression.
1140 Helper for ast_for_genexp().
1144 count_gen_fors(struct compiling
*c
, const node
*n
)
1147 node
*ch
= CHILD(n
, 1);
1159 if (TYPE(ch
) == gen_for
)
1161 else if (TYPE(ch
) == gen_if
) {
1164 goto count_gen_iter
;
1170 /* Should never be reached */
1171 PyErr_SetString(PyExc_SystemError
,
1172 "logic error in count_gen_fors");
1176 /* Count the number of 'if' statements in a generator expression.
1178 Helper for ast_for_genexp().
1182 count_gen_ifs(struct compiling
*c
, const node
*n
)
1188 if (TYPE(CHILD(n
, 0)) == gen_for
)
1199 /* TODO(jhylton): Combine with list comprehension code? */
1201 ast_for_genexp(struct compiling
*c
, const node
*n
)
1203 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1204 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1210 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1213 elt
= ast_for_expr(c
, CHILD(n
, 0));
1217 n_fors
= count_gen_fors(c
, n
);
1221 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1226 for (i
= 0; i
< n_fors
; i
++) {
1227 comprehension_ty ge
;
1229 expr_ty expression
, first
;
1234 for_ch
= CHILD(ch
, 1);
1235 t
= ast_for_exprlist(c
, for_ch
, Store
);
1238 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1242 /* Check the # of children rather than the length of t, since
1243 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1244 first
= (expr_ty
)asdl_seq_GET(t
, 0);
1245 if (NCH(for_ch
) == 1)
1246 ge
= comprehension(first
, expression
, NULL
, c
->c_arena
);
1248 ge
= comprehension(Tuple(t
, Store
, first
->lineno
, first
->col_offset
,
1250 expression
, NULL
, c
->c_arena
);
1260 n_ifs
= count_gen_ifs(c
, ch
);
1264 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1268 for (j
= 0; j
< n_ifs
; j
++) {
1273 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1276 asdl_seq_SET(ifs
, j
, expression
);
1280 /* on exit, must guarantee that ch is a gen_for */
1281 if (TYPE(ch
) == gen_iter
)
1285 asdl_seq_SET(genexps
, i
, ge
);
1288 return GeneratorExp(elt
, genexps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1292 ast_for_atom(struct compiling
*c
, const node
*n
)
1294 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1295 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1297 node
*ch
= CHILD(n
, 0);
1301 /* All names start in Load context, but may later be
1303 PyObject
*name
= NEW_IDENTIFIER(ch
);
1306 return Name(name
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1309 PyObject
*str
= parsestrplus(c
, n
);
1311 #ifdef Py_USING_UNICODE
1312 if (PyErr_ExceptionMatches(PyExc_UnicodeError
)){
1313 PyObject
*type
, *value
, *tback
, *errstr
;
1314 PyErr_Fetch(&type
, &value
, &tback
);
1315 errstr
= PyObject_Str(value
);
1319 s
= PyString_AsString(errstr
);
1320 PyOS_snprintf(buf
, sizeof(buf
), "(unicode error) %s", s
);
1324 ast_error(n
, "(unicode error) unknown error");
1333 PyArena_AddPyObject(c
->c_arena
, str
);
1334 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1337 PyObject
*pynum
= parsenumber(c
, STR(ch
));
1341 PyArena_AddPyObject(c
->c_arena
, pynum
);
1342 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1344 case LPAR
: /* some parenthesized expressions */
1347 if (TYPE(ch
) == RPAR
)
1348 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1350 if (TYPE(ch
) == yield_expr
)
1351 return ast_for_expr(c
, ch
);
1353 return ast_for_testlist_gexp(c
, ch
);
1354 case LSQB
: /* list (or list comprehension) */
1357 if (TYPE(ch
) == RSQB
)
1358 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1361 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1362 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1366 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1369 return ast_for_listcomp(c
, ch
);
1371 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1373 asdl_seq
*keys
, *values
;
1376 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1377 keys
= asdl_seq_new(size
, c
->c_arena
);
1381 values
= asdl_seq_new(size
, c
->c_arena
);
1385 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1388 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1392 asdl_seq_SET(keys
, i
/ 4, expression
);
1394 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1398 asdl_seq_SET(values
, i
/ 4, expression
);
1400 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1402 case BACKQUOTE
: { /* repr */
1404 if (Py_Py3kWarningFlag
&&
1405 !ast_warn(c
, n
, "backquote not supported in 3.x; use repr()"))
1407 expression
= ast_for_testlist(c
, CHILD(n
, 1));
1411 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1414 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1420 ast_for_slice(struct compiling
*c
, const node
*n
)
1423 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1428 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1432 if (TYPE(ch
) == DOT
)
1433 return Ellipsis(c
->c_arena
);
1435 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1436 /* 'step' variable hold no significance in terms of being used over
1438 step
= ast_for_expr(c
, ch
);
1442 return Index(step
, c
->c_arena
);
1445 if (TYPE(ch
) == test
) {
1446 lower
= ast_for_expr(c
, ch
);
1451 /* If there's an upper bound it's in the second or third position. */
1452 if (TYPE(ch
) == COLON
) {
1454 node
*n2
= CHILD(n
, 1);
1456 if (TYPE(n2
) == test
) {
1457 upper
= ast_for_expr(c
, n2
);
1462 } else if (NCH(n
) > 2) {
1463 node
*n2
= CHILD(n
, 2);
1465 if (TYPE(n2
) == test
) {
1466 upper
= ast_for_expr(c
, n2
);
1472 ch
= CHILD(n
, NCH(n
) - 1);
1473 if (TYPE(ch
) == sliceop
) {
1476 This is an extended slice (ie "x[::]") with no expression in the
1477 step field. We set this literally to "None" in order to
1478 disambiguate it from x[:]. (The interpreter might have to call
1479 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1481 identifier none
= new_identifier("None", c
->c_arena
);
1485 step
= Name(none
, Load
, LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1490 if (TYPE(ch
) == test
) {
1491 step
= ast_for_expr(c
, ch
);
1498 return Slice(lower
, upper
, step
, c
->c_arena
);
1502 ast_for_binop(struct compiling
*c
, const node
*n
)
1504 /* Must account for a sequence of expressions.
1505 How should A op B op C by represented?
1506 BinOp(BinOp(A, op, B), op, C).
1510 expr_ty expr1
, expr2
, result
;
1511 operator_ty newoperator
;
1513 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1517 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1521 newoperator
= get_operator(CHILD(n
, 1));
1525 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1530 nops
= (NCH(n
) - 1) / 2;
1531 for (i
= 1; i
< nops
; i
++) {
1532 expr_ty tmp_result
, tmp
;
1533 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1535 newoperator
= get_operator(next_oper
);
1539 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1543 tmp_result
= BinOp(result
, newoperator
, tmp
,
1544 LINENO(next_oper
), next_oper
->n_col_offset
,
1548 result
= tmp_result
;
1554 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1556 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1557 subscriptlist: subscript (',' subscript)* [',']
1558 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1561 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1563 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1564 n
->n_col_offset
, c
->c_arena
);
1566 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1568 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1569 PyObject
*attr_id
= NEW_IDENTIFIER(CHILD(n
, 1));
1572 return Attribute(left_expr
, attr_id
, Load
,
1573 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1576 REQ(CHILD(n
, 0), LSQB
);
1577 REQ(CHILD(n
, 2), RSQB
);
1580 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1583 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1587 /* The grammar is ambiguous here. The ambiguity is resolved
1588 by treating the sequence as a tuple literal if there are
1595 asdl_seq
*slices
, *elts
;
1596 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1599 for (j
= 0; j
< NCH(n
); j
+= 2) {
1600 slc
= ast_for_slice(c
, CHILD(n
, j
));
1603 if (slc
->kind
!= Index_kind
)
1605 asdl_seq_SET(slices
, j
/ 2, slc
);
1608 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1609 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1611 /* extract Index values and put them in a Tuple */
1612 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1615 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1616 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1617 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1618 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1620 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1623 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1624 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1630 ast_for_factor(struct compiling
*c
, const node
*n
)
1632 node
*pfactor
, *ppower
, *patom
, *pnum
;
1635 /* If the unary - operator is applied to a constant, don't generate
1636 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1637 constant. The peephole optimizer already does something like
1638 this but it doesn't handle the case where the constant is
1639 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1642 if (TYPE(CHILD(n
, 0)) == MINUS
&&
1644 TYPE((pfactor
= CHILD(n
, 1))) == factor
&&
1645 NCH(pfactor
) == 1 &&
1646 TYPE((ppower
= CHILD(pfactor
, 0))) == power
&&
1648 TYPE((patom
= CHILD(ppower
, 0))) == atom
&&
1649 TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1650 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1654 strcpy(s
+ 1, STR(pnum
));
1655 PyObject_FREE(STR(pnum
));
1657 return ast_for_atom(c
, patom
);
1660 expression
= ast_for_expr(c
, CHILD(n
, 1));
1664 switch (TYPE(CHILD(n
, 0))) {
1666 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1669 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1672 return UnaryOp(Invert
, expression
, LINENO(n
),
1673 n
->n_col_offset
, c
->c_arena
);
1675 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1681 ast_for_power(struct compiling
*c
, const node
*n
)
1683 /* power: atom trailer* ('**' factor)*
1688 e
= ast_for_atom(c
, CHILD(n
, 0));
1693 for (i
= 1; i
< NCH(n
); i
++) {
1694 node
*ch
= CHILD(n
, i
);
1695 if (TYPE(ch
) != trailer
)
1697 tmp
= ast_for_trailer(c
, ch
, e
);
1700 tmp
->lineno
= e
->lineno
;
1701 tmp
->col_offset
= e
->col_offset
;
1704 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1705 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1708 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1716 /* Do not name a variable 'expr'! Will cause a compile error.
1720 ast_for_expr(struct compiling
*c
, const node
*n
)
1722 /* handle the full range of simple expressions
1723 test: or_test ['if' or_test 'else' test] | lambdef
1724 or_test: and_test ('or' and_test)*
1725 and_test: not_test ('and' not_test)*
1726 not_test: 'not' not_test | comparison
1727 comparison: expr (comp_op expr)*
1728 expr: xor_expr ('|' xor_expr)*
1729 xor_expr: and_expr ('^' and_expr)*
1730 and_expr: shift_expr ('&' shift_expr)*
1731 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1732 arith_expr: term (('+'|'-') term)*
1733 term: factor (('*'|'/'|'%'|'//') factor)*
1734 factor: ('+'|'-'|'~') factor | power
1735 power: atom trailer* ('**' factor)*
1737 As well as modified versions that exist for backward compatibility,
1738 to explicitly allow:
1739 [ x for x in lambda: 0, lambda: 1 ]
1740 (which would be ambiguous without these extra rules)
1742 old_test: or_test | old_lambdef
1743 old_lambdef: 'lambda' [vararglist] ':' old_test
1754 if (TYPE(CHILD(n
, 0)) == lambdef
||
1755 TYPE(CHILD(n
, 0)) == old_lambdef
)
1756 return ast_for_lambdef(c
, CHILD(n
, 0));
1757 else if (NCH(n
) > 1)
1758 return ast_for_ifexpr(c
, n
);
1766 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1769 for (i
= 0; i
< NCH(n
); i
+= 2) {
1770 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1773 asdl_seq_SET(seq
, i
/ 2, e
);
1775 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1776 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1778 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1779 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1786 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1790 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1802 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1805 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1809 for (i
= 1; i
< NCH(n
); i
+= 2) {
1810 cmpop_ty newoperator
;
1812 newoperator
= ast_for_comp_op(c
, CHILD(n
, i
));
1817 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1822 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1823 asdl_seq_SET(cmps
, i
/ 2, expression
);
1825 expression
= ast_for_expr(c
, CHILD(n
, 0));
1830 return Compare(expression
, ops
, cmps
, LINENO(n
),
1831 n
->n_col_offset
, c
->c_arena
);
1835 /* The next five cases all handle BinOps. The main body of code
1836 is the same in each case, but the switch turned inside out to
1837 reuse the code for each type of operator.
1849 return ast_for_binop(c
, n
);
1853 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1857 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1864 return ast_for_factor(c
, n
);
1866 return ast_for_power(c
, n
);
1868 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1871 /* should never get here unless if error is set */
1876 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1879 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1881 argument: [test '='] test [gen_for] # Really [keyword '='] test
1884 int i
, nargs
, nkeywords
, ngens
;
1887 expr_ty vararg
= NULL
, kwarg
= NULL
;
1894 for (i
= 0; i
< NCH(n
); i
++) {
1895 node
*ch
= CHILD(n
, i
);
1896 if (TYPE(ch
) == argument
) {
1899 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1905 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1906 ast_error(n
, "Generator expression must be parenthesized "
1907 "if not sole argument");
1911 if (nargs
+ nkeywords
+ ngens
> 255) {
1912 ast_error(n
, "more than 255 arguments");
1916 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1919 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1924 for (i
= 0; i
< NCH(n
); i
++) {
1925 node
*ch
= CHILD(n
, i
);
1926 if (TYPE(ch
) == argument
) {
1930 ast_error(CHILD(ch
, 0),
1931 "non-keyword arg after keyword arg");
1935 ast_error(CHILD(ch
, 0),
1936 "only named arguments may follow *expression");
1939 e
= ast_for_expr(c
, CHILD(ch
, 0));
1942 asdl_seq_SET(args
, nargs
++, e
);
1944 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1945 e
= ast_for_genexp(c
, ch
);
1948 asdl_seq_SET(args
, nargs
++, e
);
1956 /* CHILD(ch, 0) is test, but must be an identifier? */
1957 e
= ast_for_expr(c
, CHILD(ch
, 0));
1960 /* f(lambda x: x[0] = 3) ends up getting parsed with
1961 * LHS test = lambda x: x[0], and RHS test = 3.
1962 * SF bug 132313 points out that complaining about a keyword
1963 * then is very confusing.
1965 if (e
->kind
== Lambda_kind
) {
1966 ast_error(CHILD(ch
, 0),
1967 "lambda cannot contain assignment");
1969 } else if (e
->kind
!= Name_kind
) {
1970 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1974 if (!forbidden_check(c
, CHILD(ch
, 0), PyBytes_AS_STRING(key
)))
1976 for (k
= 0; k
< nkeywords
; k
++) {
1977 tmp
= PyString_AS_STRING(
1978 ((keyword_ty
)asdl_seq_GET(keywords
, k
))->arg
);
1979 if (!strcmp(tmp
, PyString_AS_STRING(key
))) {
1980 ast_error(CHILD(ch
, 0), "keyword argument repeated");
1984 e
= ast_for_expr(c
, CHILD(ch
, 2));
1987 kw
= keyword(key
, e
, c
->c_arena
);
1990 asdl_seq_SET(keywords
, nkeywords
++, kw
);
1993 else if (TYPE(ch
) == STAR
) {
1994 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
1999 else if (TYPE(ch
) == DOUBLESTAR
) {
2000 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
2007 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
,
2008 func
->col_offset
, c
->c_arena
);
2012 ast_for_testlist(struct compiling
*c
, const node
* n
)
2014 /* testlist_gexp: test (',' test)* [','] */
2015 /* testlist: test (',' test)* [','] */
2016 /* testlist_safe: test (',' test)+ [','] */
2017 /* testlist1: test (',' test)* */
2019 if (TYPE(n
) == testlist_gexp
) {
2021 assert(TYPE(CHILD(n
, 1)) != gen_for
);
2024 assert(TYPE(n
) == testlist
||
2025 TYPE(n
) == testlist_safe
||
2026 TYPE(n
) == testlist1
);
2029 return ast_for_expr(c
, CHILD(n
, 0));
2031 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
2034 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2039 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
2041 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2042 /* argument: test [ gen_for ] */
2043 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
2044 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
2045 return ast_for_genexp(c
, n
);
2046 return ast_for_testlist(c
, n
);
2049 /* like ast_for_testlist() but returns a sequence */
2051 ast_for_class_bases(struct compiling
*c
, const node
* n
)
2053 /* testlist: test (',' test)* [','] */
2058 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
2061 base
= ast_for_expr(c
, CHILD(n
, 0));
2064 asdl_seq_SET(bases
, 0, base
);
2068 return seq_for_testlist(c
, n
);
2072 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
2075 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2076 | ('=' (yield_expr|testlist))*)
2077 testlist: test (',' test)* [',']
2078 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2079 | '<<=' | '>>=' | '**=' | '//='
2080 test: ... here starts the operator precendence dance
2084 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
2088 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2090 else if (TYPE(CHILD(n
, 1)) == augassign
) {
2091 expr_ty expr1
, expr2
;
2092 operator_ty newoperator
;
2093 node
*ch
= CHILD(n
, 0);
2095 expr1
= ast_for_testlist(c
, ch
);
2098 if(!set_context(c
, expr1
, Store
, ch
))
2100 /* set_context checks that most expressions are not the left side.
2101 Augmented assignments can only have a name, a subscript, or an
2102 attribute on the left, though, so we have to explicitly check for
2104 switch (expr1
->kind
) {
2106 case Attribute_kind
:
2107 case Subscript_kind
:
2110 ast_error(ch
, "illegal expression for augmented assignment");
2115 if (TYPE(ch
) == testlist
)
2116 expr2
= ast_for_testlist(c
, ch
);
2118 expr2
= ast_for_expr(c
, ch
);
2122 newoperator
= ast_for_augassign(c
, CHILD(n
, 1));
2126 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
2135 /* a normal assignment */
2136 REQ(CHILD(n
, 1), EQUAL
);
2137 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2140 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2142 node
*ch
= CHILD(n
, i
);
2143 if (TYPE(ch
) == yield_expr
) {
2144 ast_error(ch
, "assignment to yield expression not possible");
2147 e
= ast_for_testlist(c
, ch
);
2149 /* set context to assign */
2153 if (!set_context(c
, e
, Store
, CHILD(n
, i
)))
2156 asdl_seq_SET(targets
, i
/ 2, e
);
2158 value
= CHILD(n
, NCH(n
) - 1);
2159 if (TYPE(value
) == testlist
)
2160 expression
= ast_for_testlist(c
, value
);
2162 expression
= ast_for_expr(c
, value
);
2165 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
,
2171 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
2173 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2174 | '>>' test [ (',' test)+ [','] ] )
2176 expr_ty dest
= NULL
, expression
;
2177 asdl_seq
*seq
= NULL
;
2179 int i
, j
, values_count
, start
= 1;
2182 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2183 dest
= ast_for_expr(c
, CHILD(n
, 2));
2188 values_count
= (NCH(n
) + 1 - start
) / 2;
2190 seq
= asdl_seq_new(values_count
, c
->c_arena
);
2193 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2194 expression
= ast_for_expr(c
, CHILD(n
, i
));
2197 asdl_seq_SET(seq
, j
, expression
);
2200 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2201 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2205 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2213 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2216 for (i
= 0; i
< NCH(n
); i
+= 2) {
2217 e
= ast_for_expr(c
, CHILD(n
, i
));
2220 asdl_seq_SET(seq
, i
/ 2, e
);
2221 if (context
&& !set_context(c
, e
, context
, CHILD(n
, i
)))
2228 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2230 asdl_seq
*expr_list
;
2232 /* del_stmt: 'del' exprlist */
2235 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2238 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2242 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2245 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2248 continue_stmt: 'continue'
2249 return_stmt: 'return' [testlist]
2250 yield_stmt: yield_expr
2251 yield_expr: 'yield' testlist
2252 raise_stmt: 'raise' [test [',' test [',' test]]]
2260 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2262 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2263 case yield_stmt
: { /* will reduce to yield_expr */
2264 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2267 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2271 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2273 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2276 return Return(expression
, LINENO(n
), n
->n_col_offset
,
2281 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
,
2283 else if (NCH(ch
) == 2) {
2284 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2287 return Raise(expression
, NULL
, NULL
, LINENO(n
),
2288 n
->n_col_offset
, c
->c_arena
);
2290 else if (NCH(ch
) == 4) {
2291 expr_ty expr1
, expr2
;
2293 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2296 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2300 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
,
2303 else if (NCH(ch
) == 6) {
2304 expr_ty expr1
, expr2
, expr3
;
2306 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2309 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2312 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2316 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
,
2320 PyErr_Format(PyExc_SystemError
,
2321 "unexpected flow_stmt: %d", TYPE(ch
));
2325 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2330 alias_for_import_name(struct compiling
*c
, const node
*n
, int store
)
2333 import_as_name: NAME ['as' NAME]
2334 dotted_as_name: dotted_name ['as' NAME]
2335 dotted_name: NAME ('.' NAME)*
2337 PyObject
*str
, *name
;
2341 case import_as_name
: {
2342 node
*name_node
= CHILD(n
, 0);
2345 node
*str_node
= CHILD(n
, 2);
2346 if (store
&& !forbidden_check(c
, str_node
, STR(str_node
)))
2348 str
= NEW_IDENTIFIER(str_node
);
2353 if (!forbidden_check(c
, name_node
, STR(name_node
)))
2356 name
= NEW_IDENTIFIER(name_node
);
2359 return alias(name
, str
, c
->c_arena
);
2361 case dotted_as_name
:
2367 node
*asname_node
= CHILD(n
, 2);
2368 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0), 0);
2372 if (!forbidden_check(c
, asname_node
, STR(asname_node
)))
2374 a
->asname
= NEW_IDENTIFIER(asname_node
);
2382 node
*name_node
= CHILD(n
, 0);
2383 if (store
&& !forbidden_check(c
, name_node
, STR(name_node
)))
2385 name
= NEW_IDENTIFIER(name_node
);
2388 return alias(name
, NULL
, c
->c_arena
);
2391 /* Create a string of the form "a.b.c" */
2397 for (i
= 0; i
< NCH(n
); i
+= 2)
2398 /* length of string plus one for the dot */
2399 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2400 len
--; /* the last name doesn't have a dot */
2401 str
= PyString_FromStringAndSize(NULL
, len
);
2404 s
= PyString_AS_STRING(str
);
2407 for (i
= 0; i
< NCH(n
); i
+= 2) {
2408 char *sch
= STR(CHILD(n
, i
));
2409 strcpy(s
, STR(CHILD(n
, i
)));
2415 PyString_InternInPlace(&str
);
2416 PyArena_AddPyObject(c
->c_arena
, str
);
2417 return alias(str
, NULL
, c
->c_arena
);
2421 str
= PyString_InternFromString("*");
2422 PyArena_AddPyObject(c
->c_arena
, str
);
2423 return alias(str
, NULL
, c
->c_arena
);
2425 PyErr_Format(PyExc_SystemError
,
2426 "unexpected import name: %d", TYPE(n
));
2430 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2435 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2438 import_stmt: import_name | import_from
2439 import_name: 'import' dotted_as_names
2440 import_from: 'from' ('.'* dotted_name | '.') 'import'
2441 ('*' | '(' import_as_names ')' | import_as_names)
2448 REQ(n
, import_stmt
);
2450 col_offset
= n
->n_col_offset
;
2452 if (TYPE(n
) == import_name
) {
2454 REQ(n
, dotted_as_names
);
2455 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2458 for (i
= 0; i
< NCH(n
); i
+= 2) {
2459 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
), 1);
2462 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2464 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2466 else if (TYPE(n
) == import_from
) {
2469 alias_ty mod
= NULL
;
2470 identifier modname
= NULL
;
2472 /* Count the number of dots (for relative imports) and check for the
2473 optional module name */
2474 for (idx
= 1; idx
< NCH(n
); idx
++) {
2475 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2476 mod
= alias_for_import_name(c
, CHILD(n
, idx
), 0);
2481 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2486 idx
++; /* skip over the 'import' keyword */
2487 switch (TYPE(CHILD(n
, idx
))) {
2489 /* from ... import * */
2494 /* from ... import (x, y, z) */
2495 n
= CHILD(n
, idx
+ 1);
2496 n_children
= NCH(n
);
2498 case import_as_names
:
2499 /* from ... import x, y, z */
2501 n_children
= NCH(n
);
2502 if (n_children
% 2 == 0) {
2503 ast_error(n
, "trailing comma not allowed without"
2504 " surrounding parentheses");
2509 ast_error(n
, "Unexpected node-type in from-import");
2513 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2517 /* handle "from ... import *" special b/c there's no children */
2518 if (TYPE(n
) == STAR
) {
2519 alias_ty import_alias
= alias_for_import_name(c
, n
, 1);
2522 asdl_seq_SET(aliases
, 0, import_alias
);
2525 for (i
= 0; i
< NCH(n
); i
+= 2) {
2526 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
), 1);
2529 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2533 modname
= mod
->name
;
2534 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2537 PyErr_Format(PyExc_SystemError
,
2538 "unknown import statement: starts with command '%s'",
2544 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2546 /* global_stmt: 'global' NAME (',' NAME)* */
2551 REQ(n
, global_stmt
);
2552 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2555 for (i
= 1; i
< NCH(n
); i
+= 2) {
2556 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2559 asdl_seq_SET(s
, i
/ 2, name
);
2561 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2565 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2567 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2568 int n_children
= NCH(n
);
2569 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2570 PyErr_Format(PyExc_SystemError
,
2571 "poorly formed 'exec' statement: %d parts to statement",
2576 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2578 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2581 if (n_children
>= 4) {
2582 globals
= ast_for_expr(c
, CHILD(n
, 3));
2586 if (n_children
== 6) {
2587 locals
= ast_for_expr(c
, CHILD(n
, 5));
2592 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
,
2597 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2599 /* assert_stmt: 'assert' test [',' test] */
2600 REQ(n
, assert_stmt
);
2602 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2605 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
,
2608 else if (NCH(n
) == 4) {
2609 expr_ty expr1
, expr2
;
2611 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2614 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2618 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2620 PyErr_Format(PyExc_SystemError
,
2621 "improper number of parts to 'assert' statement: %d",
2627 ast_for_suite(struct compiling
*c
, const node
*n
)
2629 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2632 int i
, total
, num
, end
, pos
= 0;
2637 total
= num_stmts(n
);
2638 seq
= asdl_seq_new(total
, c
->c_arena
);
2641 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2643 /* simple_stmt always ends with a NEWLINE,
2644 and may have a trailing SEMI
2647 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2649 /* loop by 2 to skip semi-colons */
2650 for (i
= 0; i
< end
; i
+= 2) {
2652 s
= ast_for_stmt(c
, ch
);
2655 asdl_seq_SET(seq
, pos
++, s
);
2659 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2662 num
= num_stmts(ch
);
2664 /* small_stmt or compound_stmt with only one child */
2665 s
= ast_for_stmt(c
, ch
);
2668 asdl_seq_SET(seq
, pos
++, s
);
2673 REQ(ch
, simple_stmt
);
2674 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2675 /* statement terminates with a semi-colon ';' */
2676 if (NCH(CHILD(ch
, j
)) == 0) {
2677 assert((j
+ 1) == NCH(ch
));
2680 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2683 asdl_seq_SET(seq
, pos
++, s
);
2688 assert(pos
== seq
->size
);
2693 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2695 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2704 asdl_seq
*suite_seq
;
2706 expression
= ast_for_expr(c
, CHILD(n
, 1));
2709 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2713 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2717 s
= STR(CHILD(n
, 4));
2718 /* s[2], the third character in the string, will be
2724 asdl_seq
*seq1
, *seq2
;
2726 expression
= ast_for_expr(c
, CHILD(n
, 1));
2729 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2732 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2736 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2739 else if (s
[2] == 'i') {
2740 int i
, n_elif
, has_else
= 0;
2742 asdl_seq
*suite_seq
;
2743 asdl_seq
*orelse
= NULL
;
2744 n_elif
= NCH(n
) - 4;
2745 /* must reference the child n_elif+1 since 'else' token is third,
2746 not fourth, child from the end. */
2747 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2748 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2755 asdl_seq
*suite_seq2
;
2757 orelse
= asdl_seq_new(1, c
->c_arena
);
2760 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2763 suite_seq
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2766 suite_seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2770 asdl_seq_SET(orelse
, 0,
2771 If(expression
, suite_seq
, suite_seq2
,
2772 LINENO(CHILD(n
, NCH(n
) - 6)),
2773 CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2775 /* the just-created orelse handled the last elif */
2779 for (i
= 0; i
< n_elif
; i
++) {
2780 int off
= 5 + (n_elif
- i
- 1) * 4;
2781 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2784 expression
= ast_for_expr(c
, CHILD(n
, off
));
2787 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2791 asdl_seq_SET(newobj
, 0,
2792 If(expression
, suite_seq
, orelse
,
2793 LINENO(CHILD(n
, off
)),
2794 CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2797 expression
= ast_for_expr(c
, CHILD(n
, 1));
2800 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2803 return If(expression
, suite_seq
, orelse
,
2804 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2807 PyErr_Format(PyExc_SystemError
,
2808 "unexpected token in 'if' statement: %s", s
);
2813 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2815 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2820 asdl_seq
*suite_seq
;
2822 expression
= ast_for_expr(c
, CHILD(n
, 1));
2825 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2828 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2831 else if (NCH(n
) == 7) {
2833 asdl_seq
*seq1
, *seq2
;
2835 expression
= ast_for_expr(c
, CHILD(n
, 1));
2838 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2841 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2845 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2849 PyErr_Format(PyExc_SystemError
,
2850 "wrong number of tokens for 'while' statement: %d",
2856 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2858 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2860 expr_ty target
, first
;
2861 const node
*node_target
;
2862 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2866 seq
= ast_for_suite(c
, CHILD(n
, 8));
2871 node_target
= CHILD(n
, 1);
2872 _target
= ast_for_exprlist(c
, node_target
, Store
);
2875 /* Check the # of children rather than the length of _target, since
2876 for x, in ... has 1 element in _target, but still requires a Tuple. */
2877 first
= (expr_ty
)asdl_seq_GET(_target
, 0);
2878 if (NCH(node_target
) == 1)
2881 target
= Tuple(_target
, Store
, first
->lineno
, first
->col_offset
, c
->c_arena
);
2883 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2886 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2890 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2894 static excepthandler_ty
2895 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2897 /* except_clause: 'except' [test [(',' | 'as') test]] */
2898 REQ(exc
, except_clause
);
2901 if (NCH(exc
) == 1) {
2902 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2906 return ExceptHandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
2907 exc
->n_col_offset
, c
->c_arena
);
2909 else if (NCH(exc
) == 2) {
2911 asdl_seq
*suite_seq
;
2913 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2916 suite_seq
= ast_for_suite(c
, body
);
2920 return ExceptHandler(expression
, NULL
, suite_seq
, LINENO(exc
),
2921 exc
->n_col_offset
, c
->c_arena
);
2923 else if (NCH(exc
) == 4) {
2924 asdl_seq
*suite_seq
;
2926 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2929 if (!set_context(c
, e
, Store
, CHILD(exc
, 3)))
2931 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2934 suite_seq
= ast_for_suite(c
, body
);
2938 return ExceptHandler(expression
, e
, suite_seq
, LINENO(exc
),
2939 exc
->n_col_offset
, c
->c_arena
);
2942 PyErr_Format(PyExc_SystemError
,
2943 "wrong number of children for 'except' clause: %d",
2949 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2951 const int nch
= NCH(n
);
2952 int n_except
= (nch
- 3)/3;
2953 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2957 body
= ast_for_suite(c
, CHILD(n
, 2));
2961 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2962 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2963 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2964 /* we can assume it's an "else",
2965 because nch >= 9 for try-else-finally and
2966 it would otherwise have a type of except_clause */
2967 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2973 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2974 if (finally
== NULL
)
2979 /* we can assume it's an "else",
2980 otherwise it would have a type of except_clause */
2981 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2987 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2988 ast_error(n
, "malformed 'try' statement");
2995 /* process except statements to create a try ... except */
2996 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2997 if (handlers
== NULL
)
3000 for (i
= 0; i
< n_except
; i
++) {
3001 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
3002 CHILD(n
, 5 + i
* 3));
3005 asdl_seq_SET(handlers
, i
, e
);
3008 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
3009 n
->n_col_offset
, c
->c_arena
);
3013 /* if a 'finally' is present too, we nest the TryExcept within a
3014 TryFinally to emulate try ... except ... finally */
3015 body
= asdl_seq_new(1, c
->c_arena
);
3018 asdl_seq_SET(body
, 0, except_st
);
3021 /* must be a try ... finally (except clauses are in body, if any exist) */
3022 assert(finally
!= NULL
);
3023 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3026 /* with_item: test ['as' expr] */
3028 ast_for_with_item(struct compiling
*c
, const node
*n
, asdl_seq
*content
)
3030 expr_ty context_expr
, optional_vars
= NULL
;
3033 context_expr
= ast_for_expr(c
, CHILD(n
, 0));
3037 optional_vars
= ast_for_expr(c
, CHILD(n
, 2));
3039 if (!optional_vars
) {
3042 if (!set_context(c
, optional_vars
, Store
, n
)) {
3047 return With(context_expr
, optional_vars
, content
, LINENO(n
),
3048 n
->n_col_offset
, c
->c_arena
);
3051 /* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3053 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
3061 /* process the with items inside-out */
3063 /* the suite of the innermost with item is the suite of the with stmt */
3064 inner
= ast_for_suite(c
, CHILD(n
, i
));
3070 ret
= ast_for_with_item(c
, CHILD(n
, i
), inner
);
3073 /* was this the last item? */
3076 /* if not, wrap the result so far in a new sequence */
3077 inner
= asdl_seq_new(1, c
->c_arena
);
3080 asdl_seq_SET(inner
, 0, ret
);
3087 ast_for_classdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
3089 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3090 PyObject
*classname
;
3091 asdl_seq
*bases
, *s
;
3095 if (!forbidden_check(c
, n
, STR(CHILD(n
, 1))))
3099 s
= ast_for_suite(c
, CHILD(n
, 3));
3102 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3105 return ClassDef(classname
, NULL
, s
, decorator_seq
, LINENO(n
),
3106 n
->n_col_offset
, c
->c_arena
);
3108 /* check for empty base list */
3109 if (TYPE(CHILD(n
,3)) == RPAR
) {
3110 s
= ast_for_suite(c
, CHILD(n
,5));
3113 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3116 return ClassDef(classname
, NULL
, s
, decorator_seq
, LINENO(n
),
3117 n
->n_col_offset
, c
->c_arena
);
3120 /* else handle the base class list */
3121 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
3125 s
= ast_for_suite(c
, CHILD(n
, 6));
3128 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3131 return ClassDef(classname
, bases
, s
, decorator_seq
,
3132 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3136 ast_for_stmt(struct compiling
*c
, const node
*n
)
3138 if (TYPE(n
) == stmt
) {
3139 assert(NCH(n
) == 1);
3142 if (TYPE(n
) == simple_stmt
) {
3143 assert(num_stmts(n
) == 1);
3146 if (TYPE(n
) == small_stmt
) {
3148 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3149 | flow_stmt | import_stmt | global_stmt | exec_stmt
3154 return ast_for_expr_stmt(c
, n
);
3156 return ast_for_print_stmt(c
, n
);
3158 return ast_for_del_stmt(c
, n
);
3160 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3162 return ast_for_flow_stmt(c
, n
);
3164 return ast_for_import_stmt(c
, n
);
3166 return ast_for_global_stmt(c
, n
);
3168 return ast_for_exec_stmt(c
, n
);
3170 return ast_for_assert_stmt(c
, n
);
3172 PyErr_Format(PyExc_SystemError
,
3173 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3179 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3180 | funcdef | classdef | decorated
3182 node
*ch
= CHILD(n
, 0);
3183 REQ(n
, compound_stmt
);
3186 return ast_for_if_stmt(c
, ch
);
3188 return ast_for_while_stmt(c
, ch
);
3190 return ast_for_for_stmt(c
, ch
);
3192 return ast_for_try_stmt(c
, ch
);
3194 return ast_for_with_stmt(c
, ch
);
3196 return ast_for_funcdef(c
, ch
, NULL
);
3198 return ast_for_classdef(c
, ch
, NULL
);
3200 return ast_for_decorated(c
, ch
);
3202 PyErr_Format(PyExc_SystemError
,
3203 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3211 parsenumber(struct compiling
*c
, const char *s
)
3216 #ifndef WITHOUT_COMPLEX
3223 end
= s
+ strlen(s
) - 1;
3224 #ifndef WITHOUT_COMPLEX
3225 imflag
= *end
== 'j' || *end
== 'J';
3227 if (*end
== 'l' || *end
== 'L')
3228 return PyLong_FromString((char *)s
, (char **)0, 0);
3229 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3232 return PyLong_FromString((char *)s
, (char **)0, 0);
3233 return PyInt_FromLong(x
);
3235 /* XXX Huge floats may silently fail */
3236 #ifndef WITHOUT_COMPLEX
3239 PyFPE_START_PROTECT("atof", return 0)
3240 complex.imag
= PyOS_ascii_atof(s
);
3241 PyFPE_END_PROTECT(complex)
3242 return PyComplex_FromCComplex(complex);
3247 PyFPE_START_PROTECT("atof", return 0)
3248 dx
= PyOS_ascii_atof(s
);
3249 PyFPE_END_PROTECT(dx
)
3250 return PyFloat_FromDouble(dx
);
3255 decode_utf8(struct compiling
*c
, const char **sPtr
, const char *end
, char* encoding
)
3257 #ifndef Py_USING_UNICODE
3258 Py_FatalError("decode_utf8 should not be called in this build.");
3263 t
= s
= (char *)*sPtr
;
3264 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3265 while (s
< end
&& (*s
& 0x80)) s
++;
3267 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3270 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3276 #ifdef Py_USING_UNICODE
3278 decode_unicode(struct compiling
*c
, const char *s
, size_t len
, int rawmode
, const char *encoding
)
3284 if (encoding
== NULL
) {
3287 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
3291 /* check for integer overflow */
3292 if (len
> PY_SIZE_MAX
/ 4)
3294 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3295 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
3298 p
= buf
= PyString_AsString(u
);
3308 if (*s
& 0x80) { /* XXX inefficient */
3312 w
= decode_utf8(c
, &s
, end
, "utf-16-be");
3317 r
= PyString_AsString(w
);
3318 rn
= PyString_Size(w
);
3319 assert(rn
% 2 == 0);
3320 for (i
= 0; i
< rn
; i
+= 2) {
3321 sprintf(p
, "\\u%02x%02x",
3335 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3337 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3343 /* s is a Python string literal, including the bracketing quote characters,
3344 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3345 * parsestr parses it, and returns the decoded Python string object.
3348 parsestr(struct compiling
*c
, const char *s
)
3351 int quote
= Py_CHARMASK(*s
);
3354 int unicode
= c
->c_future_unicode
;
3356 if (isalpha(quote
) || quote
== '_') {
3357 if (quote
== 'u' || quote
== 'U') {
3361 if (quote
== 'b' || quote
== 'B') {
3365 if (quote
== 'r' || quote
== 'R') {
3370 if (quote
!= '\'' && quote
!= '\"') {
3371 PyErr_BadInternalCall();
3376 if (len
> INT_MAX
) {
3377 PyErr_SetString(PyExc_OverflowError
,
3378 "string to parse is too long");
3381 if (s
[--len
] != quote
) {
3382 PyErr_BadInternalCall();
3385 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3388 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3389 PyErr_BadInternalCall();
3393 #ifdef Py_USING_UNICODE
3394 if (unicode
|| Py_UnicodeFlag
) {
3395 return decode_unicode(c
, s
, len
, rawmode
, c
->c_encoding
);
3398 need_encoding
= (c
->c_encoding
!= NULL
&&
3399 strcmp(c
->c_encoding
, "utf-8") != 0 &&
3400 strcmp(c
->c_encoding
, "iso-8859-1") != 0);
3401 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3402 if (need_encoding
) {
3403 #ifndef Py_USING_UNICODE
3404 /* This should not happen - we never see any other
3407 "cannot deal with encodings in this build.");
3409 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3412 v
= PyUnicode_AsEncodedString(u
, c
->c_encoding
, NULL
);
3417 return PyString_FromStringAndSize(s
, len
);
3421 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3422 need_encoding
? c
->c_encoding
: NULL
);
3425 /* Build a Python string object out of a STRING atom. This takes care of
3426 * compile-time literal catenation, calling parsestr() on each piece, and
3427 * pasting the intermediate results together.
3430 parsestrplus(struct compiling
*c
, const node
*n
)
3434 REQ(CHILD(n
, 0), STRING
);
3435 if ((v
= parsestr(c
, STR(CHILD(n
, 0)))) != NULL
) {
3436 /* String literal concatenation */
3437 for (i
= 1; i
< NCH(n
); i
++) {
3439 s
= parsestr(c
, STR(CHILD(n
, i
)));
3442 if (PyString_Check(v
) && PyString_Check(s
)) {
3443 PyString_ConcatAndDel(&v
, s
);
3447 #ifdef Py_USING_UNICODE
3449 PyObject
*temp
= PyUnicode_Concat(v
, s
);